A categorical programming language
リビジョン | 23bcde0c09f26571b9c259618fa2bfdfb0880c99 (tree) |
---|---|
日時 | 2023-02-02 14:41:01 |
作者 | Corbin <cds@corb...> |
コミッター | Corbin |
Debug FP multiplication somewhat.
For some reason, ground inputs are not giving a ground output. This
might be an issue with the structure of eval°, but I'm not seeing how.
@@ -108,8 +108,8 @@ | ||
108 | 108 | ((== expr 'not) (not° i o)) |
109 | 109 | ((== expr 'zero) (== i 'star) (zeroo o)) |
110 | 110 | ((== expr 'succ) (succ° i o)) |
111 | - ((== expr 'f-zero) (== i 'star) (== o fp-zero+)) | |
112 | - ((== expr 'f-one) (== i 'star) (== o (build-fp 1.0))) | |
111 | + ((== expr 'f-zero) (== o fp-zero+) (== i 'star)) | |
112 | + ((== expr 'f-one) (== o (build-fp 1.0)) (== i 'star)) | |
113 | 113 | ((== expr 'f-sign) (== o #t) (fp-sign° i 'pos)) |
114 | 114 | ((== expr 'f-sign) (== o #f) (fp-sign° i 'neg)) |
115 | 115 | )) |
@@ -8,10 +8,13 @@ | ||
8 | 8 | mini-kanren |
9 | 9 | (only matchable match-lambda) |
10 | 10 | (only mathh frexp) |
11 | - rels) | |
11 | + rels | |
12 | + (chicken pretty-print)) | |
12 | 13 | |
13 | 14 | ; An implementation of relational floating-point arithmetic, as seen in: |
14 | 15 | ; https://www.cs.toronto.edu/~lczhang/sandre_float2021.pdf |
16 | + ; Zeroes and infinities are given their own distinct encodings. Otherwise, we | |
17 | + ; follow the paper relatively closely. | |
15 | 18 | |
16 | 19 | ; Like (not (var? x)) but checks entire lists. |
17 | 20 | (define nat? (match-lambda [(or (0 . n) (1 . n)) (nat? n)] [() #t] [_ #f])) |
@@ -26,9 +29,10 @@ | ||
26 | 29 | [_ #f])) |
27 | 30 | |
28 | 31 | ; Overall precision. |
29 | - (define exp-precision 2) | |
30 | - (define exp-mantissa-factor (expt 2.0 exp-precision)) | |
31 | - (define exp-bias (- (expt 2 (- exp-precision 1)) 1)) | |
32 | + (define exponent-length 3) | |
33 | + (define mantissa-length 4) | |
34 | + (define exp-mantissa-factor (expt 2.0 exponent-length)) | |
35 | + (define exp-bias (- (expt 2 (- exponent-length 1)) 1)) | |
32 | 36 | |
33 | 37 | ; Convert an inexact number into a relational term. |
34 | 38 | ; float -> Float |
@@ -70,9 +74,15 @@ | ||
70 | 74 | ; Signed infinities. |
71 | 75 | (define fp-inf+ '(pos inf)) |
72 | 76 | (define fp-inf- '(neg inf)) |
77 | + (define (fp-inf° f) (conde ((== f fp-inf+)) ((== f fp-inf-)))) | |
78 | + | |
79 | + ; Natural numbers which are mantissas. | |
80 | + (define (mantissa° m) (lengtho m (build-num mantissa-length))) | |
81 | + (define (mantissa-short° m) (lengtho m (build-num (- mantissa-length 1)))) | |
73 | 82 | |
74 | 83 | ; Decompose nonzero finite floating-point numbers. |
75 | - (define (fp-decomp° f s e m) (== f `(,s ,e ,m))) | |
84 | + (define (fp-decomp° f s e m) (== f `(,s ,e ,m)) | |
85 | + (conde ((== s 'pos)) ((== s 'neg))) (mantissa° m)) | |
76 | 86 | |
77 | 87 | (define (fp-finite° f) |
78 | 88 | (conde |
@@ -80,6 +90,7 @@ | ||
80 | 90 | ((fresh (s e m) (fp-decomp° f s e m))))) |
81 | 91 | |
82 | 92 | ; The less-than relation. |
93 | + ; See p3-4. | |
83 | 94 | (define (fp-<° x y) |
84 | 95 | (conde |
85 | 96 | ((== x fp-inf-) (fp-finite° y)) |
@@ -100,22 +111,18 @@ | ||
100 | 111 | (define (mantissa-shift° mantissa diff shifted) |
101 | 112 | (fresh (prefix) |
102 | 113 | (appendo prefix shifted mantissa) |
103 | - (lengtho shifted diff))) | |
114 | + (lengtho prefix diff))) | |
104 | 115 | |
105 | 116 | ; Natural numbers which are exponents. |
106 | 117 | (define (exponent° e) |
107 | - (fresh (n) (<=o n (build-num exp-precision)) (lengtho e n))) | |
108 | - | |
109 | - ; Natural numbers which are mantissas. | |
110 | - (define (mantissa° m) (lengtho m (build-num exp-precision))) | |
111 | - (define (mantissa-short° m) (lengtho m (build-num (- exp-precision 1)))) | |
118 | + (fresh (n) (<=o n (build-num exponent-length)) (lengtho e n))) | |
112 | 119 | |
113 | 120 | ; Trim a mantissa, saving the extra bits. |
114 | 121 | (define (drop-leastsig-bit° m rm bits) |
115 | 122 | (fresh () (mantissa° rm) (appendo bits rm m))) |
116 | 123 | |
117 | 124 | ; XXX this is not correct! |
118 | - (define (fp-overflow° e pm m) succeed) | |
125 | + (define (fp-overflow° e pm m) (== pm m)) | |
119 | 126 | |
120 | 127 | ; Helper for addition. |
121 | 128 | ; See p4-5. |
@@ -134,18 +141,20 @@ | ||
134 | 141 | (conde |
135 | 142 | ((fp-zero° f1) (== f2 r)) |
136 | 143 | ((fp-zero° f2) (== f1 r)) |
137 | - ((== f1 fp-inf+) (== f2 fp-inf+) (== r fp-inf+)) | |
138 | - ((== f1 fp-inf-) (== f2 fp-inf-) (== r fp-inf-)))) | |
144 | + ((== r fp-inf+) | |
145 | + (conde ((== f1 fp-inf+) (fp-finite° f2)) ((fp-finite° f1) (== f2 fp-inf+)))) | |
146 | + ((== r fp-inf-) | |
147 | + (conde ((== f1 fp-inf-) (fp-finite° f2)) ((fp-finite° f1) (== f2 fp-inf-)))))) | |
139 | 148 | ; XXX typical cases |
140 | 149 | |
141 | 150 | ; XOR for sign bits. |
142 | 151 | ; For refutational completeness, this is given as a table. |
143 | 152 | (define (sign-xor° s1 s2 s3) |
144 | 153 | (conde |
145 | - ((== s1 'pos) (== s2 'pos) (== s3 'neg)) | |
146 | - ((== s1 'pos) (== s2 'neg) (== s3 'pos)) | |
147 | - ((== s1 'neg) (== s2 'pos) (== s3 'pos)) | |
148 | - ((== s1 'neg) (== s2 'neg) (== s3 'neg)))) | |
154 | + ((== s1 'pos) (== s2 'pos) (== s3 'pos)) | |
155 | + ((== s1 'pos) (== s2 'neg) (== s3 'neg)) | |
156 | + ((== s1 'neg) (== s2 'pos) (== s3 'neg)) | |
157 | + ((== s1 'neg) (== s2 'neg) (== s3 'pos)))) | |
149 | 158 | |
150 | 159 | ; Add exponents for multiplication. |
151 | 160 | (define (compute-exp° e1 e2 bits re) |
@@ -154,17 +163,19 @@ | ||
154 | 163 | ((mantissa° bits) (succ° pre-re re)) |
155 | 164 | ((mantissa-short° bits) (== pre-re re))) |
156 | 165 | (+o e1 e2 esum) |
157 | - (+o exp-bias pre-re re))) | |
166 | + (+o (build-num exp-bias) pre-re esum))) | |
158 | 167 | |
159 | 168 | ; Multiplication. |
160 | 169 | ; See p6. |
161 | 170 | (define (fp-*° f1 f2 r) |
162 | 171 | (fresh (s1 s2 rs) |
163 | 172 | (sign-xor° s1 s2 rs) |
173 | + (fp-sign° f1 s1) (fp-sign° f2 s2) (fp-sign° r rs) | |
164 | 174 | (conde |
165 | - ((== f1 `(,s1 zero)) (caro f2 s2) (== r `(,rs zero))) | |
166 | - ((caro f1 s1) (== f2 `(,s2 zero)) (== r `(,rs zero))) | |
167 | - ; XXX infinities | |
175 | + ((fp-zero° r) (conde ((fp-zero° f1)) ((fp-zero° f2)))) | |
176 | + ((fp-inf° r) (conde | |
177 | + ((fp-inf° f1) (fp-finite° f2)) | |
178 | + ((fp-finite° f1) (fp-inf° f2)))) | |
168 | 179 | ((fresh (e1 m1 e2 m2 re rm) |
169 | 180 | (fp-decomp° f1 s1 e1 m1) |
170 | 181 | (fp-decomp° f2 s2 e2 m2) |