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

Similar documents
Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

Programming Languages and Compilers (CS 421)

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

n Parsing function takes a lexing function n Returns semantic attribute of 10/27/16 2 n Contains arbitrary Ocaml code n May be omitted 10/27/16 4

n <exp>::= 0 1 b<exp> <exp>a n <exp>m<exp> 11/7/ /7/17 4 n Read tokens left to right (L) n Create a rightmost derivation (R)

CMSC 330: Organization of Programming Languages

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

Programming Languages and Compilers (CS 421)

n Takes abstract syntax trees as input n In simple cases could be just strings n One procedure for each syntactic category

Example. Programming Languages and Compilers (CS 421) Disambiguating a Grammar. Disambiguating a Grammar. Steps to Grammar Disambiguation

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Programming Languages and Compilers (CS 421)

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Programming Languages and Compilers (CS 421)

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Lecture 13 CIS 341: COMPILERS

Programming Languages and Compilers (CS 421)

CMSC 330: Organization of Programming Languages

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421)

CSCI-GA Scripting Languages

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Introduction to Lexical Analysis

The Substitution Model

Introduction to OCaml

Syntax. A. Bellaachia Page: 1

The Substitution Model. Nate Foster Spring 2018

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Syntax. In Text: Chapter 3

Related Course Objec6ves

Programming Languages Third Edition

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

Recap: Functions as first-class values

CSE 130, Fall 2006: Final Examination

CS153: Compilers Lecture 14: Type Checking

Metaprogramming assignment 3

Tracing Ambiguity in GADT Type Inference

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

Programming Languages

CST-402(T): Language Processors

CMSC 330: Organization of Programming Languages

CSE 130, Fall 2005: Final Examination

Forward Recursion. Programming Languages and Compilers (CS 421) Mapping Recursion. Forward Recursion: Examples. Folding Recursion.

COP4020 Spring 2011 Midterm Exam

Theory and Compiling COMP360

CMSC 330: Organization of Programming Languages. Lambda Calculus

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

CSE-321: Assignment 8 (100 points)

Lecture #23: Conversion and Type Inference

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CMSC 330: Organization of Programming Languages

Static Checking and Type Systems

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

CS558 Programming Languages

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

ML 4 Unification Algorithm

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

CMSC 330: Organization of Programming Languages. Lambda Calculus

CSE 582 Autumn 2002 Exam 11/26/02

Type Declarations. Γ <num> : num [... <id> τ... ] <id> : τ Γ true : bool Γ false : bool Γ e 1 : num Γ e 2 : num Γ. {+ e 1 e 2 } : num

Lexical Analysis. Introduction

If a program is well-typed, then a type can be inferred. For example, consider the program

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

7. Introduction to Denotational Semantics. Oscar Nierstrasz

A Simple Syntax-Directed Translator

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Compiler Design Overview. Compiler Design 1

LECTURE 3. Compiler Phases

CMSC 330: Organization of Programming Languages

CPS 506 Comparative Programming Languages. Syntax Specification

Lecture 15 CIS 341: COMPILERS

Type Checking and Type Inference

Com S 541. Programming Languages I

CSCE 314 Programming Languages

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

Introduction. Compilers and Interpreters

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Topic 3: Syntax Analysis I

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

Introduction to Lexical Analysis

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Introduction to Scheme

Chapter 3. Describing Syntax and Semantics ISBN

CS 421, Fall Sample Final Questions & Solutions

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Programming Language Syntax and Analysis

1. For every evaluation of a variable var, the variable is bound.

1 Lexical Considerations

Transcription:

Two Problems Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https://courses.engr.illinois.edu/cs421/fa2017/cs421a Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa L Gunter 10/17/2017 1 Type checking Question: Does exp. e have type in env? Answer: Yes / No Method: Type derivation Typability Question Does exp. e have some type in env.? If so, what is it? Answer: Type / error Method: Type inference 10/17/2017 2 Type Inference - Outline Type Inference - Begin by assigning a type variable as the type of the whole expression Decompose the expression into component expressions Use typing rules to generate constraints on components and whole Recursively find substitution that solves typing judgment of first subcomponent Apply substitution to next subcomponent and find substitution solving it; compose with first, etc. Apply composition of all substitutions to original type variable to get answer 3 What type can we give to (fun x -> fun f -> f (f x)) Start with a type variable and then look at the way the term is constructed 10/17/2017 4 Type Inference - First approximate: Give type to full expr. { } - (fun x -> fun f -> f (f x)) : Second approximate: use fun rule Type Inference - Third approximate: use fun rule {f : ; x : } - f (f x) : { } - (fun x -> fun f -> f(f x)) : Remember constraint ( ) 10/17/2017 5 10/17/2017 6 1

