![Page 1: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/1.jpg)
Type Driven Development in Idris
Edwin Brady ([email protected])University of St Andrews, Scotland, UK
@edwinbrady
Kats Workshop, Dublin, 21st May 2016
![Page 2: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/2.jpg)
Scotland, Home of Functional Programming
![Page 3: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/3.jpg)
Welcome to Fife
![Page 4: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/4.jpg)
Escape from Fife
![Page 5: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/5.jpg)
Idris is a Pac-man Complete functional programming languagewith dependent types
http://idris-lang.org/
Still undergoing lots of research. . .
. . . but lots of fun to play (and learn) with!
Workshop materials (code, slides, exercises):
http://www.idris-lang.org/documentation/
workshops/kats-workshop-may-2016/
Also, I have stickers!
![Page 6: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/6.jpg)
Idris is a Pac-man Complete functional programming languagewith dependent types
http://idris-lang.org/
Still undergoing lots of research. . .
. . . but lots of fun to play (and learn) with!
Workshop materials (code, slides, exercises):
http://www.idris-lang.org/documentation/
workshops/kats-workshop-may-2016/
Also, I have stickers!
![Page 7: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/7.jpg)
Idris is a Pac-man Complete functional programming languagewith dependent types
http://idris-lang.org/
Still undergoing lots of research. . .
. . . but lots of fun to play (and learn) with!
Workshop materials (code, slides, exercises):
http://www.idris-lang.org/documentation/
workshops/kats-workshop-may-2016/
Also, I have stickers!
![Page 8: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/8.jpg)
Idris is a Pac-man Complete functional programming languagewith dependent types
http://idris-lang.org/
Still undergoing lots of research. . .
. . . but lots of fun to play (and learn) with!
Workshop materials (code, slides, exercises):
http://www.idris-lang.org/documentation/
workshops/kats-workshop-may-2016/
Also, I have stickers!
![Page 9: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/9.jpg)
Workshop outline:
Part 1 (Fundamentals): First class types, vectors, definingdata types
Part 2 (Relationships between data): equality proofs,expressing assumptions in types
Part 3 (Effects): IO, Side effects, reasoning about state
Part 4 (Total Functional Programming): views, streams,processes and type safe concurrency
There are exercises for the breaks!
![Page 10: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/10.jpg)
Workshop outline:
Part 1 (Fundamentals): First class types, vectors, definingdata types
Part 2 (Relationships between data): equality proofs,expressing assumptions in types
Part 3 (Effects): IO, Side effects, reasoning about state
Part 4 (Total Functional Programming): views, streams,processes and type safe concurrency
There are exercises for the breaks!
![Page 11: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/11.jpg)
https://www.manning.com/books/
type-driven-development-with-idris
(Ask me about discount codes)
![Page 12: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/12.jpg)
Why types?
We can use type systems for:
Checking a program has the intended properties
Guiding a programmer towards a correct program
Building expressive and generic libraries
Type Driven Development puts types first. Three steps:
Type: Write a type for a function
Define: Create a (possibly incomplete) implementation
Refine: Improve/complete the implementation
![Page 13: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/13.jpg)
Why types?
We can use type systems for:
Checking a program has the intended properties
Guiding a programmer towards a correct program
Building expressive and generic libraries
Type Driven Development puts types first. Three steps:
Type: Write a type for a function
Define: Create a (possibly incomplete) implementation
Refine: Improve/complete the implementation
![Page 14: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/14.jpg)
Time to write some code!
Type directed editing
First-class types, vectors, defining data types
Proofs and relationships between data (after the break)
![Page 15: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/15.jpg)
Type Error!
![Page 16: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/16.jpg)
Security Error!
![Page 17: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/17.jpg)
Programming with Effects
So far, the programs we’ve written have been pure
But. . . real programs need to interact with the outside world!
Two approaches:
The IO type (the Haskell approach)The Eff dependent type (describe allowed side effectsprecisely)
![Page 18: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/18.jpg)
The IO Type
IO is an abstract type which allows us to describe interactiveprograms
data IO : Type -> Type where ...
Then we can execute interactive programs using the :exec
command. For example:
Idris> :exec putStrLn (show (47 * 2))
94
![Page 19: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/19.jpg)
The IO Type
IO is an abstract type which allows us to describe interactiveprograms
data IO : Type -> Type where ...
Then we can execute interactive programs using the :exec
command. For example:
Idris> :exec putStrLn (show (47 * 2))
94
![Page 20: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/20.jpg)
The IO Type
Evaluator
Run Time Environment
putStrLn (show (47 * 2))
Write "94\n"to console
original expression
description of interactions
![Page 21: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/21.jpg)
Coding time: IO and Effects
![Page 22: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/22.jpg)
State machine example: Door opening
![Page 23: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/23.jpg)
Coding time: Door Protocol and Effects
![Page 24: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/24.jpg)
Total Functional Programming
A total function is a function which is defined for all possiblewell-typed inputs. That is:
It covers all possible well-typed input cases
For all well-typed inputs it either
terminates with a well-typed resultproduces a new constructor of an infinite data type
Idris always checks whether a function is guaranteed to be total.
This is conservative, due to the halting problem. . .
![Page 25: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/25.jpg)
Total Functional Programming
A total function is a function which is defined for all possiblewell-typed inputs. That is:
It covers all possible well-typed input cases
For all well-typed inputs it either
terminates with a well-typed resultproduces a new constructor of an infinite data type
Idris always checks whether a function is guaranteed to be total.
This is conservative, due to the halting problem. . .
![Page 26: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/26.jpg)
Total Functional Programming
Why does totality matter?
Correctness: “Well-typed programs don’t go wrong”
Efficiency: optimiser can be more aggressive when functionsare guaranteed to terminate
Precision: related to correctness
![Page 27: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/27.jpg)
Total Functional Programming
Why does totality matter?
Correctness: “Well-typed programs don’t go wrong”
Efficiency: optimiser can be more aggressive when functionsare guaranteed to terminate
Precision: related to correctness
![Page 28: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/28.jpg)
Total Functional Programming
A function may be total, but why does that help if we don’t knowhow long it runs for?
We can still guard against accidental non-termination
Assertion: accidental non-termination is always a bug
A program without a termination proof is highly suspicious
![Page 29: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/29.jpg)
Coding time: Streams and Views
![Page 30: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/30.jpg)
Summary
Why are we interested in dependent types?
Safety
Programs checked against precise specifications
Expressivity
Better, more descriptive APIsType directed developmentType system should be helping, not telling you off!
Genericity
e.g. program generation
Efficiency
More precise type information should help the compilerPartial evaluation, erasure
![Page 31: Type Driven Development in Idris · Type Driven Development puts types rst. Three steps: Type: Write a type for a function De ne: Create a (possibly incomplete) implementation Re](https://reader034.vdocuments.net/reader034/viewer/2022050208/5f5b07dda28b0b786f2adf49/html5/thumbnails/31.jpg)
https://www.manning.com/books/
type-driven-development-with-idris
(Ask me about discount codes!)