Programming Languages and Compilers CS 421 Sasa Misailovic

  • Slides: 90
Download presentation
Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https: //courses. engr.

Programming Languages and Compilers (CS 421) Sasa Misailovic 4110 SC, UIUC https: //courses. engr. illinois. edu/cs 421/fa 2017/CS 421 A Based on slides by Elsa Gunter, which were inspired by earlier slides by Mattox Beckman, Vikram Adve, and Gul Agha 10/26/2021 1

Two Problems n Type checking n n Question: Does exp. e have type in

Two Problems n Type checking n n Question: Does exp. e have type in env ? Answer: Yes / No Method: Type derivation Typability n n n Question Does exp. e have some type in env. ? If so, what is it? Answer: Type / error Method: Type inference 10/26/2021 2

Type Inference - Outline n n n Begin by assigning a type variable as

Type Inference - Outline n n n 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

Type Inference - Example n What type can we give to (fun x ->

Type Inference - Example n What type can we give to (fun x -> fun f -> f (f x)) n Start with a type variable and then look at the way the term is constructed 10/26/2021 4

Type Inference - Example n n n First approximate: Give type to full expr

Type Inference - Example n n n First approximate: Give type to full expr { }|- (fun x -> fun f -> f (f x)) : Second approximate: use fun rule {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : Remember constraint ( ) 10/26/2021 5

Type Inference - Example n n Third approximate: use fun rule {f : ;

Type Inference - Example n n Third approximate: use fun rule {f : ; x : } |- f (f x) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f(f x)) : ( ); ( ) 10/26/2021 6

Type Inference - Example n n Fourth approximate: use app rule {f: ; x:

Type Inference - Example n n Fourth approximate: use app rule {f: ; x: }|- f : {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); ( ) 10/26/2021 7

Type Inference - Example Fifth approximate: use var rule, get constraint , Solve with

Type Inference - Example Fifth approximate: use var rule, get constraint , Solve with same n Apply to next sub-proof {f: ; x: }|- f : {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 8

Type Inference - Example n n Current subst: { } … {f: ; x:

Type Inference - Example n n Current subst: { } … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); ( ) 10/26/2021 9

Type Inference - Example n Current subst: { } Use App Rule {f: ;

Type Inference - Example n Current subst: { } Use App Rule {f: ; x: }|- f: z {f: ; x: }|- x: z … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) 10/26/2021 10

Type Inference - Example Current subst: { } n Var rule: Solve z Unification

Type Inference - Example Current subst: { } n Var rule: Solve z Unification {f: ; x: }|- f: z {f: ; x: }|- x: z … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 11

Type Inference - Example Current subst: {z , } o { } n Var

Type Inference - Example Current subst: {z , } o { } n Var rule: Solve z Unification {f: ; x: }|- f: z {f: ; x: }|- x: z … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 12

Type Inference - Example Current subst: {z , , } n Apply to next

Type Inference - Example Current subst: {z , , } n Apply to next sub-proof (done)… {f: ; x: }|- x: z … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 13

Type Inference - Example Current subst: {z , , } n Apply to next

Type Inference - Example Current subst: {z , , } n Apply to next sub-proof (done)… {f: ; x: }|- x: … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 14

Type Inference - Example Current subst: {z , , } n Var rule: …

Type Inference - Example Current subst: {z , , } n Var rule: … {f: ; x: }|- x: … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 15

Type Inference - Example Current subst: { }o{z , , } n Solves subproof;

Type Inference - Example Current subst: { }o{z , , } n Solves subproof; return one layer … {f: ; x: }|- x: … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : n ( ); ( ) n 10/26/2021 16

Type Inference - Example Current subst: { , z , , } n Solves

Type Inference - Example Current subst: { , z , , } n Solves this subproof; return one layer … n n … {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); ( ) 10/26/2021 17

Type Inference - Example Current subst: { , z , , } n Need

Type Inference - Example Current subst: { , z , , } n Need to satisfy constraint ( ), given subst, becomes: (( ) ) … n n {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); ( ) 10/26/2021 18

Type Inference - Example Current subst: { (( ) ), , z , ,

Type Inference - Example Current subst: { (( ) ), , z , , } n Solves subproof; return one layer … n n {f : ; x : } |- (f (f x)) : {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); ( ) 10/26/2021 19

Type Inference - Example Current subst: { (( ) ), , z , ,

Type Inference - Example Current subst: { (( ) ), , z , , } n Need to satisfy constraint ( ) given subst: ( (( ) )) … n n {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : ( ); 10/26/2021 20

Type Inference - Example Current subst: { ( (( ) )), (( ) ),

Type Inference - Example Current subst: { ( (( ) )), (( ) ), , z , , } n Solves subproof; return on layer n {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f (f x)) : 10/26/2021 21

Type Inference - Example Current subst: { ( (( ) )), (( ) ),

Type Inference - Example Current subst: { ( (( ) )), (( ) ), , z , , } n n Done: ( (( ) )) { } |- (fun x -> fun f -> f (f x)) : 10/26/2021 22

Type Inference Algorithm Let infer ( , e, ) = n is a typing

Type Inference Algorithm Let infer ( , e, ) = n is a typing environment (giving polymorphic types to expression variables) n e is an expression n is a type (with type variables), n is a substitution of types for type variables n Idea: represents the constraints on type variables necessary for |- e : n Should have ( ) |- e : ( ) valid Slight abuse of notation: ( ) is substitution applied to all terms in the environment = {x: …} (i. e. , ( )= {x: ( ) 10/26/2021 23 …}). n

Type Inference Algorithm (All in one!) 10/26/2021 24

Type Inference Algorithm (All in one!) 10/26/2021 24

Type Inference Algorithm infer ( , exp, ) = n Case exp of n

Type Inference Algorithm infer ( , exp, ) = n Case exp of n Var v --> return Unify{ fresh. Instance( (v))} n n n 10/26/2021 Replace all quantified type vars by fresh ones Const c --> return Unify{ fresh. Instance } where |- c : by the constant rules fun x -> e --> n Let , be fresh variables n Let = infer ({x: } + , e, ) n Return Unify({ ( )}) o 25

Inference Example (Repeat) Fifth approximate: use var rule, get constraint , Solve with same

Inference Example (Repeat) Fifth approximate: use var rule, get constraint , Solve with same n Apply to next sub-proof n {f: ; x: } |- f : 10/26/2021 26

Inference Example (Repeat) n What do we do here? {f: . ; x: }

Inference Example (Repeat) n What do we do here? {f: . ; x: } |- f : n And here? {f: . ; x: } |- f : 10/26/2021 27

Inference Example (Repeat) Third approximate: use fun rule … {x : } |- (fun

Inference Example (Repeat) Third approximate: use fun rule … {x : } |- (fun f -> f (f x)) : { } |- (fun x -> fun f -> f(f x)) : n ( ); n 10/26/2021 28

Type Inference Algorithm (cont) n Case exp of n App (e 1 e 2)

Type Inference Algorithm (cont) n Case exp of n App (e 1 e 2) --> n Let be a fresh variable n Let 1 = infer( , e 1, ) n Let 2 = infer( 1( ), e 2, 1( )) n Return 2 o 1 10/26/2021 29

Type Inference - Example n Fourth approximate: use app rule {f: ; x: }|-

Type Inference - Example n Fourth approximate: use app rule {f: ; x: }|- f : {f: ; x: }|- f x : {f : ; x : } |- (f (f x)) : 10/26/2021 30

Type Inference Algorithm (cont) n Case exp of n If e 1 then e

Type Inference Algorithm (cont) n Case exp of n If e 1 then e 2 else e 3 --> n Let 1 = infer( , e 1, bool) n Let 2 = infer( 1( ), e 2, 1( )) n Let 3 = infer( 2 o 1( ), e 2, 2 o ( )) n Return 3 o 2 o 1 10/26/2021 31

Type Inference Algorithm (cont) n Case exp of n let x = e 1

Type Inference Algorithm (cont) n Case exp of n let x = e 1 in e 2 --> Let be a fresh variable n Let 1 = infer( , e 1, ) n Let 2 = infer({x: GEN( 1( ), 1( ))} + 1( ), e 2, 1( )) n Return 2 o 1 n 10/26/2021 32

Type Inference Algorithm (cont) n Case exp of n let rec x = e

Type Inference Algorithm (cont) n Case exp of n let rec x = e 1 in e 2 --> n Let be a fresh variable n Let 1 = infer({x: } + , e 1, ) n Let 2 = infer({x: GEN( 1( ), 1( ))} + 1( ), e 2, 1( )) n Return 2 o 1 10/26/2021 33

Type Inference Algorithm (cont) n To infer a type, introduce type_of n Let be

Type Inference Algorithm (cont) n To infer a type, introduce type_of n Let be a fresh variable n type_of ( , e) = let be a fresh variable in let = infer ( , e, ) in ( ) Need substitution! n Need an algorithm for Unif! n 10/26/2021 34

Reminder: Type Terms n n Terms made from constructors and variables Reminder: 10/26/2021 35

Reminder: Type Terms n n Terms made from constructors and variables Reminder: 10/26/2021 35

Reminder: Type Terms n n n Terms made from constructors and variables Constructors may

Reminder: Type Terms n n n Terms made from constructors and variables 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/26/2021 36

Substitution Implementation type term = Variable of string | Constructor of (string * term

Substitution Implementation type term = Variable of string | Constructor of (string * term list) let rec subst var_name residue term = match term with Variable name -> if var_name = name then residue else term | Constructor (c, tys) -> let newt = List. map (subst var_name residue) tys in Constructor (c, newt); ; 10/26/2021 37

Unification Problem Given a set of pairs of terms (“equations”) {(s 1, t 1),

Unification Problem Given a set of pairs of terms (“equations”) {(s 1, t 1), (s 2, t 2), …, (sn, tn)} * (the unification problem) does there exist a substitution (the unification solution) of terms for variables such that (si) is the same as (ti), for all i = 1, …, n? • Think of these pairs as {(“s 1= t 1”), (“s 2= t 2”), …, (“sn= tn”)} • This is the notation we’re going to use in the example 10/26/2021 38

Uses for Unification Type Inference and type checking n Pattern matching as in OCaml

Uses for Unification Type Inference and type checking n Pattern matching as in OCaml n n Can use a simplified version of algorithm Logic Programming - Prolog n Simple parsing n 10/26/2021 39

Unification Algorithm n Let S = {(s 1= t 1), (s 2= t 2),

Unification Algorithm n Let S = {(s 1= t 1), (s 2= t 2), …, (sn= tn)} be a unification problem. n n Case S = { }: Unif(S) = Identity function n n Unif(S) returns a substitution (i. e. , no substitution) Case S = {(s = t)} S’: Four main steps n Delete, Decompose, Orient, Eliminate 10/26/2021 40

Unification Algorithm for S = {(s = t)} S’ n n n Delete: if

Unification Algorithm for S = {(s = t)} S’ n n n Delete: if s is t (s and t are the same term) then Unif(S) = Unif(S’) Decompose: if s is f (q 1, … , qm) and t is f (r 1, … , rm) (same f, same m!), then Unif(S) = Unif({(q 1= r 1), …, (qm=rm)} S’) Orient: if t is x (a variable), and s is not a variable, Unif(S) = Unif ({(x = s)} S’) 10/26/2021 41

Unification Algorithm for S = {(s = t)} S’ n Eliminate: if s is

Unification Algorithm for S = {(s = t)} S’ n Eliminate: if s is x (a variable), and x does not occur in t (use “occurs (x, t)” check!) then n Let = {x t} n Let = Unif( (S’) ) n Unif(S) = {x (t)} o Be careful when composing substitutions: n {x a} o {y b} = {y ({x a}(b))} o {x a} if y not in a n n So we can also write the last step as 10/26/2021 42

Tricks for Efficient Unification Don’t return substitution, rather do it incrementally n Make substitution

Tricks for Efficient Unification Don’t return substitution, rather do it incrementally n Make substitution be constant time n Requires implementation of terms to use mutable structures (or possibly lazy structures) n We won’t discuss these n 10/26/2021 43

Example n x, y, z variables, f, g constructors n Unify {(f(x) = f(g(f(z),

Example n x, y, z variables, f, g constructors n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = ? 10/26/2021 44

Example n n n x, y, z variables, f, g constructors Unify {(f(x) =

Example n n n x, y, z variables, f, g constructors Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = ? For example: n n X list = (Z list * Y) list (Y * Y) = X 10/26/2021 45

Example n x, y, z variables, f, g constructors S = {(f(x) = f(g(f(z),

Example n x, y, z variables, f, g constructors S = {(f(x) = f(g(f(z), y))), (g(y, y) = x)} is nonempty n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = ? n 10/26/2021 46

Example n x, y, z variables, f, g constructors Pick a pair: (g(y, y)

Example n x, y, z variables, f, g constructors Pick a pair: (g(y, y) = x) n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = ? n 10/26/2021 47

Example n n n x, y, z variables, f, g constructors Pick a pair:

Example n n n x, y, z variables, f, g constructors 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 n 10/26/2021 48

Example n x, y, z variables, f, g constructors n Unify {(f(x) = f(g(f(z),

Example n x, y, z variables, f, g constructors n Unify {(f(x) = f(g(f(z), y))), (x = g(y, y))} = ? 10/26/2021 49

Example n x, y, z variables, f, g constructors {(f(x) = f(g(f(z), y))), (x

Example n x, y, z variables, f, g constructors {(f(x) = f(g(f(z), y))), (x = g(y, y))} is non-empty n Unify {(f(x) = f(g(f(z), y))), (x = g(y, y))} = ? n 10/26/2021 50

Example n x, y, z variables, f, g constructors Pick a pair: (x =

Example n x, y, z variables, f, g constructors Pick a pair: (x = g(y, y)) n Unify {(f(x) = f(g(f(z), y))), (x = g(y, y))} = ? n 10/26/2021 51

Example n n x, y, z variables, f, g constructors Pick a pair: (x

Example n n x, y, z variables, f, g constructors Pick a pair: (x = g(y, y)) Eliminate x with substitution {x g(y, y)} n Check: x not in g(y, y). Unify {(f(x) = f(g(f(z), y))), (x = g(y, y))} = ? 10/26/2021 52

Example n n x, y, z variables, f, g constructors Pick a pair: (x

Example n n x, y, z variables, f, g constructors 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/26/2021 53

Example n n x, y, z variables, f, g constructors Unify {(f(g(y, y)) =

Example n n x, y, z variables, f, g constructors Unify {(f(g(y, y)) = f(g(f(z), y)))} o {x g(y, y)} = ? 10/26/2021 54

Example n n n x, y, z variables, f, g constructors {(f(g(y, y)) =

Example n n n x, y, z variables, f, g constructors {(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)} = ? 10/26/2021 55

Example n n n x, y, z variables, f, g constructors Pick a pair:

Example n n n x, y, z variables, f, g constructors Pick a pair: (f(g(y, y)) = f(g(f(z), y))) Unify {(f(g(y, y)) = f(g(f(z), y)))} o {x g(y, y)} = ? 10/26/2021 56

Example n n x, y, z variables, f, g constructors Pick a pair: (f(g(y,

Example n n x, y, z variables, f, g constructors 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 {(g(y, y) = g(f(z), y))} o {x g(y, y)} 10/26/2021 57

Example n n n x, y, z variables, f, g constructors {(g(y, y) =

Example n n n x, y, z variables, f, g constructors {(g(y, y) = g(f(z), y))} is non-empty Unify {(g(y, y) = g(f(z), y))} o {x g(y, y)} = ? 10/26/2021 58

Example n n n x, y, z variables, f, g constructors Pick a pair:

Example n n n x, y, z variables, f, g constructors Pick a pair: (g(y, y) = g(f(z), y)) Unify {(g(y, y) = g(f(z), y))} o {x g(y, y)} = ? 10/26/2021 59

Example n n x, y, z variables, f, g constructors Pick a pair: (f(g(y,

Example n n x, y, z variables, f, g constructors 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 {(g(y, y) = g(f(z), y))} o {x g(y, y)} = Unify {(y = f(z)); (y = y)} o {x g(y, y)} 10/26/2021 60

Example n x, y, z variables, f, g constructors n Unify {(y = f(z));

Example n x, y, z variables, f, g constructors n Unify {(y = f(z)); (y = y)} o {x g(y, y)} = ? 10/26/2021 61

Example n x, y, z variables, f, g constructors {(y = f(z)); (y =

Example n x, y, z variables, f, g constructors {(y = f(z)); (y = y)} o {x g(y, y)} is non-empty n Unify {(y = f(z)); (y = y)} o {x g(y, y)} = ? n 10/26/2021 62

Example n x, y, z variables, f, g constructors Pick a pair: (y =

Example n x, y, z variables, f, g constructors Pick a pair: (y = f(z)) n Unify {(y = f(z)); (y = y)} o {x g(y, y)} = ? n 10/26/2021 63

Example n n x, y, z variables, f, g constructors Pick a pair: (y

Example n n x, y, z variables, f, g constructors 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))} 10/26/2021 64

Example n n x, y, z variables, f, g constructors Unify {(f(z) = f(z))}

Example n n x, y, z variables, f, g constructors Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} = ? 10/26/2021 65

Example n n n x, y, z variables, f, g constructors {(f(z) = f(z))}

Example n n n x, y, z variables, f, g constructors {(f(z) = f(z))} is non-empty Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} = ? 10/26/2021 66

Example n n n x, y, z variables, f, g constructors Pick a pair:

Example n n n x, y, z variables, f, g constructors Pick a pair: (f(z) = f(z)) Unify {(f(z) = f(z))} o {y f(z); x g(f(z), f(z))} = ? 10/26/2021 67

Example n n x, y, z variables, f, g constructors Pick a pair: (f(z)

Example n n x, y, z variables, f, g constructors 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))} 10/26/2021 68

Example n x, y, z variables, f, g constructors n Unify {} o {y

Example n x, y, z variables, f, g constructors n Unify {} o {y f(z); x g(f(z), f(z))} = ? 10/26/2021 69

Example n n x, y, z variables, f, g constructors {} is empty Unify

Example n n x, y, z variables, f, g constructors {} 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))} 10/26/2021 70

Example n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = {y f(z);

Example n 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/26/2021 71

Example n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = {y f(z);

Example n Unify {(f(x) = f(g(f(z), y))), (g(y, y) = x)} = {y f(z); x g(f(z), f(z))} y int list, x (int list * int list) f( x ) = f(g(f(z), y )) f(g(f(z), f(z))) = f(g(f(z), f(z))). (int list * int list) list = (int list * int list) list g( y , y ) = x. g(f(z), f(z)) = g(f(z), f(z)). (int list * int list) = (int list * int list) 10/26/2021 72

Example of Failure: Decompose n Unify{(f(x, g(y)) = f(h(y), x))} Decompose: (f(x, g(y)) =

Example of Failure: Decompose n 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)} n No rule to apply! Decompose fails! 10/26/2021 73

Example of Failure: Occurs Check n n n Unify{(f(x, g(x)) = f(h(x), x))} Decompose:

Example of Failure: Occurs Check n n n 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))} n No rules apply. 10/26/2021 74

Course Objectives n New programming paradigm n n n Phases of an interpreter /

Course Objectives n New programming paradigm n n n Phases of an interpreter / compiler n n Functional programming Environments and Closures Patterns of Recursion Continuation Passing Style Lexing and parsing Type systems Interpretation Programming Language Semantics n n n Lambda Calculus Operational Semantics Axiomatic Semantics 10/26/2021 75

Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis

Major Phases of a Compiler Source Program Lex Tokens Parse Abstract Syntax Semantic Analysis Environment Analyze + Transform Optimized IR Instruction Selection Unoptimized Machine-Specific Assembly Language Instruction Optimize Assembler Relocatable Object Code Linker Machine Code Optimized Machine-Specific Assembly Language Translate Emit code Intermediate Representation Assembly Language Modified from “Modern Compiler Implementation in ML”, by Andrew Appel

Programming Languages & Compilers Three Main Topics of the Course I III New Programming

Programming Languages & Compilers Three Main Topics of the Course I III New Programming Paradigm Language Semantics 10/26/2021 77

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics

Programming Languages & Compilers I II III New Programming Paradigm Language Translation Language Semantics 10/26/2021 78

Programming Languages & Compilers I : New Programming Paradigm Functional Environments Patterns of Programming

Programming Languages & Compilers I : New Programming Paradigm Functional Environments Patterns of Programming and Recursion Closures 10/26/2021 Continuation Passing Style 79

Programming Languages & Compilers II : Language Translation Lexing and Parsing 10/26/2021 Type Systems

Programming Languages & Compilers II : Language Translation Lexing and Parsing 10/26/2021 Type Systems Interpretation 80

Programming Languages & Compilers III : Language Semantics Operational Semantics 10/26/2021 Lambda Calculus Axiomatic

Programming Languages & Compilers III : Language Semantics Operational Semantics 10/26/2021 Lambda Calculus Axiomatic Semantics 81

Meta-discourse n n n Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and

Meta-discourse n n n Language Syntax and Semantics Syntax - Regular Expressions, DFSAs and NDFSAs - Grammars Semantics - Natural Semantics - Transition Semantics 10/26/2021 82

Language Syntax n n n Syntax is the description of which strings of symbols

Language Syntax n n n 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 10/26/2021 83

Syntax of English Language n Pattern 1 n Pattern 2 10/26/2021 84

Syntax of English Language n Pattern 1 n Pattern 2 10/26/2021 84

Elements of Syntax n n n n Character set – previously always ASCII, now

Elements of Syntax n n n n 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) 10/26/2021 85

Elements of Syntax n Expressions if. . . then begin. . . ; .

Elements of Syntax n Expressions if. . . then begin. . . ; . . . end else begin. . . ; . . . end n Type expressions typexpr 1 -> typexpr 2 n Declarations (in functional languages) let pattern 1 = expr 1 in expr n Statements (in imperative languages) a=b+c n Subprograms let pattern 1 = let rec inner = … in expr 10/26/2021 86

Elements of Syntax Modules n Interfaces n Classes (for object-oriented languages) n 10/26/2021 87

Elements of Syntax Modules n Interfaces n Classes (for object-oriented languages) n 10/26/2021 87

Lexing and Parsing n Converting strings to abstract syntax trees done in two phases

Lexing and Parsing n Converting strings to abstract syntax trees done in two phases n Lexing: Converting string (or streams of characters) into lists (or streams) of tokens (the “words” of the language) n n Specification Technique: Regular Expressions Parsing: Convert a list of tokens into an abstract syntax tree n 10/26/2021 Specification Technique: BNF Grammars 88

Formal Language Descriptions n n n Regular expressions, regular grammars, finite state automata Context-free

Formal Language Descriptions n n n 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 10/26/2021 89

Grammars n n Grammars are formal descriptions of which strings over a given character

Grammars n n 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/26/2021 90