Type Inference - Fourth approximate: use app rule {f: ; x: } - f : {f: ; x: } - f x : 10/17/2017 7 Type Inference - Fifth approximate: use var rule, get constraint, Solve with same Apply to next sub-proof {f: ; x: } - f : {f: ; x: } - f x : 10/17/2017 8 Type Inference - Current subst: { } Type Inference - Current subst: { } Use App Rule 10/17/2017 9 {f: ; x: } - f:z {f: ; x: } - x:z 10/17/2017 10 Type Inference - Current subst: { } Var rule: Solve z Unification {f: ; x: } - f:z {f: ; x: } - x:z 10/17/2017 11 Type Inference - Current subst: {z, } o { } Var rule: Solve z Unification {f: ; x: } - f:z {f: ; x: } - x:z 10/17/2017 12 2

Type Inference - Current subst: {z,, } Apply to next sub-proof (done) {f: ; x: } - x:z 10/17/2017 13 Type Inference - Current subst: {z,, } Apply to next sub-proof (done) {f: ; x: } - x: 10/17/2017 14 Type Inference - Current subst: {z,, } Var rule: {f: ; x: } - x: 10/17/2017 15 Type Inference - Current subst: { }o{z,, } Solves subproof; return one layer {f: ; x: } - x: 10/17/2017 16 Type Inference - Current subst: {, z,, } Solves this subproof; return one layer 10/17/2017 17 Type Inference - Current subst: {, z,, } Need to satisfy constraint ( ), given subst, becomes: (( ) ) 10/17/2017 18 3

Type Inference - Type Inference - Current subst: { (( ) ),, z,, } Solves subproof; return one layer 10/17/2017 19 Current subst: { (( ) ),, z,, } Need to satisfy constraint ( ) given subst: ( (( ) )) ( ); 10/17/2017 20 Type Inference - Current subst: { ( (( ) )), (( ) ),, z,, } Solves subproof; return on layer Type Inference - Current subst: { ( (( ) )), (( ) ),, z,, } Done: ( (( ) )) 10/17/2017 21 10/17/2017 22 Type Inference Algorithm Let infer (, e, ) = s is a typing environment (giving polymorphic types to expression variables) e is an expression is a type (with type variables), s is a substitution of types for type variables Idea: s is the constraints on type variables necessary for - e : Should have s( ) - e : s( ) valid Type Inference Algorithm infer (, exp, ) = Case exp of Var v --> return Unify{ freshinstance( (v))} Replace all quantified type vars by fresh ones Const c --> return Unify{ freshinstance } where - c : by the constant rules fun x -> e --> Let, be fresh variables Let s = infer ({x: } +, e, ) Return Unify({s( ) s( )}) o s 10/17/2017 23 10/17/2017 24 4

Type Inference Algorithm (cont) Case exp of App (e 1 e 2 ) --> Let be a fresh variable Let s 1 = infer(, e 1, ) Let s 2 = infer(s( ), e 2, s( )) Return s 2 o s 1 Type Inference Algorithm (cont) Case exp of If e 1 then e 2 else e 3 --> Let s 1 = infer(, e 1, bool) Let s 2 = infer(s, e 2, s 1 ( )) Let s 3 = infer(s 2 o s 1 ( ),e 2,s 2 o s( )) Return s 3 o s 2 o s 1 10/17/2017 25 10/17/2017 26 Type Inference Algorithm (cont) Case exp of let x = e 1 in e 2 --> Let be a fresh variable Let s 1 = infer(, e 1, ) Let s 2 = infer({x:gen(s 1 ( ), s 1 ( ))} + s 1 ( ), e 2, s 1 ( )) Type Inference Algorithm (cont) Case exp of let rec x = e 1 in e 2 --> Let be a fresh variable Let s 1 = infer({x: } +, e 1, ) Let s 2 = infer({x:gen(s 1 ( ),s 1 ( ))} + s 1 ( )}, e 2, s 1 ( )) Return s 2 o s 1 Return s 2 o s 1 10/17/2017 28 10/17/2017 27 Type Inference Algorithm (cont) To infer a type, introduce type_of Let be a fresh variable type_of (, e) = Let s = infer (, e, ) Return s ( ) Need an algorithm for Unif Background for Unification Terms made from constructors and variables (for the simple first order case) Constructors may be applied to arguments (other terms) to make new terms Variables and constructors with no arguments are base cases Constructors applied to different number of arguments (arity) considered different Substitution of terms for variables 10/17/2017 29 10/17/2017 30 5

