-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeano.pl
67 lines (43 loc) · 1009 Bytes
/
peano.pl
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% peano numbers:
% zero (0 in N)
% s(zero) (1 in N)
% ...
encode(zero, 0).
% encode(s(zero), 1).
% encode(s(s(zero)), 1).
% ...
encode(s(X), N) :-
encode(X, TN),
N is TN + 1.
len(zero, 0).
len(s(X), R) :-
len(X, TR),
R is TR + 1.
% in peano arithmetic
% x + zero = x
% x + s(y) = s(x+y)
add(X, zero, X).
% add(X, s(Y), R) :-
% add(X, Y, TR),
% R = s(TR).
add(X, s(Y), s(R)) :- add(X, Y, R).
addn(N1, N2, R) :-
encode(P1, N1),
encode(P2, N2),
add(P1, P2, PR),
encode(PR, R).
sub(X, zero, X).
sub(X, s(Y), R) :- sub(X, Y, s(R)).
mul(_, zero, zero).
mul(X, s(Y), R) :- mul(X, Y, RM), add(RM, X, R).
div(X, Y, _) :-
less_than(X, Y), !.
div(X, Y, s(R)) :-
sub(X, Y, D), div(D, Y, R).
mod(X, Y, R) :-
less_than(X, Y), R = X, !.
mod(X, Y, R) :-
sub(X, Y, D), mod(D, Y, R).
greater_than(X, Y) :- len(X, LX), len(Y, LY), LX > LY.
less_than(X, Y) :- len(X, LX), len(Y, LY), LX < LY.
eq(X, Y) :- len(X, L), len(Y, L).