The power of - Days 2020 in Krakopsztxa/talks/lambda-20.pdf · Typingdisciplines C staticbutweak...

32
The power of Π λ Days 2020 in Krakow Thorsten Altenkirch Functional Programming Laboratory School of Computer Science University of Nottingham February 19, 2020 Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 1 / 32

Transcript of The power of - Days 2020 in Krakopsztxa/talks/lambda-20.pdf · Typingdisciplines C staticbutweak...

  • The power of Πλ Days 2020 in Krakow

    Thorsten Altenkirch

    Functional Programming LaboratorySchool of Computer ScienceUniversity of Nottingham

    February 19, 2020

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 1 / 32

  • Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 2 / 32

  • Typing disciplines

    C static but weakJava, C# static / dynamic but strongPython, Scheme dynamic and strongHaskell, SML static and strong

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 3 / 32

  • Haskell’s type systemis not up to the job!Because it lacks dependent types.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 4 / 32

  • What are dependent types?

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 5 / 32

  • Conor McBride: Winging It, 2000?

    The established social order

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 6 / 32

  • Proletarian revolution: do the types run away in terror?

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 7 / 32

  • No! They’re overjoyed at their new articulacy.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 8 / 32

  • Π-types

    The title of this talk was stolen from a ICFP 08 paper byNicolas Oury and Wouter Swierstra

    zeroes : (n : N) → Vec N nzeroes zero = []zeroes (suc n) = 0 :: zeroes n

    Dependent function type, also called Π-type.

    Πn : NVec A n

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 9 / 32

  • Why is it called Π-type?

    Fin n = {0 , 1 , . . n-1}f : Fin 3 → Nf 0 = 2f 1 = 3f 2 = 4

    (x : Fin 3) → Fin (f x) = Πi

  • What is the power of Π?

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 11 / 32

  • Good programs that can’t be typed without Π

    printf : (s : String) → Args sArgs : String → SetArgs "%s" :: s = String → Args sArgs "%d" :: s = Int → Args sArgs c :: s = Args sArgs "" = IO >

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 12 / 32

  • Pre-Newtonian dependent types

    ObjectionUsing data kinds we can represent types like this in Haskell.

    Yes, but only if the format string is static.What if it is part of a scheme that is computed internally?

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 13 / 32

  • The scheme is part of the data

    Scheme : Set

    Data : Scheme → Setrecord DataBase : Set wherefield

    scheme : Schemecontent : Data scheme

    The power of Σ.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 14 / 32

  • Eliminating runtime errors

    Matr : N → N → SetMatr m n = Vec m (Vec n R)_×_ : { i j k : N} → Matr i j → Matr j k → Matr i k

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 15 / 32

  • Gradual Typing

    U : Set

    El : U → Setrecord Dynamic wherefield

    type : Uvalue : El type

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 16 / 32

  • Summary : use cases for dependent types

    Giving types to all sensible programsSchemes are part of the dataEliminating run time errorsCapturing gradual typing

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 17 / 32

  • If dependent types are so greatwhy isn’t everybody using them?

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 18 / 32

  • Some myths and factsaboutdependently typed programming(DTP)

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 19 / 32

  • Myth 1 : Programming in DTP languages is more difficult

    DTP languages contain Haskell types as a subset.Hence you can always not use dependent types.If you use dependent types you may have to work harder to get yourprograms through the type checker.Pay as you go.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 20 / 32

  • Myth 2 : You have to prove that your program isterminating

    Good programs are total programs.It can be hard to convince a compiler that your program is total.You can declare that your program is total without proving it.I am a doctor.In some cases knowing that your program is total can make theprogram faster without compromising safety.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 21 / 32

  • Knowing that your program terminates makes it faster

    have : Vec A m

    ? : Vec A n

    thm : m ≡ ntransport (Vec A) thm have : Vec A n

    Do we need to run thm at runtime?No, if we know that it is total.Yes, if we don’t.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 22 / 32

  • Myth 3 : DTP languages are not Turing complete

    If we insist that all programs are total we can’t have all programs.Partiality is a monad (see our paper).TA, Nils Anders Danielsson, Nicolai KrausPartiality, Revisited: The Partiality Monad as a QuotientInductive-Inductive TypeUsing the partiality monad we can write and run all partially recursivefunctions in a DTP language.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 23 / 32

  • Myth 4 : DTP doesn’t work with effects

    What happens if an effectful computation appears in a type?Does the compiler execute the effect?E.g. we implement a missile control system andstartMissiles : IO > appears in a dependent type . . .

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 24 / 32

  • Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 25 / 32

  • Beauty in the Beast

    Wouter Swierstra and I made a proposal how to integrate effects inDTP in 2007.At compiletime we use a mathematical semantics of effects, afunctional program.At runtime we just execute the effect.Example: Partiality

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 26 / 32

  • Fact 1: DTP makes big demands on the IDE

    Need special support to effectively develop DTP software.Interactive and incremental typingAutomatic case splittingMoving between implicit and explicit syntax

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 27 / 32

  • Fact 2: Compile time can be an issue

    The problem is not that you run functions at compile time.Solving unification problems with existential variables may blow upcompilation time.Cause: loss of sharingSolutions have been suggested (e.g. by Andras Kovacs) but still anissue in current implementations.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 28 / 32

  • Fact 3: More precise types can hamper reusability

    Eg we have lists, vectors, sorted lists, . . .In many cases this can be addressed by identifying new abstractions.Cf Conor McBride’s paper:Ornamental Algebras, Algebraic Ornaments

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 29 / 32

  • Fact 4: DTP toolchains are not yet ready for professionaldevelopers

    This guy may disagree:

    Edwin Brady, developer of Idrisand author of Type driven development with Idris

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 30 / 32

  • Summary: Myths vs facts

    Myths 1 Programming in DTP languages is more difficult2 You have to prove that your program is terminating3 DTP languages are not Turing complete4 DTP doesn’t work with effects

    Facts 1 DTP makes big demands on the IDE2 Compile time can be an issue3 More precise types can hamper reusability4 DTP toolchains are not yet ready for professional

    developers

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 31 / 32

  • Claims

    A strong static type discipline requires dependent types.Dependent types are not an additional feature to be added in the end.Full dependent types are feasible and extremely useful forprogramming.

    Thorsten Altenkirch (Nottingham) The power of Π February 19, 2020 32 / 32