Skip to content

Commit

Permalink
start of dmiles branch
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Feb 12, 2024
1 parent aff8e20 commit d8ea93c
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 98 deletions.
21 changes: 21 additions & 0 deletions examples/performance/comparisons/fibo_444444c.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

;; Turn off our memoization feature so we get a fair "raw" recursion test against RustHE
;; however our interpeter TCO's EVERYTHING so we are in this case 10M+ times faster than RustHE
;; !(pragma! tabling false)
;; makes this file be treated as if the command line --compile=full was supplied
!(pragma! compile full)

(= (fib $n)
(if (== $n 0)
0
(if (== $n 1)
1
(+ (fib (- $n 1)) (fib (- $n 2))))))

!(println! (fib 444444))

; Execution took 63.44 seconds.




Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

;; Turn off our memoization feature so we get a fair "raw" recursion test against RustHE
;; however our runtime TCOs EVERYTHING so we are in this case 1M+ times faster than RustHE
;; !(pragma! tabling false) ;; if uncommented fib1/1 finishes 700 times faster yet
;; however our interpeter TCO's EVERYTHING so we are in this case 10M+ times faster than RustHE
;; !(pragma! tabling false)
;; makes this file be treated as if the command line --compile=full was supplied
!(pragma! compile full)

Expand All @@ -21,9 +21,7 @@

!(println! (fib1 0 1 900 0))

(= (fib1 $n) (fib1 0 1 $n 0))

!(println! (fib1 900))



Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

;; makes this file be treated as if the command line --compile=false was supplied
;; !(pragma! compile false)
!(pragma! compile false)

(= (fib $n)
(if (== $n 0)
Expand All @@ -9,8 +9,8 @@
1
(+ (fib (- $n 1)) (fib (- $n 2))))))

!(println! (argv-metta-fn 1))
!(println! (fib (argv-metta-fn 1)))
!(call-fn (argv-metta 1))
!(println! (fib (call-fn (argv-metta 1))))



17 changes: 17 additions & 0 deletions examples/performance/comparisons/fibo_arg1c.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

;; makes this file be treated as if the command line --compile=full was supplied
!(pragma! compile full)

(= (fib $n)
(if (== $n 0)
0
(if (== $n 1)
1
(+ (fib (- $n 1)) (fib (- $n 2))))))


!(call-fn (argv-metta 1))
!(println! (fib (call-fn (argv-metta 1))))



7 changes: 5 additions & 2 deletions examples/performance/comparisons/fibo_hang_900.metta
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

;; Turn off our memoization feature so we get a fair "raw" recursion test against RustHE
;; however our interpeter TCO's EVERYTHING so we are in this case 10M+ times faster than RustHE
;; !(pragma! tabling false)
;; however our runtime TCOs EVERYTHING so we are in this case 1M+ times faster than RustHE

;; !(pragma! tabling false) ;; if uncommented fib1/1 finishes 700 times faster yet
;; makes this file be treated as if the command line --compile=full was supplied
!(pragma! compile full)

Expand All @@ -21,7 +22,9 @@

!(println! (fib1 0 1 900 0))

(= (fib1 $n) (fib1 0 1 $n 0))

!(println! (fib1 900))



42 changes: 24 additions & 18 deletions metta_vspace/pyswip/metta_compiler.pl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
:- ensure_loaded(metta_reader).
:- ensure_loaded(metta_interp).
:- ensure_loaded(metta_space).

% =======================================
% TODO move non flybase specific code between here and the compiler
:- ensure_loaded(flybase_main).
%:- ensure_loaded(flybase_main).
% =======================================
%:- set_option_value(encoding,utf8).

Expand All @@ -30,6 +31,7 @@
for_all(G1,G2):- forall(G1,G2).

:- op(700,xfx,'=~').
:- op(690,xfx, =~ ).

compound_non_cons(B):- compound(B), \+ B = [_|_].
iz_conz(B):- compound(B), B=[_|_].
Expand Down Expand Up @@ -89,6 +91,8 @@
decl_functional_predicate_arg(match,4,4).
decl_functional_predicate_arg('TupleConcat',3,3).
decl_functional_predicate_arg('new-space',1,1).
decl_functional_predicate_arg('exec0',1,1).
decl_functional_predicate_arg('exec1',1,1).

