Skip to content

Commit 27223c3

Browse files
author
Roy Ward
committed
merge
2 parents d13e74b + da08a75 commit 27223c3

File tree

6 files changed

+134
-39
lines changed

6 files changed

+134
-39
lines changed

examples/games/GreedyChess.metta

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -344,19 +344,19 @@
344344
(rank p)
345345

346346
; Chess symbols to display on console
347-
(s k ♔)
348-
(s q ♕)
349-
(s r ♖)
350-
(s b ♗)
351-
(s n ♘)
352-
(s p ♙)
353-
354-
(g k ♚)
355-
(g q ♛)
356-
(g r ♜)
357-
(g b ♝)
358-
(g n ♞)
359-
(g p ♟)
347+
(g k ♔)
348+
(g q ♕)
349+
(g r ♖)
350+
(g b ♗)
351+
(g n ♘)
352+
(g p ♙)
353+
354+
(s k ♚)
355+
(s q ♛)
356+
(s r ♜)
357+
(s b ♝)
358+
(s n ♞)
359+
(s p ♟)
360360

361361
;*******************************************************
362362
; Declare initializing game state

prolog/metta_lang/metta_compiler.pl

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,11 @@
405405

406406
% !(compile-for-assert (plus1 $x) (+ 1 $x) )
407407
compile_for_assert(HeadIsIn, AsBodyFnIn, Converted) :-
408-
compile_for_assert_3(HeadIsIn, AsBodyFnIn, Converted).
408+
compile_for_assert_2(HeadIsIn, AsBodyFnIn, Converted).
409+
410+
compile_for_assert_2(HeadIsIn, AsBodyFnIn, Converted) :-
411+
metta_to_metta_macro(HeadIsIn, AsBodyFnIn, HeadIs, AsBodyFn),
412+
compile_for_assert_3(HeadIs, AsBodyFn, Converted).
409413

