cleanup handling of attributed variables:

- try to make it clear when to call project
- try to make it clear when to generate goals for attributed variables (get_goalist_from_attvars).
- change call_residue to rely this infrastructure and then add extra variables.
- change frozen never to call project.
This commit is contained in:
Vitor Santos Costa 2009-05-16 12:00:56 -07:00
parent d333de262e
commit 1fe1b19534
2 changed files with 118 additions and 74 deletions

View File

@ -524,12 +524,7 @@ true :- true.
\+ '$undefined'(bindings_message(_,_,_), swi), \+ '$undefined'(bindings_message(_,_,_), swi),
swi:bindings_message(V, LGs, []), !. swi:bindings_message(V, LGs, []), !.
'$output_frozen'(G,V,LGs) :- '$output_frozen'(G,V,LGs) :-
'$extract_goal_vars_for_dump'(V,LIV), '$project_and_delayed_goals'(G,LGs).
'$show_frozen'(G,LIV,LGs).
'$extract_goal_vars_for_dump'([],[]).
'$extract_goal_vars_for_dump'([[_|V]|VL],[V|LIV]) :-
'$extract_goal_vars_for_dump'(VL,LIV).
% %
% present_answer has three components. First it flushes the streams, % present_answer has three components. First it flushes the streams,

View File

