foundations of software fall 2019
play

Foundations of Software Fall 2019 Week 7 Plan PREVIOUSLY: unit, - PowerPoint PPT Presentation

Foundations of Software Fall 2019 Week 7 Plan PREVIOUSLY: unit, sequencing, let, pairs, tuples TODAY: 1. options, variants 2. recursion 3. state NEXT: exceptions? NEXT: polymorphic (not so simple) typing Records t ::= ... terms {l i =t i


  1. Foundations of Software Fall 2019 Week 7

  2. Plan PREVIOUSLY: unit, sequencing, let, pairs, tuples TODAY: 1. options, variants 2. recursion 3. state NEXT: exceptions? NEXT: polymorphic (not so simple) typing

  3. Records t ::= ... terms {l i =t i i ∈ 1 .. n } record projection t.l v ::= ... values i ∈ 1 .. n } record value {l i =v i T ::= ... types {l i :T i i ∈ 1 .. n } type of records

  4. Evaluation rules for records i ∈ 1 .. n }.l j − ( E-ProjRcd ) {l i =v i → v j → t ′ t 1 − 1 ( E-Proj ) → t ′ t 1 .l − 1 .l t j − → t ′ j ( E-Rcd ) i ∈ 1 .. j − 1 ,l j =t j ,l k =t k k ∈ j + 1 .. n } {l i =v i 1 ,l j =t ′ − → {l i =v i i ∈ 1 .. j − j ,l k =t k k ∈ j + 1 .. n }

  5. Typing rules for records for each i Γ ⊢ t i : T i ( T-Rcd ) Γ ⊢ {l i =t i i ∈ 1 .. n } : {l i :T i i ∈ 1 .. n } Γ ⊢ t 1 : {l i :T i i ∈ 1 .. n } ( T-Proj ) Γ ⊢ t 1 .l j : T j

  6. Sums and variants

  7. Sums – motivating example PhysicalAddr = {firstlast:String, addr:String} VirtualAddr = {name:String, email:String} Addr = PhysicalAddr + VirtualAddr “ PhysicalAddr → PhysicalAddr+VirtualAddr ” inl : inr “ VirtualAddr → PhysicalAddr+VirtualAddr ” : getName = λ a:Addr. case a of inl x ⇒ x.firstlast | inr y ⇒ y.name;

  8. New syntactic forms t ::= ... terms tagging (left) inl t inr t tagging (right) case case t of inl x ⇒ t | inr x ⇒ t v ::= ... values tagged value (left) inl v inr v tagged value (right) T ::= ... types sum type T+T T 1 +T 2 is a disjoint union of T 1 and T 2 (the tags inl and inr ensure disjointness)

  9. New evaluation rules → t ′ t − → [ x 1 �→ v 0 ] t 1 ( E-CaseInl ) case (inl v 0 ) − of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 → [ x 2 �→ v 0 ] t 2 ( E-CaseInr ) case (inr v 0 ) − of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 t 0 − → t ′ 0 ( E-Case ) case t 0 of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 − → case t ′ 0 of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 → t ′ t 1 − 1 ( E-Inl ) → inl t ′ inl t 1 − 1 → t ′ t 1 − 1 ( E-Inr ) inr t 1 − → inr t ′ 1

  10. New typing rules Γ ⊢ t : T Γ ⊢ t 1 : T 1 ( T-Inl ) Γ ⊢ inl t 1 : T 1 +T 2 Γ ⊢ t 1 : T 2 ( T-Inr ) Γ ⊢ inr t 1 : T 1 +T 2 Γ ⊢ t 0 : T 1 +T 2 Γ , x 1 :T 1 ⊢ t 1 : T Γ , x 2 :T 2 ⊢ t 2 : T Γ ⊢ case t 0 of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 : T ( T-Case )

  11. Sums and Uniqueness of Types Problem: If t has type T , then inl t has type T+U for every U . I.e., we’ve lost uniqueness of types. Possible solutions: ◮ “Infer” U as needed during typechecking ◮ Give constructors different names and only allow each name to appear in one sum type (requires generalization to “variants,” which we’ll see next) — OCaml’s solution ◮ Annotate each inl and inr with the intended sum type. For simplicity, let’s choose the third.

  12. New syntactic forms t ::= ... terms tagging (left) inl t as T inr t as T tagging (right) v ::= ... values tagged value (left) inl v as T tagged value (right) inr v as T Note that as T here is not the ascription operator that we saw before — i.e., not a separate syntactic form: in essence, there is an ascription “built into” every use of inl or inr .

  13. New typing rules Γ ⊢ t : T Γ ⊢ t 1 : T 1 ( T-Inl ) Γ ⊢ inl t 1 as T 1 +T 2 : T 1 +T 2 Γ ⊢ t 1 : T 2 ( T-Inr ) Γ ⊢ inr t 1 as T 1 +T 2 : T 1 +T 2

  14. Evaluation rules ignore annotations: → t ′ t − case (inl v 0 as T 0 ) of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 ( E-CaseInl ) → [ x 1 �→ v 0 ] t 1 − case (inr v 0 as T 0 ) ( E-CaseInr ) of inl x 1 ⇒ t 1 | inr x 2 ⇒ t 2 → [ x 2 �→ v 0 ] t 2 − → t ′ t 1 − 1 ( E-Inl ) → inl t ′ inl t 1 as T 2 − 1 as T 2 → t ′ t 1 − 1 ( E-Inr ) → inr t ′ inr t 1 as T 2 − 1 as T 2

  15. Variants Just as we generalized binary products to labeled records, we can generalize binary sums to labeled variants .

  16. New syntactic forms t ::= ... terms tagging <l=t> as T case t of <l i =x i > ⇒ t i i ∈ 1 .. n case T ::= ... types type of variants <l i :T i i ∈ 1 .. n >

  17. New evaluation rules → t ′ t − i ∈ 1 .. n case (<l j =v j > as T) of <l i =x i > ⇒ t i ( E-CaseVariant ) − → [ x j �→ v j ] t j → t ′ t 0 − 0 ( E-Case ) i ∈ 1 .. n case t 0 of <l i =x i > ⇒ t i → case t ′ − 0 of <l i =x i > ⇒ t i i ∈ 1 .. n → t ′ t i − i ( E-Variant ) → <l i =t ′ <l i =t i > as T − i > as T

  18. New typing rules Γ ⊢ t : T Γ ⊢ t j : T j i ∈ 1 .. n > ( T-Variant ) Γ ⊢ <l j =t j > as <l i :T i i ∈ 1 .. n > : <l i :T i Γ ⊢ t 0 : <l i :T i i ∈ 1 .. n > for each i Γ , x i :T i ⊢ t i : T ( T-Case ) i ∈ 1 .. n : T Γ ⊢ case t 0 of <l i =x i > ⇒ t i

  19. Example Addr = <physical:PhysicalAddr, virtual:VirtualAddr>; a = <physical=pa> as Addr; getName = λ a:Addr. case a of <physical=x> ⇒ x.firstlast | <virtual=y> ⇒ y.name;

  20. Options Just like in OCaml... OptionalNat = <none:Unit, some:Nat>; Table = Nat → OptionalNat; emptyTable = λ n:Nat. <none=unit> as OptionalNat; extendTable = λ t:Table. λ m:Nat. λ v:Nat. λ n:Nat. if equal n m then <some=v> as OptionalNat else t n; x = case t(5) of <none=u> ⇒ 999 | <some=v> ⇒ v;

  21. Enumerations Weekday = <monday:Unit, tuesday:Unit, wednesday:Unit, thursday:Unit, friday:Unit>; nextBusinessDay = λ w:Weekday. case w of <monday=x> ⇒ <tuesday=unit> as Weekday | <tuesday=x> ⇒ <wednesday=unit> as Weekday | <wednesday=x> ⇒ <thursday=unit> as Weekday | <thursday=x> ⇒ <friday=unit> as Weekday | <friday=x> ⇒ <monday=unit> as Weekday;

Recommend


More recommend