Add mod, gcd, and hypot; term_expansion for math ops.

This commit is contained in:
Simon Forman 2019-07-20 19:26:11 -07:00
parent ae769d2341
commit 206f49598e
2 changed files with 16 additions and 5 deletions

View File

@ -9,7 +9,7 @@ average == [sum 1.0 *] [size] cleave /
b == [i] dip i
binary == unary popd
ccons == cons cons
cleave == fork [popd] dip
cleave == fork popdd
codireco == cons dip rest cons
dinfrirst == dip infrst
disenstacken == ? [uncons ?] loop pop
@ -22,7 +22,9 @@ enstacken == stack [clear] dip
flatten == [] swap [concat] step
fork == [i] app2
fourth == rest third
gcd == true [tuck mod dup 0 >] loop pop
grba == [stack popd] dip
hypot == [sqr] ii + sqrt
ifte == [nullary] dipd swap branch
ii == [dip] dupdip i
infra == swons swaack [i] dip swaack

View File

@ -44,6 +44,14 @@ term_expansion(comparison_operator(X), (func(X, [A, B|S], [C|S]) :-
term_expansion(comparison_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
F =.. [Y, B, A], catch((F -> C=true ; C=false), _, C=F))).
% Likewise for math operators, try to evaluate, otherwise use the symbolic form.
term_expansion(math_operator(X), (func(X, [A, B|S], [C|S]) :-
F =.. [X, B, A], catch(C is F, _, C=F))).
term_expansion(math_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
F =.. [Y, B, A], catch(C is F, _, C=F))).
/*
An entry point.
@ -128,10 +136,11 @@ func(dup, [A|S], [A, A|S]).
func(pop, [_|S], S ).
% Symbolic math. Compute the answer, or derivative, or whatever, later.
func(+, [A, B|S], [B + A|S]).
func(-, [A, B|S], [B - A|S]).
func(*, [A, B|S], [B * A|S]).
func(/, [A, B|S], [B / A|S]).
math_operator(+).
math_operator(-).
math_operator(*).
math_operator(/).
math_operator(mod).
% Attempt to calculate the value of a symbolic math expression.
func(calc, [A|S], [B|S]) :- B is A.