Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrickTench committed Dec 19, 2024
2 parents f658a38 + 1cde39d commit 2adc31a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 56 deletions.
77 changes: 39 additions & 38 deletions prolog/metta_lang/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,19 @@
eval_true(X):- eval_args(X,Y), once(var(Y) ; \+ is_False(Y)).

eval(Depth,Self,X,Y):- eval('=',_,Depth,Self,X,Y).
eval(Eq,RetType,Depth,Self,X,Y):-
catch_metta_return(eval_args(Eq,RetType,Depth,Self,X,Y),Y).
eval(Eq,RetType,Depth,Self,X,O):-
eval_reducable(Eq,RetType,Depth,Self,X,eval_args(Eq,RetType,Depth,Self,X,Y),Y,O).


eval_reducable(Eq,RetType,Depth,Self,X,G,Y,O):- catch_metta_return(G,Y), return_x_g_y(Eq,RetType,Depth,Self,X,X,Y,O).

return_x_g_y(_Eq,_RetType,_Depth,_Self,X,_,Y,R):- Y == 'NotReducable',!,R=X.
return_x_g_y(Eq,RetType,Depth, Self,X,M,Y,R):- M\=@=Y, !, eval_args(Eq,RetType,Depth,Self,Y,Z), return_x_g_y(Eq,RetType,Depth,Self,X,Y,Z,R).
return_x_g_y(_Eq,_RetType,_Depth,_Self,_X,_M,R,R).

catch_metta_return(G,Y):-
catch(G,metta_return(Y),ignore(retract(thrown_metta_return(Y)))).


%:- set_prolog_flag(gc,false).
/*
Expand Down Expand Up @@ -227,8 +238,7 @@
eval_ret(Eq,RetType,Depth,Self,X,Y):-
eval_00(Eq,RetType,Depth,Self,X,Y), is_returned(Y).

catch_metta_return(G,Y):-
catch(G,metta_return(Y),ignore(retract(thrown_metta_return(Y)))).


allow_repeats_eval_(_):- !.
allow_repeats_eval_(_):- option_value(no_repeats,false),!.
Expand All @@ -243,7 +253,7 @@
eval_00(Eq,RetType,Depth,Self,X,YO):-
eval_01(Eq,RetType,Depth,Self,X,YO).
eval_01(Eq,RetType,Depth,Self,X,YO):-
X\==[empty], % speed up n-queens x60
% X\==[empty], % speed up n-queens x60 but breaks other things
if_t((Depth<1, trace_on_overflow),
debug(metta(eval_args))),
notrace((Depth2 is Depth-1, copy_term(X, XX))),
Expand All @@ -253,7 +263,7 @@
;eval_01(Eq,RetType,Depth2,Self,M,Y)),
eval_02(Eq,RetType,Depth2,Self,Y,YO))).

eval_02(Eq,RetType,Depth2,Self,Y,YO):- Y\==[empty], % speed up n-queens x60
eval_02(Eq,RetType,Depth2,Self,Y,YO):- % Y\==[empty], % speed up n-queens x60 but breaks other things
once(if_or_else((subst_args_here(Eq,RetType,Depth2,Self,Y,YO)),
if_or_else((fail,finish_eval(Eq,RetType,Depth2,Self,Y,YO)),
Y=YO))).
Expand All @@ -268,7 +278,8 @@
% % this next one at least causes no failures and 5x speedup
subst_args_here(_Eq,_RetType,_Depth2,_Self,Y,YO):- wont_need_subst(Y),!, Y=YO.
subst_args_here(Eq,RetType,Depth2,Self,Y,YO):-
subst_args(Eq,RetType,Depth2,Self,Y,YO),
%subst_args(Eq,RetType,Depth2,Self,Y,YO),
Y =YO,
notrace(if_t_else((wont_need_subst(Y),Y\=@=YO),
(write_src_uo(needed_subst_args(Y,YO)),bt,sleep(1.0)),
nop(write_src_uo(unneeded_subst_args(Y))))).
Expand Down Expand Up @@ -1249,15 +1260,13 @@


eval_20(Eq,RetType,Depth,Self,['if',Cond,Then,Else],Res):- !,
eval_args(Eq,'Bool',Depth,Self,Cond,TF),
(is_True(TF)
-> eval_args(Eq,RetType,Depth,Self,Then,Res)
(eval_args_true(Eq,'Bool',Depth,Self,Cond)
*-> eval_args(Eq,RetType,Depth,Self,Then,Res)
; eval_args(Eq,RetType,Depth,Self,Else,Res)).

eval_20(Eq,RetType,Depth,Self,['If',Cond,Then,Else],Res):- !,
eval_args(Eq,'Bool',Depth,Self,Cond,TF),
(is_True(TF)
-> eval_args(Eq,RetType,Depth,Self,Then,Res)
(eval_args_true(Eq,'Bool',Depth,Self,Cond)
*-> eval_args(Eq,RetType,Depth,Self,Then,Res)
; eval_args(Eq,RetType,Depth,Self,Else,Res)).

eval_20(Eq,RetType,Depth,Self,['If',Cond,Then],Res):- !,
Expand Down Expand Up @@ -1971,9 +1980,9 @@
(!,write_src(E),fail))),!.


empty('Empty').
','(A,B,(AA,BB)):- eval_args(A,AA),eval_args(B,BB).
':'(A,B,[':',A,B]).
%empty('Empty').
%','(A,B,(AA,BB)):- eval_args(A,AA),eval_args(B,BB).
%':'(A,B,[':',A,B]).
'<'(A,B,TFO):- as_tf(A<B,TF),!,TF=TFO.
'>'(A,B,TFO):- as_tf(A<B,TF),!,TF=TFO.
minus(A,B,C):- plus(B,C,A).
Expand Down Expand Up @@ -2270,25 +2279,15 @@
*/
%eval_40(Eq,RetType,_Dpth,_Slf,['==',X,Y],Res):- !, subst_args(Eq,RetType,_Dpth,_Slf,['==',X,Y],Res).