410414
compile_for_assert_3(HeadIsIn, AsBodyFnIn, Converted) :-
411415
%must_det_lls((
@@ -1244,7 +1248,7 @@
12441248
%label_arg_types(F,0,[A|Args1]),
12451249
%LenArgs1 is LenArgs+1,
12461250
append(Args1,[A],Args2),
1247-
R ~.. [xxx(FIn),Fp|Args2],
1251+
R ~.. [f(FIn),Fp|Args2],
12481252
(Caller=caller(CallerInt,CallerSz),(CallerInt-CallerSz)\=(F-LenArgs),\+ transpiler_depends_on(CallerInt,CallerSz,F,LenArgs) ->
12491253
compiler_assertz(transpiler_depends_on(CallerInt,CallerSz,F,LenArgs)),
12501254
transpiler_debug(2,format_e("Asserting: transpiler_depends_on(~q,~q,~q,~q)\n",[CallerInt,CallerSz,F,LenArgs]))
@@ -1279,7 +1283,7 @@
12791283
create_mc_name(LenArgsAll,FIn,Fp),
12801284
%label_arg_types(FIn,0,[A|Args1]),
12811285
%LenArgs1 is LenArgs+1,
1282-
R0 ~.. [xxx(a),Fp|Args1],
1286+
R0 ~.. [xxx(4),Fp|Args1],
12831287
%R1=R0),
12841288
(Caller=caller(CallerInt,CallerSz),(CallerInt-CallerSz)\=(FIn-LenArgs),\+ transpiler_depends_on(CallerInt,CallerSz,FIn,LenArgs) ->
12851289
compiler_assertz(transpiler_depends_on(CallerInt,CallerSz,FIn,LenArgs)),
@@ -1301,7 +1305,7 @@
13011305
append(FixedPart,VariablePart,Args1),
13021306
append(FixedPart,[VariablePart],Args1a),
13031307
append(Args1a,[A],Args2),
1304-
R ~.. [xxx(c),Fp|Args2],
1308+
R ~.. [xxx(3),Fp|Args2],
13051309
(Caller=caller(CallerInt,CallerSz),(CallerInt-CallerSz)\=(F-0),\+ transpiler_depends_on(CallerInt,CallerSz,F,0) ->
13061310
compiler_assertz(transpiler_depends_on(CallerInt,CallerSz,F,0)),
13071311
transpiler_debug(2,format_e("Asserting: transpiler_depends_on(~q,~q,~q,~q)\n",[CallerInt,CallerSz,F,0]))
@@ -1313,7 +1317,7 @@
13131317
% label_arg_types(F,1,Args0),
13141318
% maplist(ast_to_prolog_aux(Caller,DontStub),Args0,Args1),
13151319
% label_arg_types(F,1,Args1),
1316-
% A ~.. [xxx(d),F|Args1],
1320+
% A ~.. [xxx(2),F|Args1],
13171321
% notice_callee(Caller,A))).
13181322
%ast_to_prolog_aux(Caller,DontStub,[assign,A,[call(FIn)|ArgsIn]],R) :- (fullvar(A); \+ compound(A)),callable(FIn),!,
13191323
% must_det_lls((
@@ -1597,9 +1601,18 @@
15971601
cmpd4lst(A,_):- nonvar(A),dmp_break,fail.
15981602
cmpd4lst(_A,[Cmpd,_F|_Args]):- \+ compound(Cmpd),dmp_break,fail.
15991603
cmpd4lst(_A,[_Cmpd,_F|Args]):- \+ is_list(Args),dmp_break,fail.
1600-
cmpd4lst(A,[_Cmpd,F,Arg]):- F==is_True,!,A=is_True(Arg),!.
1601-
cmpd4lst(call_fn_native(F,XXX,Args),[Cmpd,F|Args]):- compound(Cmpd),xxx(XXX)=Cmpd,!.
1602-
cmpd4lst(call_fn_native(F,xxx(?),Args),[F|Args]):- !.
1604+
%cmpd4lst(A,[_Cmpd,F|Args]):- atom(F),is_cmp_builtin(F),A=..[F|Args],!.
1605+
%cmpd4lst(call_fn_native(F,XXX,Args),[Cmpd,F|Args]):- compound(Cmpd),f(XXX)=Cmpd,!.
1606+
cmpd4lst(A,[_Cmpd,F|Args]):- atom(F),!,A=..[F|Args],!.
1607+
cmpd4lst(call_fn_native_error(F,xxx(?),Args),[F|Args]):- !.
1608+
1609+
is_cmp_builtin(is_True).
1610+
is_cmp_builtin(as_p1_expr).
1611+
is_cmp_builtin(as_p1_exec).
1612+
is_cmp_builtin(ispeEnN).
1613+
is_cmp_builtin(call_fn_native).
1614+
1615+
16031616
/*
16041617
cmpd4lst2(A,Info):- compound(A), A=call_fn_native(F,XXX,Args),!,cmpd4lst(F,XXX,Args,Info).
16051618
cmpd4lst2(A,Info):- compound(A), compound_name_arguments(A,F,Args),!,cmpd4lst(F,xxx(?),Args,Info).
@@ -2940,6 +2953,7 @@
29402953
;
29412954
(AsPred = [F|ArgsL])).
29422955

2956+
as_functor_args(call_fn_native(F,_,ArgsL),F,A,ArgsL):- length(ArgsL,A),!.
29432957
%as_functor_args(AsPred,_,_,_Args):- is_ftVar(AsPred),!,fail.
29442958
as_functor_args(AsPred,F,A,ArgsL):- \+ iz_conz(AsPred),
29452959
AsPred @.. List,!, as_functor_args(List,F,A,ArgsL),!.

prolog/metta_lang/metta_compiler_lib.pl

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
:- dynamic(transpiler_predicate_store/7).
22
:- discontiguous transpiler_predicate_store/7.
33
:- discontiguous transpiler_predicate_nary_store/9.
4+
:- discontiguous compile_flow_control/8.
45

56
from_prolog_args(_,X,X).
67
:-dynamic(pred_uses_fallback/2).
@@ -243,6 +244,12 @@
243244
transpiler_predicate_store(builtin, 'decons-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[list])).
244245
'mc__1_1_decons-atom'([A|B],[A,B]).
245246

247+
%transpiler_predicate_store(builtin, 'length', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[number])).
248+
%'mc__1_1_length'(L,S) :- length(L,S).
249+
250+
transpiler_predicate_store(builtin, 'size-atom', [1], '@doc', '@doc', [x(noeval,eager,[list])], x(noeval,eager,[number])).
251+
'mc__1_1_size-atom'(L,S) :- length(L,S).
252+
246253
%%%%%%%%%%%%%%%%%%%%% set
247254

248255
lazy_member(P,R2) :- as_p1_exec(R2,P).
@@ -315,11 +322,11 @@
315322
% This allows match to supply hits to the correct metta_atom/2 (Rather than sending a variable
316323
match_pattern(Space, Pattern):-
317324
if_t(compound(Pattern),
318-
(functor(Pattern,F,A,Type), functor(Atom,F,A,Type))),
319-
metta_atom(Space, Atom),
325+
(functor(Pattern,F,A,Type), functor(Atom,F,A,Type))),
326+
metta_atom(Space, Atom),
320327
%unify_with_occurs_check(Atom,Pattern). % 0.262 secs.
321328
Atom=Pattern. % 0.170 secs
322-
%wocf(Atom=Pattern).
329+
%wocf(Atom=Pattern).
323330
%woc(Atom=Pattern). % 2.09 seconds.
324331

325332
transpiler_predicate_store(builtin, match, [3], '@doc', '@doc', [x(doeval,eager,[]), x(noeval,eager,[]), x(doeval,lazy,[])], x(doeval,eager,[])).
@@ -351,9 +358,12 @@
351358
transpiler_predicate_nary_store(builtin, progn, 0, [], 'Atom', 'Atom', [], x(doeval,eager,[]), x(doeval,eager,[])).
352359
'mc_n_0__progn'(List,Ret) :- append(_,[Ret],List).
353360

354-
transpiler_predicate_nary_store(builtin, 'call-fn!', 1, ['Atom'], 'Atom', 'Atom', [x(doeval,eager,[])], x(doeval,eager,[]), x(doeval,eager,[])).
361+
transpiler_predicate_nary_store(builtin, 'call-fn!', 1, ['Atom'], 'Atom', 'Atom', [x(noeval,eager,[])], x(noeval,eager,[]), x(doeval,eager,[])).
355362
'mc_n_1__call-fn!'(Fn,List,Ret) :- append(List,[Ret],List2),apply(Fn,List2).
356363

364+
transpiler_predicate_nary_store(builtin, 'call-fn', 1, ['Atom'], 'Atom', 'Atom', [x(doeval,eager,[])], x(doeval,eager,[]), x(doeval,eager,[])).
365+
'mc_n_1__call-fn'(Fn,List,Ret) :- append(List,[Ret],List2),apply(Fn,List2).
366+
357367
transpiler_predicate_nary_store(builtin, 'call-p!', 1, ['Atom'], 'Atom', 'Atom', [x(doeval,eager,[])], x(doeval,eager,[]), x(doeval,eager,[])).
358368
'mc_n_1__call-p!'(Fn,List,Ret) :- (apply(Fn,List)->Ret='True';Ret='False').
359369

@@ -540,4 +550,52 @@
540550
predsort(listing_order,Unsorted,Sorted).
541551

542552

553+
metta_to_metta_macro(HeadIsN, AsBodyFnN, HeadIsC, AsBodyFnOut):-
554+
must_det_lls((
555+
copy_term(AsBodyFnN+HeadIsN,AsBodyFnC+HeadIsC,_),
556+
number_vars_wo_conficts(AsBodyFnC+HeadIsC,AsBodyFn+HeadIs),
557+
(AsBodyFnC+HeadIsC=AsBodyFn+HeadIs),
558+
metta_body_macro(HeadIs, AsBodyFn, AsBodyFnOut),!,
559+
\+ \+ if_t(AsBodyFn\=@=AsBodyFnOut,
560+
( debug_info(metta_macro_in,c(print_tree([=,HeadIs, AsBodyFn]))),!,
561+
debug_info(metta_macro_out,c(print_tree([=,HeadIs, AsBodyFnOut]))))))),!.
562+
563+
metta_body_macro(HeadIs, AsBodyFn, AsBodyFnOut):-
564+
must_det_lls((metta_body_macro1(HeadIs, [], AsBodyFn, AsBodyFnE),
565+
metta_body_macro2(HeadIs, [], AsBodyFnE, AsBodyFnMid),
566+
(AsBodyFn =@= AsBodyFnMid -> AsBodyFnMid = AsBodyFnOut ;
567+
metta_body_macro(HeadIs, AsBodyFnMid, AsBodyFnOut)))).
568+
569+
metta_body_macro1(_HeadIs, _, AsBodyFn, AsBodyFn):- \+ compound(AsBodyFn), !.
570+
metta_body_macro1(_HeadIs, _, AsBodyFn, AsBodyFn):- \+ is_list(AsBodyFn), !.
571+
metta_body_macro1(HeadIs, Stack, [Op|AsBodyFn], AsBodyFnOut):- fail, \+ is_funcall_op(Op), !, maplist(metta_body_macro1(HeadIs, Stack), [Op|AsBodyFn], AsBodyFnOut),!.
572+
metta_body_macro1(HeadIs, Stack, [Op|AsBodyFn], AsBodyFnOut):-
573+
maplist(metta_body_macro1(HeadIs, [Op|Stack]), AsBodyFn, AsBodyFnMid),
574+
[Op|AsBodyFnMid]=OpAsBodyMid,
575+
copy_term(OpAsBodyMid,OpAsBodyMidCopy),
576+
metta_body_macro_final1(HeadIs, Stack, OpAsBodyMid , AsBodyFnOut),
577+
OpAsBodyMid=@=OpAsBodyMidCopy,!.
578+
579+
metta_body_macro_final1(_HeadIs,_Stack, [NonOp|More], AsBodyFn):- \+ atom(NonOp),!,[NonOp|More]= AsBodyFn.
580+
metta_body_macro_final1(_HeadIs,_Stack, ['if-unify',Var1,Var2|Rest], [if,['metta-unify',Var1,Var2]|Rest]).
581+
metta_body_macro_final1(_HeadIs,_Stack, ['if-equal',Var1,Var2|Rest], [if,['metta-equal',Var1,Var2]|Rest]).
582+
metta_body_macro_final1(_HeadIs,_Stack, ['if-decons-expr',Expr,Head,Tail|Rest],[if,['decons-ht',Expr,Head,Tail]|Rest]).
583+
metta_body_macro_final1(_HeadIs,_Stack, ['if-decons',Expr,Head,Tail|Rest],[if,['decons-ht',Expr,Head,Tail]|Rest]).
584+
metta_body_macro_final1(_HeadIs,_Stack, ['chain',[Ceval,Eval],Var|Rest], ['let',Var,Eval|Rest]):- Ceval == eval,!.
585+
metta_body_macro_final1(_HeadIs,_Stack, ['chain',Eval,Var|Rest], ['let',Var,Eval|Rest]).
586+
%metta_body_macro_final1(_HeadIs,_Stack, [eval,Next], Next).
587+
metta_body_macro_final1(_HeadIs,_Stack, AsBodyFnOut, AsBodyFnOut).
588+
589+
metta_body_macro2(_HeadIs, _, AsBodyFn, AsBodyFn):- \+ compound(AsBodyFn), !.
590+
metta_body_macro2(_HeadIs, _, AsBodyFn, AsBodyFn):- \+ is_list(AsBodyFn), !.
591+
metta_body_macro2(HeadIs, Stack, OpAsBody, AsBodyFnOutReally):-
592+
copy_term(OpAsBody,OpAsBodyMidCopy),
593+
metta_body_macro_final2(HeadIs, Stack, OpAsBody , AsBodyFnOut),
594+
OpAsBody=@=OpAsBodyMidCopy,!,
595+
maplist( metta_body_macro2(HeadIs, Stack), AsBodyFnOut,AsBodyFnOutReally).
596+
597+
metta_body_macro_final2(_HeadIs,_Stack, [NonOp|More], AsBodyFn):- \+ atom(NonOp),!,[NonOp|More]= AsBodyFn.
598+
metta_body_macro_final2(_HeadIs,_Stack, [eval,Next], Next).
599+
metta_body_macro_final2(_HeadIs,_Stack, AsBodyFnOut, AsBodyFnOut).
600+
543601

prolog/metta_lang/metta_debug.pl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -837,23 +837,25 @@
837837
mesg_color(Topic, TopicColor),
838838
mesg_color(Info, InfoColor),
839839
\+ \+ (( % numbervars(Info,4123,_,[attvar(bind)]),
840-
number_vars_wo_conficts(Info,RNVInfo),
840+
%number_vars_wo_conficts(Info,RNVInfo),
841+
if_t(var(RNVInfo),Info=RNVInfo),
841842
format(X,'~N ~@: ~@ ~n~n',[ansicall(TopicColor,write(Topic)),ansicall(InfoColor,debug_pp_info(RNVInfo))]))).
842843

843844
debug_pp_info(Info):- compound(Info), compound_name_arguments(Info,F,Args),!,debug_pp_cmpd(Info,F,Args).
844845
debug_pp_info(Info):- write_src(Info).
846+
debug_pp_cmpd(_Info,'c',[Call]):- !, nl, write(' '), call(Call).
845847
debug_pp_cmpd(Info,':-',_):- !, nl, write(' '), debug_pp_tree(Info).
846-
debug_pp_cmpd(Info,'[|]',_):- !, write_src(Info).
847-
debug_pp_cmpd(Info,_,_Args):- debug_pp_term(Info).
848-
debug_pp_now(Info):- pp_as_src(Info),!,debug_pp_src(Info).
848+
debug_pp_cmpd(Info,'[|]',_):- !, write_src(Info),!.
849+
debug_pp_cmpd(Info,_,_Args):- debug_pp_term(Info),!.
850+
debug_pp_now(Info):- pp_as_src(Info),!,debug_pp_src(Info),!.
849851
debug_pp_now(Info):- debug_pp_src(Info),!.
850852
debug_pp_now(Info):- debug_pp_tree(Info),!.
851853

852854

853855
%debug_pp_tree(Info):- ignore(catch(notrace(write_src_wi(Info)),E,((writeq(Info),nl,nop(((display(E=Info),bt))))))),!.
854-
debug_pp_src(Info):- ignore(catch(notrace( write_src(Info)),_,((debug_pp_tree(Info))))).
855-
debug_pp_tree(Info):- ignore(catch(notrace(print_tree(Info)),_,((debug_pp_term(Info))))).
856-
debug_pp_term(Info):- ignore(catch(notrace(print(Info)),E,((writeq(Info),nl,nop(((display(E=Info),bt))))))).
856+
debug_pp_src(Info):- ignore(catch(notrace( write_src(Info)),_,((nl,writeln(err),nl,debug_pp_tree(Info))))),!.
857+
debug_pp_tree(Info):- ignore(catch(notrace(print_tree(Info)),_,((nl,writeln(err),nl,debug_pp_term(Info))))),!.
858+
debug_pp_term(Info):- ignore(catch(notrace(print(Info)),E,((writeq(Info),nl,writeln(err),nl,nop(((display(E=Info),bt))))))),!.
857859

858860
pp_as_src(Info):- compound(Info), arg(_,Info,E),is_list(E),E=[H|_],is_list(H),!.
859861

prolog/metta_lang/metta_eval.pl

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -767,10 +767,13 @@
767767
eval_10(Eq,RetType,Depth,Space,Eval,Result)*->true;(Eval=Result).
768768

769769

770-
eval_20(Eq,RetType,Depth,Self,['eval',X],Res):- !,
770+
eval_20(Eq,RetType,Depth,Self,['capture',X],Res):- !,
771771
eval_args(Eq,RetType,Depth,Self,X, Res).
772772

773773

774+
eval_20(Eq,RetType,Depth,Self,['eval',X],Res):- !,
775+
eval_args(Eq,RetType,Depth,Self,X, Res).
776+
774777
eval_20(Eq,RetType,Depth,Self,['eval-for',Type,X],Res):- !,
775778
ignore(Type=RetType),
776779
eval_args(Eq,Type,Depth,Self,X, Res).
@@ -2904,8 +2907,23 @@
29042907
).
29052908

29062909

2907-
variant_by_type(X,Y):- var(X),!,X==Y.
2908-
variant_by_type(X,Y):- X=@=Y.
2910+
%variant_by_type(X,Y):- var(X),!,X==Y.
2911+
variant_by_type(X,Y):- var(X),!, matching_vn_relaxed(X,Y).
2912+
%variant_by_type(X,Y):- X=@=Y.
2913+
variant_by_type(X, Y) :-
2914+
copy_term(X+Y, X1+Y1, _Gs),
2915+
X1 =@= Y1, X1 = Y1,
2916+
term_variables(X, VX),
2917+
term_variables(Y, VY),
2918+
maplist(matching_vn_relaxed, VX, VY), !,
2919+
X1 = X, Y1 = Y.
2920+
2921+
2922+
matching_vn_relaxed(V1, V2) :-
2923+
( get_attr(V1, vn, VN1) ->
2924+
( get_attr(V2, vn, VN2) -> (VN1 == VN2, V1 = V2) ; (V1=V2) )
2925+
; V1 == V2 ).
2926+
29092927

29102928
call_as_p2(P2,X,Y):-
29112929
once(eval([P2,X,Y],TF)),
@@ -2917,7 +2935,7 @@
29172935

29182936
eval_20(Eq,RetType,Depth,Self,['unique',Eval],RetVal):- !,
29192937
term_variables(Eval+RetVal,Vars),
2920-
no_repeats_var(YY),
2938+
no_repeats_var(variant_by_type,YY),
29212939
eval_args(Eq,RetType,Depth,Self,Eval,RetVal),YY=Vars.
29222940

29232941
eval_20(Eq,RetType,Depth,Self,['unique-by',P2,Eval],RetVal):- !,
@@ -3183,8 +3201,11 @@
31833201
transpiler_peek(Sym,Len,Type,Fn):-
31843202
transpiler_predicate_store(_, Sym, Len, _, _, _, _),
31853203
if_t(var(Type),member(Type,['mx','mi','mc'])),
3186-
format(atom(Fn),'~w_~w__~w',[Type,Len,Sym]),
3187-
succ(Len,LenP1), current_predicate(Fn/LenP1).
3204+
format(atom(Fn),'~w__1_~w_~w',[Type,Len,Sym]),
3205+
succ(Len,LenP1), current_predicate(Fn/LenP1),
3206+
\+ transpiler_predicate_store(_,Sym,[Len],_,_,_,_),
3207+
% \+ transpiler_predicate_store(_,Sym,[Len],_,_,_,_).
3208+
\+ transpiler_clause_store(Sym,[Len],_,_,_,_,_,_,_).
31883209

31893210
eval_20(Eq, RetType, Depth, Self, [Sym | Args], Res) :- symbol(Sym), is_list(Args),
31903211
length(Args, Len),

prolog/metta_lang/metta_interp.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4091,7 +4091,7 @@
40914091
debug_info(assert_hooks,load_hook_not_ready(Load, Self, Fact)),
40924092
%fail,
40934093
!,
4094-
nop(woc(load_hook_compiler(Load, Self, Fact))).
4094+
woc(load_hook_compiler(Load, Self, Fact)).
40954095

40964096
load_hook1(Load, Self, Fact) :-
40974097
% Ensure the Metta compiler is ready for use.

0 commit comments

Comments
 (0)