|
221 | 221 |
|
222 | 222 | % not sure about the signature for this one |
223 | 223 | 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). |
227 | 227 |
|
228 | 228 | transpiler_predicate_store(builtin, '<', [2], '@doc', '@doc', [x(doeval,eager,[number]), x(doeval,eager,[number])], x(doeval,eager,[boolean])). |
229 | 229 | 'mc__1_2_<'(A,B,R) :- should_be(number,A),should_be(number,B),!,(A<B -> R='True' ; R='False'). |
|
244 | 244 | %%%%%%%%%%%%%%%%%%%%% lists |
245 | 245 |
|
246 | 246 | 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|_]). |
248 | 248 |
|
249 | 249 | 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]). |
251 | 251 |
|
252 | 252 | 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]). |
254 | 254 |
|
255 | 255 | 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]). |
257 | 257 |
|
258 | 258 | check_type_error(_Check,_Error):- \+ option_value(typecheck, true), !. |
259 | 259 | check_type_error( Check, Error):- if_t(Check, raise_type_error( Check, Error)). |
|
296 | 296 |
|
297 | 297 | %%%%%%%%%%%%%%%%%%%%% superpose, collapse |
298 | 298 |
|
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). % |
300 | 303 | 'mc__1_1_superpose'(S,R) :- should_be(is_list,S), member(E,S), as_p1_exec(E,R). |
301 | 304 |
|
| 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 | + |
302 | 342 | transpiler_predicate_store(builtin, collapse, [1], '@doc', '@doc', [x(doeval,lazy,[])], x(doeval,eager,[])). |
303 | 343 |
|
304 | 344 | /* |
|
337 | 377 | convert_space(S,S). |
338 | 378 |
|
339 | 379 | 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). |
341 | 382 |
|
342 | 383 | transpiler_predicate_store(builtin, 'remove-atom', [2], '@doc', '@doc', [x(doeval,eager,[]), x(noeval,eager,[])], x(doeval,eager,[])). |
343 | 384 | 'mc__1_2_remove-atom'(Space,PredDecl,[]) :- convert_space(Space,Space1),retractall(metta_atom_asserted(Space1,PredDecl)). |
344 | 385 |
|
345 | 386 | 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). |
347 | 388 |
|
348 | 389 | % This allows match to supply hits to the correct metta_atom/2 (Rather than sending a variable |
349 | 390 | match_pattern(Space, Pattern):- |
350 | 391 | if_t(compound(Pattern), |
351 | 392 | (functor(Pattern,F,A,Type), functor(Atom,F,A,Type))), |
352 | 393 | 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 |
355 | 396 | %wocf(Atom=Pattern). |
356 | 397 | %woc(Atom=Pattern). % 2.09 seconds. |
357 | 398 |
|
|
369 | 410 | % otherwise calls prolog unification (with occurs check later) |
370 | 411 | unify_pattern(Atom, Pattern):- metta_unify(Atom, Pattern). |
371 | 412 |
|
372 | | -metta_unify(Atom, Pattern):- Atom=Pattern. |
| 413 | +metta_unify(Atom, Pattern):- unify_with_occurs_check(Atom,Pattern). |
373 | 414 |
|
374 | 415 | % TODO FIXME: sort out the difference between unify and match |
375 | 416 | 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). |
377 | 418 |
|
378 | 419 | transpiler_predicate_store(builtin, unify, [4], '@doc', '@doc', [x(doeval,eager,[]), x(doeval,eager,[]), x(doeval,lazy,[]), x(doeval,lazy,[])], x(doeval,eager,[])). |
379 | 420 | 'mc__1_4_unify'(Space,Pattern,Psuccess,PFailure,RetVal) :- |
|
408 | 449 | 'mc__1_0_empty'(_) :- fail. |
409 | 450 |
|
410 | 451 | 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). |
412 | 454 |
|
413 | 455 | transpiler_predicate_store(builtin, 'get-metatype', [1], '@doc', '@doc', [x(noeval,eager,[])], x(doeval,eager,[])). |
414 | 456 | 'mc__1_1_get-metatype'(X,Y) :- 'get-metatype'(X,Y). % use the code in the interpreter for now |
|
464 | 506 | equal_enough_for_test_renumbered_l(not_alpha_equ,AA,BB), C). |
465 | 507 |
|
466 | 508 | 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). |
468 | 510 | compile_flow_control(HeadIs,LazyVars,RetResult,RetResultN,LazyRetQuoted,Convert, QuotedCode1a, QuotedCode1N) :- |
469 | 511 | Convert = ['quote',Quoted],!, |
470 | 512 | f2p(HeadIs,LazyVars,QuotedResult,QuotedResultN,LazyRetQuoted,Quoted,QuotedCode,QuotedCodeN), |
|
0 commit comments