SLIDE 1 Ilya Sergey
ilyasergey.net
PLMW @ ICFP 2019
Functional Programming is Everywhere
SLIDE 2 About myself
2
MSc Saint Petersburg State University, 2008
PhD KU Leuven, 2008-2012
Currently Associate Professor (tenure-track) at Yale-NUS College & NUS
Previously Lecturer → Associate Professor at University College London
Postdoc at IMDEA Software Institute
Software Engineer at JetBrains
SLIDE 3 Functional programmer since 2005
3
SLIDE 4 Functional programmer since 2005
4
2005 2006 2007 2008 2010 2011
SLIDE 5 Functional Languages
5
SLIDE 6 6
The Essence of Functional Languages
- Algebraic Data Types
- Pattern Matching
- Folds
- Continuations and CPS
- Structural Recursion
- Type Classes
- Monads
Check out this year’s ICFP program…
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Purely functional data structures
SLIDE 7 Functional Programming
7
This Talk
SLIDE 8 Functional Programming
8
This Talk
SLIDE 9 Functional Programming Ideas
9
This Talk
λ
SLIDE 10 10
Functional Programming Ideas in…
Software Engineering Teaching
Disclaimer: personal experience
Research
SLIDE 11 11
Functional Programming Ideas in…
Software Engineering Teaching Research
SLIDE 12 Moore’s Law
12
Clock speed flattening sharply Transistor count still rising
SLIDE 13 The Multicore Processor
13
cache
Bus
Bus
shared memory
cache cache
All on the same chip Sun T2000 Niagara
SLIDE 15 Specifications for Concurrent Data Structures
15
SLIDE 16 Reusable Specifications for Concurrent Data Structures
16
My research agenda since 2014
SLIDE 17
push x
{ S = xs } { S′= x :: xs }
pop()
{ S = xs }
Suitable for sequential programming
{ res = ⊥ ⋀ S = Nil ⋁ ∃x, xs′. res = x ⋀
xs = x :: xs′ ⋀ S′ = xs′ }
Abstract Specifications of a Stack
SLIDE 18
push x
{ S = xs } { S′= x :: xs }
pop()
{ S = xs } { res = ⊥ ⋀ S = Nil ⋁ ∃x, xs′. res = x ⋀
xs = x :: xs′ ⋀ S′ = xs′ }
Breaks composition in the presence of thread interference.
Abstract Specifications of a Stack
SLIDE 19
y := pop(); { y = ??? } { S = Nil }
SLIDE 20 y := pop();
{ y ∈ {⊥} ∪ {1, 2} }
push 2; { S = Nil }
SLIDE 21 y := pop(); { S = Nil }
{ y ∈ {⊥} ∪ {1, 2, 3} }
push 1; push 2; push 3;
r
r e u s e
(not thread-modular)
SLIDE 22
A reusable specification for pop?
y := pop(); { y = ??? } { S = Nil }
SLIDE 23
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Purely functional data structures
23
The Essence of Functional Languages
- Algebraic Data Types
- Pattern Matching
- Folds
- Continuations and CPS
- Structural Recursion
- Type Classes
- Monads
SLIDE 24
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Purely functional data structures
24
The Essence of Functional Languages
- Algebraic Data Types
- Pattern Matching
- Folds
- Continuations and CPS
- Structural Recursion
- Type Classes
- Monads
Enablers for Modular Development
SLIDE 25 Capture the effect of self-thread,
parametrise over the effect of others.
(aka Subjective specifications)
Idea: Interference-Parameterised Specifications
SLIDE 26 push x
{ S = xs } { S′ = x :: xs }
Atomic stack specifications
SLIDE 27 x :: xs xs
“abstract timestamp”
tk →
Atomic stack specifications
SLIDE 28 tk → tk+1 →
tk+2 → tk+3 →
……
tk+n →
| {z }
abstract time increases at
every concrete push/pop operation
tk+4 →
SLIDE 29 Changes by this thread Changes by other threads
tk+4 →
tk+1 →
tk+3 → tk+n →
tk →
tk+2 →
……
SLIDE 30 y := pop();
{ y = ⊥ ⋁ y = v, where v ∈ pushed(Ho) } { Hs = ∅ }
- Hs — “ghost history” of my pushes/pops to the stack
- Ho — “ghost history” of pushes/pops by all other threads
Subjective stack specifications
what I popped depends
- n what the others have pushed
| {z }
Sergey, Nanevski, Banerjee [ESOP’15]
λHo . pick (pushed(Ho))
SLIDE 31
y := pop(); { S = Nil } push 1; push 2; push 3;
SLIDE 32 push 1; push 2;
{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }
SLIDE 33 push 1; push 2;
{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }
SLIDE 34 push 1; push 2;
{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }
push 3;
{ Hs = t3 ↦ (zs, 3::zs) }
SLIDE 35 { Hs = t1 ↦ (xs, 1::xs) ⊕
t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }
push 1; push 2; push 3;
{ Hs = ∅ }
y := pop();
{ y ∈ {⊥} ∪ pushed(Ho) }
SLIDE 36 { Hs = t1 ↦ (xs, 1::xs) ⊕
t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }
push 1; push 2; push 3;
{ Hs = ∅ }
y := pop();
{ y ∈ {⊥} ∪ pushed(Ho) } 1 2 3
SLIDE 37 { Hs = t1 ↦ (xs, 1::xs) ⊕
t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }
push 1; push 2; push 3;
{ Hs = ∅ }
y := pop();
{ y ∈ {⊥} ∪ {1, 2, 3} }
SLIDE 38 Spin-lock Ticketed lock Allocator Increment Abstract lock Treiber stack Flat combiner FC stack Producer/Consumer Sequential stack Abstract stack
Jayanti’s
snapshot Snapshot client Atomic
snapshot Exchanger Counting network Quiescent
client Quantitatively relaxed client
Concurrent Graph Manipulations
Payoff: Verified Concurrent Libraries
Sergey et al. [PLDI’15]
SLIDE 39 39
Functional Programming Ideas in…
Software Engineering Teaching Research
… for modularity and proof reuse
SLIDE 40 40
Functional Programming Ideas in…
Software Engineering Teaching Research
… for Modularity and Proof Reuse
SLIDE 41 Algorithmic Competitions at UCL (2016-2018)
- Targeting 2nd year undergrads, team work
- One week-long
- Should involve math and programming
- Challenging for students, but easy to assess
SLIDE 42
Algorithmic Competitions at UCL (2016-2018)
SLIDE 43
SLIDE 44 The Competitions
- 2016: Art Gallery Competition
- 2017: Move-and-Tag Competition
- 2018: Room Furnishing
SLIDE 45
based on:
Chvátal’s Art Gallery Problem (1975)
Art Gallery Competition
SLIDE 46
SLIDE 47 How many guards do we really need?
The answer depends on the shape of the gallery.
SLIDE 48 How many guards do we really need?
The answer depends on the shape of the gallery.
SLIDE 49
How many guards do we really need?
SLIDE 50
How many guards do we really need?
SLIDE 51
How many guards do we really need?
SLIDE 52
How many guards do we really need?
SLIDE 53
For a given gallery (polygon),
find the minimal set of guards’ positions,
so together the guards can “see” the whole interior.
Art Gallery Problem
SLIDE 54 Project: Art Gallery Competition
Find the best solutions for a collection of large polygons.
SLIDE 55 Making it Fun
- Problem generator;
- Polygons with different “features” (convex, rectangular, etc.)
- Solution checker with online feedback
- geometric machinery (triangulation, visibility, …)
- web-server
- Make sure that it all works.
SLIDE 56
- Problem generator;
- Polygons with different “features” (convex, rectangular, etc.)
- Solution checker with online feedback
- geometric machinery (triangulation, visibility, …)
- web-server
- Make sure that it all works.
Making it Fun
SLIDE 57 Growing polygons
Primitive polygons with specific “features”
SLIDE 58 Seed
Growing polygons
SLIDE 59
Growing polygons
SLIDE 60
Growing polygons
SLIDE 61
Growing polygons
SLIDE 62
Growing polygons
SLIDE 63
Growing polygons
SLIDE 64
Growing polygons
SLIDE 65 Can we enumerate “primitive” polygons
and plug arbitrary shapes generators?
65
SLIDE 66 66
The Essence of Functional Languages
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Algebraic Data Types
- Purely functional data structures
- Pattern Matching
- Folds
- Continuations and CPS
- Structural Recursion
- Type Classes
- Monads
SLIDE 67
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Algebraic Data Types
67
The Essence of Functional Languages
- Purely functional data structures
- Pattern Matching
- Folds
- Continuations and CPS
- Structural Recursion
- Type Classes
- Monads
Enumeration and Extensibility
SLIDE 69 “Polygon Combinator”
trait PolygonGenerator extends GeneratorPrimitives {
val seeds : List[Polygon] val primitives : List[(Int) => Polygon] val locate : Double => Option[(Double, Double)] val seedFreqs : List[Int] val primFreqs : List[Int] val generations : Int ... }
SLIDE 70 Generating random polygons
Rectilinear
SLIDE 71 Quasi-convex
Generating random polygons
SLIDE 72 Crazy
Generating random polygons
SLIDE 73 Can we Quick-Check geometric algorithms?
73
SLIDE 74 Bug in textbook visibility algorithm
Randomly generated, 260 vertices, guards in every node ??!
SLIDE 75 Bug in textbook visibility algorithm
Randomly generated, 260 vertices, guards in every node
SLIDE 76 After shrinking: 20 vertices
Bug in textbook visibility algorithm
SLIDE 77 Removed irrelevant
light sources
Bug in textbook visibility algorithm
SLIDE 78 Removed irrelevant
light sources
Bug in textbook visibility algorithm
ICFP 2016
SLIDE 79 Beyond Classroom: ICFP Programming Contest 2019
Contest Report
SLIDE 80 80
Functional Programming Ideas in…
Software Engineering Teaching Research
… for creating fun assignments … for modularity and proof reuse
SLIDE 81 81
Functional Programming Ideas in…
Software Engineering Teaching Research
… for modularity and proof reuse … for creating fun assignments
SLIDE 84 Smart Contracts
- Stateful mutable objects replicated via a consensus protocol
- Use valuable resource (gas) to prevent “expensive” computations
- Yet, should be able to handle arbitrarily large data
- Can fail at any moment and roll-back (transactional behaviour)
SLIDE 85 Smart Contracts
- Stateful mutable objects replicated via a consensus protocol
- Use valuable resource (gas) to prevent “expensive” computations
- Yet, should be able to handle arbitrarily large data
- Can fail at any moment and roll-back (transactional behaviour)
Can we have an interpreter
supporting all of these, while keeping the “core” semantics simple
and easy to maintain?
z }| {
<latexit sha1_base64="o/9y/+iYATmvWYLFwU2SaLHtLQ=">ACXicbVBNS8NAEN3Ur1q/oh69LBbBU0lE0GPRi8cK9gPaUCbTbt0sxt2N0IJuXrxr3jxoIhX/4E3/43bNoi2Ph4vDfDzLw4Uwbz/tySiura+sb5c3K1vbO7p67f9DSMlWENonkUnVC0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X82bAy8QvSBUVaPTdz95AkjSmwhAOWnd9LzFBsowmle6aWaJkDGMKRdSwXEVAfZ7JMcn1hlgCOpbAmDZ+rviQxirSdxaDtjMCO96E3F/7xuaqLIGMiSQ0VZL4oSjk2Ek9jwQOmKDF8YgkQxeytmIzAxmJseBUbgr/48jJpndV8r+bfnlfrV0UcZXSEjtEp8tEFqMb1EBNRNADekIv6NV5dJ6dN+d93lpyiplD9AfOxzfaMpsN</latexit><latexit sha1_base64="o/9y/+iYATmvWYLFwU2SaLHtLQ=">ACXicbVBNS8NAEN3Ur1q/oh69LBbBU0lE0GPRi8cK9gPaUCbTbt0sxt2N0IJuXrxr3jxoIhX/4E3/43bNoi2Ph4vDfDzLw4Uwbz/tySiura+sb5c3K1vbO7p67f9DSMlWENonkUnVC0JQzQZuGU47iaIQh5y2w/H1G/fU6WZFHdmktAghqFgESNgrNR3cU9aO1RAaNZLRiCMjDP4QZ73apX82bAy8QvSBUVaPTdz95AkjSmwhAOWnd9LzFBsowmle6aWaJkDGMKRdSwXEVAfZ7JMcn1hlgCOpbAmDZ+rviQxirSdxaDtjMCO96E3F/7xuaqLIGMiSQ0VZL4oSjk2Ek9jwQOmKDF8YgkQxeytmIzAxmJseBUbgr/48jJpndV8r+bfnlfrV0UcZXSEjtEp8tEFqMb1EBNRNADekIv6NV5dJ6dN+d93lpyiplD9AfOxzfaMpsN</latexit>
SLIDE 86 86
The Essence of Functional Languages
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Algebraic Data Types
- Purely functional data structures
- Pattern Matching
- Folds
- Structural Recursion
- Continuations and CPS
- Type Classes
- Monads
SLIDE 87
- Higher-order functions and closures
- Types and Type Inference
- Polymorphism
- Laziness
- Point-free style
- Combinator Libraries
- Algebraic Data Types
87
The Essence of Functional Languages
- Purely functional data structures
- Pattern Matching
- Folds
- Structural Recursion
- Continuations and CPS
- Type Classes
- Monads
Expressing any Effects
&
Modular Interpreters
We show how a set of building blocks can be used to construct programming language interpreters, and present implementations of such building blocks capable of supporting many commonly known features, including simple expressions, three different function call mechanisms […], references and assignment, nondeterminism, first-class continuations, and program tracing.
SLIDE 88 88
- About 200 LOC of OCaml
- Hasn’t been affected
by multiple modifications in the back-end protocol
- Changes in gas accounting
have not affected the core interpreter
- Lots of performance bottlenecks
fixed without ever touching the evaluator
Powered by Monads
SLIDE 89 89
Functional Programming Ideas in…
Software Engineering Teaching Research
… for modularity and proof reuse … for creating fun assignments … for robust and maintainable artefacts
SLIDE 90 90
Functional Programming Ideas in…
Software Engineering Teaching Research
… for modularity and proof reuse … for creating fun assignments … for robust and maintainable artefacts
SLIDE 91
- FP insights spread far beyond programming in
OCaml, Haskell, Racket, etc.
- FP keeps evolving: new powerful ideas are
constantly emerging: effect handlers, staging, automatic differentiation, security type systems…
- Those ideas can be your tools, too!
91
To Take Away
Thanks!