Logic and Computationcs.ioc.ee/ewscs/2019/ariola/ariola-lecture2-slides.pdf · Logic and...

Preview:

Citation preview

Logic and ComputationLecture 2

Zena M. Ariola

University of Oregon

24th Estonian Winter School in Computer Science, EWSCS ’19

Curry-Howard isomorphism

A correspondence between

minimal propositional logic and simply typed lambda-calculus

Types (→,+,×) are Propositions (→,∧,∨)

Terms are Proofs

Computation is Eliminations of detours

Extensionality is Expansion

A system is both a programming language and a logic (Coq, Agda, Idris)

Outline

Extend the isomorphism to more expressive systems

Logic Type Theory

Second-order propositional logic PolymorphismIntuitionistic logic λ-calculus + Abort

Classical logic λ-calculus + Jumps

Compilation ≈ logical embeddings

Outline

Extend the isomorphism to more expressive systems

Logic Type Theory

Second-order propositional logic Polymorphism

Intuitionistic logic λ-calculus + AbortClassical logic λ-calculus + Jumps

Compilation ≈ logical embeddings

Outline

Extend the isomorphism to more expressive systems

Logic Type Theory

Second-order propositional logic PolymorphismIntuitionistic logic λ-calculus + Abort

Classical logic λ-calculus + Jumps

Compilation ≈ logical embeddings

Outline

Extend the isomorphism to more expressive systems

Logic Type Theory

Second-order propositional logic PolymorphismIntuitionistic logic λ-calculus + Abort

Classical logic λ-calculus + Jumps

Compilation ≈ logical embeddings

Outline

Extend the isomorphism to more expressive systems

Logic Type Theory

Second-order propositional logic PolymorphismIntuitionistic logic λ-calculus + Abort

Classical logic λ-calculus + Jumps

Compilation ≈ logical embeddings

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→I

A→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

Minimal Second-order Propositional Logic

A ` A Ax

` A→ A→IA→ B ` A→ B Ax

` (A→ B)→ (A→ B)→I

A ∧ B ` A ∧ B Ax

` (A ∧ B)→ (A ∧ B)→I

How do we express the fact that they are the same proof?

X ` X Ax

` X → X→I

X ` X Ax

` X → X→I

` ∀X .X → X ∀I

What about this proof?

X ` X Ax

X ` ∀X .X ∀I

X ` XX ` ∀X .X ∀IX ` B

∀E` X → B

→I

Γ ` A X does not occur free in ΓΓ ` ∀X .A ∀I

Γ ` ∀X .A no variable capture occursΓ ` A[B/X ]

∀E

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

System F - Jean-Yves Girard, 1972

Girard believed in Howard’s approach that proofs aremathematical objects. He introduced System F as arepresentations of proofs in second-order propositional logic

A ∧ B = ∀X .(A→ B→ X)→ XA ∨ B = ∀X .(A→ X)→ (B→ X)→ X⊥ = ∀X .Xnat = ∀X .X → (X → X)→ Xbool = ∀X .X → X → X

If Γ ` M : A then it does not exists an infinite reduction starting fromM

System F - Jean-Yves Girard, 1972

Girard believed in Howard’s approach that proofs aremathematical objects. He introduced System F as arepresentations of proofs in second-order propositional logic

A ∧ B = ∀X .(A→ B→ X)→ XA ∨ B = ∀X .(A→ X)→ (B→ X)→ X⊥ = ∀X .Xnat = ∀X .X → (X → X)→ Xbool = ∀X .X → X → X

If Γ ` M : A then it does not exists an infinite reduction starting fromM

System F - Jean-Yves Girard, 1972

Girard believed in Howard’s approach that proofs aremathematical objects. He introduced System F as arepresentations of proofs in second-order propositional logic

A ∧ B = ∀X .(A→ B→ X)→ XA ∨ B = ∀X .(A→ X)→ (B→ X)→ X⊥ = ∀X .Xnat = ∀X .X → (X → X)→ Xbool = ∀X .X → X → X

If Γ ` M : A then it does not exists an infinite reduction starting fromM

