Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Dependent Types

Dependent Types

Dependent Types (and other ideas for guaranteeing correctness with types)

A few strategies for protecting yourself from your future self's mistakes. Write more robust code by expressing constraints in the type signature and avoiding partial functions. Give your compiler the tools it needs to assist you. Let it help you.

Avatar for Radek Pietruszewski

Radek Pietruszewski

November 16, 2016
Tweet

More Decks by Radek Pietruszewski

Other Decks in Programming

Transcript

  1. Dependent Types and other ideas for guaranteeing correctness with types

    Radek Pietruszewski @radexp • radex.io • nozbe.com
  2. @radexp • radex.io • nozbe.com let xs = [1, 2,

    3] xs[0] // => 1 xs[4] // => crash xs[-1] // => crash
  3. @radexp • radex.io • nozbe.com let xs = [1, 2,

    3] xs[n] = {some value; n ∈ ⟨0,2⟩ ¯\_(ϑ)_/¯; n<0 ⋁ n>2
  4. @radexp • radex.io • nozbe.com enum Direction { case Up,

    Down, Left, Right } func move(direction: Direction)
  5. @radexp • radex.io • nozbe.com struct User { var loggedIn:

    Bool ... } /// `user` must be logged in! func doSomethingImportant(user: User)
  6. @radexp • radex.io • nozbe.com /// `user` must be logged

    in! func doSomethingImportant(user: User) User(loggedIn: false)
  7. @radexp • radex.io • nozbe.com struct LoggedInValidator: Validator { static

    func validate(value: User) -> Bool { return value.loggedIn } }
  8. @radexp • radex.io • nozbe.com struct LoggedInValidator: Validator { static

    func validate(value: User) -> Bool { return value.loggedIn } }