This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
recce.ltx
19674 lines (18515 loc) · 542 KB
/
recce.ltx
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
% Copyright 2015 Jeffrey Kegler
% This document is licensed under
% a Creative Commons Attribution-NoDerivs 3.0 United States License.
\documentclass[12pt,openany,twoside]{amsbook}
% \RequirePackage[l2tabu, orthodox]{nag}
\usepackage{microtype}
\usepackage{ragged2e}
\usepackage{mathtools}
\usepackage{tikz-cd}
\usepackage{tikz-qtree}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{amssymb}
\usepackage{amsmidx}
\usepackage{url}
\usepackage{amsfonts}% to get the \mathbb alphabet
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}
% \usepackage[columns=1]{idxlayout}
\usepackage[pdfpagelabels]{hyperref}
\usepackage{algpseudocode}
\usepackage{algorithm}
\allowdisplaybreaks
% In "Writing a thesis with LATEX", Lapo F. Mori
% says anything but '[tb]' and '[p]' for floats
% is counterproductive. I switched over based on
% his suggestion, and he's absolutely right.
% for escaping in index commands ...
% the quote sign does not seem to work in amsmidx
\newcommand{\Pipe}{\char‘\|}
\makeindex{recce-general}
\makeindex{recce-algorithms}
% \makeindex{recce-figures}
\makeindex{recce-theorems}
\makeindex{recce-definitions}
\makeindex{recce-notation}
% This is used to find the font size if need be
% its uses are usually commented out
\makeatletter
\newcommand\thefontsize[1]{{#1 The current font size is: \f@size pt\par}}
\makeatother
% \DeclareMathSizes{6}{6}{6}{6}
% \DeclareMathSizes{8}{8}{8}{8}
% \DeclareMathSizes{10}{10}{10}{10}
% \DeclareMathSizes{10.95}{10.95}{10.95}{10.95}
% \DeclareMathSizes{12}{12}{12}{12}
% \DeclareMathSizes{14.4}{14.4}{14.4}{14.4}
% \DeclareMathSizes{20.74}{20.74}{20.74}{20.74}
% \DeclareMathSizes{24.88}{24.88}{24.88}{24.88}
\newcommand{\mysee}[2]{\emph{see} #1}
\newcommand{\myixentry}[2]{#1}
% \newcommand{\todo}[1]{\par{\large\textbf Todo: #1}\par}
% I use multi-character variable names, and TeX's usual math italic
% does not work well for them. Rather than get into changing the
% default math font, I use a special macro for variables.
% It also allows me to use hyphens in variable names.
\newcommand{\var}[1]{\ensuremath{\texttt{#1}}}
% Many of the macros have two forms --
% '\xyz' and '\Vxyz'. The \Vxyz is the same
% as the \xyz form, except that it typesets its
% argument as a math variable in the style of this
% monograph.
\newcommand{\myfnname}[1]{\ensuremath{\texttt{#1}}}
\newcommand{\myopname}[1]{\ensuremath{\texttt{#1}}}
\newcommand{\myfn}[2]{\ensuremath{\myfnname{#1}(#2)}}
\newcommand{\vat}[2]{\ensuremath{\myfnname{v}(#1,#2)}}
\newcommand{\vatp}[3]{\ensuremath{\myfnname{v}(#1,#2,#3)}}
% definitions for "elements" -- x[i]
\newcommand{\el}[2]{\ensuremath{\texttt{#1}[#2]}}
\newcommand{\Vel}[2]{\el{#1}{\var{#2}}}
% definitions for "operators" -- x(a, ...)
\newcommand{\op}[2]{\ensuremath{\texttt{#1}(#2)}}
\newcommand{\iop}[3]{\ensuremath{
\myfnname{#1}^{\displaystyle #2}(#3)
}}
\newcommand{\bigop}[2]{\ensuremath{\texttt{#1}\big(#2\big)}}
\newcommand{\Vop}[2]{\op{#1}{\var{#2}}}
% For a type name, when it occurs in text
\newcommand{\type}[1]{\ensuremath{#1}}
% For defining a type
\newcommand{\dtype}[1]{\type{#1}%
\index{recce-definitions}{#1 (type)}%
\index{recce-notation}{#1@\ensuremath{\var{x}_{#1}} (#1 type)}%
}
\newcommand{\defined}{\mathrel{\underset{\text{def}}{\equiv}}}
\newcommand{\xdfn}[2]{\textbf{#1}\index{recce-definitions}{#2}}
\newcommand{\dfn}[1]{\xdfn{#1}{#1}}
\newcommand{\qdfn}[1]{{\textbf{#1}}}
\newcommand{\sep}{\,\mid\,}
\newcommand{\mydot}{\raisebox{.05em}{$\,\bullet\,$}}
\newcommand{\cat}{}
\newcommand{\Cat}{\,.\,}
\newcommand{\size}[1]{\ensuremath{\left | {#1} \right |}}
\newcommand{\bigsize}[1]{\ensuremath{\bigl| {#1} \bigr|}}
\newcommand{\order}[1]{\ensuremath{{\mathcal O}(#1)}}
\newcommand{\Oc}{\order{1}}
\newcommand{\On}[1][]{%
\ifthenelse{\equal{#1}{}}%
{\ensuremath{\order{\var{n}}}}%
{\ensuremath{\order{\var{n}^{#1}}}}%
}
\newcommand{\LRk}{\ensuremath{\var{LR}(\var{k})}}
\newcommand{\inference}[2]{\genfrac{}{}{1pt}{}{#1}{#2}}
\newcommand{\undefined}{\ensuremath{\mathord{\perp}}}
% I use hyphens in variable names,
% so I need to ensure that subtraction is
% clearly distinguished by the typography
\newcommand{\subtract}{\,{}-\,}
\newcommand{\xxsubtract}[2]{\ensuremath{#1\,{}-\,#2}}
\newcommand{\decr}[1]{\xxsubtract{#1}{1}}
\newcommand{\Vdecr}[1]{\decr{\var{#1}}}
\newcommand{\incr}[1]{\ensuremath{#1 + 1}}
\newcommand{\Vincr}[1]{\incr{\var{#1}}}
\newcommand{\de}{\mathrel{::=}}
\newcommand{\derives}{\mapsto}
\newcommand{\nderives}[1]{
\mathrel{%
\ifthenelse{\equal{#1}{}}{%
{\not\mapsto}%
}{%
{\mbox{$\:\stackrel{\!{#1}}{\not\mapsto\!}\:$}}%
}%
}}%
\newcommand{\xderives}[1]
{\mathrel{\mbox{$\:\stackrel{\!{#1}}{\mapsto\!}\:$}}}
\newcommand{\destar}
{\mathrel{\mbox{$\:\stackrel{\!{\ast}}{\mapsto\!}\:$}}}
\newcommand{\ndestar}{\nderives{\ast}}
\newcommand{\deplus}
{\mathrel{\mbox{$\:\stackrel{\!{+}}{\mapsto\!}\:$}}}
\newcommand{\ndeplus}{\nderives{+}}
\newcommand{\derivg}[1]{\mathrel{\mbox{$\:\mapsto\:$}}}
\newcommand{\derivrg}[2]{\mathrel{\mbox{$\:\stackrel{\!{#1}}%
{\mapsto\!}\:$}}}
\newcommand{\drv}[3]{\ensuremath{\texttt{#1}[#2][#3]}}
\newcommand{\drvVV}[3]{\drv{#1}{\var{#2}}{\var{#3}}}
% \newcommand{\lblarrow}[1]
% {\mathrel{\mbox{$\:\stackrel{\!{#1}}{\mapsto\!}\:$}}}
\newcommand{\set}[1]{{\left\lbrace #1 \right\rbrace} }
\newcommand{\bigset}[1]{{\bigl\lbrace #1 \bigr\rbrace} }
\newcommand{\Bigset}[1]{{\Bigl\lbrace #1 \Bigr\rbrace} }
\newcommand{\dr}[1]{#1_{DR}}
\newcommand{\Vdr}[1]{\ensuremath{\var{#1}_{DR}}}
\newcommand{\Vdrset}[1]{\ensuremath{\var{#1}_{\set{DR}}}}
\newcommand{\eim}[1]{#1_{EIM}}
\newcommand{\Veim}[1]{\ensuremath{\var{#1}_{EIM}}}
\newcommand{\Veimset}[1]{\ensuremath{\var{#1}_{\set{EIM}}}}
\newcommand{\leo}[1]{#1_{LEO}}
\newcommand{\Vleo}[1]{\ensuremath{\var{#1}_{LEO}}}
\newcommand{\inst}[1]{#1_{INST}}
\newcommand{\Vinst}[1]{\ensuremath{\var{#1}_{INST}}}
\newcommand{\Ees}[1]{\ensuremath{#1_{ES}}}
\newcommand{\loc}[1]{\ensuremath{{#1}_{LOC}}}
\newcommand{\Vloc}[1]{\loc{\var{#1}}}
\newcommand{\Ves}[1]{\Ees{\var{#1}}}
\newcommand{\Vrule}[1]{\ensuremath{\var{#1}_{RULE}}}
\newcommand{\Vruleset}[1]{\ensuremath{\var{#1}_{\set{RULE}}}}
\newcommand{\Vsize}[1]{\ensuremath{\size{\var{#1}}}}
\newcommand{\str}[1]{\ensuremath{#1_{STR}}}
\newcommand{\Vstr}[1]{\ensuremath{\langle\langle\var{#1}\rangle\rangle}}
\newcommand{\sym}[1]{\ensuremath{#1_{SYM}}}
\newcommand{\Vsym}[1]{\ensuremath{\langle\var{#1}\rangle}}
\newcommand{\Vorig}[1]{\ensuremath{\var{#1}_{ORIG}}}
\newcommand{\symset}[1]{#1_{\lbrace SYM \rbrace} }
\newcommand{\Vsymset}[1]{\ensuremath{\var{#1}_{\set{SYM}}}}
\newcommand{\term}[1]{#1_{TERM}}
\newcommand{\mk}[1]{\ensuremath{\lfloor#1\rceil}}
\newcommand{\bigmk}[1]{\big\lfloor#1\big\rceil}
% \newcommand{\mkdelim}{\,\,}
\newcommand{\mkdelim}{}
\newcommand{\mkr}[1]{\mkdelim \mk{#1}}
\newcommand{\mkl}[1]{\mk{#1} \mkdelim}
\newcommand{\mkm}[1]{\mkdelim \mk{#1} \mkdelim}
\newcommand{\Vmk}[1]{\mk{\var{#1}}}
\newcommand{\Vmkl}[1]{\Vmk{#1} \mkdelim}
\newcommand{\Vmkr}[1]{\mkdelim \Vmk{#1}}
\newcommand{\Vmkm}[1]{\mkdelim \Vmk{#1} \mkdelim}
\newcommand{\lastix}[1]{\ensuremath{
% I want a hash sign to be a 'last index' operator, but
% the regular hash sign is ungainly and not resizeable.
% This tames it a bit.
{\protect\scalebox{0.8}{\protect\raisebox{0.4ex}{\#}}{#1}}
}}
\newcommand{\Vlastix}[1]{\lastix{\var{#1}}}
\newcommand{\alg}[1]{\ensuremath{\textsc{#1}}}
\newcommand{\Earley}{\ensuremath{\alg{Earley}}}
\newcommand{\Leo}{\ensuremath{\alg{Leo}}}
\newcommand{\Marpa}{\ensuremath{\alg{Marpa}}}
\newcommand{\Alt}[1]{\ensuremath{\myfnname{Alt}(#1)}}
\newcommand{\Cdr}{\var{dr}}
\newcommand{\Cg}{\var{g}}
\newcommand{\Crules}{\var{rules}}
\newcommand{\Current}[1]{\ensuremath{\myfnname{Current}(#1)}}
\newcommand{\CVw}[1]{\ensuremath{\sym{\Cw[\var{#1}]}}}
\newcommand{\CW}{\ensuremath{\mathbb W}}
\newcommand{\Cw}{\var{w}}
\newcommand{\Dotix}[1]{\ensuremath{\myfnname{Dotix}(#1)}}
\newcommand{\DR}[1]{\ensuremath{\myfnname{DR}(#1)}}
\newcommand{\GOTO}{\ensuremath{\myfnname{GOTO}}}
\newcommand{\ID}[1]{\myfnname{ID}(#1)}
\newcommand{\Layer}[1]{\ensuremath{\myfnname{Layer}(#1)}}
\newcommand{\Leosilo}[1]{\ensuremath{\myfnname{Leo-silo}(#1)}}
\newcommand{\Maximal}[1]{\ensuremath{\myfnname{Maximal}(#1)}}
\newcommand{\Maxleosilo}[2]{\ensuremath{\myfnname{Max-Leo-silo}(#1, #2)}}
\newcommand{\Left}[1]{\ensuremath{\myfnname{Left}(#1)}}
\newcommand{\LHS}[1]{\ensuremath{\myfnname{LHS}(#1)}}
\newcommand{\Memo}[1]{\ensuremath{\myfnname{Memo}(#1)}}
\newcommand{\Memoized}[1]{\ensuremath{\myfnname{Memoized}(#1)}}
\newcommand{\myL}[1]{\myfnname{L}(#1)}
\newcommand{\Next}[1]{\myfnname{Next}(#1)}
\newcommand{\Origin}[1]{\ensuremath{\myfn{Origin}{#1}}}
\newcommand{\Penult}[1]{\myfnname{Penult}(#1)}
\newcommand{\Postdot}[1]{\ensuremath{\myfnname{Postdot}(#1)}}
\newcommand{\Predict}[1]{\myfnname{Predict}(#1)}
\newcommand{\Predot}[1]{\ensuremath{\myfnname{Predot}(#1)}}
\newcommand{\Prev}[1]{\myfnname{Prev}(#1)}
\newcommand{\PSL}[2]{\myfnname{PSL}[#1][#2]}
\newcommand{\RHS}[1]{\ensuremath{\myfnname{RHS}(#1)}}
\newcommand{\Right}[1]{\ensuremath{\myfnname{Right}(#1)}}
\newcommand{\Rule}[1]{\ensuremath{\myfnname{Rule}(#1)}}
\newcommand{\Silo}[1]{\ensuremath{\myfnname{Silo}(#1)}}
\newcommand{\Symbol}[1]{\ensuremath{\myfnname{Symbol}(#1)}}
\newcommand{\Valid}[1]{\ensuremath{\myfnname{Valid}(#1)}}
\newcommand{\Instantiated}[1]{\ensuremath{\myfnname{Instantiated}(#1)}}
\newcommand{\PLSY}[1]{\ensuremath{\myfnname{PLSY}(#1)}}
\newcommand{\LSY}[1]{\ensuremath{\myfnname{LSY}(#1)}}
\newcommand{\SymEq}[1]{\ensuremath{\myfnname{Sym-Eq}(#1)}}
\newcommand\Ctables{\var{tables}}
\newcommand\Vtables[1]{\Ctables[\alg{#1}]}
\newcommand\Etable[1]{\ensuremath{\myfnname{table}[#1]}}
\newcommand\bigEtable[1]{\ensuremath{\myfnname{table}\bigl[#1\bigr]}}
\newcommand\Rtable[1]{\ensuremath{\myfnname{table}[#1]}}
\newcommand\Rtablesize[1]{\ensuremath{\bigl| \myfnname{table}[#1] \bigr|}}
\newcommand\Vtable[1]{\Etable{\var{#1}}}
\newcommand\EEtable[2]{\ensuremath{\myfnname{table}[#1,#2]}}
\newcommand\EVtable[2]{\EEtable{#1}{\var{#2}}}
% I want to use 'call' outside of pseudocode
\newcommand\mycallname[1]{\textproc{#1}}
\newcommand\call[2]{\mycallname{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
% I don't like to put whole paragraphs in italics,
% so I make this simple variation on the "plain" theoremstyle
\newtheoremstyle{myplain}
{10pt} % ABOVESPACE
{10pt} % BELOWSPACE
{\normalfont} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\bfseries} % HEADFONT
{.} % HEADPUNCT
{5pt plus 1pt minus 1pt} % HEADSPACE
{} % CUSTOM-HEAD-SPEC
\theoremstyle{myplain}
% \newtheorem{oldtheorem}{Theorem}[chapter]
\newtheorem{oldtheorem}{Theorem}
\newenvironment{theorem}
{
\begin{oldtheorem}
}
{
% \begin{center}
% \vspace{-.4\baselineskip}
% \rule{3em}{.5pt}
% \end{center}
\end{oldtheorem}
}
\newtheorem{lemma}[oldtheorem]{Lemma}
\newtheorem{baredefinition}[oldtheorem]{Definition}
\newenvironment{definition}{ \begin{baredefinition}
}{ \begin{center}
\nopagebreak[4]%
\vspace{-.4\baselineskip}%
\nopagebreak[4]%
\rule{3em}{.5pt}
\pagebreak[0]
\end{center} \end{baredefinition} }
\newtheorem{bareobservation}[oldtheorem]{Observation}
\newenvironment{observation}{ \begin{bareobservation}
}{ \begin{center}
\vspace{-.4\baselineskip}
\rule{3em}{.5pt}
\end{center} \end{bareobservation} }
\newtheorem{bareconstruction}[oldtheorem]{Construction}
\newenvironment{construction}{ \begin{bareconstruction}
}{ \begin{center}
\vspace{-.4\baselineskip}
\rule{3em}{.5pt}
\end{center} \end{bareconstruction} }
\newcommand{\padnum}[1]{%
\ifnum #1 < 10 0\fi%
\ifnum #1 < 100 0\fi%
\ifnum #1 < 1000 0\fi%
#1%
}
% My macros for definition & theorem titles and references
% The 'q' forms are 'quiet' -- they do not index
% Don't use definition titles for index -- use \xdfn instead.
\newcommand\dtitle[1]{\textbf{#1}:\index{recce-theorems}{%
\padnum{\theoldtheorem}@Definition \theoldtheorem. #1%
}}
% \newcommand\dtitle[1]{\textbf{#1}:}
\newcommand{\rawdref}[2]{%
\ifthenelse{\equal{#1}{}}%
{(D\ref{#2})}%
{(D\ref{#2} ``#1'')}%
}
\newcommand{\dref}[2][]{%
\ifmmode{\text{\rawdref{#1}{#2}}}%
\else{\rawdref{#1}{#2}}%
\fi%
}
% \newcommand\qdtitle[1]{\textbf{#1}:}
\newcommand\ttitle[1]{\textbf{#1}:\index{recce-theorems}{%
\padnum{\theoldtheorem}@Theorem \theoldtheorem. #1%
}
}
\newcommand\tref[1]{\text{(T\ref{#1})}}
\newcommand\lemref[1]{\text{(L\ref{#1})}}
\newcommand\ltitle[1]{\textbf{#1}:\index{recce-theorems}{%
\padnum{\theoldtheorem}@Lemma \theoldtheorem. #1%
}}
\newcommand{\algtitle}[2]{%
\caption{#1}
\label{#2}
\index{recce-algorithms}{%
Algorithm \padnum{\thealgorithm}@Algorithm \thealgorithm: #1%
}}
\newcommand{\figtitle}[2]{%
\caption{#1}
\label{#2}
\index{recce-algorithms}{%
Figure \padnum{\thefigure}@Figure \thefigure: #1%
}}
\newcommand{\becuz}{\ensuremath{\because}}
\hyphenation{oper-and oper-ands}
\hyphenation{look-ahead}
\hyphenation{memo-ization}
\hyphenation{al-ti-tude}
% I use parboxes in equations. This sets a useful width for them.
\newlength{\mathparwidth}
\newlength{\longtagwidth}
\settowidth{\longtagwidth}{(9999)\quad}
\setlength{\mathparwidth}{\dimexpr\textwidth-\longtagwidth}
\newcommand{\myparbox}[2][\mathparwidth]{%
\parbox[t]{#1}{%
\raggedright#2\par
\vspace{-\prevdepth} % remove the depth of the last line
\vspace{1ex} % add a fixed vertical space
}%
}
% \makeindex
\begin{document}
\title{The Marpa book}
\author{Jeffrey Kegler}
\thanks{%
Copyright \copyright\ 2015 Jeffrey Kegler.
}
\thanks{%
This document is licensed under
a Creative Commons Attribution-NoDerivs 3.0 United States License.
}
\thanks{\textbf{This is a draft.}}
\thanks{Date: \today}
% \begin{abstract}
% Marpa is
% a practical and fully implemented
% algorithm for the recognition,
% parsing and evaluation of context-free grammars.
% The Marpa recognizer is the first
% practical implementation
% of the improvements
% to Earley's algorithm found in
% Joop Leo's%
% \index{recce-general}{Leo, Joop}
% 1991 paper.
% Marpa has a new parse engine that
% allows the user to alternate
% between Earley-style syntax-driven parsing,
% and procedural parsing of the type available
% with recursive descent.
% Marpa is left-eidetic so that,
% unlike recursive descent,
% Marpa's procedural logic has available
% full information about
% the state of the parse so far.
% The Marpa recognizer described
% is a simplification of that
% in our 2013 paper~\cite{Marpa-2013}.
% \end{abstract}
\maketitle
\chapter{Status of this paper}
This paper is a draft.
Please use the date at the bottom of the
first page
to make sure this is the latest revision.
If this revision is not the latest, please ignore it.
If this does seems to be the latest version,
and you are adventurous,
then read on.
Chapters
\ref{ch:introduction},
\ref{ch:preliminaries},
\ref{ch:rewrite},
\ref{ch:dotted},
\ref{ch:earley-items},
\ref{ch:tethers}
and
\ref{ch:earley-tables}
are in the ``advanced draft'' stage.
Advanced draft chapters are still subject to revision,
but the author hopes they are stable enough
to make comments and corrections useful.
Readers should note that changes in early draft chapters
sometimes require changes
to chapters whose content was thought to
be settled.
Therefore, it is possible that
even chapters in advanced draft status
will change dramatically.
TODO: When done with this revision, update chapters
Chapters
\ref{ch:silos},
\ref{ch:leo},
\ref{ch:pseudocode},
\ref{ch:correctness},
and
\ref{ch:complexity}
are in \dfn{early draft} stage.
``Early draft'' means that the author's thoughts are not well settled,
and the chapters are likely to contain inconsistencies and errors.
Comments and corrections on early draft
chapters are not encouraged ---
the material may be already slated for deletion,
rewriting or rethinking.
\chapter{Introduction}
\label{ch:introduction}
The Marpa project was intended to create
a practical and highly available tool
to generate and use general context-free
parsers.
Tools of this kind
had long existed
for LALR~\cite{Johnson} and
regular expressions.
But, despite an encouraging academic literature,
no such tool had existed for context-free parsing.
The first stable version of Marpa was uploaded to
a public archive on Solstice Day 2011.
This monograph describes the algorithm used
in the most recent version of Marpa,
Marpa::R2~\cite{Marpa-R2}.
It is a simplification of the algorithm presented
in our earlier paper~\cite{Marpa-2013}.
\section{A proven algorithm}
While the presentation in this monograph is theoretical,
the approach is practical.
The Marpa::R2 implementation has been widely available
for some time,
and has seen considerable use,
including in production environments.
Many of the ideas in the parsing literature
satisfy theoretical criteria,
but in practice turn out to face significant obstacles.
An algorithm may be as fast as reported, but may turn
out not to allow
adequate error reporting.
Or a modification may speed up the recognizer,
but require additional processing at evaluation time,
leaving no advantage to compensate for
the additional complexity.
In this monograph, we describe the Marpa
algorithm
as it was implemented for Marpa::R2.
In many cases,
we believe there are better approaches than those we
have described.
But we treat these techniques,
however solid their theory,
as conjectures.
Whenever we mention a technique
that was not actually implemented in
Marpa::R2,
we will always explicitly state that
that technique is not in Marpa as implemented.
\section{Features}
\subsection{General context-free parsing}
As implemented,
Marpa parses
all ``proper'' context-free grammars.
The
proper context-free grammars are those which
are free of cycles,
unproductive symbols,
and
inaccessible symbols.
Worst case time bounds are never worse than
those of Earley~\cite{Earley1970},
and therefore never worse than $\order{\var{n}^3}$.
\subsection{Linear time for practical grammars}
Currently, the grammars suitable for practical
use are thought to be a subset
of the deterministic context-free grammars.
Using a technique discovered by
Leo~\cite{Leo1991},
Marpa parses all of these in linear time.
Leo's modification of Earley's algorithm is
\On{} for LR-regular grammars.
Leo's modification
also parses many ambiguous grammars in linear
time.
\subsection{Left-eidetic}
The original Earley algorithm kept full information
about the parse ---
including partial and fully
recognized rule instances ---
in its tables.
At every parse location,
before any symbols
are scanned,
Marpa's parse engine makes available
its
information about the state of the parse so far.
This information is
in useful form,
and can be accessed efficiently.
\subsection{Recoverable from read errors}
When
Marpa reads a token which it cannot accept,
the error is fully recoverable.
An application can try to read another
token.
The application can do this repeatedly
as long as none of the tokens are accepted.
Once the application provides
a token that is accepted by the parser,
parsing will continue
as if the unsuccessful read attempts had never been made.
\subsection{Ambiguous tokens}
Marpa allows ambiguous tokens.
These are often useful in natural language processing
where, for example,
the same word might be a verb or a noun.
Use of ambiguous tokens can be combined with
recovery from rejected tokens so that,
for example, an application could react to the
rejection of a token by reading two others.
\section{Using the features}
\subsection{Error reporting}
An obvious application of left-eideticism is error
reporting.
Marpa's abilities in this respect are
ground-breaking.
For example,
users typically regard an ambiguity as an error
in the grammar.
Marpa, as currently implemented,
can detect an ambiguity and report
specifically where it occurred
and what the alternatives were.
\subsection{Event driven parsing}
As implemented,
Marpa::R2~\cite{Marpa-R2}
allows the user to define ``events''.
Events can be defined that trigger when a specified rule is complete,
when a specified rule is predicted,
when a specified symbol is nulled,
when a user-specified lexeme has been scanned,
or when a user-specified lexeme is about to be scanned.
A mid-rule event can be defined by adding a nulling symbol
at the desired point in the rule,
and defining an event which triggers when the symbol is nulled.
\subsection{Ruby slippers parsing}
Left-eideticism, efficient error recovery,
and the event mechanism can be combined to allow
the application to change the input in response to
feedback from the parser.
In traditional parser practice,
error detection is an act of desperation.
In contrast,
Marpa's error detection is so painless
that it can be used as the foundation
of new parsing techniques.
For example,
if a token is rejected,
the lexer is free to create a new token
in the light of the parser's expectations.
This approach can be seen
as making the parser's
``wishes'' come true,
and we have called it
``Ruby Slippers Parsing''.
One use of the Ruby Slippers technique is to
parse with a clean
but oversimplified grammar,
programming the lexical analyzer to make up for the grammar's
short-comings on the fly.
As part of Marpa::R2~\cite{Marpa-R2},
the author has implemented an HTML parser,
based on a grammar that assumes that all start
and end tags are present.
Such an HTML grammar is too simple even to describe perfectly
standard-conformant HTML,
but the lexical analyzer is
programmed to supply start and end tags as requested by the parser.
The result is a simple and cleanly designed parser
that parses very liberal HTML
and accepts all input files,
in the worst case
treating them as highly defective HTML.
\subsection{Ambiguity as a language design technique}
In current practice, ambiguity is avoided in language design.
This is very different from the practice in the languages humans choose
when communicating with each other.
Human languages exploit ambiguity in order to design highly flexible,
powerfully expressive languages.
For example,
the language of this monograph, English, is notoriously
ambiguous.
Ambiguity of course can present a problem.
A sentence in an ambiguous
language may have undesired meanings.
But note that this is not a reason to ban potential ambiguity ---
it is only a problem with actual ambiguity.
Syntax errors, for example, are undesired, but nobody tries
to design languages to make syntax errors impossible.
A language in which every input was well-formed and meaningful
would be cumbersome and even dangerous:
all typos in such a language would be meaningful,
and parser would never warn the user about errors, because
there would be no such thing.
With Marpa, ambiguity can be dealt with in the same way
that syntax errors are dealt with in current practice.
The language can be designed to be ambiguous,
but any actual ambiguity can be detected
and reported at parse time.
This exploits Marpa's ability
to report exactly where
and what the ambiguity is.
Marpa::R2 own parser description language, the SLIF,
uses ambiguity in this way.
\subsection{Auto-generated languages}
\cite[pp. 6-7]{Culik1973} points out that the ability
to efficiently parse LR-regular languages
opens the way to auto-generated languages.
In particular,
\cite{Culik1973} notes that a parser which
can parse any LR-regular language will be
able to parse a language generated using syntax macros.
\subsection{Second order languages}
In the literature, the term ``second order language''
is usually used to describe languages with features
which are useful for second-order programming.
True second-order languages --- languages which
are auto-generated
from other languages ---
have not been seen as practical,
since there was no guarantee that the auto-generated
language could be efficiently parsed.
With Marpa, this barrier is raised.
As an example,
Marpa::R2's own parser description language, the SLIF,
allows ``precedenced rules''.
Precedenced rules are specified in an extended BNF.
The BNF extensions allow precedence and associativity
to be specified for each RHS.
Marpa::R2's precedenced rules are implemented as
a true second order language.
The SLIF representation of the precedenced rule
is parsed to create a BNF grammar which is equivalent,
and which has the desired precedence.
Essentially,
the SLIF does a standard textbook transformation.
The transformation starts
with a set of rules,
each of which has a precedence and
an associativity specified.
The result of the transformation is a set of
rules in pure BNF.
The SLIF's advantage is that it is powered by Marpa,
and therefore the SLIF can be certain that the grammar
that it auto-generates will
parse in linear time.
Notationally, Marpa's precedenced rules
are an improvement over
similar features
in LALR-based parser generators like
yacc or bison.
In the SLIF,
there are two important differences.
First, in the SLIF's precedenced rules,
precedence is generalized, so that it does
not depend on the operators:
there is no need to identify operators,
much less class them as binary, unary, etc.
This more powerful and flexible precedence notation
allows the definition of multiple ternary operators,
and multiple operators with arity above three.
Second, and more important, a SLIF user is guaranteed
to get exactly the language that the precedenced rule specifies.
The user of the yacc equivalent must hope their
syntax falls within the limits of LALR.
\section{How to read this document}
TODO: When done with this revision, update chapters
Chapter
\ref{ch:preliminaries} describes the notation and conventions
of this monograph.
Chapter \ref{ch:rewrite} deals with Marpa's
grammar rewrites.
The next three sections develop the ideas for Earley's algorithm.
Chapter \ref{ch:dotted} describes dotted rules.
Chapter \ref{ch:earley-items} describes Earley items.
Chapter \ref{ch:tethers} introduces tethers,
which are chains of top-down causation.
Chapter \ref{ch:earley-tables} describes
the remaining ideas behind
basic Earley implementations,
including
Earley sets
and Earley tables.
Chapter \ref{ch:silos} introduces silos.
Like tethers, silos are chains of causation,
but unlike tethers, in silos the causation is
largely bottom-up.
Chapter \ref{ch:leo} describes Leo's modification
to Earley's algorithm.
Chapter \ref{ch:pseudocode} presents the pseudocode
for Marpa's recognizer.
Chapter
\ref{ch:correctness}
contains a proof of Marpa's correctness.
Chapter \ref{ch:complexity} sets out our
time and space complexity results.
Because of its practical applications,
we expect this monograph to be of interest to many
who do not ordinarily read documents with this
level of mathematical apparatus.
For those readers, we offer some suggestions
which will be well known to our more mathematical
readers.
In most fields, texts are intended to be,
and often are, read through to the end,
starting at page one.
A monograph of this kind is rarely
read that way.
Even the most mathematical sophisticated
reader will skip most or all
of the proofs
on a first reading.
And a mathematically inclined
reader will usually
not read a proof line-by-line
unless and until her previous readings
have convinced her that the proof
is of sufficient
interest to deserve this kind of
attention.
This is not to say that we think
that the proofs are unimportant.
The proofs explore how our ideas
and claims
are connected to each other.
There is a aesthetic satisfaction in
this deeper level of knowledge.
And the proofs
increase our
assurance that our claims are, in fact, true.
But the proofs are also of practical use,
even to the programmer who is willing to
take our word for everything in these pages.
If, when coding,
the programmer only knows ``what'' and ``how'',
he will find it hard to keep all of this material
in his mind at once.
If the programmer knows,
not just ``what'' and ``how'',
but also ``why'',
he will understand the connections among
these ideas.
When the programmer
understands ``why'',
the book is always open in front of him,
turned to whatever page it is that
he needs.
We expect
most readers of this monograph
to have a practical bent.
For those readers,
one way to start is to read the preliminary
material for as long as it seems relevant,
skipping the lemmas,
as well as the proofs for both lemmas and
theorems.
When impatience for something ``closer to
the metal'' arises,
this reader should
jump ahead
to the pseudocode in
Chapter \ref{ch:pseudocode}.
\chapter{Preliminaries}
\label{ch:preliminaries}
We assume familiarity with the theory of parsing.
Earley's algorithm is described in full,
but previous familiarity will be helpful.
\section{Notation}
This monograph will
use subscripts to indicate commonly occurring types.
\begin{center}
\begin{tabular}{ll}
$\var{X}_T$ & The variable \var{X} of type \type{T} \\
$\var{set-one}_\set{T}$ & The variable \var{set-one} of type set of \type{T} \\
\type{SYM} & The type for a symbol \\
\type{STR} & The type for a string \\
\type{EIM} & The type for an Earley item \\
\sym{\var{a}} & A variable \var{a} of type \type{SYM} \\
\str{\var{a}} & A variable \var{a} of type \type{STR} \\
\Veim{a} & A variable \var{a} of type \type{EIM} \\
\Vsymset{set-two} & The variable \var{set-two}, a set of strings \\
\Veimset{set-two} & The variable \var{set-two}, a set of Earley items
\end{tabular}
\end{center}
Strings and symbols occur frequently and have a special
notation:
\begin{center}
\begin{tabular}{ll}
\Vsym{a} & \var{a}, a symbol variable \\
\Vstr{a} & \var{a}, a string variable
\end{tabular}
\end{center}%
\index{recce-notation}{<<sym>>@\Vstr{sym}}%
\index{recce-notation}{<str>@\Vsym{sym}}
Subscripts may be omitted when the type
is obvious from the context.
The notation for
constants is the same as that for variables.
Multi-character variable names will be common.
Concatenation is shown
only when useful for clarity.
All other operations are always explicit.
\begin{center}
\begin{tabular}{ll}
Multiplication & $\var{a} \times \var{b}$ \\
Concatenation & $\var{a} \Cat \var{b}$ \\
Subtraction & $\var{symbol-count} \subtract \var{terminal-count}$ \\
\end{tabular}
\end{center}
Where $\myfnname{f}$ is a function,
we use the notation $\myfnname{f}^{\displaystyle \var{n}}$
for the iterated function, so that
\begin{align*}
\myfnname{f}^0(\var{x}) \quad & \defined \quad \var{x}, \\
\myfnname{f}^1(\var{x}) \quad & \defined \quad \myfnname{f}(\var{x}), \\
\myfnname{f}^2(\var{x}) \quad & \defined \quad
\myfnname{f}(\myfnname{f}(\var{x})), \quad \text{etc.} \\
\text{Also,} \quad \myfnname{f}^\ast \quad & \defined \quad
\myfnname{f}^\var{n} \quad \text{for some $\var{n} \ge 0$ and} \\
\myfnname{f}^+ \quad & \defined \quad
\myfnname{f}^\var{n} \quad \text{for some $\var{n} \ge 1$}.
\end{align*}
The statements of this monograph often require us to introduce
many new variables at once,
so that we might say,
``for some \var{a}, \var{b}, \var{c}, \ldots{} \var{z},
let \ldots{}''.
When we introduce an definition, and it
contains new variables
which cause no loss of generality,
we will prefer to simply say so,
noting any exceptions.
In cases where brevity is important,
such as in proofs,
we may abbreviate
``without loss of generality'' as \dfn{WLOG},
``assumption'' as \dfn{ASM},
``theorem'' as \dfn{Th},
and
``definition'' as \dfn{Def}.
We use the standard notation for equations,
writing
``(\textit{n})'' to refer to equation \textit{n}.%
\index{recce-notation}{(0)@(\textit{n})}
To indicate references, we write,
where \var{n} is a reference number,
(L\textit{n})%
\index{recce-notation}{(L0)@(L\textit{n})}%
\index{recce-notation}{L0@(L\textit{n})}
to say Lemma \textit{n},
(T\textit{n})%
\index{recce-notation}{(T0)@(T\textit{n})}%
\index{recce-notation}{T0@(T\textit{n})}
to say Theorem \textit{n}
and
(D\textit{n})%
\index{recce-notation}{(D0)@(D\textit{n})}%
\index{recce-notation}{D0@(D\textit{n})}
to say Definition \textit{n}.
A definition reference may define several terms:
when we wish to pinpoint one of these we write
the reference
(D\textit{n} ``\textit{x}'')%
\index{recce-notation}{(D0 "x")@(D\textit{n} ``\textit{x}'')}%
\index{recce-notation}{D0 x@(D\textit{n} ``\textit{x}'')}
to say the definiton
of ``\textit{n}'' in Definition section
with reference number \textit{n}.
For example, we would write
\[
\begin{aligned}
& \text{(D42) to say Definition 42;} \\
& \text{(D7 ``\var{x}'') to say the definition of ``x'' in Definition section 7;} \\
& \text{(T11) to say Theorem 11; and} \\