/* consult('examples.prolog') . * and by saying for CLPR: reconsult("examples.prolog") . for GProlog: consult('examples.prolog') . */ /* parent(child, parent) . */ parent(Child, Parent) :- father(Child, Parent) . parent(X, Y) :- mother(X, Y) . grandparent(X, Z) :- parent(X, Y), parent(Y, Z) . ancestor(X, Z) :- parent(X, Z) . ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z). sibling(X, Y) :- mother(X, M), mother(Y, M), father(X, F), father(Y, F), X \= Y . father(cindy, george) . father(george, albert) . father(jeffrey, albert) . father(mary, john). father(victor, george) . mother(cindy, mary). mother(george, alice) . mother(jeffrey, alice) . mother(mary, sue) . mother(victor, mary) . married(H,W) :- father(X, H) , mother(X, W) . /* try these queries: grandparent(victor, albert) . grandparent(albert, victor) . sibling(X, jeffrey) . ancestor(cindy, X) . ancestor(cindy, X) , sibling(X, jeffrey) . sibling(A, B) . grandparent(X, john) , parent(X, Y), sibling(Y, jeffrey) . */ /* lists */ p([1,2,3,4]) . /* queries: p(Y) . p([X | Y]) . p([_, _, X | Y]) . */ /* append of lists */ /* append([], [], []) . append([], [H | T], [H | T]) . append([X|L1], L2, [X | L3]) :- append(L1, L2, L3) . */ append([], B, B) . append([X|Y], B, [X|I]) :- append(Y, B, I) . /* queries: append([1,2], [3,4], [1,2,3,4]) . append([1,2], [3, 4], S) . append([1,A], [3, 4], [1,2,3,4]) . append([1,A], Y, [1,2,3,4]) . append([7,A], Y, [1,2,3,4]) . append(X, Y, [1,2,3,4]) . append([1|X], B, [2 | Y]) . append(X, Y, Z) . */ /* member of */ memberOf(X, [X|_]) . memberOf(X, [_|Y]) :- memberOf(X, Y) . /* queries: memberOf(4, [1,2,3]) . memberOf(4, [1,2,4]) . memberOf(X, [1,2,3]) . memberOf(2, [1,3|X]) . memberOf(2, X) . */ newMemberOf(X, L) :- append(_, [X | _], L) . /* queries: newMemberOf(4, [1,2,3]) . newMemberOf(4, [1,2,4]) . newMemberOf(X, [1,2,3]) . newMemberOf(2, [1,3|X]) . newMemberOf(2, X) . */ /* defining natural numbers */ sum(0, X, X) . sum(succ(X), Y, succ(Z)) :- sum(X, Y, Z) . dif(X, Y, Z) :- sum(Z, Y, X) . /* queries: sum(succ(succ(0)), succ(succ(succ(0))), A) . sum(succ(0), X, succ(succ(succ(0)))) . sum(X, succ(0), succ(succ(succ(0)))) . dif(succ(0), succ(0), Y) . dif(succ(0), succ(succ(0)), Y) . sum(X, Y, 0) . sum(X, Y, succ(0)) . sum(X, Y, succ(succ(0))) . X = 2 + 3 . X is 2 + 3 . X = Y + 3 , Y < 3 , X = 5.4 . X is Y + 3 , Y < 3 , X is 5.4 . */ /* defining Lisp functions */ car(cons(X, _), X) . cdr(cons(_, L), L) . list(nil) . list(cons(_, L)) :- list(L). null(nil) . /* queries: car(cons(cons(a, cons(b, nil)), cons(c, cons(d, nil))), X) . */ /* binary tree generation */ bintree(0, nil) . /* empty tree */ bintree(N, tree(Left,Right)) :- upto(1, N, Root), /* root */ L is Root-1, bintree(L, Left), R is N-Root, bintree(R, Right) . upto(M, N, M) :- M =< N. upto(M, N, Answer) :- M < N, Next is M+1, upto(Next, N, Answer) . allTree(N) :- bintree(N, Tree), print(Tree), nl, fail . /* queries: allTree(3). */ /* differentiation rules, using structures */ /* d(variable name, formula, derivative) */ d(X, C, 0) :- atomic(C) , C \= X . d(X, X, 1) . d(X, plus(U,V), plus(DU,DV)) :- d(X, U, DU), d(X, V, DV) . d(X, minus(U,V), minus(DU,DV)) :- d(X, U, DU), d(X, V, DV) . d(X, times(U,V), plus(times(DU,V),times(DV,U))) :- d(X, U, DU), d(X, V, DV) . /* queries: d(X, X, A) . d(X, 0, A) . d(X, plus(X,X), A) . */ /* equality */ equal(X, X) . /* queries equal(f(Y), Y) . equal(Y, f(Y)) . */ /* naive sort */ naiveSort(Old, New) :- permute(Old, New), sorted(New) . permute([],[]) . permute(L, [H|T]) :- append(V, [H|U], L), append(V, U, W) , permute(W, T) . sorted([]). sorted([_]). sorted([A, B | T]) :- A =< B, sorted([B|T]) . /* queries permute([1,2,3], A) . naiveSort([1,3,2],X) . naiveSort(X, [1,2,3]) . */ /* termination issues */ odd(1) . odd(N) :- odd(M), N is M+2. /* queries odd(5) . odd(2) . */ /* control programming */ memberOf3(X, [X|_]) :- ! . memberOf3(X, [_|Y]) :- memberOf3(X, Y) . /* queries memberOf(3, [3,4,3,3,5]) . memberOf3(3, [3,4,3,3,5]) . memberOf3(4, [3,4,3,3,4]) . */ /* while loops */ while(X) :- cond(X), body(X), fail . cond(X) :- memberOf(X, [1,2,3,7,1]) . body(X) :- print(X), print(' '). /* queries while(_) . */ /* factorial: more control programming */ fact(0, 1) . fact(N, F) :- nonvar(N), N > 0, M is N-1, fact(M, G), F is N*G . fact(N, F) :- var(N), nonvar(F), fact(N, G), F =< G, ! , F = G . fact(N, F) :- var(N), var(F), fact(M, G), N is M+1, F is N*G . /* queries fact(3,5) . fact(3,6) . fact(4, X) . fact(X, 6) . fact(X, 7) . fact(Small, Big) . */ /* negations: see my text page 290 */ not(C) :- call(C), !, fail. not(C) . mother(nora, fatima) . /* queries not(mother(cindy, jaleh)) . not(mother(cindy, mary)) . not(mother(X, jaleh)) . not(mother(X, mary)) . */ /* metaprogramming: see my text page 297 */ /* assert/retract */ allow(X) :- asserta(person(zul)) , asserta(person(kealoha)), person(X) . deny(X) :- retract(person(X)) . /* queries person(X) . allow(fred) . person(X) . deny(beruria) . person(X) . deny(zul) . person(X) . deny(kealoha) . */ /* impure programming */ squares_to(N) :- asserta(current(1)) , repeat, one_step, M is N+1, current(M). one_step :- current(K), Sq is K*K, print(Sq), nl, NewK is K+1, retract(current(K)), asserta(current(NewK)) , ! . repeat . repeat :- repeat . /* queries squares_to(4) . */ /* vim:filetype=prolog nospell: */