decl_functional_predicate_arg(superpose, 2, 2).

Expand Down Expand Up @@ -180,7 +184,7 @@
sexpr_s2p(I, M),
f2p(_,_,M,O),
expand_to_hb(O,H,B),
head_preconds_into_body(H,B,HH,BB),!,
optimize_head_and_body(H,B,HH,BB),!,
OO = ':-'(HH,BB).

% ?- compile_for_exec(RetResult, is(pi+pi), Converted).
Expand All @@ -189,36 +193,35 @@
%ignore(Res='$VAR'('RetResult')),
compile_for_exec0(Res,I,O),!.



compile_for_exec0(Res,I,eval_args(I,Res)):- is_ftVar(I),!.
compile_for_exec0(Res,(:- I),O):- !, compile_for_exec0(Res,I,O).

compile_for_exec0(Res,I,BB):-
compile_for_assert([exec0], I, H:-BB),
arg(1,H,Res).

compile_for_exec0(Res,I,BB):-
%ignore(Res='$VAR'('RetResult')),
compile_flow_control(exec(),Res,I,O),
head_preconds_into_body(exec(Res),O,_,BB).
compound_name_arguments(EXEC1, exec1, []),
f2p(EXEC1,Res,I,O),
optimize_head_and_body(exec1(Res),O,_,BB).

compile_for_exec0(Res,I,BB):- fail,
compound_name_arguments(EXEC0, exec0, []),
compile_for_assert(EXEC0, I, H:-BB),
arg(1,H,Res).


%compile_for_exec0(Res,I,O):- f2p(exec(),Res,I,O).


% If Convert is of the form (AsFunction=AsBodyFn), we perform conversion to obtain the equivalent predicate.
compile_head_for_assert(HeadIs, (Head:-Body)):-
compile_fact_for_assert(HeadIs, (Head:-Body)):-
compile_head_for_assert(HeadIs, NewHeadIs,Converted),
head_preconds_into_body(NewHeadIs,Converted,Head,Body).
optimize_head_and_body(NewHeadIs,Converted,Head,Body).

head_as_is(Head):-
as_functor_args(Head,Functor,A,_),!,
head_as_is(Functor,A).
head_as_is(if,3).

compile_head_for_assert(Head, Head, true):-
head_as_is(Head),!.

compile_head_for_assert(Head, Head, true):- head_as_is(Head),!.
compile_head_for_assert(Head, NewestHead, HeadCode):-
compile_head_variablization(Head, NewHead, VHeadCode),
compile_head_args(NewHead, NewestHead, AHeadCode),
Expand Down Expand Up @@ -286,7 +289,9 @@
as_functor_args(AsPred,F,A,ArgsL):-
nonvar(F),length(ArgsL,A),AsPred = [F|ArgsL].

compile_for_assert(HeadIs, AsBodyFn, Converted) :- (AsBodyFn =@= HeadIs ; AsBodyFn == []), !,/*trace,*/ compile_head_for_assert(HeadIs,Converted).
compile_for_assert(HeadIs, AsBodyFn, Converted) :-
(AsBodyFn =@= HeadIs ; AsBodyFn == []), !,/*trace,*/
compile_fact_for_assert(HeadIs,Converted).

% If Convert is of the form (AsFunction=AsBodyFn), we perform conversion to obtain the equivalent predicate.
compile_for_assert(Head, AsBodyFn, Converted) :-
Expand Down Expand Up @@ -320,7 +325,7 @@
%RetResult = Converted,
%RetResult = _,
optimize_head_and_body(Head,NextBody,HeadC,NextBodyC),
%fbug([convert(Convert),head_preconds_into_body(HeadC:-NextBodyC)]),
%fbug([convert(Convert),optimize_head_and_body(HeadC:-NextBodyC)]),
%if_t(((Head:-NextBody)\=@=(HeadC:-NextBodyC)),fbug(was(Head:-NextBody))),
nop(ignore(Result = '$VAR'('HeadRes'))))),!.

Expand All @@ -333,7 +338,6 @@
combine_code(CodeForHeadArgs,NextBody,BodyC),!,
optimize_head_and_body(HeadC,BodyC,HeadCC,BodyCC),!.


