Skip to content

Instantly share code, notes, and snippets.

@cth
Created March 26, 2009 13:59
Show Gist options
  • Save cth/86110 to your computer and use it in GitHub Desktop.
Save cth/86110 to your computer and use it in GitHub Desktop.
% Combine several rules into a single one.
cl1(a, b, X) :- X is 2+2.
cl1(a, c, X) :- X is 4+4.
head_condition([],[],true).
head_condition([Parameter|RestParams], [RefParam|RestRefParams], (Cond, CondRest)) :-
Cond = (RefParam = Parameter),
head_condition(RestParams,RestRefParams, CondRest).
embed_clauses(_, [], fail).
embed_clauses(RefParams, [Clause|ClauseRest], (EmbedClause ; EmbedClausesRest)) :-
Clause = [ Params, Body ],
head_condition(Params, RefParams, Condition),
EmbedClause = (Condition -> Body),
embed_clauses(RefParams, ClauseRest, EmbedClausesRest).
combine_clauses_matching(Head, NewRule) :-
Head =.. [ ClauseName | Params ],
length(Params, ParamLength),
length(ReferenceParams, ParamLength),
NewHead =.. [ ClauseName | ReferenceParams ],
findall([Params, Body], clause(Head, Body), Clauses),
embed_clauses(ReferenceParams, Clauses, NewBody),
NewRule =.. [ :-, NewHead, NewBody].
test :-
combine_clauses_matching(cl1(a,_,_), NewRule),
portray_clause(NewRule).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment