lecture 4 5 types
play

Lecture 4-5 : Types Dont prove correctness: just find bugs .. - - PowerPoint PPT Presentation

CS6202: Advanced Topics in Rise of Lightweight Formal Methods Rise of Lightweight Formal Methods Programming Languages and Systems Lecture 4-5 : Types Dont prove correctness: just find bugs .. - model checking - light specification and


  1. CS6202: Advanced Topics in Rise of Lightweight Formal Methods Rise of Lightweight Formal Methods Programming Languages and Systems Lecture 4-5 : Types Don’t prove correctness: just find bugs .. - model checking - light specification and verification (e.g. ESC, SLAM ..) “Types for Extended Lambda Calculus” - type-checking! Basic ideas are long established; but industrial attitudes Lecturer : Chin Wei Ngan have been softened by the success of model checking in hardware design. Email : chinwn@comp.nus.edu.sg Office : S15 06-01 “Formal methods will never have any impact until they can be used by people that don’t understand them” : Tom Melham CS6202 Extended Lambda Calculus 1 CS6202 Extended Lambda Calculus 2 What is a Type Systems? What is a Type Systems? Why Type Systems? Why Type Systems? A Type System is a Type systems are good for: tractable syntactic method • detecting errors • • abstraction • for proving the absence of certain program • documentation behaviors • language design • efficiency • by classifying phrases according to the • safety kinds of values they compute • .. etc.. (security,exception,theorem-proving,web- metadata,categorical grammer) CS6202 Extended Lambda Calculus 3 CS6202 Extended Lambda Calculus 4

  2. Pure Simply Typed Lambda Calculus Typing Pure Simply Typed Lambda Calculus Typing x:T ∈ Γ t ::= terms • (T-Var) Γ ` x : T variable x λ x:T.t abstraction application t t v ::= value Γ , x:T 1 ` t 2 : T 2 • (T-Abs) λ x :T.t abstraction value Γ ` λ x:T 1 .t 2 : T 1 → T 2 T ::= types • type of functions T → T Γ ::= • contexts Γ ` t 1 : T 1 → T 2 Γ ` t 2 : T 1 ∅ (T-App) empty context Γ ` t 1 t 2 : T 2 Γ , x:T type variable binding CS6202 Extended Lambda Calculus 5 CS6202 Extended Lambda Calculus 6 Where are the Base Types? Where are the Base Types? Unit Type Unit Type New Syntax: T ::= types • t ::= … terms type of functions T → T constant unit unit v ::= … values constant unit unit Extend with uninterpreted base types, e.g. T ::= … types T ::= types • unit type Unit type of functions T → T Note that Unit type has only one possible value. base type 1 A base type 2 New Evaluation Rules: None B base type 3 C New Typing Rules : : Γ ` unit : Unit T-Unit CS6202 Extended Lambda Calculus 7 CS6202 Extended Lambda Calculus 8

  3. Sequencing : Basic Idea Lambda Calculus with Sequencing Sequencing : Basic Idea Lambda Calculus with Sequencing New Syntax Syntax : e1; e2 t ::= … terms • sequence t ; t Evaluate an expression (to achieve some side-effect, such as printing), ignore its result and then evaluate another New Evaluation Rules: expression. Examples: t 1 → t ‘ 1 (E-Seq) t 1 ; t 2 → t ‘ 1 ; t 2 (print x); x+1 (printcurrenttime); compute; (printcurrenttime) unit ; t → t (E-SeqUnit) CS6202 Extended Lambda Calculus 9 CS6202 Extended Lambda Calculus 10 Sequencing (cont) Sequencing (cont) Sequencing (Second Version) Sequencing (Second Version) Treat t 1 ;t 2 as an abbreviation for ( λ x:Unit. t 2 ) t 1 . • New Typing Rule: • Then the evaluation and typing rules for abstraction and Γ ` t 1 : Unit 1 Γ ` t 2 : T 2 application will take care of sequencing! (T-Seq) Γ ` t 1 ; t 2 : T 2 • Such shortcuts are called derived forms (or syntactic sugar ) and are heavily used in programming language definition. CS6202 Extended Lambda Calculus 11 CS6202 Extended Lambda Calculus 12

  4. Equivalence of two Sequencing Ascription : Motivation Equivalence of two Sequencing Ascription : Motivation Let λ E be the simply typed lambda calculus with the Unit type Sometimes, we want to say explicitly that a term has a and the sequencing construct. certain type. Let λ I be the simply-typed lambda calculus with Unit only. Reasons: • as comments for inline documentation Let e ∈ λ E → λ I be the elaboration function that translates • for debugging type errors from λ E To λ I . • control printing of types (together with type syntax) Then, we have for each term t : • casting (Chapter 15) • resolve ambiguity (see later) t → E t’ iff e(t) → I e(t’) • Γ ` E t:T iff Γ ` I e(t):T • CS6202 Extended Lambda Calculus 13 CS6202 Extended Lambda Calculus 14 Ascription : Syntax Ascription : Syntax Ascription (cont) Ascription (cont) New Evaluation Rules: New Syntax t ::= … terms • v as T → v (E-Ascribe1) ascription t as T t → t ‘ (E- Ascribe2) t as T → t ‘ as T Example: New Typing Rules: (f (g (h x y z))) as Bool Γ ` t : T (T-Ascribe) Γ ` t as T : T CS6202 Extended Lambda Calculus 15 CS6202 Extended Lambda Calculus 16

  5. Let Bindings : Motivation Lambda Calculus with Let Binding Let Bindings : Motivation Lambda Calculus with Let Binding New Syntax • Let expression allow us to give a name to the result t ::= … terms of an expression for later use and reuse. let binding let x=t in t • Examples: let pi=<long computation> in …pi..pi..pi…. New Typing Rule: let square = λ x:Nat. x*x in ….(square 2)..(square 4)… Γ ` t 1 : T 1 Γ , x:T 1 ` t 2 : T 2 (T-Let) Γ ` let x=t 1 in t 2 : T 2 CS6202 Extended Lambda Calculus 17 CS6202 Extended Lambda Calculus 18 Let Bindings as Derived Form Let Bindings as Derived Form Pairs : Motivation Pairs : Motivation Pairs provide the simplest kind of data structures. We can consider let expressions as derived form: Examples: In untyped setting: let x=t 1 in t 2 abbreviates to ( λ x. t 2 ) t 1 {9, 81} In a typed setting: λ x : Nat. {x, x*x} let x=t 1 in t 2 abbreviates to ( λ x:?. t 2 ) t 1 How to get type declaration for the formal parameter? Answer : Type inference (see later). CS6202 Extended Lambda Calculus 19 CS6202 Extended Lambda Calculus 20

  6. Pairs : Syntax Pairs : Typing Rules Pairs : Syntax Pairs : Typing Rules Γ ` t 1 : T 1 Γ ` t 2 : T 2 t ::= … terms • (T-Pair) Γ ` {t 1 ,t 2 } : T 1 × T 2 variable {t, t} first projection t.1 second projection t.2 Γ ` t : T 1 × T 2 (T-Proj1) v ::= … value Γ ` t.1 : T 1 • pair value {v, v} T ::= … types • Γ ` t : T 1 × T 2 (T-Proj2) product type T × T Γ ` t.2 : T 2 CS6202 Extended Lambda Calculus 21 CS6202 Extended Lambda Calculus 22 Tuples Tuples Records Records Tuples are a straightforward generalization of pairs, Sometimes it is better to have components labeled more where n terms are combined in a tuple expression. meaningfully instead of via numbers 1..n, as in tuples Example: Tuples with labels are called records. {1, true, unit} : {Nat, Bool, Unit} {1,{true, 0}} : {Nat, {Bool, Nat}} Example: {} : {} {partno=5524,cost=30.27,instock =false} has type {partno:Nat, cost:Float, instock:Bool} Note that n may be 0 . Then the only value is {} with instead of: type {} . Such a type is isomorphic to Unit . {5524,30.27,false} : {Nat, Float, Bool} CS6202 Extended Lambda Calculus 23 CS6202 Extended Lambda Calculus 24

  7. Sums : Motivation Sums : Motivation Sums : Motivation Sums : Motivation Often, we may want to handle values of different Given a sum type; e.g. structures with the same function. K = Nat + Bool Examples: Need to use tags inl and inr to indicate that a value is a particular member of the sum type; e.g. PhysicalAddr={firstlast:String, add:String} VirtualAddr={name:String, email:String} but not inr 5 : K nor 5 : K inl 5 : K A sum type can then be written like this: inr true : K Addr = PhysicalAddr + VirtualAddr CS6202 Extended Lambda Calculus 25 CS6202 Extended Lambda Calculus 26 Sums : Motivation Sums : Motivation Sums : Syntax Sums : Syntax Given the address type: t ::= … terms • tagging (left) inl t as T Addr = PhysicalAddr + VirtualAddr tagging (right) inr t as T pattern matching case t of {p i => t i } We can use case construct to analyse the particular value of sum type: v ::= … value • tagged value (left) inl v as T getName = λ a : Addr. case a of tagged value (right) inr v as T inl x => a.firstlast inr y => y.name T ::= … types • sum type T + T CS6202 Extended Lambda Calculus 27 CS6202 Extended Lambda Calculus 28

  8. Sums : Typing Rules Variants : Labeled Sums Sums : Typing Rules Variants : Labeled Sums Instead of inl and inr , we may like to use nicer labels, Γ ` t 1 : T 1 (T-Inl) just as in records. This is what variants do. Γ ` inl t 1 as T 1 × T 2 : T 1 × T 2 For types, instead of: T 1 + T 2 we write: <l 1 :T 1 + l 2 :T 2 > Γ ` t 2 : T 2 (T-Inr) Γ ` inr t 2 as T 1 × T 2 : T 1 × T 2 For terms, instead of: inl r as T 1 + T 2 we write: <l 1 = t> as <l 1 :T 1 + l 2 :T 2 > CS6202 Extended Lambda Calculus 29 CS6202 Extended Lambda Calculus 30 Variants : Example Variants : Example Application : Enumeration Application : Enumeration An example using variant type: Enumeration are variants that only make use of their Addr =<physical:PhysicalAddr, virtual:VirtualAddr> labels. Each possible value is unit . A variant value: Weekday =<monday:Unit, tuesday:Unit, a = <physical=pa> as Addr wednesday:Unit, thursday:Unit, friday:Unit > Function over variant value: getName = λ a : Addr. case a of <physical=x> ⇒ x.firstlast ⇒ y.name <virtual=y> CS6202 Extended Lambda Calculus 31 CS6202 Extended Lambda Calculus 32

Recommend


More recommend