Continuation-Passing Style Transforms Type Theory and Coq Vincent - - PowerPoint PPT Presentation

continuation passing style transforms
SMART_READER_LITE
LIVE PREVIEW

Continuation-Passing Style Transforms Type Theory and Coq Vincent - - PowerPoint PPT Presentation

Continuation-Passing Style Transforms Type Theory and Coq Vincent Koppen 15-05-2018 Paper Title: Polymorphic Type Assignment and CPS Conversion Authors: Robert Harpery and Mark Lillibridge Year: 1993 Journal: lisp and symbolic computation : An


slide-1
SLIDE 1

Continuation-Passing Style Transforms

Type Theory and Coq Vincent Koppen 15-05-2018

slide-2
SLIDE 2

Paper

Title: Polymorphic Type Assignment and CPS Conversion Authors: Robert Harpery and Mark Lillibridge Year: 1993 Journal: lisp and symbolic computation: An International Journal

slide-3
SLIDE 3

Overview

1 Untyped Terms

call-by-value call-by-value (modified) call-by-name

2 Simple Type Assignment

slide-4
SLIDE 4

Overview

1 Untyped Terms

call-by-value call-by-value (modified) call-by-name

2 Simple Type Assignment

slide-5
SLIDE 5

Untyped Terms

Terms e ::= x | λx.e | e1e2 | callcc | throw | let x be e1 in e2 callcc, throw introduced last week let x be e1 in e2: needed for the polymorphic type assignment Values (call-by-value) v ::= x | λx.e| callcc | throw

slide-6
SLIDE 6

Untyped Terms

Terms e ::= x | λx.e | e1e2 | callcc | throw | let x be e1 in e2 callcc, throw introduced last week let x be e1 in e2: needed for the polymorphic type assignment Values (call-by-value) v ::= x | λx.e| callcc | throw

slide-7
SLIDE 7

Untyped Terms

Terms e ::= x | λx.e | e1e2 | callcc | throw | let x be e1 in e2 callcc, throw introduced last week let x be e1 in e2: needed for the polymorphic type assignment Values (call-by-value) v ::= x | λx.e| callcc | throw

slide-8
SLIDE 8

Untyped Terms

Terms e ::= x | λx.e | e1e2 | callcc | throw | let x be e1 in e2 callcc, throw introduced last week let x be e1 in e2: needed for the polymorphic type assignment Values (call-by-value) v ::= x | λx.e| callcc | throw

slide-9
SLIDE 9

CPS Transform (call-by-value)

Definition |v|cbv = λk.k||v||cbv |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) | let x be e1 in e2|cbv = λk.|e1|cbv(λx.|e2|cbvk) ||x||cbv = x ||λx.e||cbv = λx.|e|cbv || callcc ||cbv = λf .λk.fkk || throw ||cbv = λc.λk.k(λx.λl.cx)

Note: | |cbv for terms, || ||cbv for values. callcc gets a function f which has k both as input and as continuation throw discard continuation l and continue with k

slide-10
SLIDE 10

CPS Transform (call-by-value)

Definition |v|cbv = λk.k||v||cbv |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) | let x be e1 in e2|cbv = λk.|e1|cbv(λx.|e2|cbvk) ||x||cbv = x ||λx.e||cbv = λx.|e|cbv || callcc ||cbv = λf .λk.fkk || throw ||cbv = λc.λk.k(λx.λl.cx)

Note: | |cbv for terms, || ||cbv for values. callcc gets a function f which has k both as input and as continuation throw discard continuation l and continue with k

slide-11
SLIDE 11

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-12
SLIDE 12

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-13
SLIDE 13

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-14
SLIDE 14

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-15
SLIDE 15

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-16
SLIDE 16

Example

Let |e1|cbv = λk′.k′||v||cbv, |e2|cbv = λk′′.k′′||w||cbv. We have: |e1e2|cbv = λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = λk.(λk′.k′||v||cbv)(λx1.(λk′′.k′′||w||cbv)(λx2.x1x2k)) →β λk.(λk′.k′||v||cbv)(λx1.(λx2.x1x2k)||w||cbv) →β λk.(λk′.k′||v||cbv)(λx1.x1||w||cbvk) →β λk.(λx1.x1||w||cbvk)||v||cbv →β λk.||v||cbv||w||cbvk

slide-17
SLIDE 17

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-18
SLIDE 18

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-19
SLIDE 19

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-20
SLIDE 20

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-21
SLIDE 21

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-22
SLIDE 22

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-23
SLIDE 23

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-24
SLIDE 24

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 Induction Hypothesis on e1, e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-25
SLIDE 25

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-26
SLIDE 26

CPS commutes with substitution

Lemma ||[v/x]v′||cbv = [||v||cbv/x]||v′||cbv |[v/x]e|cbv = [||v||cbv/x]|e|cbv

Proof: simultaneous induction on the structure of v ′, e. v ′ = x′

x′ = x: ||[v/x]x||cbv = ||v||cbv = [||v||cbv/x]||x||cbv x′ = x: ||[v/x]x′||cbv = ||x′||cbv = [||v||cbv/x]||x′||cbv

e = e1e2 |[v/x](e1e2)|cbv = |[v/x]e1[v/x]e2|cbv = λk.|[v/x]e1|cbv(λx1.|[v/x]e2|cbv(λx2.x1x2k)) = λk.[||v||cbv/x]|e1|cbv(λx1.[||v||cbv/x]|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]λk.|e1|cbv(λx1.|e2|cbv(λx2.x1x2k)) = [||v||cbv/x]|e1e2|cbv

slide-27
SLIDE 27

Overview

1 Untyped Terms

call-by-value call-by-value (modified) call-by-name

2 Simple Type Assignment

slide-28
SLIDE 28

call-by-value (modified)

Define a different call-by-value transform (cbv’), where we require the let-bound to be a value. So: let x be v in e instead of the previous let x be e1 in e2 The new transform is given by | let x be v in e|cbv′ = λk. let x be ||v||cbv′ in (|e|cbv′k)

This is the only difference between cbv and cbv’

slide-29
SLIDE 29

call-by-value (modified)

Define a different call-by-value transform (cbv’), where we require the let-bound to be a value. So: let x be v in e instead of the previous let x be e1 in e2 The new transform is given by | let x be v in e|cbv′ = λk. let x be ||v||cbv′ in (|e|cbv′k)

This is the only difference between cbv and cbv’

slide-30
SLIDE 30

call-by-value (modified)

Define a different call-by-value transform (cbv’), where we require the let-bound to be a value. So: let x be v in e instead of the previous let x be e1 in e2 The new transform is given by | let x be v in e|cbv′ = λk. let x be ||v||cbv′ in (|e|cbv′k)

This is the only difference between cbv and cbv’

slide-31
SLIDE 31

CPS commutes with substitution (cbv’)

The previous lemma also holds for cbv’: Lemma ||[v/x]v′||cbv′ = [||v||cbv′/x]||v′||cbv′ |[v/x]e|cbv′ = [||v||cbv′/x]|e|cbv

slide-32
SLIDE 32

Overview

1 Untyped Terms

call-by-value call-by-value (modified) call-by-name

2 Simple Type Assignment

slide-33
SLIDE 33

CPS tranforms (call-by-name)

Values in call by name: n ::= λx.e| callcc | throw Definition

|n|cbn = λk.k||n||cbn |x|cbn = x |e1e2|cbn = λk.|e1|cbn(λx1.x1|e2|cbnk) | let x be e1 in e2|cbn = λk. let x be |e1|cbn in (|e2|cbnk) ||λx.e||cbn = λx.|e|cbn || callcc ||cbn = λf .λk.f (λf ′.f ′(λl.lk)k) || throw ||cbn = λc.λk.k(λx.λl.c(λc′.x(λx′.c′x′)))

Note the difference with the cbv definitions of callcc, throw

|| callcc ||cbv = λf .λk.fkk || throw ||cbv = λc.λk.k(λx.λl.cx)

slide-34
SLIDE 34

CPS tranforms (call-by-name)