eval_20(Eq,RetType,Depth,Self,[EQ, X,Y],Res):- EQ=='==', using_all_spaces, !,
eval_20(Eq,RetType,Depth,Self,['==', X,Y],TF):- !,
suggest_type(RetType,'Bool'),
as_tf(eval_until_unify(Eq,_SharedType,Depth,Self,X,Y),Res).

eval_20(Eq,RetType,_Depth,_Self,[EQ,X,Y],TF):- EQ=='==', !,
suggest_type(RetType,'Bool'), !,
as_tf(eval_until_unify(Eq,_SharedType, X, Y), TF).
%eq_unify(Eq,_SharedType,Depth,Self, X, Y, Res).

as_tf(eval_until_unify(Eq,_SharedType,Depth,Self,X,Y), TF).

eq_unify(Eq,RetType,Depth,Self,X,Y, TF):- as_tf(eval_until_unify(Eq,RetType,Depth,Self,X,Y), TF).
% eq_unify( Eq, SharedType, X, Y, TF):- as_tf(eval_until_unify(Eq,SharedType, X, Y), TF).


eval_20(_Eq,RetType,_Dpth,_Slf,[EQ,X,Y],TF):- EQ=='===', !,
eval_20(Eq,RetType,Depth,Self,_Slf,['===',X,Y],TF):- !,
suggest_type(RetType,'Bool'),
as_tf(X==Y,TF).
as_tf(\+ \+ eval_until_unify(Eq,_SharedType,Depth,Self,X,Y), TF).

eval_20(_Eq,RetType,_Dpth,_Slf,[EQ,X,Y],TF):- EQ=='====', !,
eval_20(_Eq,RetType,_Dpth,_Slf,['====',X,Y],TF):- !,
suggest_type(RetType,'Bool'),
as_tf(same_terms(X,Y),TF).

Expand Down Expand Up @@ -2695,15 +2694,19 @@
:-if(true).
:- nodebug(metta('defn')).

eval_maybe_defn(Eq,RetType,Depth,Self,X,Res):-

eval_maybe_defn(Eq,RetType,Depth,Self,X,O):-
eval_reducable(Eq,RetType,Depth,Self,X,eval_maybe_defn_now(Eq,RetType,Depth,Self,X,Res),Res,O).

eval_maybe_defn_now(Eq,RetType,Depth,Self,X,Res):-
\+ fail_on_constructor,
\+ \+ (curried_arity(X,F,A),
is_metta_type_constructor(Self,F,AA),
( \+ AA\=A ),!,
if_trace(e,color_g_mesg('#772000',
indentq2(Depth,defs_none_cached((F/A/AA)=X))))),!,
eval_constructor(Eq,RetType,Depth,Self,X,Res).
eval_maybe_defn(Eq,RetType,Depth,Self,X,Y):- can_be_ok(eval_maybe_defn,X),!,
eval_maybe_defn_now(Eq,RetType,Depth,Self,X,Y):- can_be_ok(eval_maybe_defn,X),!,
trace_eval(eval_defn_choose_candidates(Eq,RetType),'defn',Depth,Self,X,Y).

