-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtex-translate.ss
1486 lines (1376 loc) · 55.4 KB
/
tex-translate.ss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(module tex-translate mzscheme
(require (lib "plt-match.ss")
(lib "pretty.ss")
(lib "list.ss")
(lib "etc.ss")
(lib "string.ss"))
(define-syntax (fmatch stx)
(syntax-case stx ()
[(_ arg a b c ...)
(identifier? (syntax arg))
(syntax
(let ([f (lambda () (fmatch arg b c ...))])
(match arg
a
[else (f)])))]
[(_ arg a)
(syntax (match arg a))]))
(define filename-prefix "r6-fig")
(define latex-negation "\\ensuremath{\\neg}")
(define (translate file)
(let ((r (with-input-from-file file read)))
(initialize-metafunctions/matches r)
(tex-translate r)))
(define nonterminals-break-before
'(P var pp sym p* p v
E F PG S))
(define flatten-args-metafunctions '(Var-set!d? circular? Trim))
(define otherwise-metafunctions '(pRe poSt Trim reachable))
(define combine-metafunction-names '((Trim pRe poSt) (Qtoc Qtoic)))
(define metafunctions-to-skip '(r6rs-subst-one r6rs-subst-many))
(define relation-as-metafunction '(Var-set!d? circular?))
(define two-line-metafunctions '(observable))
(define (side-condition-below? x)
(member x '("6appN!" "6refu" "6setu" "6xref" "6xset"
"6letrec" "6letrec*"
"6applyc" "6applyce")))
(define (two-line-name? x) (not (one-line-name? x)))
(define (one-line-name? x)
(memq x '(Eqv--and--equivalence
Underspecification
Basic--syntactic--forms
Arithmetic)))
;; tex-translate : sexp -> void
(define (tex-translate exp)
(let ([names/clauses combine-metafunction-names])
(let loop ([exp exp])
(fmatch exp
[`(module ,_ ,_ ,body ...)
(for-each loop body)]
[`(define ,lang (language ,productions ...))
(print-language productions)]
[`(metafunction-type ,name ,type)
(unless (member name metafunctions-to-skip)
(set! metafunction-types (cons (list name type) metafunction-types)))]
[`(define-metafunction ,name ,lang ,clauses ...)
(unless (member name metafunctions-to-skip)
(let ([in (ormap (λ (x) (member name x)) names/clauses)])
(if in
(set! names/clauses (replace-in-list name names/clauses (list name clauses)))
(print-metafunction name clauses))))]
[`(define ,name (reduction-relation ,lang ,r ...))
(print-reductions name r (one-line-name? name))]
[else (void)]))
(for-each print-metafunctions names/clauses)))
(define (replace-in-list sym lst new-thing)
(map (λ (l)
(map (λ (s) (if (eq? sym s)
new-thing
s))
l))
lst))
(define (print-language productions)
(let loop ([productions productions]
[acc null])
(cond
[(equal? (car (car productions)) 'P)
(print-partial-language (format "~a-grammar-parti.tex" filename-prefix) (reverse acc))
(print-partial-language (format "~a-grammar-partii.tex" filename-prefix) productions)]
[else
(loop (cdr productions)
(cons (car productions) acc))])))
(define (print-partial-language n productions)
(fprintf (current-error-port) "Writing file ~a ...\n" n)
(with-output-to-file n
(lambda ()
(display "\\begin{center}\n")
(display "\\begin{displaymath}\n")
(display "\\begin{array}{lr@{}ll}\n")
(for-each typeset-production productions)
(display "\\end{array}\n")
(display "\\end{displaymath}\n")
(display "\\end{center}\n"))
'truncate))
(define (check r)
(let ((v (ormap (lambda (l) (if (and (list? l) (andmap string? l))
#f
l))
r)))
(when v
(error 'check "not a list of strings: ~s" v))
r))
(define current-filename #f)
(define (print-something reduction-translate name reductions start end)
(let ((n (format "~a-~a.tex" filename-prefix (regexp-replace* #rx"[?*]" (format "~a" name) "_"))))
(fprintf (current-error-port) "Writing file ~a ...\n" n)
(set! current-filename name)
(with-output-to-file n
(lambda ()
(let* ([rules (check (map/last reduction-translate reductions))]
[reductions (apply append rules)])
(printf "~a\n" start)
(for-each display reductions)
(printf "~a\n" end)))
'truncate)
(set! current-filename #f)))
(define (map/last f lst)
(let loop ([lst lst])
(cond
[(null? lst) '()]
[(null? (cdr lst)) (list (f (car lst) #t))]
[else (cons (f (car lst) #f) (loop (cdr lst)))])))
(define (print-reductions name reductions left-right?)
(print-something (lambda (r last-one?) (reduction-translate r left-right?))
name
reductions
"\\begin{displaymath}\n\\begin{array}{l@{}l@{}lr}"
"\\end{array}\n\\end{displaymath}"))
(define (reduction-translate r left-right?)
(fmatch r
;; normal rules
[`(--> ,lhs ,rhs ,name ,extras ...)
(print-->reduction lhs rhs name left-right? (extras->side-conditions extras))]
[`(/-> ,lhs ,rhs ,name , extras ...)
(print/->reduction lhs rhs name left-right? (extras->side-conditions extras))]
[`where
;; skip where
'()]
[`((/-> ,a ,b) ,c)
;; skip defn of /->
'()]
[_
(fprintf (current-error-port) "WARNING: reduction translate on wrong thing ~s\n" r)
'()]))
(define (extras->side-conditions extras)
(let loop ([extras extras])
(cond
[(null? extras) null]
[else
(match (car extras)
[`(fresh ((,(? symbol? var) ,'...)
(,(? symbol?) ,'...)))
(append (format-side-cond `(fresh ,var ...)) (loop (cdr extras)))]
[`(fresh ((,(? symbol? var) ,'...)
(,(? symbol?) ,'...)
,whatever))
(append (format-side-cond `(fresh ,var ...)) (loop (cdr extras)))]
[`(fresh (,(? symbol? var) ,whatever))
(append (format-side-cond `(fresh (,var))) (loop (cdr extras)))]
[`(fresh ,(? symbol? vars) ...)
(append (format-side-cond `(fresh ,vars)) (loop (cdr extras)))]
[`(side-condition ,sc ...)
(append (apply append (map format-side-cond sc))
(loop (cdr extras)))]
[else
(error 'extras->side-conditions "unknown extra ~s" (car extras))])])))
;; ============================================================
;; METAFUNCTION PRINTING
;; ============================================================
(define metafunction-names '())
(define metafunction-types '())
(define (metafunction-name? x) (memq x metafunction-names))
(define (translate-mf-name name)
(case name
[(Qtoc) "\\mathscr{Q}_{i}"]
[(Qtoic) "\\mathscr{Q}_{m}"]
[(A_0) "\\mathscr{A}_{0}"]
[(A_1) "\\mathscr{A}_{1}"]
[(observable-value) "\\mathscr{O}_{v}"]
[else
(let ([char
(let loop ([chars (string->list (format "~a" name))])
(cond
[(null? chars) (char-upcase (car (string->list (format "~a" name))))]
[else
(if (char-upper-case? (car chars))
(car chars)
(loop (cdr chars)))]))])
(format "\\mathscr{~a}" char))]))
(define test-matches '())
(define (initialize-metafunctions/matches exp)
(fmatch exp
[`(module ,_ ,_ ,body ...)
(for-each
(lambda (x)
(fmatch x
[`(define-metafunction ,name ,lang ,clauses ...)
(set! metafunction-names (cons name metafunction-names))]
[`(define ,name (test-match ,lang ,pat))
(set! test-matches (cons (list name pat) test-matches))]
[else (void)]))
body)]))
(define (print-metafunction name clauses)
(let ([flatten-lhs? (memq name flatten-args-metafunctions)])
(cond
[(memq name two-line-metafunctions)
(print-something
(lambda (x last-one?)
(match x
[`(,left ,right)
(p-twoline-case name
left
right
(and last-one? (memq name otherwise-metafunctions)))]
[else (error 'print-metafunction "mismatch ~s" x)]))
name
clauses
(string-append
"\\begin{displaymath}\n\\begin{array}{l@{}l}\n"
(metafunction-type-line name 2))
"\\end{array}\n\\end{displaymath}")]
[(memq name relation-as-metafunction)
(print-something
(lambda (x last-one?)
(match x
[`(,left #f)
;; a relation skips those!
(list)]
[`(,left #t)
(p-rel-case name
left
#f
#f
flatten-lhs?)]
[`(,left ,right)
(p-rel-case name
left
right
#f
flatten-lhs?)]
[`(,left #t (side-condition ,sc))
(p-rel-case name
left
#f
sc
flatten-lhs?)]
[`(,left ,right (side-condition ,sc))
(p-rel-case name
left
right
sc
flatten-lhs?)]
[else (error 'print-metafunction "mismatch ~s" x)]))
name
clauses
(string-append
"\\begin{displaymath}\n\\begin{array}{lc@{~}l}\n"
(metafunction-type-line name 3))
"\\end{array}\n\\end{displaymath}")]
[else
(print-something
(cond
[flatten-lhs?
(lambda (x last-one?)
(match x
[`(,left ,right)
(p-flatten-case name
left
right
(and last-one? (memq name otherwise-metafunctions)))]
[else (error 'print-metafunction "mismatch ~s" x)]))]
[else
(lambda (x last-one?)
(match x
[`(,left ,right)
(p-case name left right
(and last-one? (memq name otherwise-metafunctions)))]
[else (error 'print-metafunction "mismatch ~s" x)]))])
name
clauses
(string-append
"\\begin{displaymath}\n\\begin{array}{lcl}\n"
(metafunction-type-line name 3))
"\\end{array}\n\\end{displaymath}")])))
(define (print-metafunctions names/clauses)
(let ([names/spread-out
(apply
append
(map (lambda (name/clauses)
(if (eq? 'blank name/clauses)
(list 'blank)
(let ([name (car name/clauses)])
(cons
name
(map/last (lambda (clause last-one?) (list name clause last-one?))
(cadr name/clauses))))))
(add-between 'blank names/clauses)))])
(print-something (lambda (name/clause really-the-last-one?)
(cond
[(eq? name/clause 'blank)
(list "\\\\")]
[(symbol? name/clause)
(list (metafunction-type-line name/clause 3))]
[else
(let ([name (list-ref name/clause 0)]
[clause (list-ref name/clause 1)]
[last-one? (list-ref name/clause 2)])
(fmatch clause
[`(,left ,right)
(if (memq name flatten-args-metafunctions)
(p-flatten-case name
left
right
(and last-one? (memq name otherwise-metafunctions)))
(p-case name left right
(and last-one? (memq name otherwise-metafunctions))))]
[else (error 'print-metafunctions "mismatch ~s" name)]))]))
(apply string-append (map symbol->string (map car names/clauses)))
names/spread-out
"\\begin{displaymath}\n\\begin{array}{lcl}"
"\\end{array}\n\\end{displaymath}")))
(define (metafunction-type-line name cols)
(let ([type-pr (assoc name metafunction-types)])
(unless type-pr
(error 'metafunction-type "cannot find type for ~s" name))
(let ([type (cadr type-pr)])
(cond
[(eq? (car type) '->)
(let* ([doms (cdr (all-but-last type))]
[rng (car (last-pair type))])
(format "\\multicolumn{~a}{l}{~a : ~a \\rightarrow ~a}\\\\"
cols
(translate-mf-name name)
(apply string-append (add-between " \\times " (map format-nonterm doms)))
(format-nonterm rng)))]
[else
(format "\\multicolumn{~a}{l}{~a \\in 2^{~a}}\\\\"
cols
(translate-mf-name name)
(apply string-append (add-between " \\times " (map format-nonterm type))))]))))
(define (add-between x lst)
(cond
[(null? lst) null]
[else (let loop ([fst (car lst)]
[next (cdr lst)])
(cond
[(null? next) (list fst)]
[else (list* fst x (loop (car next) (cdr next)))]))]))
(define (p-flatten-case name lhs rhs show-otherwise?)
(let ([ls (map (λ (x) (let-values ([(a b) (pattern->tex x)])
(unless (null? b)
(error 'p-flatten-case "found side conditions, but don't support them here"))
a))
lhs)])
(let-values ([(r cl) (pattern->tex rhs)])
(let ([side-conditions
(if (null? cl)
#f
(format "(~a)" (string-join cl ", ")))])
(list
(cond
[side-conditions
(format
"~a \\llbracket ~a \\rrbracket & = &\n~a \\\\\n\\multicolumn{3}{l}{\\hbox to .2in{} ~a}\\\\\n"
(translate-mf-name name)
(apply string-append (add-between ", " (map (λ (l) (inline-tex l #f)) ls)))
(inline-tex r #f)
side-conditions)]
[else
(format
"~a \\llbracket ~a \\rrbracket & = &\n~a ~a\\\\\n"
(translate-mf-name name)
(apply string-append (add-between ", " (map (λ (l) (inline-tex l #f)) ls)))
(inline-tex r #f)
(if show-otherwise?
"~ ~ ~ ~ ~ ~ ~ \\mbox{\\textrm{(otherwise)}}"
""))]))))))
(define (p-case name lhs rhs show-otherwise?)
(let*-values ([(l l-cl) (pattern->tex lhs)]
[(r r-cl) (pattern->tex rhs)])
(let* ([cl (append l-cl r-cl)]
[side-conditions
(if (null? cl)
#f
(format "(~a)" (string-join cl ", ")))])
(list
(cond
[side-conditions
(format
"~a \\llbracket ~a \\rrbracket & = &\n~a \\\\\n\\multicolumn{3}{l}{\\hbox to .2in{} ~a}\\\\\n"
(translate-mf-name name)
(inline-tex l #f)
(inline-tex r #f)
side-conditions)]
[else
(format
"~a \\llbracket ~a \\rrbracket & = &\n~a ~a\\\\\n"
(translate-mf-name name)
(inline-tex l #f)
(inline-tex r #f)
(if show-otherwise?
"~ ~ ~ ~ ~ ~ ~ \\mbox{\\textrm{(otherwise)}}"
""))])))))
(define (p-rel-case name lhs rhs side-condition flatten-lhs?)
(let*-values ([(l) (map (λ (x) (let-values ([(a b) (pattern->tex x)])
(unless (null? b)
(error 'p-rel-case "found side conditions, but don't support them here"))
a))
(if flatten-lhs?
lhs
(list lhs)))]
[(r r-cl) (pattern->tex rhs)])
(unless (null? r-cl)
(error 'p-rel-case "don't use side-conditions in patterns for metafunction-as-relations"))
(let ([side-cond-tex
(and side-condition
(apply string-append
(add-between
"\\textrm{~and~}"
(format-side-cond side-condition))))]
[l-tex (apply string-append (add-between ", " (map (λ (x) (inline-tex x #f)) l)))])
(list
(cond
[(and side-cond-tex rhs)
(format
"~a \\llbracket ~a \\rrbracket & \\textrm{if} &\n~a \\textrm{~~and~~} ~a\\\\\n"
(translate-mf-name name)
l-tex
(inline-tex r #f)
side-cond-tex)]
[side-cond-tex
(format
"~a \\llbracket ~a \\rrbracket & \\textrm{if} &\n~a\\\\\n"
(translate-mf-name name)
l-tex
side-cond-tex)]
[rhs
(format
"~a \\llbracket ~a \\rrbracket & \\textrm{if} &\n~a\\\\\n"
(translate-mf-name name)
l-tex
(inline-tex r #f))]
[else
(format
"~a \\llbracket ~a \\rrbracket \\\\\n"
(translate-mf-name name)
l-tex)])))))
(define (p-twoline-case name lhs rhs show-otherwise?)
(let*-values ([(l l-cl) (pattern->tex lhs)]
[(r r-cl) (pattern->tex rhs)])
(let* ([cl (append l-cl r-cl)]
[side-conditions
(if (null? cl)
#f
(format "(~a)" (string-join cl ", ")))])
(list
(cond
[side-conditions
(format
"~a \\llbracket & ~a \\rrbracket = \\\\ \n& ~a \\\\\n\\multicolumn{2}{l}{\\hbox to .2in{} ~a}\\extrasptermn"
(translate-mf-name name)
(inline-tex l #f)
(inline-tex r #f)
side-conditions)]
[else
(format
"~a \\llbracket & ~a \\rrbracket = \\\\ \n& ~a ~a\\extraspterm\n"
(translate-mf-name name)
(inline-tex l #f)
(inline-tex r #f)
(if show-otherwise?
"~ ~ ~ ~ ~ ~ ~ \\mbox{\\textrm{(otherwise)}}"
""))])))))
(define TEX-NEWLINE "\\\\\n")
;; ============================================================
;; LANGUAGE NONTERMINAL PRINTING
;; ============================================================
(define (prod->string p)
(fmatch p
[`(hole multi) "\\holes"]
[`(hole single) "\\holeone"]
[`(error string) (format "\\textbf{error: } \\textit{error message}")]
[`(unknown string) (format "\\textbf{unknown: } \\textit{description}")]
[`(uncaught-exception v) (format "\\textbf{uncaught exception: } \\nt{v}")]
[_ (do-pretty-format p)]))
(define (show-rewritten-nt lhs str) (display (string-append lhs " " str " " TEX-NEWLINE)))
(define (typeset-production p)
(let* ([nonterm (car p)]
[lhs (format "~a & ::=~~~~ & " (format-nonterm nonterm))])
(when (memq nonterm nonterminals-break-before)
(display TEX-NEWLINE))
(case nonterm
[(ip) (show-rewritten-nt lhs "\\textrm{[immutable pair pointers]}")]
[(mp) (show-rewritten-nt lhs "\\textrm{[mutable pair pointers]}")]
[(sym) (show-rewritten-nt lhs "\\textrm{[variables except \\sy{dot}]}")]
[(x) (show-rewritten-nt lhs "\\textrm{[variables except \\sy{dot} and keywords]}")]
[(n) (show-rewritten-nt lhs "\\textrm{[numbers]}")]
[else
(let ([max-line-length
(case nonterm
[(e) 60]
[(es) 65]
[(a*) 90]
[(S) 75]
[(U) 90]
[else 70])])
(printf "~a & ::=~~~~& " (format-nonterm nonterm))
(let loop ([prods (map prod->string (cdr p))]
[sizes (map prod->size (cdr p))]
[line-length 0]
[needs-bar-before #f]
[beginning-of-line? #t])
(cond
[(null? prods)
(display "\\\\\n")]
[else
(let ([first (car prods)]
[new-length (+ (car sizes) line-length)])
(cond
[(or (<= new-length max-line-length)
(and (not (<= new-length max-line-length))
beginning-of-line?))
(when needs-bar-before
(display "~~\\mid~~"))
(display (car prods))
(loop (cdr prods)
(cdr sizes)
(+ (car sizes) line-length)
#t
#f)]
[else
(printf "\\\\\n")
(display "&\\mid~~~~&\n")
(loop prods sizes 0 #f #t)]))])))])))
(define (prod->size p) (string-length (format "~s" p)))
;; ============================================================
;; REDUCTION RULE PRINTING
;; ============================================================
(define (count-lines str)
(add1 (length (regexp-match* "\n." str))))
(define (p-rule arrow lwrap rwrap)
(opt-lambda (lhs rhs raw-name left-right? [extra-side-conditions '()])
(record-index-entries raw-name lhs)
;(record-index-entries raw-name rhs) ;; right-hand side has too much junk. Only index left-hand side stuff
(let*-values ([(l l-cl) (pattern->tex* (lwrap lhs) left-right?)]
[(r r-cl) (pattern->tex* (rwrap rhs) left-right?)])
(let* ([arrow-tex
(case arrow
((-->) "\\rightarrow")
((*->) "\\ovserset{\\rightarrow}{\ast}"))]
[name (format "\\rulename{~a}" (quote-tex-specials raw-name))]
[cl (append extra-side-conditions l-cl r-cl)]
[lbox-height (count-lines l)]
[rbox-height (count-lines r)]
[extra-inlined-arrow? #f]
[side-conditions
(if (null? cl)
#f
(format "(~a)" (string-join cl ", ")))])
(list
(cond
[(and side-conditions (= 2 rbox-height))
(let ([lines (regexp-split #rx"\n" r)])
(format
"\\threelinescruleB\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
(inline-tex l extra-inlined-arrow?)
(list-ref lines 0)
(list-ref lines 1)
(inline-tex name #f)
side-conditions
(if extra-inlined-arrow?
""
(inline-tex arrow-tex #f))))]
[(= 2 rbox-height)
(let ([lines (regexp-split #rx"\n" r)])
(format
"\\threelinescruleA\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
(inline-tex l extra-inlined-arrow?)
(list-ref lines 0)
(list-ref lines 1)
(inline-tex name #f)
(if extra-inlined-arrow?
""
(inline-tex arrow-tex #f))))]
[(= 3 rbox-height)
(unless side-conditions
(error 'tex-translate "three line rules without side-conditions aren't supported"))
(let ([lines (regexp-split #rx"\n" r)])
(format
"\\fourlinescruleB\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
(inline-tex l extra-inlined-arrow?)
(list-ref lines 0)
(list-ref lines 1)
(list-ref lines 2)
(inline-tex name #f)
side-conditions
(if extra-inlined-arrow?
""
(inline-tex arrow-tex #f))))]
[side-conditions
(format
(if left-right?
"\\onelinescruleA\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
(if (side-condition-below? raw-name)
"\\twolinescruleB\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
"\\twolinescruleA\n {~a}\n {~a}\n {~a}\n {~a}\n {~a}\n\n"))
(inline-tex l extra-inlined-arrow?)
(inline-tex r #f)
(inline-tex name #f)
side-conditions
(if extra-inlined-arrow?
""
(inline-tex arrow-tex #f)))]
[else
(format
(if left-right?
"\\onelineruleA\n {~a}\n {~a}\n {~a}\n {~a}\n\n"
"\\twolineruleA\n {~a}\n {~a}\n {~a}\n {~a}\n\n")
(inline-tex l extra-inlined-arrow?)
(inline-tex r #f)
(inline-tex name #f)
(if extra-inlined-arrow?
""
(inline-tex arrow-tex #f)))]))))))
(define (quote-tex-specials name)
(let ([ans (regexp-replace*
#rx"~"
(regexp-replace*
#rx"μ"
(regexp-replace*
#rx"[#]"
name
"\\\\#")
"\\\\ensuremath{\\\\mu}")
"\\\\~{}")])
; (fprintf (current-error-port) "~s -> ~s\n" name ans)
ans))
(define (inline-tex str arrow?)
(format "~a~a"
str
(if arrow? " \\rightarrow" "")))
;; add-newlines : nat str -> str
;; adds Scheme newlines to a string destined for a Scheme context
(define (add-newlines n str)
(let ((s (open-output-string)))
(display str s)
(let loop ((n n))
(cond
[(= n 0) (void)]
[else (display "\n--relax" s)
(loop (sub1 n))]))
(begin0
(get-output-string s)
(close-output-port s))))
(define (boxstr name code n arrow?)
(string-append
(format "\\newsavebox{\\~a}\n" name)
(format "\\sbox{\\~a}{%\n\\begin{schemebox}\n~a~a\\end{schemebox}}\n"
name
(add-newlines n code)
(if arrow? " \\rightarrow" ""))))
(define print-->reduction
(p-rule '--> (lambda (x) x) (lambda (x) x)))
(define print/->reduction
(p-rule '--> (lambda (x) `(in-hole* hole P_none ,x)) (lambda (x) `(in-hole* hole P_none ,x))))
(define print*->reduction
(p-rule '*-> (lambda (x) x) (lambda (x) x)))
(pretty-print-columns 50)
(define (p n) (fprintf (current-error-port) "~a\n" n))
(pretty-print-size-hook
(let ((ppsh (pretty-print-size-hook)))
(lambda (item display? port)
(fmatch item
[(and (? symbol?) (? (lambda (x) (regexp-match "(.*)_(.*)" (symbol->string x)))))
(let-values ([(_ name subscript) (apply values (regexp-match "(.*)_(.*)" (symbol->string item)))])
(+ (string-length name) (string-length subscript)))]
[`(mark ,v)
(+ (string-length (format "~a" v)) 1)]
[`(in-hole ,hole ,exp)
(string-length (format-hole hole exp))]
[`(in-hole* ,_ ,hole ,exp)
(string-length (format-hole hole exp))]
[(list 'comma x)
(fprintf (current-error-port) "WARNING: found comma ~s\n" item)
(ppsh item display? port)]
[_ (ppsh item display? port)]))))
(define (pretty-inf v port)
(parameterize ([pretty-print-columns 'infinity])
(pretty-print v port)))
(pretty-print-print-hook
(let ((ppph (pretty-print-print-hook)))
(lambda (item display? port)
(fmatch item
[(and (? symbol?) (? (lambda (x) (regexp-match #rx"(.*)_(.*)" (symbol->string x)))))
(let-values ([(_ name subscript) (apply values (regexp-match #rx"(.*)_(.*)" (symbol->string item)))])
(fprintf port "~a_{~a}" name subscript))]
[`(mark ,v)
(pretty-inf v port)
(display "\\umrk" port)]
[`(in-hole ,hole ,exp)
(display (format-hole hole exp) port)]
[`(in-hole* ,_ ,hole ,exp)
(display (format-hole hole exp) port)]
[else (ppph item display? port)]))))
(define (format-hole hole exp)
(parameterize ([pretty-print-columns 'infinity])
(if (memq exp '(hole hole-here))
(do-pretty-format hole)
(string-append (do-pretty-format hole)
"\\["
(do-pretty-format exp)
"\\]"))))
(define (do-pretty-format p)
(let ((o (open-output-string)))
(let loop ((p p))
(cond
((eq? '... p)
(display "\\cdots" o))
((eqv? #t p)
(display "\\semtrue{}" o))
((eqv? #f p)
(display "\\semfalse{}" o))
((symbol? p)
(display (format-symbol p) o))
((number? p)
(display p o))
((null? p)
(display "\\texttt{()}" o))
((string? p)
(display "\\textrm{``" o)
(display (quote-tex-specials p) o)
(display "''}" o))
((list? p)
(case (car p)
[(hole)
(case (list-ref p 1)
[(multi) (display "\\holes{}" o)]
[(single) (display "\\holeone{}" o)])]
[(quote)
(display "'" o)
(loop (cadr p))]
[(unquote)
(let ([unquoted (cadr p)])
(if (and (list? unquoted)
(= 3 (length unquoted))
(eq? (car unquoted) 'format))
(begin
(display "\\mbox{``" o)
(display (regexp-replace "~a" (list-ref unquoted 1) "") o)
(display "}" o)
(loop (list-ref unquoted 2))
(display "\\mbox{''}" o))
(loop unquoted)))]
((term)
(loop (cadr p)))
((r6rs-subst-one)
(let* ([args (cadr p)]
[var-arg (car args)]
[becomes-arg (cadr args)]
[exp-arg (caddr args)])
(display "\\{" o)
(loop var-arg)
(display "\\mapsto " o)
(loop becomes-arg)
(display "\\}" o)
(loop exp-arg)
(display "" o)))
((r6rs-subst-many)
(let* ([args (cadr p)]
[var-arg (caar args)]
[becomes-arg (cadar args)]
[dots (cadr args)]
[exp-arg (caddr args)])
(unless (eq? '... dots)
(error 'tex-translate.ss "cannot handle use of r6rs-subst-many"))
(display "\\{" o)
(loop var-arg)
(display " \\mapsto " o)
(loop becomes-arg)
(display "\\cdots \\}" o)
(loop exp-arg)
(display "" o)))
((in-hole)
(loop (cadr p))
(unless (memq (caddr p) '(hole hole-here))
(display "[" o)
(loop (caddr p))
(display "]" o)))
(else
(cond
[(memq (car p) flatten-args-metafunctions)
(display (translate-mf-name (car p)) o)
(display "\\llbracket{}" o)
(let ([lst (cadr p)])
(cond
[(null? lst) (void)]
[else
(loop (car lst))
(let i-loop ([lst (cdr lst)])
(cond
[(null? lst) (void)]
[else
(display ", " o)
(loop (car lst))
(i-loop (cdr lst))]))]))
(display "\\rrbracket" o)]
[(metafunction-name? (car p))
(display (translate-mf-name (car p)) o)
(display "\\llbracket{}" o)
(loop (cadr p))
(display "\\rrbracket" o)]
[else
(display "\\texttt{(}" o)
(loop (car p))
(for-each (lambda (el)
(display "~" o)
(loop el))
(cdr p))
(display "\\texttt{)}" o)]))))
((pair? p)
(display "\\texttt{(}" o)
(loop (car p))
(display " \\texttt{.} " o)
(loop (cdr p))
(display "\\texttt{)}" o))))
(close-output-port o)
(get-output-string o)))
(define (format-symbol op)
(let ([command (format "\\~a" (categorize-symbol op))]
[o (open-output-string)])
(parameterize ((current-output-port o))
(d-var op command))
(close-output-port o)
(get-output-string o)))
(define (categorize-symbol op)
(case op
[(language
name subst reduction reduction/context red term apply-values store dot consi throw push pop trim dw mark condition make-cond handlers
define beginF throw lambda set! if begin0 quote begin letrec letrec* reinit l! bh)
'sy]
[(cons unspecified null list dynamic-wind apply values null? pair? car cdr call/cc procedure? condition? unspecified? set-car! set-cdr! eqv? call-with-values with-exception-handler raise-continuable raise + * - /)
'va]
[else 'nt]))
;; pattern->tex : pattern -> (values string (listof string))
;; given a pattern, returns a string representation and some strings of side constraints
(define (pattern->tex pat)
(pattern->tex* pat #t))
(define (pattern->tex* pat left-to-right?)
(let ((side-conds (get-side-conditions pat))
(s (open-output-string)))
(parameterize ((current-output-port s))
(pattern->tex/int pat left-to-right?)
(values (get-output-string s)
(apply append (map format-side-cond side-conds))))))
(define (getpatstr pat lr?)
(let ((s (open-output-string)))
(parameterize ((current-output-port s))
(pattern->tex/int pat lr?)
(close-output-port s)
(get-output-string s))))
(define (getrhsstr rhs lr?)
(let ((s (open-output-string)))
(parameterize ((current-output-port s))
(rhs->tex/int rhs lr?)
(close-output-port s)
(get-output-string s))))
;; format-side-cond : side-condition -> (listof string[tex-code])
(define (format-side-cond sc)
(define (gps v) (getpatstr v #t))
(fmatch sc
[`(and ,@(list sc ...))
(apply append (map format-side-cond sc))]
[`(or ,sc ...)
(let* ([scs (map format-side-cond sc)]
[single-lines
(map (λ (x) (if (null? (cdr x))
(car x)
(apply string-append (add-between " \\textrm{ and }" x))))
scs)])
(let loop ([lst (cdr single-lines)]
[strs (list (car single-lines))])
(cond
[(null? lst) (list (apply string-append (reverse strs)))]
[else (loop (cdr lst)
(list* (car lst)
"\\textrm{ or }"
strs))])))]
[`(fresh ,xs)
(list (format "~a \\textrm{ fresh}" (string-join (map (lambda (x) (format "~a" (gps x))) xs) ", ")))]
[`(fresh ,(? symbol? x) ,'...)
(list (format "~a \\cdots \\textrm{ fresh}" (gps x)))]
[`(freshS ,xs)
(list (format "~a \\cdots \\textrm{ fresh}" (string-join (map (lambda (x) (format "~a" (gps x))) xs) ", ")))]
[`(not (,(? (λ (x) (memq x '(equal? eq?)))) (term ,t1) (term ,t2)))
(list (format "~a \\neq ~a" (gps t1) (gps t2)))]
[`(,(? (λ (x) (memq x '(equal? eq?)))) (term ,t1) (term ,t2))
(list (format "~a = ~a" (gps t1) (gps t2)))]
[`(not (eq? (term ,t1) #f))
(list (format "~a \\neq \\semfalse{}" (gps t1)))]
[`(not (null-v? (term ,v)))
(list (format "~a \\neq \\va{null}" (gps v)))]
[`(not (prefixed-by? (term ,v) (quote p:)))
(list (format "~a \\not\\in \\nt{pp}" (gps v)))]
[`(not (condition? (term ,v)))
(list (format "~a \\neq (\\sy{make\\mbox{\\texttt{-}}cond}~~\\nt{string})" (gps v)))]
[`(not (lambda-null? (term ,v)))
(list (format "~a \\neq \\texttt{(lambda}~~\\texttt{()}~~\\nt{e}\\texttt{)}" v))]
[`(not (,(? (λ (x)
(and (symbol? x)
(regexp-match #rx"\\?$" (symbol->string x))))
sym)
(term ,v)))
(list (format "~a \\not\\in \\nt{~a}"
(gps v)
(regexp-replace ".$" (symbol->string sym) "")))]
[`(= (length (term (,arg1 ,dots))) (length (term (,arg2 ,dots))))
(list (arglen-str "=" arg1 arg2))]
[`(not (= (length (term (,arg1 ,dots))) (length (term (,arg2 ,dots)))))
(list (arglen-str "\\neq" arg1 arg2))]
[`(< (length (term (,arg1 ,dots))) (length (term (,arg2 ,dots))))
(list (arglen-str "<" arg1 arg2))]
[`(< (length (term (,arg1 ,dots))) (length (term (,arg2 ,arg3 ,dots))))
(list (string-append (arglen-str "<" arg1 arg3) " + 1"))]
[`(>= (length (term (,arg1 ,dots))) (length (term (,arg2 ,dots))))
(list (arglen-str "\\geq" arg1 arg2))]
[`(not (= (length (term (,v ,_))) ,(? number? n)))
(list (format "\\#~a \\neq ~a" (gps v) n))]
[`(not (list-v? (term ,v_last)))
(list (format "~a \\not\\in \\nt{pp}" (gps v_last))