@ -36,23 +36,19 @@
:- assert((extensions_to_present_answer(Level) :- :- assert((extensions_to_present_answer(Level) :-
'$show_frozen_goals'(Level))). '$show_frozen_goals'(Level))).
'$show_frozen'(G,V,LGs) :- '$project_and_delayed_goals'(G,LGs) :-
\+ '$undefined'(all_attvars(LAV), attributes),
attributes:all_attvars(LAV), attributes:all_attvars(LAV),
LAV = [_|_], !, LAV = [_|_], !,
'$convert_to_list_of_frozen_goals'(V,LAV,G,LGs). % SICStus compatible step,
'$show_frozen'(_,_,[]). % just try to simplify store by projecting constraints
% over query variables.
'$project_attributes'(LAV, G),
% now get a list of frozen goals.
attributes:all_attvars(NLAV),
'$get_goalist_from_attvars'(NLAV, LGs).
'$project_and_delayed_goals'(_,[]).
'$convert_to_list_of_frozen_goals'(LIV,LAV,_,NLG) :-
'$project'(LAV,LIV,NLG).
'$get_rid_of_vls'((_-G),G).
'$get_rid_of_vls'((A,B),(NA,NB)) :-
'$get_rid_of_vls'(A,NA),
'$get_rid_of_vls'(B,NB).
% %
% wake_up_goal is called by the system whenever a suspended goal % wake_up_goal is called by the system whenever a suspended goal
% resumes. % resumes.
@ -502,12 +498,7 @@ when(_,Goal) :-
frozen(V, G) :- nonvar(V), !, frozen(V, G) :- nonvar(V), !,
'$do_error'(type_error(variable,V),frozen(V,G)). '$do_error'(type_error(variable,V),frozen(V,G)).
frozen(V, LG) :- frozen(V, LG) :-
'$project'([V],[V],Gs), '$get_goalist_from_attvars'([V], LG).
'$simplify_list_of_frozen_goals'(Gs,LG).
'$simplify_list_of_frozen_goals'([],[]).
'$simplify_list_of_frozen_goals'([(_-G)|Gs],[G|NGs]) :-
'$simplify_list_of_frozen_goals'(Gs,NGs).
'$find_att_vars'([], []). '$find_att_vars'([], []).
'$find_att_vars'([V|LGs], [V|AttVars]) :- attvar(V), !, '$find_att_vars'([V|LGs], [V|AttVars]) :- attvar(V), !,
@ -585,19 +576,9 @@ call_residue_vars(Goal,Residue) :-
copy_term(Term, Copy, Goals) :- copy_term(Term, Copy, Goals) :-
term_variables(Term, TVars), term_variables(Term, TVars),
'$pick_vars_for_project'(TVars,AttVars), '$get_conj_from_attvars'(TVars, Goals0),
'$call_attribute_goals'(AttVars, Goals0, LDs),
'$fetch_delays_and_simplify'(AttVars, LDs, LGs),
'$convert_att_vars'(AttVars, LGs),
copy_term_nat([Term|Goals0], [Copy|Goals]). copy_term_nat([Term|Goals0], [Copy|Goals]).
'$fetch_delays_and_simplify'(AttVars, LDs, LGs) :-
'$fetch_delays'(AttVars, LDs0, LGs),
'$fetch_delays_simplify'(LDs0, LDs).
'$fetch_delays_simplify'(LDs0, LDs0) :- var(LDs0), !.
'$fetch_delays_simplify'([_-G|LDs0], [G|LDs0]) :-
'$fetch_delays_simplify'(LDs0, LDs0).
call_residue(Goal,Residue) :- call_residue(Goal,Residue) :-
var(Goal), !, var(Goal), !,
@ -614,54 +595,48 @@ call_residue(Goal,Residue) :-
copy_term_nat(Goal, NGoal), copy_term_nat(Goal, NGoal),
( '$set_svar_list'(CurrentAttsList), ( '$set_svar_list'(CurrentAttsList),
'$system_catch'(NGoal,Module,Error,'$residue_catch_trap'(Error,OldAttsList)), '$system_catch'(NGoal,Module,Error,'$residue_catch_trap'(Error,OldAttsList)),
'$project_and_delayed_goals'(NGoal,Residue0),
'$call_residue_continuation'(NGoal,NResidue), '$add_vs_to_vlist'(Residue0, Residue),
( '$set_svar_list'(OldAttsList), ( '$set_svar_list'(OldAttsList),
copy_term_nat(NGoal+NResidue, Goal+Residue) copy_term_nat(NGoal+NResidue, Goal+Residue)
; ;
'$set_svar_list'(CurrentAttsList), fail '$set_svar_list'(CurrentAttsList), fail
) )
; ;
'$set_svar_list'(OldAttsList), fail '$set_svar_list'(OldAttsList), fail
). ).
'$add_vs_to_vlist'([], []).
'$add_vs_to_vlist'([G|Residue0], [Vs-G|Residue]) :-
term_variables(G, TVs),
'$pick_att_vars'(TVs, Vs),
'$add_vs_to_vlist'(Residue0, Residue).
% make sure we set the suspended goal list to its previous state! % make sure we set the suspended goal list to its previous state!
'$residue_catch_trap'(Error,OldAttsList) :- '$residue_catch_trap'(Error,OldAttsList) :-
'$set_svar_list'(OldAttsList), '$set_svar_list'(OldAttsList),
throw(Error). throw(Error).
% % make sure we have installed a SICStus like constraint solver.
% goal needs to be in a different procedure to catch suspended goals. '$project_attributes'(_, _) :-
% '$undefined'(modules_with_attributes(_),attributes), !.
'$call_residue_continuation'(Goal,Residue) :- '$project_attributes'(AllVs, G) :-
'$variables_in_term'(Goal,[],LIV),
'$show_frozen'(Goal,LIV,Residue).
'$project'([],_,[]).
'$project'(Vs,_,LGs) :-
% we don't have constraints yet, so we must be talking about delays.
'$undefined'(modules_with_attributes(_),attributes), !,
'$fetch_delays'(Vs, LGs, []).
'$project'([V|LAV], LIV, LDs) :-
attvar(V), !,
attributes:modules_with_attributes(LMods), attributes:modules_with_attributes(LMods),
'$pick_vars_for_project'(LIV, NLIV), term_variables(G, InputVs),
'$project_module'(LMods, NLIV, [V|LAV]), '$pick_att_vars'(InputVs, AttIVs),
'$convert_att_vars'(NLIV, LGs), '$project_module'(LMods, AttIVs, AllVs).
attributes:all_attvars(NLAV),
'$call_attribute_goals'(NLAV, LGs1, LGs),
'$fetch_delays'([V|LAV], LDs, LGs1).
'$pick_vars_for_project'([],[]). '$pick_att_vars'([],[]).
'$pick_vars_for_project'([V|L],[V|NL]) :- attvar(V), !, '$pick_att_vars'([V|L],[V|NL]) :- attvar(V), !,
'$pick_vars_for_project'(L,NL). '$pick_att_vars'(L,NL).
'$pick_vars_for_project'([_|L],NL) :- '$pick_att_vars'([_|L],NL) :-
'$pick_vars_for_project'(L,NL). '$pick_att_vars'(L,NL).
'$project_module'([], _, _). '$project_module'([], _, _).
'$project_module'([Mod|LMods], LIV, LAV) :- '$project_module'([Mod|LMods], LIV, LAV) :-
\+ '$undefined'(project_attributes(LIV, LAV), Mod), current_predicate(Mod:project_attributes/2),
'$execute'(Mod:project_attributes(LIV, LAV)), !, '$notrace'(Mod:project_attributes(LIV, LAV)), !,
attributes:all_attvars(NLAV), attributes:all_attvars(NLAV),
'$project_module'(LMods,LIV,NLAV). '$project_module'(LMods,LIV,NLAV).
'$project_module'([_|LMods], LIV, LAV) :- '$project_module'([_|LMods], LIV, LAV) :-
@ -756,8 +731,8 @@ call_residue(Goal,Residue) :-
'$update_att'(V, G). '$update_att'(V, G).
'$update_att'(V, G) :- '$update_att'(V, G) :-
attributes:get_module_atts(V, prolog(_,Gs)), !, attributes:get_module_atts(V, prolog(_,Gs)), !,
attributes:put_module_atts(V, prolog(_,[G|Gs])). attributes:put_module_atts(V, prolog(_,[G|Gs])).
'$update_att'(V, G) :- '$update_att'(V, G) :-
attributes:put_module_atts(V, prolog(_,[G])). attributes:put_module_atts(V, prolog(_,[G])).
@ -769,7 +744,6 @@ call_residue(Goal,Residue) :-
var(V), var(V),
attributes:get_att(V, prolog, 2, Gs), nonvar(Gs). attributes:get_att(V, prolog, 2, Gs), nonvar(Gs).
'$call_attribute_goals'(NLIV, LGsF, LGs0) :- '$call_attribute_goals'(NLIV, LGsF, LGs0) :-
findall(Mod, ('$all_current_modules'(Mod),current_predicate(Mod:attribute_goals/3)), LMs), findall(Mod, ('$all_current_modules'(Mod),current_predicate(Mod:attribute_goals/3)), LMs),
'$call_attribute_goals'(NLIV, LMs, LGsF, LGs0). '$call_attribute_goals'(NLIV, LMs, LGsF, LGs0).
@ -785,3 +759,78 @@ call_residue(Goal,Residue) :-
'$call_attribute_goals_for_module'(LMs, V, LGsI, LGs0). '$call_attribute_goals_for_module'(LMs, V, LGsI, LGs0).
'$call_attribute_goals_for_module'([_|LMs], V, LGsF, LGs0) :- '$call_attribute_goals_for_module'([_|LMs], V, LGsF, LGs0) :-
'$call_attribute_goals_for_module'(LMs, V, LGsF, LGs0). '$call_attribute_goals_for_module'(LMs, V, LGsF, LGs0).
%
% given a list of attributed variables, generate a conjunction of goals.
%
'$get_conj_from_attvars'(TVars, Goals) :-
'$get_goalist_from_attvars'(TVars, [], GoalList, []),
'$list_to_conjunction'(GoalList, Goals).
'$get_goalist_from_attvars'(TVars, GoalList) :-
'$get_goalist_from_attvars'(TVars, [], GoalList, []).
'$get_goalist_from_attvars'([], _, GoalList, GoalList).
'$get_goalist_from_attvars'([V|TVars], DonesSoFar, GoalListF, GoalList0) :-
'$get_goalist_from_attvar'(V, DonesSoFar, MoreDonesSoFar, GoalListF, GoalListI),
'$get_goalist_from_attvars'(TVars, MoreDonesSoFar, GoalListI, GoalList0).
'$get_goalist_from_attvar'(V, DonesSoFar, MoreDonesSoFar, GoalListF, GoalList0) :- attvar(V), !,
attributes:get_all_atts(V, AllAtts),
'$all_atts_to_goals'(AllAtts, V, DonesSoFar, MoreDonesSoFar, GoalListF, GoalList0).
'$get_goalist_from_attvar'(_, DonesSoFar, DonesSoFar, GoalList, GoalList).
'$all_atts_to_goals'(AllAtts, _, DonesSoFar, DonesSoFar, GoalList, GoalList) :- var(AllAtts), !.
'$all_atts_to_goals'(AllAtts, V, DonesSoFar, MoreDonesSoFar, GoalListF, GoalList0) :-
functor(AllAtts, Mod, _),
arg(1, AllAtts, MoreAtts),
'$attgoals_for_module'(Mod, V, AllAtts, DonesSoFar, IDonesSoFar, GoalListF, GoalListI),
'$all_atts_to_goals'(MoreAtts, V, IDonesSoFar, MoreDonesSoFar, GoalListI, GoalList0).
'$attgoals_for_module'(prolog, V, prolog(_,Gs), DonesSoFar, MoreDonesSoFar, GoalListF, GoalList0) :- !,
% dif, when, freeze
'$attgoals_for_prolog'(Gs, V, DonesSoFar, MoreDonesSoFar, GoalListF, GoalList0).
'$attgoals_for_module'(Mod, V, _Gs, DonesSoFar, DonesSoFar, GoalListF, GoalList0) :-
% SWI, HProlog
current_predicate(Mod:attribute_goals/3), !,
(
'$notrace'(Mod:attribute_goals(V,GoalListF,GoalList0))
->
true
;
GoalListF = GoalList0
).
'$attgoals_for_module'(Mod, V, _, DonesSoFar, DonesSoFar, GoalListF, GoalList0) :-
% SICStus
current_predicate(Mod:attribute_goal/2), !,
(
'$notrace'(Mod:attribute_goal(V,G))
->
GoalListF = [G|GoalList0]
;
GoalListF = GoalList0
).
'$attgoals_for_module'(Mod, V, _, DonesSoFar, DonesSoFar, GoalList, GoalList).
'$attgoals_for_prolog'([], _, DonesSoFar, DonesSoFar, GoalList, GoalList).
'$attgoals_for_prolog'([G|AllAtts], V, DonesSoFar, MoreDonesSoFar, [AttGoal|GoalListI], GoalList0) :-
'$attgoal_for_prolog'(G, Done, AttGoal),
'$not_vmember'(Done, DonesSoFar), !,
'$attgoals_for_prolog'(AllAtts, V, [Done|DonesSoFar], MoreDonesSoFar, GoalListI, GoalList0).
'$attgoals_for_prolog'([_|AllAtts], V, DonesSoFar, MoreDonesSoFar, GoalListI, GoalList0) :-
'$attgoals_for_prolog'(AllAtts, V, DonesSoFar, MoreDonesSoFar, GoalListI, GoalList0).
'$attgoal_for_prolog'('$redo_dif'(Done, X, Y), Done, prolog:dif(X,Y)).
'$attgoal_for_prolog'('$redo_freeze'(Done, V, Goal), Done, prolog:freeze(V,Goal)).
'$attgoal_for_prolog'('$redo_eq'(Done, X, Y, Goal), Done, prolog:when(X=Y,Goal)).
'$attgoal_for_prolog'('$redo_ground'(Done, X, Goal), Done, prolog:when(ground(X),Goal)).
'$not_vmember'(_, []).
'$not_vmember'(V, [V1|DonesSoFar]) :-
V \== V1,
'$not_vmember'(V, DonesSoFar).
'$list_to_conjunction'([], true).
'$list_to_conjunction'([G], G) :- !.
'$list_to_conjunction'([G|GoalList], (G,Goals0)) :-
'$list_to_conjunction'(GoalList, Goals0).