Type Checking  Legality checks  Operator determination  Overload resolution.

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Modern Programming Languages, 2nd ed.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Chapter 6 - Data Types Programming Languages:
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Chapter 7:: Data Types Programming Language Pragmatics
COEN Expressions and Assignment
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Type Checking.
Compiler Construction
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Mark Hennessy CS351 Dept Computer Science NUI Maynooth 1 Types CS351 – Programming Paradigms.
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.
Honors Compilers An Introduction to Algol-68S Jan 24 th 2002.
CSE 452: Programming Languages Expressions and Control Flow.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Names Variables Type Checking Strong Typing Type Compatibility 1.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Chapter 9: Functional Programming in a Typed Language.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Arithmetic Expressions
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Compiler Design – CSE 504 Type Checking
Functional Programming
Types CSCE 314 Spring 2016.
Chapter 7: Expressions and Assignment Statements
Semantic Analysis Type Checking
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML: a quasi-functional language with strong typing
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
Chapter 7: Expressions and Assignment Statements
FP Foundations, Scheme In Text: Chapter 14.
Type Systems CSE 340 – Principles of Programming Languages Fall 2016
CS 363 – Chapter 7 Chapter 7 – type systems Types that are supported
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Compiler Construction
Java Programming Language
Compiler Construction
Presentation transcript:

Type Checking  Legality checks  Operator determination  Overload resolution

Type as a synthesized attribute Expr ::= Expr + Term if Etype (Expr 2 ) = Etype (Term) then Etype (Expr 1 ) := Etype (Expr 2 ); else Etype (Expr1) = Any_Type; Error (“incompatible types for +”, Expr 1 ); end if;  Sufficient for Pascal and C

Rules for operations on composite types Indexed_Component ::= Prefix ( Expr ) A_Typ := Etype (Prefix); Etype (Indexed_Component) := Any_Type; - - by default if not Is_Array_Type (A_Typ) then Error (“expect array type in indexed component”, Prefix); elsif Etype (Expr) /= Index_Type (A_Typ) then Error (“wrong index type”, Expr); else Etype (Indexed_Component) := Component_Type (A_Typ); end if;

Type expressions Built from: Primitive types: boolean, float, integer Type constructors: array, record, access, function Type_Expr ::= Prim_Type Type_Expr ::= Type_Name Type_Expr ::= array (Type_Expr) Type_Expr Pascal, Ada : index type is part of type expression Early Pascal : index bounds are part of type expression (bad idea) Type_Expr ::= record (…) Type_Expr ::= access Type_Expr Type checking is a set of rules to compute the type expressions of all expressions in the program

Product types and functions Type_Expr := Type_Expr * Type_Expr Intuitively, a pair of types Type_Expr := Type_Expr 1 -> Type_Expr 2 A function that takes an argument of type Type_Expr 1 and returns a value of type Type_Expr 2 function Distance (C1, C2 : Character) return Integer; Distance: character * character -> integer real merge (int[] a, int[]b); merge: array ( int ) * array ( int ) -> real

Type checking and type equivalence Type correctness can be stated in terms of equivalence of type expressions: int [ ] arr1; // arr2 : array ( int ) int [ ] arr2; // arr2 : array ( int ) … Arr1 = arr2; // ok in Java: type expressions are equivalent Usually the rule in languages where a declaration can contain an arbitrary type expression

Type checking and name equivalence Arr1 : array (1..10) of integer; Arr2 : array (1..10) of integer; … Arr1 := Arr2; // illegal in Ada: different anonymous types. -- same as: type anon1 is array (1..10) of integer; Arr1 : anon1; -- Arr1 : anon1 type anon2 is array (1..10) of integer; Arr2 : anon2: -- Arr2 : anon2 -- Arr1 and Arr2 are not equivalent. Language does not allow arbitrary (anonymous) type expressions in a declaration

Type expressions with cycles type t1 = record c: integer; p: pointer (t1); end record; type t2 = record c: integer; p: pointer (t2); end record; t1 and t2 are equivalent in Algol68 C approach: name of type is part of type expression, types are not equivalent: struct cell { int c; struct cell *next; /* struct cell is in type expression */ };

Type checking and coercions If language allows coercions, type depends on context, cannot be purely synthesized. For C++ boolean operators (Stroustrup C.6.3) If either operand is of type long double, the other is converted to long double Otherwise, if either operand is double, the other is converted to double Otherwise, if either is float, the other is converted to float Otherwise, integral promotions are performed on both: char, (un) signed_char, (unsigned) short int -> int bit-field -> int bool -> int Further complication: user-defined conversions.

Overload resolution If expression is overloaded, collect set of possible meanings sm. If context is overloaded, collect set of possible context types sc. Expression is legal in context if intersection of sc and sm is a singleton: | sc ∩ sm | = 1 C++: if multiple interpretations, select the one with smallest number of coercions Ada: if multiple interpretations, select predefined numeric operator over user-defined one

Overloaded context: procedure call procedure P (x : integer; y : float); procedure P (x : float; y : float); procedure P (x : boolean; z : integer); function F (x : integer) return boolean; function F (x : float) return integer; function F (x : integer) return float; function F (x : float) return boolean; … P (f (3.14), f (1)); -- P 1 (f 2 (3.14), f 3 (1));

Two-pass type resolution Bottom-up (analyze) : synthesize candidate types Top-down (resolve): propagate unique context type procedure analyze_indexed_component (N : Node_Id) is begin analyze prefix, collect set of interpretations Pre analyze index, collect set of interpretations Ind forall t in Pre loop if there is a compatible ix in Ind then add component_Type (t) to interpretations of N else remove t from Pre; end if; end loop; end;

Two-pass type resolution (2) procedure resolve_indexed_component (N : Node_Id; Typ : Entity_Id) is begin find unique interpretation of prefix whose component type is Typ resolve index using this interpretation end; General scheme: bottom-up: analyze descendants, synthesize local attribute top-down : disambiguate construct, propagate to descendants

Type variables and polymorphism In a language with list primitives (LISP, ML) what is the type expression that describes CAR or hd? Informally, given a list of components of any kind, CAR yields a value of that kind: Car : list_of_whatever -> whatever A type variable is universally quantified: the expression is valid for any instantiation of the variable. In ML notation: Head (hd) α list -> α Tail (tl) α list -> α list Constructor (::) : α * α list -> α list

Polymorphic functions fun len lis = if lis = null then 0 else 1 + len (tl lis) Len : α list -> int fun map (f lis) = if L = null then null else f (hd lis) :: map (f (tl lis)) Map: (α ->β) * α list -> β list Map applies a function to each element of a list. The result type of the function is not necessarily equal to the element type of the list.

The limits of type inference Overloading must be handled specially Self-application is not typable: 1 + g (g) g is a function: g: α -> β g applied to itself yields an integer: β = int α = α -> β : circular definition (not unifiable)