Values in call by name: n ::= λx.e| callcc | throw Definition

|n|cbn = λk.k||n||cbn |x|cbn = x |e1e2|cbn = λk.|e1|cbn(λx1.x1|e2|cbnk) | let x be e1 in e2|cbn = λk. let x be |e1|cbn in (|e2|cbnk) ||λx.e||cbn = λx.|e|cbn || callcc ||cbn = λf .λk.f (λf ′.f ′(λl.lk)k) || throw ||cbn = λc.λk.k(λx.λl.c(λc′.x(λx′.c′x′)))

Note the difference with the cbv definitions of callcc, throw

|| callcc ||cbv = λf .λk.fkk || throw ||cbv = λc.λk.k(λx.λl.cx)

slide-35
SLIDE 35

CPS tranforms (call-by-name)

Values in call by name: n ::= λx.e| callcc | throw Definition

|n|cbn = λk.k||n||cbn |x|cbn = x |e1e2|cbn = λk.|e1|cbn(λx1.x1|e2|cbnk) | let x be e1 in e2|cbn = λk. let x be |e1|cbn in (|e2|cbnk) ||λx.e||cbn = λx.|e|cbn || callcc ||cbn = λf .λk.f (λf ′.f ′(λl.lk)k) || throw ||cbn = λc.λk.k(λx.λl.c(λc′.x(λx′.c′x′)))

Note the difference with the cbv definitions of callcc, throw

|| callcc ||cbv = λf .λk.fkk || throw ||cbv = λc.λk.k(λx.λl.cx)

slide-36
SLIDE 36

CPS commutes with substitution (cbn)

Lemma ||[e/x]n||cbn = [|e|cbn/x]||n||cbn |[e/x]e′|cbn = [|e|cbn/x]|e′|cbn

slide-37
SLIDE 37

Plotkin and Griffin

Plotkin: relation between call-by-value/call-by-name CPS transform and

  • perational semantics of call-by-value/call-by-name for pure λ-terms

Griffin: extended this relation to CPS primitives for the call-by-value case

Theorem (Plotkin,Griffin) The closed expression e evaluates to v under call-by-value iff |e|cbv(λx.x) evaluates to ||v||cbv under either call-by-value or call-by-name

slide-38
SLIDE 38

Plotkin and Griffin

Plotkin: relation between call-by-value/call-by-name CPS transform and

  • perational semantics of call-by-value/call-by-name for pure λ-terms

Griffin: extended this relation to CPS primitives for the call-by-value case

Theorem (Plotkin,Griffin) The closed expression e evaluates to v under call-by-value iff |e|cbv(λx.x) evaluates to ||v||cbv under either call-by-value or call-by-name

slide-39
SLIDE 39

Plotkin and Griffin

Plotkin: relation between call-by-value/call-by-name CPS transform and

  • perational semantics of call-by-value/call-by-name for pure λ-terms

Griffin: extended this relation to CPS primitives for the call-by-value case

Theorem (Plotkin,Griffin) The closed expression e evaluates to v under call-by-value iff |e|cbv(λx.x) evaluates to ||v||cbv under either call-by-value or call-by-name

slide-40
SLIDE 40

Overview

1 Untyped Terms

call-by-value call-by-value (modified) call-by-name

2 Simple Type Assignment

slide-41
SLIDE 41

Simple Type Assignment

Definition: λ→ Types and Contexts

types: τ ::= b | τ1 → τ2 contexts: Γ ::= • | Γ, x : τ

Where b is a base type There is a α in b which represents the “answer” type of a CPS transform.

slide-42
SLIDE 42

Typing Rules for λ→

Γ ⊢ x : Γ(x)

(var)

Γ, x : τ1 ⊢ e : τ2 Γ ⊢ λx.e : τ1 → τ2 (x ∈ dom(Γ))

(abs)

Γ ⊢ e1 : τ1 → τ Γ ⊢ e2 : τ1 Γ ⊢ e1e2 : τ

(app)

Γ ⊢ e1 : τ1 Γ, x : τ1 ⊢ e2 : τ Γ ⊢ let x be e1 in e2 : τ