eval_constructor(Eq,RetType,Depth,Self,X,Res):-
Expand Down Expand Up @@ -2833,16 +2836,14 @@
findall_eval(_Eq,_RetType,_Dpth,_Slf,X,L):- self_eval(X),!,L=[X].
findall_eval(_Eq,_RetType,_Dpth,_Slf,X,L):- typed_list(X,_Type,L),!.
findall_eval(Eq,RetType,Depth,Self,Funcall,L):-
findall_ne(E,
catch_metta_return(eval_args(Eq,RetType,Depth,Self,Funcall,E),E),L).
findall_ne(E,eval(Eq,RetType,Depth,Self,Funcall,E),L).

%bagof_eval(Eq,RetType,Depth,Self,X,L):- bagof_eval(Eq,RetType,_RT,Depth,Self,X,L).
%bagof_eval(Eq,RetType,Depth,Self,X,S):- bagof(E,eval_ne(Eq,RetType,Depth,Self,X,E),S)*->true;S=[].
bagof_eval(_Eq,_RetType,_Dpth,_Slf,X,L):- self_eval(X),!,L=[X].
bagof_eval(_Eq,_RetType,_Dpth,_Slf,X,L):- typed_list(X,_Type,L),!.
bagof_eval(Eq,RetType,Depth,Self,Funcall,L):-
bagof_ne(E,
catch_metta_return(eval_args(Eq,RetType,Depth,Self,Funcall,E),E),L).
bagof_ne(E,eval(Eq,RetType,Depth,Self,Funcall,E),L).

setof_eval(Depth,Self,Funcall,L):- setof_eval('=',_RT,Depth,Self,Funcall,L).
setof_eval(Eq,RetType,Depth,Self,Funcall,S):- findall_eval(Eq,RetType,Depth,Self,Funcall,L),
Expand All @@ -2858,7 +2859,7 @@
((eval_args(Eq,RetType,Depth,Self,Funcall,E))
*-> is_returned(E);(fail,E=Funcall)).

is_returned(E):- notrace( \+ is_empty(E)).
is_returned(E):- notrace( \+ is_empty(E)), nop(assertion(E \== 'NotReducable')).
is_empty(E):- notrace(( nonvar(E), sub_var('Empty',E))),!.


Expand Down
26 changes: 18 additions & 8 deletions prolog/metta_lang/metta_interp.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1400,10 +1400,10 @@

:- dynamic(metta_atom_asserted/2).
:- multifile(metta_atom_asserted/2).
:- dynamic(metta_atom_asserted_deduced/2).
:- multifile(metta_atom_asserted_deduced/2).
:- dynamic(metta_atom_deduced/2).
:- multifile(metta_atom_deduced/2).
metta_atom_asserted(X,Y):-
metta_atom_asserted_deduced(X,Y),
metta_atom_deduced(X,Y),
\+ clause(metta_atom_asserted(X,Y),true).


Expand Down Expand Up @@ -1439,26 +1439,36 @@
% \+ \+ metta_atom_asserted('&corelib',[=,[F|_]|_]),
write_src_uo([H,A|T]).

/*
should_inherit_op_from_corelib('=').

is_code_inheritor(KB):- current_self(KB). % code runing from a KB can see corlib
%should_inherit_op_from_corelib('=').
should_inherit_op_from_corelib(':').
should_inherit_op_from_corelib('@doc').
%should_inherit_op_from_corelib(_).
*/

metta_atom_asserted('&self','&corelib').
metta_atom_asserted('&self','&stdlib').
metta_atom_asserted('top','&corelib').
metta_atom_asserted('top','&stdlib').
metta_atom_asserted('&stdlib','&corelib').
metta_atom_asserted('&flybase','&corelib').
metta_atom_asserted('&catalog','&corelib').
metta_atom_asserted('&catalog','&stdlib').

/*
maybe_resolve_space_dag(Var,[XX]):- var(Var),!, \+ attvar(Var), freeze(XX,space_to_ctx(XX,Var)).
maybe_resolve_space_dag('&self',[Self]):- current_self(Self).
in_dag(X,XX):- is_list(X),!,member(XX,X).
in_dag(X,X).

space_to_ctx(Top,Var):- nonvar(Top),current_self(Top),!,Var='&self'.
space_to_ctx(Top,Var):- 'mod-space'(Top,Var).

'mod-space'(top,'&self').
'mod-space'(catalog,'&catalog').
'mod-space'(corelib,'&corelib').
'mod-space'(stdlib,'&stdlib').
'mod-space'(Top,'&self'):- Top == self.
*/

