2.67k likes | 2.68k Views
Explore the Curry-Howard Correspondence in classical logics, how control operators influence programming calculus, and their relation to natural deduction. Learn through informal, intuitive discussion with examples and exercises.
E N D
Natural Delimited ControlA Curry-Howard Correspondence for a Canonical Classical Natural Deduction Alexander J Summers Department of Computing Imperial College London
Overview • Interested in the extension of the Curry-Howard Correspondence to Classical Logics • Talk roughly in three parts... • 1. Brief introduction to Control Operators • 2. Definition of a programming calculus based on classical natural deduction • 3. How are the two related? • Talk is semi-informal: focus on intuition/explanation • Feel free to ask questions..
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest -->if n=0 then0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest -->if n=0 then0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest))
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l (Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest)) ?
Contexts and Continuations • Control operators give control over the context in which an execution takes place termcontext
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))termcontext
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole”
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●)))
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4)))
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4))) • A continuation is a term representation of a context • A term with a ‘hole’ for another term
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4))) • A continuation is a term representation of a context • A term with a ‘hole’ for another term • We represent this with a binder νover the ‘hole’
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4))) • A continuation is a term representation of a context • A term with a ‘hole’ for another term • We represent this with a binder νover the ‘hole’ νx.print (1 * (2 * (●)))
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4))) • A continuation is a term representation of a context • A term with a ‘hole’ for another term • We represent this with a binder νover the ‘hole’ νx.print (1 * (2 * (x)))
Contexts and Continuations • Control operators give control over the context in which an execution takes place print (1 * (2 * (3 * 4)))term context • We write contexts as E{●} where ● is the “hole” E{●}= print (1 * (2 * (●))) E{3 * 4}= print (1 * (2 * (3 * 4))) • A continuation is a term representation of a context • A term with a ‘hole’ for another term • We represent this with a binder νover the ‘hole’ νx.print (1 * (2 * (x)))
Control Operators • Programming constructs for functional languages • Allow the expression of “non-functional” behaviour • e.g., jumps, exceptions, loops, ... • Simplest example: A (“abort”) • Defined by: E{A M}→M • Completely discards the surrounding context print (1 * (2 * (A (3 * 4))))
Control Operators • Programming constructs for functional languages • Allow the expression of “non-functional” behaviour • e.g., jumps, exceptions, loops, ... • Simplest example: A (“abort”) • Defined by: E{A M}→M • Completely discards the surrounding context print (1 * (2 * (A (3 * 4)))) → print (1 * (2 * (A 12)))
Control Operators • Programming constructs for functional languages • Allow the expression of “non-functional” behaviour • e.g., jumps, exceptions, loops, ... • Simplest example: A (“abort”) • Defined by: E{A M}→M • Completely discards the surrounding context print (1 * (2 * (A (3 * 4)))) → print (1 * (2 * (A 12))) → print (1 * (2 * (A12)))
Control Operators • More interesting example: C (“control”) • CM gives M explicit control over the context • Stores the context in an “escape procedure” E{CM}→Mλx.(AE{x}) • The escape procedure is passed to M • If M calls procedure with argument N, resulting term AE{N}aborts current execution and restores the context E{N} • In this way, M can “throw” values back to the context
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest)) ?
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest)) ?
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest)) ?
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then 0 else n*(f rest)) ?
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then k 0 else n*(f rest)) ?
Multiplying lists of numbers.. • Exercise: write a recursive function / functional program to calculate the product of a list of numbers • Lists are defined by: l ::= [] | n:l Cλk.k(Fix f. λy.match y with [] --> 1 n:rest --> if n=0 then k 0 else n*(f rest))
Types for Control Operators • What about types for these operators? e.g., abort • Consider the reduction rule: E{A M}→M • (A M)should have the type of the “hole” in E • M can have any type? • But, for the rule to be sound, Mmust have the type of E{A M} • Griffin (‘90): introduce special type for “top-level” • The type of “finished computation” - M must have it Γ⊢M : B (A) Γ⊢(AM) : C
Types for Control Operators • What about types for these operators? e.g., abort • Consider the reduction rule: E{A M}→M • (A M)should have the type of the “hole” in E • M can have any type? • But, for the rule to be sound, Mmust have the type of E{A M} • Griffin (‘90): introduce special type for “top-level” • The type of “finished computation” - M must have it • Now A makes logical sense in terms of types too • corresponds with -elimination from intuitionistic logic Γ⊢M : (A) Γ⊢(AM) : C
Types for Control Operators • What about types for these operators? e.g., abort • Consider the reduction rule: E{A M}→M • (A M)should have the type of the “hole” in E • M can have any type? • But, for the rule to be sound, Mmust have the type of E{A M} • Griffin (‘90): introduce special type for “top-level” • The type of “finished computation” - M must have it • Now A makes logical sense in terms of types too • corresponds with -elimination from intuitionistic logic Γ⊢M : (A) Γ⊢(A M) : C
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x})
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x})
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x}) A
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x}) A B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x}) A B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→M λx.(AE{x}) A B A→B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→Mλx.(AE{x}) A B A→B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→Mλx.(AE{x}) A B (A→B)→ A→B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→Mλx.(AE{x}) A B (A→B)→ A→B
Control Operators - types • What about the more powerful C control operator? • Recall the reduction rule: E{C M}→Mλx.(AE{x}) • Observation (Griffin): if we set B= then C can be typed as double-negation-elimination:((A→)→)→A ≡¬ ¬ A→A • Led to research into computational interpretations of classical logics.. including this talk! A B ((A→B)→)→A (A→B)→ A→B
Delimited Control Operators • So far, control operators capture entire context • A refinement to the previous ideas: delimited control • “markers” # can be placed around any subterm: # M • Only capture the context up to the enclosing # • Refines reduction behaviours: