Anthony Gauthier
Anthony Gauthier

Reputation: 21

Analyze rule body in different rule

I need to be able to access the body of a rule in a different rule. For example, in the following, I want to be able to use the facts of rule in myRule when I call myRule(rule).

rule :-
    fact1(...),
    fact2(...),
    fact3(...).


myRule(RuleName) :-
    RuleName :- (F1, F2, F3),
    write(F1).

Obviously the above code does not work, and I have no idea of how to go about this, so I am asking for tips or anything to get me going in the right direction.

Please note that I am very new to Prolog and to logic programming in general. I'm having a hard time finding answers since I am not sure what exactly to look for.

Upvotes: 0

Views: 43

Answers (1)

Jan Wielemaker
Jan Wielemaker

Reputation: 1720

Calling clause(Head, Body) allows you to inspect rules. Thus, clause(rule, (F1,F2,F3)) should succeed, binding F1, etc. Note that the standard demands the predicate to inspect to be declared dynamic, SWI-Prolog doesn't enforce that.

Upvotes: 2

Related Questions