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
  2. 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!
  3. 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
  4. History! Credit: https://manishearth.github.io/blog/2017/03/04/what-are-sum-product-and-pi-types/ ∀x: fn() → Array<bool, x> "For all

    x, we can construct this array!" ∃x: fn() → Array<bool, x> "There exists an x such that we can construct this array!"
  5. 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
  6. 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
  7. 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
  8. 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/
  9. 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
  10. 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
  11. 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
  12. 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