% Natural numbers and operations: complete axiomatizations. % nat(X) iff X is a natural number nat(0). nat(s(N)) :- nat(N). % sum(X,Y,Z) iff Z = X + Y sum(0,Y,Y). sum(s(X),Y,s(Z)) :- sum(X,Y,Z). % product(X,Y,P) iff P = X * Y product(0,X,0). product(s(X),Y,P) :- product(X,Y,Z), sum(Z,Y,P). % lessthan(X,Y) iff X < Y. lessthan(0,s(X)). lessthan(s(X),s(Y)) :- lessthan(X,Y). % Programming with Prolog's built- arithametics % fact(N,M) will succeed with M = N!, if N is not a free variable initially fact(N,1) :- N =< 1. fact(N,M) :- N > 1, N2 is (N - 1), fact(N2,M2), M is (M2 * N). sumto(1,1). sumto(N,S) :- N > 1, N2 is (N - 1), sumto(N2,S2), S is (S2 + N). % Tradeoffs: % ?- sum(s(s(0)),Y,s(s(s(s(s0))))). will return % Y = s(s(s0)) , but: % ?- 2 + Y = 5 will return % no. % "=" in Prolog is (usualy) only syntactic pattern matching. % The point in this demonstration is that, though all programming, and % programming languages, are based in one way or another on symbolic % logic, various "trade-offs" has been made, especially in "imperative" % languages such as C - to make programming more efficient and convenient % at the expense of logical clarity. Even Prolog must make compromises % to make itself friendlier to humans. Reading "s(s(s(0)))" is fine % for computers, but "3" is much easier for us.