480 lines
18 KiB
Plaintext
480 lines
18 KiB
Plaintext
<> (-- ?x) ()
|
|
|
|
-- ( N: little-endian natural numbers )
|
|
|
|
-- ( NOTE: this file requires a patched modal which increases some of the limits )
|
|
-- ( otherwise you'll get a Segmentation Fault )
|
|
|
|
-- ( constants )
|
|
<> zero ((0 ()))
|
|
<> one ((1 ()))
|
|
<> two ((0 (1 ())))
|
|
<> three ((1 (1 ())))
|
|
<> four ((0 (0 (1 ()))))
|
|
<> eight ((0 (0 (0 (1 ())))))
|
|
<> ten ((0 (1 (0 (1 ())))))
|
|
<> sixteen ((0 (0 (0 (0 (1 ()))))))
|
|
|
|
-- reverse ()-terminated list
|
|
<> (reverse ?x) (reverse1 () ?x)
|
|
<> (reverse1 ?a ()) (?a)
|
|
<> (reverse1 ?a (?h ?t)) (reverse1 (?h ?a) ?t)
|
|
|
|
-- ( to natural number )
|
|
<> ((nat ?*)) ((nat1 (?*)))
|
|
<> ((nat1 ?x)) ((sum f (N one) g reverse ?x))
|
|
<> (g ()) (())
|
|
<> (g (?h ?t)) (((binary ?h) g ?t))
|
|
<> (f (N ?u) ()) (())
|
|
<> (f (N ?u) (?h ?t)) (((mul (N ?h) (N ?u)) f (mul (N ?u) (N ten)) ?t))
|
|
|
|
-- ( to integer )
|
|
<> ((nat-base (N ?b) ?*)) ((sum f' (N ?b) (N one) g' reverse (?*)))
|
|
<> (g' ()) (())
|
|
<> (g' (?h ?t)) (((binary ?h) g' ?t))
|
|
<> (f' (N ?b) (N ?u) ()) (())
|
|
<> (f' (N ?b) (N ?u) (?h ?t)) (((mul (N ?h) (N ?u)) f' (N ?b) (mul (N ?u) (N ?b)) ?t))
|
|
|
|
-- ( comparison operartions )
|
|
<> ((cmp (N ?x) (N ?y))) ((cmpc #eq ?x ?y))
|
|
<> ((cmpc ?e () ())) (?e)
|
|
<> ((cmpc ?e (1 ?x) ())) (#gt)
|
|
<> ((cmpc ?e (0 ?x) ())) ((cmpc ?e ?x ()))
|
|
<> ((cmpc ?e () (1 ?y))) (#lt)
|
|
<> ((cmpc ?e () (0 ?y))) ((cmpc ?e () ?y))
|
|
<> ((cmpc ?e (0 ?x) (0 ?y))) ((cmpc ?e ?x ?y))
|
|
<> ((cmpc ?e (1 ?x) (0 ?y))) ((cmpc #gt ?x ?y))
|
|
<> ((cmpc ?e (0 ?x) (1 ?y))) ((cmpc #lt ?x ?y))
|
|
<> ((cmpc ?e (1 ?x) (1 ?y))) ((cmpc ?e ?x ?y))
|
|
|
|
-- ( addition )
|
|
<> ((add (N ?x) (N ?y))) (add/e force (addc 0 ?x ?y))
|
|
<> ((addc 0 () ())) (())
|
|
<> ((addc 1 () ())) ((1 ()))
|
|
<> ((addc 0 ?x ())) (?x)
|
|
<> ((addc 0 () ?y)) (?y)
|
|
<> ((addc 1 ?x ())) ((addc 1 ?x (0 ())))
|
|
<> ((addc 1 () ?y)) ((addc 1 (0 ()) ?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 (1 ?x) (0 ?y))) ((1 (addc 0 ?x ?y)))
|
|
<> ((addc 0 (1 ?x) (1 ?y))) ((0 (addc 1 ?x ?y)))
|
|
<> ((addc 1 (0 ?x) (0 ?y))) ((1 (addc 0 ?x ?y)))
|
|
<> ((addc 1 (0 ?x) (1 ?y))) ((0 (addc 1 ?x ?y)))
|
|
<> ((addc 1 (1 ?x) (0 ?y))) ((0 (addc 1 ?x ?y)))
|
|
<> ((addc 1 (1 ?x) (1 ?y))) ((1 (addc 1 ?x ?y)))
|
|
<> (add/e force/r ?x) ((N ?x))
|
|
|
|
-- ( summation )
|
|
<> ((sum ())) ((N (0 ())))
|
|
<> ((sum (?a ()))) (?a)
|
|
<> ((sum (?a (?b ?c)))) ((sum ((add ?a ?b) ?c)))
|
|
|
|
-- ( multiplication )
|
|
<> ((mul (N ?x) (N ?y))) (mul/e (mulc () ?x ?y))
|
|
<> ((mulc ?t () ?y)) ((sum ?t))
|
|
<> ((mulc ?t (0 ?x) ?y)) ((mulc ?t ?x (0 ?y)))
|
|
<> ((mulc ?t (1 ?x) ?y)) ((mulc ((N ?y) ?t) ?x (0 ?y)))
|
|
<> (mul/e (N ?x)) ((N ?x))
|
|
|
|
-- ( subtraction )
|
|
<> ((sub (N ?x) (N ?y))) ((sub1 0 ?x ?y ()))
|
|
|
|
<> ((sub1 ?c (?b ?x) (?b ?y) ?s)) ((sub1 ?c ?x ?y (?c ?s)))
|
|
<> ((sub1 0 (1 ?x) (0 ?y) ?s)) ((sub1 0 ?x ?y (1 ?s)))
|
|
<> ((sub1 1 (1 ?x) (0 ?y) ?s)) ((sub1 0 ?x ?y (0 ?s)))
|
|
<> ((sub1 0 (0 ?x) (1 ?y) ?s)) ((sub1 1 ?x ?y (1 ?s)))
|
|
<> ((sub1 1 (0 ?x) (1 ?y) ?s)) ((sub1 1 ?x ?y (0 ?s)))
|
|
|
|
<> ((sub1 0 () () ?s)) ((sub/u ?s))
|
|
<> ((sub1 1 () ?y ?s)) (#err)
|
|
<> ((sub1 ?c ?x () ?s)) ((sub1 ?c ?x (0 ()) ?s))
|
|
<> ((sub1 ?c () ?y ?s)) ((sub1 ?c (0 ()) ?y ?s))
|
|
|
|
<> ((sub/u (1 ?t))) ((sub/v (1 ?t) ()))
|
|
<> ((sub/u (0 ()))) ((N (0 ())))
|
|
<> ((sub/u (0 ?t))) ((sub/u ?t))
|
|
<> ((sub/v () ?s)) ((N ?s))
|
|
<> ((sub/v (?h ?t) ?s)) ((sub/v ?t (?h ?s)))
|
|
|
|
<> ((dec (N (1 ())))) ((N (0 ())))
|
|
<> ((dec (N (0 ())))) (#err)
|
|
<> ((dec (N (0 ?t)))) (dec/e (dec1 (0 ?t)))
|
|
<> ((dec (N (1 ?t)))) ((N (0 ?t)))
|
|
|
|
<> ((dec1 (0 ?t))) ((1 (dec1 ?t)))
|
|
<> ((dec1 (1 ()))) (dec/r ())
|
|
<> ((dec1 (1 ?t))) (dec/r (0 ?t))
|
|
<> ((?h dec/r ?t)) (dec/r (?h ?t))
|
|
<> (dec/e dec/r ?x) ((N ?x))
|
|
|
|
-- ( inc )
|
|
<> ((inc (N ?x))) (inc/e force (inc1 ?x))
|
|
<> ((inc1 ())) ((1 ()))
|
|
<> ((inc1 (0 ?t))) ((1 ?t))
|
|
<> ((inc1 (1 ?t))) ((0 (inc1 ?t)))
|
|
<> (inc/e force/r ?x) ((N ?x))
|
|
|
|
-- ( left shift; lshift x b means x<<b )
|
|
<> ((lshift (N ?x) (N ?k))) (lshift/e force/r (lshift1 ?x (N ?k)))
|
|
<> ((lshift1 ?x (N (0 ())))) (?x)
|
|
<> ((lshift1 ?x (N (1 ())))) ((0 ?x))
|
|
<> ((lshift1 ?x (N (0 (?a ?b))))) ((lshift1 (0 ?x) (dec (N (0 (?a ?b))))))
|
|
<> ((lshift1 ?x (N (1 (?a ?b))))) ((lshift1 (0 ?x) (N (0 (?a ?b)))))
|
|
<> (lshift/e force/r ?x) ((N ?x))
|
|
|
|
<> ((rshift1 (N (?a ())))) ((N (0 ())))
|
|
<> ((rshift1 (N (?a (?b ?c))))) ((N (?b ?c)))
|
|
|
|
-- ( 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 (N ?x) (N ?y))) (divmod/p (divmod1 ?x ?y (cmp (N ?x) (N ?y))))
|
|
<> ((divmod1 ?x ?y #lt)) (((N zero) (N ?x)))
|
|
<> ((divmod1 ?x ?y #eq)) (((N one) (N zero)))
|
|
<> ((divmod1 ?x ?y #gt)) ((divmod2 (N ?x) (N ?y) (N zero) (N ?y)))
|
|
<> ((divmod2 (N ?x) (N ?y) (N ?s) (N ?m))) ((divmod3 (N ?x) (N ?y) (N ?s) (N ?m) (cmp (N ?x) (N (0 ?m)))))
|
|
<> ((divmod3 (N ?x) (N ?y) (N ?s) (N ?m) #gt)) ((divmod2 (N ?x) (N ?y) (inc (N ?s)) (N (0 ?m))))
|
|
<> ((divmod3 (N ?x) (N ?y) (N ?s) (N ?m) #eq)) ((divmod4 (N ?x) (N ?y) (inc (N ?s)) (N (0 ?m)) (N zero)))
|
|
<> ((divmod3 (N ?x) (N ?y) (N ?s) (N ?m) #lt)) ((divmod4 (N ?x) (N ?y) (N ?s) (N ?m) (N zero)))
|
|
<> ((divmod4 (N ?x) (N ?y) (N (0 ())) (N ?m) (N ?d))) (((add (N ?d) (N one)) (sub (N ?x) (N ?y))))
|
|
<> ((divmod4 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d))) ((divmod5 (sub (N ?x) (N ?m)) (N ?y) (dec (N ?s)) (rshift1 (N ?m)) (add (N ?d) (lshift (N one) (N ?s)))))
|
|
<> ((divmod5 (N (0 ())) (N ?y) (N ?s) (N ?m) (N ?d))) (((N ?d) (N (0 ()))))
|
|
<> ((divmod5 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d))) ((divmod6 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d) (cmp (N ?x) (N ?m))))
|
|
<> ((divmod6 (N ?x) (N ?y) (N (0 ())) (N ?m) (N ?d) #lt)) (((N ?d) (N ?x)))
|
|
<> ((divmod6 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d) #lt)) ((divmod5 (N ?x) (N ?y) (dec (N ?s)) (rshift1 (N ?m)) (N ?d)))
|
|
<> ((divmod6 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d) #eq)) ((divmod4 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d)))
|
|
<> ((divmod6 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d) #gt)) ((divmod4 (N ?x) (N ?y) (N ?s) (N ?m) (N ?d)))
|
|
<> (divmod/p ((N ?q) (N ?r))) (divmod/e (force ?q force ?r))
|
|
<> (divmod/e (force/r ?q force/r ?r)) (((N ?q) (N ?r)))
|
|
|
|
-- ( floor divison )
|
|
<> ((div (N ?x) (N ?y))) ((div1 (divmod (N ?x) (N ?y))))
|
|
<> ((div1 (?q ?r))) (?q)
|
|
|
|
-- ( remainder )
|
|
<> ((mod (N ?x) (N ?y))) ((mod1 (divmod (N ?x) (N ?y))))
|
|
<> ((mod1 (?q ?r))) (?r)
|
|
|
|
-- ( expontentiation )
|
|
<> ((pow (N ?x) ())) ((N one))
|
|
<> ((pow (N ?x) (N (0 ())))) ((pow (N ?x) ()))
|
|
<> ((pow (N ?x) (N (1 ())))) ((N ?x))
|
|
<> ((pow (N ?x) (N (0 ?k)))) ((pow (mul (N ?x) (N ?x)) (N ?k)))
|
|
<> ((pow (N ?x) (N (1 ?k)))) ((mul (N ?x) (pow (mul (N ?x) (N ?x)) (N ?k))))
|
|
|
|
-- ( greatest common denominator )
|
|
<> ((gcd ?a ?b)) ((gcd1 ?a ?b (cmp ?b (N (0 ())))))
|
|
<> ((gcd1 ?a ?b #eq)) (?a)
|
|
<> ((gcd1 ?a ?b #gt)) ((gcd ?b (mod ?a ?b)))
|
|
<> ((gcd1 ?a ?b #lt)) ((gcd ?b (mod ?a ?b)))
|
|
|
|
-- ( least common multiple )
|
|
<> ((lcm ?a ?b)) ((mul ?a (div ?b (gcd ?a ?b))))
|
|
|
|
-- ( decimal digit to binary )
|
|
<> ((binary 0)) ((0 ()))
|
|
<> ((binary 1)) ((1 ()))
|
|
<> ((binary 2)) ((0 (1 ())))
|
|
<> ((binary 3)) ((1 (1 ())))
|
|
<> ((binary 4)) ((0 (0 (1 ()))))
|
|
<> ((binary 5)) ((1 (0 (1 ()))))
|
|
<> ((binary 6)) ((0 (1 (1 ()))))
|
|
<> ((binary 7)) ((1 (1 (1 ()))))
|
|
<> ((binary 8)) ((0 (0 (0 (1 ())))))
|
|
<> ((binary 9)) ((1 (0 (0 (1 ())))))
|
|
<> ((binary a)) ((0 (1 (0 (1 ())))))
|
|
<> ((binary b)) ((1 (1 (0 (1 ())))))
|
|
<> ((binary c)) ((0 (0 (1 (1 ())))))
|
|
<> ((binary d)) ((1 (0 (1 (1 ())))))
|
|
<> ((binary e)) ((0 (1 (1 (1 ())))))
|
|
<> ((binary f)) ((1 (1 (1 (1 ())))))
|
|
<> ((binary g)) ((0 (0 (0 (0 (1 ()))))))
|
|
<> ((binary h)) ((1 (0 (0 (0 (1 ()))))))
|
|
<> ((binary i)) ((0 (1 (0 (0 (1 ()))))))
|
|
<> ((binary j)) ((1 (1 (0 (0 (1 ()))))))
|
|
<> ((binary k)) ((0 (0 (1 (0 (1 ()))))))
|
|
<> ((binary l)) ((1 (0 (1 (0 (1 ()))))))
|
|
<> ((binary m)) ((0 (1 (1 (0 (1 ()))))))
|
|
<> ((binary n)) ((1 (1 (1 (0 (1 ()))))))
|
|
<> ((binary o)) ((0 (0 (0 (1 (1 ()))))))
|
|
<> ((binary p)) ((1 (0 (0 (1 (1 ()))))))
|
|
<> ((binary q)) ((0 (1 (0 (1 (1 ()))))))
|
|
<> ((binary r)) ((1 (1 (0 (1 (1 ()))))))
|
|
<> ((binary s)) ((0 (0 (1 (1 (1 ()))))))
|
|
<> ((binary t)) ((1 (0 (1 (1 (1 ()))))))
|
|
<> ((binary u)) ((0 (1 (1 (1 (1 ()))))))
|
|
<> ((binary v)) ((1 (1 (1 (1 (1 ()))))))
|
|
<> ((binary w)) ((0 (0 (0 (0 (0 (1 ())))))))
|
|
<> ((binary x)) ((1 (0 (0 (0 (0 (1 ())))))))
|
|
<> ((binary y)) ((0 (1 (0 (0 (0 (1 ())))))))
|
|
<> ((binary z)) ((1 (1 (0 (0 (0 (1 ())))))))
|
|
|
|
-- ( binary to digits )
|
|
<> ((decimal ())) (0)
|
|
<> ((decimal (0 ()))) (0)
|
|
<> ((decimal (1 ()))) (1)
|
|
<> ((decimal (0 (1 ())))) (2)
|
|
<> ((decimal (1 (1 ())))) (3)
|
|
<> ((decimal (0 (0 (1 ()))))) (4)
|
|
<> ((decimal (1 (0 (1 ()))))) (5)
|
|
<> ((decimal (0 (1 (1 ()))))) (6)
|
|
<> ((decimal (1 (1 (1 ()))))) (7)
|
|
<> ((decimal (0 (0 (0 (1 ())))))) (8)
|
|
<> ((decimal (1 (0 (0 (1 ())))))) (9)
|
|
|
|
-- ( Z: signed integers )
|
|
|
|
<> ((nat>int + (N ?x))) ((Z (+ ?x)))
|
|
<> ((nat>int - (N ?x))) ((Z (- ?x)))
|
|
<> ((nat>pos (N ?x))) ((Z (+ ?x)))
|
|
<> ((nat>neg (N ?x))) ((Z (- ?x)))
|
|
|
|
<> ((negate (Z (+ ?x)))) ((Z (- ?x)))
|
|
<> ((negate (Z (+ ?x)))) ((Z (- ?x)))
|
|
|
|
<> ((cmp (Z (?s (0 ()))) (Z (?s (0 ()))))) (#eq)
|
|
<> ((cmp (Z (+ ?x)) (Z (+ ?y)))) ((cmp (N ?x) (N ?y)))
|
|
<> ((cmp (Z (+ ?x)) (Z (?s ?y)))) (#gt)
|
|
<> ((cmp (Z (- ?x)) (Z (- ?y)))) ((cmp (N ?y) (N ?x)))
|
|
<> ((cmp (Z (- ?x)) (Z (?s ?y)))) (#lt)
|
|
|
|
<> ((add (Z (+ ?x)) (Z (+ ?y)))) ((nat>pos (add (N ?x) (N ?y))))
|
|
<> ((add (Z (- ?x)) (Z (- ?y)))) ((nat>neg (add (N ?x) (N ?y))))
|
|
<> ((add (Z (+ ?x)) (Z (- ?y)))) ((zadd ?x ?y (cmp (N ?x) (N ?y))))
|
|
<> ((add (Z (- ?x)) (Z (+ ?y)))) ((zadd ?y ?x (cmp (N ?y) (N ?x))))
|
|
<> ((zadd ?p ?n #gt)) ((nat>pos (sub (N ?p) (N ?n))))
|
|
<> ((zadd ?p ?n #eq)) ((Z (+ (0 ()))))
|
|
<> ((zadd ?p ?n #lt)) ((nat>neg (sub (N ?n) (N ?p))))
|
|
|
|
<> ((mul (Z (?s ?x)) (Z (?s ?y)))) ((nat>pos (mul (N ?x) (N ?y))))
|
|
<> ((mul (Z (?s ?x)) (Z (?t ?y)))) ((nat>neg (mul (N ?x) (N ?y))))
|
|
|
|
<> ((sub (Z (+ ?x)) (Z (+ ?y)))) ((add (Z (+ ?x)) (Z (- ?y))))
|
|
<> ((sub (Z (+ ?x)) (Z (- ?y)))) ((add (Z (+ ?x)) (Z (+ ?y))))
|
|
<> ((sub (Z (- ?x)) (Z (+ ?y)))) ((add (Z (- ?x)) (Z (- ?y))))
|
|
<> ((sub (Z (- ?x)) (Z (- ?y)))) ((add (Z (- ?x)) (Z (+ ?y))))
|
|
|
|
-- ( n/d = q, n%d = r )
|
|
-- ( n = d * q + r )
|
|
-- ( ---------------- )
|
|
-- ( 9 = 2 * 4 + 1 )
|
|
-- ( -9 = 2 * -4 - 1 )
|
|
-- ( 9 = -2 * -4 + 1 )
|
|
-- ( -9 = -2 * 4 - 1 )
|
|
<> ((divmod (Z (+ ?x)) (Z (+ ?y)))) ((zdm + + (divmod (N ?x) (N ?y))))
|
|
<> ((divmod (Z (- ?x)) (Z (+ ?y)))) ((zdm - - (divmod (N ?x) (N ?y))))
|
|
<> ((divmod (Z (+ ?x)) (Z (- ?y)))) ((zdm - + (divmod (N ?x) (N ?y))))
|
|
<> ((divmod (Z (- ?x)) (Z (- ?y)))) ((zdm + - (divmod (N ?x) (N ?y))))
|
|
<> ((zdm ?s ?t ((N ?q) (N ?r)))) (((Z (?s ?q)) (Z (?t ?r))))
|
|
|
|
<> ((div (Z (?s ?x)) (Z (?s ?y)))) ((nat>pos (div (N ?x) (N ?y))))
|
|
<> ((div (Z (?s ?x)) (Z (?t ?y)))) ((nat>neg (div (N ?x) (N ?y))))
|
|
|
|
<> ((mod (Z (+ ?x)) (Z (?s ?y)))) ((nat>pos (div (N ?x) (N ?y))))
|
|
<> ((mod (Z (- ?x)) (Z (?s ?y)))) ((nat>neg (div (N ?x) (N ?y))))
|
|
|
|
<> ((pow (Z (+ ?x)) (N ?k))) ((nat>pos (pow (N ?x) (N ?k))))
|
|
<> ((pow (Z (- ?x)) (N (0 ?k)))) ((nat>pos (pow (N ?x) (N (0 ?k)))))
|
|
<> ((pow (Z (- ?x)) (N (1 ?k)))) ((nat>neg (pow (N ?x) (N (1 ?k)))))
|
|
|
|
-- ( to integer )
|
|
<> ((int ?*)) ((int1 (?*)))
|
|
<> ((int1 (- ?t))) ((int2 - (nat1 ?t)))
|
|
<> ((int1 (?h ?t))) ((int2 + (nat1 (?h ?t))))
|
|
<> ((int2 ?s (N ?x))) ((Z (?s ?x)))
|
|
|
|
-- ( Q: rational numbers )
|
|
|
|
-- ( ensure n and d are coprime by dividing both by their gcd )
|
|
<> ((ratify ?s (N (0 ())) (N ?d))) ((Q (+ zero one)))
|
|
<> ((ratify ?s (N ?n) (N ?d))) ((ratify1 ?s (N ?n) (N ?d) (gcd (N ?n) (N ?d))))
|
|
<> ((ratify1 ?s (N ?n) (N ?d) (N ?g))) ((ratify2 ?s (div (N ?n) (N ?g)) (div (N ?d) (N ?g))))
|
|
<> ((ratify2 ?s (N ?n) (N ?d))) ((Q (?s ?n ?d)))
|
|
|
|
-- ( convert N to Q )
|
|
<> ((nat>rat + (N ?x))) ((Q (+ ?x one)))
|
|
<> ((nat>rat - (N ?x))) ((Q (- ?x one)))
|
|
|
|
-- ( convert Z to Q )
|
|
<> ((int>rat (Z (+ ?x)))) ((Q (+ ?x one)))
|
|
<> ((int>rat (Z (- ?x)))) ((Q (- ?x one)))
|
|
|
|
<> ((negate (Q (+ ?n ?d)))) ((Q (- ?n ?d)))
|
|
<> ((negate (Q (- ?n ?d)))) ((Q (+ ?n ?d)))
|
|
|
|
<> ((cmp (Q (?s (0 ()) (1 ()))) (Q (?s (0 ()) (1 ()))))) (#eq)
|
|
<> ((cmp (Q (+ ?x ?d)) (Q (+ ?y ?e)))) ((cmp (mul (N ?x) (N ?e)) (mul (N ?y) (N ?d))))
|
|
<> ((cmp (Q (+ ?x ?d)) (Q (?s ?y ?e)))) (#gt)
|
|
<> ((cmp (Q (- ?x ?d)) (Q (- ?y ?e)))) ((cmp (mul (N ?y) (N ?d)) (mul (N ?x) (N ?e))))
|
|
<> ((cmp (Q (- ?x ?d)) (Q (?s ?y ?e)))) (#lt)
|
|
|
|
<> ((add (Q (?s ?x ?d)) (Q (?t ?y ?e)))) ((qadd1 ?s ?t (mul (N ?x) (N ?e)) (mul (N ?y) (N ?d)) (mul (N ?d) (N ?e))))
|
|
<> ((qadd1 ?s ?t (N ?x) (N ?y) (N ?d))) ((qadd2 (add (Z (?s ?x)) (Z (?t ?y))) (N ?d)))
|
|
<> ((qadd2 (Z (?s ?n)) (N ?d))) ((ratify ?s (N ?n) (N ?d)))
|
|
|
|
<> ((sub (Q (?s ?x ?d)) (Q (?t ?y ?e)))) ((qsub1 ?s ?t (mul (N ?x) (N ?e)) (mul (N ?y) (N ?d)) (mul (N ?d) (N ?e))))
|
|
<> ((qsub1 ?s ?t (N ?x) (N ?y) (N ?d))) ((qsub2 (sub (Z (?s ?x)) (Z (?t ?y))) (N ?d)))
|
|
<> ((qsub2 (Z (?s ?n)) (N ?d))) ((ratify ?s (N ?n) (N ?d)))
|
|
|
|
<> ((mul (Q (?s ?x ?d)) (Q (?t ?y ?e)))) ((qmul1 (mul (Z (?s ?x)) (Z (?t ?y))) (mul (N ?d) (N ?e))))
|
|
<> ((qmul1 (Z (?s ?n)) (N ?d))) ((ratify ?s (N ?n) (N ?d)))
|
|
|
|
<> ((div (Q (?s ?x ?d)) (Q (?t ?y ?e)))) ((mul (Q (?s ?x ?d)) (Q (?t ?e ?y))))
|
|
|
|
<> ((pow (Q (?s ?x ?d)) (N ?k))) ((qpow (pow (Z (?s ?x)) (N ?k)) (pow (N ?d) (N ?k))))
|
|
<> ((qpow (Z (?s ?n)) (N ?d))) ((ratify ?s (N ?n) (N ?d)))
|
|
|
|
-- ( to rational number )
|
|
<> ((rat ?*)) ((rat1 (?*)))
|
|
<> ((rat1 (- ?t))) ((rat2 - ?t ()))
|
|
<> ((rat1 (?h ?t))) ((rat2 + (?h ?t) ()))
|
|
<> ((rat2 ?s (/ ?t) ?a)) ((rat3 ?s (nat1 reverse ?a) (nat1 ?t)))
|
|
<> ((rat2 ?s (?h ?t) ?a)) ((rat2 ?s ?t (?h ?a)))
|
|
<> ((rat3 ?s (N ?n) (N ?d))) ((Q (?s ?n ?d)))
|
|
|
|
-- ( render as a list of characters )
|
|
<> ((tostr (N (0 ())))) ((0 ()))
|
|
<> ((tostr (N ?x))) ((tostr1 (N ?x) ()))
|
|
<> ((tostr1 (N (0 ())) ?a)) (?a)
|
|
<> ((tostr1 (N (?h ?t)) ?a)) ((tostr2 (divmod (N (?h ?t)) (N ten)) ?a))
|
|
<> ((tostr2 ((N ?q) (N ?r)) ?a)) ((tostr1 (N ?q) ((decimal ?r) ?a)))
|
|
<> ((tostr (Z (+ ?x)))) ((tostr (N ?x)))
|
|
<> ((tostr (Z (- ?x)))) ((concat (- ()) (tostr (N ?x))))
|
|
|
|
-- ( concatenate lists )
|
|
<> ((concat (?h ?t) ?r)) ((?h (concat ?t ?r)))
|
|
<> ((concat () ?r)) (?r)
|
|
|
|
-- ( to string )
|
|
<> ((str (N ?x))) (emit force (tostr (N ?x)))
|
|
<> ((str (Z (+ ?x)))) ((str (N ?x)))
|
|
<> ((str (Z (- ?x)))) (emit force (- (tostr (N ?x))))
|
|
<> ((str (Q (?s ?n ?d)))) ((qstr ?s (force (tostr (N ?n))) (force (tostr (N ?d)))))
|
|
<> ((qstr + (force/r ?l) (force/r ?m))) (emit force (concat ?l (/ ?m)))
|
|
<> ((qstr - (force/r ?l) (force/r ?m))) (emit force (concat (- ?l) (/ ?m)))
|
|
|
|
-- ( force a list to evaluate to digits/letters )
|
|
<> ((?h force/r ?t)) (force/r (?h ?t))
|
|
<> (force ()) (force/r ())
|
|
<> (force (- ?t)) ((- force ?t))
|
|
<> (force (/ ?t)) ((/ force ?t))
|
|
<> (force (0 ?t)) ((0 force ?t))
|
|
<> (force (1 ?t)) ((1 force ?t))
|
|
<> (force (2 ?t)) ((2 force ?t))
|
|
<> (force (3 ?t)) ((3 force ?t))
|
|
<> (force (4 ?t)) ((4 force ?t))
|
|
<> (force (5 ?t)) ((5 force ?t))
|
|
<> (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 (o ?t)) ((o force ?t))
|
|
<> (force (x ?t)) ((x force ?t))
|
|
|
|
-- ( emit )
|
|
<> (emit force/r ?^) ((S ?^))
|
|
|
|
-- ( to binary string )
|
|
<> ((bstr (N ?x))) ((bstr1 force ?x ()))
|
|
<> ((bstr1 force/r () ?a)) (emit force/r (0 (b ?a)))
|
|
<> ((bstr1 force/r (?h ?t) ?a)) ((bstr1 force/r ?t (?h ?a)))
|
|
|
|
-- ( to octal string )
|
|
<> ((ostr (N ?x))) ((ostr1 () ?x))
|
|
<> ((ostr1 ?s (?a (?b (?c ?t))))) ((ostr1 ((hdigit ?a ?b ?c 0) ?s) ?t))
|
|
<> ((ostr1 ?s (?a (?b ?t)))) ((ostr1 ((hdigit ?a ?b 0 0) ?s) ?t))
|
|
<> ((ostr1 ?s (?a ?t))) ((ostr1 ((hdigit ?a 0 0 0) ?s) ?t))
|
|
<> ((ostr1 ?s ())) (emit force (0 (o ?s)))
|
|
|
|
-- ( to hex string )
|
|
<> ((hstr (N ?x))) ((hstr1 () ?x))
|
|
<> ((hstr1 ?s (?a (?b (?c (?d ?t)))))) ((hstr1 ((hdigit ?a ?b ?c ?d) ?s) ?t))
|
|
<> ((hstr1 ?s (?a (?b (?c ?t))))) ((hstr1 ((hdigit ?a ?b ?c 0) ?s) ?t))
|
|
<> ((hstr1 ?s (?a (?b ?t)))) ((hstr1 ((hdigit ?a ?b 0 0) ?s) ?t))
|
|
<> ((hstr1 ?s (?a ?t))) ((hstr1 ((hdigit ?a 0 0 0) ?s) ?t))
|
|
<> ((hstr1 ?s ())) (emit force (0 (x ?s)))
|
|
<> ((hdigit 0 0 0 0)) (0)
|
|
<> ((hdigit 1 0 0 0)) (1)
|
|
<> ((hdigit 0 1 0 0)) (2)
|
|
<> ((hdigit 1 1 0 0)) (3)
|
|
<> ((hdigit 0 0 1 0)) (4)
|
|
<> ((hdigit 1 0 1 0)) (5)
|
|
<> ((hdigit 0 1 1 0)) (6)
|
|
<> ((hdigit 1 1 1 0)) (7)
|
|
<> ((hdigit 0 0 0 1)) (8)
|
|
<> ((hdigit 1 0 0 1)) (9)
|
|
<> ((hdigit 0 1 0 1)) (a)
|
|
<> ((hdigit 1 1 0 1)) (b)
|
|
<> ((hdigit 0 0 1 1)) (c)
|
|
<> ((hdigit 1 0 1 1)) (d)
|
|
<> ((hdigit 0 1 1 1)) (e)
|
|
<> ((hdigit 1 1 1 1)) (f)
|
|
|
|
|
|
-- approximate decimal expansion of rational
|
|
<> ((approx (Q (?s ?n ?d)) (N ?k))) (emit force (astr ?s (N ?k) (div (mul (pow (N ten) (N ?k)) (N ?n)) (N ?d))))
|
|
<> ((astr - (N ?k) (N ?x))) ((- (astr1 (N ?k) (N ?x) ())))
|
|
<> ((astr + (N ?k) (N ?x))) ((astr1 (N ?k) (N ?x) ()))
|
|
<> ((astr1 (N (0 ())) (N ?x) ?a)) ((concat (tostr (N ?x)) (. ?a)))
|
|
<> ((astr1 (N ?k) (N (0 ())) ?a)) ((astr1 (dec (N ?k)) (N (0 ())) (0 ?a)))
|
|
<> ((astr1 (N ?k) (N ?x) ?a)) ((astr2 (N ?k) (divmod (N ?x) (N ten)) ?a))
|
|
<> ((astr2 (N ?k) ((N ?q) (N ?r)) ?a)) ((astr1 (dec (N ?k)) (N ?q) ((decimal ?r) ?a)))
|
|
|
|
-- ( first 16 convergents of the continued fraction. the 14th term provides more precision than 64-bit floating point )
|
|
<> (pi/cf) (((nat 3) ((nat 7) ((nat 15) ((nat 1) ((nat 292) ((nat 1) ((nat 1) ((nat 1) ((nat 2) ((nat 1) ((nat 3) ((nat 1) ((nat 14) ((nat 2) ((nat 1) ((nat 1) ())))))))))))))))))
|
|
|
|
<> ((pi/rat (N ?k))) ((pi/rat1 (N ?k) pi/cf))
|
|
<> ((pi/rat1 (N (0 ())) ((N ?h) ?t))) ((Q (+ ?h one)))
|
|
<> ((pi/rat1 (N ?k) ())) (#err)
|
|
<> ((pi/rat1 (N ?k) ((N ?h) ?t))) ((add (Q (+ ?h one)) (div (Q (+ one one)) (pi/rat1 (dec (N ?k)) ?t))))
|
|
|
|
<> ((pi/rat-x (N ?k))) ((pi/finish (pi/approx (N ?k) (N one) (N three))))
|
|
<> ((pi/approx (N (0 ())) (N ?n) (N ?d))) ((ratify + (N ?n) (inc (N ?d))))
|
|
<> ((pi/approx (N ?k) (N ?n) (N ?d))) ((div (Q (+ ?n one)) (add (Q (+ ?d one)) (pi/approx (dec (N ?k)) (add (N ?n) (N ?d)) (add (N ?d) (N two))))))
|
|
<> ((pi/finish (Q ?q))) ((div (Q (+ four one)) (add (Q (+ one one)) (Q ?q))))
|
|
|
|
<> (print (S ?:)) (?:)
|
|
|
|
-- ( print (str (mul (add (rat 3/4) (rat 19/21)) (rat 135/136))) )
|
|
print (str (pi/rat (nat 0)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 1)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 2)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 3)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 4)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 5)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 6)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 7)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 8)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 9)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 10)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 11)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 12)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 13)))
|
|
print (S \n)
|
|
print (str (pi/rat (nat 14)))
|
|
print (S \n)
|