Functional Programming is Everywhere Ilya Sergey ilyasergey.net - - PowerPoint PPT Presentation

functional programming is everywhere
SMART_READER_LITE
LIVE PREVIEW

Functional Programming is Everywhere Ilya Sergey ilyasergey.net - - PowerPoint PPT Presentation

Functional Programming is Everywhere Ilya Sergey ilyasergey.net PLMW @ ICFP 2019 2 About myself MSc Saint Petersburg State University, 2008 PhD KU Leuven, 2008-2012 Currently Associate Professor (tenure-track) at Yale-NUS College


slide-1
SLIDE 1

Ilya Sergey

ilyasergey.net

PLMW @ ICFP 2019

Functional Programming is Everywhere

slide-2
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
SLIDE 3

Functional programmer since 2005

3

slide-4
SLIDE 4

Functional programmer since 2005

4

2005 2006 2007 2008 2010 2011

slide-5
SLIDE 5

Functional Languages

5

slide-6
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
SLIDE 7

Functional Programming

7

This Talk

slide-8
SLIDE 8

Functional Programming

8

This Talk

slide-9
SLIDE 9

Functional Programming Ideas

9

This Talk

λ

slide-10
SLIDE 10

10

Functional Programming Ideas in…

Software Engineering Teaching

Disclaimer: personal experience

Research

slide-11
SLIDE 11

11

Functional Programming Ideas in…

Software Engineering Teaching Research

slide-12
SLIDE 12

Moore’s Law

12

Clock speed flattening sharply Transistor count still rising

slide-13
SLIDE 13

The Multicore Processor

13

cache

Bus

Bus

shared memory

cache cache

All on the same chip Sun T2000 Niagara

slide-14
SLIDE 14

14

slide-15
SLIDE 15

Specifications for Concurrent Data Structures

15

slide-16
SLIDE 16

Reusable Specifications for Concurrent Data Structures

16

My research agenda since 2014

slide-17
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
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
SLIDE 19

y := pop(); { y = ??? } { S = Nil }

slide-20
SLIDE 20

y := pop();

{ y ∈ {⊥} ∪ {1, 2} }

  • push 1;

push 2; { S = Nil }

slide-21
SLIDE 21

y := pop(); { S = Nil }

{ y ∈ {⊥} ∪ {1, 2, 3} }

push 1; push 2; push 3;

  • N
  • p

r

  • f

r e u s e

(not thread-modular)

slide-22
SLIDE 22

A reusable specification for pop?

y := pop(); { y = ??? } { S = Nil }

slide-23
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
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
SLIDE 25

Capture the effect of self-thread,
 parametrise over the effect of others.

(aka Subjective specifications)

Idea: Interference-Parameterised Specifications

slide-26
SLIDE 26

push x

{ S = xs } { S′ = x :: xs }

Atomic stack specifications

slide-27
SLIDE 27

x :: xs xs

“abstract timestamp”

tk →

Atomic stack specifications

slide-28
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
SLIDE 29

Changes by this thread Changes by other threads

tk+4 →

tk+1 →

tk+3 → tk+n →

tk →

tk+2 →

……

slide-30
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
SLIDE 31

y := pop(); { S = Nil } push 1; push 2; push 3;

slide-32
SLIDE 32

push 1; push 2;

{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }

slide-33
SLIDE 33

push 1; push 2;

{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }

slide-34
SLIDE 34

push 1; push 2;

{ Hs = ∅ } { Hs = t1 ↦ (xs, 1::xs) } { Hs = t1 ↦ (xs, 1::xs) ⊕ t2 ↦ (ys, 2::ys) }

push 3;

  • { Hs = ∅ }

{ Hs = t3 ↦ (zs, 3::zs) }

slide-35
SLIDE 35

{ Hs = t1 ↦ (xs, 1::xs) ⊕ 
 t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }

push 1; push 2; push 3;

  • { Hs = ∅ }

{ Hs = ∅ }

y := pop();

  • { Hs = ∅ }

{ y ∈ {⊥} ∪ pushed(Ho) }

slide-36
SLIDE 36

{ Hs = t1 ↦ (xs, 1::xs) ⊕ 
 t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }

push 1; push 2; push 3;

  • { Hs = ∅ }

{ Hs = ∅ }

