Reduction strategies for CBN and CBV Structural operational style - - PowerPoint PPT Presentation

reduction strategies for cbn and cbv
SMART_READER_LITE
LIVE PREVIEW

Reduction strategies for CBN and CBV Structural operational style - - PowerPoint PPT Presentation

Reduction strategies for CBN and CBV Structural operational style and Felleisen style Bas Broere April 24th 2018 Bas Broere Reduction strategies for CBN and CBV 1/22 Introduction Disclaimer I have (almost) no experience in functional


slide-1
SLIDE 1

Reduction strategies for CBN and CBV

Structural operational style and Felleisen style Bas Broere April 24th 2018

Bas Broere — Reduction strategies for CBN and CBV 1/22

slide-2
SLIDE 2

Introduction

Disclaimer I have (almost) no experience in functional programming.

Bas Broere — Reduction strategies for CBN and CBV 2/22

slide-3
SLIDE 3

Introduction

Disclaimer I have (almost) no experience in functional programming. Question: What is a functional programming language?

Bas Broere — Reduction strategies for CBN and CBV 2/22

slide-4
SLIDE 4

Introduction

Disclaimer I have (almost) no experience in functional programming. Question: What is a functional programming language? ”A language that takes functions seriously”

Bas Broere — Reduction strategies for CBN and CBV 2/22

slide-5
SLIDE 5

Introduction

Disclaimer I have (almost) no experience in functional programming. Question: What is a functional programming language? ”A language that takes functions seriously” A language that emphasises what needs to be calculated, instead of how this needs to be done, i.e. what does it mean to calculate something?

Bas Broere — Reduction strategies for CBN and CBV 2/22

slide-6
SLIDE 6

Introduction

Disclaimer I have (almost) no experience in functional programming. Question: What is a functional programming language? ”A language that takes functions seriously” A language that emphasises what needs to be calculated, instead of how this needs to be done, i.e. what does it mean to calculate something? Important: It supports passing functions as arguments, returning them as value etc. Basically, you can do everything with them.

Bas Broere — Reduction strategies for CBN and CBV 2/22

slide-7
SLIDE 7

Setting up an FP language

Bas Broere — Reduction strategies for CBN and CBV 3/22

slide-8
SLIDE 8

Setting up an FP language

1 Take a λ-calculus, so:

Terms: M, N ::= x | λx.M | M N; Define β-reduction as the smallest congruence relation s.t. (λx.M) N →β M[x := N];

Bas Broere — Reduction strategies for CBN and CBV 3/22

slide-9
SLIDE 9

Setting up an FP language

1 Take a λ-calculus, so:

Terms: M, N ::= x | λx.M | M N; Define β-reduction as the smallest congruence relation s.t. (λx.M) N →β M[x := N];

2 Choose your reduction strategy (topic of today);

Bas Broere — Reduction strategies for CBN and CBV 3/22

slide-10
SLIDE 10

Setting up an FP language

1 Take a λ-calculus, so:

Terms: M, N ::= x | λx.M | M N; Define β-reduction as the smallest congruence relation s.t. (λx.M) N →β M[x := N];

2 Choose your reduction strategy (topic of today); 3 Add some stuff like primitive data-types and operations;

Bas Broere — Reduction strategies for CBN and CBV 3/22

slide-11
SLIDE 11

Setting up an FP language

1 Take a λ-calculus, so:

Terms: M, N ::= x | λx.M | M N; Define β-reduction as the smallest congruence relation s.t. (λx.M) N →β M[x := N];

2 Choose your reduction strategy (topic of today); 3 Add some stuff like primitive data-types and operations; 4 Make an execution model.

Bas Broere — Reduction strategies for CBN and CBV 3/22

slide-12
SLIDE 12

Evaluation strategies

First: Choose evaluation strategy.

Bas Broere — Reduction strategies for CBN and CBV 4/22

slide-13
SLIDE 13

