Class 25: Prolog, Concluded
Held Monday, April 5
- Reading: Louden, Chapter 4.
- The exam is due on Wednesday.
- My break was consumed by large tasks, and I was unable to grade
assignment 4. I hope to
have it ready tomorrow morning.
- We'll spend a little bit more time on Prolog before moving on to
grammars and syntax.
- Be sure to read Chapter 4 of Louden for Wednesday.
- I apologize if the discussion of syntax is review for some of you,
since we cover similar concepts in CSC341 and CSC362. Hopefully,
you'll find this gives a somewhat different perspective.
- Let's design a trinary
concatenate(Xs,Ys,Zs) holds when
Zs is the
list created by concatenating
- When you concatenate the empty list with anything, you get that thing.
- When you concatenate the list beginning with X with anything, you
get a list beginning with X and whose remainder is Whatever if
when you concatenate the rest of the first list with the second
list, you get Whatever.
concat([X|Xs], Ys, [X|Whatever]) :-
- Note that we don't need a rule that says "when you concatenate
anything with the empty list, you get the empty list". Why not?
Because that's already been handled by the first two cases and is
therefore redundant (and redundancy can be much more dangerous in
Prolog than it is in other languages).
- We can use this predicate in many ways.
- We can ask whether the concatenation of two lists is a third list.
?- concat([a,b,c], [d,e,f], [a,b,c,d,e,f]).
- We can ask what list we create by concatenating two lists.
?- concat([a,b,c], [d,e,f], L).
- We might be able to ask what list we need to concatenate to
one list to get another.
?- concat([a,b,c], X, [a,b,c,d,e,f]).
?- concat(X, [d,e,f], [a,b,c,d,e,f]).
- Can we go so far as to ask what lists we need to concatenate to
get a third? Try it and see.
?- concat(Xs, Ys, [a,b,c,d,e,f]).
- Now we're ready to think about more sophisiticated predicates, like
- When is X and member of the list L?
- Do we have to worry about explaining when X is not a member? No,
because Prolog works under the assumption that "if I can't prove it,
then it's not true"
- Note that a well-designed
member might even be able
to extract the members of a list.
- While Prolog generally uses the same philosophy as the functional languages
(you type an expression and see a result) it also includes a more traditional
mechanism for output (similar to scheme's
write(X) predicate always holds and has the side effect
of writing its argument to stdout.
nl predicate always holds and has the side effect
of writing a newline to stdout.
- For example, to extend our
in_tree function to list the nodes
it looks at, we might write:
showpath(Val,empty) :- write("not found!"), nl, fail.
Val < NodeVal, write(NodeVal), nl, showpath(Val,Less).
Val > NodeVal, write(NodeVal), nl, showpath(Val,Greater).
- Prolog lets you define functions that, it many ways, resemble
Haskell's constructors (but without the sophisticated typing).
- Basically, you can use a function within a predicate and Prolog does a
form of "pattern matching" on that function.
- For example, we might use
empty to define trees.
inTree(Val,empty) :- fail.
Val < NodeVal, in_tree(Val,Less).
Val > NodeVal, in_tree(Val,Greater).
- Think about how you'd write
- Created Tuesday, January 19, 1999 as a blank outline.
- Filled in the details on Monday, April 5, 1999.