From 4fefc04da314918d7857b9c0c64b5093775aa969 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Mon, 15 Jul 2019 15:53:07 -0700 Subject: [PATCH] Load definitions from a file. This paves the way to regularize the defs between Python and Prolog (and and future other) implementations. --- thun/defs.txt | 31 +++++++++++++++++++++++++++++++ thun/thun.pl | 25 +++++++++++++------------ 2 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 thun/defs.txt diff --git a/thun/defs.txt b/thun/defs.txt new file mode 100644 index 0000000..4f954aa --- /dev/null +++ b/thun/defs.txt @@ -0,0 +1,31 @@ +++ == 1 + +anamorphism == [pop []] swap [dip swons] genrec +average == [sum 1.0 *] [size] cleave / +binary == unary popd +cleave == fork [popd] dip +codireco == cons dip rest cons +dinfrirst == dip infra first +disenstacken == ? [uncons ?] loop pop +down_to_zero == [0 >] [dup --] while +dupdipd == dup dipd +enstacken == stack [clear] dip +flatten == [] swap [concat] step +fork == [i] app2 +gcd == 1 [tuck modulus dup 0 >] loop pop +make_generator == [codireco] ccons +of == swap at +pam == [i] map +primrec == [i] genrec +product == 1 swap [*] step +quoted == [unit] dip +range == [0 <=] [1 - dup] anamorphism +range_to_zero == unit [down_to_zero] infra +run == [] swap infra +size == 0 swap [pop ++] step +sqr == dup * +step_zero == 0 roll> step +ternary == binary popd +unary == nullary popd +unquoted == [i] dip +while == swap [nullary] cons dup dipd concat loop +x == dup i \ No newline at end of file diff --git a/thun/thun.pl b/thun/thun.pl index 11d4316..9ebfc65 100644 --- a/thun/thun.pl +++ b/thun/thun.pl @@ -20,6 +20,7 @@ :- use_module(library(dcg/basics)). :- op(990, xfy, ≡). % for Joy definitions. :- dynamic func/3. +:- dynamic '≡'/2. /* An entry point. @@ -148,24 +149,18 @@ app1 ≡ [grba, infrst]. app2 ≡ [[grba, swap, grba, swap], dip, [infrst], cons, ii]. at ≡ [drop, first]. b ≡ [[i], dip, i]. -binary ≡ [unary, popd]. ccons ≡ [cons, cons]. -cleave ≡ [fork, [popd], dip]. -codireco ≡ [cons, dip, rest, cons]. drop ≡ [[rest], times]. dupd ≡ [[dup], dip]. dupdd ≡ [[dup], dipd]. -fork ≡ [[i], app2]. fourth ≡ [rest, third]. grba ≡ [[stack, popd], dip]. ifte ≡ [[nullary], dipd, swap, branch]. ii ≡ [[dip], dupdip, i]. infra ≡ [swons, swaack, [i], dip, swaack]. infrst ≡ [infra, first]. -make_generator ≡ [[codireco], ccons]. neg ≡ [0, swap, -]. nullary ≡ [stack, popd, [i], infrst]. -of ≡ [swap, at]. pm ≡ [[+], [-], cleave, popdd]. popd ≡ [[pop], dip]. popdd ≡ [[pop], dipd]. @@ -175,17 +170,11 @@ popopdd ≡ [[popop], dipd]. product ≡ [1, swap, [*], step]. rrest ≡ [rest, rest]. second ≡ [rest, first]. -size ≡ [0, swap, [pop, 1, +], step]. -sqr ≡ [dup, *]. sum ≡ [0, swap, [+], step]. swons ≡ [swap, cons]. third ≡ [rest, second]. -trinary ≡ [binary, popd]. -unary ≡ [nullary, popd]. unit ≡ [[], cons]. unswons ≡ [uncons, swap]. -while ≡ [swap, [nullary], cons, dup, dipd, concat, loop]. -x ≡ [dup, i]. /* @@ -239,3 +228,15 @@ contracto, [Def] --> {Def ≡ Body}, Body. % phrase(expando, ExprIn, ExprOut). + +read_defs(DefsFile, Defs) :- + read_file_to_codes(DefsFile, Codes, []), + phrase(joy_defs(Defs), Codes). + +assert_defs(DefsFile) :- + read_defs(DefsFile, Defs), + forall(member(Def, Defs), assertz(Def)). + +:- assert_defs("defs.txt"). +% working_directory(_, 'c:/users/sforman/desktop/src/joypy/thun/'), +