not_metta_atom_corelib(A,N):- A \== '&corelib' , metta_atom('&corelib',N).

%metta_atom_asserted_fallback( KB,Atom):- metta_atom_stdlib(KB,Atom)
Expand Down
10 changes: 5 additions & 5 deletions prolog/metta_lang/metta_loader.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3647,11 +3647,11 @@
forall(metta_type('&corelib', Symb, Def),
gen_interp_stubs('&corelib', Symb, Def)).

% Dynamic and multifile declaration for metta_atom_asserted_deduced/2.
:- dynamic(metta_atom_asserted_deduced/2).
:- multifile(metta_atom_asserted_deduced/2).
% Dynamic and multifile declaration for metta_atom_deduced/2.
:- dynamic(metta_atom_deduced/2).
:- multifile(metta_atom_deduced/2).

%! metta_atom_asserted_deduced(+Source, +Term) is nondet.
%! metta_atom_deduced(+Source, +Term) is nondet.
%
% Determines if a `Term` is part of the core library, logging the term if so.
%
Expand All @@ -3661,7 +3661,7 @@
% @arg Source The source of the term, expected to be `&corelib`.
% @arg Term The term to verify.
%
metta_atom_asserted_deduced('&corelib', Term) :- fail,
metta_atom_deduced('&corelib', Term) :- fail,
% Log terms matching core library types.
%\+ did_generate_interpreter_stubs,
metta_atom_corelib_types(Term),
Expand Down
5 changes: 3 additions & 2 deletions prolog/metta_lang/metta_repl.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@
(C==esc('[B',[27,91,66]) -> (nb_setarg(3, Control, leap),Cut=false,Next=true) ;
(C=='L' -> nb_setarg(2, Control, ResNum) ;
(C=='l' -> (nb_setarg(3, Control, leap),Next=true) ;
(((C=='\n');(C=='\r')) -> (Cut=true,Next=false);
(((C=='\n');(C=='\r')) -> (Cut=false,nb_setarg(3, Control, leap),Next=true);
(C=='g' -> write_src(exec(TermV));
(C=='s' -> (Cut=true,Next=false);
(true -> (write('Unknown Char'),fail))))))))))))))))))),
Expand All @@ -1258,7 +1258,7 @@
) *-> % Each forall_interactive
(((flag(result_num,ResNum,ResNum),ResNum >= MaxResults) -> ! ; true),ignore(Result = res(FOut)),ignore(Output = (FOut)))
; % Last forall_interactive
(flag(result_num,ResNum,ResNum),(ResNum==0-> (old_not_compatio(format('~N;; no-results ;; ~n~n')),!,true);true))
(flag(result_num,ResNum,ResNum),(ResNum==0-> (old_not_compatio(format('~N;; no-results ;; ~n')),!,true);true))

),

Expand Down Expand Up @@ -1317,6 +1317,7 @@



%old_not_compatio(_G):- \+ is_testing, !.
old_not_compatio(G):- call(G),ttyflush.

%! maybe_assign(+N_V) is det.
Expand Down
6 changes: 3 additions & 3 deletions prolog/metta_lang/metta_types.pl
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@
% Ensure no repeated types using no_repeats_var/1.
no_repeats_var(NoRepeatType),
% Retrieve the type of the value.
get_type_each(Depth, Self, Val, Type),
get_type_each(Depth, Self, Val, Type), Type\=='',
% Ensure the type matches the expected no-repeat type.
NoRepeatType = Type,
Type = TypeO,
Expand All @@ -452,7 +452,7 @@
%
% Succeeds if only the first matching type should be returned.
%
return_only_first_type :-
return_only_first_type :- fail,
% Check if the flag is set to true.
true_flag.

Expand Down Expand Up @@ -1311,7 +1311,7 @@
%
ignored_args_conform(Depth, Self, A, L) :-
% If either Args or List is not a conz structure, succeed without further checks.
(\+ iz_conz(Args); \+ iz_conz(List)), !.
(\+ iz_conz(A); \+ iz_conz(L)), !.
ignored_args_conform(Depth, Self, A, L) :-
% Check if each argument conforms to its corresponding expected type.
maplist(ignored_arg_conform(Depth, Self), A, L).
Expand Down

0 comments on commit 2adc31a

Please sign in to comment.