Exercise Determine the output of the following program assuming static and dynamic scoping. Explain the difference, if there is any. object MyClass { val.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
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.
Chapter 5 ( ) of Programming Languages by Ravi Sethi
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero)
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Imperative Programming
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
Logic (continuation) Boolean Logic and Bit Operations.
Linear Algebra. Circuits The circuits in computers and other input devices have inputs, each of which is either a 0 or 1, the output is also 0s and 1s.
Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Types in programming languages1 What are types, and why do we need them?
Chapter 3 Part II Describing Syntax and Semantics.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Pascal Programming Pointers and Dynamic Variables.
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Parsing using CYK Algorithm Transform grammar into Chomsky Form: 1.remove unproductive symbols 2.remove unreachable symbols 3.remove epsilons (no non-start.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Soundness of Types Ensuring that a type system is not broken.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Arrays Using array as an expression, on the right-hand side Assigning to an array.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Methods. The Structure of a Method Essentially, a method is a block of code with a name. You can execute the code by using the method’s name. You can.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Selection (also known as Branching) Jumail Bin Taliba by
Mutation testing Julius Purvinis IFM-0/2.
User-Defined Functions
Proving Properties of Recursive Functions and Data Structures
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CHAPTER 2 BASIC JAVASCRIPT INSTRUCTIONS
Programming Languages and Paradigms
Recursive Procedures and Scopes
Presentation transcript:

Exercise Determine the output of the following program assuming static and dynamic scoping. Explain the difference, if there is any. object MyClass { val x = 5 def foo(z: Int): Int = { x + z } def bar(y: Int): Int = { val x = 1; val z = 2 foo(y) } def main() { val x = 7 println(foo(bar(3))) } }

if the (free) variables of e have types given by gamma, then e (correctly) type checks and has type T If e 1 type checks in gamma and has type T 1 and... and e n type checks in gamma and has type T n then e type checks in gamma and has type T type judgement relation type rule

Type Checker Implementation Sketch def typeCheck(  : Map[ID, Type], e : ExprTree) : TypeTree = { e match { case Var(id) => {  (id) match case Some(t) => t case None => error(UnknownIdentifier(id,id.pos)) } case If(c,e1,e2) => { val tc = typeCheck( ,c) if (tc != BooleanType) error(IfExpectsBooleanCondition(e.pos)) val t1 = typeCheck( , e1); val t2 = typeCheck( , e2) if (t1 != t2) error(IfBranchesShouldHaveSameType(e.pos)) t1 }... }}

Derivation Using Type Rules x : Int y : Int

Type Rule for Function Application We can treat operators as variables that have function type We can replace many previous rules with application rule: ()

Computing the Environment of a Class object World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Int = { var k = r + 2 m(k, n(k)) } } Type check each function m,n,p in this global environment

Extending the Environment class World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Int = { var k:Int = r + 2 m(k, n(k)) } }

Type Checking Expression in a Body class World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Int = { var k:Int = r + 2 m(k, n(k)) } }

Remember Function Updates

Type Rule for Method Bodies Type Rule for Assignments Type Rules for Block: { var x 1 :T 1... var x n :T n ; s 1 ;... s m ; e }

Blocks with Declarations in the Middle just expression empty declaration is first statement is first

Rule for While Statement

Rule for Method Call

Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z } else { 0 } }  0 = { (z, Boolean), (u, Int), (f, Boolean  Int) }  1 =  0  {(y, Boolean)} Exercise:

Overloading of Operators Not a problem for type checking from leaves to root Int x Int  Int String x String  String

Arrays Using array as an expression, on the right-hand side Assigning to an array

Example with Arrays def next(a : Array[Int], k : Int) : Int = { a[k] = a[a[k]] } Given  = {(a, Array(Int)), (k, Int)}, check  |- a[k] = a[a[k]]: Int

Type Rules (1) variable constant function application plus if assignment while

Type Rules (2) array use array assignment block

Type Rules (3) field use field assignment  c - top-level environment of class C class C { var x: Int; def m(p: Int): Boolean = {…} }  c = { (x, Int), (m, C x Int  Boolean)} method invocation

Does this program type check? class Rectangle { var width: Int var height: Int var xPos: Int var yPos: Int def area(): Int = { if (width > 0 && height > 0) width * height else 0 } def resize(maxSize: Int) { while (area > maxSize) { width = width / 2 height = height / 2 } } }

Meaning of Types Types can be viewed as named entities –explicitly declared classes, traits –their meaning is given by methods they have –constructs such as inheritance establish relationships between classes Types can be viewed as sets of values –Int = {..., -2, -1, 0, 1, 2,... } –Boolean = { false, true } –Int  Int = { f : Int -> Int | f is computable }

Types as Sets Sets so far were disjoint Sets can overlap Boolean true, false String “Richard” “cat” Int  Int Int  Pos Int Pos (1 2) Neg (-1) 16 bit class C class F class D class E F extends D, D extends C C ED F C represents not only declared C, but all possible extensions as well

SUBTYPING

Subtyping Subtyping corresponds to subset Systems with subtyping have non-disjoint sets T 1 <: T 2 means T 1 is a subtype of T 2 –corresponds to T 1  T 2 in sets of values Rule for subtyping: analogous to set reasoning

Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero) Pos <: Int Neg <: Int Pos  Int Neg  Int (y not zero) (x/y well defined)