Evaluation strategies

First: Choose evaluation strategy. Difference evaluation and reduction strategies An evaluation strategy tells you when you need to evaluate an expression.

Bas Broere — Reduction strategies for CBN and CBV 4/22

slide-14
SLIDE 14

Evaluation strategies

First: Choose evaluation strategy. Difference evaluation and reduction strategies An evaluation strategy tells you when you need to evaluate an expression. A reduction strategy tells you how to reduce a complex expression.

Bas Broere — Reduction strategies for CBN and CBV 4/22

slide-15
SLIDE 15

Evaluation strategies

Idea:

Bas Broere — Reduction strategies for CBN and CBV 5/22

slide-16
SLIDE 16

Evaluation strategies

Idea: Add integer value n to terms: M, N ::= n | x | λx.M | M N Values (results of evaluations): v ::= n | λx.M

Bas Broere — Reduction strategies for CBN and CBV 5/22

slide-17
SLIDE 17

Evaluation strategies

Idea: Add integer value n to terms: M, N ::= n | x | λx.M | M N Values (results of evaluations): v ::= n | λx.M Call-by-value (CBV) The argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function.

Bas Broere — Reduction strategies for CBN and CBV 5/22

slide-18
SLIDE 18

Evaluation strategies

Idea: Add integer value n to terms: M, N ::= n | x | λx.M | M N Values (results of evaluations): v ::= n | λx.M Call-by-value (CBV) The argument expression is evaluated, and the resulting value is bound to the corresponding variable in the function. Call-by-name (CBN) The arguments to a function are not evaluated before the function is called.

Bas Broere — Reduction strategies for CBN and CBV 5/22

slide-19
SLIDE 19

Reduction strategies for CBV

Structural operation style (SOS) One-step reductions: (λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

Bas Broere — Reduction strategies for CBN and CBV 6/22

slide-20
SLIDE 20

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-21
SLIDE 21

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-22
SLIDE 22

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-23
SLIDE 23

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-24
SLIDE 24

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-25
SLIDE 25

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-26
SLIDE 26

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.x) 1 → 1 (λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 7/22

slide-27
SLIDE 27

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) [ ((λx.x) 1) (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-28
SLIDE 28

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) [ ((λx.x) 1) (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-29
SLIDE 29

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) [ ((λx.x) 1) (λx.x) ] → (λx.λy.y x) [ 1 (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-30
SLIDE 30

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.λy.y x) [ ((λx.x) 1) (λx.x) ] → (λx.λy.y x) [ 1 (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-31
SLIDE 31

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

((λx.x) 1) (λx.x) → 1 (λx.x) (λx.λy.y x) [ ((λx.x) 1) (λx.x) ] → (λx.λy.y x) [ 1 (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-32
SLIDE 32

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

((λx.x) 1) (λx.x) → 1 (λx.x) (λx.λy.y x) [ ((λx.x) 1) (λx.x) ] → (λx.λy.y x) [ 1 (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-33
SLIDE 33

Examples

Recution steps

(λx.M) v → M[x := v] (βv) M → M′ M N → M′ N (app-l) N → N′ v N → v N′ (app-r)

(λx.x) 1 → 1 ((λx.x) 1) (λx.x) → 1 (λx.x) (λx.λy.y x) [ ((λx.x) 1) (λx.x) ] → (λx.λy.y x) [ 1 (λx.x) ]

Bas Broere — Reduction strategies for CBN and CBV 8/22

slide-34
SLIDE 34

Features/limitations of SOS in CBV

We cannot reduce under λ-abstractions (weak reduction). So this is NOT a thing:

M → M′ λx.M → λx.M′

Bas Broere — Reduction strategies for CBN and CBV 9/22

slide-35
SLIDE 35

Features/limitations of SOS in CBV

We cannot reduce under λ-abstractions (weak reduction). So this is NOT a thing:

M → M′ λx.M → λx.M′

We need to reduce N to a value if we want to evaluate (λx.M)N (call-by-value). So also this is NOT a thing:

(λx.M)(N P) → M[x := N P]

Bas Broere — Reduction strategies for CBN and CBV 9/22

slide-36
SLIDE 36

Features/limitations of SOS in CBV

We cannot reduce under λ-abstractions (weak reduction). So this is NOT a thing:

M → M′ λx.M → λx.M′

We need to reduce N to a value if we want to evaluate (λx.M)N (call-by-value). So also this is NOT a thing:

(λx.M)(N P) → M[x := N P]

In the application M N, we need to reduce M to a value first (left-to-right), because:

N → N′ v N → v N′ (app-r).

Bas Broere — Reduction strategies for CBN and CBV 9/22

slide-37
SLIDE 37

Features/limitations of SOS in CBV

We cannot reduce under λ-abstractions (weak reduction). So this is NOT a thing:

M → M′ λx.M → λx.M′

We need to reduce N to a value if we want to evaluate (λx.M)N (call-by-value). So also this is NOT a thing:

(λx.M)(N P) → M[x := N P]

In the application M N, we need to reduce M to a value first (left-to-right), because:

N → N′ v N → v N′ (app-r).

It is deterministic, i.e. for every M there is at most one M′ s.t. M → M′.

Bas Broere — Reduction strategies for CBN and CBV 9/22

slide-38
SLIDE 38

Multiple reductions

When making a reduction sequence we have 3 possibilities:

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-39
SLIDE 39

Multiple reductions

When making a reduction sequence we have 3 possibilities:

1 The reductions end in a value (termination):

M → M1 → M2 → . . . → v.

Example: (λx.λy.y x) ((λx.x)1) (λx.x) → (λx.λy.y x) 1 (λx.x) → (λy.y 1) (λx.x) → (λx.x) 1 → 1

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-40
SLIDE 40

Multiple reductions

When making a reduction sequence we have 3 possibilities:

1 The reductions end in a value (termination):

M → M1 → M2 → . . . → v.

Example: (λx.λy.y x) ((λx.x)1) (λx.x) → (λx.λy.y x) 1 (λx.x) → (λy.y 1) (λx.x) → (λx.x) 1 → 1

2 The reduction never ends (divergence):

M → M1 → M2 → . . . → Mn → . . ..

Example:

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-41
SLIDE 41

Multiple reductions

When making a reduction sequence we have 3 possibilities:

1 The reductions end in a value (termination):

M → M1 → M2 → . . . → v.

Example: (λx.λy.y x) ((λx.x)1) (λx.x) → (λx.λy.y x) 1 (λx.x) → (λy.y 1) (λx.x) → (λx.x) 1 → 1

2 The reduction never ends (divergence):

M → M1 → M2 → . . . → Mn → . . ..

Example: ω ω, where ω = λx.x x.

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-42
SLIDE 42

Multiple reductions

When making a reduction sequence we have 3 possibilities:

1 The reductions end in a value (termination):

M → M1 → M2 → . . . → v.

Example: (λx.λy.y x) ((λx.x)1) (λx.x) → (λx.λy.y x) 1 (λx.x) → (λy.y 1) (λx.x) → (λx.x) 1 → 1

2 The reduction never ends (divergence):

M → M1 → M2 → . . . → Mn → . . ..

Example: ω ω, where ω = λx.x x.

3 Error: M → M1 → M2 → . . . → Mn →, where Mn is not

a value, but also does not reduce.

Example:

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-43
SLIDE 43

Multiple reductions

When making a reduction sequence we have 3 possibilities:

1 The reductions end in a value (termination):

M → M1 → M2 → . . . → v.

Example: (λx.λy.y x) ((λx.x)1) (λx.x) → (λx.λy.y x) 1 (λx.x) → (λy.y 1) (λx.x) → (λx.x) 1 → 1

2 The reduction never ends (divergence):

M → M1 → M2 → . . . → Mn → . . ..

Example: ω ω, where ω = λx.x x.

3 Error: M → M1 → M2 → . . . → Mn →, where Mn is not

a value, but also does not reduce.

Example: ω 2 → 2 2 →.

Bas Broere — Reduction strategies for CBN and CBV 10/22

slide-44
SLIDE 44

Reduction strategies for CBV

Reduction contexts (felleisen style) Head reductions: (λx.M) v →ǫ M[x := v] (βv)

Bas Broere — Reduction strategies for CBN and CBV 11/22

slide-45
SLIDE 45

Reduction strategies for CBV

Reduction contexts (felleisen style) Head reductions: (λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), with E ::= [ ] | E b | v E terms with a hole [ ] in it.

Bas Broere — Reduction strategies for CBN and CBV 11/22

slide-46
SLIDE 46

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-47
SLIDE 47

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-48
SLIDE 48

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-49
SLIDE 49

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-50
SLIDE 50

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-51
SLIDE 51

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-52
SLIDE 52

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-53
SLIDE 53

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-54
SLIDE 54

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x) E = [ ]

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-55
SLIDE 55

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x) E = [ ] → (λx.x) 1

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-56
SLIDE 56

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x) E = [ ] → (λx.x) 1 E = [ ]

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-57
SLIDE 57

Example

Reduction contexts (Felleisen style)

(λx.M) v →ǫ M[x := v] (βv) M →ǫ M′ E[M] → E[M′] (context), E ::= [ ] | E b | v E

Same example as before: (λx.λy.y x) ((λx.x) 1) (λx.x) E = (λx.λy.x y) [ ] (λx.x) → (λx.λy.y x) 1 (λx.x) E = [ ] (λx.x) → (λy.y 1) (λx.x) E = [ ] → (λx.x) 1 E = [ ] → 1

Bas Broere — Reduction strategies for CBN and CBV 12/22

slide-58
SLIDE 58

Equivalence SOS and Felleisen

Remember the earlier example: (λx.x) 1 → 1 (λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x) This corresponds to the context:

Bas Broere — Reduction strategies for CBN and CBV 13/22

slide-59
SLIDE 59

Equivalence SOS and Felleisen

Remember the earlier example: (λx.x) 1 → 1 (λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x) This corresponds to the context: E =

Bas Broere — Reduction strategies for CBN and CBV 13/22

slide-60
SLIDE 60

Equivalence SOS and Felleisen

Remember the earlier example: (λx.x) 1 → 1 (λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x) This corresponds to the context: E = ((λx.λy.y x) [ ]) (λx.x) with head reduction (λx.x) 1 →ǫ 1.

Bas Broere — Reduction strategies for CBN and CBV 13/22

slide-61
SLIDE 61

Equivalence SOS and Felleisen

Remember the earlier example: (λx.x) 1 → 1 (λx.λy.y x) ((λx.x) 1) → (λx.λy.y x) 1 (λx.λy.y x) ((λx.x) 1) (λx.x) → (λx.λy.y x) 1 (λx.x) This corresponds to the context: E = ((λx.λy.y x) [ ]) (λx.x) with head reduction (λx.x) 1 →ǫ 1. Equivalence SOS and Felleisen approach are one-to-one!

Bas Broere — Reduction strategies for CBN and CBV 13/22

slide-62
SLIDE 62

Result

From this equivalence, it follow that Felleisen style also has the previously discussed properties: Left-to-right, Call-by-value, Weak reduction, Deterministic*.

Bas Broere — Reduction strategies for CBN and CBV 14/22

slide-63
SLIDE 63

Result

From this equivalence, it follow that Felleisen style also has the previously discussed properties: Left-to-right, Call-by-value, Weak reduction, Deterministic*. *: The determinism is also a consequence of the following theorem: Unique decomposition theorem For all terms M, there exists at most one reduction context E and one term N, s.t. M = E[N] and N can reduce by head-reduction.

Bas Broere — Reduction strategies for CBN and CBV 14/22

slide-64
SLIDE 64

Result

From this equivalence, it follow that Felleisen style also has the previously discussed properties: Left-to-right, Call-by-value, Weak reduction, Deterministic*. *: The determinism is also a consequence of the following theorem: Unique decomposition theorem For all terms M, there exists at most one reduction context E and one term N, s.t. M = E[N] and N can reduce by head-reduction. Proof: Induction on the terms.

Bas Broere — Reduction strategies for CBN and CBV 14/22

slide-65
SLIDE 65

Reduction strategies for CBN

Call-by-name The arguments to a function are not evaluated before the function is called (perform β-reduction asap).

Bas Broere — Reduction strategies for CBN and CBV 15/22

slide-66
SLIDE 66

Reduction strategies for CBN

Call-by-name The arguments to a function are not evaluated before the function is called (perform β-reduction asap). Structural operation style (SOS) (λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

Bas Broere — Reduction strategies for CBN and CBV 15/22

slide-67
SLIDE 67

Reduction strategies for CBN

Call-by-name The arguments to a function are not evaluated before the function is called (perform β-reduction asap). Structural operation style (SOS) (λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l) Felleisen style (λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), with E ::= [ ] | E M

Bas Broere — Reduction strategies for CBN and CBV 15/22

slide-68
SLIDE 68

Reduction strategies for CBN

Call-by-name The arguments to a function are not evaluated before the function is called (perform β-reduction asap). Structural operation style (SOS) (λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l) Felleisen style (λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), with E ::= [ ] | E M, so E ::= [ ] M1 . . . Mn.

Bas Broere — Reduction strategies for CBN and CBV 15/22

slide-69
SLIDE 69

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-70
SLIDE 70

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-71
SLIDE 71

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-72
SLIDE 72

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-73
SLIDE 73

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z) → (λz.z) ((λx.x) 1)

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-74
SLIDE 74

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z) → (λz.z) ((λx.x) 1) → (λx.x) 1

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-75
SLIDE 75

Examples

Structural operation style (SOS)

(λx.M)N → M[x := N] (βn) M → M′ M N → M′ N (app-l)

The same example once again, in SOS-style: (λx.λy.y x) ((λx.x) 1) (λz.z) → (λy.y ((λx.x) 1)) (λz.z) → (λz.z) ((λx.x) 1) → (λx.x) 1 → 1.

Bas Broere — Reduction strategies for CBN and CBV 16/22

slide-76
SLIDE 76

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-77
SLIDE 77

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λx.x)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-78
SLIDE 78

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-79
SLIDE 79

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-80
SLIDE 80

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-81
SLIDE 81

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ]

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-82
SLIDE 82

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ] → (λz.z) ((λx.x) 1)

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-83
SLIDE 83

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ] → (λz.z) ((λx.x) 1) E = [ ]

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-84
SLIDE 84

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ] → (λz.z) ((λx.x) 1) E = [ ] → (λx.x) 1

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-85
SLIDE 85

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ] → (λz.z) ((λx.x) 1) E = [ ] → (λx.x) 1 E = [ ]

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-86
SLIDE 86

