Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Substitution & Evaluation Order cos 441 David Walker.
Intermediate Code Generation
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
Foundations of Programming Languages: Introduction to Lambda Calculus
1 Amazing fact #3: -calculus is Turing-complete! But the -calculus is too weak, right? No multiple arguments! No numbers or arithmetic! No booleans or.
Inductive Definitions COS 510 David Walker. Inductive Definitions Inductive definitions play a central role in the study of programming languages They.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Logic Gates Circuits to manipulate 0’s and 1’s. 0’s and 1’s used for numbers Also to make decisions within the computer. In that context, 1 corresponds.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
(Advanced) topics in Programming Languages Instructor: Mooly Sagiv TA: Shachar Itzhaky Spring 2012 Inspired.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Type Safety Kangwon National University 임현승 Programming Languages.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
Types and Programming Languages Lecture 12 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lesson2 Lambda Calculus Basics 1/10/02 Chapter 5.1, 5.2.
Type Checking Textbook: Types and Programming Languages Benjamin Pierce.
Lambda Calculus CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lesson 3 Formalizing and Implementing Pure Lambda Calculus 1/15/02 Chapters 5.3, 6, 7.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Lecture 9 : Universal Types
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
Lesson 5 Simple extensions of the typed lambda calculus
CS5205: Foundations in Programming Languages
String is a synonym for the type [Char].
CS 550 Programming Languages Jeremy Johnson
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
CSE-321 Programming Languages Simply Typed -Calculus
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
More on Lambda Calculus
CS 611: Lecture 9 More Lambda Calculus: Recursion, Scope, and Substitution September 17, 1999 Cornell University Computer Science Department Andrew Myers.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Typed Arithmetic Expressions
Lesson2 Lambda Calculus Basics
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
CSE S. Tanimoto Lambda Calculus
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Switching Lemmas and Proof Complexity
Presentation transcript:

Lesson 4 Typed Arithmetic Typed Lambda Calculus 1/21/02 Chapters 8, 9, 10

Lesson 4: Typed Arith & Lambda2 Outline Types for Arithmetic –types –the typing relation –safety = progress + preservation The simply typed lambda calculus –Function types –the typing relation –Curry-Howard correspondence –Erasure: Curry-style vs Church-style Implementation

Lesson 4: Typed Arith & Lambda3 Terms for arithmetic t :: = true false if t then t else t 0 succ t pred t iszero t v :: = true false nv nv ::= 0 succ nv TermsValues

Lesson 4: Typed Arith & Lambda4 Boolean and Nat terms Some terms represent booleans, some represent natural numbers. t :: = true false if t then t else t 0 succ t pred t iszero t if t then t else t

Lesson 4: Typed Arith & Lambda5 Nonsense terms Some terms don’t make sense. They represent neither booleans nor natural numbers. succ true iszero false if succ(0) then true else false These terms are stuck -- no evaluation rules apply, but they are not values. But what about the following? if iszero(0) then true else 0

Lesson 4: Typed Arith & Lambda6 Space of terms 0 false true succ(0) iszero(pred(0)) Terms if true then 0 else succ(0) succ(succ(0))

Lesson 4: Typed Arith & Lambda7 Bool and Nat values iszero(pred(0)) Terms if true then 0 else succ(0) false true 0 succ(0) succ(succ(0)) Boolean values Nat values

Lesson 4: Typed Arith & Lambda8 Bool and Nat types Terms false true Evals to Bool value 0 Evals to Nat value Bool type Nat type

Lesson 4: Typed Arith & Lambda9 Evaluation preserves type Terms Bool Nat

Lesson 4: Typed Arith & Lambda10 A Type System 1.type expressions: T ::=... 2.typing relation : t : T 3.typing rules giving an inductive definition of t: T

Lesson 4: Typed Arith & Lambda11 Typing rules for Arithmetic: BN (typed) T ::= Bool | Nat (type expressions) true : Bool (T-True) false : Bool (T-False) 0 : Nat (T-Zero) t1: Boolt3: T if t1 then t2 else t3 : T (T-If) t2: T t1: Nat succ t1 : Nat (T-Succ) t1: Nat pred t1 : Nat (T-Pred) t1: Nat iszero t1 : Bool (T-IsZero)

Lesson 4: Typed Arith & Lambda12 Typing relation Defn: The typing relation t: T for arithmetic expressions is the smallest binary relation between terms and types satisfying the given rules. A term t is typable (or well typed) if there is some T such that t : T.

Lesson 4: Typed Arith & Lambda13 Inversion Lemma Lemma (8.2.2). [Inversion of the typing relation] 1. If true : R then R = Bool 2. If false : R then R = Bool 3. If if t1 then t2 else t3 : R then t1 : Bool and t2, t3 : R 4. If 0: R then R = Nat 5. If succ t1 : R then R = Nat and t1 : Nat 6. If pred t1 : R then R = Nat and t1 : Nat 7. If iszero t1 : R then R = Bool and t1 : Nat

Lesson 4: Typed Arith & Lambda14 Typing Derivations A type derivation is a tree of instances of typing rules with the desired typing as the root. iszero(0): Boolpred(0): Nat if iszero(0) then 0 else pred 0 : Nat (T-If) 0: Nat (T-Zero) (T-Pred)(T-IsZero) The shape of the derivation tree exactly matches the shape of the term being typed.

Lesson 4: Typed Arith & Lambda15 Uniqueness of types Theorem (8.2.4). Each term t has at most one type. That is, if t is typable, then its type is unique, and there is a unique derivation of its type.

Lesson 4: Typed Arith & Lambda16 Safety (or Soundness) Safety = Progress + Preservation Progress: A well-typed term is not stuck -- either it is a value, or it can take a step according to the evaluation rules. Preservation: If a well-typed term makes a step of evaluation, the resulting term is also well-typed. Preservation is also known as “subject reduction”

Lesson 4: Typed Arith & Lambda17 Cannonical forms Defn: a cannonical form is a well-typed value term. Lemma (8.3.1). 1. If v is a value of type Bool, then v is true or v is false. 2. If v is a value of type Nat, then v is a numeric value, i.e. a term in nv, where nv ::= 0 | succ nv.

Lesson 4: Typed Arith & Lambda18 Progress and Preservation for Arithmetic Theorem (8.3.2) [Progress] If t is a well-typed term (that is, t: T for some type T), then either t is a value or else t  t’ for some t’. Theorem (8.3.3) [Preservation] If t: T and t  t’ then t’ : T. Proofs are by induction on the derivation of t: T.

Lesson 4: Typed Arith & Lambda19 Simply typed lambda calculus To type terms of the lambda calculus, we need types for functions (lambda terms): T1 -> T2 A function type T1 -> T2 specifies the argument type T1 and the result type T2 of the function.

Lesson 4: Typed Arith & Lambda20 Simply typed lambda calculus The abstract syntax of type terms is T ::= base types T -> T We need base types (e.g Bool) because otherwise we could build no type terms. We also need terms of these base types,so we have an “applied” lambda calculus. In this case, we will take Bool as the sole base type and add corresponding Boolean terms.

Lesson 4: Typed Arith & Lambda21 Abstract syntax and values Terms t :: = true false if t then t else t x x: T. t t t v :: = true false x: T. t Values Note that terms contain types! Lambda expressions are explicitly typed.

Lesson 4: Typed Arith & Lambda22 Typing rule for lambda terms The body of a lambda term (usually) contains free variable occurrences. We need to supply a context (  ) that gives types for the free variables. Defn. A typing context  is a list of free variables with their types. A variable can appear only once in a context.  ::=  | , x: T , x: T1 |- t2 : T2  |- x: T1. t2 : T1 -> T2 (T-Abs)

Lesson 4: Typed Arith & Lambda23 Typing rule for applications The type of the argument term must agree with the argument type of the function term.  |- t2 : T11  |- t1 t2 : T12 (T-App)  |- t1 : T11 -> T12

Lesson 4: Typed Arith & Lambda24 Typing rule for variables The type of a variable is taken from the supplied context.  |- x : T (T-Var) x : T  

Lesson 4: Typed Arith & Lambda25 Inversion of typing relation Lemma (9.3.1). [Inversion of the typing relation] 1. If  |- x : R then x: R   2. If  |- x: T1. t2 : R then R = T1 -> R2 for some R2 with , x: T1 |- t2 : R2. 3. If  |- t1 t2 : R, then there is a T11 such that  |- t1: T11 -> R and  |- t2 : T If  |- true : R then R = Bool 5. If  |- false : R then R = Bool 6. If  |- if t1 then t2 else t3 : R then  |- t1 : Bool and  |- t2, t3 : R

Lesson 4: Typed Arith & Lambda26 Uniqueness of types Theorem (9.3.3): In a given typing context  containing all the free variables of term t, there is at most one type T such that  |- t: T.

Lesson 4: Typed Arith & Lambda27 Canonical Forms (  ) Lemma (9.3.4): 1. If v is a value of type Bool, then v is either true or false. 2. If v is a value of type T1->T2, then v = x: T1.t.

Lesson 4: Typed Arith & Lambda28 Progress (  ) Theorem (9.3.5): Suppose t is a closed, well-typed term (so |- t: T for some T). Then either t is a value, or t  t’ for some t’. Proof: by induction on the derivation of |- t: T. Note: if t is not closed, e.g. f true, then it may be in normal form yet not be a value.

Lesson 4: Typed Arith & Lambda29 Permutation and Weakening Lemma (9.3.6)[Permutation]: If  |- t: T and  is a permutation of , then  |- t: T. Lemma (9.3.7)[Weakening]: If  |- t: T and x  dom(  ), then for any type S, , x: S |- t: T, with a derivation of the same depth. Proof: by induction on the derivation of |- t: T.

Lesson 4: Typed Arith & Lambda30 Substitution Lemma Lemma (9.3.8) [Preservation of types under substitutions]: If , x: S |- t : T and  |- s: S, then  |- [x  s]t: T. Proof: induction of the derivation of , x: S |- t : T. Replace leaf nodes for occurences of x with copies of the derivation of  |- s: S.

Lesson 4: Typed Arith & Lambda31 Substitution Lemma Lemma (9.3.8) [Preservation of types under substitutions]: If , x: S |- t : T and  |- s: S, then  |- [x  s]t: T. Proof: induction of the derivation of , x: S |- t : T. Replace leaf nodes for occurences of x with copies of the derivation of  |- s: S.

Lesson 4: Typed Arith & Lambda32 Preservation (  ) Theorem (9.3.9) [Preservation]: If  |- t : T and t  t’, then  |- t’ : T. Proof: induction of the derivation of  |- t : T, similar to the proof for typed arithmetic, but requiring the Substitution Lemma for the beta redex case. Homework: write a detailed proof of Thm

Lesson 4: Typed Arith & Lambda33 Introduction and Elimination rules Introduction  |- t2 : T11  |- t1 t2 : T12 (T-App)  |- t1 : T11 -> T12 , x: T1 |- t2 : T2  |- x: T1. t2 : T1 -> T2 (T-Abs) Elimination Typing rules often come in intro-elim pairs like this. Sometimes there are multiple intro or elim rules for a construct.

Lesson 4: Typed Arith & Lambda34 Erasure Defn: The erasure of a simply typed term is defined by: erase(x) = x erase( x: T. t) = x. erase(t) erase(t1 t2) = (erase(t1))(erase(t2)) erase maps a simply typed term in  to the corresponding untyped term in. erase( x: Bool. y: Bool -> Bool. y x) = x. y. y x

Lesson 4: Typed Arith & Lambda35 Erasure commutes with evaluation t m’ t’ m erase eval  eval Theorem (9.5.2) 1. if t  t’ in  then erase(t)  erase(t’) in. 2. if erase(t)  m in then there exists t’ such that t  t’ in  and erase(t’) = m.

Lesson 4: Typed Arith & Lambda36 Curry style and Church style Curry: define evaluation for untyped terms, then define the well-typed subset of terms and show that they don’t exhibit bad “run-time” behaviors. Erase and then evaluate. Church: define the set of well-typed terms and give evaluation rules only for such well-typed terms.

Lesson 4: Typed Arith & Lambda37 Homework Modify the simplebool program to add arithmetic terms and a second primitive type Nat. 1.Add Nat, 0, succ, pred, iszero tokens to lexer and parser. 2.Extend the definition of terms in the parser with arithmetic forms (see tyarith) 3.Add type and term constructors to abstract syntax in syntax.sml, and modify print functions accordingly. 3.Modify the eval and typeof functions in core.sml to handle arithmetic expressions.

Lesson 4: Typed Arith & Lambda38 Optional homework Can you define the arithmetic plus operation in  (BN)?

Lesson 4: Typed Arith & Lambda39 Sample some text

Lesson 4: Typed Arith & Lambda40 Rules prem1prem2 concl (Label) axiom(Label) prem1 concl (Label)

Lesson 4: Typed Arith & Lambda41 Symbols                                             

Lesson 4: Typed Arith & Lambda42 Space of terms Bool Nat 0 false true succ iszero Terms