Forward Chaining in Prolog - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Forward Chaining in Prolog

Description:

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)))) ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 7
Provided by: timf83
Category:

less

Transcript and Presenter's Notes

Title: Forward Chaining in Prolog


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
  • AgtB 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)gtQ) - !, (P1gt(P2gtQ)).
  • LHS is a disjunction
  • ((P1P2)gtQ) - !, (P1gtQ), (P2gtQ).
  • LHS is atomic. trigger already in DB?
  • (PgtQ) - ifAdded(P,Q), !.
  • LHS is atomic. Add trigger to DB
  • (PgtQ) -
  • assert(ifAdded(P,Q)),
  • foreach(clause(P,true),fcDo(Q)).

4
If-removed rules
  • A/gtB adds ifRemoved rule to execute B whenever
    the
  • assertions in A have all been removed from the
    database.
  • ((P1,P2)/gtQ) - !, (P1/gt(P2/gtQ)).
  • ((P1P2)/gtQ) - !, (P1/gtQ), (P2/gtQ).
  • (P/gtQ) - assert(ifRemoved(P,Q)).
  • (gtX) - add(X).

5
A test file
  • FCTEST
  • - spouse(P1,P2) gt add(spouse(P2,P1)).
  • - spouse(P1,P2) /gt remove(spouse(P2,P1)).
  • - gt spouse(adam,eve)).
  • - spouse(X,Y), female(X) gt add(wife(X,Y)).
  • - gt female(eve).
  • - female(X), male(X) gt add(contradiction(female(
    X), male(X))).
  • - contradiction(P,Q) gt format(CONTRADICTION
    w and w.\n, P,Q).

6
Fc in action
  • ?- 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)gtadd(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)gtadd(d(A)))).
  • ifAdded(b(1), (c(1)gtadd(d(1)))).
  • ifAdded(c(1), add(d(1))).
  • ?- add(c(1)).
  • Adding c(1).
  • Adding d(1).
  • Yes
  • ?- b(X) gt 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)gtadd(d(A)))).
  • ifAdded(b(1), (c(1)gtadd(d(1)))).
  • ifAdded(c(1), add(d(1))).
  • ifAdded(b(A), (add(foo(A)),add(bar(A)))).
  • yes
Write a Comment
User Comments (0)
About PowerShow.com