1 / 6

Forward Chaining in Prolog

Forward Chaining in Prolog. FC Core. % add(P) adds P to DB and triggers forward chaining rules. add(P) :- clause(P,true), !. add(P) :- dbug("Adding ~p.~n",[P]), assert(P), foreach(ifAdded(P,Actions), call(Actions)). % remove(P) removes P from DB, triggering ifRemoved rules.

ashtyn
Download Presentation

Forward Chaining in Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Forward Chainingin Prolog

  2. FC Core % add(P) adds P to DB and triggers forward chaining rules. add(P) :- clause(P,true), !. add(P) :- dbug("Adding ~p.~n",[P]), assert(P), foreach(ifAdded(P,Actions), call(Actions)). % remove(P) removes P from DB, triggering ifRemoved rules. remove(P) :- dbug("Removing ~p.~n",[P]), retract(P), foreach(ifRemoved(P,Actions), call(Actions)).

  3. Mapping rules into triggers % A=>B adds forward chaining rule to execute B whenever the facts % in A have all been added to the DB. % LHS is a conjunction ((P1,P2)=>Q) :- !, (P1=>(P2=>Q)). % LHS is a disjunction ((P1;P2)=>Q) :- !, (P1=>Q), (P2=>Q). % LHS is atomic. trigger already in DB? (P=>Q) :- ifAdded(P,Q), !. % LHS is atomic. Add trigger to DB (P=>Q) :- assert(ifAdded(P,Q)), foreach(clause(P,true),fcDo(Q)).

  4. If-removed rules % A=/>B adds ifRemoved rule to execute B whenever the % assertions in A have all been removed from the database. ((P1,P2)=/>Q) :- !, (P1=/>(P2=/>Q)). ((P1;P2)=/>Q) :- !, (P1=/>Q), (P2=/>Q). (P=/>Q) :- assert(ifRemoved(P,Q)). (=>X) :- add(X).

  5. A test file % FCTEST :- spouse(P1,P2) => add(spouse(P2,P1)). :- spouse(P1,P2) =/> remove(spouse(P2,P1)). :- => spouse(adam,eve)). :- spouse(X,Y), female(X) => add(wife(X,Y)). :- => female(eve). :- female(X), male(X) => add(contradiction(female(X), male(X))). :- contradiction(P,Q) => format(“CONTRADICTION: ~w and ~w.\n”, [P,Q]).

  6. | ?- [fctest]. Adding spouse(adam,eve). Adding spouse(eve,adam). yes | ?- listing(ifAdded). ifAdded(spouse(A,B), add(spouse(B,A))). ifAdded(a(A), (b(A),c(A)=>add(d(A)))). yes | ?- add(b(1)). Adding b(1). yes | ?- add(a(1)). Adding a(1). yes | ?- listing(ifAdded). ifAdded(spouse(A,B), add(spouse(B,A))). ifAdded(a(A), (b(A),c(A)=>add(d(A)))). ifAdded(b(1), (c(1)=>add(d(1)))). ifAdded(c(1), add(d(1))). yes | ?- add(c(1)). Adding c(1). Adding d(1). Yes | ?- b(X) => add(foo(X)), add(bar(X)). Adding foo(1). Adding bar(1). yes | ?- listing(ifAdded). ifAdded(spouse(A,B), add(spouse(B,A))). ifAdded(a(A), (b(A),c(A)=>add(d(A)))). ifAdded(b(1), (c(1)=>add(d(1)))). ifAdded(c(1), add(d(1))). ifAdded(b(A), (add(foo(A)),add(bar(A)))). yes Fc in action

More Related