YAP 7.1.0
eval.yap
Go to the documentation of this file.
1/*************************************************************************
2* *
3* YAP Prolog *
4* *
5* Yap Prolog was developed at NCCUP - Universidade do Porto *
6* *
7* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8* *
9**************************************************************************
10* *
11* File: eval.yap *
12* Last rev: *
13* mods: *
14* comments: optimise disjunction handling *
15* *
16*************************************************************************/
17
18/**
19 * @file eval.yap
20 * @author VITOR SANTOS COSTA <vsc@VITORs-MBP-2.lan>
21 * @date Thu Oct 19 11:52:48 2017
22 *
23 * @brief Compiling expressions
24 *
25 * @defgroup CompiledExpressions Compiled Form of Arithmetic Expressions in Prolog
26 * @ingroup drectives
27 *
28 *
29*/
30:- system_module( '$_eval', [], ['$full_clause_optimisation'/4]).
31
32:- new_variables_in_term/3variables_within_term/3use_system_module( terms, [,
33 ]).
34
35:- multifile '$full_clause_optimisation'/4.
36
37
38'$add_extra_safe'('$plus'(_,_,V)) --> '$add_extra_safe', [V].
39'$add_extra_safe'('$minus'(_,_,V)) --> '$add_extra_safe', [V].
40'$add_extra_safe'('$times'(_,_,V)) --> '$add_extra_safe', [V].
41'$add_extra_safe'('$div'(_,_,V)) --> '$add_extra_safe', [V].
42'$add_extra_safe'('$and'(_,_,V)) --> '$add_extra_safe', [V].
43'$add_extra_safe'('$or'(_,_,V)) --> '$add_extra_safe', [V].
44'$add_extra_safe'('$sll'(_,_,V)) --> '$add_extra_safe', [V].
45'$add_extra_safe'('$slr'(_,_,V)) --> '$add_extra_safe', [V].
46'$add_extra_safe'(C=D,A,B) :-
47 '$add_extra_safe',
48 ( compound(C) ->
49 '$variables_in_term'(C,E,A)
50 ;
51 E=A
52 ),
53 ( compound(D) ->
54 '$variables_in_term'(D,B,E)
55 ;
56 B=E
57 ).
58'$add_extra_safe'(_) --> [].
59
60
61'$gen_equals'([], [], _, O, O).
62'$gen_equals'([V|Commons],[NV|NCommons], LV0, O, NO) :- V == NV, '$gen_equals',
63 '$gen_equals'(Commons,NCommons, LV0, O, NO).
64'$gen_equals'([V|Commons],[NV|NCommons], LV0, O, OO) :-
65 '$vmember'(V,LV0),
66 OO = (V=NV,'$safe'(NV),NO),
67 '$gen_equals'(Commons,NCommons, LV0, O, NO).
68'$gen_equals'([V|Commons],[NV|NCommons], LV0, O, OO) :-
69 OO = (V=NV,NO),
70 '$gen_equals'(Commons,NCommons, LV0, O, NO).
71
72'$safe_guard'((A,B), M) :- '$safe_guard',
73 '$safe_guard'(A, M),
74 '$safe_guard'(B, M).
75'$safe_guard'((A;B), M) :- '$safe_guard',
76 '$safe_guard'(A, M),
77 '$safe_guard'(B, M).
78'$safe_guard'(A, M) :- '$safe_guard',
79 '$safe_builtin'(A, M).
80
81'$safe_builtin'(G, Mod) :-
82 '$predicate_flags'(G, Mod, Fl, Fl),
83 Fl /\ 0'$predicate_flags' =\= 0.
84
85'$vmember'(V,[V1|_]) :- V == V1, '$vmember'.
86'$vmember'(V,[_|LV0]) :-
87 '$vmember'(V,LV0).
88
89
90'$localise_disj_vars'((B;B2), M, (NB ; NB2), LV, LV0, LEqs) :- '$localise_disj_vars',
91 '$localise_vars'(B, M, NB, LV, LV0, LEqs),
92 '$localise_disj_vars'(B2, M, NB2, LV, LV0, LEqs).
93'$localise_disj_vars'(B2, M, NB, LV, LV0, LEqs) :-
94 '$localise_vars'(B2, M, NB, LV, LV0, LEqs).
95
96'$localise_vars'((A->B), M, (A->NB), LV, LV0, LEqs) :-
97 '$safe_guard'(A, M), '$safe_guard',
98 '$variables_in_term'(A, LV, LV1),
99 '$localise_vars'(B, M, NB, LV1, LV0, LEqs).
100'$localise_vars'((A;B), M, (NA;NB), LV1, LV0, LEqs) :- '$localise_vars',
101 '$localise_vars'(A, M, NA, LV1, LV0, LEqs),
102 '$localise_disj_vars'(B, M, NB, LV1, LV0, LEqs).
103'$localise_vars'(((A,B),C), M, NG, LV, LV0, LEqs) :- '$localise_vars',
104 '$flatten_bd'((A,B),C,NB),
105 '$localise_vars'(NB, M, NG, LV, LV0, LEqs).
106'$localise_vars'((!,B), M, (!,NB), LV, LV0, LEqs) :- '$localise_vars',
107 '$localise_vars'(B, M, NB, LV, LV0, LEqs).
108'$localise_vars'((X=Y,B), M, (X=Y,NB1), LV, LV0, LEqs) :-
109 var(X), var(Y), var,
110 '$localise_vars'(B, M, NB1, LV, LV0, [X,Y|LEqs]).
111'$localise_vars'((G,B), M, (G,NB1), LV, LV0, LEqs) :-
112 '$safe_builtin'(G, M), '$safe_builtin',
113 '$variables_in_term'(G, LV, LV1),
114 '$add_extra_safe'(G, NLV0, LV0),
115 '$localise_vars'(B, M, NB1, LV1, NLV0, LEqs).
116'$localise_vars'((G1,B1), _, O, LV, LV0, LEqs) :- '$localise_vars',
117 '$localise_vars':variables_within_term(LV, B1, Commons),
118 variables_within_term:new_variables_in_term(LV, B1, New),
119 copy_term(Commons+New+LEqs+B1, NCommons+NNew+NLEqs+NB1),
120 NNew = New,
121 NLEqs = LEqs,
122 '$gen_equals'(Commons, NCommons, LV0, (G1,NB1), O).
123'$localise_vars'(G, _, G, _, _, _).
124
125'$flatten_bd'((A,B),R,NB) :- '$flatten_bd',
126 '$flatten_bd'(B,R,R1),
127 '$flatten_bd'(A,R1,NB).
128'$flatten_bd'(A,R,(A,R)).
129
130% the idea here is to make global variables in disjunctions
131% local.
132'$localise_vars_opt'(H, M, (B1;B2), (NB1;NB2)) :-
133 '$variables_in_term'(H, [], LV),
134 '$localise_vars'(B1, M, NB1, LV, LV, []),
135 '$localise_disj_vars'(B2, M, NB2, LV, LV, []).
136
137
138%, portray_clause((H:-BF))
139'$full_clause_optimisation'(_H, _M, B, B).
140%:-
141% '$localise_vars_opt'(H, M, B0, BF), !.
142
copy_term(? TI,- TF)
variables_within_term(+ Variables,? Term, - OutputVariables)
new_variables_in_term(+_CurrentVariables_, ? Term, -_Variables_)
compound( T)
var( T)