Programming: Polymorphism

The simple type system we have seen so far forces us to duplicate code:

sortI: int list -> (int->int->bool)->int list

sortR: real list->(real->real->bool)->real list

Weaken the type system by introducing a universal type :void qsort (void* base, int num, int size,

int (*comparator)(const void*,const void*));

Enrich the type system by allowing to express the fact that the function’sbehavior is uniform for di�erent type instantiation

Programming: Polymorphism

The simple type system we have seen so far forces us to duplicate code:

sortI: int list -> (int->int->bool)->int list

sortR: real list->(real->real->bool)->real list

Weaken the type system by introducing a universal type :void qsort (void* base, int num, int size,

int (*comparator)(const void*,const void*));

Enrich the type system by allowing to express the fact that the function’sbehavior is uniform for di�erent type instantiation

Programming: Polymorphism

The simple type system we have seen so far forces us to duplicate code:

sortI: int list -> (int->int->bool)->int list

sortR: real list->(real->real->bool)->real list

Weaken the type system by introducing a universal type :void qsort (void* base, int num, int size,

int (*comparator)(const void*,const void*));

Enrich the type system by allowing to express the fact that the function’sbehavior is uniform for di�erent type instantiation

Polymorphic lambda calculus - John Reynold, 1974

Given the expressions M= (2+2)+(2+2) and N = (3+3)+(3+3)we are accustomed to abstract over the expressions2+2 and 3+3 giving the function λx.x + x so thatM=(λx.x + x) (2+2) N =(λx.x + x) (3+3)

Given the types :

τ = int list -> (int->int->bool)->int list σ = real list->(real->real->bool)->real list

why not abstracting over the types int and real giving the function type:forall α.α list -> (α -> α -> bool) -> α list

so thatτ = ( forall α.α list -> (α -> α -> bool) -> α list) intσ = (forall α.α list -> (α -> α -> bool) -> α list) real

The same idea to avoid duplication of code applies to avoid replication at the typelevel

Polymorphic lambda calculus - John Reynold, 1974

Terms M ::= λx : σ.M | MM | x | Λα.M | M [σ]

Types σ ::= α | σ → σ | ∀α.σ

Type system Γ, x : σ ` x : σ

Γ ` M : σ → τ Γ ` N : σΓ ` MN : τ

Γ, x : σ ` M : τ

Γ ` λx : σ.M : σ → τ

Γ ` M : σ α not free in ΓΓ ` Λα.M : ∀α.σ

Γ ` M : ∀α.σΓ ` M[τ ] : σ[τ/α]

Reduction (λx.M)N → M[N/x] (Λα.M) [σ]→ M[σ/α]

Expansion λx.Mx → M Λα.M [α]→ M

Proofs are terms

` (∀X .X)→ A ` A→ ∀X .((A→ X)→ X) 6` A→ ∀X .X

` (∀α.α)→ σ ` σ → ∀α.((σ → α)→ α) 6` σ → ∀α.α

z : ∀α.α ` z : ∀α.αAx

z : ∀α.α ` z[σ] : σ∀E

` λz : (∀α.α).z[σ] : (∀α.α)→ σ→ I

z : σ, y : σ → α ` y : σ → αAx z : σ, y : σ → α ` z : σ

Ax

z : σ, y : σ → α ` y z : σ→E

z : σ ` λy : σ → α.y z : (σ → α)→ α→ I

z : σ ` Λα.λy : σ → α.y z : ∀α.((σ → α)→ α)∀I

