a typed calculus with call by name and call by value
play

A typed -calculus with call-by-name and call-by-value iteration - PowerPoint PPT Presentation

A typed -calculus with call-by-name and call-by-value iteration Herman Geuvers Radboud University Nijmegen and Eindhoven University of Technology The Netherlands Types 2015, Tallinn May 21, 2015 Contents Church data and Scott data


  1. A typed λ -calculus with call-by-name and call-by-value iteration Herman Geuvers Radboud University Nijmegen and Eindhoven University of Technology The Netherlands Types 2015, Tallinn May 21, 2015

  2. Contents ◮ Church data and Scott data ◮ Iteration, case and primitive recursion ◮ Call-by-value and Call-by name iteration ◮ Examples: addition and storage operator ◮ Properties (normalization) and connections

  3. Church numerals The most well-known Church data type λ x f . f p ( x ) 0 := λ x f . x p := 1 := λ x f . f x Succ := λ n .λ x f . f ( n x f ) 2 := λ x f . f ( f x ) ◮ The Church data types have iteration as basis. The numerals are iterators. ◮ Iteration scheme for Nat . (Let D be any type.) f : D → D d : D It d f 0 d ։ with It d f ( Succ x ) f ( It d f x ) ։ It d f : Nat → D ◮ Advantage: quite a bit of well-founded recursion for free. ◮ Disadvantage: no pattern matching built in; predecessor is hard to define; the reduction is actually a conversion

  4. Scott numerals � 0 � := λ x f . x � n + 1 � := λ x f . f � n � � 1 � λ x f . f � 0 � := succ := λ p .λ x f . f p � 2 � := λ x f . f � 1 � ◮ The Scott numerals are case distinctors. ◮ Case scheme for nat . (Let D be any type.) d : D f : nat → D Case d f � 0 � d ։ with Case d f ( succ x ) f x ։ Case d f : nat → D ◮ Case d f := λ x : nat . x d f . ◮ Advantage: the predecessor can immediately be defined: pred := λ p . p � 0 � ( λ y . y ). ◮ Disadvantage: No recursion (which one has to get from somewhere else, e.g. a fixed point-combinator).

  5. Church and Scott for general data types Given a data type with ◮ constructors c 1 , . . . , c k , ◮ with arity ar( i ) of constructor c i . The Church encoding is := λ x 1 . . . x ar( i ) .λ c 1 . . . c k . c i ( x 1 � c ) . . . ( x ar( i ) � c ) c i The Scott encoding is � c i � := λ x 1 . . . x ar( i ) .λ c 1 . . . c k . c i x 1 . . . x ar( i )

  6. Why study Scott data types? ◮ No recursion built in ⇒ more control over the definable functions. Brunel, Terui 2010; Baillot, De Benedetti, Ronchi della Rocca 2014. ◮ Direct access to predecessor. Destructor is constant-time. ◮ Combined Church-Scott data types give Primitive Recursion scheme. For Nat , these are known as Parigot numerals. (See also Stump, Fu 2014) d : D f : Nat → D → D Rec d f 0 d ։ Rec d f ( Succ x ) f x ( Rec d f x ) ։ Rec d f : Nat → D One can define Rec in terms of It , but that’s painful. (This is what Kleene found out at the dentist.)

  7. Typing Church and Scott data types ◮ Church data types can be typed in polymorphic λ -calculus, λ 2. E.g. for Church numbers: Nat := ∀ X . X → ( X → X ) → X . ◮ To type Scott data types we need λ 2 µ : λ 2 + positive recursive types: ◮ µ X . Φ is well-formed if X occurs positively in Φ. ◮ Equality on types is the congruence generated from µ X . Φ = Φ[ µ X . Φ / X ]. ◮ Additional derivation rule: Γ ⊢ M : A A = B Γ ⊢ M : B E.g. for Scott numerals: nat := µ Y . ∀ X . X → ( Y → X ) → X . We will use this as nat = ∀ X . X → ( nat → X ) → X .

  8. The system λ 2 µ It The system λ 2 µ It combines polymorphic lambda calculus with positive recursive types and CBN and CBV iterators. T := TVar | (T → T) | µ TVar . T | ∀ TVar . T , x : A ∈ Γ Γ ⊢ M : A → B Γ ⊢ N : A Γ ⊢ x : A Γ ⊢ M N : B Γ , x : A ⊢ M : B Γ ⊢ x : A if A = B Γ ⊢ λ x : A . M : A → B Γ ⊢ x : B Γ ⊢ M : ∀ X . A Γ ⊢ M : A if X / ∈ FV(Γ) Γ ⊢ M : A [ B / X ] Γ ⊢ M : ∀ X . A The evaluation relation is weak-head reduction, → wh given by M → wh N ( λ x . M ) P → wh M [ P / x ] M P → wh N P Plus additional typing and reduction rules for Itcbv and Itcbn.

  9. Call-by-name and call-by-value iteration To be able to define functions on Scott data types, we add for every data-type two program rules for iteration: ◮ Itcbn for call-by-name iteration from data-type D to B , Idea: compute first constructor of the output of type B and pass to the appropriate continuation for type B . ◮ Itcbv for call-by-value iteration from data-type D to B . Idea: compute input value completely and then pass on to a continuation of type B → X . So, this function will be of type ∀ X . ( B → X ) → ( D → X ) To be abbreviated to ∀ X . ¬ X B → ¬ X D

  10. Data types in λ 2 µ It Data types have the following shape D = µ T . ∀ X . (Φ 1 ( T ) → X ) . . . → (Φ n ( T ) → X ) → X , with X positive in Φ i ( X ). Phrased differently: D = ∀ X . (Φ 1 ( D ) → X ) . . . → (Φ n ( D ) → X ) → X , Examples: ∀ X . X → X → X bool := := ∀ X . X → ( nat → X ) → X nat ∀ X . X → ( A → list A → X ) → X list A := := ∀ X . ( A → X ) → ( btree A → btree A → X ) → X btree A

  11. Constructors are defined terms in λ 2 µ It := ∀ X . X → ( nat → X ) → X nat ∀ X . X → ( A → list A → X ) → X list A := The constructors are (without type information with the λ ): zero := λ z .λ s . z : nat := λ n .λ z .λ s . s n : nat → nat succ nil := λ n .λ c . n : list A λ a .λ l .λ n .λ c . c a l : A → list A → list A cons := We write � d � for the encoding of a data-element as a term. So, for n ∈ N , � n � : nat , given by � 0 � := zero � n + 1 � succ � n � :=

  12. Derivation rule for Itcbn, Call-by-Name iterator Given D = ∀ X . (Φ 1 ( X ) → D ) . . . → (Φ n ( X ) → D ) → X . f 1 : Φ 1 ( B ) → B f n : Φ n ( B ) → B . . . Itcbn D B f 1 . . . f n : D → B Plus reduction rule If B is also a data-type, we have B = ∀ X . (Ψ 1 ( X ) → B ) . . . → (Ψ m ( X ) → B ) → X . We could also write the rule for Itcbn as follows (for A an arbitrary type): f 1 : Φ 1 ( B ) → B f n : Φ n ( B ) → B . . . d : D c 1 : Ψ 1 ( B ) → A c m : Ψ m ( B ) → A . . . Itcbn D B f 1 . . . f n d c 1 . . . c m : A Idea: c 1 : Ψ 1 ( m ) → A . . . c m : Ψ m ( B ) → A are the continuations for data type B , one for each constructor of B .

  13. Derivation rule for Itcbv, Call-by-Value iterator Idea: ¬ A B := B → A is the type of the continuation; c : ¬ A B is called “after the value has been computed”. Let D = ∀ X . (Φ 1 ( X ) → D ) . . . → (Φ n ( X ) → D ) → X . f 1 : ¬ A B → ¬ A Φ 1 ( B ) f n : ¬ A B → ¬ A Φ n ( B ) . . . Itcbv D B f 1 . . . f n : ¬ A B → ¬ A D Plus reduction rule This rule can also be phrased in “fully applied form”: f 1 : ¬ A B → ¬ A Φ 1 ( B ) f n : ¬ A B → ¬ A Φ n ( B ) c : ¬ A B . . . d : D Itcbv D B f 1 . . . f n c d : A

  14. Call-by-name and call-by-value iteration for nat We show the case for nat = ∀ X . X → ( nat → X ) → X . Call-by-name: f 1 : B f 2 : B → B Itcbn f 1 f 2 : nat → B with reduction rule: Itcbn f 1 f 2 n → n f 1 ( λ x : nat . f 2 (Itcbn f 1 f 2 x )) Call-by-value: f 1 : ¬ A ¬ A B f 2 : ¬ A B → ¬ A B Itcbv f 1 f 2 : ¬ A B → ¬ A nat with reduction rule: Itcbv f 1 f 2 c n → n ( f 1 c ) (Itcbv f 1 f 2 ( f 2 c )) . Note: f 1 c : A and Itcbv f 1 f 2 ( f 2 c ) : nat → A .

  15. Example for the case nat : Call-by-Value Addition f 1 : ¬ A ¬ A nat f 2 : ¬ A nat → ¬ A nat Itcbv f 1 f 2 : ¬ A nat → ¬ A nat Define: succ := λ c : ¬ A nat .λ n : nat . c ( succ n ) : ¬ A nat → ¬ A nat � ˆ n := λ c : ¬ A nat . c n : ¬ A ¬ A nat (for n : nat ) Then : nat → nat → ¬ A ¬ A nat AddCBV AddCBV := λ x y : nat .λ c : ¬ A nat . Itcbv ˆ y � succ c x We have AddCBV � n � � m � c ։ c � n + m � where � n � represents the number n .

  16. Example for the case nat : Call-by-Name Addition f 1 : nat f 2 : nat → nat Itcbn f 1 f 2 : nat → nat AddCBN := λ x y : nat . Itcbn y succ . Then AddCBN � n + 1 � � m � succ ( AddCBN � n � � m � ) ։ λ z s . s ( AddCBN � n � � m � ) ։ ◮ Weak-head reduction stops here. ◮ AddCBN t q computes the first ‘pattern’ of the output and passes it on to the appropriate continuation. (In this case s .)

  17. Storage operators The idea of a storage operator (Krivine, Nour), Stor, for the natural numbers nat is: For every n ∈ N there is a term t n such that, given f : nat → A and M : nat with M = β vn � n � , Stor M f ։ f t n . To compute f M call-by-value, we compute Stor M f . This will first compute M and then feed this into f .

  18. Storage operators in λ 2 µ It A storage operator for data type D is a term Stor : D → ∀ X . ¬ X ¬ X D satisfying, for all M with M = β vn � d � , Stor M f ։ f � d � . Define Stor nat : nat → ∀ X . ¬ X ¬ X nat and Unstor nat : ( ∀ X . ¬ X ¬ X nat ) → nat as follows. Stor nat := λ n .λ f . Itcbv � zero � succ f n Unstor nat := λ f .λ z s . f ( λ n . n z s ) with � zero := λ c . c zero , � succ := λ c m . c ( succ m ). Lemma The term Stor nat is a storage operator for nat and Unstor nat (Stor nat � n � ) = β vn � n � for all n ∈ N .

  19. Strong Normalization The proof uses the well-known saturated sets construction to give an interpretation for types as sets of (untyped) terms. X is a saturated set ( X ∈ SAT) if 1. X ⊆ SN, 2. for x a variable and � P a sequence of terms, x � P ∈ X , if x � P ∈ SN, 3. X is closed under reduction: if M ∈ X and M ։ N , then N ∈ X , 4. X is closed under weak-head-redex expansion: if M ∈ X and N → wh M , then N ∈ X ,

Recommend


More recommend