Skip to content

Commit 6e553d8

Browse files
committed
prolog/metta_lang/metta_typed_functions.pl doubles speed
1 parent 0c9819f commit 6e553d8

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

prolog/metta_lang/metta_eval.pl

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
:- dynamic(symbol_impl_not_exists/3).
6868
:- discontiguous(symbol_impl_only/3).
6969

70+
current_predicate_fast(FA):-
71+
current_predicate(FA).
72+
7073
%self_eval0(X):- var(X),!,fail.
7174
self_eval0(X):- \+ callable(X),!.
7275
self_eval0(X):- is_valid_nb_state(X),!.
@@ -1845,7 +1848,8 @@
18451848

18461849

18471850
eval_space(Eq,RetType,_Dpth,_Slf,['add-atom',Space,PredDecl],Res):- !,
1848-
do_metta(python,load,Space,PredDecl,TF),make_nop(RetType,TF,Res),check_returnval(Eq,RetType,Res).
1851+
do_metta(python,load,Space,PredDecl,TF),make_nop(RetType,TF,Res),check_returnval(Eq,RetType,Res).
1852+
18491853

18501854
eval_space(Eq,RetType,_Dpth,_Slf,['remove-atom',Space,PredDecl],Res):- !,
18511855
do_metta(python,unload_all,Space,PredDecl,TF),
@@ -2346,7 +2350,7 @@
23462350
into_listoid(AtomC,Atom):- is_list(AtomC),!,Atom=AtomC.
23472351
into_listoid(AtomC,Atom):- typed_list(AtomC,_,Atom),!.
23482352

2349-
:- if( \+ current_predicate( typed_list / 3 )).
2353+
:- if( \+ current_predicate_fast( typed_list / 3 )).
23502354
typed_list(Cmpd,Type,List):- compound(Cmpd), Cmpd\=[_|_], compound_name_arguments(Cmpd,Type,[List|_]),is_list(List).
23512355
:- endif.
23522356

@@ -3075,12 +3079,13 @@
30753079
current_function_arity(FF_mc,A).
30763080
*/
30773081

3082+
30783083
current_predicate_arity(F,A):-
30793084
metta_atom('&self',[:,F,[->|Args]]),
30803085
!,
30813086
length(Args,A).
30823087
current_predicate_arity(F,A):-
3083-
current_predicate(F/A).
3088+
current_predicate_fast(F/A).
30843089