` λz : σ.Λα.λy : σ → α.y z : σ → ∀α.((σ → α)→ α)→ I

Barendregt’s lambda cube

λω λC

λ2 λP2

λω λPω

λ→ λP

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Intuitionistic Logic

Intuitionistic Logic = Minimal Logic + Ex Falso �odlibet

Formulae: A,B ::= X | A→ B | A ∧ B | A ∨ B | ⊥ ¬A = A→ ⊥

No introduction rule for ⊥

One elimination rule for ⊥ (Ex Falso �odlibet):

Γ ` ⊥Γ ` A

EFQ

Local reduction:

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

Computational interpretation of IL

What are terms of type σ → ⊥?

They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What are terms of type σ → ⊥? They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What are terms of type σ → ⊥? They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What are terms of type σ → ⊥? They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What are terms of type σ → ⊥? They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What are terms of type σ → ⊥? They are special functions: they never return. Wecall these functions continuations

One predefined continuation is the top-level also called the prompt - tp

Invoking the top-level means aborting the program.

Terms: M ::= x | λx.M | MM | Abort M

fun product nil = 0| product (x :: xs) = if x=0 then Abort 0 else x ∗( prod xs)

Computational interpretation of IL

What is the type of Abort 0?

Abort 0 + 2 (Abort 0) 9 if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ? Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Computational interpretation of IL

What is the type of Abort 0?Abort 0 + 2

(Abort 0) 9 if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ? Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Computational interpretation of IL

What is the type of Abort 0?Abort 0 + 2 (Abort 0) 9

if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ? Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Computational interpretation of IL

What is the type of Abort 0?Abort 0 + 2 (Abort 0) 9 if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ? Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Computational interpretation of IL

What is the type of Abort 0?Abort 0 + 2 (Abort 0) 9 if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ?

Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Computational interpretation of IL

What is the type of Abort 0?Abort 0 + 2 (Abort 0) 9 if Abort 0 then...else.....

It seems that Abort 0 can have any type. So we have:

Γ ` M :?Γ ` Abort M : σ

What is the restriction on M ? Whatever the top-level is expecting!

Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` Abort M : σ

Exampletp : ¬int ` Abort 6 : σ tp : ¬bool ` Abort true : σ tp : ¬bool 6` Abort 5 : σ

Abort M should be read as “ throw to the top-level M": throw tpMΓ, tp : ¬τ ` tp : ¬τ Γ, tp : ¬τ ` M : τ

Γ, tp : ¬τ ` tpM : ⊥ →E

Γ, tp : ¬τ ` throw (tpM) : σEFQ

Evaluation semantics

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

product([2,3,0,9,8])=2*product([3,0,9,8])=2*3*product([0,9,8])=2*3*Abort 0=2*Abort 0=Abort 0

(λx.M)N → M[N/x]

(Abort M)N → Abort M

Example

What is the result of (λx.Abort 0)(Abort 1)?

Evaluation semantics

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

product([2,3,0,9,8])=2*product([3,0,9,8])=2*3*product([0,9,8])=2*3*Abort 0=2*Abort 0=Abort 0

(λx.M)N → M[N/x]

(Abort M)N → Abort M

Example

What is the result of (λx.Abort 0)(Abort 1)?

Evaluation semantics

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

product([2,3,0,9,8])=2*product([3,0,9,8])=2*3*product([0,9,8])=2*3*Abort 0=2*Abort 0=Abort 0

(λx.M)N → M[N/x]

(Abort M)N → Abort M

Example

What is the result of (λx.Abort 0)(Abort 1)?

Evaluation semantics

E⊥A

EFQ

DB

=⇒

E⊥B

EFQ

product([2,3,0,9,8])=2*product([3,0,9,8])=2*3*product([0,9,8])=2*3*Abort 0=2*Abort 0=Abort 0

(λx.M)N → M[N/x]

(Abort M)N → Abort M

Example

What is the result of (λx.Abort 0)(Abort 1)?

Call-by-value (CBV) and Call-by-name (CBN)

Call-by-name Evaluation Contexts and the notion of Values:

E ::= � | E M V ::= M

Call-by-value Evaluation Contexts and the notion of Values:

E ::= � | E M | V E V ::= x | λx.M

Reduction semantics:

(λx.M)V → M[V/x]

E[Abort M] → Abort M

Example

(λx.Abort 0)(Abort 1) evaluates to 1 in CBV and to 0 in CBN

Call-by-value (CBV) and Call-by-name (CBN)

Call-by-name Evaluation Contexts and the notion of Values:

E ::= � | E M V ::= M

Call-by-value Evaluation Contexts and the notion of Values:

E ::= � | E M | V E V ::= x | λx.M

Reduction semantics:

(λx.M)V → M[V/x]

E[Abort M] → Abort M

Example

(λx.Abort 0)(Abort 1) evaluates to 1 in CBV and to 0 in CBN

Classical Logic

Classical Logic is obtained by adding one of the following axioms to Intuitionisticlogic:

A ∨ ¬A Tertium non datur - Law of Excluded Middle EM¬¬A→ A Law of Double Negation DN(¬A→ ⊥)→ A Reductio ad absurdum - Proof by Contradiction PBC(¬A→ A)→ A Consequentia mirabilis - Weak Pierce Law PL⊥((A→ B)→ A)→ A Pierce law PL

A B A∨¬A ¬¬A → A (¬A → ⊥) → A (¬A → A) → A ((A → B) → A) → A

0 0 1 1 1 1 11 1 1 1 1 1 11 0 1 1 1 1 11 1 1 1 1 1 1

Classical Logic

Classical Logic is obtained by adding one of the following axioms to Intuitionisticlogic:

A ∨ ¬A Tertium non datur - Law of Excluded Middle EM¬¬A→ A Law of Double Negation DN(¬A→ ⊥)→ A Reductio ad absurdum - Proof by Contradiction PBC(¬A→ A)→ A Consequentia mirabilis - Weak Pierce Law PL⊥((A→ B)→ A)→ A Pierce law PL

A B A∨¬A ¬¬A → A (¬A → ⊥) → A (¬A → A) → A ((A → B) → A) → A

0 0 1 1 1 1 11 1 1 1 1 1 11 0 1 1 1 1 11 1 1 1 1 1 1

Truth versus evidence

Proving that something is true is the same as proving that it cannot be false

Since ¬¬(A ∨ ¬A) is true than A ∨ ¬A holds.

Proving that A ∨ ¬A holds means providing evidence of either A or ¬A.

David Hilbert’s words from 1927:Taking the principle of excluded middle from the mathematician would be thesame, say, as proscribing the telescope to the astronomer or to the boxer theuse of his fists. To prohibit the principle of excluded middle is tantamount torelinquishing the science of mathematics altogether.

` ∃x.Drink(x)→ ∀x.Drink(x)

Truth versus evidence

Proving that something is true is the same as proving that it cannot be false

Since ¬¬(A ∨ ¬A) is true than A ∨ ¬A holds.

Proving that A ∨ ¬A holds means providing evidence of either A or ¬A.

David Hilbert’s words from 1927:Taking the principle of excluded middle from the mathematician would be thesame, say, as proscribing the telescope to the astronomer or to the boxer theuse of his fists. To prohibit the principle of excluded middle is tantamount torelinquishing the science of mathematics altogether.

` ∃x.Drink(x)→ ∀x.Drink(x)

Truth versus evidence

Proving that something is true is the same as proving that it cannot be false

Since ¬¬(A ∨ ¬A) is true than A ∨ ¬A holds.

Proving that A ∨ ¬A holds means providing evidence of either A or ¬A.

David Hilbert’s words from 1927:Taking the principle of excluded middle from the mathematician would be thesame, say, as proscribing the telescope to the astronomer or to the boxer theuse of his fists. To prohibit the principle of excluded middle is tantamount torelinquishing the science of mathematics altogether.

` ∃x.Drink(x)→ ∀x.Drink(x)

Truth versus evidence

Proving that something is true is the same as proving that it cannot be false

Since ¬¬(A ∨ ¬A) is true than A ∨ ¬A holds.

Proving that A ∨ ¬A holds means providing evidence of either A or ¬A.

David Hilbert’s words from 1927:Taking the principle of excluded middle from the mathematician would be thesame, say, as proscribing the telescope to the astronomer or to the boxer theuse of his fists. To prohibit the principle of excluded middle is tantamount torelinquishing the science of mathematics altogether.

