In this section, we are working with a predicate for ``flattening'' a
tree where internal nodes have the form branch(
,
) and
leaves have the form leaf(
). The flattened form is a list
of all the items that show up in leaves. The predicate uses a helper
predicate flattenH that takes an extra accumulator list (for
efficiency).
unify:
resolution:
solve,
resolution and unify, even ones that fail.
solve([flattenH(T,[],[1,2,3])])Assume the program consists of only the following rules:
flattenH(leaf(X),L,[X|L]). flattenH(branch(V,W),L,N) :- flattenH(W,L,M), flattenH(V,M,N).
Do Exercise 23.1 (page 520) and Exercise 23.2 parts (b) and (c).
For 23.2(b), use if, lt, and minus as the AST
nodes, and use true and false as new potential values.
Put the complete Language Four interpreter in four.pl,
as well as giving the additional natural semantic rules on paper.
The source for the Language Three interpreter is available in
/afs/cs.uwm.edu/users/classes/cs431/src/homework12/three.pl
A small-step semantics work by returning the program with one step of evaluation done. Function calls and let's work by substituting the value for the variable into the body. The output is always an AST. To evaluate fully, one applies the small-step rules until no other rule is possible, which happens when there is a type error, or we end up with a value such as const(42). There are two kinds of values for Language Three: constants and functions.
The small-step semantics for
Language Three is as follows, where
means any expression, and
means the expression must be a value, and
means the expression
must be an integer constant.
Implement these rules in Prolog. The predicate
step(E1,E2) should be true if
E1 goes to E2 in one step, or as written mathematically:
. Write a second predicate
eval(E,V) that is true if E goes to V in zero or
more steps and V cannot be evaluated further. Hint: you will need to write a
predicate isValue(E) that is true for values, and a
predicate subst(E1,X,E2,E3) which unifies E3 with the
result of substituting X with E2 everywhere it occurs in
E1 (except inside of functions or ``let''s with formal
parameter equal to X).
Test your code, in particular using the examples from pages 510 and 512,
and the following erroneous terms:
let(f,const(3),apply(var(f),const(4))) let(x,const(3),plus(var(x),var(y)))
You submit your program work by putting it in the homework12 directory
in your AFS class volume.
You may do all your work in this directory, or you may wish to do your
work in a different directory and copy things when correct into this
directory. In any case, you will lose permission to write things in
this directory after the deadline, which is 4:00pm on Tuesday,
May 4th. In other words, you must be done before lecture
starts.
The homework12 directory should include the following:
four.plsmall.pl