/* CS 1621 Fall 2005 -- This handout demonstrates how facts and rules can be dynamically asserted into and removed from the db. When dealing with rules, the functor that you assert is actually the :- functor. The head of the rule is the first argument, and the tail is the second. If the tail has some subgoals (i.e. commas) then you need to use the comma (,) operator to combine them into a single goal. See details in the comments below. */ :-assertz(man(joe)). :-assertz(man(fred)). :-assertz(man(herman)). :-assertz(woman(edna)). :-assertz(woman(rita)). :-assertz(woman(sarah)). :-assertz(child(tommy)). :-assertz(child(jill)). /* rule1 asserts into the database using literals. Note that the functor is :- and the arguments are the head and the tail */ rule1 :- assertz(:-(person(X), man(X))). /* rule2 asserts into the database using list notation and the =.. predicate. */ rule2 :- L = [:-, person(X), woman(X)], P=..L, assertz(P). /* rule3 is more complicated, but it is again doing basically the same thing. This time, two functors are created, each with 1 argument, for the head and tail of the rule. Their arguments are matched up (so that in the final rule the same variable is indicated in both the head and tail). Then functor P is created with 2 arguments, with the left matching functor Left and the right matching functor Right. Finally, the rule is asserted. */ rule3 :- functor(Left, person, 1), functor(Right, child, 1), arg(1, Left, X), arg(1, Right, X), functor(P, :-, 2), arg(1, P, Left), arg(2, P, Right), assertz(P). /* In rule4, we have subgoals in the tail, so the comma operator is used */ rule4 :- assertz(:-(person(X), ','(pinocchio(X), magic(X)))). /* Note now when retracting, we are not looking for the functor person, but, rather, the functor :- having a first argument of person */ remove :- retract(:-(person(_), _)), fail. remove.