fp for blockchain - university of kent · (commitcash com2 bob ada20 20 200 (when...

90
FUNCTIONAL PROGRAMMING FOR 3G BLOCKCHAIN SIMON THOMPSON, IOHK & KENT UNI

Upload: others

Post on 29-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

FUNCTIONAL PROGRAMMING FOR 3G BLOCKCHAIN

SIMON THOMPSON, IOHK & KENT UNI

Page 2: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Haskell

Erlang

OCaml

Scala

Elm

F#

Idris

LISPMiranda

Elixir

Page 3: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Haskell

Erlang

OCaml

Scala

Elm

F#

Idris

LISPMiranda

Elixir

Page 4: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 5: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

pattern matching

data

higher-order functions

recursionlambdastypes

Page 6: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

pattern matching

data

higher-order functions

recursionlambdastypes

Page 7: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

pattern matching

data

higher-order functions

recursionlambdastypes

monads

monoids

reactive

lazy

immutability

lenses

DSLs

types, types, types, …

effects

dependent types

Page 8: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

fun

Page 9: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Model the world as data

Page 10: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

immutable data

Page 11: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

32

Page 12: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

4 3

Page 13: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

32

Page 14: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

324

Page 15: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

32

4

Page 16: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Model the world as data +

Functions over the data

Page 17: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

And when we say “function” …

We mean in the mathematical sense, taking inputs to outputs,

and doing nothing else!

Page 18: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

So what about side-effects?

Page 19: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

input output

Page 20: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

input output

state after

state before

Page 21: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Model the world as data +

Functions over the data +

Functions as data

Page 22: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Behaviour becomes data:

Page 23: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Behaviour becomes data: map/reduce, monads,

APIs, laziness …

Page 24: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

rock paper

scissors

Page 25: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Original image: https://www.thishopeanchors.com/single-post/2017/04/06/Rock-Paper-Scissors

Page 26: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Original image: https://www.thishopeanchors.com/single-post/2017/04/06/Rock-Paper-Scissors

I choose what to play, depending on

the history of all your moves.

Page 27: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Original image: https://www.thishopeanchors.com/single-post/2017/04/06/Rock-Paper-Scissors

data Move = Rock | Paper | Scissors

type Strategy = [Move] -> Move

I choose what to play, depending on

the history of all your moves.

Page 28: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Original image: https://www.thishopeanchors.com/single-post/2017/04/06/Rock-Paper-Scissors

data Move = Rock | Paper | Scissors

type Strategy = [Move] -> Move

I choose what to play, depending on

the history of all your moves.

beat :: Strategy

beat (x:xs) = case x of

    Rock -> Scissors     Paper -> Rock     Scissors -> Paper

beat [] = Rock

Page 29: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Original image: http://www.metso.com/services/spare-wear-parts-conveyors/conveyor-belts/

Page 30: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

types

Page 31: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Functions give us expressivity +

Types help to constrain that +

Type-driven development

Page 32: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type Point = (Float,Float)

data Shape = Circle Point Float | Rectangle Point Float Float

area :: Shape -> Float

area (Circle _ r) = pi*r*r area (Rectangle _ h w) = h*w

Page 33: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

nub: remove all duplicates

Page 34: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type inference

Page 35: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type inference polymorphism = generics

Page 36: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type inference polymorphism = generics type classes = overloading

Page 37: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type inference polymorphism = generics type classes = overloading

monads, monoids, lenses, …

Page 38: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

type inference polymorphism = generics type classes = overloading

monads, monoids, lenses, … dependent types

Page 39: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

not just “types” effects

information flow regions

Page 40: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

calculation

Page 41: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

nub: remove all duplicates

Page 42: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

Rewrite … work “top down” nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Page 43: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 44: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 45: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 46: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 47: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 48: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 49: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 50: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 51: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

nub :: Eq a => [a] -> [a]

nub [] = []

nub(x:xs) = if elem x xs then nub xs else x : nub xs

The nub of a list is the list with all duplicates removed.

nub [1,2,1] = nub (1:[2,1]) = nub [2,1] = nub (2:[1]) = 2 : nub [1] = 2 : nub (1:[]) = 2 : 1 : nub [] = 2 : 1 : [] = 2 :[1] = [2,1]

Rewrite … work “top down”

Page 52: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Not just in theory ...

Page 53: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Haskell

startups

GHC

stack/cabal

tools

IDEs

experienced people

metaprogramming

libraries

beacon language

Page 54: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 55: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Cardano

3rd-gen

PoS

Sidechains

Page 56: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Secure foundations

This board belongs to Newcastle University PhD student Tom Fisher, who is doing research in homological algebra.

Thanks to Christian Perfect for the photo.whatsonmyblackboard.wordpress.com

Page 57: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Secure foundations Immutable data Explicit effects

Functions as data Expressive types

Page 58: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Secure foundations Immutable data Explicit effects

Functions as data Expressive types

Develop from a formal spec

Page 59: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 60: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 61: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 62: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 63: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 64: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 65: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Secure foundations Develop from a formal spec

Property-based random tests Model in a proof assistant Minimal “napkin” machine

Page 66: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Haskell

Cardano SL

Plutus

MarloweReal world / time User wallets

Scripting Cardano

Page 67: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Minimal “napkin” machine

Page 68: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Functional transaction model

Page 69: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

UTxO vs Accounts

Functional, dataflow

Compositional

Imperative, entangled

Shared state

Page 70: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Extended UTxO

Page 71: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

UTxO vs Extended UTxO

Validator(Redeemer) = TrueValidator(Redeemer, Data, State) = True

Page 72: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Validator(Redeemer, Data, State) = True

UTxO vs Extended UTxO

data flows with value

scripts have an identity

a transaction can control how its UTxOs are spent

Page 73: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Haskell “all the way down”

contribute :: Campaign -> Value -> MockWallet () contribute campaign value = do when (value <= 0) $ throwOtherError "Must contribute a positive value"

ownPK <- ownPubKey tx <- payToScript (Ledger.scriptAddress (contributionScript campaign)) value DataScript (Ledger.lifted ownPK)

register (refundTrigger campaign) (refundHandler (Ledger.hashTx tx) campaign)

contributionScript :: Campaign -> ValidatorScript contributionScript campaign = ValidatorScript (validator `apply` campaign) where validator =

Ledger.fromCompiledCode $$(PlutusTx.compile [|| (\Campaign{..} action contrib tx -> let PendingTx ps outs _ _ (Height h) _ _ = tx isValid = case action of Refund -> h > collectionDeadline && contributorOnly outs && $$(txSignedBy) tx contrib Collect -> h > deadline

Unify on- & off-chain code

Meta-programming

Page 74: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Domain-specific languages

Page 75: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Domain-specific languages

Domain-specific languages … … as data types, monads,

… and embedded in Haskell

Page 76: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Marlowe

Page 77: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Marlowe

(When (Or (majority_chose refund) (majority_chose pay)) (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original))

(When (Or (majority_chose refund) (majority_chose pay)) (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original)

Page 78: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Marlowe

(When (Or (majority_chose refund) (majority_chose pay)) (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original))

(When (Or (majority_chose refund) (majority_chose pay)) 90 (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original) redeem_original)

Page 79: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Marlowe

(When (Or (majority_chose refund) (majority_chose pay)) (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original))

(CommitCash id1 alice 15000 10 100 (When (Or (majority_chose refund) (majority_chose pay)) 90 (Choice (majority_chose pay) (Pay alice bob AvailableMoney) redeem_original) redeem_original) Null)

Page 80: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Implementing Marlowe

Validator(Redeemer, Data, State) = True

the Marlowe interpreter is a single Plutus script

use the Data script for residual contract

we could also compile …

question of fees, code reuse, libraries, …

Page 81: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 82: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1
Page 83: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Marlowe & ACTUS

www.actusfrf.org

Page 84: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

immutable + explicit effects

Page 85: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

immutable + explicit effects

Page 86: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

immutable + explicit effects strongly typed + formal specs

Page 87: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

immutable + explicit effects strongly typed + formal specs

full stack + ecosystem

Page 88: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

www.iohk.io

github.com/input-output-hk/marlowe

Page 89: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

extra slides

Page 90: FP for blockchain - University of Kent · (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1

Syntax WTF!

CommitCash com1 alice ada100 10 200 (CommitCash com2 bob ada20 20 200 (When (PersonChoseSomething choice1 alice) 100 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)) (Pay pay1 bob alice ada20 200 (Both (RedeemCC com1 Null) (RedeemCC com2 Null)))) (RedeemCC com1 Null)) Null