30853090
current_function_arity(F,A):-
30863091
current_predicate_arity(F,PA)
@@ -3123,10 +3128,10 @@
31233128
%((ignore(pfcRemove(do_compile(KB,X,_))),
31243129
% pfcWatch,
31253130
pfcAdd_Now(do_compile(KB,X,_)),
3126-
if_t( \+ current_predicate(X/_),
3131+
if_t( \+ current_predicate_fast(X/_),
31273132
forall(metta_defn(KB,[X | Args] ,BodyFn),
31283133
compile_metta_defn(KB,X,Len,Args,BodyFn,_Clause))),
3129-
if_t( \+ current_predicate(X/_),
3134+
if_t( \+ current_predicate_fast(X/_),
31303135
(ignore(nortrace),forall(metta_defn(KB,[X | Args] ,BodyFn),
31313136
(maybe_trace(compile_metta_defn),compile_metta_defn(KB,X,Len,Args,BodyFn,_ClauseU))))),
31323137
% pfcNoWatch,
@@ -3158,7 +3163,7 @@
31583163
len_or_unbound(More,Len),
31593164

31603165
must_det_ll((
3161-
current_predicate(AE/Arity),
3166+
current_predicate_fast(AE/Arity),
31623167
maplist(as_prolog_x(Depth,Self), More , Adjusted))),!,
31633168
eval_201(Eq,RetType,Depth,Self,MettaPred,Adjusted,Arity,Len,Res),
31643169
nonvar(Res),
@@ -3320,8 +3325,8 @@
33203325
%must_use_eval(_,2):- fail.
33213326

33223327
call_as_p2a(F2,A,B):- unnegate_f2(F2,P2),!, \+ call_as_p2(P2,A,B).
3323-
call_as_p2a(P2,A,B):- current_predicate(P2/2),!,call(P2,A,B).
3324-
call_as_p2a(P2,A,B):- current_predicate(P2/3),!,call(P2,A,B,RetVal),f2_success(RetVal,A,B).
3328+
call_as_p2a(P2,A,B):- current_predicate_fast(P2/2),!,call(P2,A,B).
3329+
call_as_p2a(P2,A,B):- current_predicate_fast(P2/3),!,call(P2,A,B,RetVal),f2_success(RetVal,A,B).
33253330
call_as_p2a(F,X,Y):- must_use_eval(F,2), !,
33263331
once(eval([F,X,Y],RetVal)),
33273332
f2_success(RetVal,X,Y).
@@ -3331,7 +3336,7 @@
33313336

33323337
f2_success(RetVal,A,B):- once(RetVal=='True';RetVal==A;RetVal==B).
33333338

3334-
eval_as_f2(F2,A,B,RetVal):- current_predicate(F2/3),!,call(F2,A,B,RetVal),!.
3339+
eval_as_f2(F2,A,B,RetVal):- current_predicate_fast(F2/3),!,call(F2,A,B,RetVal),!.
33353340
eval_as_f2(F2,A,B,RetVal):- f2_to_p3(F2,P3),!,call(P3,A,B,RetVal).
33363341
eval_as_f2(F2,A,B,RetVal):- once(eval([F2,A,B],TF)),
33373342
(TF == 'True'-> RetVal=A ;
@@ -3636,7 +3641,7 @@
36363641
if_t(var(Type),member(Type,['mx','mi','mc'])),
36373642
if_t(var(Len),between(1,10,Len)),
36383643
format(atom(Fn),'~w__1_~w_~w',[Type,Len,Sym]),
3639-
succ(Len,LenP1), current_predicate(Fn/LenP1),
3644+
succ(Len,LenP1), current_predicate_fast(Fn/LenP1),
36403645
ok_call_predicate(Sym,Len,Type).
36413646

36423647
transpiler_peek_impl(Sym,Len,Type,Fn, Min, restAsList):-
@@ -3646,15 +3651,15 @@
36463651
if_t(var(Min),between(0,Len, Min)),
36473652
(format(atom(Fn),'~w_n_~w__~w',[Type,Min,Sym]);
36483653
format(atom(Fn),'~w__1_~w+_~w',[Type,Min,Sym])),
3649-
succ(Min,N1),succ(N1,LenP1), current_predicate(Fn/LenP1),
3654+
succ(Min,N1),succ(N1,LenP1), current_predicate_fast(Fn/LenP1),
36503655
ok_call_predicate(Sym,Len,Type).
36513656

36523657
/*
36533658
transpiler_peek_impl(Sym,Len,Type,Fn, Min, restAsList):-
36543659
between(0,Len, Min),
36553660
if_t(var(Type),member(Type,['mx','mi','mc'])),
36563661
format(atom(Fn),'~w_n_~w__~w',[Type,Min,Sym]),
3657-
succ(Min,N1),succ(N1,LenP1), current_predicate(Fn/LenP1),
3662+
succ(Min,N1),succ(N1,LenP1), current_predicate_fast(Fn/LenP1),
36583663
ok_call_predicate(Sym,Len,Type).
36593664
*/
36603665

@@ -3924,7 +3929,7 @@
39243929
% predicate inherited by system
39253930
eval_40(Eq,RetType,Depth,Self,[AE|More],TF):- allow_host_functions,
39263931
is_host_predicate([AE|More],Pred,Len),
3927-
current_predicate(Pred/Len),!,
3932+
current_predicate_fast(Pred/Len),!,
39283933
%fake_notrace( \+ is_user_defined_goal(Self,[AE|More])),!,
39293934
% adjust_args(Depth,Self,AE,More,Adjusted),
39303935
maplist(as_prolog_x(Depth,Self), More , Adjusted),
@@ -3996,11 +4001,11 @@
39964001
% function inherited from system
39974002
eval_40(Eq,RetType,Depth,Self,[AE|More],Res):- allow_host_functions,
39984003
is_host_function([AE|More],Pred,Len), % thus maybe -fn or !
3999-
Len1 is Len+1, current_predicate(Pred/Len1), !,
4004+
Len1 is Len+1, current_predicate_fast(Pred/Len1), !,
40004005
%fake_notrace( \+ is_user_defined_goal(Self,[AE|More])),!,
40014006
%adjust_args(Depth,Self,AE,More,Adjusted),!,
40024007
%Len1 is Len+1,
4003-
%current_predicate(Pred/Len1),
4008+
%current_predicate_fast(Pred/Len1),
40044009
maplist(as_prolog_x(Depth,Self),More,Adjusted),
40054010
append(Adjusted,[Res],Args),!,
40064011
if_trace(host;prolog,ppt(apply(Pred,Args))),

prolog/metta_lang/metta_typed_functions.pl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@
236236
%metta_defn_decl(Self, [Op | Args], [ [do_apply ,[Op | Args], Body] | Apply], [let, ReturnVal, [Body|Apply], ReturnVal], ReturnVal):- is_list( Args),
237237
% metta_defn(Self, [[Op | Args] | Apply], Body).
238238

239-
metta_defn_fallback(_Self, [Op | Parameters], [let, ReturnVal, Body, ReturnVal], Body, ReturnVal):- is_list(Parameters),
239+
metta_defn_fallback(_Self, [Op | Parameters], [let, ReturnVal, Body, ReturnVal], Body, ReturnVal):- fail, is_list(Parameters),
240240
must_length(Parameters, Len),
241241
format(atom(Fn),'mc_~w__~w',[Len,Op]),
242-
current_predicate(Fn/_),
242+
current_predicate_fast(Fn/_),
243243
Body = ['call-fn',Fn|Parameters],!.
244244
metta_defn_fallback(_Self, [Op | Parameters], Body, Body, ReturnVal):- fail,
245245
Body = [let, [quote, ReturnVal], [quote, ['interp!', Op | Parameters]], ReturnVal], Op \=='interp!'.
@@ -363,7 +363,7 @@
363363
ReturnVal = '$VAR'('_returnVal'),
364364
call_showing(transpiler_predicate_store(Op, LenP1, _, _)),
365365
call_showing(transpiler_clause_store(Op, LenP1, _, _, _, _, _, _, _)),
366-
format(atom(Fn),'mc_~w__~w',[Len,Op]), % forall(current_predicate(Fn/LenP1),listing(Fn/LenP1)),
366+
format(atom(Fn),'mc_~w__~w',[Len,Op]), % forall(current_predicate_fast(Fn/LenP1),listing(Fn/LenP1)),
367367
call_showing(Fn/LenP1),
368368
call_showing(function_declaration_scores(Self, Op, Len, Parameters, ParamTypes, _RetType, Body, ReturnVal,_)),
369369
if_t(\+ function_declaration_scores(Self, Op, Len, Parameters, ParamTypes, __RetType, Body, ReturnVal,_),
@@ -373,11 +373,11 @@
373373
true.
374374

375375
call_showing(Var):- \+ callable(Var), !, write_src_nl(not(callable(Var))).
376-
call_showing(Atom):- atom(Atom), \+ current_predicate(Atom/_, _), !, write_src_nl(unknown(Atom)).
377-
call_showing(Atom):- atom(Atom), !, forall(current_predicate(Atom/N),call_showing(Atom/N)).
378-
call_showing(Op/Len):- \+ current_predicate(Op/Len), !, write_src_nl(unknown(Op/Len)).
379-
call_showing(Op/Len):- !, forall(current_predicate(Op/Len, SHOWP), call_showing(clause(SHOWP,Body), (SHOWP:-Body))).
380-
call_showing(SHOWP):- \+ current_predicate(_, SHOWP), !, write_src_nl(unknown(SHOWP)).
376+
call_showing(Atom):- atom(Atom), \+ current_predicate_fast(Atom/_, _), !, write_src_nl(unknown(Atom)).
377+
call_showing(Atom):- atom(Atom), !, forall(current_predicate_fast(Atom/N),call_showing(Atom/N)).
378+
call_showing(Op/Len):- \+ current_predicate_fast(Op/Len), !, write_src_nl(unknown(Op/Len)).
379+
call_showing(Op/Len):- !, forall(current_predicate_fast(Op/Len, SHOWP), call_showing(clause(SHOWP,Body), (SHOWP:-Body))).
380+
call_showing(SHOWP):- \+ current_predicate_fast(_, SHOWP), !, write_src_nl(unknown(SHOWP)).
381381
call_showing(SHOWP):- call_showing(SHOWP, SHOWP).
382382

383383
call_showing(SHOWP, Template):-

0 commit comments

Comments
 (0)