a call by value calculus with lists and control
play

A call-by-value -calculus with lists and control Robbert Krebbers - PowerPoint PPT Presentation

A call-by-value -calculus with lists and control Robbert Krebbers July 08, 2012 @ CL&C Warwick, United Kingdom Problem A lot of -calculi with control have been studied: C , , , C , , and many


  1. Example: typing ; α : [ ⊤ ] ⊢ throw α nil : ⊤ ; α : [ ⊤ ] ⊢ nil : [ ⊤ ] ; α : [ ⊤ ] ⊢ ( throw α nil ) :: nil : [ ⊤ ] ; ⊢ catch α . ( throw α nil ) :: nil : [ ⊤ ] How to think of this derivation: 1. Our goal is [ ⊤ ] 2. We save the current continuation as α 3. We construct a singleton list . . . leaving us to construct a term of type ⊤

  2. Example: typing ; α : [ ⊤ ] ⊢ nil : [ ⊤ ] ; α : [ ⊤ ] ⊢ throw α nil : ⊤ ; α : [ ⊤ ] ⊢ nil : [ ⊤ ] ; α : [ ⊤ ] ⊢ ( throw α nil ) :: nil : [ ⊤ ] ; ⊢ catch α . ( throw α nil ) :: nil : [ ⊤ ] How to think of this derivation: 1. Our goal is [ ⊤ ] 2. We save the current continuation as α 3. We construct a singleton list . . . leaving us to construct a term of type ⊤ 4. But instead we jump back to α with nil

  3. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r

  4. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r Reduction: ( λ x . t ) v → t [ x := v ] lrec v r v s nil → v r lrec v r v s ( v h :: v t ) → v s v h v t ( lrec v r v s v t )

  5. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r Contexts: E ::= � t | v � | throw β � Reduction: ( λ x . t ) v → t [ x := v ] lrec v r v s nil → v r lrec v r v s ( v h :: v t ) → v s v h v t ( lrec v r v s v t ) E [ throw α t ] → throw α t

  6. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r Contexts: E ::= � t | v � | throw β � Reduction: ( λ x . t ) v → t [ x := v ] lrec v r v s nil → v r lrec v r v s ( v h :: v t ) → v s v h v t ( lrec v r v s v t ) E [ throw α t ] → throw α t catch α . throw α t → catch α . t

  7. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r Contexts: E ::= � t | v � | throw β � Reduction: ( λ x . t ) v → t [ x := v ] lrec v r v s nil → v r lrec v r v s ( v h :: v t ) → v s v h v t ( lrec v r v s v t ) E [ throw α t ] → throw α t catch α . throw α t → catch α . t catch α . throw β v → throw β v if α / ∈ { β } ∪ FCV( v )

  8. Reduction Values: v , w ::= x | () | nil | (::) | (::) v | (::) v w | lrec | lrec v r | lrec v r v s | λ x . r Contexts: E ::= � t | v � | throw β � Reduction: ( λ x . t ) v → t [ x := v ] lrec v r v s nil → v r lrec v r v s ( v h :: v t ) → v s v h v t ( lrec v r v s v t ) E [ throw α t ] → throw α t catch α . throw α t → catch α . t catch α . throw β v → throw β v if α / ∈ { β } ∪ FCV( v ) catch α . v → v if α / ∈ FCV( v )

  9. Example: reduction catch α . ( throw α nil ) :: nil

  10. Example: reduction catch α . ( throw α nil ) :: nil

  11. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ]

  12. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ]

  13. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ] → catch α . throw α nil

  14. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ] → catch α . throw α nil

  15. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ] → catch α . throw α nil → catch α . nil

  16. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ] → catch α . throw α nil → catch α . nil

  17. Example: reduction catch α . ( throw α nil ) :: nil ≡ catch α . ( � :: nil )[ throw α nil ] → catch α . throw α nil → catch α . nil → nil

  18. Why restrict to → -free types? (1) ⇒ t is a value or ∃ t ′ . t → t ′ Progress : ; ⊢ t : τ =

  19. Why restrict to → -free types? (1) ⇒ t is a value or ∃ t ′ . t → t ′ Progress : ; ⊢ t : τ = ◮ Without the → -free restriction, the term catch α . λ x . throw α ( λ y . y ) : ⊤ → ⊤ would not reduce

  20. Why restrict to → -free types? (1) ⇒ t is a value or ∃ t ′ . t → t ′ Progress : ; ⊢ t : τ = ◮ Without the → -free restriction, the term catch α . λ x . throw α ( λ y . y ) : ⊤ → ⊤ would not reduce ◮ Hence progress would fail

  21. Why restrict to → -free types? (1) ⇒ t is a value or ∃ t ′ . t → t ′ Progress : ; ⊢ t : τ = ◮ Without the → -free restriction, the term catch α . λ x . throw α ( λ y . y ) : ⊤ → ⊤ would not reduce ◮ Hence progress would fail ◮ Note: an analogue term in the λµ -calculus µα. [ α ] λ x .µ . [ α ] λ y . y does not reduce either

  22. Why restrict to → -free types? (2) Consequences of progress ◮ In Herbelin’s IQC MP : ◮ If ; ⊢ t : ρ ∨ σ , then ∃ t ′ . ; ⊢ t ′ : ρ or ; ⊢ t ′ : σ ◮ If ; ⊢ t : ∃ x . P ( x ), then ∃ t ′ . ; ⊢ t ′ : P ( t ′ )

  23. Why restrict to → -free types? (2) Consequences of progress ◮ In Herbelin’s IQC MP : ◮ If ; ⊢ t : ρ ∨ σ , then ∃ t ′ . ; ⊢ t ′ : ρ or ; ⊢ t ′ : σ ◮ If ; ⊢ t : ∃ x . P ( x ), then ∃ t ′ . ; ⊢ t ′ : P ( t ′ ) ◮ In λ :: catch : ◮ Unique representation of data ◮ One-to-one correspondence between closed terms of N and N

  24. Natural numbers We define a type N := [ ⊤ ], with: 0 := nil S := (::) () nrec := λ x r y s . lrec x r ( λ . x s ) Notation: n := S n 0

  25. Inefficient predecessor We could define pred : N → N as pred := nrec 0 ( λ xh . x )

  26. Inefficient predecessor We could define pred : N → N as pred := nrec 0 ( λ xh . x ) Inefficient with call-by-value reduction pred n ։ ( λ xh . x ) n − 1 ( pred n − 1)

  27. Inefficient predecessor We could define pred : N → N as pred := nrec 0 ( λ xh . x ) Inefficient with call-by-value reduction pred n ։ ( λ xh . x ) n − 1 ( pred n − 1) ։ ( λ h . n − 1) ( pred n − 2) � �� � not a value

  28. Inefficient predecessor We could define pred : N → N as pred := nrec 0 ( λ xh . x ) Inefficient with call-by-value reduction pred n ։ ( λ xh . x ) n − 1 ( pred n − 1) ։ ( λ h . n − 1) ( pred n − 2) � �� � not a value ։ ( λ h . n − 1) (( λ h . n − 2) ( pred n − 2) ) � �� � not a value

  29. Inefficient predecessor We could define pred : N → N as pred := nrec 0 ( λ xh . x ) Inefficient with call-by-value reduction pred n ։ ( λ xh . x ) n − 1 ( pred n − 1) ։ ( λ h . n − 1) ( pred n − 2) � �� � not a value ։ ( λ h . n − 1) (( λ h . n − 2) ( pred n − 2) ) � �� � not a value ։ . . .

  30. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n

  31. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1

  32. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1

  33. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n )

  34. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n )

  35. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n )

  36. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n )

  37. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . ( throw α n ) ( lrec 0 ( λ x . throw α x ) n )

  38. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . ( throw α n ) ( lrec 0 ( λ x . throw α x ) n ) We use the rule ( throw α t ) r → throw α t to discard the recursive call

  39. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . ( throw α n ) ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . throw α n We use the rule ( throw α t ) r → throw α t to discard the recursive call

  40. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . ( throw α n ) ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . throw α n

  41. A more efficient predecessor in λ :: catch We redefine pred : N → N as pred := λ n . catch α . nrec 0 ( λ x . throw α x ) n Using catch and throw it becomes more efficient pred n + 1 ։ catch α . nrec 0 ( λ x . throw α x ) ( S n ) ։ catch α . ( λ x . throw α x ) n ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . ( throw α n ) ( lrec 0 ( λ x . throw α x ) n ) ։ catch α . throw α n ։ n

  42. Example: list multiplication ◮ We want to define F : [ N ] → N such that F [ t 1 , . . . , t n ] = t 1 ∗ . . . ∗ t n

  43. Example: list multiplication ◮ We want to define F : [ N ] → N such that F [ t 1 , . . . , t n ] = t 1 ∗ . . . ∗ t n ◮ The straightforward definition F := lrec 1 ( λ x h . x ∗ h ) continues to multiply once a zero has been encountered

  44. Example: list multiplication ◮ We want to define F : [ N ] → N such that F [ t 1 , . . . , t n ] = t 1 ∗ . . . ∗ t n ◮ The straightforward definition F := lrec 1 ( λ x h . x ∗ h ) continues to multiply once a zero has been encountered ◮ We use control to jump out when we encounter a zero F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x

  45. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]:

  46. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9]

  47. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9]

  48. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9])

  49. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9])

  50. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9])

  51. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9])

  52. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( throw α 0 )

  53. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( throw α 0 )

  54. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( throw α 0 ) ։ catch α . throw α 0

  55. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( throw α 0 ) ։ catch α . throw α 0

  56. Example: list multiplication (continued) The definition of list multiplication: F := λ l . catch α . lrec 1 H l H := λ x . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) x A computation of F [4 , 0 , 9]: ։ catch α . lrec 1 H [4 , 0 , 9] ։ catch α . nrec ( throw α 0 ) ( λ y h . S y ∗ h ) 4 ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( lrec 1 H [0 , 9]) ։ catch α . ( λ h . 4 ∗ h ) ( throw α 0 ) ։ catch α . throw α 0 ։ 0

  57. Properties of λ :: catch ◮ Subject reduction. Γ; ∆ ⊢ t : ρ and t → t ′ , then Γ; ∆ ⊢ t ′ : ρ ◮ A standard substitution lemma is needed ◮ Induction on the structure of t → t ′

  58. Properties of λ :: catch ◮ Subject reduction. Γ; ∆ ⊢ t : ρ and t → t ′ , then Γ; ∆ ⊢ t ′ : ρ ◮ A standard substitution lemma is needed ◮ Induction on the structure of t → t ′ ◮ Progress. ; ⊢ t : ρ , then t is a value or ∃ t ′ , t → t ′ ◮ A simple generalization is needed ◮ Induction on the typing judgment.

  59. Properties of λ :: catch ◮ Subject reduction. Γ; ∆ ⊢ t : ρ and t → t ′ , then Γ; ∆ ⊢ t ′ : ρ ◮ A standard substitution lemma is needed ◮ Induction on the structure of t → t ′ ◮ Progress. ; ⊢ t : ρ , then t is a value or ∃ t ′ , t → t ′ ◮ A simple generalization is needed ◮ Induction on the typing judgment. ◮ Confluence. t ։ r and t ։ s , then ∃ q . r ։ q and s ։ q

  60. Properties of λ :: catch ◮ Subject reduction. Γ; ∆ ⊢ t : ρ and t → t ′ , then Γ; ∆ ⊢ t ′ : ρ ◮ A standard substitution lemma is needed ◮ Induction on the structure of t → t ′ ◮ Progress. ; ⊢ t : ρ , then t is a value or ∃ t ′ , t → t ′ ◮ A simple generalization is needed ◮ Induction on the typing judgment. ◮ Confluence. t ։ r and t ։ s , then ∃ q . r ։ q and s ։ q ◮ Strong Normalization. Γ; ∆ ⊢ t : ρ , then no infinite t → t 1 . . .

  61. Parallel reduction Usual approach [Tait/Martin-L¨ of] 1. Define a parallel reduction ⇒ 2. Prove that ⇒ is confluent 3. Prove that t 1 → t 2 implies t 1 ⇒ t 2 4. Prove that t 1 ⇒ t 2 implies t 1 ։ t 2

  62. Parallel reduction Usual approach [Tait/Martin-L¨ of] 1. Define a parallel reduction ⇒ 2. Prove that ⇒ is confluent 3. Prove that t 1 → t 2 implies t 1 ⇒ t 2 4. Prove that t 1 ⇒ t 2 implies t 1 ։ t 2 For the ordinary λ -calculus t ⇒ t ′ t ⇒ t ′ r ⇒ r ′ λ x . t ⇒ λ x . t ′ tr ⇒ t ′ r ′ x ⇒ x t ⇒ t ′ r ⇒ r ′ ( λ x . t ) r ⇒ t ′ [ x := r ′ ]

  63. Parallel reduction for λ :: catch ◮ Consider the naive rule for throw t ⇒ t ′ E [ throw α t ] ⇒ throw α t ′

  64. � � Parallel reduction for λ :: catch ◮ Consider the naive rule for throw t ⇒ t ′ E [ throw α t ] ⇒ throw α t ′ ◮ Problem: not confluent throw α 1 ( throw α 2 ( throw α 3 ( throw α 4 ( throw α 5 x )))) throw α 1 ( throw α 3 ( throw α 5 x )) throw α 2 ( throw α 4 ( throw α 5 x ))

  65. � � Parallel reduction for λ :: catch ◮ Consider the naive rule for throw t ⇒ t ′ E [ throw α t ] ⇒ throw α t ′ ◮ Problem: not confluent throw α 1 ( throw α 2 ( throw α 3 ( throw α 4 ( throw α 5 x )))) throw α 1 ( throw α 3 ( throw α 5 x )) throw α 2 ( throw α 4 ( throw α 5 x )) ◮ Solution: jump over a compound context t ⇒ t ′ � E [ throw α t ] ⇒ throw α t ′

  66. Complete development Define t ⋄ such that if t 1 ⇒ t 2 , then t 2 ⇒ t ⋄ 1 [Takahashi, 1995]

  67. � � Complete development Define t ⋄ such that if t 1 ⇒ t 2 , then t 2 ⇒ t ⋄ 1 [Takahashi, 1995] Confluence of ⇒ is a direct consequence t 1 t 2 t 3

Recommend


More recommend