4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:

Slides:



Advertisements
Similar presentations
Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Advertisements

Letrec fact(n) = if zero?(n) then 1 else *(n, (fact sub1(n))) 4.4 Type Inference Type declarations aren't always necessary. In our toy typed language,
1 How to transform an analyzer into a verifier. 2 OUTLINE OF THE LECTURE a verification technique which combines abstract interpretation and Park’s fixpoint.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
INF 3110/ INF INF Oppgave 6.7 Grammar RuleSemantic Rule decl → var-list : type var-list 1 → var-list 2 ; id var-list.
Conditionals Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 9 Decisions, Decisions, Decisions.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Compiler Construction
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
EOPL3: Section 3.3 PROC and App B: SLLGEN
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Thinking Mathematically Statements, Negations, and Quantified Statements.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Lesson 1.4 Equations and Inequalities Goal: To learn how to solve equations and check solutions of equations and inequalities.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
Lesson thirteen Conditional Statement "if- else" ©
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Operational Semantics of Scheme
Relational Operator and Operations
Abstract Syntax cs7100 (Prasad) L7AST.
Implementing Recursion
Chapter 4: Decision Structures and Boolean Logic
Case Study: Undefined Variables
زبان بدن Body Language.
Chapter 4: Types.
Chapter 4: Decision Structures and Boolean Logic
SOLVING ABSOLUTE-VALUE EQUATIONS
TRUTH TABLES.
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Chapter 1 Review: BNF Grammar for lists:
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
Relational Operators.
Nate Brunelle Today: Conditional Decision Statements
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Chapter 4: Decision Structures and Boolean Logic
Programming Languages Dan Grossman 2013
Inductively Defined Data Concrete and Abstract syntax
CMPU-145: Foundations of Computer Science Spring, 2019
Presentation transcript:

4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:

4.2 Type Checking Will want to check the type of all expressions. Some expressions ( proc, let / letrec ) will need to declare types. Others ( if, application) will know about types from sub-expressions. Language of this chapter is functional, not OO.

::= int int-type-exp () ::= bool bool-type-exp () ::= ( { } * ( * ) -> ) proc-type-exp (arg-texps result-texp) Recall grammar for type declarations: Now can write grammar for typed language: e.g., (int*int) -> bool

::= proc ( { }* (,) ) proc-exp (arg-texps ids body) Grammar for Typed Language ::= true true-exp ::= false false-exp

Grammar for Typed Language ::= letrec { ( { }* (,) ) = }* in letrec-exp (result-texps proc-names arg-texpss idss bodies letrec-body)

Grammar for Typed Language Examples: (1) proc (int x) add1(x) (2) letrec int fact (int x) = if zero?(x) then 1 else *(x, (fact sub1(x))) in (fact 3) Q: Why don't we need to say int proc in (1) ?

Need a data structure for types. Recall that types are either primitives ( int, bool ) or procedures/functions over types: int->bool (define-datatype type type? (atomic-type (name symbol?)) ; int, bool,... (proc-type (arg-types (list-of type?)) (result-type type?))) Data Structures for Typed Language

Now can define as many primitive types as we like: (define int-type (atomic-type 'int)) (define bool-type (atomic-type 'bool)) Checking type equality can be done using equal? (define check-equal-type! (lambda (t1 t2) (if (not (equal? t1 t2)) (eopl:error...)))) Q: why ! in check-equal-type! Now can write type-of-expression :

(define type-of-expression (lambda (exp tenv) (cases expression exp (lit-exp (number) int-type) (true-exp () bool-type) (false-exp () bool-type) (var-exp (id) (apply-tenv tenv id))