` ∃x.Drink(x)→ ∀x.Drink(x)

Truth versus evidence

Proving that something is true is the same as proving that it cannot be false

Since ¬¬(A ∨ ¬A) is true than A ∨ ¬A holds.

Proving that A ∨ ¬A holds means providing evidence of either A or ¬A.

David Hilbert’s words from 1927:Taking the principle of excluded middle from the mathematician would be thesame, say, as proscribing the telescope to the astronomer or to the boxer theuse of his fists. To prohibit the principle of excluded middle is tantamount torelinquishing the science of mathematics altogether.

` ∃x.Drink(x)→ ∀x.Drink(x)

Axioms are not all equivalent in Minimal Logic

Weak Pierce Law ((¬A→ A)→ A) and Excluded Middle (A ∨ ¬A) are equivalent

Double negation (¬¬A→ A) implies Pierce Law (((A→ B)→ A)→ A ) but notconversely.

Double negation, Excluded Middle + EFQ, Weak Pierce Law + EFQ, and Pierce Law+ EFQ are all equivalent

Intuitionist Logic = Minimal Logic + EFQMinimal Classical Logic = Minimal Logic + Pierce LawClassical Logic = Minimal Logic + Pierce Law + EFQ

Control operators

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is

the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is

the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Continuation

Given a program M and a subexpression e of M, the continuation of e is whatremains to be done a�er the execution of e has delivered a value.

The continuation can be seen as a function taking the value of e and delivering thevalue of the program M

Given the expression (2 + 3) + (7 + 8) and assuming the evaluation of thearithmetic expressions is le�-to-right :

The continuation of (2 + 3) is the function λv.v + (7 + 8)

The continuation of (7 + 8) is the function λv.5 + v . Note that thecontinuation is not λv.(7 + 8) + v since by the time the evaluation gets to theexpression (7 + 8), the expression (2 + 3) has already been evaluated

What will happen if we now assume a right-to-le� evaluation?

Control operators

Let’s add the possibility to the programmer to grab the continuation

The first extension of functional programming with first-class control wasdone by Peter Landin (1965):

Example (Code)f=fn x.let g1=fn y.N1

g2=J(fn z.N2)in M

When g2 is called, it does not return to where it was called, but to where f wascalled.

callcc (call with current continuation) in Scheme.

callcc

Given callcc(λk.M):

Variable k is bound to the continuation of the callcc expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM is the result of the entire callcc expression

If continuation k is invoked during the evaluation of M, with say value v ,evaluation of M is aborted and control returns to k with value v

E[callcc(λk.M)]→ E[M[λx.E[x]/k]]

E[throw k M]→ throw k M]

Example

callcc(λk.1 + throw k 0 + fib 100) + 4→(1 + throw (λx.(x + 4)) 0 + fib 100) + 4→ throw (λx.(x + 4)) 0→ 0 + 4→ 4

callcc

Given callcc(λk.M):

Variable k is bound to the continuation of the callcc expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM is the result of the entire callcc expression

If continuation k is invoked during the evaluation of M, with say value v ,evaluation of M is aborted and control returns to k with value v

E[callcc(λk.M)]→ E[M[λx.E[x]/k]]

E[throw k M]→ throw k M]

Example

callcc(λk.1 + throw k 0 + fib 100) + 4→(1 + throw (λx.(x + 4)) 0 + fib 100) + 4→ throw (λx.(x + 4)) 0→ 0 + 4→ 4

callcc

Given callcc(λk.M):

Variable k is bound to the continuation of the callcc expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM is the result of the entire callcc expression

If continuation k is invoked during the evaluation of M, with say value v ,evaluation of M is aborted and control returns to k with value v

E[callcc(λk.M)]→ E[M[λx.E[x]/k]]

E[throw k M]→ throw k M]

Example

callcc(λk.1 + throw k 0 + fib 100) + 4→(1 + throw (λx.(x + 4)) 0 + fib 100) + 4→ throw (λx.(x + 4)) 0→ 0 + 4→ 4

callcc

Given callcc(λk.M):

Variable k is bound to the continuation of the callcc expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM is the result of the entire callcc expression

If continuation k is invoked during the evaluation of M, with say value v ,evaluation of M is aborted and control returns to k with value v

