15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog...

24
15.01.09 GK Prolog - Cut, Negation, Dat enbasis 1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf [email protected]

Transcript of 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog...

Page 1: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 1

Cut, Negation, Manipulation der Datenbasis

Prolog Grundkurs WS 08/09

Christof Rumpf

[email protected]

Page 2: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 2

Der Cut

Der Cut „!“ ist ein eingebautes Prädikat, mit dem Backtracking kontrolliert werden kann.

Der Cut kann folgendes bewirken:– Effizienzsteigerung– Speichereinsparung– Kürzere Programme

Page 3: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 3

Wirkungsweise des Cut

– Der Cut wird im Rumpf von Regeln eingesetzt und verhindert Backtracking.

– Der Top-Down-Beweis des Cut gelingt immer. – Gelangt der Interpreter beim Backtracking

zurück zu einem Cut, scheitert der Beweis des Prädikats im Kopf der Regel endgültig - ungeachtet weiterer Backtrackingmöglichkeiten vor dem Cut bzw durch nachfolgende Klauseln.

Page 4: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 4

Generelles Beispiel

A1:- ... . ... Aj:- B1,...,Bi-1,!,Bi+1,..., Bn. ... Am:- ... .

Das Prädikat A ist durch m Klauseln definiert. Ein Beweis des Cut in Aj beschränkt Backtracking auf den

blau umrahmten Bereich.

Page 5: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 5

max/3 mit und ohne Cut

% ohne Cut max(X,Y,X):- X >= Y. max(X,Y,Y):- X < Y.

% mit Cut max(X,Y,X):- X >= Y, !. max(X,Y,Y).

Die Lösung mit Cut macht den Test X < Y für die zweite Klausel überflüssig und verleiht der zweiten Klausel Default-Charakter: Y ist immer das Maximum, außer wenn X größer oder gleich Y ist.

Page 6: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 6

ifthenelse/3

% ohne Cut ifthenelse(B,P,Q):- call(B), call(P). ifthenelse(B,P,Q):- call(not(B)), call(Q).

% mit Cut ifthenelse(B,P,Q):-

call(B), !, call(P). ifthenelse(B,P,Q):- call(Q).

Der Cut macht die nochmalige Prüfung der Bedingung B in der zweiten Klausel überflüssig.

Page 7: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 7

Rote und grüne Cuts

In der Literatur wird oft zwischen roten und grünen Cuts unterschieden.– Ein grüner Cut kann aus einem Programm

entfernt werden, ohne daß sich die Bedeutung des Programms ändert.

– Ein roter Cut kann nicht aus einem Programm entfernt werden, ohne daß sich die Bedeutung des Programms ändert.

Page 8: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 8

max/3 mit grünen und roten Cuts

% grüne Cuts max(X,Y,X):- X >= Y, !. max(X,Y,Y):- X < Y, !.

% roter Cut max(X,Y,X):- X >= Y, !. max(X,Y,Y).

Die grünen Cuts könnten genausogut weggelassen werden.

Page 9: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 9

Ist es ein roter oder grüner Cut?

Die Entscheidung ist nicht immer trivial. Beispiel:

append([],L,L):- !. append([H|T1],L,[H|T2]):- append(T1,L,T2).

Bei einer Anfrage ?- append([1,2],[3,4],L). liefert append/3 mit Cut korrekt L=[1,2,3,4].

Bei einer Anfrage ?- append(X,Y,[1,2,3,4]). bekommen wir allerdings nur noch (deterministisch) die Lösung X=[] und Y=[1,2,3,4] .

Page 10: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 10

Die Schattenseite des Cut

– Der Cut zertört die Deklarativität von Prolog-Programmen.

– Die Interpretation einer Prädikatsdefinition mit roten Cuts ist i.d.R. nur noch unter Berücksichtigung der Reihenfolge der Beweisschritte möglich.

– Deshalb: Cut nur einsetzen, wenn ein offensichtlicher Vorteil erzielt werden kann.

Page 11: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 11

Verwendungsweisen des Cut

Die Verwendung des Cuts kann je nach Anwendungsfall verschieden charakterisiert werden:

– Beschneiden des Suchraums.– Erzwingen von Determinismus.– Modellierung von Defaults.– Modellierung von Negation.

Page 12: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 12

Negation

Negation wird in Prolog mit Hilfe einer Cut-Fail-Kombination realisiert und zur Unterscheidung von der Negation z.B. in der Prädikatenlogik „Negation als Scheitern“ bzw „negation as failure“ genannt. Hier die Definition des eingebauten Prädikats not/1:

