|
55 | 55 |
|
56 | 56 | (define (altn-errors altn pcontext ctx errcache mask) |
57 | 57 | (define repr (context-repr ctx)) |
58 | | - (define repr-bits (representation-total-bits repr)) |
59 | 58 | (define err (errors-score-masked (hash-ref errcache (alt-expr altn)) mask)) |
60 | | - (format-accuracy err repr-bits #:unit "%")) |
61 | | - |
62 | | -(define (expr->fpcore expr ctx #:ident [ident #f]) |
63 | | - (list 'FPCore |
64 | | - (context-vars ctx) |
65 | | - (let loop ([expr expr]) |
66 | | - (match expr |
67 | | - [(? symbol?) expr] |
68 | | - [(? number?) expr] |
69 | | - [(? literal?) (literal-value expr)] |
70 | | - [(approx spec impl) (loop impl)] |
71 | | - [(hole precision spec) (loop spec)] |
72 | | - [(list op args ...) (cons op (map loop args))])))) |
| 59 | + (format-accuracy err repr #:unit "%")) |
73 | 60 |
|
74 | 61 | (define (mixed->fpcore expr ctx) |
75 | 62 | (define expr* |
|
123 | 110 | (make-list (pcontext-length pcontext) #f)) |
124 | 111 |
|
125 | 112 | ;; HTML renderer for derivations |
126 | | -(define (render-history altn pcontext ctx errcache [mask (make-mask pcontext)]) |
127 | | - (match altn |
128 | | - [(alt prog 'start (list) _) |
129 | | - (define err (altn-errors altn pcontext ctx errcache mask)) |
| 113 | +(define (render-history json ctx) |
| 114 | + (define err |
| 115 | + (match (hash-ref json 'error) |
| 116 | + [(? number? n) (format-accuracy n (context-repr ctx) #:unit "%")] |
| 117 | + [other other])) |
| 118 | + (define prog (read (open-input-string (hash-ref json 'program)))) |
| 119 | + (match (hash-ref json 'type) |
| 120 | + ["start" |
130 | 121 | (list `(li (p "Initial program " (span ((class "error")) ,err)) |
131 | | - (div ((class "math")) "\\[" ,(program->tex prog ctx) "\\]")))] |
| 122 | + (div ((class "math")) "\\[" ,(fpcore->tex prog) "\\]")))] |
132 | 123 |
|
133 | | - [(alt prog 'add-preprocessing `(,prev) _) |
134 | | - ;; TODO message to user is? proof later |
135 | | - `(,@(render-history prev pcontext ctx errcache mask) (li "Add Preprocessing"))] |
136 | | - |
137 | | - [(alt _ `(regimes ,splitpoints) prevs _) |
138 | | - (define intervals |
139 | | - (for/list ([start-sp (cons (sp -1 -1 #f) splitpoints)] |
140 | | - [end-sp splitpoints]) |
141 | | - (interval (sp-cidx end-sp) (sp-point start-sp) (sp-point end-sp) (sp-bexpr end-sp)))) |
142 | | - (define repr (context-repr ctx)) |
| 124 | + ["add-preprocessing" `(,@(render-history (hash-ref json 'prev) ctx) (li "Add Preprocessing"))] |
143 | 125 |
|
| 126 | + ["regimes" |
| 127 | + (define prevs (hash-ref json 'prevs)) |
144 | 128 | `((li ((class "event")) "Split input into " ,(~a (length prevs)) " regimes") |
145 | 129 | (li ,@(apply append |
146 | | - (for/list ([entry prevs] |
147 | | - [idx (in-naturals)] |
148 | | - [new-mask (regimes-pcontext-masks pcontext splitpoints prevs ctx)]) |
149 | | - (define mask* (map and-fn mask new-mask)) |
150 | | - (define entry-ivals |
151 | | - (filter (λ (intrvl) (= (interval-alt-idx intrvl) idx)) intervals)) |
152 | | - (define condition |
153 | | - (string-join (map (curryr interval->string repr) entry-ivals) " or ")) |
154 | | - `((h2 (code "if " (span ((class "condition")) ,condition))) |
155 | | - (ol ,@(render-history entry pcontext ctx errcache mask*)))))) |
| 130 | + (for/list ([entry (in-list prevs)] |
| 131 | + [condition (in-list (hash-ref json 'conditions))]) |
| 132 | + `((h2 (code "if " (span ((class "condition")) ,(string-join condition " or ")))) |
| 133 | + (ol ,@(render-history entry ctx)))))) |
156 | 134 | (li ((class "event")) "Recombined " ,(~a (length prevs)) " regimes into one program."))] |
157 | 135 |
|
158 | | - [(alt prog `(taylor ,loc ,pt ,var) `(,prev) _) |
159 | | - (define core (mixed->fpcore prog ctx)) |
160 | | - `(,@(render-history prev pcontext ctx errcache mask) |
161 | | - (li (p "Taylor expanded in " ,(~a var) " around " ,(~a pt)) |
162 | | - (div ((class "math")) |
163 | | - "\\[\\leadsto " |
164 | | - ,(core->tex core #:loc (and loc (cons 2 loc)) #:color "blue") |
165 | | - "\\]")))] |
| 136 | + ["taylor" |
| 137 | + (define-values (prev pt var loc) (apply values (map (curry hash-ref json) '(prev pt var loc)))) |
| 138 | + `(,@(render-history prev ctx) |
| 139 | + (li (p "Taylor expanded in " ,var " around " ,pt) |
| 140 | + (div ((class "math")) "\\[\\leadsto " ,(fpcore->tex prog #:loc loc) "\\]")))] |
166 | 141 |
|
167 | | - [(alt prog `(evaluate ,loc) `(,prev) _) |
168 | | - (define core (mixed->fpcore prog ctx)) |
169 | | - (define err (altn-errors altn pcontext ctx errcache mask)) |
170 | | - `(,@(render-history prev pcontext ctx errcache mask) |
| 142 | + ["evaluate" |
| 143 | + (define-values (prev loc) (apply values (map (curry hash-ref json) '(prev loc)))) |
| 144 | + `(,@(render-history prev ctx) |
171 | 145 | (li (p "Evaluated real constant" (span ((class "error")) ,err)) |
172 | | - (div ((class "math")) |
173 | | - "\\[\\leadsto " |
174 | | - ,(core->tex core #:loc (and loc (cons 2 loc)) #:color "blue") |
175 | | - "\\]")))] |
| 146 | + (div ((class "math")) "\\[\\leadsto " ,(fpcore->tex prog #:loc loc) "\\]")))] |
176 | 147 |
|
177 | | - [(alt prog `(rr ,loc ,input ,proof) `(,prev) _) |
178 | | - (define err (altn-errors altn pcontext ctx errcache mask)) |
179 | | - `(,@(render-history prev pcontext ctx errcache mask) |
180 | | - (li ,(if proof |
181 | | - (render-proof proof pcontext ctx errcache mask) |
182 | | - "")) |
| 148 | + ["rr" |
| 149 | + (define-values (prev loc proof) (apply values (map (curry hash-ref json) '(prev loc proof)))) |
| 150 | + `(,@(render-history prev ctx) |
| 151 | + (li ,(if (eq? proof (json-null)) |
| 152 | + "" |
| 153 | + (render-proof proof ctx))) |
183 | 154 | (li (p "Applied rewrites" (span ((class "error")) ,err)) |
184 | | - (div ((class "math")) "\\[\\leadsto " ,(program->tex prog ctx #:loc loc) "\\]")))])) |
| 155 | + (div ((class "math")) "\\[\\leadsto " ,(fpcore->tex prog #:loc loc) "\\]")))])) |
185 | 156 |
|
186 | 157 | (define (errors-score-masked errs mask) |
187 | 158 | (if (ormap identity mask) |
|
191 | 162 | err)) |
192 | 163 | (errors-score errs))) |
193 | 164 |
|
194 | | -(define (render-proof proof pcontext ctx errcache mask) |
195 | | - `(div ((class "proof")) |
196 | | - (details (summary "Step-by-step derivation") |
197 | | - (ol ,@(for/list ([step proof]) |
198 | | - (define-values (dir rule loc expr) (splice-proof-step step)) |
199 | | - ;; need to handle mixed real/float expressions |
200 | | - (define-values (err prog) |
201 | | - (cond |
202 | | - [(impl-prog? expr) ; impl program? |
203 | | - (define score (errors-score-masked (hash-ref errcache expr) mask)) |
204 | | - (define bits (representation-total-bits (context-repr ctx))) |
205 | | - (values (format-accuracy score bits) (program->fpcore expr ctx))] |
206 | | - [else (values "N/A" (mixed->fpcore expr ctx))])) |
207 | | - ; the proof |
208 | | - (if (equal? dir 'Goal) |
209 | | - "" |
210 | | - `(li ,(let ([dir (match dir |
211 | | - ['Rewrite<= "right to left"] |
212 | | - ['Rewrite=> "left to right"])]) |
213 | | - `(p (code ([title ,dir]) ,(~a rule)) |
214 | | - (span ((class "error")) ,err))) |
215 | | - (div ((class "math")) |
216 | | - "\\[\\leadsto " |
217 | | - ,(core->tex prog #:loc (and loc (cons 2 loc)) #:color "blue") |
218 | | - "\\]")))))))) |
| 165 | +(define (render-proof proof-json ctx) |
| 166 | + `(div |
| 167 | + ((class "proof")) |
| 168 | + (details |
| 169 | + (summary "Step-by-step derivation") |
| 170 | + (ol ,@ |
| 171 | + (for/list ([step (in-list proof-json)]) |
| 172 | + (define-values (direction err loc rule prog-str) |
| 173 | + (apply values (map (curry hash-ref step) '(direction error loc rule program)))) |
| 174 | + (define dir |
| 175 | + (match direction |
| 176 | + ["goal" "goal"] |
| 177 | + ["rtl" "right to left"] |
| 178 | + ["ltr" "left to right"])) |
| 179 | + (define prog (read (open-input-string prog-str))) |
| 180 | + (if (equal? dir "goal") |
| 181 | + "" |
| 182 | + `(li (p (code ([title ,dir]) ,rule) |
| 183 | + (span ((class "error")) |
| 184 | + ,(if (number? err) |
| 185 | + (format-accuracy err (context-repr ctx) #:unit "%") |
| 186 | + err))) |
| 187 | + (div ((class "math")) "\\[\\leadsto " ,(fpcore->tex prog #:loc loc) "\\]")))))))) |
219 | 188 |
|
220 | 189 | (define (render-json altn pcontext ctx errcache [mask (make-list (pcontext-length pcontext) #f)]) |
221 | 190 | (define repr (context-repr ctx)) |
|
226 | 195 |
|
227 | 196 | (match altn |
228 | 197 | [(alt prog 'start (list) _) |
229 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) (type . "start") (error . ,err))] |
| 198 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) (type . "start") (error . ,err))] |
230 | 199 |
|
231 | 200 | [(alt prog `(regimes ,splitpoints) prevs _) |
232 | 201 | (define intervals |
233 | 202 | (for/list ([start-sp (cons (sp -1 -1 #f) splitpoints)] |
234 | 203 | [end-sp splitpoints]) |
235 | 204 | (interval (sp-cidx end-sp) (sp-point start-sp) (sp-point end-sp) (sp-bexpr end-sp)))) |
236 | 205 |
|
237 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) |
| 206 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) |
238 | 207 | (type . "regimes") |
| 208 | + (error . ,err) |
239 | 209 | (conditions . ,(for/list ([entry prevs] |
240 | 210 | [idx (in-naturals)]) |
241 | 211 | (define entry-ivals |
|
247 | 217 | (render-json entry pcontext ctx errcache mask*))))] |
248 | 218 |
|
249 | 219 | [(alt prog `(taylor ,loc ,pt ,var) `(,prev) _) |
250 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) |
| 220 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) |
251 | 221 | (type . "taylor") |
252 | 222 | (prev . ,(render-json prev pcontext ctx errcache mask)) |
253 | 223 | (pt . ,(~a pt)) |
|
256 | 226 | (error . ,err))] |
257 | 227 |
|
258 | 228 | [(alt prog `(evaluate ,loc) `(,prev) _) |
259 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) |
| 229 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) |
260 | 230 | (type . "evaluate") |
261 | 231 | (prev . ,(render-json prev pcontext ctx errcache mask)) |
262 | 232 | (loc . ,loc) |
263 | 233 | (error . ,err))] |
264 | 234 |
|
265 | 235 | [(alt prog `(rr ,loc ,input ,proof) `(,prev) _) |
266 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) |
| 236 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) |
267 | 237 | (type . "rr") |
268 | 238 | (prev . ,(render-json prev pcontext ctx errcache mask)) |
269 | 239 | (proof . ,(if proof |
|
273 | 243 | (error . ,err))] |
274 | 244 |
|
275 | 245 | [(alt prog 'add-preprocessing `(,prev) preprocessing) |
276 | | - `#hash((program . ,(fpcore->string (expr->fpcore prog ctx))) |
| 246 | + `#hash((program . ,(fpcore->string (program->fpcore prog ctx))) |
277 | 247 | (type . "add-preprocessing") |
278 | 248 | (prev . ,(render-json prev pcontext ctx errcache mask)) |
279 | 249 | (error . ,err) |
|
282 | 252 | (define (render-proof-json proof pcontext ctx errcache mask) |
283 | 253 | (for/list ([step proof]) |
284 | 254 | (define-values (dir rule loc expr) (splice-proof-step step)) |
285 | | - (define err |
286 | | - (if (impl-prog? expr) |
287 | | - (errors-score-masked (hash-ref errcache expr) mask) |
288 | | - "N/A")) |
| 255 | + (define-values (err fpcore) |
| 256 | + (cond |
| 257 | + [(impl-prog? expr) |
| 258 | + (values (errors-score-masked (hash-ref errcache expr) mask) (program->fpcore expr ctx))] |
| 259 | + [else (values "N/A" (mixed->fpcore expr ctx))])) |
289 | 260 |
|
290 | 261 | `#hash((error . ,err) |
291 | | - (program . ,(fpcore->string (expr->fpcore expr ctx))) |
| 262 | + (program . ,(fpcore->string fpcore)) |
292 | 263 | (direction . ,(match dir |
293 | 264 | ['Rewrite<= "rtl"] |
294 | 265 | ['Rewrite=> "ltr"] |
|
0 commit comments