04 typing and denotational semantics of simpl
play

04Typing and Denotational Semantics of simPL CS4215: Programming - PowerPoint PPT Presentation

Typing of simPL Denotational Semantics of simPL 04Typing and Denotational Semantics of simPL CS4215: Programming Language Implementation Martin Henz February 3, 2012 Generated on Thursday 2 February, 2012, 18:17 CS4215: Programming


  1. Typing of simPL Denotational Semantics of simPL 04—Typing and Denotational Semantics of simPL CS4215: Programming Language Implementation Martin Henz February 3, 2012 Generated on Thursday 2 February, 2012, 18:17 CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  2. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL 1 Typing of simPL Type Environments Typing Relation for simPL Type Safety of simPL 2 Denotational Semantics of simPL CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  3. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Example Is x + 3 well-typed? CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  4. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Type Environments A Type environment , denoted by Γ, keeps track of the type of indentifiers appearing in the expression. Γ( x ) returns the type that is known by environment Γ for the identifier x . CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  5. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Environment Extension If Γ[ x ← t ]Γ ′ , then Γ ′ behaves like Γ, except that the type of x is t . CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  6. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Example Let Γ = ∅ . ∅ [ AboutPi ← int ]Γ ′ Γ ′ ( AboutPi ) = int Γ ′ [ Square ← int -> int ]Γ ′′ dom (Γ ′′ ) = { AboutPi , Square } CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  7. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Typing Relation The set of well-typed expressions is defined by the ternary typing relation , written Γ ⊢ E : t , where Γ is a type environment such that E ⊲ ⊳ X and X ⊆ dom (Γ). “The expression E has type t , under the assumption that its free identifiers have the types given by Γ.” CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  8. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Examples Γ ′ ⊢ AboutPi ∗ 2 : int Γ ′′ ⊢ fun { int -> int } x -> AboutPi * (Square 2 ) end : int -> int CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  9. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Examples Γ ′ ⊢ fun { int -> int } x -> AboutPi * (Square 2 ) end : int -> int does not hold, because Square occurs free in the expression, but the type environment Γ ′ to the left of the ⊢ symbol is not defined for Square . CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  10. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation [VarT] Γ ⊢ x : Γ( x ) If Γ( x ) is not defined, then this rule is not applicable. In this case, we say that there is no type for x derivable from the assumptions Γ. CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  11. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) [NumT] [TrueT] Γ ⊢ n : int Γ ⊢ true : bool [FalseT] Γ ⊢ false : bool CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  12. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) For each primitive operation p that takes n arguments of types t 1 , ..., t n and returns a value of type t , we have exactly one rule of the following form. Γ ⊢ E 1 : t 1 · · · Γ ⊢ E n : t n [PrimT] Γ ⊢ p [ E 1 , . . . , E n ] : t CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  13. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) p t 1 t 2 t + int int int - int int int * int int int / int int int & bool bool bool | bool bool bool \ bool bool = int int bool < int int bool > int int bool CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  14. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) Γ ⊢ E : bool Γ ⊢ E 1 : t Γ ⊢ E 2 : t Γ ⊢ if E then E 1 else E 2 end : t CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  15. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) Γ 1 [ x 1 ← t 1 ]Γ 2 · · · Γ n [ x n ← t n ]Γ n +1 Γ n +1 ⊢ E : t Γ 1 ⊢ fun { t 1 ∗ · · · ∗ t n -> t } x 1 . . . x n -> E end : t 1 ∗ · · · ∗ t n -> t CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  16. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) Γ[ f ← t 1 ∗ · · · ∗ t n -> t ]Γ 1 Γ 1 [ x 1 ← t 1 ]Γ 2 · · · Γ n [ x n ← t n ]Γ n +1 Γ n +1 ⊢ E : t Γ ⊢ recfun f { t 1 ∗ · · · ∗ t n -> t } x 1 . . . x n -> E end : t 1 ∗ · · · t n -> t CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  17. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Definition of Typing Relation (cont’d) Γ ⊢ E : t 1 ∗ · · · ∗ t n -> t Γ ⊢ E 1 : t 1 · · · Γ ⊢ E n : t n Γ ⊢ ( E E 1 · · · E n ) : t CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  18. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Well-Typedness An expression E is well-typed, if there is a type t such that E : t . CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  19. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Example Proof ∅ ⊢ 2 : int ∅ ⊢ 3 : int ∅ ⊢ 2*3 : int ∅ ⊢ 7 : int ∅ ⊢ 2*3>7 : bool CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  20. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Example Proof Γ ⊢ x : int Γ ⊢ 1 : int ∅ [ x ← int ]Γ Γ ⊢ x+1 : int ∅ ⊢ fun { int -> int } x -> x+1 end : int -> int ∅ ⊢ 2 : int ∅ ⊢ (fun { int -> int } x -> x+1 end 2) : int CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  21. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Unique Type Lemma For every expression E and every type assignment Γ , there exists at most one type t such that Γ ⊢ E : t. CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  22. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL More Properties of Typing Relation Lemma Typing is not affected by “junk” in the type assignment. If Γ ⊢ E : t, and Γ ⊂ Γ ′ , then Γ ′ ⊢ E : t. Lemma Substituting an identifier by an expression of the same type does not affect typing. If Γ[ x ← t ′ ]Γ ′ , Γ ′ ⊢ E : t, and Γ ⊢ E ′ : t ′ , then Γ ⊢ E ′′ : t, where E [ x ← E ′ ] E ′′ . CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  23. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Type Safety Type safety is a property of a given language with a given static and dynamic semantics. It says that if a program of the languge is well-typed, certain problems are guaranteed not to occur at runtime. What do we consider as “problems”? CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

  24. Type Environments Typing of simPL Typing Relation for simPL Denotational Semantics of simPL Type Safety of simPL Components of Type Safety Progress. Well-typed expressions are values or can be further evaluated. Preservation. Well-typed expressions do not change their type during evaluation. CS4215: Programming Language Implementation 04—Typing and Denotational Semantics of simPL

Recommend


More recommend