(mono-let)

The type system λ→ + cont is defined by adding the type expression τ cont and the following typing rules for the continuation-passing primitives: Γ ⊢ callcc : (τ cont → τ) → τ

(callcc)

Γ ⊢ throw : τ cont → τ → τ ′

(throw)

slide-43
SLIDE 43

Typing Rules for λ→

Γ ⊢ x : Γ(x)

(var)

Γ, x : τ1 ⊢ e : τ2 Γ ⊢ λx.e : τ1 → τ2 (x ∈ dom(Γ))

(abs)

Γ ⊢ e1 : τ1 → τ Γ ⊢ e2 : τ1 Γ ⊢ e1e2 : τ

(app)

Γ ⊢ e1 : τ1 Γ, x : τ1 ⊢ e2 : τ Γ ⊢ let x be e1 in e2 : τ

(mono-let)

The type system λ→ + cont is defined by adding the type expression τ cont and the following typing rules for the continuation-passing primitives: Γ ⊢ callcc : (τ cont → τ) → τ

(callcc)

Γ ⊢ throw : τ cont → τ → τ ′

(throw)

slide-44
SLIDE 44

Typing Rules for λ→

Γ ⊢ x : Γ(x)

(var)

Γ, x : τ1 ⊢ e : τ2 Γ ⊢ λx.e : τ1 → τ2 (x ∈ dom(Γ))

(abs)

Γ ⊢ e1 : τ1 → τ Γ ⊢ e2 : τ1 Γ ⊢ e1e2 : τ

(app)

Γ ⊢ e1 : τ1 Γ, x : τ1 ⊢ e2 : τ Γ ⊢ let x be e1 in e2 : τ

(mono-let)

The type system λ→ + cont is defined by adding the type expression τ cont and the following typing rules for the continuation-passing primitives: Γ ⊢ callcc : (τ cont → τ) → τ

(callcc)

Γ ⊢ throw : τ cont → τ → τ ′

(throw)

slide-45
SLIDE 45

Typing Rules for λ→

Γ ⊢ x : Γ(x)

(var)

Γ, x : τ1 ⊢ e : τ2 Γ ⊢ λx.e : τ1 → τ2 (x ∈ dom(Γ))

(abs)

Γ ⊢ e1 : τ1 → τ Γ ⊢ e2 : τ1 Γ ⊢ e1e2 : τ

(app)

Γ ⊢ e1 : τ1 Γ, x : τ1 ⊢ e2 : τ Γ ⊢ let x be e1 in e2 : τ

(mono-let)

The type system λ→ + cont is defined by adding the type expression τ cont and the following typing rules for the continuation-passing primitives: Γ ⊢ callcc : (τ cont → τ) → τ

(callcc)

Γ ⊢ throw : τ cont → τ → τ ′

(throw)

slide-46
SLIDE 46

Typing Rules for λ→

Γ ⊢ x : Γ(x)

(var)

Γ, x : τ1 ⊢ e : τ2 Γ ⊢ λx.e : τ1 → τ2 (x ∈ dom(Γ))

(abs)

Γ ⊢ e1 : τ1 → τ Γ ⊢ e2 : τ1 Γ ⊢ e1e2 : τ

(app)

Γ ⊢ e1 : τ1 Γ, x : τ1 ⊢ e2 : τ Γ ⊢ let x be e1 in e2 : τ

(mono-let)

The type system λ→ + cont is defined by adding the type expression τ cont and the following typing rules for the continuation-passing primitives: Γ ⊢ callcc : (τ cont → τ) → τ

(callcc)

Γ ⊢ throw : τ cont → τ → τ ′

(throw)

slide-47
SLIDE 47

Type Transform for λ→ (call-by-value)