Simple Implementation Background Unification Problem type term = Variable of string Const of (string * term list) let rec subst var_name residue term = match term with Variable name -> if var_name = name then residue else term Const (c, tys) -> Const (c, List.map (subst var_name residue) tys);; Given a set of pairs of terms ( equations ) {(s 1, t 1 ), (s 2, t 2 ),, (s n, t n )} (the unification problem) does there exist a substitution s (the unification solution) of terms for variables such that s(s i ) = s(t i ), for all i = 1,, n? 10/17/2017 31 10/17/2017 32 Uses for Unification Type Inference and type checking Pattern matching as in OCaml Can use a simplified version of algorithm Logic Programming - Prolog Simple parsing Unification Algorithm Let S = {(s 1 = t 1 ), (s 2 = t 2 ),, (s n = t n )} be a unification problem. Case S = { }: Unif(S) = Identity function (i.e., no substitution) Case S = {(s, t)} S : Four main steps 10/17/2017 33 10/17/2017 34 Unification Algorithm Delete: if s = t (they are the same term) then Unif(S) = Unif(S ) Decompose: if s = f(q 1,, q m ) and t =f(r 1,, r m ) (same f, same m!), then Unif(S) = Unif({(q 1, r 1 ),, (q m, r m )} S ) Orient: if t = x is a variable, and s is not a variable, Unif(S) = Unif ({(x = s)} S ) Unification Algorithm Eliminate: if s = x is a variable, and x does not occur in t (the occurs check), then Let = {x t} Let = Unif( (S )) Unif(S) = {x (t)} o Note: {x a} o {y b} = {y ({x a}(b))} o {x a} if y not in a 10/17/2017 35 10/17/2017 36 6

Tricks for Efficient Unification Don t return substitution, rather do it incrementally Make substitution be constant time Requires implementation of terms to use mutable structures (or possibly lazy structures) We won t discuss these Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} =? 10/17/2017 37 10/17/2017 38 S = {(f(x) = f(g(f(z),y))), (g(y,y) = x)} is nonempty Pick a pair: (g(y,y) = x) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} =? Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} =? 10/17/2017 39 10/17/2017 40 Pick a pair: (g(y,y)) = x) Orient: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} by Orient Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} =? 10/17/2017 41 10/17/2017 42 7

{(f(x) = f(g(f(z),y))), (x = g(y,y))} is nonempty Pick a pair: (x = g(y,y)) Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} =? Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} =? 10/17/2017 43 10/17/2017 44 Pick a pair: (x = g(y,y)) Eliminate x with substitution {x g(y,y)} Check: x not in g(y,y). Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} =? Pick a pair: (x = g(y,y)) Eliminate x with substitution {x g(y,y)} Unify {(f(x) = f(g(f(z),y))), (x = g(y,y))} = Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x g(y,y)} 10/17/2017 45 10/17/2017 46 {(f(g(y,y)) = f(g(f(z),y)))} is non-empty Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x g(y,y)} =? Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x g(y,y)} =? 10/17/2017 47 10/17/2017 48 8

Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose:(f(g(y,y)) = f(g(f(z),y))) becomes {(g(y,y) = g(f(z),y))} Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x g(y,y)} =? Unify {(f(g(y,y)) = f(g(f(z),y)))} o {x g(y,y)} = Unify {(g(y,y) = g(f(z),y))} o {x g(y,y)} 10/17/2017 49 10/17/2017 50 {(g(y,y) = g(f(z),y))} is non-empty Pick a pair: (g(y,y) = g(f(z),y)) Unify {(g(y,y) = g(f(z),y))} o {x g(y,y)} =? Unify {(g(y,y) = g(f(z),y))} o {x g(y,y)} =? 10/17/2017 51 10/17/2017 52 Pick a pair: (f(g(y,y)) = f(g(f(z),y))) Decompose: (g(y,y)) = g(f(z),y)) becomes {(y = f(z)); (y = y)} Unify {(y = f(z)); (y = y)} o {x g(y,y)} =? Unify {(g(y,y) = g(f(z),y))} o {x g(y,y)} = Unify {(y = f(z)); (y = y)} o {x g(y,y)} 10/17/2017 53 10/17/2017 54 9

