Skip to content

Commit 9730101

Browse files
committed
much safer occurs checks + workarround for lazy_findall SEGV
1 parent 22f9473 commit 9730101

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

prolog/metta_lang/metta_compiler.pl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@
458458
maplist(recompile_from_depends_child(FnName/LenArgs),List).
459459

460460
unnumbervars_wco(X,XXX):- compound(X),
461-
sub_term(E, X), compound(E), E = '$VAR'(_),!,
461+
sub_term_safely(E, X), compound(E), E = '$VAR'(_),!,
462462
subst001(X,E,_,XX),unnumbervars_wco(XX,XXX).
463463
unnumbervars_wco(X,X).
464464

@@ -505,14 +505,14 @@
505505

506506
% !(compile-for-assert (plus1 $x) (+ 1 $x) )
507507
compile_for_assert(HeadIsIn, AsBodyFnIn, Converted) :-
508-
compile_for_assert_2(HeadIsIn, AsBodyFnIn, Converted).
508+
woc(compile_for_assert_2(HeadIsIn, AsBodyFnIn, Converted)).
509509

510510
compile_for_assert_2(HeadIsIn, AsBodyFnIn, Converted) :-
511511
must_det_lls((
512512
IN = ['=',HeadIsIn, AsBodyFnIn],
513-
metta_to_metta_macro_recurse(IN, OUT),
513+
woc(metta_to_metta_macro_recurse(IN, OUT)),
514514
OUT = ['=',HeadIs, AsBodyFn],
515-
compile_for_assert_3(HeadIs, AsBodyFn, Converted))).
515+
woc(compile_for_assert_3(HeadIs, AsBodyFn, Converted)))).
516516

