changeset e3187f3d4123 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=e3187f3d4123 user: Simon Forman <sform****@hushm*****> date: Mon Jul 15 12:53:48 2019 -0700 description: Parse joy definitions from text. This way a definitions file can be shared between Python Joy and Prolog Joy. changeset f4ede71d5067 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=f4ede71d5067 user: Simon Forman <sform****@hushm*****> date: Mon Jul 15 15:50:22 2019 -0700 description: Very minor docs change. changeset 992c8dda18ab in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=992c8dda18ab user: Simon Forman <sform****@hushm*****> date: Mon Jul 15 15:53:07 2019 -0700 description: Load definitions from a file. This paves the way to regularize the defs between Python and Prolog (and and future other) implementations. changeset cc831a1b005c in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=cc831a1b005c user: Simon Forman <sform****@hushm*****> date: Mon Jul 15 16:01:59 2019 -0700 description: Retract previous definitions before asserting new ones. changeset 3096787a1234 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=3096787a1234 user: Simon Forman <sform****@hushm*****> date: Mon Jul 15 16:11:49 2019 -0700 description: Move all definitions to defs.txt. diffstat: joy/library.py | 2 +- thun/defs.txt | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ thun/thun.pl | 65 +++++++++++++++++++-------------------------------------- 3 files changed, 83 insertions(+), 44 deletions(-) diffs (173 lines): diff -r d3b42dfd8a60 -r 3096787a1234 joy/library.py --- a/joy/library.py Mon Jul 15 12:28:01 2019 -0700 +++ b/joy/library.py Mon Jul 15 16:11:49 2019 -0700 @@ -990,7 +990,7 @@ def infra(stack, expression, dictionary): ''' Accept a quoted program and a list on the stack and run the program - with the list as its stack. + with the list as its stack. Does not affect the rest of the stack. :: ... [a b c] [Q] . infra diff -r d3b42dfd8a60 -r 3096787a1234 thun/defs.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thun/defs.txt Mon Jul 15 16:11:49 2019 -0700 @@ -0,0 +1,61 @@ +++ == 1 + +anamorphism == [pop []] swap [dip swons] genrec +app1 == grba infrst +app2 == [grba swap grba swap] dip [infrst] cons ii +at == drop first +average == [sum 1.0 *] [size] cleave / +b == [i] dip i +binary == unary popd +ccons == cons cons +cleave == fork [popd] dip +codireco == cons dip rest cons +dinfrirst == dip infra first +disenstacken == ? [uncons ?] loop pop +down_to_zero == [0 >] [dup --] while +drop == [rest] times +dupd == [dup] dip +dupdd == [dup] dipd +dupdipd == dup dipd +enstacken == stack [clear] dip +flatten == [] swap [concat] step +fork == [i] app2 +fourth == rest third +gcd == 1 [tuck modulus dup 0 >] loop pop +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 +pam == [i] map +pm == [+] [-] cleave popdd +popd == [pop] dip +popdd == [pop] dipd +popop == pop pop +popopd == [popop] dip +popopdd == [popop] dipd +primrec == [i] genrec +product == 1 swap [*] step +product == 1 swap [*] step +quoted == [unit] dip +range == [0 <=] [1 - dup] anamorphism +range_to_zero == unit [down_to_zero] infra +rrest == rest rest +run == [] swap infra +second == rest first +size == 0 swap [pop ++] step +sqr == dup * +step_zero == 0 roll> step +sum == 0 swap [+] step +swons == swap cons +ternary == binary popd +third == rest second +unary == nullary popd +unit == [] cons +unquoted == [i] dip +unswons == uncons swap +while == swap [nullary] cons dup dipd concat loop +x == dup i \ No newline at end of file diff -r d3b42dfd8a60 -r 3096787a1234 thun/thun.pl --- a/thun/thun.pl Mon Jul 15 12:28:01 2019 -0700 +++ b/thun/thun.pl Mon Jul 15 16:11:49 2019 -0700 @@ -20,6 +20,7 @@ :- use_module(library(dcg/basics)). :- op(990, xfy, ≡). % for Joy definitions. :- dynamic func/3. +:- dynamic '≡'/2. /* An entry point. @@ -41,13 +42,16 @@ joy_term(N) --> number(N), !. joy_term(J) --> "[", !, joy_parse(J), "]". -joy_term(C) --> chars(Chars), !, {atom_string(C, Chars)}. +joy_term(C) --> symbol(C). + +symbol(C) --> chars(Chars), !, {Chars \= [61, 61], atom_string(C, Chars)}. chars([Ch|Rest]) --> char(Ch), chars(Rest). chars([Ch]) --> char(Ch). char(Ch) --> [Ch], {Ch \== 91, Ch \== 93, code_type(Ch, graph)}. + /* Interpreter thun(Expression, InputStack, OutputStack) @@ -137,48 +141,23 @@ /* Definitions */ -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]. -popop ≡ [pop, pop]. -popopd ≡ [[popop], dip]. -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]. + +joy_def(Def ≡ Body) --> symbol(Def), blanks, "==", joy_parse(Body). + +joy_defs([Def|Defs]) --> blanks, joy_def(Def), blanks, joy_defs(Defs). +joy_defs([]) --> []. + +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), assert_def(Def)). + +assert_def(Def≡Body) :- retractall(Def≡_), assertz(Def≡Body). + +:- assert_defs("defs.txt"). /*