type driven development in idris · type driven development puts types rst. three steps: type:...

31
Type Driven Development in Idris Edwin Brady ([email protected]) University of St Andrews, Scotland, UK @edwinbrady Kats Workshop, Dublin, 21st May 2016

Upload: others

Post on 19-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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://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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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://www.manning.com/books/

type-driven-development-with-idris

(Ask me about discount codes!)