{(y = f(z)); (y = y)} o {x g(y,y) is nonempty Unify {(y = f(z)); (y = y)} o {x g(y,y)} =? Pick a pair: (y = f(z)) Unify {(y = f(z)); (y = y)} o {x g(y,y)} =? 10/17/2017 55 10/17/2017 56 Pick a pair: (y = f(z)) Eliminate y with {y f(z)} Unify {(y = f(z)); (y = y)} o {x g(y,y)} = Unify {(f(z) = f(z))} o {y f(z)} o {x g(y,y)}= Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} =? 10/17/2017 57 10/17/2017 58 {(f(z) = f(z))} is non-empty Pick a pair: (f(z) = f(z)) Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} =? Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} =? 10/17/2017 59 10/17/2017 60 10

Pick a pair: (f(z) = f(z)) Delete Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} = Unify {} o {y f(z); x g(f(z), f(z))} Unify {} o {y f(z); x g(f(z), f(z))} =? 10/17/2017 61 10/17/2017 62 {} is empty Unify {} = identity function Unify {} o {y f(z); x g(f(z), f(z))} = {y f(z); x g(f(z), f(z))} Unify {(f(x) = f(g(f(z),y))), (g(y,y) = x)} = {y f(z); x g(f(z), f(z))} f( x ) = f(g(f(z), y )) f(g(f(z), f(z))) = f(g(f(z), f(z))). g( y, y ) = x. g(f(z),f(z)) = g(f(z), f(z)). 10/17/2017 63 10/17/2017 64 of Failure: Decompose Unify{(f(x,g(y)) = f(h(y),x))} Decompose: (f(x,g(y)) = f(h(y),x)) = Unify {(x = h(y)), (g(y) = x)} Orient: (g(y) = x) = Unify {(x = h(y)), (x = g(y))} Eliminate: (x = h(y)) Unify {(h(y) = g(y))} o {x h(y)} No rule to apply! Decompose fails! of Failure: Occurs Check Unify{(f(x,g(x)) = f(h(x),x))} Decompose: (f(x,g(x)) = f(h(x),x)) = Unify {(x = h(x)), (g(x) = x)} Orient: (g(y) = x) = Unify {(x = h(x)), (x = g(x))} No rules apply. 10/17/2017 65 10/17/2017 66 11

Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Symbol Table Translate Intermediate Representation Optimize Optimized IR Instruction Selection Unoptimized Machine- Specific Assembly Language Optimize Optimized Machine-Specific Assembly Language Emit code Assembly Language Assembler Relocatable Object Code Linker Machine Code Modified from Modern Compiler Implementation in ML, by Andrew Appel Meta-discourse Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and NDFSAs - Grammars Semantics - Natural Semantics - Transition Semantics 10/17/2017 68 Language Syntax Syntax is the description of which strings of symbols are meaningful expressions in a language It takes more than syntax to understand a language; need meaning (semantics) too Syntax is the entry point Syntax of English Language Pattern 1 Pattern 2 10/17/2017 69 10/17/2017 70 Elements of Syntax Character set previously always ASCII, now often 64 character sets Keywords usually reserved Special constants cannot be assigned to Identifiers can be assigned to Operator symbols Delimiters (parenthesis, braces, brackets) Blanks (aka white space) Elements of Syntax Expressions if... then begin... ;... end else begin... ;... end Type expressions typexpr 1 -> typexpr 2 Declarations (in functional languages) let pattern 1 = expr 1 in expr Statements (in imperative languages) a = b + c Subprograms let pattern 1 = let rec inner = in expr 10/17/2017 71 10/17/2017 72 12

Elements of Syntax Modules Interfaces Classes (for object-oriented languages) Lexing and Parsing Converting strings to abstract syntax trees done in two phases Lexing: Converting string (or streams of characters) into lists (or streams) of tokens (the words of the language) Specification Technique: Regular Expressions Parsing: Convert a list of tokens into an abstract syntax tree Specification Technique: BNF Grammars 10/17/2017 73 10/17/2017 74 Formal Language Descriptions Regular expressions, regular grammars, finite state automata Context-free grammars, BNF grammars, syntax diagrams Whole family more of grammars and automata covered in automata theory Grammars Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs 10/17/2017 75 10/17/2017 76 13