Skip to content

Commit 1de120d

Browse files
committed
Integrate arithmetic expansion into loader.pl
1 parent 2892477 commit 1de120d

File tree

3 files changed

+88
-162
lines changed

3 files changed

+88
-162
lines changed

src/loader.pl

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,61 @@
3535
),
3636
write('.').
3737

38+
trace(on).
39+
40+
writeln(X) :-
41+
trace(on), write(X), nl.
42+
x(G_0) :-
43+
catch(loader:G_0, E, (loader:writeln(exception:E:G_0),throw(E))).
44+
w(G_0) :-
45+
writeln(call:G_0), x(G_0), writeln(exit:G_0).
46+
w(_) :-
47+
writeln(done).
48+
49+
%% arithmetic_relation_expanded(?Term, ListDifference).
50+
%
51+
% Recursively traverse Term and assemble a list of replacements that make a
52+
% valid aruthmetic relation.
53+
%
54+
% 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.
77+
%
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]).
3893

3994
:- non_counted_backtracking '$print_message_and_fail'/1.
4095

@@ -43,6 +98,7 @@
4398
nl,
4499
'$fail'.
45100

101+
46102
expand_term(Term, ExpandedTerm) :-
47103
( '$predicate_defined'(user, term_expansion, 2),
48104
catch(user:term_expansion(Term, ExpandedTerm0),
@@ -69,16 +125,13 @@
69125
term_expansion_list(Terms, ExpandedTerms0Tail, ExpandedTermsTail)
70126
).
71127

72-
73128
:- non_counted_backtracking goal_expansion/3.
74129

75-
goal_expansion(G, user, (Y = Rhs, X is Y)) :-
130+
goal_expansion(G, _, Gx) :-
76131
% Additional rule just to replace invalid arithmetic expression with
77132
% runtime exception
78133
nonvar(G),
79-
G = (X is Rhs),
80-
nonvar(Rhs),
81-
(Rhs == []; Rhs = [_|_]).
134+
arithmetic_relation_expanded(G, Gx-[]).
82135
goal_expansion(Goal, Module, ExpandedGoal) :-
83136
( atom(Module),
84137
'$predicate_defined'(Module, goal_expansion, 2),

tests/scryer/cli/issues/incorrect_arithmetics.in/hoist-expr.pl

Lines changed: 0 additions & 144 deletions
This file was deleted.

tests/scryer/cli/issues/incorrect_arithmetics.in/incorrect_arithmetics.pl

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,54 @@
44
:- use_module(library(iso_ext)).
55
:- use_module(library(format)).
66
:- use_module(library(debug)).
7+
:- use_module(library(time)).
8+
9+
:- dynamic(rznvivy/0).
710

811
% Asserting and consulting of erroneous arithmetic relation shall succeed,
912
% but then it must fail at runtime.
1013
main :-
11-
template_relation("tttftt", R),
12-
findall(T, test(R, T), [assert,consult]).
14+
template_relation("tttf", R),
15+
ignore_exception(test(R, consult)),
16+
ignore_exception(test(R, assert)),
17+
true.
1318

1419
test(Relation, TestVariant) :-
15-
load_and_call(TestVariant, (rznvivy :- false, Relation), \+rznvivy).
20+
load_and_call(TestVariant, rznvivy/0, (rznvivy :- false, Relation), \+rznvivy).
1621

17-
load_and_call(assert, Clause, Query) :-
22+
load_and_call(assert, PI, Clause, Query) :-
1823
setup_call_cleanup(
1924
ignore_exception(assertz(Clause)),
20-
$-Query,
21-
retract(Clause)
25+
callf(PI, Query),
26+
$abolish(PI)
2227
).
23-
load_and_call(consult, Clause, Query) :-
28+
load_and_call(consult, PI, Clause, Query) :-
2429
T = 'chnytjl.pl',
2530
setup_call_cleanup(
2631
open(T, write, S),
2732
portray_clause(S, Clause),
2833
close(S)
2934
),
30-
ignore_exception(consult(T)),
31-
$-Query.
35+
setup_call_cleanup(
36+
ignore_exception(consult(T)),
37+
callf(PI, Query),
38+
$abolish(PI)
39+
).
40+
41+
callf(PI, G_0) :-
42+
clause(rznvivy, I),
43+
I \= true,
44+
always(ignore_exception(listing(PI))),
45+
$G_0.
46+
47+
always(G_0) :- call(G_0).
48+
always(_).
3249

3350
template_relation(Template, R) :-
34-
setof(E, phrase(arith_relation(E), Template), Es),
51+
time(setof(E, phrase(arith_relation(E), Template), Es)),
3552
length(Es, L),
3653
random_integer(0, L, I),
37-
format("% Info: Selected ~dth out of ~d found aritmetic relations that satisfy ~s template:~n\t", [I,L,Template]),
54+
format(" % Info: Selected ~d/~d aritmetic terms that satisfy ~s template:~n\t", [I,L,Template]),
3855
nth0(I, Es, R),
3956
portray_clause(R).
4057

@@ -57,7 +74,7 @@
5774
func(Expr) --> {fn(T, Expr)}, [T].
5875
rel(Expr) --> {rl(T, Expr)}, [T].
5976

60-
%rl(t, expr(F,A,B)) :- member(F, [A<B,A=:=B]).
77+
rl(t, expr(F,A,B)) :- member(F, [A<B,A=:=B]).
6178
rl(t, expr(F,A)) :- member(F, [_ is A]).
6279

6380
fn(t, expr(A)) :- rnd(A).
@@ -66,7 +83,7 @@
6683
fn(t, expr(F,A,B)) :- member(F, [A+B,A-B,A*B,A/B,A^B,max(A,B)]).
6784
fn(f, expr(F)) :- member(F, [[],phi,[_|_]]).
6885
fn(f, expr(F,A)) :- member(F, [zeta(A)]).
69-
fn(f, expr(F,A,B)) :- member(F, [[A,B]]).
86+
fn(f, expr(F,A,B)) :- member(F, [[A,B],[A|B]]).
7087

7188
%% rnd(N).
7289
%

0 commit comments

Comments
 (0)