summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/drv/sample/drv.mp
blob: 5eb38396144c0608b54e8b6eda797e4de4389b8c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
%% drv.mp 0.93
%% Copyright 2009 Laurent M\'ehats
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Laurent M\'ehats.
% <laurent.mehats@gmail.com>
%
% This work consists of the file drv.mp


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% User's macros summary
%
%
% * Judgment & inference declarations
% ===================================
%
%   jgm <nat> <str list>
%	<nat>		judgment index
%	<str list>	sub-judgments math-mode LaTeX code
%
%   nfr <nat> (<nat list>) (<str>, <id>)
%	<nat>		inference index
%	<nat list>	list of premise indices
%	<str>		inference label math-mode LaTeX code
%	<id>		inference line style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%			  0	none
%			  1	simple
%			  2	double
%			  3	dotted
%			  4	dashed
%			  5	waved
%			  6	TeX-dotted
%			  _	default (set by drv_path_style)
%
%   mvd <nat 1> (<nat 2>, <id>)
%	<nat 1>		index of the origin judgment
%	<nat 2>		number of phantom steps
%	<id>		phantom steps path-style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   Nfr <nat> (<nat list>) (<str 1>, <str 2>, <str 3>, <id>)
%	<nat>		inference index
%	<nat list>	list of premise indices
%	<str 1>		inference label math-mode LaTeX code
%	<str 2>		left delimiter label math-mode LaTeX code
%	<str 3>		right delimiter label math-mode LaTeX code
%	<id>		inference line style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   Mvd <nat 1> (<nat 2>, <str 1>, <str 2>, <id>)
%	<nat 1>		index of the origin judgment
%	<nat 2>		number of phantom steps
%	<str 1>		left label math-mode LaTeX code
%	<str 2>		right label math-mode LaTeX code
%	<id>		phantom steps path-style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   NFR <nat> (<nat list>) (<str 1>, <str 2>, <str 3>, <str 4>)
%     (<id 1>, <id 2>, <id 3>)
%	<nat>		inference index
%	<nat list>	list of premise indices
%	<str 1>		left inference label math-mode LaTeX code
%	<str 2>		right inference label math-mode LaTeX code
%	<str 3>		left delimiter label math-mode LaTeX code
%	<str 4>		right delimiter label math-mode LaTeX code
%	<id 1>		junction style identifier (0, 1, 2, 3 or _)
%			  0	fully-interlacing
%			  1	semi-interlacing
%			  2	non-interlacing
%			  3	user specified (tricky)
%			  _	default (set by drv_junction_style)
%	<id 2>		alignment style identifier (l, c, r, u or _)
%			  l	left
%			  c	centered
%			  r	right
%			  u	user specified (tricky)
%			  _	default (set by drv_alignment_style)
%	<id 3>		inference line style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   MVD <nat 1> (<nat 2>, <str 1>, <str 2>, <id 1> <id 2>)
%	<nat 1>		index of the origin judgment
%	<nat 2>		number of phantom steps
%	<str 1>		left label math-mode LaTeX code
%	<str 2>		right label math-mode LaTeX code
%	<id 1>		alignment style identifier (l, c, r, u or _)
%	<id 2>		phantom steps path-style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   NFR_opt <nat> (<nat list>) (<str list 1>) (<str list 2>)
%     (<id 1>, <id 2>, <id 3>)
%	<nat>		inference index
%	<nat list>	list of premise indices
%	<str list 1>	list of inference labels math-mode LaTeX code
%	<str list 2>	list of delimiter labels math-mode LaTeX code
%	<id 1>		junction style identifier (0, 1, 2, 3 or _)
%	<id 2>		alignment style identifier (l, c, r, u or _)
%	<id 3>		inference line style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
%   MVD_opt <nat 1> (<nat 2>) (<str list>) (<id 1> <id 2>)
%	<nat 1>		index of the origin judgment
%	<nat 2>		number of phantom steps
%	<str list>	list of labels math-mode LaTeX code
%	<id 1>		alignment style identifier (l, c, r or u)
%	<id 2>		phantom steps path-style identifier
%			(0, 1, 2, 3, 4, 5, 6 or _)
%
% * Tunings
% =========
%
%   drv_font_size <str>
%	<str>		LaTeX font-size command
%			  "\tiny"
%			  "\scriptsize"
%			  "\footnotesize"
%			  "\small"
%			  "\normalsize"
%			  "\large"
%			  "\Large"
%			  etc.
%
%   drv_math_style (<id>, <str>)
%	<id>		component identifier (drv, jdg, ilb, dlb or plb)
%			  drv	derivation trees
%			  jdg	judgments
%			  ilb	inference labels
%			  dlb	delimiter labels
%			  plb	phantom steps labels
%	<str>		LaTeX math-style command
%
%   drv_scale (<id>, <float>)
%	<id>		scale identifier (clr, prm, jdg or ilb)
%			  clr	nice explanation soon (see examples)
%			  prm	nice explanation soon (see examples)
%			  jdg	nice explanation soon (see examples)
%			  ilb	nice explanation soon (see examples)
%	<float>		scale value
%
%   drv_junction_style <id>
%	<id>		junction style identifier (0, 1 or 2)
%			  0	``fully-interlacing''
%			  1	``semi-interlacing''
%			  2	``non-interlacing''
%
%   drv_alignment_style <id>
%	<id>		alignment style identifier (l, c or r)
%			  l	left
%			  c	centered
%			  r	right
%
%   drv_path_style (<id 1>, <id 2>)
%	<id 1>		path-type identifier (iln or phm)
%			  iln	inference lines
%			  phm	phantom steps paths
%	<id 2>		path-style identifier (0, 1, 2, 3, 4, 5 or 6)
%
%   drv_labels_position (<id 1>, <id 2>)
%	<id 1>		label-type identifier (ilb, plb or dlb)
%			  ilb	inference labels
%			  dlb	delimiter labels
%			  plb	phantom steps labels
%	<id 2>		position identifier (l or r)
%			  l	left
%			  r	right
%
%   drv_roots_position <id>
%	<id>		position identifier (t or b)
%			  t	top
%			  b	bottom
%
%   drv_axis_reference <id>
%	<id>		reference identifier (iln or jdg)
%			  iln	root inference line axis
%			  jdg	root judgment math-axis
%
%   drv_left_delimiter <str>
%	<str>		left delimiter math-mode LaTeX code
%			  "("	$($
%			  "["	$[$
%			  "\lbrace"	$\lbrace$
%			  "\langle"	$\langle$
%			  etc.
%
%   drv_right_delimiter <str>
%	<str>		right delimiter math-mode LaTeX code
%			  ")"
%			  "]"
%			  "\rbrace"
%			  "\rangle"
%			  etc.
%
%   drv_box_mode <id>
%	<id>		status identifier (on or off)
%			  on
%			  off
%
%   drv_fraction_mode <id>
%	<id>		status identifier (on or off)
%			  on
%			  off
%
%   drv_proof_mode <id>
%	<id>		status identifier (on or off)
%			  on
%			  off
%
% * Derivation tree pictures
% ==========================
%
%   drv_bbox <nat>
%	<nat>		sub-tree root index
%
%   drv_axis (<id>, <nat>)
%	<id>		reference type identifier (iln, jdg or dlm)
%			  iln	inference line axis
%			  jdg	judgment math-axis
%			  dlm	delimiter axis
%	<nat>		reference index
%
%   <path> drv_styled <id>
%	<path>		MetaPost path expression
%	<id>		path style identifier (0, 1, 2, 3, 4, 5 or 6)
%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MetaPost messages formatting
%

% drv_format prevents messages from being automatically cut into lines every
% 79 characters: if possible, a word which would otherwise be cut is 'pushed'
% to the following line by inserting space characters before it.

vardef drv_format (expr str, fst_len, nxt_len, dbl_pcs)=
  % str: the message to be formatted
  % fst_len: first line length
  % nxt_len: following lines length
  % dbl_pcs (boolean): occurrences of the percent character must be doubled
  save str_len, sub_len, aux_len, chr_rnk, occ_num;
  numeric str_len, sub_len, aux_len, chr_rnk, occ_num;
  save aux_str, chr;
  string aux_str, chr;
  str_len:=length str;
  sub_len:=0;
  chr_rnk:=0;
  occ_num:=0;
  aux_len:=0;
  aux_str:="";
  forever:
    exitif (chr_rnk=fst_len) or (chr_rnk=str_len);
    chr:=substring (chr_rnk, chr_rnk+1) of str;
    aux_str:=aux_str&chr;
    if chr=" ":
      sub_len:=chr_rnk+1;
      aux_len:=sub_len+occ_num;
    elseif (chr="%") and dbl_pcs:
      aux_str:=aux_str&"%";
      occ_num:=occ_num+1;
    fi
    chr_rnk:=chr_rnk+1;
  endfor
  if chr_rnk=fst_len:
    begingroup
      if sub_len=0:
        sub_len:=fst_len;
        aux_len:=sub_len+occ_num;
      fi
      substring (0, aux_len) of aux_str&
        for idx=sub_len+1 upto fst_len:
          " "&
        endfor
        drv_format (substring (sub_len, str_len) of str,
          nxt_len, nxt_len, dbl_pcs)
    endgroup
  else:
    aux_str
  fi
enddef;

boolean drv_inside_fig;
drv_inside_fig:=false;

vardef drv_context=
  "drv"&if drv_inside_fig: " (fig. "&decimal charcode&")"&fi ": "
enddef;

vardef drv_message expr str=
  message drv_format (drv_context&str, 79, 79, false);
enddef;

vardef drv_errhelp expr str=
  % Occurrences of the percent character must be doubled: errhelp makes single
  % single occurrences behave as newline characters (this is inherited from
  % MetaFont).
  errhelp drv_format (str, 79, 79, true);
enddef;

vardef drv_errmessage expr str=
  % errmessage inserts the 2-characters string  "! " before its argument.
  % Hence 77 as a first line length argument.
  errmessage drv_format (drv_context&str, 77, 79, false);
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Delayed LaTeX processing
%

picture tex_pct[];
string delayed_tex_file;
delayed_tex_file:=jobname&"-delayed.mp";

%
% reading LaTeX picture definitions
%

boolean drv_nullpicture;
drv_nullpicture:=true;

string readfrom_str;
readfrom_str:=readfrom delayed_tex_file;
if readfrom_str<>EOF:
  closefrom delayed_tex_file;
  drv_message ("reading `"&delayed_tex_file&"' LateX picture definitions.");
  batchmode;
  scantokens ("input "&delayed_tex_file);
  errorstopmode;
  drv_message "done.";
  drv_nullpicture:=false;
fi

%
% copying LaTeX preamble
%

vardef drv_read (expr search_str) (suffix accum_str)=
  save readfrom_str, readfrom_substr;
  string readfrom_str, readfrom_substr;
  accum_str:="";
  forever:
    readfrom_str:=readfrom jobname;
    readfrom_substr:=substring (0, length search_str) of readfrom_str;
    exitif (readfrom_substr=search_str) or (readfrom_str=EOF);
    accum_str:=accum_str&readfrom_str&char(10);
  endfor
  if readfrom_str=EOF:
    closefrom jobname;
    drv_errhelp ("`"&jobname&".mp' should contain a line starting with "&
      ditto&search_str&ditto&".");
    drv_errmessage (ditto&search_str&ditto&" is missing");
  fi
enddef;

string tex_preamble_str[];
% tex_preamble_str[0] from "verbatimtex %&latex" to "\begin{document}"
% tex_preamble_str[1] from "\begin{document}" to "etex;"

drv_message ("reading `"&jobname&".mp' LateX preamble.");
  drv_read ("verbatimtex %&latex", tex_preamble_str[0]);
  drv_read ("\begin{document}", tex_preamble_str[0]);
  drv_read ("etex;", tex_preamble_str[1]);
  closefrom jobname;
  write
    "%"&char(10)&
    "% AUTOMATICALLY GENERATED BY DRV.MP - DO NOT MODIFY."&char(10)&
    "%"&char(10)&char(10)&
    "verbatimtex %&latex"&char(10)&
    tex_preamble_str[0]&
    "\begin{document}"&char(10)&
    tex_preamble_str[1]&
    "etex;"
  to delayed_tex_file;
drv_message "done.";

%
% writing LaTeX picture definitions
%

numeric tex_pct_idx;
tex_pct_idx:=0;

% Math style indices: 0, 1, 2, 3
string mth_sty_str[];
mth_sty_str[0]:="\displaystyle";
mth_sty_str[1]:="\textstyle";
mth_sty_str[2]:="\scriptstyle";
mth_sty_str[3]:="\scriptscriptstyle";

string drv_fontsize;

vardef drv_tex[] expr tex_str=
  % @: math style index
  % tex_str: math mode LaTeX code
  save def_str, pct;
  string def_str;
  picture pct;
  def_str:="btex{"&drv_fontsize&"$"&mth_sty_str[@]&"{}"&tex_str&"{}$}etex;";
  write "tex_pct["&(decimal tex_pct_idx)&"]:="&def_str to delayed_tex_file;
  pct:=
    if known tex_pct[tex_pct_idx]:
      tex_pct[tex_pct_idx]
    else:
      nullpicture
    fi;
  tex_pct_idx:=tex_pct_idx+1;
  pct
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% MetaPost shorthands
%

vardef depth expr pct=
  ypart llcorner pct
enddef;

vardef height expr pct=
  ypart urcorner pct
enddef;

vardef width expr pct=
  xpart urcorner pct-xpart llcorner pct
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Font-specific parameters
%

%
% em[s]		width of an m (s: mathstyle index)
% ex[s]		height of an x
% axis_hg[s]	height of the mathematical axis
% num_bl_hg[s]	height of a numerator baseline w.r.t. the fraction bar axis
% den_bl_dp[s]	depth of a denominator baseline w.r.t. the fraction bar axis
% rul_thk[s]	default rule thickness
%

numeric em[], ex[], axis_hg[], num_bl_hg[], den_bl_dp[], rul_thk[];

%
% thn_sp[s]	thin space, usually 1em/6
% med_sp[s]	medium space, usually 2em/9
% thk_sp[s]	thick space, usually 5em/18
%

numeric thn_sp[], med_sp[], thk_sp[];

%
% pdt_hg[s]	height of a punctuation ldot
% pdt_wd[s]	width of a punctuation ldot
%

numeric pdt_hg[], pdt_wd[];

%
% tt_dgt[s][i]	typewriter typeface digits (i ranges from 0 to 9)
%

picture tt_dgt[][];

%
% min_clr[s]	minimal clearance between a numerator
%		or a denominator and the fraction bar
% prm_skp[s]	<nice explanation>
% jdg_skp[s]	<nice explanation>
% ilb_skp[s]	<nice explanation>
% plb_skp[s]	<nice explanation>
%

numeric min_clr[], prm_skp[], jdg_skp[], ilb_skp[], plb_skp[];

vardef drv_fontdimen[] expr font=
  save dim_pct;
  picture dim_pct;
  dim_pct:=drv_tex[0] ("\rule{\the\fontdimen"&(decimal @)&font&"}{0bp}");
  width dim_pct
enddef;

vardef drv_font_size expr size_str=
  save pdt_pct, sp_pct;
  picture pdt_pct, sp_pct;
  drv_fontsize:=size_str;
  write char(10)&"% font-specific parameters ("&drv_fontsize&")"&char(10)
    to delayed_tex_file;
  ex[0]:=drv_fontdimen[5] "\textfont2";
  ex[1]:=drv_fontdimen[5] "\textfont2";
  ex[2]:=drv_fontdimen[5] "\scriptfont2";
  ex[3]:=drv_fontdimen[5] "\scriptscriptfont2";
  em[0]:=drv_fontdimen[6] "\textfont2";
  em[1]:=drv_fontdimen[6] "\textfont2";
  em[2]:=drv_fontdimen[6] "\scriptfont2";
  em[3]:=drv_fontdimen[6] "\scriptscriptfont2";
  rul_thk[0]:=drv_fontdimen[8] "\textfont3";
  rul_thk[1]:=drv_fontdimen[8] "\textfont3";
  rul_thk[2]:=drv_fontdimen[8] "\scriptfont3";
  rul_thk[3]:=drv_fontdimen[8] "\scriptscriptfont3";
  axis_hg[0]:=drv_fontdimen[22] "\textfont2";
  axis_hg[1]:=drv_fontdimen[22] "\textfont2";
  axis_hg[2]:=drv_fontdimen[22] "\scriptfont2";
  axis_hg[3]:=drv_fontdimen[22] "\scriptscriptfont2";
  num_bl_hg[0]:=(drv_fontdimen[8] "\textfont2")-axis_hg[0];
  num_bl_hg[1]:=(drv_fontdimen[9] "\textfont2")-axis_hg[1];
  num_bl_hg[2]:=(drv_fontdimen[9] "\scriptfont2")-axis_hg[2];
  num_bl_hg[3]:=(drv_fontdimen[9] "\scriptscriptfont2")-axis_hg[3];
  den_bl_dp[0]:=-(drv_fontdimen[11] "\textfont2")-axis_hg[0];
  den_bl_dp[1]:=-(drv_fontdimen[12] "\textfont2")-axis_hg[1];
  den_bl_dp[2]:=-(drv_fontdimen[12] "\scriptfont2")-axis_hg[2];
  den_bl_dp[3]:=-(drv_fontdimen[12] "\scriptscriptfont2")-axis_hg[3];
  min_clr[0]:=3*rul_thk[0]; % The TEXbook,
  min_clr[1]:=1*rul_thk[1]; % Appendix G,
  min_clr[2]:=1*rul_thk[2]; % Rule 15d.
  min_clr[3]:=1*rul_thk[3]; %
  for mth_sty:=0 upto 3:
    sp_pct:=drv_tex[mth_sty] "\,";
    thn_sp[mth_sty]:=width sp_pct;
    sp_pct:=drv_tex[mth_sty] "\>";
    med_sp[mth_sty]:=width sp_pct;
    sp_pct:=drv_tex[mth_sty] "\;";
    thk_sp[mth_sty]:=width sp_pct;
    pdt_pct:=drv_tex[mth_sty] "\ldotp";
    pdt_hg[mth_sty]:=height pdt_pct;
    pdt_wd[mth_sty]:=width pdt_pct;
    for dgt=0 upto 9:
      tt_dgt[mth_sty][dgt]:=drv_tex[mth_sty] "\texttt{"&(decimal dgt)&"}";
    endfor
    prm_skp[mth_sty]:=3*med_sp[mth_sty]; % arbitrary
    jdg_skp[mth_sty]:=thn_sp[mth_sty];   % arbitrary
    ilb_skp[mth_sty]:=thn_sp[mth_sty];   % arbitrary
    plb_skp[mth_sty]:=thk_sp[mth_sty];   % arbitrary
  endfor
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Math-related parameters & macros
%

%
% Math styles
% drv_sty		derivation trees
% jdg_sty[drv_sty]	judgments
% ilb_sty[drv_sty]	inference labels
% dlb_sty[drv_sty]	delimiter labels
% plb_sty[drv_sty]	phantom steps labels
%

numeric drv_sty, jdg_sty[], ilb_sty[], dlb_sty[], plb_sty[];

%
% Math-axis centered LaTeX pictures
%

vardef mth_tex[] expr tex_str=
  (drv_tex[@] tex_str) shifted (0, -axis_hg[@])
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% drv-boxes
%

vardef drv_box text STR=
  for str=STR:
    expandafter picture scantokens str;
    expandafter pair scantokens str.l;
    expandafter pair scantokens str.c;
    expandafter pair scantokens str.r;
    expandafter numeric scantokens str.hg;
    expandafter numeric scantokens str.dp;
  endfor
enddef;

vardef box_init @# (expr pct)=
  @#:=pct;
  @#.r-@#.l=(width pct, 0);
  @#.c=(@#.l+@#.r)/2;
  @#.hg:=height pct;
  @#.dp:=depth pct;
enddef;

vardef box_freeze @#=
  @#:=@# shifted @#.l;
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% beginfig & endfig
%

%
% drv_beginfig: figure initialization
%

pen drv_pen;

vardef drv_beginfig=
  %
  % Judgments
  %
  % jdg[i]	judgment drv-box
  % sbj_num[i]	number of sub-judgments
  % sbj[i][j]	sub-judgment drv-box (j ranges from 0 to sbj_num[i]-1)
  %
  drv_box "jdg[]";
  numeric sbj_num[];
  drv_box "sbj[][]";
  %
  % Inferences
  %
  % prm_num[i]	number of premises
  % prm[i][j]	premise index (j ranges from 0 to prm_num[i]-1)
  % ccl[i]	conclusion index
  % jct_sty[i]	junction style
  % alg_sty[i]	alignment style
  % iln_sty[i]	inference line style
  % l_prm[i]	leftmost `graphical' premise index
  % r_prm[i]	rightmost `graphical' premise index
  %
  numeric prm_num[], prm[][], ccl[];
  numeric jct_sty[];
  string alg_sty[];
  numeric iln_sty[];
  numeric l_prm[], r_prm[];
  %
  % imin_x[i]	inference minimal x
  % l_dlb[i]	left delimiter label drv-box
  % l_dlm[i]	left delimiter drv-box
  % l_ilb[i]	left label drv-box
  % iln[i]	inference line drv-box
  % r_ilb[i]	right label drv-box
  % r_dlm[i]	right delimiter drv-box
  % r_dlb[i]	right delimiter label drv-box
  % imax_x[i]	inference maximal x
  %
  numeric imin_x[];
  drv_box "l_dlb[]", "l_dlm[]", "l_ilb[]";
  drv_box "iln[]";
  drv_box "r_ilb[]", "r_dlm[]", "r_dlb[]";
  numeric imax_x[];
  %
  % Sub-trees
  %
  % smin_x[i]	sub-tree minimal x
  % smax_x[i]	sub-tree maximal x
  % smin_y[i]	sub-tree minimal y
  % smax_y[i]	sub-tree maximal y
  % boxed[i]	boolean
  %
  numeric smin_x[], smax_x[], smin_y[], smax_y[];
  boolean boxed[];
  %
  % Pen initialization
  %
  drv_pen:=pencircle scaled rul_thk[drv_sty];
  %
  % num_hg	height of a numerator axis w.r.t. the fraction bar axis
  % den_dp	depth of a denominator axis w.r.t. the fraction bar axis
  % iln_hg[s]	height of a style s inference line
  % iln_dp[s]	depth of a style s inference line
  %
  numeric num_hg, den_dp, iln_hg[], iln_dp[];
  num_hg:=num_bl_hg[drv_sty]+axis_hg[jdg_sty[drv_sty]];
  den_dp:=den_bl_dp[drv_sty]+axis_hg[jdg_sty[drv_sty]];
  picture iln_pct;
  for sty=0 upto 6:
    iln_pct:=((0, 0)--(10, 0)) drv_styled sty;
    iln_hg[sty]:=height iln_pct;
    iln_dp[sty]:=depth iln_pct;
  endfor
  %
  % Derivation trees
  %
  % root_num	number of roots
  % root[i]	root index (i ranges from 0 to root_num-1)
  % drv_built	boolean
  % drv_frozen	boolean
  %
  numeric root_num, root[];
  boolean drv_checked, drv_built, drv_frozen;
  root_num:=0;
  drv_checked:=false;
  drv_built:=false;
  drv_frozen:=false;
  %
  % Phantom steps
  %
  % phm_idx	phantom inferences index
  % src[i]	base phantom judgment index (in case of an origin judgment)
  % tgt[i]	origin judgment index (in case of a base phantom judgment)
  % phm_sty[i]	phantom steps style
  % l_plb[i]	left phantom steps label drv-box (in case of a base
  %		phantom judgment)
  % phm[i]	phantom steps drv-box (in case of a base phantom
  %		judgment)
  % r_plb[i]	right phantom steps label drv-box (in case of a base
  %		phantom judgment)
  %
  numeric phm_idx, src[], tgt[], phm_sty[];
  phm_idx:=0;
  drv_box "l_plb[]", "phm[]", "r_plb[]";
  %
  % Proof pictures
  %
  % jdg_prf[i]		judgment index drv-box
  % sbj_prf[i][j]	sub-judgment index drv-box (j ranges from 0 to
  %			sbj_num[i]-1)
  %
  drv_box "jdg_prf[]", "sbj_prf[][]";
  %
  % Info
  %
  write char(10)&"% fig. "&(decimal charcode)&char(10) to delayed_tex_file;
  drv_inside_fig:=true; % context: inside a figure
enddef;

extra_beginfig:=extra_beginfig&"drv_beginfig;";

%
% drv_endfig: proof-file generation
%

string proof_file;
proof_file:=jobname&"-proof.tex";

% TODO: taking outputtemplate into account.
vardef output_file[]=
  jobname&"."&(decimal @)
enddef;

string buf_str;
buf_str:=
  "%"&char(10)&
  "% AUTOMATICALLY GENERATED BY DRV.MP."&char(10)&
  "%"&char(10)&char(10)&
  tex_preamble_str[0]&
  "\usepackage{graphicx}"&char(10)&
  "\makeatletter"&char(10)&
  "\def\Gin@def@bp#1\relax#2#3{\gdef#2{#3}}"&char(10)&
  "\newsavebox{\graphicsbox}"&char(10)&
  "\newcommand*{\drv}[1]{%"&char(10)&
  "\sbox{\graphicsbox}{\includegraphics{#1}}%"&char(10)&
  "\raisebox{\Gin@lly bp}%"&char(10)&
  "{\usebox{\graphicsbox}}}"&char(10)&
  "\makeatother"&char(10)&char(10)&
  "\newcount\h"&char(10)&
  "\newcount\m"&char(10)&
  "\h=\time"&char(10)&
  "\divide\h 60"&char(10)&
  "\m=-\h"&char(10)&
  "\multiply\m 60"&char(10)&
  "\advance\m\time"&char(10)&
  "\def\hm{\number\h:\ifnum\m<10{}0\fi\number\m}"&char(10)&char(10)&
  "\newcommand{\mathaxis}{\frac{\quad}{}}"&char(10)&char(10)&
  "\begin{document}"&char(10)&
  tex_preamble_str[1]&
  "\noindent\today\ at \hm\medskip"&char(10)&char(10)&"\noindent";

vardef drv_endfig=
  drv_inside_fig:=false;   % context: outside a figure
  buf_str:=buf_str&char(10)&
  "\texttt{"&output_file[charcode]&"}"&char(10)&
  "  (\verb+"&drv_fontsize&"+, \verb+"&(mth_sty_str[drv_sty])&"+)"&char(10)&
  "    {"&drv_fontsize&char(10)&
  "      \["&(mth_sty_str[drv_sty])&char(10)&
  "        \mathaxis\drv{"&output_file[charcode]&"}\mathaxis"&char(10)&
  "      \]}\\";
  write EOF to proof_file; % erases the previous content
  write buf_str&char(10)&"\end{document}" to proof_file;
  write EOF to proof_file;
  if drv_nullpicture:
    draw btex{\tt drv}: run {\tt mpost} again.etex withcolor (1, 0, 0);
  fi
enddef;

extra_endfig:="drv_endfig;"&extra_endfig;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Judgment declarations
%

vardef jgm[] text SUB=
  % @: judgment index
  % SUB: list of `sub-judgments' (pictures or strings)
  if @<0:
    drv_errhelp "An judgment index can not be negative.";
    drv_errmessage (decimal @)&" is negative";
  elseif known jdg[@]:
    drv_errhelp "An index can be used only once to declare a judgment.";
    drv_errmessage (decimal @)&" has been used already as a judgment index";
  else:
    save jdg_pct, sbj_pct, jdg_wd, sbj_rnk;
    picture jdg_pct, sbj_pct;
    numeric jdg_wd, sbj_rnk;
    jdg_pct:=nullpicture;
    jdg_wd:=0;
    sbj_rnk:=0;
    for sub=SUB:
      sbj_pct:=
        if picture sub:
          sub
        elseif sub<>"":
          mth_tex[jdg_sty[drv_sty]] sub
        else:
          nullpicture
        fi;
      box_init sbj[@][sbj_rnk] (sbj_pct);
      if sbj_rnk>0:
        sbj[@][sbj_rnk-1].r=sbj[@][sbj_rnk].l;
      fi
      addto jdg_pct also sbj_pct shifted (jdg_wd, 0);
      jdg_wd:=width jdg_pct;
      sbj_rnk:=sbj_rnk+1;
    endfor
    sbj_num[@]:=sbj_rnk;
    box_init jdg[@] (jdg_pct);
    if sbj_num[@]>0:
      jdg[@].l=sbj[@][0].l;
      jdg[@].r=sbj[@][sbj_num[@]-1].r;
    fi
  fi
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Inference declarations
%

vardef check_junction_style (suffix sty_sfx) (expr default)=
  save sty_str;
  string sty_str;
  sty_str:=str sty_sfx;
  if (sty_str="0")
  or (sty_str="1")
  or (sty_str="2")
  or (sty_str="3"):
    sty_sfx
  elseif (sty_str="_"):
    default
  else:
    begingroup
      drv_errhelp "Valid identifiers are"&
        " 0 (fully-interlacing),"&
        " 1 (semi-interlacing),"&
        " 2 (non-interlacing),"&
        " 3 (user specified) and"&
        " _ (default).";
      drv_errmessage sty_str&" is not a valid junction style identifier";
      default
    endgroup
  fi
enddef;

vardef check_alignment_style (suffix sty_sfx) (expr default)=
  save sty_str;
  string sty_str;
  sty_str:=str sty_sfx;
  if (sty_str="l")
  or (sty_str="c")
  or (sty_str="r")
  or (sty_str="u"):
    sty_str
  elseif (sty_str="_"):
    default
  else:
    begingroup
      drv_errhelp "Valid identifiers are"&
        " l (none),"&
        " c (simple),"&
        " r (double),"&
        " u (dotted) and"&
        " _ (default).";
      drv_errmessage sty_str&" is not a valid alignment style identifier";
      default
    endgroup
  fi
enddef;

vardef check_path_style (suffix sty_sfx) (expr default)=
  save sty_str;
  string sty_str;
  sty_str:=str sty_sfx;
  if (sty_str="0")
  or (sty_str="1")
  or (sty_str="2")
  or (sty_str="3")
  or (sty_str="4")
  or (sty_str="5")
  or (sty_str="6"):
    sty_sfx
  elseif (sty_str="_"):
    default
  else:
    begingroup
      drv_errhelp "Valid identifiers are"&
        " 0 (none),"&
        " 1 (simple),"&
        " 2 (double),"&
        " 3 (dotted),"&
        " 4 (dashed),"&
        " 5 (waved),"&
        " 6 (TeX-dotted) and"&
        " _ (default).";
      drv_errmessage sty_str&" is not a valid path style identifier";
      default
    endgroup
  fi
enddef;

vardef opt_init[] @# expr arg=
  if picture arg:
    if arg<>nullpicture:
      box_init @# (arg);
    fi
  elseif arg<>"":
    box_init @# (mth_tex[@] arg);
  fi
enddef;

vardef NFR[] (text PRM) (expr lilb, rilb, ldlb, rdlb)
  (suffix j_sty, a_sty, i_sty)=
  %
  % @: inference index
  %
  if known prm_num[@]:
    drv_errhelp "An index can be used only once to declare an inference.";
    drv_errmessage (decimal @)&" has been used already as an inference index";
  else:
    %
    % PRM: list of premise indices
    %
    save prm_rnk;
    numeric prm_rnk;
    prm_rnk:=0;
    for prm_idx=PRM:
      if prm_idx=@:
        drv_errhelp "An inference index can not be used"&
          " as one of its own premise indices.";
        drv_errmessage "the inference index "&(decimal prm_idx)&
          " is used as one of its own premise indices";
      elseif known ccl[prm_idx]:
        drv_errhelp "An index can be used only once as a premise index.";
        drv_errmessage (decimal prm_idx)&" has been used already as a"&
          " premise index for inference declaration "&(decimal ccl[prm_idx]);
      else:
        if known prm_num[prm_idx]:
          %
          % roots list update
          %
          save root_idx;
          numeric root_idx;
          root_idx:=0;
          forever:
            exitif root[root_idx]=prm_idx;
            root_idx:=root_idx+1;
          endfor
          forever:
            exitif root_idx=root_num-1;
            root[root_idx]:=root[root_idx+1];
            root_idx:=root_idx+1;
          endfor
          root_num:=root_num-1;
        fi
        ccl[prm_idx]:=@;
        prm[@][prm_rnk]:=prm_idx;
        prm_rnk:=prm_rnk+1;
      fi
    endfor
    prm_num[@]:=prm_rnk;
    if known ccl[@]:
      %
      % cycle detection
      %
      save ccl_idx, ccl_str;
      numeric ccl_idx;
      string ccl_str;
      ccl_idx:=@;
      ccl_str:=decimal ccl_idx;
      forever:
        ccl_idx:=ccl[ccl_idx];
        if ccl_idx>=0:
          ccl_str:=(decimal ccl_idx)&", "&ccl_str;
        fi
        exitif (unknown ccl[ccl_idx]) or (ccl_idx=@);
      endfor
      if ccl_idx=@:
        drv_errhelp  "Inferences declarations can not be cyclic.";
        drv_errmessage "inference declaration "&(decimal @)&" creates a"&
          " cycle of inferences (indices "&ccl_str&" from conclusions to"&
          " premises)";
      fi
    else:
      root[root_num]:=@;
      root_num:=root_num+1;
    fi
    %
    % lilb, rilb: inference labels
    %
    opt_init[ilb_sty[drv_sty]] l_ilb[@] lilb;
    opt_init[ilb_sty[drv_sty]] r_ilb[@] rilb;
    %
    % ldlb, rdlb: delimiter labels
    %
    opt_init[dlb_sty[drv_sty]] l_dlb[@] ldlb;
    opt_init[dlb_sty[drv_sty]] r_dlb[@] rdlb;
    %
    % j_sty, a_sty and i_sty: junction, alignment and inference styles
    %
    jct_sty[@]:=check_junction_style (j_sty, drv_jct_sty);
    alg_sty[@]:=check_alignment_style (a_sty, drv_alg_sty);
    iln_sty[@]:=check_path_style (i_sty, drv_iln_sty);
    if drv_boxmode_on:
      boxed[@]:=true;
    fi
  fi
enddef;

vardef DCL[] (text PRM) (expr lilb, rilb, ldlb, rdlb)
  (suffix j_sty, a_sty, i_sty) text SUB=
    NFR[@] (PRM) (lilb, rilb, ldlb, rdlb, j_sty, a_sty, i_sty);
    jgm[@] SUB;
enddef;

vardef MVD[] (expr inf_num, lplb, rplb) (suffix a_sty, p_sty)=
  if inf_num<1:
    drv_errhelp "The minimal value is 1.";
    drv_errmessage (decimal inf_num)&" is not a valid number of inference"&
      " steps";
  else:
    save aux_idx, aux_sty, phm_jdg;
    numeric aux_idx, aux_sty;
    picture phm_jdg;
    aux_sty:=check_path_style (p_sty, drv_phm_sty);
    phm_jdg:=nullpicture;
    setbounds phm_jdg to (0, 0)--(iln_hg[aux_sty]-iln_dp[aux_sty], 0)--cycle;
    aux_idx:=@;
    for i=1 upto inf_num:
      phm_idx:=phm_idx-1;
      box_init jdg[phm_idx] (phm_jdg);
      sbj_num[phm_idx]:=0;
      NFR[phm_idx] (aux_idx) ("", "", "", "", 0, a_sty, 0);
      aux_idx:=phm_idx;
    endfor
    src[@]:=phm_idx;
    tgt[phm_idx]:=@;
    phm_sty[phm_idx]:=aux_sty;
    %
    % lplb, rplb: phantom steps labels
    %
    opt_init[plb_sty[drv_sty]] l_plb[phm_idx] lplb;
    opt_init[plb_sty[drv_sty]] r_plb[phm_idx] rplb;
    phm_idx
  fi
enddef;

vardef bxd[]=
  boxed[@]:=true;
  @
enddef;

%
% Optional labels & shorthands
%

numeric drv_jct_sty;	% default junction style
string  drv_alg_sty;	% default junction style
numeric drv_iln_sty;	% default inference line path-style
numeric drv_phm_sty;	% default phantom steps path-style

boolean drv_left_ilb;	% inference labels on the left
boolean drv_left_dlb;	% delimiter labels (hence delimiters) on the left
boolean drv_left_plb;	% phantom steps labels on the left

vardef arg_type expr arg=
  if picture arg:
    "picture"
  else:
    "string"
  fi
enddef;

vardef opt_lbls @# (text LBL) expr left_lbls=
  save lbl_num, fst_lbl, snd_lbl;
  numeric lbl_num;
  lbl_num:=0;
  for lbl=LBL:
    if lbl_num=0:
      scantokens (arg_type lbl) fst_lbl;
      fst_lbl:=lbl;
    elseif lbl_num=1:
      scantokens (arg_type lbl) snd_lbl;
      snd_lbl:=lbl;
    else:
      drv_errhelp "At most two labels can be specified:"&
        " a left one and a right one.";
      drv_errmessage "more than two labels have been specified";
    fi
    lbl_num:=lbl_num+1;
  endfor
  if lbl_num=0:
    string @#.l;
    string @#.r;
    @#.l:="";
    @#.r:="";
  elseif lbl_num=1:
    if left_lbls:
      scantokens (arg_type fst_lbl) @#.l;
      string @#.r;
      @#.l:=fst_lbl;
      @#.r:="";
    else:
      string @#.l;
      scantokens (arg_type fst_lbl) @#.r;
      @#.l:="";
      @#.r:=fst_lbl;
    fi
  else:
    scantokens (arg_type fst_lbl) @#.l;
    scantokens (arg_type snd_lbl) @#.r;
    @#.l:=fst_lbl;
    @#.r:=snd_lbl;
  fi
enddef;

vardef NFR_opt[] (text PRM) (text ILB) (text DLB)
  (suffix j_sty, a_sty, i_sty)=
    opt_lbls ilb (ILB) drv_left_ilb;
    opt_lbls dlb (DLB) drv_left_dlb;
    NFR[@] (PRM) (ilb.l, ilb.r, dlb.l, dlb.r, j_sty, a_sty, i_sty);
enddef;

vardef DCL_opt[] (text PRM) (text ILB) (text DLB)
  (suffix j_sty, a_sty, i_sty) text SUB=
    NFR_opt[@] (PRM) (ILB) (DLB) (j_sty, a_sty, i_sty);
    jgm[@] SUB;
enddef;

vardef MVD_opt[] (expr inf_num) (text PLB) (suffix a_sty, p_sty)=
  opt_lbls plb (PLB) drv_left_plb;
  MVD[@] (inf_num, plb.l, plb.r, a_sty, p_sty)
enddef;

% Nfr, Dcl & Mvd

vardef Nfr[] (text PRM) (expr ilb, ldlb, rdlb) (suffix i_sty)=
  NFR_opt[@] (PRM) (ilb) (ldlb, rdlb) (_, _, i_sty);
enddef;

vardef Dcl[] (text PRM) (expr ilb, ldlb, rdlb) (suffix i_sty) text SUB=
  Nfr[@] (PRM) (ilb, ldlb, rdlb, i_sty);
  jgm[@] SUB;
enddef;

vardef Mvd[] (expr inf_num, lplb, rplb) (suffix p_sty)=
  MVD[@] (inf_num, lplb, rplb, _, p_sty)
enddef;

% nfr, dcl & mvd

vardef nfr[] (text PRM) (expr ilb) (suffix i_sty)=
  NFR_opt[@] (PRM) (ilb) () (_, _, i_sty);
enddef;

vardef dcl[] (text PRM) (expr ilb) (suffix i_sty) text SUB=
  nfr[@] (PRM) (ilb, i_sty);
  jgm[@] SUB;
enddef;

vardef mvd[] (expr inf_num) (suffix p_sty)=
  MVD_opt[@] (inf_num) () (_, p_sty)
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Pictured paths
%

vardef dotted_aux (expr pth, dot_wd, dots_dist)=
  save pth_len, pct;
  numeric pth_len;
  picture pct;
  pth_len:=arclength pth;
  pct:=nullpicture;
  if dots_dist>0:
    stp_num:=round ((pth_len-dot_wd)/dots_dist);
    if stp_num>0:
      stp_len:=(pth_len-dot_wd)/stp_num;
      addto pct doublepath pth dashed dashpattern (on 0 off stp_len)
        shifted (dot_wd/2, 0) withpen pencircle scaled dot_wd;
    fi
  fi
  pct
enddef;

primarydef pth dotted wd_dist=
  dotted_aux(pth, xpart wd_dist, ypart wd_dist)
enddef;

vardef expanded_aux (expr pth, scl, dir, stp_len)=
  save pth_len, aux_len;
  numeric pth_len, aux_len;
  pth_len:=arclength pth;
  if (stp_len>0) and (pth_len>stp_len):
    aux_len:=pth_len/(pth_len div stp_len);
    point arctime 0 of pth of pth shifted
      (unitvector direction arctime 0 of pth of pth
        rotated dir scaled scl)..
          {direction arctime 0 of pth of pth}
    for i=aux_len step aux_len until pth_len-aux_len:
      point arctime i of pth of pth shifted
        (unitvector direction arctime i of pth of pth
          rotated dir scaled scl)
            {direction arctime i of pth of pth}..
    endfor
    point arctime pth_len of pth of pth shifted
      (unitvector direction arctime pth_len of pth of pth
        rotated dir scaled scl)
          {direction arctime pth_len of pth of pth}
  else:
    pth
  fi
enddef;

primarydef pth expanded scl_dir_len=
  expanded_aux (pth,
    redpart scl_dir_len, greenpart scl_dir_len, bluepart scl_dir_len)
enddef;

vardef waved_aux (expr pth, amp)=
  save pth_len, mid;
  numeric pth_len, mid;
  pth_len:=arclength pth;
  if (amp>0) and (pth_len>2(1+sqrt 5)*amp):
    mid:=pth_len/(2(pth_len div (2(1+sqrt 5)*amp)));
    point arctime 0 of pth of pth shifted
      (unitvector direction arctime 0 of pth of pth
        rotated -90 scaled amp)
          {direction arctime 0 of pth of pth}
    for i=mid step 2mid until pth_len:
    ..point arctime i of pth of pth shifted
        (unitvector direction arctime i of pth of pth
          rotated 90 scaled amp)
            {direction arctime i of pth of pth}
    ..point arctime i+mid of pth of pth shifted
        (unitvector direction arctime i+mid of pth of pth
          rotated -90 scaled amp)
            {direction arctime i+mid of pth of pth}
    endfor
  else:
    pth
  fi
enddef;

primarydef pth waved amp=
  waved_aux(pth, amp)
enddef;

vardef drv_styled_aux[] expr pth=
  save sty_idx, pth_len, pct;
  numeric sty_idx, pth_len;
  picture pct;
  sty_idx:=check_path_style (@, 1);
  pth_len:=arclength pth;
  pct:=nullpicture;
  if sty_idx=1:
    interim linecap:=butt;
    addto pct doublepath pth withpen drv_pen;
  elseif sty_idx=2:
    interim linecap:=butt;
    addto pct doublepath pth
      expanded (1.25rul_thk[drv_sty],  90, rul_thk[drv_sty])
      withpen drv_pen;
    addto pct doublepath pth
      expanded (1.25rul_thk[drv_sty], -90, rul_thk[drv_sty])
      withpen drv_pen;
  elseif sty_idx=3:
    addto pct also pth dotted (rul_thk[drv_sty], 2rul_thk[drv_sty]);
  elseif sty_idx=4:
    if rul_thk[drv_sty]>0:
      interim linecap:=butt;
      save dsh_num, dsh_len;
      numeric dsh_num, dsh_len;
      dsh_num:=round(pth_len/(3rul_thk[drv_sty]));
      dsh_len:=
        if odd dsh_num:
          pth_len/dsh_num
        else:
          pth_len/(dsh_num+1)
        fi;
      addto pct doublepath pth dashed dashpattern(on dsh_len off dsh_len)
        withpen drv_pen;
    fi
  elseif sty_idx=5:
    interim linecap:=butt;
    addto pct doublepath pth
      waved 1.25rul_thk[drv_sty] withpen drv_pen;
  elseif (sty_idx=6) and (pth_len>0):
    addto pct also pth dotted (pdt_hg[drv_sty], pdt_wd[drv_sty]);
  fi
  pct
enddef;

primarydef pth drv_styled sty=
  drv_styled_aux[sty] pth
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Delimiters
%

%
% 1. Global parameters
%

string drv_l_dlm; % left delimiter LaTeX code
string drv_r_dlm; % right delimiter LaTeX code

%
% 2. Macros
%

vardef minmax_y[]=(
  if drv_bot_roots:
    if known ccl[@]:
      ypart jdg[@].c-num_hg+iln_hg[(iln_sty[ccl[@]])]
        +min_clr_scl*min_clr[drv_sty]/2
    else:
      smin_y[@]-min_clr_scl*min_clr[drv_sty]
    fi,
    smax_y[@]+min_clr_scl*min_clr[drv_sty]
  else:
    smin_y[@]-min_clr_scl*min_clr[drv_sty],
    if known ccl[@]:
      ypart jdg[@].c-den_dp-iln_hg[(iln_sty[ccl[@]])]
        -min_clr_scl*min_clr[drv_sty]/2
    else:
      smax_y[@]+min_clr_scl*min_clr[drv_sty]
    fi
  fi)
enddef;

vardef delim @# expr dlm_len=
  save rule_str, dlm_str, dlm_pct, dlm_dp;
  string rule_str, dlm_str;
  picture dlm_pct;
  numeric dlm_dp;
  rule_str:="\rule{0bp}{"&(decimal(dlm_len/2+axis_hg[drv_sty]))&"bp}";
  dlm_str:=
    if str @#="l":
      "\left"&drv_l_dlm&rule_str&"\right."
    else:
      "\left."&rule_str&"\right"&drv_r_dlm
    fi;
  dlm_pct:=mth_tex[drv_sty] dlm_str;
  dlm_dp:=depth dlm_pct;
  if dlm_dp=0:
    nullpicture
  else:
    begingroup
      setbounds dlm_pct to
        (xpart llcorner dlm_pct,  dlm_dp)
      --(xpart ulcorner dlm_pct, -dlm_dp)
      --(xpart urcorner dlm_pct, -dlm_dp)
      --(xpart lrcorner dlm_pct,  dlm_dp)
      --cycle;
      dlm_pct yscaled (dlm_len/(2*height dlm_pct))
    endgroup
  fi
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Checking, building, freezing (very inefficient, to be improved)
%

%
% 1. Global parameters
%

numeric min_clr_scl;		%
numeric prm_skp_scl;		%
numeric jdg_skp_scl;		%
numeric ilb_skp_scl;		%
numeric plb_skp_scl;		%

boolean drv_bot_roots;		%
boolean drv_boxmode_on;		%
boolean drv_fractionmode_on;	%
string  drv_axis_ref;		%

%
% 2. Macros
%

%
% Checking
%

vardef fraction_inference[]=
      (prm_num[@]=0)
  and (iln_sty[@]=0)
  and (unknown l_ilb[@])
  and (unknown r_ilb[@])
  and drv_fractionmode_on
enddef;

vardef drv_check_root[]=
  if unknown jdg[@]:
    drv_errhelp "An index must be used to declare a both a judgment and an"&
      " inference.";
    drv_errmessage (decimal @)&" has been used as an inference index but not"&
      " as a judgment index";
  elseif unknown prm_num[@]:
    drv_errhelp "An index must be used to declare a both a judgment and an"&
      " inference.";
    drv_errmessage (decimal @)&" has been used as a judgment index but not"&
      " as an inference index";
  else:
    save i_sty, prm_idx;
    numeric i_sty, prm_idx;
    i_sty:=iln_sty[@];
    for prm_rnk=0 upto prm_num[@]-1:
      prm_idx:=prm[@][prm_rnk];
      drv_check_root[prm_idx];
      %
      % numerators and denominators clearances
      % The TEXbook, Appendix G, Rule 15d.
      %
      if drv_bot_roots:
        num_hg:=max(num_hg,
          iln_hg[i_sty]+min_clr_scl*min_clr[drv_sty]-jdg[prm_idx].dp);
      else:
        den_dp:=min(den_dp,
          iln_dp[i_sty]-min_clr_scl*min_clr[drv_sty]-jdg[prm_idx].hg);
      fi
    endfor
    if not fraction_inference[@]:
      if drv_bot_roots:
        den_dp:=min(den_dp,
          iln_dp[i_sty]-min_clr_scl*min_clr[drv_sty]-jdg[@].hg);
      else:
        num_hg:=max(num_hg,
          iln_hg[i_sty]+min_clr_scl*min_clr[drv_sty]-jdg[@].dp);
      fi
    fi
    if unknown boxed[@]:
      boxed[@]:=false;
    fi
  fi
enddef;

vardef drv_check=
  if root_num=0:
    drv_errhelp "Well ...";
    drv_errmessage "no inference has been declared";
  else:
    save root_idx;
    numeric root_idx;
    root_idx:=root[0];
    drv_check_root[root_idx];
    if root_num>1:
      save root_str;
      string root_str;
      root_str:=decimal root_idx;
      for idx=1 upto root_num-1:
        root_idx:=root[idx];
        drv_check_root[root_idx];
        root_str:=root_str&", "&(decimal root_idx);
      endfor
      drv_message "warning, multiple root indices ("&root_str&").";
    fi
  fi
  drv_checked:=true;
enddef;

%
% Building
%

vardef drv_build_root[]=
  %
  % premises junction (from left to right)
  %
  for prm_rnk=0 upto  prm_num[@]-1:
    drv_build_root[prm[@][prm_rnk]];
    if prm_rnk>0:
      save ll_idx, lr_idx, rl_idx, rr_idx;
      save lmax_x, rmin_x, min_skp;
      numeric ll_idx, lr_idx, rl_idx, rr_idx;
      numeric lmax_x, rmin_x, min_skp;
      ypart jdg[prm[@][prm_rnk-1]].c=ypart jdg[prm[@][prm_rnk]].c;
      ll_idx:=prm[@][0];
      lr_idx:=prm[@][prm_rnk-1];
      rl_idx:=prm[@][prm_rnk];
      rr_idx:=prm[@][prm_rnk];
      lmax_x:=imax_x[lr_idx];
      rmin_x:=imin_x[rl_idx];
      min_skp[0]:=rmin_x-lmax_x;
      forever:
        exitif (unknown r_prm[lr_idx]) or (unknown l_prm[rl_idx]);
        ll_idx:=l_prm[ll_idx];
        lr_idx:=r_prm[lr_idx];
        rl_idx:=l_prm[rl_idx];
        rr_idx:=r_prm[rr_idx];
        lmax_x:=max(lmax_x, imax_x[lr_idx]);
        rmin_x:=min(rmin_x, imin_x[rl_idx]);
        min_skp[0]:=min(min_skp[0], imin_x[rl_idx]-imax_x[lr_idx]);
      endfor
      min_skp[1]:=rmin_x-lmax_x;
      if known r_prm[lr_idx]:
        r_prm[rr_idx]:= r_prm[lr_idx];
        lmax_x:=smax_x[lr_idx];
      elseif known l_prm[rl_idx]:
        l_prm[ll_idx]:=l_prm[rl_idx];
        rmin_x:=smin_x[rl_idx];
      fi
      min_skp[2]:=rmin_x-lmax_x;
      if jct_sty[@]<3:
        min_skp[jct_sty[@]]=prm_skp_scl*prm_skp[drv_sty];
      fi
    fi
  endfor
  if  prm_num[@]>0:
    l_prm[@]:=prm[@][0];
    r_prm[@]:=prm[@][prm_num[@]-1];
  fi
  %
  % judgment alignment
  %
  if prm_num[@]=0:
    imin_x[@]:=xpart jdg[@].l-jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
    imax_x[@]:=xpart jdg[@].r+jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
  else:
    save l_xref, r_xref;
    l_xref=
      if boxed[prm[@][0]]:
        smin_x[prm[@][0]]
      else:
        xpart jdg[prm[@][0]].l
      fi;
    r_xref=
      if boxed[prm[@][prm_num[@]-1]]:
        smax_x[prm[@][prm_num[@]-1]]
      else:
        xpart jdg[prm[@][prm_num[@]-1]].r
      fi;
    if alg_sty[@]="l":
      xpart jdg[@].l=l_xref;
    elseif alg_sty[@]="c":
      xpart jdg[@].c=(l_xref+r_xref)/2;
    elseif alg_sty[@]="r":
      xpart jdg[@].r=r_xref;
    fi
    imin_x[@]:=min(xpart jdg[@].l, l_xref)
          -jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
    imax_x[@]:=max(xpart jdg[@].r, r_xref)
          +jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
  fi
  %
  % inference line
  %
  save iln_wd, iln_pct;
  numeric iln_wd;
  picture iln_pct;
  iln_wd:=imax_x[@]-imin_x[@];
  iln_pct:=((0, 0)--(iln_wd, 0)) drv_styled iln_sty[@];
  setbounds iln_pct to
    (0, ypart llcorner iln_pct)
  --(0, ypart ulcorner iln_pct)
  --(iln_wd, ypart ulcorner iln_pct)
  --(iln_wd, ypart llcorner iln_pct)
  --cycle; % ! `linecap:=butt' and `linecap:=squared' -> same bounding box !
  box_init iln[@] (iln_pct);
  xpart iln[@].c=(imin_x[@]+imax_x[@])/2;
  %
  % orientation, smin_y & smax_y
  %
  if drv_bot_roots:
    ypart iln[@].c=ypart jdg[@].c-den_dp;
    smin_y[@]:=ypart jdg[@].c+jdg[@].dp;
    smax_y[@]:=
      if iln_sty[@]=0:
        ypart jdg[@].c+jdg[@].hg
      else:
        ypart iln[@].c+iln[@].hg
      fi;
    if prm_num[@]>0:
      ypart iln[@].c=ypart jdg[prm[@][0]].c-num_hg;
      for prm_rnk=0 upto prm_num[@]-1:
        smax_y[@]:=max(smax_y[@], smax_y[prm[@][prm_rnk]]);
      endfor;
    fi
  else:
    ypart iln[@].c=ypart jdg[@].c-num_hg;
    smin_y[@]:=
      if iln_sty[@]=0:
        ypart jdg[@].c+jdg[@].dp
      else:
        ypart iln[@].c+iln[@].dp
      fi;
    smax_y[@]:=ypart jdg[@].c+jdg[@].hg;
    if prm_num[@]>0:
      ypart iln[@].c=ypart jdg[prm[@][0]].c-den_dp;
      for prm_rnk=0 upto prm_num[@]-1:
        smin_y[@]:=min(smin_y[@], smin_y[prm[@][prm_rnk]]);
      endfor;
    fi
  fi
  %
  % inference labels
  %
  if known l_ilb[@]:
    l_ilb[@].r=iln[@].l shifted (-ilb_skp_scl*ilb_skp[drv_sty], 0);
    imin_x[@]:=xpart l_ilb[@].l;
    smin_y[@]:=min(smin_y[@], ypart l_ilb[@].c+l_ilb[@].dp);
    smax_y[@]:=max(smax_y[@], ypart l_ilb[@].c+l_ilb[@].hg);
  fi
  if known r_ilb[@]:
    r_ilb[@].l=iln[@].r shifted (ilb_skp_scl*ilb_skp[drv_sty], 0);
    imax_x[@]:=xpart r_ilb[@].r;
    smin_y[@]:=min(smin_y[@], ypart r_ilb[@].c+r_ilb[@].dp);
    smax_y[@]:=max(smax_y[@], ypart r_ilb[@].c+r_ilb[@].hg);
  fi
  %
  % phantom steps
  %
  if known tgt[@]:
    save phm_pth, phm_pct;
    path phm_pth;
    picture phm_pct;
    save top_y, mid_y, bot_y;
    numeric top_y, bot_y;
    if drv_bot_roots:
      bot_y:=xpart minmax_y[@]+min_clr_scl*min_clr[drv_sty]/2;
      top_y:=xpart minmax_y[tgt[@]];
    else:
      bot_y:=ypart minmax_y[tgt[@]];
      top_y:=ypart minmax_y[@]-min_clr_scl*min_clr[drv_sty]/2;
    fi
    mid_y:=(top_y+bot_y)/2;
    phm_pth:=(0, (bot_y-top_y)/2)--(0, (top_y-bot_y)/2);
    phm_pct:=phm_pth drv_styled phm_sty[@];
    box_init phm[@] (phm_pct shifted ((width phm_pct)/2, 0));
    phm[@].c=(xpart jdg[@].c, mid_y);
    imin_x[@]:=min(imin_x[@], xpart phm[@].l);
    imax_x[@]:=max(imax_x[@], xpart phm[@].r);
    %
    % phantom steps labels
    %
    if known l_plb[@]:
      l_plb[@].r=phm[@].l shifted (-plb_skp[drv_sty], 0);
      imin_x[@]:=min(imin_x[@], xpart l_plb[@].l);
    fi
    if known r_plb[@]:
      r_plb[@].l=phm[@].r shifted ( plb_skp[drv_sty], 0);
      imax_x[@]:=max(imax_x[@], xpart r_plb[@].r);
    fi
  fi
  %
  % smin_x & smax_x
  %
  smin_x[@]:=imin_x[@];
  smax_x[@]:=imax_x[@];
  for prm_rnk=0 upto prm_num[@]-1:
    smin_x[@]:=min(smin_x[@], smin_x[prm[@][prm_rnk]]);
    smax_x[@]:=max(smax_x[@], smax_x[prm[@][prm_rnk]]);
  endfor
  %
  % delimiters
  %
  if known l_dlb[@] or known r_dlb[@]:
    save idx, mmy, min_y, max_y;
    numeric idx, min_y, max_y;
    pair mmy;
    mmy:=minmax_y[@];
    min_y:=xpart mmy;
    max_y:=ypart mmy;
    if known l_dlb[@]:
      box_init l_dlm[@] (delim l (max_y-min_y));
      l_dlm[@].r=(smin_x[@], (max_y+min_y)/2);
      l_dlb[@].r=l_dlm[@].l;
      smin_x[@]:=xpart l_dlb[@].l;
    fi
    if known r_dlb[@]:
      box_init r_dlm[@] (delim r (max_y-min_y));
      r_dlm[@].l=(smax_x[@], (max_y+min_y)/2);
      r_dlb[@].l=r_dlm[@].r;
      smax_x[@]:=xpart r_dlb[@].r;
    fi
  fi
  if (known l_dlb[@]) or boxed[@]:
    idx:=@;
    forever:
      imin_x[idx]:=smin_x[@];
      exitif unknown l_prm[idx];
      idx:=l_prm[idx];
    endfor
  fi
  if (known r_dlb[@]) or boxed[@]:
    idx:=@;
    forever:
      imax_x[idx]:=smax_x[@];
      exitif unknown r_prm[idx];
      idx:=r_prm[idx];
    endfor
  fi
enddef;

vardef drv_build=
  if not drv_checked:
    drv_check;
  fi
  for idx=0 upto root_num-1:
    drv_build_root[root[idx]];
  endfor
  drv_built:=true;
enddef;

%
% Freezing
%

vardef drv_freeze_root[]=
  for prm_rnk=0 upto prm_num[@]-1:
    drv_freeze_root[prm[@][prm_rnk]];
  endfor
  for sbj_rnk=0 upto sbj_num[@]-1:
    box_freeze sbj[@][sbj_rnk];
  endfor
  forsuffixes sfx=
    iln, jdg, phm,
    l_dlb, l_dlm, l_ilb, l_plb,
    r_dlb, r_dlm, r_ilb, r_plb:
      if known sfx[@]:
        box_freeze sfx[@];
      fi
  endfor
enddef;

vardef drv_freeze=
  if not drv_built:
    drv_build;
  fi
  save root_idx;
  numeric root_idx;
  root_idx:=root[0];
  if unknown iln[root_idx].c:
    %
    % default math axis
    %
    if drv_axis_ref="iln":
      iln[root_idx].c=(0, axis_hg[drv_sty]);
    else: % drv_axis_ref="jdg"
      jdg[root_idx].c=(0, axis_hg[drv_sty]);
    fi
    drv_freeze_root[root_idx];
  fi
  for idx=1 upto root_num-1:
    root_idx:=root[idx];
    if unknown iln[root_idx].c:
      smin_x[root_idx]=smax_x[root[idx-1]]+prm_skp_scl*prm_skp[drv_sty];
      ypart iln[root_idx].c=ypart iln[root[idx-1]].c;
    fi
    drv_freeze_root[root_idx];
  endfor
  drv_frozen:=true;
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Derivation tree pictures
%

%
% 1. Global parameters
%

boolean drv_proofmode_on;	%
color prf_clr;			%
color jdg_clr;			%
color sbj_clr;			%

prf_clr:=.75background;		% arbitrary
jdg_clr:=red;			% arbitrary
sbj_clr:=blue;			% arbitrary

%
% 2. Macros
%

vardef drv_tree_addto @# (expr idx) text draw_options=
  for prm_rnk=0 upto prm_num[idx]-1:
    drv_tree_addto @# (prm[idx][prm_rnk]) draw_options;
  endfor
  forsuffixes sfx=
    jdg, phm,
    l_dlb, l_dlm, l_ilb, l_plb,
    r_dlb, r_dlm, r_ilb, r_plb:
      if known sfx[idx]:
        addto @# also sfx[idx] draw_options;
      fi
  endfor
  if iln_sty[idx]>0:
      addto @# also iln[idx] draw_options;
  fi
enddef;

vardef tt_nat[] expr nat= % typewriter typeface naturals
  save nat_aux, nat_pct_wd, dgt_num, nat_pct;
  numeric nat_aux, nat_pct_wd, dgt_num;
  picture nat_pct;
  nat_aux:=abs nat;
  nat_pct:=nullpicture;
  nat_pct_wd:=0;
  dgt_num:=1;
  forever:
    exitif nat_aux div (10**dgt_num)=0;
    dgt_num:=dgt_num+1;
  endfor
  for rnk:=1 upto dgt_num:
    addto nat_pct also tt_dgt[@][nat_aux div (10**(dgt_num-rnk))]
      shifted (nat_pct_wd, 0);
    nat_pct_wd:=width nat_pct;
    nat_aux:=nat_aux mod (10**(dgt_num-rnk));
  endfor
  nat_pct shifted (0, -axis_hg[@])
enddef;

vardef prf_tree_addto @# expr idx=
  for prm_rnk=0 upto prm_num[idx]-1:
    prf_tree_addto @# (prm[idx][prm_rnk]);
  endfor
  if idx>=0:
    box_init jdg_prf[idx] (tt_nat[2] idx);
    jdg_prf[idx].r=jdg[idx].l;
    box_freeze jdg_prf[idx];
    addto @# also jdg_prf[idx] withcolor jdg_clr;
    addto @# doublepath jdg[idx].c withcolor jdg_clr
      withpen pencircle scaled (2*pdt_hg[drv_sty]);
  fi
  for sbj_rnk=0 upto sbj_num[idx]-1:
    box_init sbj_prf[idx][sbj_rnk] (tt_nat[3] sbj_rnk);
    if odd sbj_rnk:
      sbj_prf[idx][sbj_rnk].c shifted (0, sbj_prf[idx][sbj_rnk].hg)=
        sbj[idx][sbj_rnk].c
          shifted (0, -den_dp-min_clr_scl*min_clr[drv_sty]/2);
    else:
      sbj_prf[idx][sbj_rnk].c shifted (0, sbj_prf[idx][sbj_rnk].dp)=
        sbj[idx][sbj_rnk].c
          shifted (0, -num_hg+min_clr_scl*min_clr[drv_sty]/2);
    fi
    box_freeze sbj_prf[idx][sbj_rnk];
    addto @# also sbj_prf[idx][sbj_rnk] withcolor sbj_clr;
    addto @# doublepath sbj[idx][sbj_rnk].c withcolor sbj_clr
      withpen pencircle scaled pdt_hg[drv_sty];
  endfor
enddef;

vardef drv_tree=
  save pct;
  picture pct;
  pct:=nullpicture;
  if not drv_frozen:
    drv_freeze;
  fi
  for root_idx=0 upto root_num-1:
    if drv_proofmode_on:
      drv_tree_addto pct (root[root_idx]) withcolor prf_clr;
      prf_tree_addto pct (root[root_idx]);
    else:
      drv_tree_addto pct (root[root_idx]);
    fi
  endfor
  pct
enddef;

vardef drv_bbox[]=
  save min_x, max_x, mmy, min_y, max_y;
  pair mmy;
  numeric min_x, max_x, max_y, min_y;
  if not drv_frozen:
    drv_freeze;
  fi
  min_x:=
    if known l_dlm[@].r:
      xpart l_dlm[@].r
    else:
      smin_x[@]
    fi-jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
  max_x:=
    if known r_dlm[@].l:
      xpart l_dlm[@].l
    else:
      smax_x[@]
    fi+jdg_skp_scl*jdg_skp[jdg_sty[drv_sty]];
  mmy:=minmax_y[@];
  min_y:=xpart mmy;
  max_y:=ypart mmy;
  (min_x, min_y)--
  (min_x, max_y)--
  (max_x, max_y)--
  (max_x, min_y)--
  cycle
enddef;

vardef mth_axis @# (suffix ref_sfx) (expr idx)=
  if not drv_frozen:
    drv_freeze;
  fi
  if unknown prm_num[idx]:
    drv_errhelp "An index must be used to declare an inference before it is"&
      " used to set the math axis of a picture.";
    drv_errmessage "no inference has been declared using index "&(decimal idx);
  else:
    save ref_str, yref;
    string ref_str;
    numeric yref;
    ref_str:=str ref_sfx;
    if ref_str="iln":
      yref:=ypart iln[idx].c;
    elseif ref_str="jdg":
      yref:=ypart jdg[idx].c;
    elseif ref_str="dlm":
      if known l_dlm[idx]:
        yref:=ypart l_dlm[idx].c;
      elseif known r_dlm[idx]:
        yref:=ypart r_dlm[idx].c;
      else:
        drv_errhelp "Well ...";
        drv_errmessage "no delimiter is attached to inference "&(decimal idx);
      fi
    else:
      drv_errhelp "Valid identifiers are"&
        " iln (inference line axis),"&
        " jdg (judgment math-axis) and"&
        " dlm (delimiter axis).";
      drv_errmessage (ref_str&" is not a valid reference type identifier");
    fi
    if known yref:
      @#:=@# shifted (0, axis_hg[drv_sty]-yref);
    fi
  fi
enddef;

vardef drv_axis (suffix ref) (expr idx)=
  mth_axis currentpicture (ref, idx);
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Tunings
%

vardef check_math_style (expr sty_str, default)=
  if sty_str="\displaystyle":
    0
  elseif sty_str="\textstyle":
    1
  elseif sty_str="\scriptstyle":
    2
  elseif sty_str="\scriptscriptstyle":
    3
  else:
    begingroup
      drv_errhelp "Valid commands are"&
        " \displaystyle,"&
        " \textstyle,"&
        " \scriptstyle and"&
        " \scriptscriptstyle.";
      drv_errmessage (sty_str&" is not a valid math-style command");
      default
    endgroup
  fi
enddef;

vardef check_position_left (suffix pos_sfx) (expr default)=
  save pos_str;
  string pos_str;
  pos_str:=str pos_sfx;
  if (pos_str="l"):
    true
  elseif (pos_str="r"):
    false
  else:
    begingroup
      drv_errhelp "Valid identifiers are l (left) and r (right).";
      drv_errmessage (pos_str&" is not a valid label position identifier");
      default
    endgroup
  fi
enddef;

vardef check_mode_on (suffix status_sfx) (expr default)=
  save status_str;
  string status_str;
  status_str:=str status_sfx;
  if status_str="on":
    true
  elseif status_str="off":
    false
  else:
    begingroup
      drv_errhelp "Possible identifiers are on and off";
      drv_errmessage (status_str&" is not a valid status identifier");
      default
    endgroup
  fi
enddef;

vardef drv_math_style (suffix typ_sfx) (expr sty_str)=
  save typ_str, sty_idx;
  string typ_str;
  numeric sty_idx;
  typ_str:=str typ_sfx;
  if  (typ_str<>"drv")
  and (typ_str<>"jdg")
  and (typ_str<>"ilb")
  and (typ_str<>"dlb")
  and (typ_str<>"plb"):
    drv_errhelp "Valid  identifiers are"&
      " drv (derivation tree),"&
      " jdg (judgment),"&
      " ilb (inference label),"&
      " dlb (delimiter label) and"&
      " plb (phantom steps label).";
    drv_errmessage (sty_str&" is not a valid component identifier");
  else:
    sty_idx:=check_math_style (sty_str, 0);
    if typ_str="drv":
      drv_sty:=sty_idx;
    else:
      forsuffixes sfx=scantokens (typ_str&"_sty"):
        for idx=0 upto 3:
          sfx[idx]:=min(idx+sty_idx, 3);
        endfor
      endfor
    fi
  fi
enddef;

vardef drv_scale (suffix scl_sfx) (expr scl)=
  save scl_str;
  string scl_str;
  scl_str:=str scl_sfx;
  if scl_str="clr":
    min_clr_scl:=scl;
  elseif scl_str="prm":
    prm_skp_scl:=scl;
  elseif (scl_str="jdg") or (scl_str="jgm"): % "jgm" is deprecated
    jdg_skp_scl:=scl;
  elseif (scl_str="ilb") or (scl_str="lbl"): % "lbl" is deprecated
    ilb_skp_scl:=scl;
  else:
    drv_errhelp "Valid identifiers are"&
      " clr,"&
      " prm,"&
      " jdg and"&
      " ilb.";
    drv_errmessage (scl_str&" is not a valid scale identifier");
  fi
enddef;

vardef drv_junction_style suffix sty_sfx=
  drv_jct_sty:=check_junction_style (sty_sfx, drv_jct_sty);
enddef;

vardef drv_alignment_style suffix sty_sfx=
  drv_alg_sty:=check_alignment_style (sty_sfx, drv_alg_sty);
enddef;

vardef drv_path_style (suffix typ_sfx, sty_sfx)=
  save typ_str;
  string typ_str;
  typ_str:=str typ_sfx;
  if typ_str="iln":
    drv_iln_sty:=check_path_style (sty_sfx, drv_iln_sty);
  elseif typ_str="phm":
    drv_phm_sty:=check_path_style (sty_sfx, drv_phm_sty);
  else:
    drv_errhelp "Valid identifiers are"&
      " iln (inference line) and"&
      " phm (phantom steps path).";
    drv_errmessage (typ_str&" is not a valid path-type identifier");
  fi
enddef;

vardef drv_labels_position_aux (suffix typ_sfx, pos_sfx)=
  save typ_str, idt_str, pos_str;
  string typ_str, idt_str, pos_str;
  typ_str:=str typ_sfx;
  if  typ_str="ilb":
    drv_left_ilb:=check_position_left (pos_sfx, drv_left_ilb);
  elseif typ_str="dlb":
    drv_left_dlb:=check_position_left (pos_sfx, drv_left_dlb);
  elseif typ_str="plb":
    drv_left_plb:=check_position_left (pos_sfx, drv_left_plb);
  else:
    drv_errhelp "Valid identifiers are"&
      " ilb (inference label),"&
      " dlb (delimiter label) and"&
      " plb (phantom steps label).";
    drv_errmessage (typ_str&" is not a valid label-type identifier");
  fi
enddef;

vardef drv_labels_position text ARG=
  drv_labels_position_aux if pair ARG: ARG else: (ilb, ARG) fi;
enddef;

vardef drv_roots_position suffix pos_sfx=
  save pos_str;
  string pos_str;
  pos_str:=str pos_sfx;
  if pos_str="t":
    drv_bot_roots:=false;
  elseif pos_str="b":
    drv_bot_roots:=true;
  else:
    drv_errhelp "Valid identifiers are"&
      " t (top) and"&
      " b (bottom).";
    drv_errmessage (pos_str&" is not a valid root position identifier");
  fi
enddef;

vardef drv_axis_reference suffix ref_sfx=
  save ref_str;
  string ref_str;
  ref_str:=str ref_sfx;
  if (ref_str<>"iln") and (ref_str<>"jdg"):
    drv_errhelp "Valid identifiers are iln (inference line axis) and"&
      " jdg (judgment math-axis).";
    drv_errmessage (ref_str&" is not a valid reference identifier");
  else:
    drv_axis_ref:=ref_str;
  fi
enddef;

vardef drv_delimiter (suffix pos_sfx) (expr dlm_str)=
  if check_position_left (pos_sfx, true):
    drv_l_dlm:=dlm_str;
  else:
    drv_r_dlm:=dlm_str;
  fi
enddef;

vardef drv_left_delimiter expr dlm_str=
  drv_delimiter (l, dlm_str);
enddef;

vardef drv_right_delimiter expr dlm_str=
  drv_delimiter (r, dlm_str);
enddef;

vardef drv_box_mode suffix status_sfx=
  drv_boxmode_on:=check_mode_on (status_sfx, drv_boxmode_on);
enddef;

vardef drv_fraction_mode suffix status_sfx=
  drv_fractionmode_on:=check_mode_on (status_sfx, drv_fractionmode_on);
enddef;

vardef drv_proof_mode suffix status_sfx=
  drv_proofmode_on:=check_mode_on (status_sfx, drv_proofmode_on);
enddef;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Default settings
%

drv_font_size "\normalsize";
drv_math_style (drv, "\displaystyle");
drv_math_style (jdg, "\textstyle");
drv_math_style (ilb, "\scriptstyle");
drv_math_style (dlb, "\textstyle");
drv_math_style (plb, "\textstyle");
drv_scale (clr, 1);
drv_scale (prm, 1);
drv_scale (jdg, 1);
drv_scale (ilb, 1);
drv_junction_style 1;
drv_alignment_style c;
drv_path_style (iln, 1);
drv_path_style (phm, 3);
drv_labels_position (ilb, r);
drv_labels_position (dlb, l);
drv_labels_position (plb, l);
drv_roots_position b;
drv_axis_reference iln;
drv_left_delimiter "\lbrace";
drv_right_delimiter "\rbrace";
drv_box_mode off;
drv_fraction_mode on;
drv_proof_mode off;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% LaTeX inclusion macro
%
% \drv includes a picture file in such a way that the baseline of the included
% picture coincides with the baseline of the inclusion point. The code for \drv
% was suggested by Josselin Noirel on the fr.comp.text.tex usenet group.
%
% \usepackage{graphicx}
% \makeatletter
% \def\Gin@def@bp#1\relax#2#3{\gdef#2{#3}}
% \newsavebox{\graphicsbox}
% \newcommand*{\drv}[1]{%
% \sbox{\graphicsbox}{\includegraphics{#1}}%
% \raisebox{\Gin@lly bp}%
% {\usebox{\graphicsbox}}}
% \makeatother