[mercury-users] About unification and AI
Maurizio Colucci
seguso.forever at tin.it
Fri Apr 23 21:15:28 AEST 2004
Hello again,
I still don't understand whether Mercury is more or less suited for my AI
program than prolog. This is not the videogame, but another program :-)
My AI program is an inferential engine, which has a knowledge base of facts
(called events), a set of rules, and a pattern matching demon that tries to
match facts and rules. If it finds a set of facts that _unify_ with the
precondition of a rule, it infers the consequence of the rule
(instantiated!).
The question is: can I hope to translate this to prolog, or would I better
give up now? Then program makes extensive reuse of the unification capability
of prolog...
More precisely, The KB of events is as follows:
% an event is a GROUND list (i.e. a list of constants), plus an index.
eventDef(1, [he, isFat]), % the first arg is an index, used for nesting events
eventDef(2, [he, use, boomerang]),
eventDef(3, [he, wear, scarf]),
eventDef(4, [jack, say, 3]), % jack says that he wears a scarf
The set of rules is as follows: there is a list of preconditions and a list of
consequences that can be inferred:
% rule/2
% rule( If, Then).
rule([[A, use, B], [B, isBoomerang]], % Preconditions
[[A, throw, B]]). % Consequences
and the pattern matching demon is demon/0, defined as follows:
demon:-
% try all the matchings between events and rules...
forall( (rule(Pre, Inf),
member(P, Pre),
eventDef(_, E),
P = E), % this unification binds variables globally!
% P, Pre, Inf are all changed!
( remove2(P, Pre, Pre2), % obvious
ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences(
Pre2,
(Pre, Inf)))).
ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences([], Rule):-
assertAllInferencesAndDerivations(Rule).
ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences(
Pre, Rule):-
forall( ( member(P, Pre),
eventDef(_, Ev),
P=Ev), % Inference has been instantiated too.
( remove2(P, Pre, Pre2),
ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences(Pre2, Rule))).
ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences(_, _):-
abort(ifThePreconditionsAreAllSatisfiedThenAssertTheConsequences-failed).
abort(X):-
writeln(X),
throw(X).
assertAllInferencesAndDerivations(Rule):-
Rule = (Preconditions, Inferences),
integrityCheck(ground(Preconditions)),
( thisStuffIsAlreadyAsserted(Rule)->true
;
( free_variables(Inferences, FreeVars), % The power of prolog :-)
bindEachFreeVarToANewConstant(FreeVars),
forall(member(M, Inferences),
( newConst(C),
assert(eventDef(ev-C, M)),
forall(member(P, Preconditions),
assert(derivedFrom(M, P))))))).
assertAllInferencesAndDerivations(_):-
abort(assertAllInferencesAndDerivations-should-never-fail).
thisStuffIsAlreadyAsserted(R):-
R = (Pre, Inf),
integrityCheck(ground(Pre)),
% Inf may or may not be ground!
forall(member(E, Inf),
theEventUnifiesWithAnotherAndWithTheSameDerivations(E, Pre)).
theEventUnifiesWithAnotherAndWithTheSameDerivations(E, Pre):-
eventDef(_, E2),
E = E2,
forall(member(P, Pre),
derivedFrom(E2, P)).
bindEachFreeVarToANewConstant([]).
bindEachFreeVarToANewConstant([H|T]):-
integrityCheck(var(H)),
newConst(C), % C is output.
H=c-C,
bindEachFreeVarToANewConstant(T).
newConst(C):-
nextFreeInteger(C),
retractall(nextFreeInteger(_)),
C2 is C+1,
assert(nextFreeInteger(C2)).
Thanks for any comment. Also comments about bad style are very welcome.
Maurizio
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list