517517
compile_for_assert_3(HeadIsIn, AsBodyFnIn, Converted) :-
518518
%must_det_lls((
@@ -1786,7 +1786,7 @@
17861786
compiler_assertz(Info):-
17871787
once(unnumbervars_clause(Info,Assert)),
17881788
transpiler_debug(2,output_prolog(Info)),
1789-
once(clause_asserted(Assert)->true;assertz(Assert)),!.
1789+
once(locally_clause_asserted(Assert)->true;assertz(Assert)),!.
17901790

17911791
cname_var(Sym,Expr):- gensym(Sym,ExprV),
17921792
put_attr(Expr,vn,ExprV).
@@ -3209,7 +3209,7 @@
32093209
N1 is N-1,
32103210
atomic_list_concat(['mc_',N1,'__',Fh],FPrefixed),
32113211
H2 @.. [FPrefixed|Args],
3212-
clause(H2,B,Ref),clause(HH,BB,Ref),
3212+
clause_occurs_warning(H2,B,Ref),clause(HH,BB,Ref),
32133213
strip_m(HH,HHH),HHH=@=H2,
32143214
strip_m(BB,BBB),BBB=@=B,!.
32153215

@@ -3818,7 +3818,7 @@
38183818
% If any sub-term of Convert is a function, convert that sub-term and then proceed with the conversion.
38193819
f2p(HeadIs,RetResult,Convert, Converted) :-
38203820
rev_sub_sterm(AsFunction, Convert), % Get the deepest sub-term AsFunction of Convert
3821-
% sub_term(AsFunction, Convert), AsFunction\==Convert,
3821+
% sub_term_safely(AsFunction, Convert), AsFunction\==Convert,
38223822
callable(AsFunction), % Check if AsFunction is callable
38233823
compile_flow_control(HeadIs,Result,AsFunction, AsPred),
38243824
HeadIs\=@=AsFunction,!,
@@ -4029,7 +4029,7 @@
40294029
%ignore(Result = '$VAR'('HeadRes')),
40304030
conjuncts_to_list(Body,List),
40314031
reverse(List,RevList),append(Left,[BE|Right],RevList),
4032-
compound(BE),arg(Nth,BE,ArgRes),sub_var(Result,ArgRes),
4032+
compound(BE),arg(Nth,BE,ArgRes),sub_var_safely(Result,ArgRes),
40334033
remove_funct_arg(BE, Nth, AsBodyFunction),
40344034
append(Left,[eval_args(AsBodyFunction,Result)|Right],NewRevList),
40354035
reverse(NewRevList,NewList),
@@ -4043,7 +4043,7 @@
40434043
preds_to_functs0((AsPred, Convert), Converted) :-
40444044
\+ not_function(AsPred),
40454045
pred_to_funct(AsPred, AsFunction, Result),
4046-
sub_var(Result, Convert), !,
4046+
sub_var_safely(Result, Convert), !,
40474047
% The function equivalent of AsPred _xs Result in Convert
40484048
subst(Convert, Result, AsFunction, Converting),
40494049
preds_to_functs0(Converting, Converted).

prolog/metta_lang/metta_compiler_lib.pl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
transpiler_predicate_store(builtin, 'decons-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[list])).
251251
'mc__1_1_decons-atom'(AB1,AB2):- check_type_error( \+ iz_conz(AB1), decons_atom(AB1,AB2)),!,[A|B]=AB1,AB2=[A,B].
252252

253+
check_type_error(_Check,_Error):- \+ option_value(typecheck, true), !.
253254
check_type_error( Check, Error):- if_t(Check, raise_type_error( Check, Error)).
254255
raise_type_error( Check, Error):- trace,throw(raise_type_error( Check, Error)).
255256

@@ -467,7 +468,7 @@
467468

468469
%%%%%%%%%%%%%%%%%%%%% random number generation
469470

470-
use_python_main_rng(_):-!,fail.
471+
use_python_main_rng(_):- \+ option_value('fast-random', false),!,fail.
471472
use_python_main_rng('&rng'):-!.
472473
use_python_main_rng(rng('&rng', _)).
473474

@@ -611,7 +612,7 @@
611612
'mc__1_2_metta-unify'(A,B,TF):- as_tf(unify_with_occurs_check(A,B),TF).
612613

613614
transpiler_predicate_store(builtin, 'decons-ht', [3], '@doc', '@doc', [x(noeval,eager,[]),x(noeval,eager,[]),x(noeval,eager,[])],x(doeval,eager,[boolean])).
614-
'mc__1_3_decons-ht'(E,H,T,TF):- as_tf(unify_with_occurs_check(E,[H|T]),TF).
615+
'mc__1_3_decons-ht'(E,H,T,TF):- check_type_error( \+ iz_conz(E), 'decons-ht'(E,H,T)), as_tf(unify_with_occurs_warning(E,[H|T]),TF).
615616

616617
transpiler_predicate_nary_store(builtin, 'py-atom-call', 1, ['Atom'], 'Atom', 'Atom', [x(doeval,eager,[])], x(doeval,eager,[]), x(doeval,eager,[])).
617618
'mc_n_1__py-atom-call'(SymRef,Args,Ret) :- 'mc_n_1__py-atom-call!'(SymRef,Args,Ret).

prolog/metta_lang/metta_eval.pl

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,18 @@
265265
evals_to(XX,Y):- Y=@=XX,!.
266266
evals_to(XX,Y):- Y=='True',!, is_True(XX),!.
267267

268-
:- set_prolog_flag(optimise, false).
269-
:- set_prolog_flag(access_level, system).
268+
%:- set_prolog_flag(optimise, false).
269+
%:- set_prolog_flag(access_level, system).
270270
check_term_depth(X,Y):- find_term_cycles(X), X=Y.
271271
check_term_depth(X,Y):- find_term_cycles(Y), X=Y.
272272
find_term_cycles(X):- find_term_cycles(100,X).
273273
find_term_cycles(_,X):- \+ acyclic_term_nat(X), notrace, nl,nl, writeq(X), nl,nl, break.
274274
find_term_cycles(N,X):- term_depth(X,Deep), Deep>N, notrace, nl,nl, writeq(X), nl,nl, break.
275275

276-
%eval_args(_Eq,_RetType,_Dpth,_Self,X,Y):- check_term_depth(X,Y),!.
277-
eval_args(_Eq,_RetType,_Dpth,_Slf,X,Y):- var(X),nonvar(Y),!,X=Y.
278-
eval_args(_Eq,_RetType,_Dpth,_Slf,X,Y):- notrace(self_eval(X)),!,Y=X.
276+
eval_args(_Eq,_RetType,_Dpth,_Slf,X,Y):- var(X),nonvar(Y),!,
277+
unify_woc(X,Y).
278+
eval_args(_Eq,_RetType,_Dpth,_Slf,X,Y):- notrace(self_eval(X)),!,
279+
unify_woc(X,Y).
279280

280281
eval_args(Eq,RetType,Depth,Self,X,Y):-
281282
notrace(nonvar(Y)), var(RetType),
@@ -380,7 +381,7 @@
380381
% % uncommented causes 7% failure but a 10x speedup
381382
% subst_args_here(Eq,RetType,Depth2,Self,Y,YO):- Y=YO.
382383
% % this next one at least causes no failures and 5x speedup
383-
subst_args_here(_Eq,_RetType,_Depth2,_Self,Y,YO):- iz_conz(Y), \+ is_list(Y), !, bt,trace, break, Y=YO.
384+
subst_args_here(_Eq,_RetType,_Depth2,_Self,Y,YO):- iz_conz(Y), \+ is_list(Y), !, maybe_bt(depth), break, Y=YO.
384385
%subst_args_here(Eq,RetType,Depth2,Self,Y,YO):- !, Y=YO.
385386
subst_args_here(Eq,RetType,Depth2,Self,Y,YO):- !,
386387
subst_once(subst_args(Eq,RetType,Depth2,Self,Y,YO)*->true;Y=YO).
@@ -486,9 +487,9 @@
486487
with_debug(Type,eval_09(Eq,RetType,Depth,Self,X,Y)).
487488
eval_08(Eq,RetType,Depth,Self,X,Y):- eval_09(Eq,RetType,Depth,Self,X,Y).
488489

489-
%eval_09(_Eq,_RetType, Depth,_Slf,X,Y):- Depth< 0, !, X=Y, fail.
490-
%eval_09(_Eq,_RetType, Depth,_Slf,X,Y):- Depth< 1, !, X=Y.
491-
%eval_09(_Eq,_RetType, Depth,_Slf,_X,_Y):- Depth<1, if_trace(e,bt),!, fail.
490+
%eval_09(_Eq,_RetType, Depth,_Slf,X,Y):- Depth< 0, !, unify_woc(X,Y), fail.
491+
%eval_09(_Eq,_RetType, Depth,_Slf,X,Y):- Depth< 1, !, unify_woc(X,Y).
492+
%eval_09(_Eq,_RetType, Depth,_Slf,_X,_Y):- Depth<1, if_trace(e,maybe_bt),!, fail.
492493

493494
%hybrid_interp :- !.
494495
hybrid_interp :- option_value(compile,hybrid),!.
@@ -518,7 +519,7 @@
518519
((copy_term(X+Y,YC), YC = YY)
519520
-> nop(debug(metta(todo),'no_repeat in ~w: ~q',[ThisNth,X->Y]))
520521
; once((dmsg('repeats in'([ThisNth,X->Y])),
521-
%bt,
522+
%maybe_bt,
522523
dumpST,
523524
dmsg('repeats in'([ThisNth,X->Y])),
524525
!,fail))).
@@ -579,16 +580,16 @@
579580

580581
sanity_check_eval(_,_):- is_testing,!.
581582
sanity_check_eval(_,_):- !.
582-
sanity_check_eval(Which,X):- tracing,notrace,!,call_cleanup(\+ insanity_check_eval(Which,X), maybe_trace(unknown)),!.
583+
sanity_check_eval(Which,X):- tracing,notrace,!,call_cleanup(\+ insanity_check_eval(Which,X), maybe_trace(Which)),!.
583584
sanity_check_eval(Which,X):- \+ insanity_check_eval(Which,X), !.
584585

585-
%eval_10(Eq,RetType,Depth,Self,X,Y):- \+ sanity_check_eval(eval_10_in,X),X=Y,!,var_pass(Eq,RetType,Depth,Self,Y).
586+
%eval_10(Eq,RetType,Depth,Self,X,Y):- \+ sanity_check_eval(eval_10_in,X),unify_woc(X,Y),!,var_pass(Eq,RetType,Depth,Self,Y).
586587

587588
% The first eval_20/6 clause
588589
eval_20(Eq,RetType,Depth,Self,X,Y):- var(X), !, % sanity_check_eval(eval_20_var,X),
589-
Y=X,!,var_pass(Eq,RetType,Depth,Self,Y).
590+
unify_woc(X,Y),!,var_pass(Eq,RetType,Depth,Self,Y).
590591

591-
%eval_20(Eq,RetType,Depth,Self,X,Y):- \+ sanity_check_eval(eval_20_in,X),X=Y,!,var_pass(Eq,RetType,Depth,Self,Y).
592+
%eval_20(Eq,RetType,Depth,Self,X,Y):- \+ sanity_check_eval(eval_20_in,X),unify_woc(X,Y),!,var_pass(Eq,RetType,Depth,Self,Y).
592593

593594
eval_10(Eq,RetType,Depth,Self,X,Y):- \+ compound(X), !,
594595
as_prolog_x(Depth,Self,X,XX),
@@ -618,7 +619,7 @@
618619

619620

620621
leave_args_alone('->').
621-
%eval_10_disabled(_Eq,_RetType,_Depth,_Self,[Fn|Args],Y):- leave_args_alone(Fn), !, Y=[Fn|Args].
622+
eval_10_disabled(_Eq,_RetType,_Depth,_Self,[Fn|Args],Y):- leave_args_alone(Fn), !, Y=[Fn|Args].
622623

623624
eval_10(Eq,RetType,Depth,Self,['eval-in-only',Where,Eval],Y):-
624625
Where==interp,!,
@@ -883,7 +884,7 @@
883884
%fbug(expand_eval(X,XX)),
884885
eval_10(Eq,RetType,Depth,Self,XX,RetVal).
885886

886-
expand_eval(X,Y):- \+ is_list(X),!, X=Y.
887+
expand_eval(X,Y):- \+ is_list(X),!, unify_woc(X,Y).
887888
expand_eval([H|A],[H|AA]):- \+ ground(H),!,maplist(expand_eval,A,AA).
888889
expand_eval(['let*',Lets,Body],NewBody):- expand_let_star(Lets,Body,NewBody),!.
889890
expand_eval([H|A],[H|AA]):- maplist(expand_eval,A,AA).
@@ -1364,9 +1365,7 @@
13641365
equal_enough_for_test_renumbered_l(not_alpha_equ,XX,YY), RetVal).
13651366

13661367
loonit_assert_source_tf_empty(Src,XX,YY,Goal,Check,RetVal):-
1367-
%ignore(nortrace),
13681368
loonit_assert_source_tf(Src,Goal,Check,TF),
1369-
%ignore(rtrace),
13701369
tf_to_empty(TF,['Error',Src,['\nGot: ',XX,'\nEXP: ',YY]],RetVal).
13711370

13721371
tf_to_empty(TF,Else,RetVal):-
@@ -1658,18 +1657,18 @@
16581657
as_tf('=alpha'(X,Y),TF).
16591658

16601659
equal_renumbered(X0,Y0,XX,YY):-
1661-
max_var_number(X+Y,0,N),succ(N,N2),
1660+
max_var_number(X0+Y0,0,N),succ(N,N2),
16621661
copy_term(X0+Y0,X+Y),
16631662
term_variables(X,VXs), term_variables(Y,VYs),
1664-
merge_same_named_vars(VXs,VYs,Same,Merges,_LOXVs,_LOYVs),
1665-
max_var_number(X+Y,0,N),succ(N,N2),
1663+
merge_same_named_vars(VXs,VYs,Same,Merges,LOXVs,LOYVs),
1664+
debug_info(varnames,merge_same_named_vars(VXs,VYs,Same,Merges,LOXVs,LOYVs)),
16661665
maplist(remerge_attrs,Merges),
16671666
maplist(remove_attr(vn),VXs), maplist(remove_attr(vn),VYs),
16681667
numbervars(Same,N2,_,[attvar(skip)]),
16691668
%numbervars(_Merges,N3,_,[attvar(skip)]),
16701669
renumber_vars_wo_confict_tu(X,XX),
16711670
renumber_vars_wo_confict_tu(Y,YY),!.
1672-
%if_tracemsg(unknown,equal_enough_for_test(P2,XX,YY)),
1671+
%debug_info(unknown,equal_enough_for_test(P2,XX,YY)),
16731672

16741673

16751674
renumber_vars_wo_confict_tu(X,XXX):-
@@ -1895,6 +1894,9 @@
18951894

18961895
eval_10(Eq,RetType,Depth,Self,['switch',A,CL|T],Res):- !,
18971896
eval_10(Eq,RetType,Depth,Self,['case',A,CL|T],Res).
1897+
eval_10_new(Eq,RetType,Depth,Self,I,Res):- fail,
1898+
once(metta_to_metta_body_macro_recurse(eval10,I,O)),I\=@=O,!,
1899+
eval_10p(Eq,RetType,Depth,Self,O,Res).
18981900

18991901
eval_10_disabled(Eq,RetType,Depth,Self,[P,X|More],YY):- fail, is_list(X),X=[_,_,_],simple_math(X),
19001902
eval_selfless_2(X,XX),X\=@=XX,!, eval_20(Eq,RetType,Depth,Self,[P,XX|More],YY).
@@ -3145,9 +3147,9 @@
31453147
% - E: The resulting element that is part of the union of the two sets
31463148
lazy_union(P2, E1^Call1, E2^Call2, E) :-
31473149
% Step 1: Use lazy_findall/3 to declare that all elements satisfying Call1 are supposedly in List1
3148-
lazy_findall(E1, Call1, List1),
3150+
maybe_lazy_findall(E1, Call1, List1),
31493151
% Step 2: Use lazy_findall/3 to declare that all elements satisfying Call2 are supposedly in List2
3150-
lazy_findall(E2, Call2, List2),
3152+
maybe_lazy_findall(E2, Call2, List2),
31513153
% Step 3: Perform the union logic
31523154
( % Case 1: If E is a member of List1, include it in the result
31533155
member(E, List1)
@@ -3335,7 +3337,7 @@
33353337
% Step 1: Evaluate Call1 to generate E1
33363338
call(Call1),
33373339
% Step 2: Use lazy_findall/3 to declare that all elements satisfying Call2 are supposedly in List2
3338-
lazy_findall(E2, Call2, List2),
3340+
maybe_lazy_findall(E2, Call2, List2),
33393341
% Step 3: Perform the intersection logic
33403342
% Only return E1 if it is not a member of List2
33413343
member(E2, List2), call(P2,E1,E2).
@@ -3353,12 +3355,15 @@
33533355
% Step 1: Evaluate Call1 to generate E1
33543356
call(Call1),
33553357
% Step 2: Use lazy_findall/3 to declare that all elements satisfying Call2 are supposedly in List2
3356-
lazy_findall(E2, Call2, List2),
3358+
maybe_lazy_findall(E2, Call2, List2),
33573359
% Step 3: Perform the subtraction logic
33583360
% Only return E1 if it is not a member of List2
33593361
\+ (member(E2, List2), call(P2, E1, E2)).
33603362

33613363

3364+
maybe_lazy_findall(T,G,L):- option_value(lazy_findall,true),!,lazy_findall(T,G,L).
3365+
maybe_lazy_findall(T,G,L):- findall(T,G,L).
3366+
33623367
eval_20(Eq,RetType,Depth,Self,PredDecl,Res):-
33633368
Do_more_defs = do_more_defs(true),
33643369
clause(eval_21(Eq,RetType,Depth,Self,PredDecl,Res),Body),

prolog/metta_lang/metta_interp.pl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5652,6 +5652,11 @@
56525652
fail,
56535653
if_trace(do_metta, fbug(do_metta(File,Load,Self,Cmt,Out))),fail.
56545654
*/
5655+
do_metta_f(File, Load, Self, In, Out):-
5656+
copy_term(do_metta_f(File, Load, Self, In, Out),
5657+
do_metta_f(File, Load, Self, CIn, COut)),
5658+
!, do_metta(File, Load, Self, CIn, COut).
5659+
56555660
do_metta(_File, _Load, _Self, In, Out) :-
56565661
% If the input is a variable, unify it directly with the output.
56575662
var(In), !, In = Out.

prolog/metta_lang/metta_loader.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3157,5 +3157,5 @@
31573157

31583158
without_output(G):- is_devel,!,call(G).
31593159
without_output(G):- with_output_to(string(_), G).
3160-
:- nb_setval(debug_context, 'run').
3160+
:- nb_setval(debug_context, 'init').
31613161

0 commit comments

Comments
 (0)