• R/O
  • SSH

Joypy: Commit

Main interpreter and library.


Commit MetaInfo

Revisiondb86bcbb30cb43a739d6d8c49857e15d9a100577 (tree)
Time2019-08-12 09:48:29
AuthorSimon Forman <sforman@hush...>
CommiterSimon Forman

Log Message

Build math & comparision functions.

Change Summary

Incremental Difference

diff -r 27d6936bbc54 -r db86bcbb30cb thun/gnu-prolog/Makefile
--- a/thun/gnu-prolog/Makefile Sun Aug 11 17:00:38 2019 -0700
+++ b/thun/gnu-prolog/Makefile Sun Aug 11 17:48:29 2019 -0700
@@ -10,3 +10,8 @@
1010 --consult-file thun.pl \
1111 --query-goal do
1212
13+math.pl: meta-math.pl
14+ gprolog \
15+ --consult-file meta-math.pl \
16+ --query-goal do
17+
diff -r 27d6936bbc54 -r db86bcbb30cb thun/gnu-prolog/math.pl
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/thun/gnu-prolog/math.pl Sun Aug 11 17:48:29 2019 -0700
@@ -0,0 +1,44 @@
1+func(+, [A, B|C], [D|C]) :-
2+ E =.. [+, B, A],
3+ catch(D is E, _, D = E).
4+
5+func(-, [A, B|C], [D|C]) :-
6+ E =.. [-, B, A],
7+ catch(D is E, _, D = E).
8+
9+func(*, [A, B|C], [D|C]) :-
10+ E =.. [*, B, A],
11+ catch(D is E, _, D = E).
12+
13+func(/, [A, B|C], [D|C]) :-
14+ E =.. [/, B, A],
15+ catch(D is E, _, D = E).
16+
17+func(mod, [A, B|C], [D|C]) :-
18+ E =.. [mod, B, A],
19+ catch(D is E, _, D = E).
20+
21+func(>, [A, B|C], [D|C]) :-
22+ E =.. [>, B, A],
23+ catch((E -> D = true ; D = false), _, D = E).
24+
25+func(<, [A, B|C], [D|C]) :-
26+ E =.. [<, B, A],
27+ catch((E -> D = true ; D = false), _, D = E).
28+
29+func(>=, [A, B|C], [D|C]) :-
30+ E =.. [>=, B, A],
31+ catch((E -> D = true ; D = false), _, D = E).
32+
33+func(<=, [A, B|C], [D|C]) :-
34+ E =.. [=<, B, A],
35+ catch((E -> D = true ; D = false), _, D = E).
36+
37+func(=, [A, B|C], [D|C]) :-
38+ E =.. [=:=, B, A],
39+ catch((E -> D = true ; D = false), _, D = E).
40+
41+func(<>, [A, B|C], [D|C]) :-
42+ E =.. [=\=, B, A],
43+ catch((E -> D = true ; D = false), _, D = E).
44+
diff -r 27d6936bbc54 -r db86bcbb30cb thun/gnu-prolog/meta-math.pl
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/thun/gnu-prolog/meta-math.pl Sun Aug 11 17:48:29 2019 -0700
@@ -0,0 +1,70 @@
1+/*
2+
3+To handle comparision operators the possibility of exceptions due to
4+insufficiently instantiated arguments must be handled. First try to make
5+the comparison and set the result to a Boolean atom. If an exception
6+happens just leave the comparison expression as the result and some other
7+function or combinator will deal with it. Example:
8+
9+ func(>, [A, B|S], [C|S]) :- catch(
10+ (B > A -> C=true ; C=false),
11+ _,
12+ C=(B>A) % in case of error.
13+ ).
14+
15+To save on conceptual overhead I've defined a term_expansion/2 that sets
16+up the func/3 for each op.
17+*/
18+
19+term_expansion(comparison_operator(X), (func(X, [A, B|S], [C|S]) :-
20+ F =.. [X, B, A], catch((F -> C=true ; C=false), _, C=F))).
21+
22+% I don't use Prolog-compatible op symbols in all cases.
23+term_expansion(comparison_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
24+ F =.. [Y, B, A], catch((F -> C=true ; C=false), _, C=F))).
25+
26+% Likewise for math operators, try to evaluate, otherwise use the
27+% symbolic form.
28+
29+term_expansion(math_operator(X), (func(X, [A, B|S], [C|S]) :-
30+ F =.. [X, B, A], catch(C is F, _, C=F))).
31+
32+term_expansion(math_operator(X, Y), (func(X, [A, B|S], [C|S]) :-
33+ F =.. [Y, B, A], catch(C is F, _, C=F))).
34+
35+
36+% Symbolic math. Compute the answer, or derivative, or whatever, later.
37+math_operator(+).
38+math_operator(-).
39+math_operator(*).
40+math_operator(/).
41+math_operator(mod).
42+
43+
44+comparison_operator(>).
45+comparison_operator(<).
46+comparison_operator(>=).
47+comparison_operator(<=, =<).
48+comparison_operator(=, =:=).
49+comparison_operator(<>, =\=).
50+
51+
52+expand_op(Op, Term) :- Op, expand_term(Op, Term).
53+
54+print_o(Stream, Op) :-
55+ findall(Term, expand_op(Op, Term), List),
56+ maplist(writeln(Stream), List).
57+
58+writeln(Stream, Thing) :-
59+ portray_clause(Stream, Thing),
60+ nl(Stream).
61+
62+do :-
63+ open(`math.pl`, write, Stream),
64+ print_o(Stream, math_operator(Op)),
65+ print_o(Stream, comparison_operator(Op)),
66+ print_o(Stream, comparison_operator(Op, Po)),
67+ close(Stream),
68+ halt.
69+
70+
Show on old repository browser