/*
*/
optimize_head_and_body(Head,Body,HeadNewest,BodyNewest):-
Expand Down Expand Up @@ -1060,8 +1064,10 @@
call4(G):- call(G).
call5(G):- call(G).


trace_break:- trace,break.

:- table(u_assign/2).
u_assign(FList,R):- is_list(FList),!,eval_args(FList,R).
u_assign(FList,R):- var(FList),nonvar(R), !, u_assign(R,FList).
u_assign(FList,R):- FList=@=R,!,FList=R.
Expand Down
64 changes: 37 additions & 27 deletions metta_vspace/pyswip/metta_eval.pl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%
% post match modew
%:- style_check(-singleton).
%:- style_check(-singleton:- ensure_loaded(swi_support).

self_eval0(X):- \+ callable(X),!.
self_eval0(X):- is_valid_nb_state(X),!.
Expand Down Expand Up @@ -1403,53 +1403,63 @@

eval_selfless(E,R):- eval_selfless_0(E,R).

eval_selfless_0([F,X,XY],TF):- is_assignment(F), fake_notrace(args_to_mathlib([X,XY],Lib)),!,eval_selfless3(Lib,['=',X,XY],TF).
eval_selfless_0([F|XY],TF):- eval_selfless_1([F|XY],TF),!.

eval_selfless_0a(F,X,Y,TF):- X=Y,!, TF='True'.
eval_selfless_0a(F,X,Y,TF):- var(X),!,as_tf(X=Y,TF).
eval_selfless_0a(F,X,Y,TF):- var(Y),!,as_tf(X=Y,TF).
eval_selfless_0a(F,X,Y,TF):-
notrace(args_to_mathlib([X,Y],Lib)),!, eval_selfless3(Lib,['=',X,Y],TF).


eval_selfless_0([F,X,XY],TF):- is_assignment(F), !, eval_selfless_0a(F,X,XY,TF).
eval_selfless_0([F|XY],TF):- eval_selfless_1([F|XY],TF),!.
eval_selfless_0(E,R):- eval_selfless_2(E,R).

eval_selfless_1([F|XY],TF):- \+ ground(XY),!,fake_notrace(args_to_mathlib(XY,Lib)),!,eval_selfless3(Lib,[F|XY],TF).
eval_selfless_1([F|XY],TF):- \+ ground(XY),!,notrace(args_to_mathlib(XY,Lib)),!,eval_selfless3(Lib,[F|XY],TF).
eval_selfless_1(['>',X,Y],TF):-!,as_tf(X>Y,TF).
eval_selfless_1(['<',X,Y],TF):-!,as_tf(X<Y,TF).
eval_selfless_1(['=>',X,Y],TF):-!,as_tf(X>=Y,TF).
eval_selfless_1(['<=',X,Y],TF):-!,as_tf(X=<Y,TF).
eval_selfless_1(['\\=',X,Y],TF):-!,as_tf(dif(X,Y),TF).

eval_selfless_2(['%',X,Y],TF):-!,eval_selfless_2(['mod',X,Y],TF).
eval_selfless_2(LIS,Y):- fake_notrace(( ground(LIS),
eval_selfless_2(LIS,Y):- notrace(( ground(LIS),
LIS=[F,_,_], atom(F), catch_warn(current_op(_,yfx,F)),
LIS\=[_], s2ps(LIS,IS))), fake_notrace(catch((Y is IS),_,fail)),!.
LIS\=[_], s2ps(LIS,IS))), notrace(catch((Y is IS),_,fail)),!.


eval_selfless3(Lib,FArgs,TF):- maplist(s2ps,FArgs,Next),!,compare_selfless0(Lib,Next,TF).

:- use_module(library(clpfd)).

:- clpq:use_module(library(clpq)).
:- clpr:use_module(library(clpr)).

:- use_module(library(clpfd)).
compare_selfless0(_,[F|_],_TF):- \+ atom(F),!,fail.
compare_selfless0(cplfd,['=',X,Y],TF):-!,as_tf(X#=Y,TF).
compare_selfless0(cplfd,['\\=',X,Y],TF):-!,as_tf(X #\=Y,TF).
compare_selfless0(cplfd,['>',X,Y],TF):-!,as_tf(X#>Y,TF).
compare_selfless0(cplfd,['<',X,Y],TF):-!,as_tf(X#<Y,TF).
compare_selfless0(cplfd,['=>',X,Y],TF):-!,as_tf(X#>=Y,TF).
compare_selfless0(cplfd,['<=',X,Y],TF):-!,as_tf(X#=<Y,TF).
compare_selfless0(cplfd,[F|Stuff],TF):- !,atom_concat('#',F,SharpF),P=..[SharpF|Stuff],!,as_tf(P,TF).
compare_selfless0(Lib,['\\=',X,Y],TF):-!,as_tf(Lib:{X \=Y}, TF).
compare_selfless0(Lib,['=',X,Y],TF):-!,as_tf(Lib:{X =Y}, TF).
compare_selfless0(Lib,['>',X,Y],TF):-!,as_tf(Lib:{X>Y},TF).
compare_selfless0(Lib,['<',X,Y],TF):-!,as_tf(Lib:{X<Y},TF).
compare_selfless0(Lib,['=>',X,Y],TF):-!,as_tf(Lib:{X>=Y},TF).
compare_selfless0(Lib,['<=',X,Y],TF):-!,as_tf(Lib:{X=<Y},TF).
compare_selfless0(Lib,[F|Stuff],TF):- P=..[F|Stuff],!,as_tf(Lib:{P},TF).

args_to_mathlib(XY,Lib):- sub_term(T,XY), var(T),get_attrs(T,XX),get_attrlib(XX,Lib).
args_to_mathlib(XY,clpr):- once((sub_term(T,XY), float(T))). % Reals
args_to_mathlib(XY,clpq):- once((sub_term(Rat,XY),compound(Rat),Rat='/'(_,_))).
compare_selfless0(LIB,[SharpF,X,Y],TF):- atom_concat('#',F,SharpF),!,
compare_selfless1(LIB,[F,X,Y],TF).
compare_selfless0(Lib,[F|Stuff],TF):- compare_selfless1(Lib,[F|Stuff],TF).

compare_selfless1(LIB,['=>',X,Y],TF):-!,compare_selfless2(LIB,['>=',X,Y],TF).
compare_selfless1(LIB,['<=',X,Y],TF):-!,compare_selfless2(LIB,['=<',X,Y],TF).
compare_selfless1(Lib,[F|Stuff],TF):- compare_selfless2(Lib,[F|Stuff],TF).

compare_selfless2(cplfd,[F|Stuff],TF):- !,atom_concat('#',F,SharpF),
P=..[SharpF|Stuff],!,as_tf(P,TF).
compare_selfless2(Lib,[F|Stuff],TF):- Lib\==clpfd, P=..[F|Stuff],!,as_tf(Lib:{P},TF).

sub_ele(E,Args):- is_list(Args),!, member(ST,Args),sub_ele(E,ST).
sub_ele(A,A).
sub_ele(E,Args):- compound(Args),arg(_,Args,ST),sub_ele(E,ST).

args_to_mathlib(XY,Lib):- sub_ele(T,XY), var(T),get_attrs(T,XX),get_attrlib(XX,Lib),!.
args_to_mathlib(XY,clpr):- once((sub_ele(T,XY), float(T))),!. % Reals
args_to_mathlib(XY,clpq):- once((sub_ele(Rat,XY),compound(Rat),Rat='/'(_,_))).
args_to_mathlib(_,clpfd).


get_attrlib(XX,clpfd):- sub_var(clpfd,XX),!.
get_attrlib(XX,clpq):- sub_var(clpr,XX),!.
get_attrlib(XX,clpq):- sub_var(clpq,XX),!.
get_attrlib(XX,clpr):- sub_var(clpr,XX),!.

% =================================================================
Expand Down Expand Up @@ -1568,5 +1578,5 @@

solve_quadratic(A, B, I, J, K) :-
%X in -1000..1000, % Define a domain for X
(X + A) * (X + B) #= I*X*X + J*X + K. % Define the quadratic equation
#=(((X + A) * (X + B)) , (I*X*X + J*X + K)). % Define the quadratic equation
%label([X]). % Find solutions for X
Loading

0 comments on commit d8ea93c

Please sign in to comment.