=================================================== Beispiele fuer das Arbeiten mit Prolog unter Linux. SWI-Prolog wird aufgerufen mit =================================================== \$ swipl Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 6.6.6) Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- X = a. X = a. ?- f(X,b) = f(a,Y). X = a, Y = b. ?- X = 2*3. X = 2*3. ?- X is 2*3. X = 6. ?- 1+5 = 2*3. false. ?- 1+5 =:= 2*3. true. ?- X is 1+5, X is 2*3. X = 6. ?- X is 1+5, X is 2*4. false. ?- A+B = 2+3+4. A = 2+3, B = 4. ?- 2+3+4 = 2+(3+4). false. ?- 2+3+4 =:= 2+(3+4). true. ?- 2+3+4 = (2+3)+4. true. ?- 2+3+4 = +(+(2,3),4). true. ?- A-B = 2-3+4. false. ?- A-B = 2-3-4. A = 2-3, B = 4. ?- A-B is 2-3-4. false. ?- A is 2-3-4. A = -5. ?- help. true. ?- X =\= 2*3. ERROR: =\=/2: Arguments are not sufficiently instantiated ?- X = 3*7, X =\= 2*3. X = 3*7. ?- X is 3*7, X =\= 2*3. X = 21. ?- write_canonical(2*3). *(2,3) true. ?- write_canonical(2-3+4). +(-(2,3),4) true. ?- write_canonical([a,b,c]). '.'(a,'.'(b,'.'(c,[]))) (in den meisten Prologs; geaendert in true. SWI-Prolog Version 7) ?- [X|Xs] = [1,2,3,4]. X = 1, Xs = [2, 3, 4]. ?- [X,Y|Xs] = [1,2,3,4]. X = 1, Y = 2, Xs = [3, 4]. ?- member(X,[a,c,b,a,e,d]). X = a ; X = c ; X = b ; X = a ; X = e ; X = d. ?- findall(X,member(X,[a,c,b,a,e,d]),L). L = [a, c, b, a, e, d]. ?- findall(f(X),member(X,[a,c,b,a,e,d]),L). L = [f(a), f(c), f(b), f(a), f(e), f(d)]. ?- findall(X-X,member(X,[a,c,b,a,e,d]),L). L = [a-a, c-c, b-b, a-a, e-e, d-d]. ?- findall(f(X,Y), (member(X,[a,b,c]), member(Y,[2,3])), L). L = [f(a, 2), f(a, 3), f(b, 2), f(b, 3), f(c, 2), f(c, 3)]. ?- findall(X,member(X,[a,c,b,a,e,d]),L), sort(L,M). L = [a, c, b, a, e, d], M = [a, b, c, d, e]. ?- setof(X,member(X,[a,c,b,a,e,d]),M). M = [a, b, c, d, e]. ?- findall(X, member(X,[]), L). L = []. ?- setof(X, member(X,[]), L). false. =================================================== Sei das Prolog-Programm verwandtschaft.pl als Datei im aktuellen Verzeichnis vorhanden. Wir kompilieren es mit der Anfrage =================================================== ?- [verwandtschaft]. % verwandtschaft compiled 0.00 sec, 15 clauses true. ?- grossmutter(G,P). G = berta, P = gustav ; G = dora, P = gustav ; false. ?- grossmutter(G,gustav). G = berta ; G = dora ; false. ?- grossmutter(berta,P). P = gustav. ?- grossmutter(berta,dora). false. ?- grossmutter(berta,gustav). true. ?- kind(K,M1), weiblich(M1), kind(K,M2), weiblich(M2), M1 \= M2. false. ?- halt. oder Control-d beendet Prolog