Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Intermediate Code Generation
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
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.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Compiler Construction
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Cse322, Programming Languages and Compilers 1 6/26/2015 Lecture #6, April 17, 2007 Project #1 description, Changes to Program Types, IR1 intermediate language,
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Third Edition
Cse322, Programming Languages and Compilers 1 7/13/2015 Lecture #2, April 5, 2007 Overview of backend issues (storage locations, registers, aliasing),
Cse321, Programming Languages and Compilers 1 7/15/2015 Lecture #14, Feb. 28, 2007 Writing type-checkers in ML Role of type rules Representing types Representing.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
Chapter 3: Data Types and Operators JavaScript - Introductory.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
CSE 131 Computer Science 1 Module 1: (basics of Java)
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.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Arithmetic Expressions
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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 =
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Programming Principles Operators and Expressions.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Information and Computer Sciences University of Hawaii, Manoa
Relational Operator and Operations
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
Introduction to Computer Science / Procedural – 67130
Building Java Programs Chapter 2
OPERATORS (2) CSC 111.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Chapter 2: Java Fundamentals
CSE 341 Section 5 Winter 2018.
Type & Typeclass Syntax in function
CSE-321 Programming Languages Introduction to Functional Programming
Compiler Construction
Names of variables, functions, classes
DATA TYPES AND OPERATIONS
Compiler Construction
Problem 1 Given n, calculate 2n
Presentation transcript:

Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy Setting up an ML typechecker for mini-Java Subtyping Rules for Java Declarations as functions over environments

Cse321, Programming Languages and Compilers 2 6/23/2015 HW 10, Assigned last week, due Wed, –excellent warmup for project 3 Project 3 assigned today –See webpage for details

Cse321, Programming Languages and Compilers 3 6/23/2015 Types for mini-Java Mini-Java has two kinds of programs –Expressions –Statements We need a judgment for each kind –Expressions Th, C, TE |- exp : type –Statements R, Th, C, TE |= stmt Subtyping judgment –C |~ t 1 ≤ t 2 Equality judgment –t 1 = t 2

Cse321, Programming Languages and Compilers 4 6/23/2015 Environments TE - Type environments –TE(x) = t –The variable x has type t in the environment TE R - Return mode –R = { void, explicit t, none} –void means the method has type return type void and no expression is expected after a return statement –Explicit t means the method has return type t and the argument of a return must present be a subtype of t C - Class Hierarchy –The set of classes is arranged into a hierarchy –Class colorpoint extends point {... } –C defines x »There is a class definition for x Th – the type of the self object “this”

Cse321, Programming Languages and Compilers 5 6/23/2015 Class Hierarchy object numeric point int double boolean colorpoint void C |~ int ≤ object C |~ colorpoint ≤ point C |~ boolen ≤ object C defines point

Cse321, Programming Languages and Compilers 6 6/23/2015 Type Checkers in ML To build type-checkers in ML we need –A datatype to represent types –A datatype (or datatypes) to represent programs (whose type we are checking) »Expressions »Statements »Declarations –A means for computing equality and subtyping over types –A means for expressing type errors

Cse321, Programming Languages and Compilers 7 6/23/2015 Representing Types type Id = string; (** Representing types for mini-Java **) datatype Basic = Bool | Int | Real; datatype Type = BasicType of Basic | ArrayType of Basic | ObjType of Id | VoidType;