E[callcc(λk.M)]→ E[M[λx.E[x]/k]]

E[throw k M]→ throw k M]

Example

callcc(λk.1 + throw k 0 + fib 100) + 4→(1 + throw (λx.(x + 4)) 0 + fib 100) + 4→ throw (λx.(x + 4)) 0→ 0 + 4→ 4

callcc

Given callcc(λk.M):

Variable k is bound to the continuation of the callcc expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM is the result of the entire callcc expression

If continuation k is invoked during the evaluation of M, with say value v ,evaluation of M is aborted and control returns to k with value v

E[callcc(λk.M)]→ E[M[λx.E[x]/k]]

E[throw k M]→ throw k M]

Example

callcc(λk.1 + throw k 0 + fib 100) + 4→(1 + throw (λx.(x + 4)) 0 + fib 100) + 4→ throw (λx.(x + 4)) 0→ 0 + 4→ 4

Felleisen’s C control operator

Given C(λk.M):

Variable k is bound to the continuation of the C expression

M is then evaluated

If continuation k is never invoked during the evaluation of M, then the value ofM, say v , is the result of the entire program containing the C-expression. Inother words, control returns to the top-level with value v

If continuation k is invoked during the evaluation of M, with value v ,evaluation of M is aborted and control returns to k with value v

E[C(λk.M)]→ M[λx.(E[x])/k]

Example

C(λk.99) + 1→ 99 whereas callcc(λk.99) + 1→ 100.

Expressive power

Summarizing we have three control operators: Abort , callcc and C:

C encodes Abort :Abort M = C(λ_.M)

C encodes callcc:

callccM = C(λk.k(Mk))

Expressive power

Summarizing we have three control operators: Abort , callcc and C:

C encodes Abort :Abort M = C(λ_.M)

C encodes callcc:

callccM = C(λk.k(Mk))

Expressive power

Summarizing we have three control operators: Abort , callcc and C:

C encodes Abort :Abort M = C(λ_.M)

C encodes callcc:

callccM = C(λk.k(Mk))

How do we type these control operators?

Γ ` M : ⊥Γ ` Abort M : σ

Γ, k : ¬A ` M : AΓ ` callcc(λk.M) : A

Γ, k : ¬A ` M : ⊥Γ ` C(λk.M) : A

How do we type these control operators?

Γ ` M : ⊥Γ ` Abort M : σ

Γ, k : ¬A ` M : AΓ ` callcc(λk.M) : A

Γ, k : ¬A ` M : ⊥Γ ` C(λk.M) : A

How do we type these control operators?

Γ ` M : ⊥Γ ` Abort M : σ

Γ, k : ¬A ` M : AΓ ` callcc(λk.M) : A

Γ, k : ¬A ` M : ⊥Γ ` C(λk.M) : A

C-H for classical logic

Intuitionist Logic = Minimal Logic + EFQMinimal Classical Logic = Minimal Logic + Pierce LawClassical Logic = Intuitionist Logic + Pierce Law

= Minimal Logic + EFQ + Pierce Law

Logic Type Theory

Minimal Logic λ-calculusIntuitionistic Logic λ-calculus + AbortMinimal Classical λ-calculus + callcc + throw

Classical logic λ-calculus + callcc + throw + tp

C-H for classical logic

Intuitionist Logic = Minimal Logic + EFQMinimal Classical Logic = Minimal Logic + Pierce LawClassical Logic = Intuitionist Logic + Pierce Law

= Minimal Logic + EFQ + Pierce Law

Logic Type Theory

Minimal Logic λ-calculusIntuitionistic Logic λ-calculus + Abort

Minimal Classical λ-calculus + callcc + throwClassical logic λ-calculus + callcc + throw + tp

C-H for classical logic

Intuitionist Logic = Minimal Logic + EFQMinimal Classical Logic = Minimal Logic + Pierce LawClassical Logic = Intuitionist Logic + Pierce Law

= Minimal Logic + EFQ + Pierce Law

Logic Type Theory

