Programming Languages and Compilers CS 421 Reza Zamani

  • Slides: 28
Download presentation
Programming Languages and Compilers (CS 421) Reza Zamani http: //www. cs. illinois. edu/class/cs 421/

Programming Languages and Compilers (CS 421) Reza Zamani http: //www. cs. illinois. edu/class/cs 421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 6/4/2021 1

Background for Unification n n Terms made from constructors and variables (for the simple

Background for Unification n n 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 6/4/2021 2

Simple Implementation Background type term = Variable of string | Const of (string *

Simple Implementation Background 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); ; 6/4/2021 3

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) = (ti), for all i = 1, …, n? 6/4/2021 4

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 6/4/2021 5

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

Unification Algorithm n n n Let S = {(s 1, t 1), (s 2, t 2), …, (sn, tn)} be a unification problem. Case S = { }: Unif(S) = Identity function (ie no substitution) Case S = {(s, t)} S’ : Four main steps 6/4/2021 6

Unification Algorithm n n n Delete: if s = t (they are the same

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

Unification Algorithm n Eliminate: if s = x is a variable, and x does

Unification Algorithm n Eliminate: if s = x is a variable, and x does not occur in t (the occurs check), then Let = x | t n Let = Unif( (S’)) n Unif(S) = {x | (t)} o n n 6/4/2021 Note: {x | a} o {y | b} = {y | {x | a}(b)} o {x | a} if y not in a 8

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 haven’t discussed these yet n 6/4/2021 9

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

Example n x, y, z variables, f, g constructors n S = {(f(x), f(g(y, z))), (g(y, f(y)), x)} 6/4/2021 10

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

Example n x, y, z variables, f, g constructors n Pick a pair: (g(y, f(y)), x) n S = {(f(x), f(g(y, z))), (g(y, f(y)), x)} 6/4/2021 11

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

Example n x, y, z variables, f, g constructors n Pick a pair: (g(y, f(y))), x) Orient is first rule that applies n S = {(f(x), f(g(y, z))), (g(y, f(y)), x)} n 6/4/2021 12

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

Example n x, y, z variables, f, g constructors n S -> {(f(x), f(g(y, z))), (x, g(y, f(y)))} 6/4/2021 13

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

Example n x, y, z variables, f, g constructors Pick a pair: (f(x), f(g(y, z))) n S -> {(f(x), f(g(y, z))), (x, g(y, f(y)))} n 6/4/2021 14

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

Example n n x, y, z variables, f, g constructors Pick a pair: (f(x), f(g(y, z))) Decompose it: (x, g(y, z)) S -> {(x, g(y, z)), (x, g(y, f(y)))} 6/4/2021 15

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

Example n x, y, z variables, f, g constructors Pick a pair: (x, g(y, f(y))) n S -> {(x, g(y, z)), (x, g(y, f(y)))} n 6/4/2021 16

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

Example x, y, z variables, f, g constructors n Pick a pair: (x, g(y, f(y))) n Substitute: n S -> {(g(y, f(y)), g(y, z))} With {x | g(y, f(y))} n 6/4/2021 17

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

Example n n x, y, z variables, f, g constructors Pick a pair: (g(y, f(y)), g(y, z)) S -> {(g(y, f(y)), g(y, z))} With {x | g(y, f(y))} n 6/4/2021 18

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

Example x, y, z variables, f, g constructors n Pick a pair: (g(y, f(y)), g(y, z)) n Decompose: (y, y) and (f(y), z) n S -> {(y, y), (f(y), z)} With {x | g(y, f(y))} n 6/4/2021 19

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, y) S -> {(y, y), (f(y), z)} With {x | g(y, f(y))} n 6/4/2021 20

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

Example x, y, z variables, f, g constructors n Pick a pair: (y, y) n Delete n S -> {(f(y), z)} With {x | g(y, f(y))} n 6/4/2021 21

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

Example n n x, y, z variables, f, g constructors Pick a pair: (f(y), z) S -> {(f(y), z)} With {x | g(y, f(y))} n 6/4/2021 22

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

Example x, y, z variables, f, g constructors n Pick a pair: (f(y), z) n Orient n S -> {(z, f(y))} With {x | g(y, f(y))} n 6/4/2021 23

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

Example n n x, y, z variables, f, g constructors Pick a pair: (z, f(y)) S -> {(z, f(y))} With {x | g(y, f(y))} n 6/4/2021 24

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

Example x, y, z variables, f, g constructors n Pick a pair: (z, f(y)) n Substitute n S -> { } With {x | {z | f(y)} (g(y, f(y)) } o {z | f(y)} n 6/4/2021 25

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

Example x, y, z variables, f, g constructors n Pick a pair: (z, f(y)) n Substitute n S -> { } With {x | g(y, f(y))} o {(z | f(y))} n 6/4/2021 26

Example S = {(f(x), f(g(y, z))), (g(y, f(y)), x)} Solved by {x | g(y,

Example S = {(f(x), f(g(y, z))), (g(y, f(y)), x)} Solved by {x | g(y, f(y))} o {(z | f(y))} f(g(y, f(y))) = f(g(y, f(y))) x z and g(y, f(y)) = g(y, f(y)) x 6/4/2021 27

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

Example of Failure n n n n S = {(f(x, g(y)), f(h(y), x))} Decompose S -> {(x, h(y)), (g(y), x)} Orient S -> {(x, h(y)), (x, g(y))} Substitute S -> {(h(y), g(y))} with {x | h(y)} No rule to apply! Decompose fails! 6/4/2021 28