Frage zu Prolog - Warum gibt er zweimal true aus?
-
Wenn ich folgenden Code als Prologdatei abspeichere und in Prolog lade,
verheiratet(adam, eva). verheiratet(EHEFRAU, EHEMANN):-verheiratet(EHEMANN, EHEFRAU),!.
dann erhalte ich bei der Abfrage von:
verheiratet(eva, adam).
wie erwartet, aufgrund der 2. Prämise nur ein TRUE.
Wenn ich aber folgende Abfrage stelle:
verheiratet(adam, eva).
Dann wird zweimal der Wert TRUE geliefert (sofern man nicht Return drückt und nach weiteren Möglichkeiten mit n fragt).
Warum?
Im Prinzip wird mit letzterer Abfrage ja gleich die 1. Prämise (1. Zeile) erfüllt, das wäre das erste TRUE.
Die zweite Prämise, mit den vertauschten Variablen dürfte bei dieser Abfrage aber gar nicht erfüllt werden, weil in dieser Prämise ja die Ehefrau als erster Parameter angegeben ist und daher in der Wenn..Dann Bedingung nur ein False liefern kann, wenn ADAM nun als Frau, also verheiratet(eva, adam) interpretiert wird.
-
Kann ich nicht nachvollziehen. Sonst im Debug Modus nachvollziehen.
-
-
Zweimal True? schrieb:
Da steht nix drin...
- Kein Orginalquellcode
- Kein Info über CompilerMuss du selbst debuggen.
-
Zeus schrieb:
Kann ich nicht nachvollziehen. Sonst im Debug Modus nachvollziehen.
dieses online pl taugt nichts, es bricht vor den alternativen antwortmöglichkeiten ab, zeigt also nicht alles.
-
Zeus schrieb:
Zweimal True? schrieb:
Da steht nix drin...
- Kein Orginalquellcode
- Kein Info über CompilerMuss du selbst debuggen.
das ist ne ausgabe von swi-pl, den quellcode hast du schon.
-
Sieht so aus, ob dein Cut Operator nach True evaluiert.
-
Zeus schrieb:
Sieht so aus, ob dein Cut Operator nach True evaluiert.
Der ist aber mit verheiratet(EHEMANN, EHEFRAU) mit dem AND Operator verknüpft und wenn man für die Variablen die Namen eingibt, dann ergibt verheiratet(eva, adam) false.
Womit trotz Cut Operator, unabhängig davon, ob dieser nun selbst true oder false wird, eigentlich false ausgegeben werden muss.
Zumal der Cut Operator AFAIK nur dann true ist, wenn auch die mit AND Verknüpfte verheiratet(EHEMANN, EHEFRAU) Prämise true ist, denn ansonsten wird CUT eigentlich nicht ausgeführt.
-
Das hier:
false,!.
ergibt z.B. false.
-
Mit trace. (Debug Mode) sieht's übrigens so aus:
?- trace | . true. [trace] ?- verheiratet(eva, adam). Call: (6) verheiratet(eva, adam) ? creep Call: (7) verheiratet(adam, eva) ? creep Exit: (7) verheiratet(adam, eva) ? creep Exit: (6) verheiratet(eva, adam) ? creep true. [trace] ?- verheiratet(adam, eva). Call: (6) verheiratet(adam, eva) ? creep Exit: (6) verheiratet(adam, eva) ? creep true ; Redo: (6) verheiratet(adam, eva) ? creep Call: (7) verheiratet(eva, adam) ? creep Call: (8) verheiratet(adam, eva) ? creep Exit: (8) verheiratet(adam, eva) ? creep Exit: (7) verheiratet(eva, adam) ? creep Exit: (6) verheiratet(adam, eva) ? creep true. [trace] ?-
Zeile 5-6 ist klar.
Zeile 12-15 auch, da findet er die Prämise in Zeile 1 des Quelltextes.
In Zeile 16 ruft er dann die Prämise von Zeile 2 des Quelltextes auf, dieser Vertauscht die Namen und sucht dann in 17 nach (eva, adam).
Warum er in 18 nun wieder (adam, eva) aufruft ist mir nicht klar, ich würde erwarten, das er nach Zeile 17 mit einem EXIT und false aussteigt.
-
Wieso soll Prolog bei der Suche nach der Lösung, deine Regel nicht zwei mal anwenden?
-
Hier hat jemand den Cut-Operator nicht verstanden. Oder anders: Warum wird er hier ueberhaupt benutzt?
-
wow absolut awesome das ihr euch mit so einer alten language auskennt
-
C ist genauso alt.
-
Prolog gehoert zu einer Informatikerausbildung dazu.
-
true / false schrieb:
wow absolut awesome das ihr euch mit so einer alten language auskennt
Die Sprache ist komplett anders als das was du so kennst.