Minimal Logic λ-calculusIntuitionistic Logic λ-calculus + AbortMinimal Classical λ-calculus + callcc + throw

Classical logic λ-calculus + callcc + throw + tp

C-H for classical logic

Intuitionist Logic = Minimal Logic + EFQMinimal Classical Logic = Minimal Logic + Pierce LawClassical Logic = Intuitionist Logic + Pierce Law

= Minimal Logic + EFQ + Pierce Law

Logic Type Theory

Minimal Logic λ-calculusIntuitionistic Logic λ-calculus + AbortMinimal Classical λ-calculus + callcc + throw

Classical logic λ-calculus + callcc + throw + tp

Is classical logic constructive?

In Proofs and Types, Girard says:

Intuitionistic logic is called constructive because of the correspondence be-tween proofs and algorithms. So, for example, if we prove a formula ∃n.P(n),we can exhibit an integer n which satisfies the property P. Such an interpreta-tion is not possible with classical logic: there is no sensible way of consideringproofs as algorithms. In fact, classical logic has no denotational semantics,except the trivial one which identifies all the proofs of the same type. This isrelated to the nondeterministic behaviour of cut elimination.

Continuation passing style

How do we compile programs with control operators?

Embed the evaluation order directly in the program

Call-by-name evaluation : [[c]] = λk.k c[[x]] = λk.x k[[λx.M]] = λk.k (λx. [[M]])

[[MN ]] = λk. [[M]] (λf .f [[N ]] k)

Example

` 5 : int and [[5]] = λk.k 5 : (int → ⊥)→ ⊥.λx.x : int → int and [[λx.x]] = λk.k(λx.λq.x q) : ¬ [[int → int]]→ ⊥, where[[int → int]] = (¬int → ⊥)→ ¬int → ⊥.

[[callccM]] = λk. [[M]] (λf .f (λx.λk′.x k)k)

[[CM]] = λk. [[M]] (λf .f (λx.λk′.x k)λx.x)

Continuation passing style

How do we compile programs with control operators?

Embed the evaluation order directly in the program

Call-by-name evaluation : [[c]] = λk.k c[[x]] = λk.x k[[λx.M]] = λk.k (λx. [[M]])

[[MN ]] = λk. [[M]] (λf .f [[N ]] k)

Example

` 5 : int and [[5]] = λk.k 5 : (int → ⊥)→ ⊥.λx.x : int → int and [[λx.x]] = λk.k(λx.λq.x q) : ¬ [[int → int]]→ ⊥, where[[int → int]] = (¬int → ⊥)→ ¬int → ⊥.

[[callccM]] = λk. [[M]] (λf .f (λx.λk′.x k)k)

[[CM]] = λk. [[M]] (λf .f (λx.λk′.x k)λx.x)

Continuation passing style

How do we compile programs with control operators?

Embed the evaluation order directly in the program

Call-by-name evaluation : [[c]] = λk.k c[[x]] = λk.x k[[λx.M]] = λk.k (λx. [[M]])

[[MN ]] = λk. [[M]] (λf .f [[N ]] k)

Example

` 5 : int and [[5]] = λk.k 5 : (int → ⊥)→ ⊥.λx.x : int → int and [[λx.x]] = λk.k(λx.λq.x q) : ¬ [[int → int]]→ ⊥, where[[int → int]] = (¬int → ⊥)→ ¬int → ⊥.

[[callccM]] = λk. [[M]] (λf .f (λx.λk′.x k)k)

[[CM]] = λk. [[M]] (λf .f (λx.λk′.x k)λx.x)

Negative translations

TheoremIf ` M : σ then [[M]] : ¬¬σ∗, where σ∗ is

(σ → τ)∗ = ¬¬σ∗ → ¬¬τ∗ b∗ = b

Continuation-passing style transformation is related to proof translations ofclassical mathematics into intuitionistic mathematics.

These are referred to as negative translations. The most known are the translationsdue to Kolmogorov, Gödel, Gentzen, Kuroda and Krivine.

TheoremIf a formula A is provable in classical logic, then [[A]] is provable in intuitionistic logic.

Recommended