15.08.2013 Views

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Problem 1.38 (Substitution Application)<br />

Consider the following SML data type of terms:<br />

datatype term = const of string<br />

| var of string<br />

| pair of term * term<br />

| appl of string * term<br />

Constants <strong>and</strong> variables are represented by a constructor taking their name string, whereas applications<br />

of the <strong>for</strong>m f(t) are constructed from the name string <strong>and</strong> the argument. Remember<br />

that we use f(a, b) as an abbreviation <strong>for</strong> f(〈a, b〉). Thus a term f(a, g(x)) is represented as<br />

appl("f",pair(const("a"), appl("g", var("x")))).<br />

With this, we can represent substitutions as lists of elementary substitutions, which are pairs<br />

of type term * string. Thus we can set<br />

type subst = term * string list<br />

<strong>and</strong> represent a substitution σ = [(f(a))/x], [b/y] as [(appl("f", const("a")), "x"), (const("b"), "y")].<br />

Of course we may not allow ambiguous substitutions which contain duplicate strings.<br />

Write an SML function substApply <strong>for</strong> the substitution application operation, i.e. substApply<br />

takes a substitution σ <strong>and</strong> a term A as arguments <strong>and</strong> returns the term σ(A) if σ is unambiguous<br />

<strong>and</strong> raises an exception otherwise.<br />

Make sure that your function applies substitutions in a parallel way, i.e. that [y/x], [x/z](f(z)) =<br />

f(x).<br />

Solution:<br />

exception ambiguous_substitution<br />

local<br />

fun sa(s,const(str)) = const(str)<br />

| sa(s,pair(t1,t2)) = pair(sa(s,t1),sa(s,t2))<br />

| sa(s,appl(fs,t1)) = appl(fs,sa(s,t1))<br />

| sa(nil,var(str)) = var(str)<br />

| sa((t,x)::L,var(str)) = if str = x then t else sa(L,var(str))<br />

fun ambiguous = ...<br />

in<br />

fun substApply (s,t) = if ambiguous(s)<br />

then raise ambiguous_substitution<br />

else sa(s,t)<br />

end<br />

or<br />

(* (C) by Anna Michalska *)<br />

datatype term = const of string<br />

| var of string<br />

| pair of term * term<br />

| appl of string * term;<br />

type subst = (term * string) list;<br />

exception ania;<br />

fun comparing1 ((x1,x2), []) = true | comparing1 ((x1,x2), hd::tl) = if<br />

hd=x2 then false else comparing1 ((x1,x2),tl);<br />

fun comparing2([],_)=true | comparing2 ((x3,x4)::t,tl) = if (comparing1<br />

((x3,x4),tl)) then comparing2 (t,x4::tl) else raise ania;<br />

fun tab (a,[]) = var(a)<br />

| tab (a, (a1,a2)::tl) = if (a=a2) then a1 else tab(a,tl);<br />

fun substApply_r (appl(a,b),subst_in) = appl(a,substApply_r(b,subst_in))<br />

|substApply_r (pair(a,b),subst_in) =<br />

pair(substApply_r(a,subst_in),substApply_r(b,subst_in))<br />

|substApply_r (var(a),subst_in) = tab(a,subst_in)<br />

|substApply_r (const(x),subst_in) = const(x);<br />

39<br />

30pt

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!