LPN: reorg, split notes from exocortex page.

The exocortex page was getting unwieldy, this makes more sense.
This commit is contained in:
2018-01-16 16:43:12 -08:00
parent 36ed6df77f
commit 6d4fb673ef
19 changed files with 363 additions and 0 deletions

70
lpn/ch02/crossword.pl Normal file
View File

@@ -0,0 +1,70 @@
%% Exercise 2.4: Here are six Italian words:
%%
%% - astante
%% - astoria
%% - baratto
%% - cobalto
%% - pistola
%% - statale
%%
%% They are to be arranged, crossword puzzle fashion, in the following grid:
%%
%% V1 V2 V3
%% . . .
%% H1 . 1 . 4 . 7 .
%% . . .
%% H2 . 2 . 5 . 8 .
%% . . .
%% H3 . 3 . 6 . 9 .
%% . . .
%%
%% The following knowledge base represents a lexicon containing these words:
word(astante, a,s,t,a,n,t,e).
word(astoria, a,s,t,o,r,i,a).
word(baratto, b,a,r,a,t,t,o).
word(cobalto, c,o,b,a,l,t,o).
word(pistola, p,i,s,t,o,l,a).
word(statale, s,t,a,t,a,l,e).
%% Write a predicate crossword/6 that tells us how to fill in the
%% grid. The first three arguments should be the vertical words from
%% left to right, and the last three arguments the horizontal words
%% from top to bottom.
crossword(V1, V2, V3, H1, H2, H3) :-
word(V1, _, _1, _, _2, _, _3, _),
word(V2, _, _4, _, _5, _, _6, _),
word(V3, _, _7, _, _8, _, _9, _),
word(H1, _, _1, _, _4, _, _7, _),
word(H2, _, _2, _, _5, _, _8, _),
word(H3, _, _3, _, _6, _, _9, _),
V1 \= V2, V1 \= V3, V1 \= H1, V1 \= H2, V1 \= H3,
V2 \= V3, V2 \= H1, V2 \= H2, V2 \= H3,
V3 \= H1, V3 \= H2, V3 \= H3,
H1 \= H2, H1 \= H3, H2 \= H3.
%% NB: execute the following query to find out the correct arrangement.
%% crossword(A, B, C, D, E, F).
%% $ swipl -q
%% 1 ?- [italiano].
%% true.
%%
%% 2 ?- crossword(A, B, C, D, E, F).
%% A = astante,
%% B = cobalto,
%% C = pistola,
%% D = astoria,
%% E = baratto,
%% F = statale ;
%% A = astoria,
%% B = baratto,
%% C = statale,
%% D = astante,
%% E = cobalto,
%% F = pistola ;
%% false.
%% Note that there are two solutions; these are mirrors of each other
%% with the vertical words substituted for the horizontal words.

50
lpn/ch02/notes.md Normal file
View File

@@ -0,0 +1,50 @@
## Unification
Two terms unify if they are
1. The same term
2. They contain variables that can be uniformly instantiated such that the
resulting terms are equal.
Unification is foundational to Prolog.
`=/2` checks whether its two arguments unify:
```
12 ?- =(a, a).
true.
13 ?- =(a, b).
false.
```
*NB* variable assignment with `=`:
```
24 ?- vincent = X.
X = vincent.
25 ?- jealous(Y, X).
Y = vincent,
X = marsellus ;
Y = marsellus,
X = vincent ;
false.
```
### Lines
```
/* lines.pl */
vertical(line(point(X,Y),point(X,Z))).
horizontal(line(point(X,Y),point(Z,Y))).
```
Check this out:
```
39 ?- vertical(line(point(1, 7), P)).
P = point(1, _402).
```
The answer is structured: `_402` is a placeholder variable that means "any old
value of Y will do." This answer was derived solely through unification, and
didn't require logic (e.g. modus ponens) or any sort of mathematical
relationships. "Moreover, when a program is written that makes heavy use of
unification, it is likely to be extremely efficient."

9
lpn/ch02/proof22.pl Normal file
View File

@@ -0,0 +1,9 @@
f(a).
f(b).
g(a).
g(b).
h(b).
k(X) :- f(X), g(X), h(X).