clean up and speed up

This commit is contained in:
~d6 2024-04-12 12:53:40 -04:00
parent 3904d428a8
commit 797a318843
1 changed files with 116 additions and 137 deletions

View File

@ -2,130 +2,95 @@
-- ( little endian binary integers ) -- ( little endian binary integers )
-- ( constants ) -- ( constants )
<> zero ((0 nil)) <> zero ((0 ()))
<> one ((1 nil)) <> one ((1 ()))
<> two ((0 (1 nil))) <> ten ((0 (1 (0 (1 ())))))
<> three ((1 (1 nil)))
<> ten ((0 (1 (0 (1 nil)))))
-- ( decimal digit to binary ) -- ( decimal digit to binary )
<> (binary 0) ((0 nil)) <> (binary 0) ((0 ()))
<> (binary 1) ((1 nil)) <> (binary 1) ((1 ()))
<> (binary 2) ((0 (1 nil))) <> (binary 2) ((0 (1 ())))
<> (binary 3) ((1 (1 nil))) <> (binary 3) ((1 (1 ())))
<> (binary 4) ((0 (0 (1 nil)))) <> (binary 4) ((0 (0 (1 ()))))
<> (binary 5) ((1 (0 (1 nil)))) <> (binary 5) ((1 (0 (1 ()))))
<> (binary 6) ((0 (1 (1 nil)))) <> (binary 6) ((0 (1 (1 ()))))
<> (binary 7) ((1 (1 (1 nil)))) <> (binary 7) ((1 (1 (1 ()))))
<> (binary 8) ((0 (0 (0 (1 nil))))) <> (binary 8) ((0 (0 (0 (1 ())))))
<> (binary 9) ((1 (0 (0 (1 nil))))) <> (binary 9) ((1 (0 (0 (1 ())))))
-- ( binary to decimal digit ) -- ( binary to decimal digit )
<> ((decimal (0 nil))) (0) <> ((decimal (0 ()))) (0)
<> ((decimal (1 nil))) (1) <> ((decimal (1 ()))) (1)
<> ((decimal (0 (1 nil)))) (2) <> ((decimal (0 (1 ())))) (2)
<> ((decimal (1 (1 nil)))) (3) <> ((decimal (1 (1 ())))) (3)
<> ((decimal (0 (0 (1 nil))))) (4) <> ((decimal (0 (0 (1 ()))))) (4)
<> ((decimal (1 (0 (1 nil))))) (5) <> ((decimal (1 (0 (1 ()))))) (5)
<> ((decimal (0 (1 (1 nil))))) (6) <> ((decimal (0 (1 (1 ()))))) (6)
<> ((decimal (1 (1 (1 nil))))) (7) <> ((decimal (1 (1 (1 ()))))) (7)
<> ((decimal (0 (0 (0 (1 nil)))))) (8) <> ((decimal (0 (0 (0 (1 ())))))) (8)
<> ((decimal (1 (0 (0 (1 nil)))))) (9) <> ((decimal (1 (0 (0 (1 ())))))) (9)
-- create nil-terminated list -- reverse ()-terminated list
<> (nilify (?h)) ((?h nil)) <> (reverse ?x) (reverse' () ?x)
<> (nilify (?h ?t)) ((?h nilify ?t)) <> (reverse' ?a ()) (?a)
-- reverse nil-terminated list
<> (reverse ?x) (reverse' nil ?x)
<> (reverse' ?a nil) (?a)
<> (reverse' ?a (?h ?t)) (reverse' (?h ?a) ?t) <> (reverse' ?a (?h ?t)) (reverse' (?h ?a) ?t)
-- map
<> (map ?f (list ?l)) (map/l map/f ?f ?l)
<> (map/f ?f (?h ?t)) (?f ?h (map/f ?f ?t))
<> (map/f ?f (?h)) (map/r (?f ?h))
<> (?h (map/r ?t)) (map/r (?h ?t))
<> (map/l map/r ?l) (list ?l)
-- ( normalize, remove trailing zeros ) -- ( normalize, remove trailing zeros )
-- ( currently zero is (0 nil) though arguably it could be nil ) -- ( currently zero is (0 ()) though arguably it could be () )
-- ( that change would require auditing our rules ) -- ( that change would require auditing our rules )
<> (normalize (?h ?t)) ((?h normalize' nil ?t)) <> (normalize (?h ?t)) ((?h normalize' () ?t))
<> (normalize' ?s nil) (nil) <> (normalize' ?s ()) (())
<> (normalize' ?s (0 ?t)) (normalize' (0 ?s) ?t) <> (normalize' ?s (0 ?t)) (normalize' (0 ?s) ?t)
<> (normalize' nil (1 ?t)) ((1 normalize' nil ?t)) <> (normalize' () (1 ?t)) ((1 normalize' () ?t))
<> (normalize' (0 ?s) (1 ?t)) ((0 normalize' ?s (1 ?t))) <> (normalize' (0 ?s) (1 ?t)) ((0 normalize' ?s (1 ?t)))
-- ( to integer ) -- ( to integer )
<> ((int ?*)) ((sum f (one) g reverse nilify (?*))) <> ((int ?*)) ((sum f (one) g reverse (?*)))
<> (g nil) (nil) <> (g ()) (())
<> (g (?h ?t)) ((binary ?h g ?t)) <> (g (?h ?t)) ((binary ?h g ?t))
<> (f (?u) nil) (nil) <> (f (?u) ()) (())
<> (f (?u) (?h ?t)) (((mul ?h ?u) f ((mul ?u ten)) ?t)) <> (f (?u) (?h ?t)) (((mul ?h ?u) f ((mul ?u ten)) ?t))
-- ( to binary str ) -- ( to binary str )
<> ((bstr ?x)) ((bstr1 ?x)) <> ((bstr ?x)) (emit force (0 (b ?x)))
<> ((bstr1 (0 nil))) ((0 (b 0)))
<> ((bstr1 (1 nil))) ((0 (b 1)))
<> ((bstr1 (0 (0 ?c)))) ((bstr2 ?c (0 0)))
<> ((bstr1 (1 (0 ?c)))) ((bstr2 ?c (0 1)))
<> ((bstr1 (0 (1 ?c)))) ((bstr2 ?c (1 0)))
<> ((bstr1 (1 (1 ?c)))) ((bstr2 ?c (1 1)))
<> ((bstr2 nil ?z)) ((stringify (0 (b ?z))))
<> ((bstr2 (0 ?t) ?z)) ((bstr2 ?t (0 ?z)))
<> ((bstr2 (1 ?t) ?z)) ((bstr2 ?t (1 ?z)))
-- ( to string: TODO, need division for this one ) -- ( to string: TODO, need division for this one )
<> ((str ?x)) ((str1 (divmod ?x ten) nil)) <> ((str ?x)) ((str1 ?x ()))
<> ((str1 (?q ?r) ?a)) ((str2 ?q (?r ?a))) <> ((str1 (0 ()) ?a)) (emit force ?a)
<> ((str2 (0 nil) ?a)) ((str3 ?a nil)) <> ((str1 (?h ?t) ?a)) ((str2 (divmod (?h ?t) ten) ?a))
<> ((str2 (0 (?h ?t)) ?a)) ((str1 (divmod (0 (?h ?t)) ten) ?a)) <> ((str2 (?q ?r) ?a)) ((str1 ?q ((decimal ?r) ?a)))
<> ((str2 (1 ?t) ?a)) ((str1 (divmod (1 ?t) ten) ?a))
<> ((str3 nil ?a)) ((digitize ?a nil))
<> ((str3 (?h ?t) nil)) ((str3 ?t (decimal ?h)))
<> ((str3 (?h ?t) (?a ?b))) ((str3 ?t ((decimal ?h) (?a ?b))))
<> ((digitize (0 ?t) nil)) ((digitize ?t (0))) -- ( force a list to evaluate to digits/letters )
<> ((digitize (1 ?t) nil)) ((digitize ?t (1))) <> ((?h force/r ?t)) (force/r (?h ?t))
<> ((digitize (2 ?t) nil)) ((digitize ?t (2))) <> (force ()) (force/r ())
<> ((digitize (3 ?t) nil)) ((digitize ?t (3))) <> (force (0 ?t)) ((0 force ?t))
<> ((digitize (4 ?t) nil)) ((digitize ?t (4))) <> (force (1 ?t)) ((1 force ?t))
<> ((digitize (5 ?t) nil)) ((digitize ?t (5))) <> (force (2 ?t)) ((2 force ?t))
<> ((digitize (6 ?t) nil)) ((digitize ?t (6))) <> (force (3 ?t)) ((3 force ?t))
<> ((digitize (7 ?t) nil)) ((digitize ?t (7))) <> (force (4 ?t)) ((4 force ?t))
<> ((digitize (8 ?t) nil)) ((digitize ?t (8))) <> (force (5 ?t)) ((5 force ?t))
<> ((digitize (9 ?t) nil)) ((digitize ?t (9))) <> (force (6 ?t)) ((6 force ?t))
<> (force (7 ?t)) ((7 force ?t))
<> (force (8 ?t)) ((8 force ?t))
<> (force (9 ?t)) ((9 force ?t))
<> (force (a ?t)) ((a force ?t))
<> (force (b ?t)) ((b force ?t))
<> (force (c ?t)) ((c force ?t))
<> (force (d ?t)) ((d force ?t))
<> (force (e ?t)) ((e force ?t))
<> (force (f ?t)) ((f force ?t))
<> (force (x ?t)) ((x force ?t))
<> ((digitize (0 ?t) ?a)) ((digitize ?t (0 ?a))) -- ( emit )
<> ((digitize (1 ?t) ?a)) ((digitize ?t (1 ?a))) <> (emit force/r ?*) (?*)
<> ((digitize (2 ?t) ?a)) ((digitize ?t (2 ?a)))
<> ((digitize (3 ?t) ?a)) ((digitize ?t (3 ?a)))
<> ((digitize (4 ?t) ?a)) ((digitize ?t (4 ?a)))
<> ((digitize (5 ?t) ?a)) ((digitize ?t (5 ?a)))
<> ((digitize (6 ?t) ?a)) ((digitize ?t (6 ?a)))
<> ((digitize (7 ?t) ?a)) ((digitize ?t (7 ?a)))
<> ((digitize (8 ?t) ?a)) ((digitize ?t (8 ?a)))
<> ((digitize (9 ?t) ?a)) ((digitize ?t (9 ?a)))
<> ((digitize 0 ?a)) ((stringify (0 ?a)))
<> ((digitize 1 ?a)) ((stringify (1 ?a)))
<> ((digitize 2 ?a)) ((stringify (2 ?a)))
<> ((digitize 3 ?a)) ((stringify (3 ?a)))
<> ((digitize 4 ?a)) ((stringify (4 ?a)))
<> ((digitize 5 ?a)) ((stringify (5 ?a)))
<> ((digitize 6 ?a)) ((stringify (6 ?a)))
<> ((digitize 7 ?a)) ((stringify (7 ?a)))
<> ((digitize 8 ?a)) ((stringify (8 ?a)))
<> ((digitize 9 ?a)) ((stringify (9 ?a)))
-- ( comparison operartions ) -- ( comparison operartions )
<> ((cmp ?x ?y)) ((cmpc #eq ?x ?y)) <> ((cmp ?x ?y)) ((cmpc #eq ?x ?y))
<> ((cmpc ?e nil nil)) (?e) <> ((cmpc ?e () ())) (?e)
<> ((cmpc ?e (1 ?x) nil)) (#gt) <> ((cmpc ?e (1 ?x) ())) (#gt)
<> ((cmpc ?e (0 ?x) nil)) ((cmpc ?e ?x nil)) <> ((cmpc ?e (0 ?x) ())) ((cmpc ?e ?x ()))
<> ((cmpc ?e nil (1 ?y))) (#lt) <> ((cmpc ?e () (1 ?y))) (#lt)
<> ((cmpc ?e nil (0 ?y))) ((cmpc ?e nil ?y)) <> ((cmpc ?e () (0 ?y))) ((cmpc ?e () ?y))
<> ((cmpc ?e (0 ?x) (0 ?y))) ((cmpc ?e ?x ?y)) <> ((cmpc ?e (0 ?x) (0 ?y))) ((cmpc ?e ?x ?y))
<> ((cmpc ?e (1 ?x) (0 ?y))) ((cmpc #gt ?x ?y)) <> ((cmpc ?e (1 ?x) (0 ?y))) ((cmpc #gt ?x ?y))
<> ((cmpc ?e (0 ?x) (1 ?y))) ((cmpc #lt ?x ?y)) <> ((cmpc ?e (0 ?x) (1 ?y))) ((cmpc #lt ?x ?y))
@ -133,10 +98,10 @@
-- ( addition ) -- ( addition )
<> ((add ?x ?y)) (addc 0 ?x ?y) <> ((add ?x ?y)) (addc 0 ?x ?y)
<> (addc 0 nil nil) (nil) <> (addc 0 () ()) (())
<> (addc 1 nil nil) ((1 nil)) <> (addc 1 () ()) ((1 ()))
<> (addc ?c ?x nil) (addc ?c ?x (0 nil)) <> (addc ?c ?x ()) (addc ?c ?x (0 ()))
<> (addc ?c nil ?y) (addc ?c (0 nil) ?y) <> (addc ?c () ?y) (addc ?c (0 ()) ?y)
<> (addc 0 (0 ?x) (0 ?y)) ((0 addc 0 ?x ?y)) <> (addc 0 (0 ?x) (0 ?y)) ((0 addc 0 ?x ?y))
<> (addc 0 (0 ?x) (1 ?y)) ((1 addc 0 ?x ?y)) <> (addc 0 (0 ?x) (1 ?y)) ((1 addc 0 ?x ?y))
<> (addc 0 (1 ?x) (0 ?y)) ((1 addc 0 ?x ?y)) <> (addc 0 (1 ?x) (0 ?y)) ((1 addc 0 ?x ?y))
@ -147,23 +112,23 @@
<> (addc 1 (1 ?x) (1 ?y)) ((1 addc 1 ?x ?y)) <> (addc 1 (1 ?x) (1 ?y)) ((1 addc 1 ?x ?y))
-- ( summation ) -- ( summation )
<> ((sum nil)) ((0 nil)) <> ((sum ())) ((0 ()))
<> ((sum (?a nil))) (?a) <> ((sum (?a ()))) (?a)
<> ((sum (?a (?b ?c)))) ((sum ((add ?a ?b) ?c))) <> ((sum (?a (?b ?c)))) ((sum ((add ?a ?b) ?c)))
-- ( multiplication ) -- ( multiplication )
<> ((mul ?x ?y)) (mulc nil ?x ?y) <> ((mul ?x ?y)) (mulc () ?x ?y)
<> (mulc ?t nil ?y) ((sum ?t)) <> (mulc ?t () ?y) ((sum ?t))
<> (mulc ?t (0 ?x) ?y) (mulc ?t ?x (0 ?y)) <> (mulc ?t (0 ?x) ?y) (mulc ?t ?x (0 ?y))
<> (mulc ?t (1 ?x) ?y) (mulc (?y ?t) ?x (0 ?y)) <> (mulc ?t (1 ?x) ?y) (mulc (?y ?t) ?x (0 ?y))
-- ( subtraction ) -- ( subtraction )
<> ((sub ?x ?y)) (normalize subc 0 ?x ?y) <> ((sub ?x ?y)) (normalize subc 0 ?x ?y)
<> (subc 0 nil nil) (nil) <> (subc 0 () ()) (())
<> (subc 1 nil nil) (#err) <> (subc 1 () ()) (#err)
<> (subc 0 ?x nil) (?x) <> (subc 0 ?x ()) (?x)
<> (subc 1 ?x nil) (subc 1 ?x (0 nil)) <> (subc 1 ?x ()) (subc 1 ?x (0 ()))
<> (subc ?c nil ?y) (subc ?c (0 nil) ?y) <> (subc ?c () ?y) (subc ?c (0 ()) ?y)
<> (subc 0 (0 ?x) (0 ?y)) ((0 subc 0 ?x ?y)) <> (subc 0 (0 ?x) (0 ?y)) ((0 subc 0 ?x ?y))
<> (subc 0 (0 ?x) (1 ?y)) ((1 subc 1 ?x ?y)) <> (subc 0 (0 ?x) (1 ?y)) ((1 subc 1 ?x ?y))
<> (subc 0 (1 ?x) (0 ?y)) ((1 subc 0 ?x ?y)) <> (subc 0 (1 ?x) (0 ?y)) ((1 subc 0 ?x ?y))
@ -173,39 +138,55 @@
<> (subc 1 (1 ?x) (0 ?y)) ((0 subc 0 ?x ?y)) <> (subc 1 (1 ?x) (0 ?y)) ((0 subc 0 ?x ?y))
<> (subc 1 (1 ?x) (1 ?y)) ((1 subc 1 ?x ?y)) <> (subc 1 (1 ?x) (1 ?y)) ((1 subc 1 ?x ?y))
-- ( dec ) <> (dec (0 ())) (#err)
<> (dec (0 nil)) (#err) <> (dec (1 ())) ((0 ()))
<> (dec ?x) (normalize dec' ?x) <> (dec (1 ?t)) ((0 ?t))
<> (dec' (0 ?t)) ((1 dec' ?t)) <> (dec (0 ?t)) (dec' (0 ?t))
<> (dec' (1 ())) (())
<> (dec' (1 ?t)) ((0 ?t)) <> (dec' (1 ?t)) ((0 ?t))
<> (dec' (0 ?t)) ((1 dec' ?t))
-- ( inc ) -- ( inc )
<> ((inc nil)) ((1 nil)) <> ((inc ())) ((1 ()))
<> ((inc (0 ?t))) ((1 ?t)) <> ((inc (0 ?t))) ((1 ?t))
<> ((inc (1 ?t))) ((0 (inc ?t))) <> ((inc (1 ?t))) ((0 (inc ?t)))
-- ( left shift; lshift x b means x<<b ) -- ( left shift; lshift x b means x<<b )
<> ((lshift ?x (0 nil))) (?x) <> ((lshift ?x (0 ()))) (?x)
<> ((lshift ?x (1 nil))) ((0 ?x)) <> ((lshift ?x (1 ()))) ((0 ?x))
<> ((lshift ?x (?h (?a ?b)))) ((lshift (0 ?x) dec (?h (?a ?b)))) <> ((lshift ?x (0 (?a ?b)))) ((lshift (0 ?x) dec (0 (?a ?b))))
<> ((lshift ?x (1 (?a ?b)))) ((lshift (0 ?x) (0 (?a ?b))))
<> ((rshift1 (?a ()))) ((0 ()))
<> ((rshift1 (?a (?b ?c)))) ((?b ?c))
-- ( divmod, i.e. quotient and remainder ) -- ( divmod, i.e. quotient and remainder )
-- ( x is the dividend, or what's left of it )
-- ( y is the divisor )
-- ( s is the number of bits to shift, so far )
-- ( o is the next valuet o add to the quotient )
-- ( m is the next multiple of y to work with )
-- ( d is the quotient, so far )
<> ((divmod ?x ?y)) ((divmod1 ?x ?y (cmp ?x ?y))) <> ((divmod ?x ?y)) ((divmod1 ?x ?y (cmp ?x ?y)))
<> ((divmod1 ?x ?y #lt)) ((zero ?x)) <> ((divmod1 ?x ?y #lt)) ((zero ?x))
<> ((divmod1 ?x ?y #eq)) ((one zero)) <> ((divmod1 ?x ?y #eq)) ((one zero))
<> ((divmod1 ?x ?y #gt)) ((divmod2 ?x ?y zero (0 ?y))) <> ((divmod1 ?x ?y #gt)) ((divmod2 ?x ?y zero ?y))
<> ((divmod2 ?x ?y ?s ?m)) ((divmod3 ?x ?y ?s ?m (cmp ?x ?m)))
<> ((divmod3 ?x ?y ?s ?m #lt)) ((divmod4 ?x ?y ?s zero)) <> ((divmod2 ?x ?y ?s ?m)) ((divmod3 ?x ?y ?s ?m (cmp ?x (0 ?m))))
<> ((divmod3 ?x ?y ?s ?m #eq)) ((divmod4 ?x ?y (inc ?s) zero))
<> ((divmod3 ?x ?y ?s ?m #gt)) ((divmod2 ?x ?y (inc ?s) (0 ?m))) <> ((divmod3 ?x ?y ?s ?m #gt)) ((divmod2 ?x ?y (inc ?s) (0 ?m)))
<> ((divmod4 ?x ?y (0 nil) ?d)) (((add ?d one) (sub ?x ?y))) <> ((divmod3 ?x ?y ?s ?m #eq)) ((divmod4 ?x ?y (inc ?s) (0 ?m) zero))
<> ((divmod4 ?x ?y ?s ?d)) ((divmod5 (sub ?x (lshift ?y ?s)) ?y dec ?s (add ?d (lshift one ?s)))) <> ((divmod3 ?x ?y ?s ?m #lt)) ((divmod4 ?x ?y ?s ?m zero))
<> ((divmod5 (0 nil) ?y ?s ?d)) ((?d (0 nil)))
<> ((divmod5 ?x ?y ?s ?d)) ((divmod6 ?x ?y ?s ?d (cmp ?x (lshift ?y ?s)))) <> ((divmod4 ?x ?y (0 ()) ?m ?d)) (((add ?d one) (sub ?x ?y)))
<> ((divmod6 ?x ?y (0 nil) ?d #lt)) ((?d ?x)) <> ((divmod4 ?x ?y ?s ?m ?d)) ((divmod5 (sub ?x ?m) ?y dec ?s (rshift1 ?m) (add ?d (lshift one ?s))))
<> ((divmod6 ?x ?y ?s ?d #lt)) ((divmod5 ?x ?y dec ?s ?d))
<> ((divmod6 ?x ?y ?s ?d #eq)) ((divmod4 ?x ?y ?s ?d)) <> ((divmod5 (0 ()) ?y ?s ?m ?d)) ((?d (0 ())))
<> ((divmod6 ?x ?y ?s ?d #gt)) ((divmod4 ?x ?y ?s ?d)) <> ((divmod5 ?x ?y ?s ?m ?d)) ((divmod6 ?x ?y ?s ?m ?d (cmp ?x ?m)))
<> ((divmod6 ?x ?y (0 ()) ?m ?d #lt)) ((?d ?x))
<> ((divmod6 ?x ?y ?s ?m ?d #lt)) ((divmod5 ?x ?y dec ?s (rshift1 ?m) ?d))
<> ((divmod6 ?x ?y ?s ?m ?d #eq)) ((divmod4 ?x ?y ?s ?m ?d))
<> ((divmod6 ?x ?y ?s ?m ?d #gt)) ((divmod4 ?x ?y ?s ?m ?d))
-- ( floor divison ) -- ( floor divison )
<> ((div ?x ?y)) ((div' (divmod ?x ?y))) <> ((div ?x ?y)) ((div' (divmod ?x ?y)))
@ -215,7 +196,5 @@
<> ((mod ?x ?y)) ((mod' (divmod ?x ?y))) <> ((mod ?x ?y)) ((mod' (divmod ?x ?y)))
<> ((mod' (?q ?r))) (?r) <> ((mod' (?q ?r))) (?r)
-- ( stringify ) -- (bstr (mul (int 2399) (int 3499)))
<> ((stringify ?*)) (?*) (str (int 1234567890))
(bstr (mul (int 2399) (int 3499)))