% Matthew Stone % DCS 440, Fall 2000 % "Prolog-in-Prolog" explanation code. % % We use terms to represent conditions that may % hold or not hold in the world. We specify % that a condition C applies in the world % explicitly using a relation holds, as in % % holds(C). :- discontiguous(holds/1). % We indicate a regular relationship between % conditions in the world using a relation % rule(H,B). :- discontiguous(rule/2). % This means that whenever all % the conditions in the list B hold, then the % condition H must hold, too. % % Using these relationships, we can infer that % a condition holds directly or indirectly in % one of two ways. Either the condition is % specified to hold explicitly; or there is a % rule that could establish the condition, and % all of the conditions in the body of the rule % hold (directly or indirectly). The following % two clauses formalize this. prove(C) :- holds(C). prove(C) :- rule(C, B), prove_all(B). prove_all([]). prove_all([C|R]) :- prove(C), prove_all(R). % Now, the idea behind explanation is that we % can introduce hypothesetical conditions % in proving something in addition to the % conditions that we explicitly know. % We model this using a relation dprove(A,I,H) % true if H is a list of hypotheses that extends % the list of hypotheses I, and where we can use % each new hypothesis in H (together with the % hypotheses from I) to establish that condition % A holds. For this we have the same original % cases, plus we also have cases where we make % a new assumption and reuse and existing % assumption. dprove(A,Hs,[A|Hs]) :- able_to_assume(A). dprove(A,Hs,Hs) :- member(A,Hs). dprove(A,Hs,Hs) :- holds(A). dprove(A,Hs,Os) :- rule(A,Body), dproveall(Body,Hs,Os). dproveall([],Hs,Hs). dproveall([A|R],Hs,Os) :- dprove(A,Hs,Ms), dproveall(R,Ms,Os). % Here's a slight variant of dprove that can % reuse old assumptions but cannot make new % assumptions: aprove(A,Hs) is true if condition % A follows from what we know together with % the list of hypotheses H. aprove(A,Hs) :- member(A,Hs). aprove(A,_Hs) :- holds(A). aprove(A,Hs) :- rule(A,Body), aproveall(Body,Hs). aproveall([],_Hs). aproveall([A|R],Hs) :- aprove(A,Hs), aproveall(R,Hs). % aprove allows us to discover whether % a set of assumptions is inconsistent. % We use the relation conflict(U) to % indicate that it is impossible for all % of the conditions listed in U to be true % at the same time. An explanation is % inconsistent if it does require all of % the conditions in some conflict to be true. inconsistent(H) :- conflict(U), aproveall(U,H). % We can explain a list of observations R by % coming up with a consistent list of hypotheses % that together explain the explanations. explain(R,H) :- dproveall(R,[],H), \+ inconsistent(H). member(A,[A|_]). member(A,[_|R]) :- member(A,R). % Here is a formalization of the intelligent % recommendation example from class. We don't % know the user's preferences to start out, % but we can asssume that the agent is interested % in particular topics and that the agent finds % articles that only touch on a limited list of % topics boring. able_to_assume(interested_in(_Agent,_Topic)). able_to_assume(bored(_Agent,_Topics)). % We can observe what the user does -- % selecting some articles and rejecting others. % A user will select an article about a topic % they are interseted in; they will reject an % article all of whose topics bore them. rule(selects(Agent,Article), [about(Article,Topics), member(Topic,Topics), interested_in(Agent,Topic)]). rule(rejects(Agent,Article), [about(Article,Topics), bored(Agent,Topics)]). % We axiomatize the member condition here. holds(member(T,[T|_])). rule(member(T,[_|R]), [member(T,R)]). % No agent is interested in anything that bores them. conflict([interested_in(Agent,Topic), bored(Agent,Topics), member(Topic,Topics)]). holds(about(article94,[ai,skiing])). holds(about(article34,[ai,infobahn])). holds(about(article57,[ai,cricket])). % Queries to try: % % explain([selects(u,article94)], H). % explain([selects(u,article94), selects(u,article34)], H). % explain([selects(u,article94), selects(u,article34), rejects(u,article57)], H).