Examples

Felleisen style

(λx.M)N → M[x := N] (βn) M → M′ E[M] → E[M′](context), E ::= [ ] | E M.

And in Felleisen style: (λx.λy.y x) ((λx.x) 1) (λz.z) E = [ ] (λz.z) → (λy.y ((λx.x) 1)) (λz.z) E = [ ] → (λz.z) ((λx.x) 1) E = [ ] → (λx.x) 1 E = [ ] → 1.

Bas Broere — Reduction strategies for CBN and CBV 17/22

slide-87
SLIDE 87

CBV vs CBN

Some examples: Termination:

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-88
SLIDE 88

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) →

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-89
SLIDE 89

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1;

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-90
SLIDE 90

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) →

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-91
SLIDE 91

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . .

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-92
SLIDE 92

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around.

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-93
SLIDE 93

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around. Efficiency:

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-94
SLIDE 94

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around. Efficiency: (λx.x + x) M:

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-95
SLIDE 95

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around. Efficiency: (λx.x + x) M: CBV evaluates M once, CBN twice;

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-96
SLIDE 96

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around. Efficiency: (λx.x + x) M: CBV evaluates M once, CBN twice; (λx.1) M:

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-97
SLIDE 97

CBV vs CBN

Some examples: Termination: CBN: (λx.1) (ω ω) → 1; CBV: (λx.1) (ω ω) → (λx.1) (ω ω) → . . . In general: Termination in CBV implies termination in CBN, but not the other way around. Efficiency: (λx.x + x) M: CBV evaluates M once, CBN twice; (λx.1) M: CBV evaluates M once, CBN not at all.