Cse321, Programming Languages and Compilers 8 6/23/2015 Representing Programs 1 - Auxillaries (* A slot of type (`x TC) is an option type. The *) (* parser places NONE there and the type-checker *) (* fills it in with (SOME x) when "x" is known *) type 'x TC = 'x option; (******** Representing Programs *******) datatype Op (* infix operators *) = ADD | SUB | MUL | DIV (* Arithmetic *) | AND | OR (* logical *) | EQ | NE | LT | LE | GT | GE (* relational *) datatype Constant (* Literal constants *) = Cint of int | Creal of string | Cbool of bool

Cse321, Programming Languages and Compilers 9 6/23/2015 Representing Programs 2 - expressions datatype Exp = Literal of Constant (* 5, 6.3, true *) | Binop of Op * Exp * Exp (* x + 3 *) | Relop of Op * Exp * Exp (* x < 7.7 *) | Not of Exp (* ! x *) | ArrayElm of Exp * Exp * (Basic TC) (* x[3] *) | ArrayLen of Exp (* x.length() *) | Call of Exp * Id *(Id TC)* Exp list (* x.f(1,z) *) | NewArray of Basic * Exp (* new int[3] *) | NewObject of Id (* new point() *) (* Coerce is used only in type checking *) | Coerce of Exp

Cse321, Programming Languages and Compilers 10 6/23/2015 Representing Programs 3 - statements datatype Stmt = Block of Stmt list (* {x:5; print(2)} *) | Assign of Exp option * Id * Exp option * Exp (* p.x[2]=5 p.x=5 x=5 *) | CallStmt of Exp * Id * (Id TC)* Exp list (* x.f(1,z) *) | If of Exp * Stmt * Stmt (* if (p<2) x=5 else x=6 *) | While of Exp * Stmt (* while (p) s *) | PrintE of Exp (* System.out.println(x) *) | PrintT of string (* System.out.println("zbc") *) | Return of Exp option; (* return (x+3) *)

Cse321, Programming Languages and Compilers 11 6/23/2015 Representing programs 4 - declarations datatype VarDecl = VarDecl of Type * Id * Exp option; datatype Formal = Formal of Type * Id; datatype MetDecl = MetDecl of Type * Id * Formal list * VarDecl list * Stmt list; datatype ClassDec = ClassDec of Id * Id * VarDecl list * MetDecl list; datatype Program = Program of ClassDec list;

Cse321, Programming Languages and Compilers 12 6/23/2015 Type Equality In mini-Java we need both type equality and sub-typing Type equality for testing that the primitive operators are applied to appropriate expressions of the correct type. Type equality is over basic types. Sub-typing for method calls and assignments, testing that actual arguments are subtypes of the formal arguments. Subtyping is over all types.

Cse321, Programming Languages and Compilers 13 6/23/2015 Type Equality over basic types fun typeeq (x,y) = case (x,y) of (Real,Real) => true | (Int,Int) => true | (Bool,Bool) => true | (_,_) => false Pair wise pattern match over the basic types

Cse321, Programming Languages and Compilers 14 6/23/2015 Type equality fun typeeq (x,y) = case (x,y) of (BasicType x,BasicType y) => basiceq(x,y) | (ArrayType x,ArrayType y) => basiceq(x,y) | (ObjType x,ObjType y) => x=y | (VoidType,VoidType) => true | (_,_) => false

Cse321, Programming Languages and Compilers 15 6/23/2015 Subtyping fun subtype classH (x,y) = case (x,y) of (x,ObjType “object”) => true | (BasicType Int,ObjType “numeric”) => true | (BasicType Real,ObjType “numeric”) => true | (ObjType x,ObjType y) => useTree classH (x,y) | (_,_) => typeeq(x,y); Fun useTree class (x,y) =...

Cse321, Programming Languages and Compilers 16 6/23/2015 Expressing Errors In ML we are lucky to have a rich exception mechanism. Design a set of exceptions to report errors. You might want to define helper functions –fun showt t =... –fun report exp computed expected =...

Cse321, Programming Languages and Compilers 17 6/23/2015 Type rules for mini-java One rule for each piece of syntax Use the different judgments accordingly Th, C, TE |- exp : type Th, R, C, TE |= stmt C |~ t 1 ≤ t 2 t 1 = t 2 C defines x Th, C, TE |- Y.f : (s 1, …,s n ) → t Th, C, TE |- Y.x : t [ ] Op : (t 1,t 2 ) -> t 3 t is basic x  TE and TE (x) = t Expression has type Statement is well formed t 1 is a subtype of t 2 Class Y has method f with domain and range Class Y has variable x with type

Cse321, Programming Languages and Compilers 18 6/23/2015 Simple Expressions Th, C, TE |- n : int n is integer constant like 5 Th, C, TE |- r : double r is real constant like 5.3 Th, C, TE |- this : Th Th, C, TE |- true : boolean Th, C, TE |- false : boolean

Cse321, Programming Languages and Compilers 19 6/23/2015 Variables TE (x) = t R, Th, C, TE |= x : t Th, C, TE |- e1 : Y C defines Y Th, C, TE |- Y.x : t R, Th, C, TE |= e1. x : t x  TE C defines Th Th, C, TE |- Th.x : t R, Th, C, TE |= x : t

Cse321, Programming Languages and Compilers 20 6/23/2015 Operators Th, C, TE |- e 1 : t 1 Th, C, TE |- e 2 : t 2 Op : (t 1,t 2 ) -> t Th, C, TE |- e 1 e 2 : t 3 Th, C, TE |- e 1 : boolean Th, C, TE |- ! e 1 : boolean

Cse321, Programming Languages and Compilers 21 6/23/2015 Arrays Th, C, TE |- e 1 : t [ ] Th, C, TE |- e 2 : int Th, C, TE |- e 1 [ e 2 ] : t Th, C, TE |- e : t [ ] Th, C, TE |- e.length() : int

Cse321, Programming Languages and Compilers 22 6/23/2015 Method call Expressions Note that f(x) is really shorthand for this.f(x) C defines Th Th, C, TE |- Th.f : (s 1, …,s n ) → t Th, C, TE |- e i : t i C |~ t i ≤ s i Th, C, TE |- f(e 1, …,e n ) : t Th, C, TE |- exp : X C defines X Th, C, TE |- X.f : (s 1, …,s n ) → t Th, C, TE |- e i : t i C |~ t i ≤ s i Th, C, TE |- exp. f(e 1, …,e n ) : t

Cse321, Programming Languages and Compilers 23 6/23/2015 New arrays and objects Th, C, TE |- e : t t is basic Th, C, TE |- new t [e] : t [ ] C defines x Th, C, TE |- new x ( ) : x

Cse321, Programming Languages and Compilers 24 6/23/2015 Judgments over Statements We use a different notation for typing statements The judgment for statements does not have a return type. It also uses a different “turnstile” symbol. R, Th, C, TE |= e 1. x [ e 2 ] = e 3

Cse321, Programming Languages and Compilers 25 6/23/2015 Static Implicit Assignment TE (x) = t [ ] TE |- e 2 : int Th, C, TE |- e 3 : t R, Th, C, TE |= x [ e 2 ] = e 3 TE (x) = t Th, C, TE |- e2 : t R, Th, C, TE |= x = e2 Static, because the variable is in the type environment. This means the variable is a either a parameter or local variable to the current method.

Cse321, Programming Languages and Compilers 26 6/23/2015 Explicit Hierarchical Assignment Statements Th, C, TE |- e 1 : Y C defines Y Th, C, TE |- Y.x : t [ ] Th, C, TE |- e 2 : int Th, C, TE |- e 3 : t R, Th, C, TE |= e 1. x [ e 2 ] = e 3 Th, C, TE |- e1 : Y C defines Y Th, C, TE |- Y.x : t Th, C, TE |- e2 : t R, Th, C, TE |= e1. x = e2 Explicit because the object for containg the variable is explicit in the code

Cse321, Programming Languages and Compilers 27 6/23/2015 Hierarchical Implicit Assignment x  TE C defines Th Th, C, TE |- Th.x : t [ ] Th, C, TE |- e 2 : int Th, C, TE |- e 3 : t R, Th, C, TE |= x [ e 2 ] = e 3 x  TE C defines Th Th, C, TE |- Th.x : t Th, C, TE |- e2 : t R, Th, C, TE |= x = e2 Implicit because the object containg the variable is not in the code. These rules hold only if the variable is not in the type environment

Cse321, Programming Languages and Compilers 28 6/23/2015 Note 3 forms of assignment TE (x) = t Th, C, TE |- e2 : t R, Th, C, TE |= x = e2 x  TE C defines Th Th, C, TE |- Th.x : t Th, C, TE |- e2 : t R, Th, C, TE |= x = e2 Th, C, TE |- e1 : Y C defines Y Th, C, TE |- Y.x : t Th, C, TE |- e2 : t R, Th, C, TE |= e1. x = e2 static Implicit hierarchical Explicit hierarchical

Cse321, Programming Languages and Compilers 29 6/23/2015 If statement R, Th, C, TE |= s 1 Th, C, TE |- e 1 : boolean R, Th, C, TE |= if ( e 1 ) s 1 R, Th, C, TE |= s 1 R, Th, C, TE |= s 2 Th, C, TE |- e 1 : boolean R, Th, C, TE |= if ( e 1 ) s 1 else S 2

Cse321, Programming Languages and Compilers 30 6/23/2015 While and Print statement R, Th, C, TE |= s 1 Th, C, TE |- e 1 : boolean R, Th, C, TE |= while ( e 1 ) s 1 t is basic Th, C, TE |- x : t R, Th, C, TE |= System.out.println ( x ) R, Th, C, TE |= System.out.println ( “literal string” )

Cse321, Programming Languages and Compilers 31 6/23/2015 Return statement Void, Th, C, TE |= return C |~ t ≤ s Th, C, TE |- e : s Explicit t, Th, C, TE |= return e

Cse321, Programming Languages and Compilers 32 6/23/2015 Method call statements Th, C, TE |- e i : t i C defines Th Th, C, TE |- Th.f : (s 1, …,s n ) → Void C |~ t i ≤ s i R, Th, C, TE |= f(e 1, …,e n ) Th, C, TE |- e i : t i C defines X Th, C, TE |- X.f : (s 1, …,s n ) → Void C |~ t i ≤ s i Th, C, TE |- exp : X R, C, TE |= exp. f(e 1, …,e n )

Cse321, Programming Languages and Compilers 33 6/23/2015 Sequences of statements R, Th, C, TE |= s i R, Th, C, TE |= { s 1, …,s n }

Cse321, Programming Languages and Compilers 34 6/23/2015 Declarations Declarations are type-environment to type- environment functions. R, Th, C, TE |= decl → R, Th, C, TE There are four kinds of declarations –Var-declarations –Method-declarations »Are mutually recursive –Class-declarations –Main method declaration »There is only one of these in any program

Cse321, Programming Languages and Compilers 35 6/23/2015 One Var declaration R, Th, C, TE |= t x ; → R, Th, C, TE+(x:t) Th, C, TE |- e : t R, Th, C, TE |= t x = e ; → R, Th, C, TE+(x:t)

Cse321, Programming Languages and Compilers 36 6/23/2015 Many Var declarations R, Th, C, TE |= d1 → R 1, Th 1, C 1, TE 1 R 1, Th 1, C 1, TE 1 |= d2 → R 2, Th 2, C 2, TE 2 R 2, Th 2, C 2, TE 2 |= d3 → R 3, Th 3, C 3, TE R, Th, C, TE |= {d1; d2; d3; …} → R 3, Th 3, C 3, TE 3

Cse321, Programming Languages and Compilers 37 6/23/2015 Mutually recursive Method Declaration TE A = TE + (f 1,method Th (t 1 1, …,t n 1 ) →t 1 ) + … + (f k,method Th (t 1 k, …,t n k ) →t k ) TE A |= (t 1 k x 1 k, … t n k x n k ) → TE B k TE B k |= (v 1 ; … ; v i ) → TE C k t k, Th, C, TE C k |= {s 1 ; … ;s j } R, Th, C, TE |= { public t 1 f 1 (t 1 x 1, … t n x n ) { v 1 ; … ; v i ; s 1 ; … ;s j } ; … ; public t k f k (t 1 x 1, … t n x n ) { v 1 ; … ; v i ; s 1 ; … ;s j } } → R, Th, C, TE A

Cse321, Programming Languages and Compilers 38 6/23/2015 Class Declarations Class declarations extend the Class heirarchy R, Th, C+(y/x), TE |= { v 1 ; … ; v i ; m 1 ; … ;m j } → R 2, Th 2, C 2 TE R, Th, C, TE |= Class x extends y { v 1 ; … ; v i ; m 1 ; … ;m j } → R 2, Th 2, C 2 TE 2

Cse321, Programming Languages and Compilers 39 6/23/2015 New Boiler plate for Parser structure ProgramTypes = struct (* Put type declarations here that you *) (* want to appear in both the parser *) (* and lexer. You can open this structure *) (* else where inside your application as well (* I’ve put the datatypes for the syntax here to start *) end; group is ProgramTypes.sml Mini.lex Mini.grm Driver.sml TypeCheck.sml $/basis.cm $/smlnj-lib.cm $/ml-yacc-lib.cm Mini.cm ProgramTypes.sml