Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf ·...

32
Type-driven Development of Communicating Systems in Idris Edwin Brady ([email protected]) University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016

Transcript of Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf ·...

Page 1: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development of CommunicatingSystems in Idris

Edwin Brady ([email protected])University of St Andrews, Scotland, UK

@edwinbrady

Lambda World, October 1st 2016

Page 2: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Idris is a pure functional language with dependent types:

Encourages Type-driven Development

Totality checking

In this talk:

Total Functional Programming

Termination and Productivity

Total Functional Programming and Interaction

A practical example: Type Safe Concurrency

Page 3: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

What is Type-driven Development?

Types as a plan for a program

Write types first

Define programs interactively

Programs may contain holesType checker directs programmer

Refine type and program as necessary

Process: Type, Define, Refine

Page 4: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

What is Type-driven Development?

Types as a plan for a program

Write types first

Define programs interactively

Programs may contain holesType checker directs programmer

Refine type and program as necessary

Process: Type, Define, Refine

Page 5: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

What is Type-driven Development?

Types as a plan for a program

Write types first

Define programs interactively

Programs may contain holesType checker directs programmer

Refine type and program as necessary

Process: Type, Define, Refine

Page 6: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

What is Type-driven Development?

Types as a plan for a program

Write types first

Define programs interactively

Programs may contain holesType checker directs programmer

Refine type and program as necessary

Process: Type, Define, Refine

Page 7: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development

Page 8: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development

Page 9: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development

Page 10: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Total Functional Programming

A total function is a function which, for all well-typed inputs,either

Terminates with a well-typed result

Produces a finite prefix of a well-typed infinite result in finitetime

Page 11: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Why do we care?

If we care about types, we should care about totality

Given f : T

If f is total, we know that it will always give a result of type T

If f is partial, we know that if it gives a result, it will be oftype T

Examples: Vectors and Streams

Page 12: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Why do we care?

If we care about types, we should care about totality

Given f : Theorem

If f is total, we know that it will always give a result of typeTheorem

If f is partial, we know that if it gives a result, it will be oftype Theorem ???

Examples: Vectors and Streams

Page 13: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Totality Checking

Idris checks:

Coverage: patterns for all well-typed inputs

Termination: there is a decreasing argument

Productivity: recursive call is guarded by a constructor

Page 14: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development

Page 15: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Type-driven Development

(thanks to @aaronmblevin)

Page 16: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Commercial Break

https://www.tinyurl.com/typedd

Page 17: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Interactive Programs in Idris

Idris, like Haskell, uses IO for writing interactive programs

A value of type IO ty is a description of an interactive actionwhich results in a value of type ty

Example: Sequencing IO Actions

hello : IO ()

hello = do putStr "What is your name? "

name <- getLine

putStr ("Hello " ++ name)

Page 18: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Interactive Programs in Idris

Problem: we often want interactive programs to runindefinitely

Example: Looping IO Actions

loopy : IO ()

loopy = do putStr "What is your name? "

name <- getLine

putStr ("Hello " ++ name)

loopy -- Not total!

Composing actions in a recursive function may not be total

No structurally decreasing argument, in general

Page 19: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Interactive Total Functional Programs

Solution: Describe looping programs as a stream of IO actions:

data InfIO : Type where

Do : IO a -> (a -> Inf InfIO) -> InfIO

(>>=) : IO a -> (a -> Inf InfIO) -> InfIO

(>>=) = Do

Page 20: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Interactive Total Functional Programs

Then define a run function to execute those descriptions:

run : InfIO -> IO ()

Compare with IO:

IO ty is a description of actions which result in a ty

The run-time system executes those actions

run on InfIO does a similar job, at a different level

Page 21: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Interactive Total Functional Programs

Then define a run function to execute those descriptions:

run : InfIO -> IO ()

Compare with IO:

IO ty is a description of actions which result in a ty

The run-time system executes those actions

run on InfIO does a similar job, at a different level

Page 22: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Example: Concurrency

The Idris run-time system supports message passing concurrency

A process can spawn another process

A process can create a Channel, using:

connect, which initiates a connection to another processlisten, which waits for incoming connections

Processes can send and receive messages on a Channel

Page 23: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Message Passing Concurrency in Idris

Page 24: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Message Passing Concurrency in Idris

To write correct concurrent programs in this style, we’d like toensure, at least:

Requests (like Add 2 3) and Responses (like 5) are well-typedw.r.t. each other

Server processes (like Adder) run indefinitely

That is, they are productive

Server processes always complete responses to requests

That is, processing a response terminates

Page 25: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Server Processes

Page 26: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Types for Message Passing

We can achieve this with types:

Define a type for Requests

Define a function to calculate Response types from requests

This describes valid message types for interactions betweenprocesses

Define a type for servers, parameterised by the Request andResponse types it services

This defines the type of messages we can send to a processLike InfIO, a process is an infinite sequence of commandsLike InfIO, it guarantees productivityProcesses run indefinitely, and always complete requests

Page 27: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Types for Message Passing

We can achieve this with types:

Define a type for Requests

Define a function to calculate Response types from requests

This describes valid message types for interactions betweenprocesses

Define a type for servers, parameterised by the Request andResponse types it services

This defines the type of messages we can send to a processLike InfIO, a process is an infinite sequence of commandsLike InfIO, it guarantees productivityProcesses run indefinitely, and always complete requests

Page 28: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Types for Message Passing

Adder Requests/Responses

data Request = Add Nat Nat

Response : Request -> Type

Response (Add x y) = Nat

Adder Implementation

adder : ServerLoop Response ()

adder = do Accept (\msg =>

case msg of

Add x y => Pure (x + y))

Loop adder

Page 29: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Demonstration

Concurrent Processes in Action

Page 30: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Further Reading

On total functional programming:

David Turner, Elementary Strong Functional Programming,2005

On interactive programming with dependent types

Peter Hancock and Anton Setzer, Interactive Programs inDependent Type Theory, 2000

On types for communicating systems:

Kohei Honda, Types for Dyadic Interaction, 1993

Kohei Honda, Nobuko Yoshida, Marco Carbone, MultipartyAsynchronous Session Types, 2008

Philip Wadler, Propositions as Sessions, 2012

Page 31: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

Summary

Total programs are either terminating or productive

Together, this allows us to write long running processes, whereevery request is processed in finite time

A useful pattern for concurrent programming is to:

Define server processes which respond to requestsWrite programs as a collection of client processes, makingremote procedure calls to servers

We can define long running, well typed, concurrent processesas potentially infinite streams of commands

Using dependent types (in particular, first class functions),we’ve described simple message passing protocols

Page 32: Type-driven Development of Communicating Systems in Idris › talks › idris-conc.pdf · University of St Andrews, Scotland, UK @edwinbrady Lambda World, October 1st 2016. Idris

https://www.tinyurl.com/typedd