Top Banner
Dependent Types and other ideas for guaranteeing correctness with types Radek Pietruszewski @radexp • radex.io
28

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

Feb 20, 2017

Download

Software

radexp
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dependent types (and other ideas for guaranteeing correctness with types)

Dependent Types and other ideas for guaranteeing correctness with types

Radek Pietruszewski @radexp • radex.io

Page 2: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Static typing is nice

Page 3: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Expressing guarantees

Page 4: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Partial functions

Page 5: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

let xs = [1, 2, 3]

xs[0] // => 1 xs[4] // => crash xs[-1] // => crash

Page 6: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

subscript (index: Int) -> Element

-a lot, ..., -1, 0, 1, 2, 3, 4, a lot

Page 7: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

subscript (index: Int) -> Element

-a lot, ..., -1, 0, 1, 2, 3, 4, a lot

Page 8: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

No compile time check

😕

Page 9: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Prefer total functions to partial functions

Page 10: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Optionals

Page 11: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Foo* vs Foo vs Foo?

Page 12: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Enums

Page 13: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

func move(direction: String)

move("up") move("down") move("wat") // undefined

Page 14: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

enum Direction { case Up, Down, Left, Right }

func move(direction: Direction)

Page 15: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

enum SuggestionViewModel { case Header(String) case Suggestion(Suggestion) }

Page 16: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Dependent Types

Page 17: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

struct User { var loggedIn: Bool ... }

/// `user` must be logged in! func doSomethingImportant(user: User)

Page 18: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

/// `user` must be logged in! func doSomethingImportant(user: User)

😕User(loggedIn: false)

Page 19: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

User<loggedIn=true>

Page 20: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Validated

github.com/Ben-G/Validated

Page 21: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

struct LoggedInValidator: Validator { static func validate(value: User) -> Bool { return value.loggedIn } }

Page 22: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

struct LoggedInValidator: Validator { static func validate(value: User) -> Bool { return value.loggedIn } }

Page 23: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Validated<User, LoggedInValidator>

User<loggedIn=true>

Page 24: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

typealias LoggedInUser = Validated<User, LoggedInValidator>

Page 25: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

let rawUser = User(loggedIn: false)

let loggedInUser = LoggedInUser(rawUser)

Page 26: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

func doSomethingImportant(user: LoggedInUser)

🙂

Page 27: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

Keep your functions total

Page 28: Dependent types (and other ideas for guaranteeing correctness with types)

@radexp • radex.io

github.com/Ben-G/Validated

bit.do/partial-functions