![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/4.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/36.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/37.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/43.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/44.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/45.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/46.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/47.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/48.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/49.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/50.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/51.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/52.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/53.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/54.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/55.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/56.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/57.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/58.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/59.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/60.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/61.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/62.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/63.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/64.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/65.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/66.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/67.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/68.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/69.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/70.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/71.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/72.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/73.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/74.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/75.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/76.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/77.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/78.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/79.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/80.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/81.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/82.jpg)
![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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/83.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/84.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/85.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/86.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/87.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/88.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/89.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022051606/601914d00405a6078820ea5b/html5/thumbnails/90.jpg)
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