From 6e6e52d206c7365bc579eab069ae9779c5e3f2b5 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 25 Jan 2020 16:13:06 -0800 Subject: [PATCH] Don't assert defs twice. Each definition is getting parsed with the name of the next one as part of its body, then the next one fails to parse and the thing backtracks. So each definition (but the last) gets asserted twice. def(--,[1,-,?]) def(--,[1,-]) def(?,[dup,bool,++]) def(?,[dup,bool]) def(++,[1,+,anamorphism]) def(++,[1,+]) def(anamorphism,[[pop,[]],swap,[dip,swons],genrec,app1]) def(anamorphism,[[pop,[]],swap,[dip,swons],genrec]) def(app1,[grba,infrst,app2]) def(app1,[grba,infrst]) def(app2,[[grba,swap,grba,swap],dip,[infrst],cons,ii,app3]) def(app2,[[grba,swap,grba,swap],dip,[infrst],cons,ii]) ...and so on. --- thun/thun.pl | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/thun/thun.pl b/thun/thun.pl index 8aea981..65351d4 100644 --- a/thun/thun.pl +++ b/thun/thun.pl @@ -186,10 +186,10 @@ func(>=, [A, B|S], [T|S]) :- B #>= A #<==> R, r_truth(R, T). func(<=, [A, B|S], [T|S]) :- B #=< A #<==> R, r_truth(R, T). func(<>, [A, B|S], [T|S]) :- B #\= A #<==> R, r_truth(R, T). - r_truth(0, false). r_truth(1, true). + /* Combinators */ @@ -260,20 +260,22 @@ Definitions joy_def(def(Def, Body)) --> symbol(Def), blanks, "==", joy_parse(Body). -joy_def --> joy_def(Def), {ignore(assert_def(Def))}. - -joy_defs --> blanks, joy_def, blanks, joy_defs. -joy_defs --> []. +joy_defs([Def|Rest]) --> blanks, joy_def(Def), blanks, joy_defs(Rest). +joy_defs([]) --> []. assert_defs(DefsFile) :- read_file_to_codes(DefsFile, Codes, []), - phrase(joy_defs, Codes). + phrase(joy_defs(Defs), Codes), + maplist(assert_def, Defs). assert_def(def(Def, Body)) :- - \+ func(Def, _, _), - \+ combo(Def, _, _, _, _), - retractall(def(Def, _)), - assertz(def(Def, Body)). + ( % Don't let Def "shadow" functions or combinators. + \+ func(Def, _, _), + \+ combo(Def, _, _, _, _) + ) -> ( + retractall(def(Def, _)), + assertz(def(Def, Body)) + ) ; true. % Otherwise it's okay. :- assert_defs("defs.txt").