Skip to content

Commit e1da738

Browse files
committed
Rewrite arithmetic expansion
1 parent 1de120d commit e1da738

File tree

1 file changed

+25
-44
lines changed

1 file changed

+25
-44
lines changed

src/loader.pl

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,34 @@
4343
catch(loader:G_0, E, (loader:writeln(exception:E:G_0),throw(E))).
4444
w(G_0) :-
4545
writeln(call:G_0), x(G_0), writeln(exit:G_0).
46-
w(_) :-
47-
writeln(done).
4846

49-
%% arithmetic_relation_expanded(?Term, ListDifference).
50-
%
51-
% Recursively traverse Term and assemble a list of replacements that make a
52-
% valid aruthmetic relation.
47+
%% arithmetic_expansion(+Type, ?Term, -ExpandedTerm, ListDifference).
5348
%
5449
% Hand-expanded DCG, because library(dcgs) isn't available during goal expansion.
55-
arithmetic_relation_expanded(T, L1-L2) :-
56-
maplist(loader:elaborate(F/2), [T,R], [Ts,Rs]),
57-
memberchk(F, [is,>,<,>=,=<,=:=,=\=]),
58-
next(ok(Ts,Rs), L1-[R|L2]).
59-
60-
%% next(+Marker, ListDifference).
61-
%
62-
% Describes needed replacements according to `Marker`.
63-
next(nok(T,R), [T=R|L]-L).
64-
next(ok([],[]), L-L).
65-
next(ok([T0|Ts],[R0|Rs]), L1-L3) :-
66-
arithmetic_function_expanded(T0, R0, L1-L2), next(ok(Ts,Rs), L2-L3).
67-
68-
%% arithmetic_function_expanded(?Term, -ExpandedTerm, ListDifference).
69-
arithmetic_function_expanded(T, R, LD) :-
70-
check(T, R, C), next(C, LD).
71-
72-
%% check(?Term, -ExpandedTerm, -Marker).
73-
%
74-
% `Marker` describes what is `Term` in regards to arithmetical function, it is
75-
% `ok/2` if `Term` is Ok to be a part of arithmetical function and it is `nok/2`
76-
% if it isn't.
50+
% Recursively traverse `Term` and assemble a list of replacements that makes up
51+
% a valid arithmetic relation.
7752
%
78-
% NOTE: Order of `check/3` clauses is important for correctness.
79-
check(T, T, ok([],[])) :- (var(T); number(T)), !.
80-
check(T, R, ok(Ts,Rs)) :-
81-
maplist(loader:elaborate(F/A), [T,R], [Ts,Rs]),
82-
arithmetic_functions(A, Fs),
83-
member(F, Fs), !.
84-
check(T, R, nok(T,R)) :- !.
85-
86-
elaborate(Functor/Arity, Term, Args) :-
87-
functor(Term, Functor, Arity),
88-
Term =.. [Functor|Args].
89-
90-
arithmetic_functions(0, [e,pi,epsilon]).
91-
arithmetic_functions(1, [+,-,\,sqrt,exp,log,sin,cos,tan,asin,acos,atan,sign,abs,round,ceiling,floor,truncate,float,float_integer_part,float_fractional_part]).
92-
arithmetic_functions(2, [+,-,/,*,**,^,/\,\/,xor,div,//,rdiv,<<,>>,mod,rem,max,min,gcd,atan2]).
53+
% NOTE: Order of clauses is important for correctness.
54+
arithmetic_expansion(func, T, T, L-L) :-
55+
(var(T); number(T)), !.
56+
arithmetic_expansion(list, [], [], L-L) :- !.
57+
arithmetic_expansion(list, [T0|Ts], [R0|Rs], L1-L3) :-
58+
arithmetic_expansion(func, T0, R0, L1-L2),
59+
arithmetic_expansion(list, Ts, Rs, L2-L3), !.
60+
arithmetic_expansion(Set, T, R, LD) :-
61+
functor(T, F, A),
62+
arithmetic_term(Set, A, Fs),
63+
member(F, Fs), !,
64+
functor(R, F, A),
65+
T =.. [F|Ts],
66+
R =.. [F|Rs],
67+
arithmetic_expansion(list, Ts, Rs, LD).
68+
arithmetic_expansion(func, T, R, [T=R|L]-L).
69+
70+
arithmetic_term(func, 0, [e,pi,epsilon]).
71+
arithmetic_term(func, 1, [+,-,\,sqrt,exp,log,sin,cos,tan,asin,acos,atan,sign,abs,round,ceiling,floor,truncate,float,float_integer_part,float_fractional_part]).
72+
arithmetic_term(func, 2, [+,-,/,*,**,^,/\,\/,xor,div,//,rdiv,<<,>>,mod,rem,max,min,gcd,atan2]).
73+
arithmetic_term(rela, 2, [is,>,<,>=,=<,=:=,=\=]).
9374

9475
:- non_counted_backtracking '$print_message_and_fail'/1.
9576

@@ -131,7 +112,7 @@
131112
% Additional rule just to replace invalid arithmetic expression with
132113
% runtime exception
133114
nonvar(G),
134-
arithmetic_relation_expanded(G, Gx-[]).
115+
w(arithmetic_expansion(rela, G, R, Gx-[R])).
135116
goal_expansion(Goal, Module, ExpandedGoal) :-
136117
( atom(Module),
137118
'$predicate_defined'(Module, goal_expansion, 2),

0 commit comments

Comments
 (0)