not(G):- call(G), !, fail. not(G).

Page 13: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 13

Probleme mit der Negation Negation als Scheitern ist nur dann mit Sicherheit

deklarativ, wenn in dem negierten Beweisziel beim Aufruf keine Variablen enthalten sind.

ledigerStudent(X):- not verheiratet(X), student(X).

student(peter). verheiratet(klaus).

?- ledigerStudent(peter). yes ?- ledigerStudent(X). no

Page 14: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 14

Anwendung: Defaults

can_fly(X):- penguin(X), !, fail. can_fly(X):- bird(X). bird(X):- penguin(X). bird(X):- eagle(X). penguin(tweety). eagle(klaus).

can_fly(X):- bird(X), not penguin(X). bird(X):- penguin(X). bird(X):- eagle(X). penguin(tweety). eagle(klaus).

Cut-Fail-Version (schlecht) Version mit Negation (ok)

Page 15: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 15

Manipulation der Datenbasis

Prolog stellt eine Reihe von Prädikaten zur Verfügung, die eine Manipulation der Datenbasis während der Laufzeit eines Programms ermöglichen.– assert/1– retract/1– clause/2– ...

Page 16: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 16

Erweitern der Datenbasis

– assert(Clause) fügt eine Klausel am Ende der Datenbasis hinzu.

– asserta(Clause) fügt eine Klausel am Anfang der Datenbasis hinzu.

– Beispiel:

?- assert((sterblich(X):-mensch(X))). yes.

Page 17: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 17

Anwendung: Memo-Funktion

Memo-Funktionen dienen zur Speicherung von berechneten Ergebnissen, um eine erneute Berechnung zu vermeiden.

lemma(G):- call(G), asserta((G:- !)).

?- lemma(vorfahr(X,Y)), fail.

Die Anfrage berechnet die Vorfahr-Relation vollständig und schreibt die Ergebnisse mit asserta/1 in die Datenbasis.

Page 18: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 18

Löschen in der Datenbasis

retract(+Clause) entfernt die erste Klausel aus der Datenbasis, die mit Clause unifiziert.

?- retract(mensch(X)). X = sokrates ->; X = aristoteles ->; no ?- retract((sterblich(_):- mensch(_))). yes

Page 19: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 19

Anwendung: Kopieren von Termen

copy_term(Term1,Term2) erstellt in Term2 eine Kopie von Term1 mit neuen Variablen.

copy_term(T1,T2):- assert( ´TMP´(T1)), retract(´TMP´(T2)).

?- copy_term(rel(A,A,B),Copy). Copy = rel(C,C,D), yes

Page 20: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 20

Zugriff auf Klauseln

clause(Head,Body) erlaubt einen Zugriff auf Klauseln. Head muß beim Aufruf instantiiert sein. Body hat bei Fakten die Instanz true.

?-clause(sterblich(_),T). T = mensch(X) ->; ...

Page 21: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 21

Löschen einer Prädikatsdefinition

retractall(H):- % Regeln clause(H,B), retract((H:-B)),

fail. retractall(H):- % Fakten retract(H), fail. retractall(_).

?- retractall(vorfahr(_,_)). yes

Page 22: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 22

name/2

name(Atom,List) wandelt ein Atom in eine Liste von ASCII-Symbolen um und umgekehrt.

?- name(sokrates,L). L = [115,111,107,114,97,116,101,115] yes ?- name(A,[115,111,107,114,97,116,101,115]). A = sokrates yes

Page 23: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 23

Anwendung: Erzeugen neuer Atome

gensym(Root,Symbol) erzeugt bei jedem Auruf ein neues Atom Symbol, indem an eine spezifizierbare Wurzel Root eine fortlaufende Zahl angehängt wird.

?- gensym(x, S). S = x0, yes ?- gensym(x, S). S = x1, yes

Definition

Page 24: 15.01.09GK Prolog - Cut, Negation, Datenbasis1 Cut, Negation, Manipulation der Datenbasis Prolog Grundkurs WS 08/09 Christof Rumpf rumpf@uni-duesseldorf.de.

15.01.09 GK Prolog - Cut, Negation, Datenbasis 24

gensym/2

gensym(Root,Symbol):- symbol(Root,Nr), name(Root,RootL), name(Nr,NrL), append(RootL,NrL,SymbolL), name(Symbol,SymbolL). symbol(Root,Nr):- retract(symb(Root,ONr)),!,Nr is ONr+1,

assert(symb(Root,Nr)). symbol(Root,0):- assert(symb(Root,0)).