Definition |τ|cbv = (||τ||cbv → α) → α ||b||cbv = b ||τ1 → τ2||cbv = ||τ1||cbv → |τ2|cbv = ||τ1||cbv → (||τ2||cbv → α) → α Extend to contexts by defining ||Γ||cbv(x) = ||Γ(x)||cbv for each x ∈ dom(Γ) Theorem If Γ ⊢ v : τ, then ||Γ||cbv ⊢ ||v||cbv : ||τ||cbv If Γ ⊢ e : τ, then ||Γ||cbv ⊢ |e|cbv : |τ|cbv The call-by-value transform is extended to λ→ + cont by defining ||τ cont ||cbv = ||τ||cbv → α

slide-48
SLIDE 48

Type Transform for λ→ (call-by-value)

Definition |τ|cbv = (||τ||cbv → α) → α ||b||cbv = b ||τ1 → τ2||cbv = ||τ1||cbv → |τ2|cbv = ||τ1||cbv → (||τ2||cbv → α) → α Extend to contexts by defining ||Γ||cbv(x) = ||Γ(x)||cbv for each x ∈ dom(Γ) Theorem If Γ ⊢ v : τ, then ||Γ||cbv ⊢ ||v||cbv : ||τ||cbv If Γ ⊢ e : τ, then ||Γ||cbv ⊢ |e|cbv : |τ|cbv The call-by-value transform is extended to λ→ + cont by defining ||τ cont ||cbv = ||τ||cbv → α

slide-49
SLIDE 49

Type Transform for λ→ (call-by-value)

Definition |τ|cbv = (||τ||cbv → α) → α ||b||cbv = b ||τ1 → τ2||cbv = ||τ1||cbv → |τ2|cbv = ||τ1||cbv → (||τ2||cbv → α) → α Extend to contexts by defining ||Γ||cbv(x) = ||Γ(x)||cbv for each x ∈ dom(Γ) Theorem If Γ ⊢ v : τ, then ||Γ||cbv ⊢ ||v||cbv : ||τ||cbv If Γ ⊢ e : τ, then ||Γ||cbv ⊢ |e|cbv : |τ|cbv The call-by-value transform is extended to λ→ + cont by defining ||τ cont ||cbv = ||τ||cbv → α

slide-50
SLIDE 50

Type Transform for λ→ (call-by-name)

Definition |τ|cbn = (||τ||cbn → α) → α ||b||cbn = b ||τ1 → τ2||cbn = |τ1|cbn → |τ2|cbn Extend to contexts by defining |Γ|cbn(x) = |Γ(x)|cbn for each x ∈ dom(Γ) Theorem If Γ ⊢ n : τ, then |Γ|cbn ⊢ ||v||cbn : ||τ||cbn If Γ ⊢ e : τ, then |Γ|cbn ⊢ |e|cbn : |τ|cbn The call-by-name transform is extended to λ→ + cont by defining ||τ cont ||cbn = ||τ||cbn → α

slide-51
SLIDE 51

Type Transform for λ→ (call-by-name)

Definition |τ|cbn = (||τ||cbn → α) → α ||b||cbn = b ||τ1 → τ2||cbn = |τ1|cbn → |τ2|cbn Extend to contexts by defining |Γ|cbn(x) = |Γ(x)|cbn for each x ∈ dom(Γ) Theorem If Γ ⊢ n : τ, then |Γ|cbn ⊢ ||v||cbn : ||τ||cbn If Γ ⊢ e : τ, then |Γ|cbn ⊢ |e|cbn : |τ|cbn The call-by-name transform is extended to λ→ + cont by defining ||τ cont ||cbn = ||τ||cbn → α

slide-52
SLIDE 52

Type Transform for λ→ (call-by-name)

Definition |τ|cbn = (||τ||cbn → α) → α ||b||cbn = b ||τ1 → τ2||cbn = |τ1|cbn → |τ2|cbn Extend to contexts by defining |Γ|cbn(x) = |Γ(x)|cbn for each x ∈ dom(Γ) Theorem If Γ ⊢ n : τ, then |Γ|cbn ⊢ ||v||cbn : ||τ||cbn If Γ ⊢ e : τ, then |Γ|cbn ⊢ |e|cbn : |τ|cbn The call-by-name transform is extended to λ→ + cont by defining ||τ cont ||cbn = ||τ||cbn → α