%self_eval_l1t(X):- var(X),!. %self_eval_l1t(X):- string(X),!. %self_eval_l1t(X):- number(X),!. %self_eval_l1t([]). self_eval_l1t(X):- \+ callable(X),!. self_eval_l1t(X):- is_valid_nb_state(X),!. self_eval_l1t(X):- is_list(X),!,fail. %self_eval_l1t(X):- compound(X),!. %self_eval_l1t(X):- is_ref(X),!,fail. self_eval_l1t(X):- atom(X),!, \+ nb_current(X,_),!. self_eval_l1t('True'). self_eval_l1t('False'). self_eval_l1t('F'). :- nb_setval(self_space, '&self'). eval_l1ts_to(XX,Y):- Y==XX,!. eval_l1ts_to(XX,Y):- Y=='True',!, is_True(XX),!. %current_self(Space):- nb_current(self_space,Space). eval_l1t_args(A,AA):- current_self(Space), eval_l1t_args(11,Space,A,AA). %eval_l1t_args(Depth,_Self,X,_Y):- forall(between(6,Depth,_),write(' ')),writeqln(eval_l1t_args(X)),fail. eval_l1t_args(_Dpth,_Slf,X,Y):- nonvar(Y),X=Y,!. eval_l1t_args(Depth,Self,X,Y):- nonvar(Y),!,eval_l1t_args(Depth,Self,X,XX),eval_l1ts_to(XX,Y). eval_l1t_args(_Dpth,_Slf,X,Y):- self_eval_l1t(X),!,Y=X. eval_l1t_args(_Dpth,_Slf,[X|T],Y):- T==[], \+ callable(X),!,Y=[X]. eval_l1t_args(Depth,Self,[F|X],Y):- (F=='superpose' ; ( option_value(no_repeats,false))), mnotrace((D1 is Depth-1)),!, eval_l1t_args0(D1,Self,[F|X],Y). eval_l1t_args(Depth,Self,X,Y):- mnotrace((no_repeats_var(YY), D1 is Depth-1)), eval_l1t_args0(D1,Self,X,Y), mnotrace(( \+ (Y\=YY))). :- nodebug(metta(eval_l1t)). /* debugging_metta(G):-is_debugging((eval_l1t))->ignore(G);true. w_indent(Depth,Goal):- \+ \+ mnotrace(ignore((( format('~N'), setup_call_cleanup(forall(between(Depth,101,_),write(' ')),Goal, format('~N')))))). indentq(Depth,Term):- \+ \+ mnotrace(ignore((( format('~N'), setup_call_cleanup(forall(between(Depth,101,_),write(' ')),format('~q',[Term]), format('~N')))))). with_debug(Flag,Goal):- is_debugging(Flag),!, call(Goal). with_debug(Flag,Goal):- flag(eval_l1t_num,_,0), setup_call_cleanup(set_debug(Flag,true),call(Goal),set_debug(Flag,flase)). flag_to_var(Flag,Var):- atom(Flag), \+ atom_concat('trace-on-',_,Flag),!,atom_concat('trace-on-',Flag,Var). flag_to_var(metta(Flag),Var):- !, nonvar(Flag), flag_to_var(Flag,Var). flag_to_var(Flag,Var):- Flag=Var. set_debug(Flag,Val):- \+ atom(Flag), flag_to_var(Flag,Var), atom(Var),!,set_debug(Var,Val). set_debug(Flag,true):- !, debug(metta(Flag)),flag_to_var(Flag,Var),set_option_value(Var,true). set_debug(Flag,false):- nodebug(metta(Flag)),flag_to_var(Flag,Var),set_option_value(Var,false). if_trace((Flag;true),Goal):- !, notrace(( catch_err(ignore((Goal)),E,wdmsg(E-->if_trace((Flag;true),Goal))))). if_trace(Flag,Goal):- notrace((catch_err(ignore((is_debugging(Flag),Goal)),E,wdmsg(E-->if_trace(Flag,Goal))))). %maybe_efbug(SS,G):- efbug(SS,G)*-> if_trace(eval_l1t,wdmsg(SS=G)) ; fail. maybe_efbug(_,G):- call(G). %efbug(P1,G):- call(P1,G). efbug(_,G):- call(G). is_debugging(Flag):- var(Flag),!,fail. is_debugging((A;B)):- !, (is_debugging(A) ; is_debugging(B) ). is_debugging((A,B)):- !, (is_debugging(A) , is_debugging(B) ). is_debugging(not(Flag)):- !, \+ is_debugging(Flag). is_debugging(Flag):- Flag== false,!,fail. is_debugging(Flag):- Flag== true,!. is_debugging(Flag):- debugging(metta(Flag),TF),!,TF==true. is_debugging(Flag):- debugging(Flag,TF),!,TF==true. is_debugging(Flag):- flag_to_var(Flag,Var), (option_value(Var,true)->true;(Flag\==Var -> is_debugging(Var))). :- nodebug(metta(overflow)). */ eval_l1t_args0(Depth,_Slf,X,Y):- Depth<1,!,X=Y, (\+ trace_on_overflow-> true; flag(eval_l1t_num,_,0),debug(metta(eval_l1t))). eval_l1t_args0(_Dpth,_Slf,X,Y):- self_eval_l1t(X),!,Y=X. eval_l1t_args0(Depth,Self,X,Y):- Depth2 is Depth-1, eval_l1t_args11(Depth,Self,X,M), (M\=@=X ->eval_l1t_args0(Depth2,Self,M,Y);Y=X). eval_l1t_args11(_Dpth,_Slf,X,Y):- self_eval_l1t(X),!,Y=X. eval_l1t_args11(Depth,Self,X,Y):- \+ debugging(metta(eval_l1t)),!, eval_l1t_args1(Depth,Self,X,Y). eval_l1t_args11(Depth,Self,X,Y):- flag(eval_l1t_num,EX,EX+1), option_else(traclen,Max,100), (EX>Max->(nodebug(metta(eval_l1t)),write('Switched off tracing. For a longer trace !(pragma! tracelen 101))'));true), mnotrace((no_repeats_var(YY), D1 is Depth-1)), DR is 99-D1, if_trace(metta(eval_l1t),indentq(Depth,'-->'(EX,Self,X,depth(DR)))), Ret=retval(fail), call_cleanup(( eval_l1t_args1(D1,Self,X,Y), mnotrace(( \+ (Y\=YY), nb_setarg(1,Ret,Y)))), mnotrace(ignore(((Y\=@=X,if_trace(metta(eval_l1t),indentq(Depth,'<--'(EX,Ret)))))))), (Ret\=@=retval(fail)->true;(rtrace(eval_l1t_args0(D1,Self,X,Y)),fail)). :- discontiguous eval_l1t_args1/4. :- discontiguous eval_l1t_args2/4. eval_l1t_args1(_Dpth,_Slf,Name,Value):- atom(Name), nb_current(Name,Value),!. eval_l1t_args1(Depth,Self,[V|VI],VVO):- \+ is_list(VI),!, eval_l1t_args(Depth,Self,VI,VM), ( VM\==VI -> eval_l1t_args(Depth,Self,[V|VM],VVO) ; (eval_l1t_args(Depth,Self,V,VV), (V\==VV -> eval_l1t_args(Depth,Self,[VV|VI],VVO) ; VVO = [V|VI]))). eval_l1t_args1(_Dpth,_Slf,X,Y):- \+ is_list(X),!,Y=X. eval_l1t_args1(Depth,Self,[V|VI],[V|VO]):- var(V),is_list(VI),!,maplist(eval_l1t_args(Depth,Self),VI,VO). eval_l1t_args1(_Dpth,_Slf,['repl!'],'True'):- !, repl. eval_l1t_args1(Depth,Self,['!',Cond],Res):- !, call(eval_l1t_args(Depth,Self,Cond,Res)). eval_l1t_args1(Depth,Self,['rtrace',Cond],Res):- !, rtrace(eval_l1t_args(Depth,Self,Cond,Res)). eval_l1t_args1(Depth,Self,['time',Cond],Res):- !, time(eval_l1t_args(Depth,Self,Cond,Res)). eval_l1t_args1(Depth,Self,['print',Cond],Res):- !, eval_l1t_args(Depth,Self,Cond,Res),format('~N'),print(Res),format('~N'). % !(println! $1) eval_l1t_args1(Depth,Self,['println!',Cond],Res):- !, eval_l1t_args(Depth,Self,Cond,Res),format('~N'),print(Res),format('~N'). eval_l1t_args1(_Dpth,_Slf,List,Y):- is_list(List),maplist(self_eval_l1t,List),List=[H|_], \+ atom(H), !,Y=List. eval_l1t_args1(Depth,Self,['assertTrue', X],TF):- !, eval_l1t_args(Depth,Self,['assertEqual',X,'True'],TF). eval_l1t_args1(Depth,Self,['assertFalse',X],TF):- !, eval_l1t_args(Depth,Self,['assertEqual',X,'False'],TF). eval_l1t_args1(Depth,Self,['assertEqual',X0,Y0],RetVal):- !, subst_vars(X0,X),subst_vars(Y0,Y), l1t_loonit_assert_source_tf( ['assertEqual',X0,Y0], (bagof_eval_l1t(Depth,Self,X,XX), bagof_eval_l1t(Depth,Self,Y,YY)), equal_enough_for_test(XX,YY), TF), (TF=='True'->return_empty(RetVal);RetVal=[got,XX,expected,YY]). eval_l1t_args1(Depth,Self,['assertNotEqual',X0,Y0],RetVal):- !, subst_vars(X0,X),subst_vars(Y0,Y), l1t_loonit_assert_source_tf( ['assertNotEqual',X0,Y0], (setof_eval_l1t(Depth,Self,X,XX), setof_eval_l1t(Depth,Self,Y,YY)), \+ equal_enough(XX,YY), TF), (TF=='True'->return_empty(RetVal);RetVal=[got,XX,expected,not,YY]). eval_l1t_args1(Depth,Self,['assertEqualToResult',X0,Y0],RetVal):- !, subst_vars(X0,X),subst_vars(Y0,Y), l1t_loonit_assert_source_tf( ['assertEqualToResult',X0,Y0], (bagof_eval_l1t(Depth,Self,X,XX), =(Y,YY)), equal_enough_for_test(XX,YY), TF), (TF=='True'->return_empty(RetVal);RetVal=[got,XX,expected,YY]),!. l1t_loonit_assert_source_tf(Src,Goal,Check,TF):- copy_term(Goal,OrigGoal), l1t_loonit_asserts(Src, time_eval('\n; EVAL TEST\n;',Goal), Check), as_tf(Check,TF),!, ignore(( once((TF='True', is_debugging(pass));(TF='False', is_debugging(fail))), with_debug((eval_l1t),time_eval('Trace',OrigGoal)))). l1t_loonit_asserts(Src,Goal,Check):- loonit_asserts(Src,Goal,Check). /* sort_result(Res,Res):- \+ compound(Res),!. sort_result([And|Res1],Res):- is_and(And),!,sort_result(Res1,Res). sort_result([T,And|Res1],Res):- is_and(And),!,sort_result([T|Res1],Res). sort_result([H|T],[HH|TT]):- !, sort_result(H,HH),sort_result(T,TT). sort_result(Res,Res). unify_enough(L,L):-!. unify_enough(L,C):- is_list(L),into_list_args(C,CC),!,unify_lists(CC,L). unify_enough(C,L):- is_list(L),into_list_args(C,CC),!,unify_lists(CC,L). unify_enough(C,L):- \+ compound(C),!,L=C. unify_enough(L,C):- \+ compound(C),!,L=C. unify_enough(L,C):- into_list_args(L,LL),into_list_args(C,CC),!,unify_lists(CC,LL). unify_lists(C,L):- \+ compound(C),!,L=C. unify_lists(L,C):- \+ compound(C),!,L=C. unify_lists([C|CC],[L|LL]):- unify_enough(L,C),!,unify_lists(CC,LL). equal_enough(R,V):- is_list(R),is_list(V),sort(R,RR),sort(V,VV),!,equal_enouf(RR,VV),!. equal_enough(R,V):- copy_term(R,RR),copy_term(V,VV),equal_enouf(R,V),!,R=@=RR,V=@=VV. equal_enough_for_test(X,Y):- must_det_ll((subst_vars(X,XX),subst_vars(Y,YY))),!,equal_enough(XX,YY),!. equal_enouf(R,V):- R=@=V, !. equal_enouf(_,V):- V=@='...',!. equal_enouf(L,C):- is_list(L),into_list_args(C,CC),!,equal_enouf_l(CC,L). equal_enouf(C,L):- is_list(L),into_list_args(C,CC),!,equal_enouf_l(CC,L). %equal_enouf(R,V):- (var(R),var(V)),!, R=V. equal_enouf(R,V):- (var(R);var(V)),!, R==V. equal_enouf(R,V):- number(R),number(V),!, RV is abs(R-V), RV < 0.03 . equal_enouf(R,V):- atom(R),!,atom(V), has_unicode(R),has_unicode(V). equal_enouf(R,V):- (\+ compound(R) ; \+ compound(V)),!, R==V. equal_enouf(L,C):- into_list_args(L,LL),into_list_args(C,CC),!,equal_enouf_l(CC,LL). equal_enouf_l(C,L):- \+ compound(C),!,L=@=C. equal_enouf_l(L,C):- \+ compound(C),!,L=@=C. equal_enouf_l([C|CC],[L|LL]):- !, equal_enouf(L,C),!,equal_enouf_l(CC,LL). has_unicode(A):- atom_codes(A,Cs),member(N,Cs),N>127,!. set_last_error(_). */ eval_l1t_args1(Depth,Self,['match',Other,Goal,Template],Template):- into_space(Self,Other,Space),!, metta_atom_iter_l1t(Depth,Space,Goal). eval_l1t_args1(Depth,Self,['match',Other,Goal,Template,Else],Template):- (eval_l1t_args1(Depth,Self,['match',Other,Goal,Template],Template)*->true;Template=Else). % Macro: case eval_l1t_args1(Depth,Self,X,Res):- X= [CaseSym,A,CL],CaseSym == 'case', !, into_case_l1t_list(CL,CASES), findall(Key-Value, (nth0(Nth,CASES,Case0), (is_case_l1t(Key,Case0,Value), if_trace((case),(format('~N'), writeqln(c(Nth,Key)=Value))))),KVs),!, ((eval_l1t_args(Depth,Self,A,AA), if_trace((case),writeqln(switch=AA)), (select_case_l1t(Depth,Self,AA,KVs,Value)->true;(member(Void -Value,KVs),Void=='%void%'))) *->true;(member(Void -Value,KVs),Void=='%void%')), eval_l1t_args(Depth,Self,Value,Res). select_case_l1t(Depth,Self,AA,Cases,Value):- (best_key_l1t(AA,Cases,Value) -> true ; (maybe_special_key_l1ts(Depth,Self,Cases,CasES), (best_key_l1t(AA,CasES,Value) -> true ; (member(Void -Value,CasES),Void=='%void%')))). best_key_l1t(AA,Cases,Value):- ((member(Match-Value,Cases),unify_enough(AA,Match))->true; ((member(Match-Value,Cases),AA ==Match)->true; ((member(Match-Value,Cases),AA=@=Match)->true; (member(Match-Value,Cases),AA = Match)))). %into_case_l1t_list([[C|ASES0]],CASES):- is_list(C),!, into_case_l1t_list([C|ASES0],CASES),!. into_case_l1t_list(CASES,CASES):- is_list(CASES),!. is_case_l1t(AA,[AA,Value],Value):-!. is_case_l1t(AA,[AA|Value],Value). maybe_special_key_l1ts(Depth,Self,[K-V|KVI],[AK-V|KVO]):- eval_l1t_args(Depth,Self,K,AK), K\=@=AK,!, maybe_special_key_l1ts(Depth,Self,KVI,KVO). maybe_special_key_l1ts(Depth,Self,[_|KVI],KVO):- maybe_special_key_l1ts(Depth,Self,KVI,KVO). maybe_special_key_l1ts(_Depth,_Self,[],[]). %[collapse,[1,2,3]] eval_l1t_args1(Depth,Self,['collapse',List],Res):-!, bagof_eval_l1t(Depth,Self,List,Res). %[superpose,[1,2,3]] eval_l1t_args1(Depth,Self,['superpose',List],Res):- !, member(E,List),eval_l1t_args(Depth,Self,E,Res). get_l1t_sa_p1(P3,E,Cmpd,SA):- compound(Cmpd), get_l1t_sa_p2(P3,E,Cmpd,SA). get_l1t_sa_p2(P3,E,Cmpd,call(P3,N1,Cmpd)):- arg(N1,Cmpd,E). get_l1t_sa_p2(P3,E,Cmpd,SA):- arg(_,Cmpd,Arg),get_l1t_sa_p1(P3,E,Arg,SA). eval_l1t_args1(Depth,Self, Term, Res):- fail, mnotrace(( get_l1t_sa_p1(setarg,ST,Term,P1), % ST\==Term, compound(ST), ST = [F,List],F=='superpose',nonvar(List), %maplist(atomic,List), call(P1,Var))), !, %max_counting(F,20), member(Var,List), eval_l1t_args(Depth,Self, Term, Res). /* sub_sterm(Sub,Sub). sub_sterm(Sub,Term):- sub_sterm1(Sub,Term). sub_sterm1(_ ,List):- \+ compound(List),!,fail. sub_sterm1(Sub,List):- is_list(List),!,member(SL,List),sub_sterm(Sub,SL). sub_sterm1(_ ,[_|_]):-!,fail. sub_sterm1(Sub,Term):- arg(_,Term,SL),sub_sterm(Sub,SL). */ eval_l1t_args1(Depth,Self, Term, Res):- mnotrace(( get_l1t_sa_p1(setarg,ST,Term,P1), compound(ST), ST = [F,List],F=='collapse',nonvar(List), %maplist(atomic,List), call(P1,Var))), !, setof_eval_l1t(Depth,Self,List,Var), eval_l1t_args(Depth,Self, Term, Res). %max_counting(F,Max):- flag(F,X,X+1), X true; (flag(F,_,10),!,fail). eval_l1t_args1(Depth,Self,['if',Cond,Then],Res):- !, eval_l1t_args(Depth,Self,Cond,TF), (is_True(TF) -> eval_l1t_args(Depth,Self,Then,Res) ; Res = []). eval_l1t_args1(Depth,Self,['if',Cond,Then,Else],Res):- !, eval_l1t_args(Depth,Self,Cond,TF), (is_True(TF) -> eval_l1t_args(Depth,Self,Then,Res);eval_l1t_args(Depth,Self,Else,Res)). eval_l1t_args1(_Dpth,_Slf,[_,Nothing],Nothing):- 'Nothing'==Nothing,!. eval_l1t_args1(Depth,Self,['let',A,A5,AA],OO):- !, %(var(A)->true;trace), ((eval_l1t_args(Depth,Self,A5,AE), AE=A)), eval_l1t_args(Depth,Self,AA,OO). %eval_l1t_args1(Depth,Self,['let',A,A5,AA],AAO):- !,eval_l1t_args(Depth,Self,A5,A),eval_l1t_args(Depth,Self,AA,AAO). eval_l1t_args1(Depth,Self,['let*',[],Body],RetVal):- !, eval_l1t_args(Depth,Self,Body,RetVal). eval_l1t_args1(Depth,Self,['let*',[[Var,Val]|LetRest],Body],RetVal):- !, eval_l1t_args1(Depth,Self,['let',Var,Val,['let*',LetRest,Body]],RetVal). eval_l1t_args1(Depth,Self,['colapse'|List], Flat):- !, maplist(eval_l1t_args(Depth,Self),List,Res),flatten(Res,Flat). eval_l1t_args1(Depth,Self,['get-atoms',Other],PredDecl):- !,into_space(Self,Other,Space), metta_atom_iter_l1t(Depth,Space,PredDecl). eval_l1t_args1(_Dpth,_Slf,['car-atom',Atom],CAR):- !, Atom=[CAR|_],!. eval_l1t_args1(_Dpth,_Slf,['cdr-atom',Atom],CDR):- !, Atom=[_|CDR],!. eval_l1t_args1(Depth,Self,['Cons', A, B ],['Cons', AA, BB]):- no_cons_reduce, !, eval_l1t_args(Depth,Self,A,AA), eval_l1t_args(Depth,Self,B,BB). eval_l1t_args1(Depth,Self,['Cons', A, B ],[AA|BB]):- \+ no_cons_reduce, !, eval_l1t_args(Depth,Self,A,AA), eval_l1t_args(Depth,Self,B,BB). eval_l1t_args1(Depth,Self,['change-state!',StateExpr, UpdatedValue], Ret):- !, eval_l1t_args(Depth,Self,StateExpr,StateMonad), eval_l1t_args(Depth,Self,UpdatedValue,Value), 'change-state!'(Depth,Self,StateMonad, Value, Ret). eval_l1t_args1(Depth,Self,['new-state',UpdatedValue],StateMonad):- !, eval_l1t_args(Depth,Self,UpdatedValue,Value), 'new-state'(Depth,Self,Value,StateMonad). eval_l1t_args1(Depth,Self,['get-state',StateExpr],Value):- !, eval_l1t_args(Depth,Self,StateExpr,StateMonad), 'get-state'(StateMonad,Value). % eval_l1t_args1(Depth,Self,['get-state',Expr],Value):- !, eval_l1t_args(Depth,Self,Expr,State), arg(1,State,Value). % check_type:- option_else(typecheck,TF,'False'), TF=='True'. :- dynamic is_registered_state/1. :- flush_output. :- setenv('RUST_BACKTRACE',full). /* % Function to check if an value is registered as a state name :- dynamic(is_registered_state/1). is_nb_state(G):- is_valid_nb_state(G) -> true ; is_registered_state(G),nb_current(G,S),is_valid_nb_state(S). :- multifile(state_type_method/3). :- dynamic(state_type_method/3). space_type_method(is_nb_state,new_space,init_state). space_type_method(is_nb_state,clear_space,clear_nb_values). space_type_method(is_nb_state,add_atom,add_nb_value). space_type_method(is_nb_state,remove_atom,'change-state!'). space_type_method(is_nb_state,replace_atom,replace_nb_value). space_type_method(is_nb_state,atom_count,value_nb_count). space_type_method(is_nb_state,get_atoms,'get-state'). space_type_method(is_nb_state,atom_iter,value_nb_iter). state_type_method(is_nb_state,new_state,init_state). state_type_method(is_nb_state,clear_state,clear_nb_values). state_type_method(is_nb_state,add_value,add_nb_value). state_type_method(is_nb_state,remove_value,'change-state!'). state_type_method(is_nb_state,replace_value,replace_nb_value). state_type_method(is_nb_state,value_count,value_nb_count). state_type_method(is_nb_state,'get-state','get-state'). state_type_method(is_nb_state,value_iter,value_nb_iter). %state_type_method(is_nb_state,query,state_nb_query). % Clear all values from a state clear_nb_values(StateNameOrInstance) :- fetch_or_create_state(StateNameOrInstance, State), nb_setarg(1, State, []). % Function to confirm if a term represents a state is_valid_nb_state(State):- compound(State),functor(State,'State',_). % Find the original name of a given state state_original_name(State, Name) :- is_registered_state(Name), nb_current(Name, State). % Register and initialize a new state init_state(Name) :- State = 'State'(_,_), asserta(is_registered_state(Name)), nb_setval(Name, State). % Change a value in a state 'change-state!'(Depth,Self,StateNameOrInstance, UpdatedValue, Out) :- fetch_or_create_state(StateNameOrInstance, State), arg(2, State, Type), ( (check_type,\+ get_type(Depth,Self,UpdatedValue,Type)) -> (Out = ['Error', UpdatedValue, 'BadType']) ; (nb_setarg(1, State, UpdatedValue), Out = State) ). % Fetch all values from a state 'get-state'(StateNameOrInstance, Values) :- fetch_or_create_state(StateNameOrInstance, State), arg(1, State, Values). 'new-state'(Depth,Self,Init,'State'(Init, Type)):- check_type->get_type(Depth,Self,Init,Type);true. 'new-state'(Init,'State'(Init, Type)):- check_type->get_type(10,'&self',Init,Type);true. fetch_or_create_state(Name):- fetch_or_create_state(Name,_). % Fetch an existing state or create a new one fetch_or_create_state(State, State) :- is_valid_nb_state(State),!. fetch_or_create_state(NameOrInstance, State) :- ( atom(NameOrInstance) -> (is_registered_state(NameOrInstance) -> nb_current(NameOrInstance, State) ; init_state(NameOrInstance), nb_current(NameOrInstance, State)) ; is_valid_nb_state(NameOrInstance) -> State = NameOrInstance ; writeln('Error: Invalid input.') ), is_valid_nb_state(State). */ eval_l1t_args1(Depth,Self,['get-type',Val],Type):- !, get_type(Depth,Self,Val,Type),ground(Type),Type\==[], Type\==Val,!. % mnotrace(G):- once(G). /* is_decl_type(ST):- metta_type(_,_,Type),sub_term(T,Type),T=@=ST, \+ nontype(ST). is_type(Type):- nontype(Type),!,fail. is_type(Type):- is_decl_type(Type). is_type(Type):- atom(Type). nontype(Type):- var(Type),!. nontype('->'). nontype(N):- number(N). needs_eval_l1t(EvalMe):- is_list(EvalMe),!. get_type(_Dpth,_Slf,Var,'%Undefined%'):- var(Var),!. get_type(_Dpth,_Slf,Val,'Number'):- number(Val),!. get_type(Depth,Self,Expr,['StateMonad',Type]):- is_valid_nb_state(Expr),'get-state'(Expr,Val),!, get_type(Depth,Self,Val,Type). get_type(Depth,Self,EvalMe,Type):- needs_eval_l1t(EvalMe),eval_l1t_args(Depth,Self,EvalMe,Val), \+ needs_eval_l1t(Val),!, get_type(Depth,Self,Val,Type). get_type(_Dpth,Self,[Fn|_],Type):- symbol(Fn),metta_type(Self,Fn,List),last_element(List,Type), nonvar(Type), is_type(Type). get_type(_Dpth,Self,List,Type):- is_list(List),metta_type(Self,List,LType),last_element(LType,Type), nonvar(Type), is_type(Type). get_type(Depth,_Slf,Type,Type):- Depth<1,!. get_type(_Dpth,Self,List,Type):- is_list(List),metta_type(Self,Type,['->'|List]). get_type(Depth,Self,List,Types):- List\==[], is_list(List),Depth2 is Depth-1,maplist(get_type(Depth2,Self),List,Types). get_type(_Dpth,Self,Fn,Type):- symbol(Fn),metta_type(Self,Fn,Type),!. %get_type(Depth,Self,Fn,Type):- nonvar(Fn),metta_type(Self,Fn,Type2),Depth2 is Depth-1,get_type(Depth2,Self,Type2,Type). %get_type(Depth,Self,Fn,Type):- Depth>0,nonvar(Fn),metta_type(Self,Type,Fn),!. %,!,last_element(List,Type). get_type(Depth,Self,Expr,Type):-Depth2 is Depth-1, eval_l1t_args(Depth2,Self,Expr,Val),Expr\=@=Val,get_type(Depth2,Self,Val,Type). get_type(_Dpth,_Slf,Val,'String'):- string(Val),!. get_type(_Dpth,_Slf,Val,Type):- is_decl_type(Val),Type=Val. get_type(_Dpth,_Slf,Val,'Bool'):- (Val=='False';Val=='True'),!. get_type(_Dpth,_Slf,Val,'Symbol'):- symbol(Val). %get_type(Depth,Self,[T|List],['List',Type]):- Depth2 is Depth-1, is_list(List),get_type(Depth2,Self,T,Type),!, % forall((member(Ele,List),nonvar(Ele)),get_type(Depth2,Self,Ele,Type)),!. %get_type(Depth,_Slf,Cmpd,Type):- compound(Cmpd), functor(Cmpd,Type,1),!. get_type(_Dpth,_Slf,Cmpd,Type):- \+ ground(Cmpd),!,Type=[]. get_type(_Dpth,_Slf,_,'%Undefined%'):- fail. */ eval_l1t_args1(Depth,Self,['length',L],Res):- !, eval_l1t_args(Depth,Self,L,LL), !, (is_list(LL)->length(LL,Res);Res=1). eval_l1t_args1(Depth,Self,['CountElement',L],Res):- !, eval_l1t_args(Depth,Self,L,LL), !, (is_list(LL)->length(LL,Res);Res=1). /* is_feo_f('Cons'). is_seo_f('{...}'). is_seo_f('[...]'). is_seo_f('{}'). is_seo_f('[]'). is_seo_f('StateMonad'). is_seo_f('State'). is_seo_f('Event'). is_seo_f('Concept'). is_seo_f(N):- number(N),!. */ /* eval_l1t_args1(Depth,Self,[F,A|Args],Res):- \+ self_eval_l1t(A), eval_l1t_args(Depth,Self,A,AA),AA\==A, eval_l1t_args(Depth,Self,[F,AA|Args],Res). eval_l1t_args1(Depth,Self,[F,A1|AArgs],Res):- fail, member(F,['+']), cwdl(40,(( append(L,[A|R],AArgs), \+ self_eval_l1t(A), eval_l1t_args(Depth,Self,A,AA),AA\==A,!, append(L,[AA|R],NewArgs), eval_l1t_args(Depth,Self,[F,A1|NewArgs],Res)))). */ /* %% % !(assertEqualToResult ((inc) 2) (3)) eval_l1t_args1(Depth,Self,[F|Args],Res):- is_list(F), metta_atom_iter_l1t(Depth,Self,['=',F,R]), eval_l1t_args(Depth,Self,[R|Args],Res). eval_l1t_args1(Depth,Self,[F|Args],Res):- is_list(F), Args\==[], append(F,Args,FArgs),!,eval_l1t_args(Depth,Self,FArgs,Res). */ eval_l1t_args1(_Dpth,Self,['import!',Other,File],RetVal):- into_space(Self,Other,Space),!, include_metta(Space,File),!,return_empty(Space,RetVal). %RetVal=[]. eval_l1t_args1(Depth,Self,['bind!',Other,Expr],RetVal):- into_name(Self,Other,Name),!,eval_l1t_args(Depth,Self,Expr,Value),nb_setval(Name,Value), return_empty(Value,RetVal). eval_l1t_args1(Depth,Self,['pragma!',Other,Expr],RetVal):- into_name(Self,Other,Name),!,eval_l1t_args(Depth,Self,Expr,Value),set_option_value(Name,Value), return_empty(Value,RetVal). eval_l1t_args1(_Dpth,Self,['transfer!',File],RetVal):- !, include_metta(Self,File), return_empty(Self,RetVal). eval_l1t_args1(Depth,Self,['nop',Expr],Empty):- !, eval_l1t_args(Depth,Self,Expr,_), return_empty([],Empty). /* is_True(T):- T\=='False',T\=='F',T\==[]. is_and(S):- \+ atom(S),!,fail. is_and('#COMMA'). is_and(','). is_and('and'). is_and('And'). */ eval_l1t_args1(_Dpth,_Slf,[And],'True'):- is_and(And),!. eval_l1t_args1(Depth,Self,['and',X,Y],TF):- !, as_tf((eval_l1t_args(Depth,Self,X,'True'),eval_l1t_args(Depth,Self,Y,'True')),TF). eval_l1t_args1(Depth,Self,[And,X|Y],TF):- is_and(And),!,eval_l1t_args(Depth,Self,X,TF1), is_True(TF1),eval_l1t_args1(Depth,Self,[And|Y],TF). %eval_l1t_args2(Depth,Self,[H|T],_):- \+ is_list(T),!,fail. eval_l1t_args1(Depth,Self,['or',X,Y],TF):- !, as_tf((eval_l1t_args(Depth,Self,X,'True');eval_l1t_args(Depth,Self,Y,'True')),TF). eval_l1t_args1(_Dpth,Self,['add-atom',Other,PredDecl],TF):- !, into_space(Self,Other,Space), as_tf(do_metta(Space,load,PredDecl),TF). eval_l1t_args1(_Dpth,Self,['remove-atom',Other,PredDecl],TF):- !, into_space(Self,Other,Space), as_tf(do_metta(Space,unload,PredDecl),TF). eval_l1t_args1(_Dpth,Self,['atom-count',Other],Count):- !, into_space(Self,Other,Space), findall(_,metta_defn(Expander,Other,_,_),L1),length(L1,C1),findall(_,metta_atom(Space,_),L2),length(L2,C2),Count is C1+C2. eval_l1t_args1(_Dpth,Self,['atom-replace',Other,Rem,Add],TF):- !, into_space(Self,Other,Space), copy_term(Rem,RCopy), as_tf((metta_atom_iter_l1t_ref(Space,RCopy,Ref), RCopy=@=Rem,erase(Ref), do_metta(Other,load,Add)),TF). eval_l1t_args1(Depth,Self,['+',N1,N2],N):- number(N1),!, eval_l1t_args(Depth,Self,N2,N2Res), catch_err(N is N1+N2Res,_E,(set_last_error(['Error',N2Res,'Number']),fail)). eval_l1t_args1(Depth,Self,['-',N1,N2],N):- number(N1),!, eval_l1t_args(Depth,Self,N2,N2Res), catch_err(N is N1-N2Res,_E,(set_last_error(['Error',N2Res,'Number']),fail)). eval_l1t_args1(Depth,Self,[V|VI],[V|VO]):- nonvar(V),is_metta_data_functor(V),is_list(VI),!,maplist(eval_l1t_args(Depth,Self),VI,VO). eval_l1t_args1(Depth,Self,X,Y):- (eval_l1t_args2(Depth,Self,X,Y)*->true; (eval_l1t_args2_failed(Depth,Self,X,Y)*->true;X=Y)). eval_l1t_args2_failed(_Dpth,_Slf,T,TT):- T==[],!,TT=[]. eval_l1t_args2_failed(_Dpth,_Slf,T,TT):- var(T),!,TT=T. eval_l1t_args2_failed(_Dpth,_Slf,[F|LESS],Res):- once(eval_l1t_selfless([F|LESS],Res)),mnotrace([F|LESS]\==Res),!. %eval_l1t_args2_failed(Depth,Self,[V|Nil],[O]):- Nil==[], once(eval_l1t_args(Depth,Self,V,O)),V\=@=O,!. eval_l1t_args2_failed(Depth,Self,[H|T],[HH|TT]):- !, eval_l1t_args(Depth,Self,H,HH), eval_l1t_args2_failed(Depth,Self,T,TT). eval_l1t_args2_failed(Depth,Self,T,TT):- eval_l1t_args(Depth,Self,T,TT). %eval_l1t_args(Depth,Self,X,Y):- eval_l1t_args1(Depth,Self,X,Y)*->true;Y=[]. %eval_l1t_args1(Depth,_,_,_):- Depth<1,!,fail. %eval_l1t_args1(Depth,_,X,Y):- Depth<3, !, ground(X), (Y=X). %eval_l1t_args1(_Dpth,_Slf,X,Y):- self_eval_l1t(X),!,Y=X. % Kills zero arity functions eval_l1t_args1(Depth,Self,[X|Nil],[Y]):- Nil ==[],!,eval_l1t_args(Depth,Self,X,Y). /* into_values(List,Many):- List==[],!,Many=[]. into_values([X|List],Many):- List==[],is_list(X),!,Many=X. into_values(Many,Many). eval_l1t_args2(_Dpth,_Slf,Name,Value):- atom(Name), nb_current(Name,Value),!. */ % Macro Functions %eval_l1t_args1(Depth,_,_,_):- Depth<1,!,fail. eval_l1t_args2(Depth,_,X,Y):- Depth<3, !, fail, ground(X), (Y=X). eval_l1t_args2(Depth,Self,[F|PredDecl],Res):- Depth>1, mnotrace((sub_sterm1(SSub,PredDecl), ground(SSub),SSub=[_|Sub], is_list(Sub), maplist(atomic,SSub))), eval_l1t_args(Depth,Self,SSub,Repl), mnotrace((SSub\=Repl, subst(PredDecl,SSub,Repl,Temp))), eval_l1t_args(Depth,Self,[F|Temp],Res). % user defined function eval_l1t_args2(Depth,Self,[H|PredDecl],Res):- mnotrace(is_user_defined_head(Self,H)),!, eval_l1t_args30(Depth,Self,[H|PredDecl],Res). % function inherited by system eval_l1t_args2(Depth,Self,PredDecl,Res):- eval_l1t_args40(Depth,Self,PredDecl,Res). /* last_element(T,E):- \+ compound(T),!,E=T. last_element(T,E):- is_list(T),last(T,L),last_element(L,E),!. last_element(T,E):- compound_name_arguments(T,_,List),last_element(List,E),!. %catch_warn(G):- notrace(catch_err(G,E,(wdmsg(catch_warn(G)-->E),fail))). %catch_nowarn(G):- notrace(catch_err(G,error(_,_),fail)). %as_tf(G,TF):- catch_nowarn((call(G)*->TF='True';TF='False')). */ eval_l1t_selfless(['==',X,Y],TF):- as_tf(X=:=Y,TF),!. eval_l1t_selfless(['==',X,Y],TF):- as_tf(X=@=Y,TF),!. eval_l1t_selfless(['=',X,Y],TF):-!,as_tf(X=Y,TF). eval_l1t_selfless(['>',X,Y],TF):-!,as_tf(X>Y,TF). eval_l1t_selfless(['<',X,Y],TF):-!,as_tf(X',X,Y],TF):-!,as_tf(X>=Y,TF). eval_l1t_selfless(['<=',X,Y],TF):-!,as_tf(X= Bool Atom Atom)) (= (ift True $then) $then) ; For anything that is green, assert it is Green in &kb22 !(ift (green $x) (add-atom &kb22 (Green $x))) ; Retrieve the inferred Green things: Fritz and Sam. !(assertEqualToResult (match &kb22 (Green $x) $x) (Fritz Sam)) */ :- discontiguous eval_l1t_args3/4. %eval_l1t_args2(Depth,Self,PredDecl,Res):- eval_l1t_args3(Depth,Self,PredDecl,Res). %eval_l1t_args2(_Dpth,_Slf,L1,Res):- is_list(L1),maplist(self_eval_l1t,L1),!,Res=L1. %eval_l1t_args2(_Depth,_Self,X,X). /* is_user_defined_head(Other,H):- mnotrace(is_user_defined_head0(Other,H)). is_user_defined_head0(Other,[H|_]):- !, nonvar(H),!, is_user_defined_head_f(Other,H). is_user_defined_head0(Other,H):- callable(H),!,functor(H,F,_), is_user_defined_head_f(Other,F). is_user_defined_head0(Other,H):- is_user_defined_head_f(Other,H). is_user_defined_head_f(Other,H):- is_user_defined_head_f1(Other,H). is_user_defined_head_f(Other,H):- is_user_defined_head_f1(Other,[H|_]). %is_user_defined_head_f1(Other,H):- metta_type(Other,H,_). is_user_defined_head_f1(Other,H):- metta_atom(Other,[H|_]). is_user_defined_head_f1(Other,H):- metta_defn(Expander,Other,[H|_],_). %is_user_defined_head_f(_,H):- is_metta_builtin(H). is_special_op(F):- \+ atom(F), \+ var(F), !, fail. is_special_op('case'). is_special_op(':'). is_special_op('='). is_special_op('->'). is_special_op('let'). is_special_op('let*'). is_special_op('if'). is_special_op('rtrace'). is_special_op('or'). is_special_op('and'). is_special_op('not'). is_special_op('match'). is_special_op('call'). is_special_op('let'). is_special_op('let*'). is_special_op('nop'). is_special_op('assertEqual'). is_special_op('assertEqualToResult'). is_metta_builtin(Special):- is_special_op(Special). is_metta_builtin('=='). is_metta_builtin(F):- once(atom(F);var(F)), current_op(_,yfx,F). is_metta_builtin('println!'). is_metta_builtin('transfer!'). is_metta_builtin('collapse'). is_metta_builtin('superpose'). is_metta_builtin('+'). is_metta_builtin('-'). is_metta_builtin('*'). is_metta_builtin('/'). is_metta_builtin('%'). is_metta_builtin('=='). is_metta_builtin('<'). is_metta_builtin('>'). is_metta_builtin('all'). is_metta_builtin('import!'). is_metta_builtin('pragma!'). */ eval_l1t_args30(Depth,Self,H,B):- (eval_l1t_args34(Depth,Self,H,B)*->true;eval_l1t_args37(Depth,Self,H,B)). eval_l1t_args34(_Dpth,Self,H,B):- (metta_defn(Expander,Self,H,B);(metta_atom(Self,H),B='True')). % Has argument that is headed by the same function eval_l1t_args37(Depth,Self,[H1|Args],Res):- mnotrace((append(Left,[[H2|H2Args]|Rest],Args), H2==H1)),!, eval_l1t_args(Depth,Self,[H2|H2Args],ArgRes), mnotrace((ArgRes\==[H2|H2Args], append(Left,[ArgRes|Rest],NewArgs))), eval_l1t_args30(Depth,Self,[H1|NewArgs],Res). eval_l1t_args37(Depth,Self,[[H|Start]|T1],Y):- mnotrace((is_user_defined_head_f(Self,H),is_list(Start))), metta_defn(Expander,Self,[H|Start],Left), eval_l1t_args(Depth,Self,[Left|T1],Y). % Has subterm to eval_l1t eval_l1t_args37(Depth,Self,[F|PredDecl],Res):- Depth>1, quietly(sub_sterm1(SSub,PredDecl)), mnotrace((ground(SSub),SSub=[_|Sub], is_list(Sub),maplist(atomic,SSub))), eval_l1t_args(Depth,Self,SSub,Repl), mnotrace((SSub\=Repl,subst(PredDecl,SSub,Repl,Temp))), eval_l1t_args30(Depth,Self,[F|Temp],Res). %eval_l1t_args37(Depth,Self,X,Y):- (eval_l1t_args38(Depth,Self,X,Y)*->true;metta_atom_iter_l1t(Depth,Self,[=,X,Y])). eval_l1t_args37(Depth,Self,PredDecl,Res):- fail, ((term_variables(PredDecl,Vars), (metta_atom(Self,PredDecl) *-> (Vars ==[]->Res='True';Vars=Res); (eval_l1t_args(Depth,Self,PredDecl,Res),ignore(Vars ==[]->Res='True';Vars=Res))))), PredDecl\=@=Res. eval_l1t_args38(_Dpth,Self,[H|_],_):- mnotrace( \+ is_user_defined_head_f(Self,H) ), !,fail. eval_l1t_args38(_Dpth,Self,[H|T1],Y):- metta_defn(Expander,Self,[H|T1],Y). eval_l1t_args38(_Dpth,Self,[H|T1],'True'):- metta_atom(Self,[H|T1]). eval_l1t_args38(_Dpth,Self,CALL,Y):- fail,append(Left,[Y],CALL),metta_defn(Expander,Self,Left,Y). %eval_l1t_args3(Depth,Self,['ift',CR,Then],RO):- fail, !, %fail, % trace, % metta_defn(Expander,Self,['ift',R,Then],Become),eval_l1t_args(Depth,Self,CR,R),eval_l1t_args(Depth,Self,Then,_True),eval_l1t_args(Depth,Self,Become,RO). metta_atom_iter_l1t(_Dpth,Other,[Equal,H,B]):- '=' == Equal,!, (metta_defn(Expander,Other,H,B)*->true;(metta_atom(Other,H),B='True')). metta_atom_iter_l1t(Depth,_,_):- Depth<3,!,fail. metta_atom_iter_l1t(_Dpth,_Slf,[]):-!. metta_atom_iter_l1t(_Dpth,Other,H):- metta_atom(Other,H). metta_atom_iter_l1t(Depth,Other,H):- D2 is Depth -1, metta_defn(Expander,Other,H,B),metta_atom_iter_l1t(D2,Other,B). metta_atom_iter_l1t(_Dpth,_Slf,[And]):- is_and(And),!. metta_atom_iter_l1t(Depth,Self,[And,X|Y]):- is_and(And),!,D2 is Depth -1, metta_atom_iter_l1t(D2,Self,X),metta_atom_iter_l1t(D2,Self,[And|Y]). /* metta_atom_iter_l1t2(_,Self,[=,X,Y]):- metta_defn(Expander,Self,X,Y). metta_atom_iter_l1t2(_Dpth,Other,[Equal,H,B]):- '=' == Equal,!, metta_defn(Expander,Other,H,B). metta_atom_iter_l1t2(_Dpth,Self,X,Y):- metta_defn(Expander,Self,X,Y). %, Y\=='True'. metta_atom_iter_l1t2(_Dpth,Self,X,Y):- metta_atom(Self,[=,X,Y]). %, Y\=='True'. */ metta_atom_iter_l1t_ref(Other,['=',H,B],Ref):-clause(metta_defn(Expander,Other,H,B),true,Ref). metta_atom_iter_l1t_ref(Other,H,Ref):-clause(metta_atom(Other,H),true,Ref). %not_compound(Term):- \+ is_list(Term),!. %eval_l1t_args2(Depth,Self,Term,Res):- maplist(not_compound,Term),!,eval_l1t_args345(Depth,Self,Term,Res). % function inherited by system eval_l1t_args40(Depth,Self,[F|X],FY):- is_function(F), \+ is_special_op(F), is_list(X), maplist(eval_l1t_args(Depth,Self),X,Y),!,eval_l1t_args5(Depth,Self,[F|Y],FY). eval_l1t_args40(Depth,Self,FX,FY):- eval_l1t_args5(Depth,Self,FX,FY). eval_l1t_args5(_Dpth,_Slf,[F|LESS],Res):- once(eval_l1t_selfless([F|LESS],Res)),mnotrace(([F|LESS]\==Res)),!. eval_l1t_args5(Depth,Self,[AE|More],TF):- length(More,Len), (is_syspred(AE,Len,Pred),catch_warn(as_tf(apply(Pred,More),TF)))*->true;eval_l1t_args6(Depth,Self,[AE|More],TF). eval_l1t_args6(_Dpth,_Slf,[AE|More],TF):- length([AE|More],Len), is_syspred(AE,Len,Pred),append(More,[TF],Args),!,catch_warn(apply(Pred,Args)). %eval_l1t_args40(Depth,Self,[X1|[F2|X2]],[Y1|Y2]):- is_function(F2),!,eval_l1t_args(Depth,Self,[F2|X2],Y2),eval_l1t_args(Depth,Self,X1,Y1). %cwdl(DL,Goal):- call_with_depth_limit(Goal,DL,R), (R==depth_limit_exceeded->(!,fail);true). bagof_eval_l1t(Depth,Self,X,L):- !,findall(E,eval_l1t_args(Depth,Self,X,E),L). setof_eval_l1t(Depth,Self,X,S):- !,findall(E,eval_l1t_args(Depth,Self,X,E),L),sort(L,S). %setof_eval_l1t(Depth,Self,X,S):- setof(E,eval_l1t_args(Depth,Self,X,E),S)*->true;S=[].