# Class 25: Prolog, Concluded

Back to Prolog. On to Languages and Grammars.

Held Monday, April 5

Summary

Contents

Notes

• 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.

## Prolog, Concluded

### Concatenation

• Let's design a trinary `concatenate`. `concatenate(Xs,Ys,Zs)` holds when `Zs` is the list created by concatenating `Xs` and `Ys`.
• When you concatenate the empty list with anything, you get that thing.
```concat([],Ys,Ys).
```
• 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]) :-
concat(Xs,Ys,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]).
yes
```
• We can ask what list we create by concatenating two lists.
```?- concat([a,b,c], [d,e,f], L).
L=[a,b,c,d,e,f]
yes
```
• 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]).
X=[d,e,f]
yes
?- concat(X, [d,e,f], [a,b,c,d,e,f]).
X=[a,b,c]
yes
```
• 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 `member`.
• 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.

### Input and Output

• 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 `display`).
• The `write(X)` predicate always holds and has the side effect of writing its argument to stdout.
• The `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.
showpath(Val,node(Val,Less,Greater)) :-
write(Val), nl.
showpath(Val,node(NodeVal,Less,Greater)) :-
Val < NodeVal, write(NodeVal), nl, showpath(Val,Less).
showpath(Val,node(NodeVal,Less,Greater)) :-
Val > NodeVal, write(NodeVal), nl, showpath(Val,Greater).
```

### Functions and Patterns

• 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 `node` and `empty` to define trees.
```inTree(Val,empty) :- fail.
inTree(Val,node(Val,Less,Greater)).
inTree(Val,node(NodeVal,Less,Greater)) :-
Val < NodeVal, in_tree(Val,Less).
inTree(Val,node(NodeVal,Less,Greater)) :-
Val > NodeVal, in_tree(Val,Greater).
```
• Think about how you'd write `insert` and `delete`.

History

• Created Tuesday, January 19, 1999 as a blank outline.
• Filled in the details on Monday, April 5, 1999.

Back to Prolog. On to Languages and Grammars.

Disclaimer Often, these pages were created ``on the fly'' with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.