Bas Broere — Reduction strategies for CBN and CBV 18/22

slide-98
SLIDE 98

CBN in CBV language

Bas Broere — Reduction strategies for CBN and CBV 19/22

slide-99
SLIDE 99

CBN in CBV language

Introduce functions λx.M to delay computation of M (thunks).

Bas Broere — Reduction strategies for CBN and CBV 19/22

slide-100
SLIDE 100

CBN in CBV language

Introduce functions λx.M to delay computation of M (thunks).

1 x = x () (application);

Bas Broere — Reduction strategies for CBN and CBV 19/22

slide-101
SLIDE 101

CBN in CBV language

Introduce functions λx.M to delay computation of M (thunks).

1 x = x () (application); 2 λx.M = λx.M;

Bas Broere — Reduction strategies for CBN and CBV 19/22

slide-102
SLIDE 102

CBN in CBV language

Introduce functions λx.M to delay computation of M (thunks).

1 x = x () (application); 2 λx.M = λx.M; 3 M N = M (λx.N),

where () is a constant of type unit and x ∈ FV (N).

Bas Broere — Reduction strategies for CBN and CBV 19/22

slide-103
SLIDE 103

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

=λx.x + x (λy.M)

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-104
SLIDE 104

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-105
SLIDE 105

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-106
SLIDE 106

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-107
SLIDE 107

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

