-
I am rewriting a GRAPHPLAN algorithm from a text book. There is one predicate I have not been able to fix at all, Big picture, there are terms with the shape Here's how it's used: extract_plan( [ _ ], []).
extract_plan( [ _, ActionLevel | RestOfGraph], Plan) :-
collect_vars( ActionLevel, AVars),
labeling([], AVars),
nonpersist_actions(ActionLevel, Actions),
extract_plan(RestOfGraph, RestOfPlan),
append( RestOfPlan, [Actions], Plan). % book version
collect_vars( [], []).
collect_vars( [ X/V | Rest], Vars) :-
( X \= persist(_), var( V), !, Vars = [V | RestVars ]
; Vars = RestVars
),
collect_vars( Rest, RestVars). % Collect rest of variables I thought I would have enough to do it from this discussion, but when I apply that, some examples do not terminate. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
See https://github.com/mthom/scryer-prolog/discussions/2539#discussioncomment-10579547
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
% T = true if either the functor indicator or arity are different
different_functor_t(A, B, T) :-
functor(A, AFunctor, AArity),
functor(B, BFunctor, BArity),
';'(dif(AFunctor, BFunctor), dif(AArity, BArity), T).
% my attempted rewrite
collect_vars([], []).
collect_vars([X/V|Rest], Vars) :-
if_(different_functor_t(a(_), X),
Vars=[V|RestVars],
Vars=RestVars
),
collect_vars(Rest, Vars). I have also tried: collect_vars([], []).
collect_vars([X/V|Rest], Vars) :-
(
var(V),
different_functor_t(a(_), X, true),
Vars=[V|RestVars]
; Vars=RestVars
),
collect_vars(Rest, Vars). I've also tried: collect_vars(MixedActions, Vars) :-
findall(A, (member(A/_, MixedActions), A=persist(_)), PersistActions),
xs_ys_setdiff(MixedActions, PersistActions, NonPersistActions),
findall(V, (member(_/V, NonPersistActions), var(V)), Vars). but that doesn't terminate either. I must be overlooking something obvious, but I can't spot it. Here's the full context, in case anyone that's important to anyone: https://gist.github.com/jjtolton/38382379e1c5ad58ebfdba561d68e5fc (it's an in-progress rewrite, please consider it a draft) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
@UWN sometimes I wonder if you get tired of answering the same question over and over again 🙄 I reviewed my notes, found your previous answer, combined it with @bakaq 's answer, and it solved many of the problems in the code I'm working. collect_vars([],[]).
collect_vars([X/V|Rest], Vars) :-
functor(X, F, _),
if_(F=persist, Vars=NonPersistVars, Vars=[V|NonPersistVars]),
collect_vars(Rest,NonPersistVars). @triska also already answered this question in his video essay on Writing Prolog Code. The functor part of it really just made me forget everything, apparently! |
Beta Was this translation helpful? Give feedback.
@UWN sometimes I wonder if you get tired of answering the same question over and over again 🙄
I reviewed my notes, found your previous answer, combined it with @bakaq 's answer, and it solved many of the problems in the code I'm working.
@triska also already answered this question in his video essay on Writing Prolog Code. The functor part of it really just made me forget everything, apparently!