|
43 | 43 | catch(loader:G_0, E, (loader:writeln(exception:E:G_0),throw(E))).
|
44 | 44 | w(G_0) :-
|
45 | 45 | writeln(call:G_0), x(G_0), writeln(exit:G_0).
|
46 |
| -w(_) :- |
47 |
| - writeln(done). |
48 | 46 |
|
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). |
53 | 48 | %
|
54 | 49 | % 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. |
77 | 52 | %
|
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,>,<,>=,=<,=:=,=\=]). |
93 | 74 |
|
94 | 75 | :- non_counted_backtracking '$print_message_and_fail'/1.
|
95 | 76 |
|
|
131 | 112 | % Additional rule just to replace invalid arithmetic expression with
|
132 | 113 | % runtime exception
|
133 | 114 | nonvar(G),
|
134 |
| - arithmetic_relation_expanded(G, Gx-[]). |
| 115 | + w(arithmetic_expansion(rela, G, R, Gx-[R])). |
135 | 116 | goal_expansion(Goal, Module, ExpandedGoal) :-
|
136 | 117 | ( atom(Module),
|
137 | 118 | '$predicate_defined'(Module, goal_expansion, 2),
|
|
0 commit comments