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.
This commit is contained in:
Simon Forman 2020-01-25 16:13:06 -08:00
parent 1ecb5be278
commit 6e6e52d206
1 changed files with 12 additions and 10 deletions

View File

@ -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)) :-
( % Don't let Def "shadow" functions or combinators.
\+ func(Def, _, _),
\+ combo(Def, _, _, _, _),
\+ combo(Def, _, _, _, _)
) -> (
retractall(def(Def, _)),
assertz(def(Def, Body)).
assertz(def(Def, Body))
) ; true. % Otherwise it's okay.
:- assert_defs("defs.txt").