Slide 1

Slide 1 text

DEPENDENTLY TYPED STATE MACHINES COMPOSE CONF 2018

Slide 2

Slide 2 text

WHO AM I? ▸ Tim McGilchrist @lambda_foo ▸ Haskell / OCaml programmer at Ansarada ▸ Curious about Distributed Systems ▸ Convinced about Types

Slide 3

Slide 3 text

BACKGROUND HOW DID I GET HERE?

Slide 4

Slide 4 text

TCP/IP STATE MACHINE

Slide 5

Slide 5 text

RAFT LEADER ELECTION

Slide 6

Slide 6 text

IDRIS IS A PACMAN COMPLETE LANGUAGE Edwin Brady

Slide 7

Slide 7 text

IDRIS ▸ general purpose language ▸ full dependent types with dependent pattern matching ▸ eager evaluation ▸ interfaces (ala Haskell's type classes) ▸ What if Haskell had full dependent types?

Slide 8

Slide 8 text

PROBLEMS

Slide 9

Slide 9 text

VECT : NAT -> TYPE -> TYPE

Slide 10

Slide 10 text

PAPER STATES ALL THE WAY DOWN ▸ "A useful pattern in dependently typed programming is to define a state transition system” ▸ “an architecture for dependently typed applications” ▸ “How to implement a state transition system as a dependent type “ ▸ "How to combine state transition systems into a larger system"

Slide 11

Slide 11 text

GENERALISING STATEFUL PROGRAMS ▸ Types should capture the states of resources ▸ Stateful APIs should compose ▸ Types should be readable ▸ Error messages should be readable

Slide 12

Slide 12 text

THE RECIPE IMPLEMENTING DEPENDENT STATE MACHINES 1. Draw State Diagram for the problem 2. Define State Machine as a Type 3. Implement State functions 4. Running State functions

Slide 13

Slide 13 text

INTERFACE DATASTORE (M : TYPE -> TYPE) WHERE ACCOUNT LOGIN AND ACCESS Login (success) Logout Logged In Logged Out Login (failure) Read Secret

Slide 14

Slide 14 text

ACCOUNT LOGIN AS TYPE

Slide 15

Slide 15 text

ACCOUNT LOGIN AS TYPE ▸ Type level functions everywhere

Slide 16

Slide 16 text

USING DATASTORE

Slide 17

Slide 17 text

IMPLEMENTING DATASTORE

Slide 18

Slide 18 text

(.) : (B -> C) -> (A -> B) -> A -> C TYPES OF COMPOSITION ▸ Horizontally - multiple state machines within a function ▸ Vertically - implement state machine in terms of another Examples: Application on top of a Communication Protocol Multiple resources, File IO plus State

Slide 19

Slide 19 text

HORIZONTALLY - STATE PLUS DATASTORE

Slide 20

Slide 20 text

VERTICALLY - STATE PLUS DATASTORE ▸ A composite resource is built up from a list of other resources. ▸ Use functions split and combine.

Slide 21

Slide 21 text

LIFT : MONAD M => M T -> STRANS M T RES (CONST RES) CLEANING UP THE TYPES ▸ Type level function ST ▸ List of actions on resources

Slide 22

Slide 22 text

DATASTORE - CLEAN

Slide 23

Slide 23 text

REMOVE : VAR -> TYPE -> ACTION TY PRETTY ERRORS

Slide 24

Slide 24 text

ST : (TYPE -> TYPE) -> (TY : TYPE) -> LIST (ACTION TY) -> TYPE RELATED WORK ▸ Algebraic Effects in Idris, Haskell, and Purescript ▸ Encoding State Machines ▸ Session Types ▸ Linear Types

Slide 25

Slide 25 text

MKRES : LABEL -> TYPE -> RESOURCE RESOURCES ▸ States All the Way Down, E Brady ▸ Programming and Reasoning with Algebraic Effects and Dependent Types, E Brady ▸ Idris website http://docs.idris-lang.org/ ▸ Type Driven Design with Idris (book)

Slide 26

Slide 26 text

ST : (TYPE -> TYPE) -> (TY : TYPE) -> LIST (ACTION TY) -> TYPE THANK YOU ▸ What questions do you have? ▸ Do you want to see more code?

Slide 27

Slide 27 text

MORE TYPES

Slide 28

Slide 28 text

TYPE INDEXED MONADS