aboutsummaryrefslogtreecommitdiff
path: root/sem7/pp/prolog/lec4.pl
blob: 36d3880b75078c9d8d75547183e385504746ffa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
last([X], X).
last([_|XS], X) :- last(XS, X).

/*
last([1,2,3], X) # Bruger regel 2
last([2,3], Y) # Bruger regel 2
last([3], Z) # Bruger regel 1, så Z = 3
# Så Y = Z = 3
# Så X = Y = Z = 3


Okay det er også bare fint at skrive
last([1,2,3],X)
     |
last([2,3],X)
     |
last([3], X)
*/

attach([], E, [E]).
attach([X|XS], E, [X|R]) :- attach(XS, E, R).


/*
Proof search of attach

attach([1,2], a, L)
        |      X = 1, XS = [2], E = a, L = [1|L1]
attach([2], a, L1)
        |      X1 = 2, XS1 = [], E1 = a, L1 = [2|L2]
attach([], a, L2)
        |      E2 = a, L2 = [a]
*/

nat(zero).
nat(succ(X)) :- nat(X).

after(succ(X), X).

leq(zero, Y) :- nat(Y).
leq(succ(X), succ(Y)) :- leq(X, Y), nat(X), nat(Y).

add(X, zero, X) :- nat(X).
add(X, succ(Y), succ(R)) :- add(X, Y, R), nat(X), nat(Y), nat(R).

sub(X, Y, R) :- add(R, Y, X).

min(X, Y, X) :- leq(X, Y).
min(succ(X), Y, Y) :- leq(Y, X).