Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Value Your Types!

Value Your Types!

You’re probably familiar with types in programming languages, such as “integer” or “list of integers.” But what if your type system were powerful enough to express types like “non-negative integer” or “list of strings where each string is at least eight characters long”? Welcome to the world of dependent types! In this talk, we’ll explore what dependent types are, how they work, and the relationship between type theory and predicate logic, all using the Idris programming language. We’ll even see when type checking can become (gasp) undecidable!

Eric Weinstein

February 24, 2019
Tweet

More Decks by Eric Weinstein

Other Decks in Programming

Transcript

  1. Va1u3 Your TYPES!
    A !!Con West Adventure
    Eric Weinstein
    UC Santa Cruz, California, USA
    24 February 2019

    View Slide

  2. View Slide

  3. @ericqweinstein

    View Slide

  4. @ericqweinstein

    View Slide

  5. "queue"

    View Slide

  6. Dependent Types!
    A type whose definition depends on a value
    "List of integers" is a type; "list of integers where each
    successive value is strictly larger than its predecessor" is a
    dependent type!

    View Slide

  7. History!
    1934: Haskell Curry notices similarities between the typed
    lambda calculus and propositional logic
    1960s: William Howard and Nicolaas De Bruijn extend Curry’s
    work to predicate logic

    View Slide

  8. History!
    Credit: https://manishearth.github.io/blog/2017/03/04/what-are-sum-product-and-pi-types/

    View Slide

  9. History!
    Credit: https://manishearth.github.io/blog/2017/03/04/what-are-sum-product-and-pi-types/
    ∀x: fn() → Array
    "For all x, we can construct this array!"
    ∃x: fn() → Array
    "There exists an x such that we can construct this array!"

    View Slide

  10. History!
    1934: Haskell Curry notices similarities between the typed
    lambda calculus and propositional logic
    1960s: William Howard and Nicolaas De Bruijn extend Curry’s
    work to predicate logic

    View Slide

  11. History!
    1934: Haskell Curry notices similarities between the typed
    lambda calculus and propositional logic
    1960s: William Howard and Nicolaas De Bruijn extend Curry’s
    work to predicate logic

    View Slide

  12. The Curry-Howard
    Correspondence!

    View Slide

  13. View Slide

  14. Idris!
    data Vect : Nat -> Type -> Type where
    Nil : Vect 0 a
    (::) : (x : a) -> (xs : Vect n a) -> Vect (n + 1) a
    Credit: https://en.wikipedia.org/wiki/Idris_(programming_language)#Dependent_types

    View Slide

  15. Idris!
    data Vect : Nat -> Type -> Type where
    Nil : Vect Z a
    (::) : a -> Vect n a -> Vect (S n) a
    Credit: https://www.idris-lang.org/example/

    View Slide

  16. Idris!
    total
    pairAdd : Num a => Vect n a -> Vect n a -> Vect n a
    pairAdd Nil Nil = Nil
    pairAdd (x :: xs) (y :: ys) = x + y :: pairAdd xs ys
    Credit: https://en.wikipedia.org/wiki/Idris_(programming_language)#Dependent_types

    View Slide

  17. Idris!
    total
    append : Vect n a => Vect m a -> Vect (n + m) a
    append Nil ys = ys
    append (x :: xs) ys = x :: append xs ys
    Credit: https://en.wikipedia.org/wiki/Idris_(programming_language)#Dependent_types

    View Slide

  18. Idris!
    total
    append : Vect n a => Vect m a -> Vect (n + m) a
    append Nil ys = ys
    append (x :: xs) ys = x :: append xs ys
    Credit: https://en.wikipedia.org/wiki/Idris_(programming_language)#Dependent_types

    View Slide

  19. Idris!
    Credit: https://github.com/HoTT/book/issues/743

    View Slide

  20. Aside!
    #

    View Slide

  21. #
    Aside!

    View Slide

  22. Aside!
    #

    View Slide


  23. Aside!

    View Slide


  24. Aside!

    View Slide


  25. Aside!

    View Slide


  26. Aside!

    View Slide

  27. ? ? ?
    Aside!

    View Slide

  28. Undecidable!

    View Slide

  29. Credit: The Princess Bride (1987)

    View Slide

  30. Undecidable!
    When types contain values, they become little programs...
    ...and general program equivalence is undecidable!
    !!

    View Slide

  31. Undecidable!
    "At compile-time it will only evaluate things which it knows to
    be total (i.e. terminating and covering all possible inputs) in
    order to keep type checking decidable."
    !!
    Credit: https://docs.idris-lang.org/en/latest/faq/faq.html

    View Slide

  32. View Slide

  33. Papers!
    http://www.cse.chalmers.se/~peterd/papers/
    DependentTypesAtWork.pdf
    https://dl.acm.org/citation.cfm?id=671431
    http://www.cis.upenn.edu/~plclub/lambda-eek/lambda-eek.pdf
    http://www.kb.ecei.tohoku.ac.jp/~terauchi/papers/popl10-
    depceg.pdf

    View Slide

  34. TL;DPA
    Dependent types are amazing!
    Idris is an amazing language and you should try it!!

    View Slide

  35. Credit: DefendingDRealm (https://www.redbubble.com/)

    View Slide