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