objectives typing semantics
play

Objectives Typing Semantics Explain the parts of a type judgment. - PowerPoint PPT Presentation

Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Objectives Typing Semantics Explain the parts of a type judgment. Build proof trees to indicate the derivation of a type for a program. Dr.


  1. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Objectives Typing Semantics ◮ Explain the parts of a type judgment. ◮ Build proof trees to indicate the derivation of a type for a program. Dr. Mattox Beckman ◮ Explain the circumstances under which a type environment can be University of Illinois at Urbana-Champaign modifjed. Department of Computer Science Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples The Language Format of a Type Judgment ◮ We are going to type λ -calculus extended with let , if , arithmetic, and comparisons. A type judgment has the following form: L ::= λ x . L abstractions L L applications | Γ ⊢ e : α let x = L in L Let expressions | where Γ is a type environment , e is some expression, and α is a type . if L then L else L If expressions | E expressions | ◮ Γ ⊢ if true then 4 else 38 : Int E ::= x variables ◮ Γ ⊢ true && false : Bool n integers | b booleans Note: the ⊢ is pronounced “turnstile” or “entails”. | E ⊕ E integer operations | E ∼ E integer comparisons | E && E boolean and | E || E boolean or |

  2. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples The Parts of a Rule Axioms Assumptions on top Constants ⊢ n : Int , when n is an integer . Γ ⊢ e 1 : Int Γ ⊢ e 2 : Int Γ ⊢ e 1 + e 2 : Int ⊢ true : Bool Conclusion on the bottom ◮ If a rule has no assumptions, then it is called an axiom . ⊢ false : Bool ◮ Γ is a set of the form { x : α ; . . . } . Variables Γ ⊢ x : α, if x : α ∈ Γ ◮ Γ may be left out if we don’t need a type environment. ◮ Basic Idea : The meaning of an expression can be determined by ◮ Here, α is a type variable ; it stands for another type. combining the meaning of its parts. ◮ These are rules that are true no matter what the context is. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Simple Rules Example 0 Γ ⊢ e 1 : Int Γ ⊢ e 2 : Int Arithmetic Suppose we want to prove that Γ ⊢ ( x ∗ 5 > 7) && y : Bool . Γ ⊢ e 1 ⊕ e 2 : Int Assume that Γ = { x : Int ; y : Bool } Γ ⊢ e 1 : Int Γ ⊢ e 2 : Int Relations Γ ⊢ e 1 ∼ e 2 : Bool First thing: Write down the thing you are trying to prove, and put a bar over it. Γ ⊢ e 1 : Bool Γ ⊢ e 2 : Bool Booleans Γ ⊢ ( x ∗ 5 > 7) && y : Bool Γ ⊢ e 1 && e 2 : Bool Look at the outermost expression. What rule applies here? Γ ⊢ e 1 : Bool Γ ⊢ e 2 : Bool Γ ⊢ e 1 || e 2 : Bool

  3. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 0 Example 0 Suppose we want to prove that Γ ⊢ ( x ∗ 5 > 7) && y : Bool . Assume that Γ = { x : Int ; y : Bool } Suppose we want to prove that Γ ⊢ ( x ∗ 5 > 7) && y : Bool . Assume that Γ = { x : Int ; y : Bool } First thing: Write down the thing you are trying to prove, and put a bar Write parts on top and put a bar over them as well. over it. Γ ⊢ ( x ∗ 5 > 7) && y : Bool Γ ⊢ x ∗ 5 > 7 : Bool Γ ⊢ y : Bool Look at the outermost expression. What rule applies here? Γ ⊢ ( x ∗ 5 > 7) && y : Bool What to do next? Let’s work left to right. The expression we want next is Γ ⊢ e 1 : Bool Γ ⊢ e 2 : Bool a “greater” expression. (Besides, the y expression is already an axiom.) Γ ⊢ e 1 && e 2 : Bool Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 0 Example 0 Suppose we want to prove that Γ ⊢ ( x ∗ 5 > 7) && y : Bool . Suppose we want to prove that Γ ⊢ ( x ∗ 5 > 7) && y : Bool . Assume that Γ = { x : Int ; y : Bool } Assume that Γ = { x : Int ; y : Bool } At this point, there are no more subtrees to expand out. We are done. Following the “greater” rule, we break the x * 5 > 7 into two parts. Γ ⊢ x : Int Γ ⊢ 5 : Int Γ ⊢ x ∗ 5 : Int Γ ⊢ 7 : Int Γ ⊢ x ∗ 5 : Int Γ ⊢ 7 : Int Γ ⊢ x ∗ 5 > 7 : Bool Γ ⊢ y : Bool Γ ⊢ x ∗ 5 > 7 : Bool Γ ⊢ y : Bool Γ ⊢ ( x ∗ 5 > 7) && y : Bool Γ ⊢ ( x ∗ 5 > 7) && y : Bool We will turn our attention to the multiplication now.

  4. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Type Variables in Rules Function Application A monotype τ can be a ◮ type constant (e.g., Int , Bool , etc.) ◮ instantiated type constructor (e.g., [Int] , Int → Int ) Γ ⊢ e 1 : α 2 → α Γ ⊢ e 2 : α 2 ◮ a type variable α Γ ⊢ e 1 e 2 : α If Rule ◮ If you have a function of type α 2 → α and an argument e 2 of type α 2 , then applying e 1 to e 2 will produce an expression of type α . Γ ⊢ e 1 : Bool Γ ⊢ e 2 : α Γ ⊢ e 3 : α ◮ You can generalize this rule to multiple arguments. Γ ⊢ if e 1 then e 2 else e 3 : α Γ ⊢ incList : [Int] → [Int] Γ ⊢ xx : [Int] ◮ Here, α is a meta-variable. Γ ⊢ incList xx : [Int] ◮ This rule says that if can result in any type, as long as the then and else branches have the same type. This could even include functions. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Function Rule Function Rule Γ ∪ { x : α 1 } ⊢ e : α 2 Γ ∪ { x : α 1 } ⊢ e : α 2 Γ ⊢ λ x . e : α 1 → α 2 Γ ⊢ λ x . e : α 1 → α 2 ◮ Important point: this rule describes types, and also describes when ◮ Important point: this rule describes types, and also describes when you may change Γ . you may change Γ . ◮ You may NOT change Γ except as described! ◮ You may NOT change Γ except as described! Example: show that {} ⊢ λ x . x + 1 : Int → Int . {} ⊢ λ x . x + 1 : Int → Int

  5. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Function Rule Function Rule Γ ∪ { x : α 1 } ⊢ e : α 2 Γ ∪ { x : α 1 } ⊢ e : α 2 Γ ⊢ λ x . e : α 1 → α 2 Γ ⊢ λ x . e : α 1 → α 2 ◮ Important point: this rule describes types, and also describes when ◮ Important point: this rule describes types, and also describes when you may change Γ . you may change Γ . ◮ You may NOT change Γ except as described! ◮ You may NOT change Γ except as described! { x : Int } ⊢ x : Int { x : Int } ⊢ 1 : Int { x : Int } ⊢ x + 1 : Int { x : Int } ⊢ x + 1 : Int {} ⊢ λ x . x + 1 : Int → Int {} ⊢ λ x . x + 1 : Int → Int Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Let Rule Example 1 — Proof ◮ Here is let . Note that Haskell uses the recursive rule, and it is Prove that Γ ⊢ ( λ x .λ y . x + y ) : Int → Int → Int . polymorphic. Assume that Γ = {} Γ ⊢ e 1 : τ 1 Γ ∪ [ x : τ 1 ] ⊢ e 2 : τ 2 Let Γ ⊢ let x = e 1 in e 2 : τ 2 Γ ∪ [ x : τ 1 ] ⊢ e 1 : τ 1 Γ ∪ [ x : τ 1 ] ⊢ e 2 : τ 2 Letrec Γ ⊢ let x = e 1 in e 2 : τ 2

  6. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 1 — Proof Example 1 — Proof Prove that Γ ⊢ ( λ x .λ y . x + y ) : Int → Int → Int . Prove that Γ ⊢ ( λ x .λ y . x + y ) : Int → Int → Int . Assume that Γ = {} Assume that Γ = {} { x : Int } ⊢ ( λ y . x + y ) : Int → Int {} ⊢ ( λ x .λ y . x + y ) : Int → Int → Int {} ⊢ ( λ x .λ y . x + y ) : Int → Int → Int Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 1 — Proof Example 1 — Proof Prove that Γ ⊢ ( λ x .λ y . x + y ) : Int → Int → Int . Prove that Γ ⊢ ( λ x .λ y . x + y ) : Int → Int → Int . Assume that Γ = {} Assume that Γ = {} Γ ′ ⊢ x : Int Γ ′ ⊢ y : Int { x : Int , y : Int } ⊢ x + y : Int Γ ′ ≡ { x : Int , y : Int } ⊢ x + y : Int { x : Int } ⊢ ( λ y . x + y ) : Int → Int { x : Int } ⊢ ( λ y . x + y ) : Int → Int {} ⊢ ( λ x .λ y . x + y ) : Int → Int → Int {} ⊢ ( λ x .λ y . x + y ) : Int → Int → Int

  7. Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 1 — inferencing Example 1 — inferencing Infer the type of ( λ x .λ y . x + y ) . Infer the type of ( λ x .λ y . x + y ) . Assume that Γ = {} Assume that Γ = {} {} ⊢ ( λ x .λ y . x + y ) : α Introduction Typing Rules Monotypes Examples Introduction Typing Rules Monotypes Examples Example 1 — inferencing Example 1 — inferencing Infer the type of ( λ x .λ y . x + y ) . Infer the type of ( λ x .λ y . x + y ) . Assume that Γ = {} Assume that Γ = {} { x : β, y : δ } ⊢ x + y : τ { x : β } ⊢ ( λ y . x + y ) : γ { x : β } ⊢ ( λ y . x + y ) : γ ≡ δ → τ {} ⊢ ( λ x .λ y . x + y ) : α ≡ β → γ {} ⊢ ( λ x .λ y . x + y ) : β → δ → τ

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend