Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Similar documents
Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CMSC 330: Organization of Programming Languages

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

The Substitution Model

Lecture 15 CIS 341: COMPILERS

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Lecture 16: Static Semantics Overview 1

COS 320. Compiling Techniques

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

The Substitution Model. Nate Foster Spring 2018

CS558 Programming Languages

Formal Semantics of Programming Languages

LECTURE 3. Compiler Phases

Formal Semantics of Programming Languages

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

CSCI-GA Scripting Languages

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Syntax Errors; Static Semantics

Semantic Analysis. Lecture 9. February 7, 2018

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

The role of semantic analysis in a compiler

1 Lexical Considerations

Chapter 3 (part 3) Describing Syntax and Semantics

MIDTERM EXAMINATION - CS130 - Spring 2005

CS536 Spring 2011 FINAL ID: Page 2 of 11

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Introduction to OCaml

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMSC 330: Organization of Programming Languages

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

Problem Score Max Score 1 Syntax directed translation & type

Project Compiler. CS031 TA Help Session November 28, 2011

CS558 Programming Languages. Winter 2013 Lecture 3

CSC 7101: Programming Language Structures 1. Operational Semantics. Winskel, Ch. 2 Slonneger and Kurtz Ch 8.4, 8.5, 8.6. Operational vs.

COP4020 Spring 2011 Midterm Exam

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Lecture #23: Conversion and Type Inference

To figure this out we need a more precise understanding of how ML works

CSE 12 Abstract Syntax Trees

Types and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS 11 Haskell track: lecture 1

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Begin at the beginning

Concepts of Programming Languages

Lexical Considerations

Pierce Ch. 3, 8, 11, 15. Type Systems

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSCE 314 Programming Languages. Type System

Syntax-Directed Translation. Lecture 14

In Our Last Exciting Episode

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE 307: Principles of Programming Languages

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Modules, Structs, Hashes, and Operational Semantics

Introduction to Parsing. Lecture 8

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Introduction to Parsing. Lecture 5

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

Programming Language Concepts, cs2104 Lecture 04 ( )

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

COS 126 General Computer Science Spring Written Exam 1

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Types, Type Inference and Unification

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Compilers Course Lecture 4: Context Free Grammars

Types and Type Inference

5. Semantic Analysis!

CS558 Programming Languages

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CS 242. Fundamentals. Reading: See last slide

CPS 506 Comparative Programming Languages. Syntax Specification

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

CS558 Programming Languages

4.5 Pure Linear Functional Programming

Compiler Construction I

CS 406: Syntax Directed Translation

Semester Review CSC 301

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

CS558 Programming Languages

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Transcription:

Type Inference Systems CS412/CS413 Introduction to Compilers Tim Teitelbaum Type inference systems define types for all legal programs in a language Type inference systems are to type-checking: As regular expressions are to lexical analysis As context-free grammars are to syntax analysis Lecture 14: Static Semantics 21 Feb 07 CS 412/413 Spring 2007 Introduction to Compilers 1 CS 412/413 Spring 2007 Introduction to Compilers 2 Type Judgments The type judgment: E : T means: E is a well-typed construct of type T Type judgments are to type inference systems as sentential forms are to context-free grammars Type checking program P is demonstrating the validity of the type judgment P : T for some type T Sample valid type judgments for program fragments: 2 : int 2 * (3 + 4) : int true : bool (true? 2 : 3) : int Deriving a Type Judgment Consider the judgment: (b? 2 : 3) : int What do we need in order to decide that this is a valid type judgment? b must be a bool ( b: bool) 2 must be an int ( 2: int) 3 must be an int ( 3: int) CS 412/413 Spring 2007 Introduction to Compilers 3 CS 412/413 Spring 2007 Introduction to Compilers 4 Hypothetical Type Judgments The hypothetical type judgment A E : T means In the type context A expression E is a well-typed expression with type T A type context is a set of type bindings id : T (i.e., a type context is a symbol table) Sample valid hypothetical type judgments b: bool b : bool 2 + 2 : int b: bool, x: int (b? 2 : x) : int b: bool, x: int b : bool b: bool, x: int 2 + 2 : int CS 412/413 Spring 2007 Introduction to Compilers 5 Deriving a Judgment To show: b: bool, x: int (b? 2 : x) : int Need to show: b: bool, x: int b : bool b: bool, x: int 2 : int b: bool, x: int x : int CS 412/413 Spring 2007 Introduction to Compilers 6 1

General Rule For any type environment A, expressions E, E 1 and E 2, the judgment A (E? E 1 : E 2 ) : T is valid if: A E : bool A E 1 : T A E 2 : T Inference Rule Schema Premises (a.k.a., antecedant) A E: bool A E 1 : T A E 2 : T A (E? E 1 : E 2 ) : T Conclusion (a.k.a., consequent) Holds for any choice of A, E, E 1, E 2, and T (if-rule) CS 412/413 Spring 2007 Introduction to Compilers 7 CS 412/413 Spring 2007 Introduction to Compilers 8 Why Inference Rules? Inference rules: compact, precise language for specifying static semantics (can specify languages in ~20 pages vs. 100 s of pages of Java Language Specification) Inference rules are to type inference systems as productions are to context-free grammars Type judgments are to type inference systems as nonterminals are to context-free grammars Inference rules correspond directly to recursive AST traversal that implements them Type checking is attempt to prove that type judgments A E : T are derivable Meaning of Inference Rule Inference rule says: given that the antecedent judgments are derivable with a uniform substitution for meta-variables (i.e., A, E 1, E 2 ) then the consequent judgment is derivable with the same uniform substitution for the meta-variables A E 1 : int A E 1 + E 2 : int (+) E 1 E 2 E 1 + E 2 CS 412/413 Spring 2007 Introduction to Compilers 9 CS 412/413 Spring 2007 Introduction to Compilers 10 A1 b : bool Proof Tree A construct is well-typed if there exists a type derivation for a type judgment for the construct Type derivation is a proof tree Type derivations are to type inference systems as derivations are to context-free grammars Example: if A1 = b: bool, x: int, then: A1 2 : int A1 3 : int A1!b : bool A1 2+3 : int A1 x : int A1 (!b? 2+3 : x ) : int CS 412/413 Spring 2007 Introduction to Compilers 11 More about Inference Rules No premises = axiom An inference rule with no premises is analogous to a production with no non-terminals on the right hand side A judgment may be proved in more than one way A E 1 : float A E 2 : float A E 1 + E 2 : float A true : bool A E 1 : float A E 1 + E 2 : float No need to search for rules to apply -- they correspond to nodes in the AST CS 412/413 Spring 2007 Introduction to Compilers 12 2

Type Judgments for Statements Statements that have no value are said to have type void, i.e., judgment S : void means S is a well-typed statement with no result type ML uses unit instead of void While Statements Rule for while statements: A E : bool A S : T A while (E) S : void (while) Why void type? CS 412/413 Spring 2007 Introduction to Compilers 13 CS 412/413 Spring 2007 Introduction to Compilers 14 Assignment (Expression) Statements A, id : T E : T A, id : T id = E : T (variable-assign) A E 3 : T A E 1 : array[t] (array-assign) A E 1 [E 2 ] = E 3 : T Sequence Statements Rule: A sequence of statements is well-typed if the first statement is well-typed, and the remaining are well-typed too: A S 1 : T 1 A (S 2 ; ; S n ) : T n A (S 1 ; S 2 ; ; S n ) : T n (sequence) CS 412/413 Spring 2007 Introduction to Compilers 15 CS 412/413 Spring 2007 Introduction to Compilers 16 Declaration List What about variable declarations? Declarations add entries to the environment (in the symbol table) A id : T [ = E ] : void A, id : T (S 2 ; ; S n ) : void (declaration) A (id : T [ = E ]; S 2 ; ; S n ) : void Function Calls If expression E is a function value, it has a type T 1 T 2 T n T r T i are argument types; T r is return type How to type-check function call E(E 1,,E n )? A E : T 1 T 2 T n T r A E i : T i (i 1..n) A E(E 1,,E n ) : T r (function-call) CS 412/413 Spring 2007 Introduction to Compilers 17 CS 412/413 Spring 2007 Introduction to Compilers 18 3

Function Declarations Add Arguments to Environment! Consider a function declaration of the form T r f (T 1 a 1,, T n a n ) { return E; } Type of function body must match declared return type of function, i.e., E : T r but in what type context? Let A be the context surrounding the function declaration. Then the function declaration T r f (T 1 a 1,, T n a n ) { return E; } is well-formed if A, a 1 : T 1,,, a n : T n E : T r but what about recursion? Need: f: T 1 T 2 T n T r A CS 412/413 Spring 2007 Introduction to Compilers 19 CS 412/413 Spring 2007 Introduction to Compilers 20 Recursive Function Example Factorial: int fact(int x) { if (x==0) return 1; else return x * fact(x - 1); } Prove: A x * fact(x-1) : int Where: A = { fact: int int, x : int } Mutual Recursion Example: int f(int x) { return g(x) + 1; } int g(int x) { return f(x) 1; } Need environment containing at least f: int int, g: int int when checking both f and g Two-pass approach: Scan top level of AST picking up all function signatures and creating an environment binding all global identifiers Type-check each function individually using this global environment CS 412/413 Spring 2007 Introduction to Compilers 21 CS 412/413 Spring 2007 Introduction to Compilers 22 How to Check Return? A E : T (return1) A return E : void A return statement produces no value for its containing context to use Does not return control to containing context Put Return in the Symbol Table Add a special entry { return_fun : T } when we start checking the function f, look up this entry when we hit a return statement. To check T r f (T 1 a 1,, T n a n ) { return S; } in environment A, need to check: A, a 1 : T 1,,, a n : T n, return_f : T r S : T r Suppose we use type void then how to make sure the return type of the current function is T? A E : T return_f : T A A return E : void (return) CS 412/413 Spring 2007 Introduction to Compilers 23 CS 412/413 Spring 2007 Introduction to Compilers 24 4

Static Semantics Summary Type inference system = formal specification of type-checking rules Concise form of static semantics: typing rules expressed as inference rules Expression and statements are well-formed (or well-typed) if a typing derivation (proof tree) can be constructed using the inference rules CS 412/413 Spring 2007 Introduction to Compilers 25 5