1

=(λx.x () + x ()) (λy.M)

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-108
SLIDE 108

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

1

=(λx.x () + x ()) (λy.M) →β(λy.M) () + (λy.M )()

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-109
SLIDE 109

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

1

=(λx.x () + x ()) (λy.M) →β(λy.M) () + (λy.M )() →βM + M

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-110
SLIDE 110

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

1

=(λx.x () + x ()) (λy.M) →β(λy.M) () + (λy.M )() →βM + M

1

=M () + M () →β M + M

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-111
SLIDE 111

Example

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

(λx.x + x) M

3

= λx.x + x (λy.M)

2

=(λx.x + x) (λy.M)

1

=(λx.x () + x ()) (λy.M) →β(λy.M) () + (λy.M )() →βM + M

1

=M () + M () →β M + M So we need to evaluate M twice now, just as in CBN.

Bas Broere — Reduction strategies for CBN and CBV 20/22

slide-112
SLIDE 112

Types

Rules

x

1

= x (), λx.M

2

= λx.M M N

3

= M (λx.N)

Effect on types:

1 M : A, then M : A; 2 M : A → B, then M : (unit → A) → B; 3 basetype = basetype.

Bas Broere — Reduction strategies for CBN and CBV 21/22

slide-113
SLIDE 113

CBV in CBN language

This is hard!

Bas Broere — Reduction strategies for CBN and CBV 22/22

slide-114
SLIDE 114

CBV in CBN language

This is hard! We will read about it :)

Bas Broere — Reduction strategies for CBN and CBV 22/22