Skip to content

Commit e130e11

Browse files
committed
more occurs checks
1 parent bb75d86 commit e130e11

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

prolog/metta_lang/metta_compiler.pl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
as_p1_exec(ispeEnNC(ERet,ECode,_,_,CCode),ERet) :- !, call(CCode),call(ECode).
217217
as_p1_exec(rtrace(T),TRet) :- !, rtrace(as_p1_exec(T,TRet)).
218218
as_p1_exec(call(P1,T),TRet) :- !, call(P1,as_p1_exec(T,TRet)).
219-
as_p1_exec(X,X) :- !.
219+
as_p1_exec(X,Y) :- as_p1_expr(X,S),eval(S,Y).
220220

221221

222222
as_p1_expr(ispu(URet),URet) :- !.
@@ -2433,7 +2433,7 @@
24332433
:- dynamic(maybe_optimize_prolog_term/4).
24342434
:- dynamic(maybe_optimize_prolog_assertion/4).
24352435

2436-
try_optimize_prolog(Y,Convert,Optimized):-
2436+
try_optimize_prolog(Y,Convert,Optimized):- fail,
24372437
catch_warn(maybe_optimize_prolog_assertion(Y,[],Convert,Optimized)),!.
24382438
try_optimize_prolog(_,Optimized,Optimized).
24392439
/*

prolog/metta_lang/metta_compiler_lib.pl

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@
221221

222222
% not sure about the signature for this one
223223
transpiler_predicate_store(builtin, '==', [2], '@doc', '@doc', [x(doeval,eager,[]), x(doeval,eager,[])], x(doeval,eager,[boolean])).
224-
%'mc__1_2_=='(A,B,TF):- eval_40(['==',A,B],TF).
225-
'mc__1_2_=='(A,B,TF) :- var(A),!,as_tf(A==B,TF).
226-
'mc__1_2_=='(A,B,TF) :- as_tf(A=@=B,TF).
224+
'mc__1_2_=='(A,B,TF) :- (var(A);var(B)),!,as_tf(A==B,TF).
225+
'mc__1_2_=='(A,B,TF):- eval_40(['==',A,B],TF).
226+
%'mc__1_2_=='(A,B,TF) :- as_tf(A=@=B,TF).
227227

228228
transpiler_predicate_store(builtin, '<', [2], '@doc', '@doc', [x(doeval,eager,[number]), x(doeval,eager,[number])], x(doeval,eager,[boolean])).
229229
'mc__1_2_<'(A,B,R) :- should_be(number,A),should_be(number,B),!,(A<B -> R='True' ; R='False').
@@ -244,16 +244,16 @@
244244
%%%%%%%%%%%%%%%%%%%%% lists
245245

246246
transpiler_predicate_store(builtin, 'car-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[])).
247-
'mc__1_1_car-atom'(HT,H):- check_type_error( \+ is_list(HT),'car-atom'(HT,H)),HT=[H|_].
247+
'mc__1_1_car-atom'(HT,H):- check_type_error( \+ is_list(HT),'car-atom'(HT,H)),unify_with_occurs_check(HT,[H|_]).
248248

249249
transpiler_predicate_store(builtin, 'cdr-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[list])).
250-
'mc__1_1_cdr-atom'(HT,T):- check_type_error( \+ is_list(HT),'cdr-atom'(HT,T)),HT=[_|T].
250+
'mc__1_1_cdr-atom'(HT,T):- check_type_error( \+ is_list(HT),'cdr-atom'(HT,T)),unify_with_occurs_check(HT,[_|T]).
251251

252252
transpiler_predicate_store(builtin, 'cons-atom', [2], '@doc', '@doc', [x(noeval,eager,[]), x(noeval,eager,[list])], x(noeval,eager,[list])).
253-
'mc__1_2_cons-atom'(A,B,AB):- check_type_error( \+ is_list(B),'cons-atom'(A,B,AB)),AB=[A|B].
253+
'mc__1_2_cons-atom'(A,B,AB):- check_type_error( \+ is_list(B),'cons-atom'(A,B,AB)),unify_with_occurs_check(AB,[A|B]).
254254

255255
transpiler_predicate_store(builtin, 'decons-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[list])).
256-
'mc__1_1_decons-atom'(AB1,AB2):- check_type_error( \+ iz_conz(AB1), decons_atom(AB1,AB2)),!,[A|B]=AB1,AB2=[A,B].
256+
'mc__1_1_decons-atom'(AB1,AB2):- check_type_error( \+ iz_conz(AB1), decons_atom(AB1,AB2)),!,unify_with_occurs_check([A|B],AB1),unify_with_occurs_check(AB2,[A,B]).
257257

258258
check_type_error(_Check,_Error):- \+ option_value(typecheck, true), !.
259259
check_type_error( Check, Error):- if_t(Check, raise_type_error( Check, Error)).
@@ -296,9 +296,49 @@
296296

297297
%%%%%%%%%%%%%%%%%%%%% superpose, collapse
298298

299-
transpiler_predicate_store(builtin, superpose, [1], '@doc', '@doc', [x(doeval,eager,[])], x(noeval,eager,[])).
299+
transpiler_predicate_store(builtin, superpose, [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[])).
300+
301+
'mc__1_1_superpose'(S,R) :- should_be(nonvar,S), \+ is_list(S), !, as_p1_expr(S,X), should_be(is_list,X), member(E,S), % as_p1_exec(E,R).
302+
as_p1_expr(E,Y),eval(Y,R). %
300303
'mc__1_1_superpose'(S,R) :- should_be(is_list,S), member(E,S), as_p1_exec(E,R).
301304

305+
:- op(700,xfx,'=~').
306+
307+
soon_compile_flow_control(_HeadIs, _LazyVars, RetResult, RetResultN, _ResultLazy, Convert, [inline(Converted)], ConvertedN) :-
308+
Convert =~ ['superpose',ValueL],is_ftVar(ValueL),
309+
%maybe_unlistify(UValueL,ValueL,URetResult,RetResult),
310+
Converted = eval_args(['superpose',ValueL],RetResult),
311+
cname_var('MeTTa_SP_',ValueL).
312+
313+
soon_compile_flow_control(HeadIs, _LazyVars, RetResult, RetResultN, _ResultLazy, Convert, [inline(Converted)], ConvertedN) :-
314+
Convert =~ ['superpose',ValueL],is_list(ValueL),
315+
%maybe_unlistify(UValueL,ValueL,URetResult,RetResult),
316+
cname_var('SP_Ret',RetResult),
317+
maplist(f2p_assign(HeadIs,RetResult),ValueL,CodeForValueL),
318+
list_to_disjuncts(CodeForValueL,Converted),!.
319+
320+
/*
321+
maybe_unlistify([UValueL],ValueL,RetResult,[URetResult]):- fail, is_list(UValueL),!,
322+
maybe_unlistify(UValueL,ValueL,RetResult,URetResult).
323+
maybe_unlistify(ValueL,ValueL,RetResult,RetResult).
324+
325+
list_to_disjuncts([],false).
326+
list_to_disjuncts([A],A):- !.
327+
list_to_disjuncts([A|L],(A;D)):- list_to_disjuncts(L,D).
328+
329+
330+
%f2p_assign(_HeadIs,V,Value,is_True(V)):- Value=='True'.
331+
f2p_assign(_HeadIs,ValueR,Value,ValueR=Value):- \+ compound(Value),!.
332+
f2p_assign(_HeadIs,ValueR,Value,ValueR=Value):- is_ftVar(Value),!.
333+
f2p_assign(HeadIs,ValueResult,Value,Converted):-
334+
f2p(HeadIs, _LazyVars, ValueResultR, _ResultLazy, Value,CodeForValue),
335+
%into_equals(ValueResultR,ValueResult,ValueResultRValueResult),
336+
ValueResultRValueResult = (ValueResultR=ValueResult),
337+
combine_code(CodeForValue,ValueResultRValueResult,Converted).
338+
*/
339+
340+
341+
302342
transpiler_predicate_store(builtin, collapse, [1], '@doc', '@doc', [x(doeval,lazy,[])], x(doeval,eager,[])).
303343