y := pop();

  • { Hs = ∅ }

{ y ∈ {⊥} ∪ pushed(Ho) } 1 2 3

slide-37
SLIDE 37

{ Hs = t1 ↦ (xs, 1::xs) ⊕ 
 t2 ↦ (ys, 2::ys) } { Hs = t3 ↦ (zs, 3::zs) }

push 1; push 2; push 3;

  • { Hs = ∅ }

{ Hs = ∅ }

y := pop();

  • { Hs = ∅ }

{ y ∈ {⊥} ∪ {1, 2, 3} }

slide-38
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
SLIDE 39

39

Functional Programming Ideas in…

Software Engineering Teaching Research

… for modularity and proof reuse

slide-40
SLIDE 40

40

Functional Programming Ideas in…

Software Engineering Teaching Research

… for Modularity and Proof Reuse

slide-41
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
SLIDE 42

Algorithmic Competitions at UCL (2016-2018)

slide-43
SLIDE 43
slide-44
SLIDE 44

The Competitions

  • 2016: Art Gallery Competition

  • 2017: Move-and-Tag Competition

  • 2018: Room Furnishing
slide-45
SLIDE 45

based on:
 
 Chvátal’s Art Gallery Problem (1975)

Art Gallery Competition

slide-46
SLIDE 46
slide-47
SLIDE 47

How many guards do we really need?

The answer depends on the shape of the gallery.

slide-48
SLIDE 48

How many guards do we really need?

The answer depends on the shape of the gallery.

slide-49
SLIDE 49

How many guards do we really need?

slide-50
SLIDE 50

How many guards do we really need?

slide-51
SLIDE 51

How many guards do we really need?

slide-52
SLIDE 52

How many guards do we really need?

slide-53
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
SLIDE 54

Project: Art Gallery Competition

Find the best solutions for a collection of large polygons.

  • 58 vertices
  • 5 guards
slide-55
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
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
SLIDE 57

Growing polygons

Primitive polygons with specific “features”

slide-58
SLIDE 58

Seed

Growing polygons

slide-59
SLIDE 59

Growing polygons

slide-60
SLIDE 60

Growing polygons

slide-61
SLIDE 61

Growing polygons

slide-62
SLIDE 62

Growing polygons

slide-63
SLIDE 63

Growing polygons

slide-64
SLIDE 64

Growing polygons

slide-65
SLIDE 65

Can we enumerate “primitive” polygons 
 and plug arbitrary shapes generators?

65

slide-66
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
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-68
SLIDE 68

68

slide-69
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
SLIDE 70

Generating random polygons

Rectilinear

slide-71
SLIDE 71

Quasi-convex

Generating random polygons

slide-72
SLIDE 72

Crazy

Generating random polygons

slide-73
SLIDE 73

Can we Quick-Check geometric algorithms?

73

slide-74
SLIDE 74

Bug in textbook visibility algorithm

Randomly generated, 260 vertices, guards in every node ??!

slide-75
SLIDE 75

Bug in textbook visibility algorithm

Randomly generated, 260 vertices, guards in every node

slide-76
SLIDE 76

After shrinking: 20 vertices

Bug in textbook visibility algorithm

slide-77
SLIDE 77

Removed irrelevant 
 light sources

Bug in textbook visibility algorithm

slide-78
SLIDE 78

Removed irrelevant 
 light sources

Bug in textbook visibility algorithm

ICFP 2016

slide-79
SLIDE 79

Beyond Classroom: ICFP Programming Contest 2019

Contest Report 


  • n Tuesday, 17:45
slide-80
SLIDE 80

80

Functional Programming Ideas in…

Software Engineering Teaching Research

… for creating fun assignments … for modularity and proof reuse

slide-81
SLIDE 81

81

Functional Programming Ideas in…

Software Engineering Teaching Research

… for modularity and proof reuse … for creating fun assignments

slide-82
SLIDE 82

82

slide-83
SLIDE 83

83

slide-84
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
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+sb5c3K1vbO7p67f9DSMlWENonkUnVC0JQzQZuGU47iaIQh5y2w/H1G/fU6WZFHdmktAghqF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mle6aWaJkDGMKRdSwXEVAfZ7JMcn1hlgCOpbAmDZ+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
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
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
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
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
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
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!