304344
/*
@@ -337,21 +377,22 @@
337377
convert_space(S,S).
338378

339379
transpiler_predicate_store(builtin, 'add-atom', [2], '@doc', '@doc', [x(doeval,eager,[]), x(noeval,eager,[])], x(doeval,eager,[])).
340-
'mc__1_2_add-atom'(Space,PredDecl,[]) :- convert_space(Space,Space1),A=metta_atom_asserted(Space1,PredDecl),(call(A) -> true ; assertz(A)).
380+
'mc__1_2_add-atom'(Space,PredDecl,TF) :- convert_space(Space,Space1), %A=metta_atom_asserted(Space1,PredDecl),(call(A) -> true ; assertz(A)).
381+
do_metta(python,load,Space1,PredDecl,TF).
341382

342383
transpiler_predicate_store(builtin, 'remove-atom', [2], '@doc', '@doc', [x(doeval,eager,[]), x(noeval,eager,[])], x(doeval,eager,[])).
343384
'mc__1_2_remove-atom'(Space,PredDecl,[]) :- convert_space(Space,Space1),retractall(metta_atom_asserted(Space1,PredDecl)).
344385

345386
transpiler_predicate_store(builtin, 'get-atoms', [1], '@doc', '@doc', [x(noeval,eager,[])], x(noeval,eager,[])).
346-
'mc__1_1_get-atoms'(Space,Atoms) :- metta_atom(Space, Atoms).
387+
'mc__1_1_get-atoms'(Space,Atoms) :- metta_atom(Space, Atom),unify_with_occurs_check(Atoms,Atom).
347388

348389
% This allows match to supply hits to the correct metta_atom/2 (Rather than sending a variable
349390
match_pattern(Space, Pattern):-
350391
if_t(compound(Pattern),
351392
(functor(Pattern,F,A,Type), functor(Atom,F,A,Type))),
352393
metta_atom(Space, Atom),
353-
%unify_with_occurs_check(Atom,Pattern). % 0.262 secs.
354-
Atom=Pattern. % 0.170 secs
394+
unify_with_occurs_check(Atom,Pattern). % 0.262 secs.
395+
%Atom=Pattern. % 0.170 secs
355396
%wocf(Atom=Pattern).
356397
%woc(Atom=Pattern). % 2.09 seconds.
357398

@@ -369,11 +410,11 @@
369410
% otherwise calls prolog unification (with occurs check later)
370411
unify_pattern(Atom, Pattern):- metta_unify(Atom, Pattern).
371412

372-
metta_unify(Atom, Pattern):- Atom=Pattern.
413+
metta_unify(Atom, Pattern):- unify_with_occurs_check(Atom,Pattern).
373414

374415
% TODO FIXME: sort out the difference between unify and match
375416
transpiler_predicate_store(builtin, unify, [3], '@doc', '@doc', [x(doeval,eager,[]), x(doeval,eager,[]), x(doeval,lazy,[])], x(doeval,eager,[])).
376-
'mc__1_3_unify'(Space,Pattern,P1,Ret) :- unify_pattern(Space, Atom),Atom=Pattern,as_p1_exec(P1,Ret).
417+
'mc__1_3_unify'(Space,Pattern,P1,Ret) :- unify_pattern(Space, Atom),unify_with_occurs_check(Atom,Pattern),as_p1_exec(P1,Ret).
377418

378419
transpiler_predicate_store(builtin, unify, [4], '@doc', '@doc', [x(doeval,eager,[]), x(doeval,eager,[]), x(doeval,lazy,[]), x(doeval,lazy,[])], x(doeval,eager,[])).
379420
'mc__1_4_unify'(Space,Pattern,Psuccess,PFailure,RetVal) :-
@@ -408,7 +449,8 @@
408449
'mc__1_0_empty'(_) :- fail.
409450

410451
transpiler_predicate_store(builtin, 'eval', [1], '@doc', '@doc', [x(noeval,eager,[])], x(doeval,eager,[])).
411-
'mc__1_1_eval'(X,R) :- transpile_eval(X,R).
452+
%'mc__1_1_eval'(X,R) :- transpile_eval(X,R).
453+
'mc__1_1_eval'(X,R) :- eval(X,R).
412454

413455
transpiler_predicate_store(builtin, 'get-metatype', [1], '@doc', '@doc', [x(noeval,eager,[])], x(doeval,eager,[])).
414456
'mc__1_1_get-metatype'(X,Y) :- 'get-metatype'(X,Y). % use the code in the interpreter for now
@@ -464,7 +506,7 @@
464506
equal_enough_for_test_renumbered_l(not_alpha_equ,AA,BB), C).
465507

466508
transpiler_predicate_store(builtin, 'quote', [1], '@doc', '@doc', [x(noeval,eager,[])], x(noeval,eager,[])).
467-
'mc__1_1_quote'(A,['quote',A]).
509+
'mc__1_1_quote'(A,['quote',AA]):- unify_with_occurs_check(A,AA).
468510
compile_flow_control(HeadIs,LazyVars,RetResult,RetResultN,LazyRetQuoted,Convert, QuotedCode1a, QuotedCode1N) :-
469511
Convert = ['quote',Quoted],!,
470512
f2p(HeadIs,LazyVars,QuotedResult,QuotedResultN,LazyRetQuoted,Quoted,QuotedCode,QuotedCodeN),

0 commit comments

Comments
 (0)