summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/flagderiv/flagderiv.dtx
blob: c9025c73e2e5782e4c2b6ecff2cc002244059c3c (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
% \iffalse meta-comment
%
% Copyright (C) 2005 by Paul van Tilburg <paul@luon.net>
% ------------------------------------------------------
% 
% This package is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
% 
% This package is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this package; if not, write to the Free Software
% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.%
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{flagderiv.dtx}
%</driver>
%<flagderiv>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<flagderiv>\ProvidesPackage{flagderiv}
%<*flagderiv>
    [2005/08/26 v0.10 Flag style derivation package]
%</flagderiv>
%
%<*driver>
\documentclass{ltxdoc}

\usepackage{amsmath, amssymb}
\usepackage{calc}
\usepackage{flagderiv}
\usepackage{fvrb-ex}
\usepackage{theorem}
\usepackage{url}

\setlength{\parindent}{0pt}
\setlength{\parskip}{\medskipamount}
\setcounter{tocdepth}{2}

\theoremstyle{plain}
\newtheorem{lemma}{Lemma}
\newcommand{\false}{\textit{False}}
\newcommand{\flags}{\texttt{flagderiv}}
\newcommand{\IHbase}{\underline{\textit{Base}}:~}
\newcommand{\IHhyp}{\underline{\textit{Induction-hypothesis}}:~}
\newcommand{\IHstep}{\underline{\textit{Step}}:~}
\newcommand{\Nat}{\ensuremath{\mathbb{N}}}
\newcommand{\All}{\ensuremath{\mathbb{Z}}}
\newcommand{\Powerset}{\ensuremath{\mathcal{P}}}

\renewcommand{\implies}{\Rightarrow}
\newcommand{\biimplies}{\Leftrightarrow}
\newcommand{\OHA}{\rightarrow}
\newcommand{\THA}{\twoheadrightarrow}
\newcommand{\NHA}[1]{\stackrel{#1}{\rightarrow}}
\newcommand{\CRtext}{\ensuremath{\mathit{CR}}}
\newcommand{\CR}{\ensuremath{(\forall a, b, c\in A: a\THA b\land a\THA c:%
                             (\exists d\in A:: b\THA d\land c\THA d))}}
\newcommand{\MCtext}{\ensuremath{\mathit{mixed~confluent}}}
\newcommand{\MC}{\ensuremath{(\forall a, b, c\in A: a\OHA b\land a\THA c:%
                             (\exists d\in A:: b\THA d\land c\THA d))}}

\EnableCrossrefs         
\CodelineIndex
\RecordChanges

\begin{document}
  \DocInput{flagderiv.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.9f}{2005/02/04}{Initial version.}
% \changes{v0.9g}{2005/05/16}{Added example and usage documentation.}
% \changes{v0.9h}{2005/06/06}{Added the flagderiv* environment.}
% \changes{v0.9i}{2005/06/10}{Fixed minor issues in the documentation
%                        and resolved a ref-format bug.}
% \changes{v0.10}{2005/08/26}{Drop dependancy on farray, include fix.}
%
% \GetFileInfo{flagderiv.dtx}
%
% \DoNotIndex{\newcommand,\newenvironment}
% 
% \title{The \textsf{flagderiv} package\thanks{This document
%   corresponds to \textsf{flagderiv}~\fileversion, dated \filedate.}}
% \author{Paul van Tilburg \\ \url{paul@luon.net}}
%
% \maketitle
%
% \tableofcontents
%
% \section{Introduction}
%
%  This document describes how to use \flags~to make flag-style proofs in
%  \LaTeX.
%
%  It can handle taking steps, making assumptions, introducing variables and
%  conclusions.  The package allows the style to be configured.  For
%  example, the variable introduction symbol can be redefined.  Other
%  features include multiple page flag-style proofs.
%
%  The documentation and all examples are based upon version \fileversion\ 
%  of \flags.
%
%  \begin{center}
%   \scriptsize
%     \renewcommand{\introsymb}{\text{Let}}
%     \begin{flagderiv}
%      \assume{i:assume:no-package}{\text{There is no nice package to do flag-style proofs}}{}
%       \introduce{i:intro:feature}{x\text{, such that } x\text{ is a flag-style feature}}{}
%        \skipsteps{5}{\vdots}{}
%        \step{i:feature-met}{\flags~\text{can do it}}{}
%       \conclude{i:conclusion}{\flags~\text{fulfils my every flag-style proof need}}
%                              {$\forall$-introduction on \ref{i:intro:feature} and \ref{i:feature-met}}
%       \step{i:false}{\false}{\false-introduction on \ref{i:assume:no-package} and \ref{i:conclusion}}
%      \conclude{i:conclude:not-no-package}{\lnot (\text{There is no nice package to do flag-style proofs})}
%                                          {$\lnot$-introduction on \ref{i:assume:no-package} and \ref{i:false}}
%      \step{i:step:nice-package}{\text{So, there exists a nice package to do flag-style proofs}}
%                                {$\lnot\lnot$-elimination on \ref{i:conclude:not-no-package}}
%     \end{flagderiv}
%  \end{center}
%
% \section{Acknowledgements}
%
% I want to thank Jan Zwanenburg and Erik Poll for the first versions of
% this package, professor R.P.\ Nederpelt for his ever-persisting package
% reviews, feedback and suggestions and last but not least Mark van Eijk,
% who has written parts of section~\ref{sec:usage} and all examples in
% section~\ref{sec:ex}.
%
% \section{Usage\label{sec:usage}}
%
%  This section explains how the \flags\ package works by means of examples 
%  and how the commands should be used. I try to cover all possibilities and
%  features of this package. For bigger examples, please refer to
%  section~\ref{sec:ex}; for more detailed documentation on the commands
%  and inner workings, refer to the \flags\ package itself.
% 
%   \subsection{Basic derivations}
%    \DescribeEnv{flagderiv}
%    Basic derivations are written using an intuitive command sequence
%    placed in the |flagderiv| environment\index{usage}. The example below
%    uses auto-numbering of the lines. Variable introductions, assumptions, 
%    steps and conclusions can be done using the following commands 
%    respectively:
%
%    \begin{itemize}
%     \item \DescribeMacro{\introduce}\index{usage}
%           |\introduce|\marg{label}\marg{formula}\marg{comment}, 
%     \item \DescribeMacro{\assume}\index{usage}
%           |\assume|\marg{label}\marg{formula}\marg{comment},
%     \item \DescribeMacro{\step}\index{usage}
%           |\step|\marg{label}\marg{formula}\marg{comment}, and 
%     \item \DescribeMacro{\conclude}\index{usage}
%           |\conclude|\oarg{number}\marg{label}\marg{formula}\marg{comment}.
%    \end{itemize}
% 
%    Lines can be referred to using the {\em label} that is provided as the
%    first argument to each of the commands. {\em Formulas} are placed in
%    the second argument and a {\em comment} in the third. 
%    See subsection~\ref{sec:usage:multi} for the explanation of the optional
%    argument {\em number} of |\conclude|.
% 
%    \begin{Example}[gobble=7,frame=single,label=Auto-numbering]
%      \begin{flagderiv}
%        \introduce{in-x}{x: \Nat}{Introduction of $x$}
%          \assume{as-x}{x > 5}{Assumption}
%            \step{big-x}{x > 1}{Arithmetic on \ref{in-x} and \ref{as-x}}
%          \conclude{conc}{x > 5 \implies x > 1}
%                         {$\implies$-intro on \ref{as-x} and \ref{big-x}}
%        \conclude{}{\forall x \in \Nat: x > 5 \implies x > 1}
%                   {$\forall$-intro on \ref{in-x} and \ref{conc}}
%      \end{flagderiv}
%    \end{Example}
% 
%    The following example uses manual numbering. This is done by using the
%    \DescribeMacro{\introduce*}
%    starred versions of the previous given commands (|\introduce*|,
%    \DescribeMacro{\assume*}
%    \DescribeMacro{\step*}
%    \DescribeMacro{\conclude*}
%    |\assume*|, |\step*| and |\conclude*|) and using a
%    {\em custom number} instead of a {\ em label} as the first argument. 
%    Note that because of this, derivation line references are not possible
%    and should be done manually as well.
% 
%    \begin{Example}[gobble=7,frame=single,label=Manual numbering]
%      \begin{flagderiv}
%        \introduce*{$(1)$}{x: \Nat}{Introduction of $x$}
%          \assume*{$(2)$}{P(x)}{Assumption}
%            \skipsteps*{\dots}{}
%            \step*{$(n - 2)$}{Q(x)}{}
%          \conclude*{$(n - 1)$}{P(x) \implies Q(x)}
%                               {$\implies$-intro on $(2)$ and $(n - 2)$}
%        \conclude*{$(n)$}{\forall x \in \Nat: P(x) \implies Q(x)}
%                         {$\forall$-intro on $(1)$ and $(n - 1)$}
%      \end{flagderiv}
%    \end{Example}
% 
%    Note that the |\skipsteps*| used in the previous example is
%    explained in section~\ref{sec:usage:skipsteps}.
% 
%   \subsection{Concluding multiple assumptions/introductions\label{sec:usage:multi}}
%    Sometimes it is useful to make a conclusion from multiple open
%    assumptions and/or introductions at once. This can be done by supplying
%    an optional {\em number} to the |\conclude| command. Usage is
%    illustrated in the example below.
% 
%    \begin{Example}[gobble=7,frame=single,label=Multiple conclusion]
%      \begin{flagderiv} 
%        \introduce{}{x: \Nat}{}
%          \assume{}{P(x)}{}
%            \step{}{Q(x)}{}
%        \conclude[2]{}{\forall x \in \Nat: (P(x) \implies Q(x))}{}
%      \end{flagderiv}
%    \end{Example}
% 
%   \subsection{Skipping steps\label{sec:usage:skipsteps}}
%    When writing example derivations or abbreviating those where
%    obvious/trivial steps may be skipped, one can use the
%    \DescribeMacro{\skipsteps}
%    |\skipsteps|\marg{number}\marg{formula}\marg{comment} command for
%    auto-numbered derivations.  This commands takes the {\em number} of
%    steps to skip as the first argument, a {\em formula} as the second and
%    a {\em comment} as the third, like a normal step.
%    \vspace{3em}
%    
%    \begin{Example}[gobble=7,frame=single,label=Skipping 5 steps]
%      \begin{flagderiv}
%        \assume{as}{P \implies Q}{}
%          \step{lem-p}{P \lor \neg P}{LEM}
%          \skipsteps{5}{\dots}
%                       {Prove $\neg P \lor Q$ for $P$ and $\neg P$}
%          \step{or-elm}{\neg P \lor Q}{$\lor$-elim on \ref{lem-p}}
%      \end{flagderiv}
%    \end{Example}
% 
%    For skipping an unknown number of steps in a manual numbered
%    \DescribeMacro{\skipsteps*}
%    derivation, the command |\skipsteps*|\marg{formula}\marg{comment} can
%    be used.  Here the number of steps to skip must be omitted, since there
%    is no counting involved.  See for example the $\implies$-intro rule:
% 
%    \begin{Example}[gobble=7,frame=single,label=Skipping steps]
%      \begin{flagderiv}
%        \assume*{$(1)$}{P}{Assumption}
%          \skipsteps*{\dots}{Derive $Q$}
%          \step*{$(n - 1)$}{Q}{}
%        \conclude*{$(n)$}{P \implies Q}
%                         {$\implies$-intro on $(1)$ to $(n - 1)$}
%      \end{flagderiv}
%    \end{Example}
% 
%   \subsection{Closing assumptions/introductions}
%    Assumptions and introductions (contexts) are not always closed by a
%    conclusion, it is sometimes useful to keep proven propositions within
%    \DescribeMacro{\done}
%    its context.  The |\done| command (|\done|\oarg{number}) can be used to
%    close a assumption and/or introduction without mentioning why.  It takes
%    an optional argument {\em number} to close multiple open assumptions
%    and/or introductions.  An example from the WTT (Weak Type Theory):
% 
%    \begin{Example}[gobble=7,frame=single,label=Closing assumptions/introductions]
%      \begin{flagderiv}
%        \introduce{}{n: \Nat}{}
%          \introduce{}{d: \Nat}{}
%            \step{}{\text{$d$ is a divisor of $n$} := 
%                     \exists_{k: \Nat} (n = d \cdot k)}{}
%          \done
%          \introduce{}{m: \Nat}{}
%            \step{}{\text{$m$ is a multiple of $n$} := 
%                     \exists_{k: \Nat} (m = k \cdot n)}{}
%        \done[2]
%      \end{flagderiv}
%    \end{Example}
%
%    \vspace{-1em}
%    The last |\done[2]| can be omitted since closing the \flags\ environment
%    implicitly closes all open introductions and assumptions.
%
% 
%   \subsection{Spacing between lines}
%    The spacing between derivation lines defaults to {\tt 8pt} but can be 
%    changed
%    \DescribeMacro{derivskip}\index{setting}
%    by adjusting the length (using |\setlength| on {\tt derivskip})
%    before starting or during a \flags\ environment. See for example the
%    more condensed proof:
% 
%    \begin{Example}[gobble=7,frame=single,label=Condensing a derivation]
%      \setlength{\derivskip}{4pt}
%      \begin{flagderiv}
%        \assume{}{P}{}
%          \assume{}{Q}{}
%            \step{}{P}{}
%          \conclude{}{Q \implies P}{}
%        \conclude{}{P \implies Q \implies P}{}
%      \end{flagderiv}
%    \end{Example}
% 
%    Note that flag derivations will automatically be split across pages if
%    it is too long and the |flagderiv| environment is used.  See also the
%    {\tt longtable} package.  This does not happen for |flagderiv*|, which
%    is a normal |tabular|.
% 
%   \subsection{The introduction symbol}
%    \DescribeMacro{\introsymb}\index{changing}
%    The way an introduction flag differs from an assumption flag is that it
%    is prefixed with an {\em introduction symbol}. This symbol is by
%    default set to $\mathbf{var}$ (|\mathbf{var}|) but can be changed
%    to a different symbol, for example to $\leadsto$ (|\leadsto|) or
%    just `Let'. This can be changed by redefining the |\introsymb|
%    command before opening the \flags\ environment.
% 
%    \begin{Example}[gobble=7,frame=single,label=Changing the introduction symbol]
%      \renewcommand{\introsymb}{\textbf{Let}}
%      \begin{flagderiv}
%        \introduce{intro-x}{x: \Nat}{Introduction}
%          \introduce{intro-y}{y: \Nat}{Introduction}
%            \skipsteps*{\ddots}{}
%      \end{flagderiv}
%    \end{Example}
% 
%   \subsection{Numbering and format}
%    When the derivation lines are automatically numbered the numbers are
%    formatted as ``({\em number})'' and so are the references. This format
%    \DescribeMacro{fd@stepcount}
%    \DescribeMacro{\thestepcount}\index{changing}
%    can be changed by overriding the |\thestepcount| command using the
%    {\tt fd@stepcount} counter before opening the \flags\ environment.
% 
%    \begin{Example}[gobble=7,frame=single,label=Changing the numbering format]
%      \renewcommand{\thestepcount}{[\Roman{fd@stepcount}]}
%      \begin{flagderiv}
%        \step{ass}{P \implies Q}{}
%        \step{lem}{P \lor \neg P}{LEM}
%        \assume{ass-nP}{\neg P}{Assumption}
%          \step{s0}{\neg P \lor \neg Q}{$\lor$-intro on \ref{ass-nP}}
%        \done
%        \assume{ass-nnP}{P}{Assumption}
%          \step{s1}{Q}{$\implies$-elim on \ref{ass-nnP} and \ref{ass}}
%          \step{s2}{\neg P \lor \neg Q}{$\lor$-intro on \ref{s1}}
%        \done
%        \step{or-el}{\neg P \lor Q}
%                    {$\lor$-elim on \ref{lem}, \ref{s0} and \ref{s2}}
%      \end{flagderiv}
%    \end{Example}
% 
%    \DescribeMacro{\thesteplabel}\index{changing}
%    Since the manual numbered derivations use no counter, the format can be
%    changed by overriding the |\thesteplabel| command which will take
%    one argument, the {\em custom number}.
% 
%    \begin{Example}[gobble=7,frame=single,label=Changing the numbering format]
%      \renewcommand{\thesteplabel}[1]{#1.}
%      \begin{flagderiv}
%        \assume*{1}{P}{Assumption}
%          \skipsteps*{\dots}{Derive $Q$}
%          \step*{n - 1}{Q}{}
%        \conclude*{n}{P \implies Q}{$\implies$-intro on 1 to n - 1}
%      \end{flagderiv}
%    \end{Example}
% 
%    Note that since references are also manually done, the same format
%    should be used there by the writer him/herself.
% 
%   \subsection{Inline comments}
%    In general there are two ways to place the comments with respect to
%    the formulas in derivations. The first is behind the formula (the
%    default). The second way is to place them inline, that is, on a separate 
%    line before the formula.
% 
%    All \flags\ environments can be set globally to display the comments
%    inline by passing the global option {\tt inlcmnts} to the \flags\
%    package (e.g.\\|\usepackage[inlcmnts]{flagsderiv}|).
% 
%    \DescribeMacro{\inlcmnts}\index{usage}
%    Giving the |\inlcmnts| command makes the environments after this
%    command explicitly switch into inline-comment-mode (the default or
%    \DescribeMacro{\noinlcmnts}\index{usage}
%    global option is forgotten) after which giving |\noinlcmnts| will set 
%    it back to normal mode.
% 
%    \begin{Example}[gobble=7,frame=single,label=Inline comments]
%      \inlcmnts
%      \begin{flagderiv}
%        \assume{as-P}{P}{Assume:}
%          \assume{as-Q}{Q}{Assume:}
%            \step{re-P}{P}{Rei \ref{as-P}:}
%         \conclude{imp1}{Q \implies P}
%                        {$\implies$-intro on \ref{as-Q} and \ref{re-P}:}
%        \conclude{imp2}{P \implies Q \implies P}
%                        {$\implies$-intro on \ref{as-P} and \ref{imp1}:}
%      \end{flagderiv}
%    \end{Example}
% 
%    When the comments are inserted inline, a certain format is used, namely
%    to enclose the comment with braces.  This is the default, but can be
%    \DescribeMacro{\theinlcmnt}
%    overridden by redefining the |\theinlcmnt| command.\index{override}
%    \vspace{1em}
% 
%    \begin{Example}[gobble=7,frame=single,label=Changing inline comments format]
%      \inlcmnts
%      \renewcommand{\theinlcmnt}[1]{--#1--}
%      \begin{flagderiv}
%        \assume{ass-P}{P}{Assumption}
%          \assume{ass-Q}{Q}{Assumption}
%            \step{rei-P}{P}{Rei \ref{ass-P}}
%      \end{flagderiv}
%    \end{Example}
% 
%   \subsection{Namespaces for labels}
%    In a document with a lot of auto-numbered derivations it is easy to lose
%    overview of labels already used or it may be hard to keep thinking of
%    new ones. To solve this a \flags\ environment can take a {\em namespace}
%    as optional argument\index{namespacing}. This makes the lines referable 
%    as normal within the same derivation but also globally when the namespace
%    and a colon is added outside the derivation. For example a derivation for 
%    Modus Tollens (MT):
% 
%    \begin{Example}[gobble=7,frame=single,label=Derivation label namespace]
%      \begin{flagderiv}[mt]
%        \step{s0}{P \implies Q}{}
%        \assume{as-nQ}{\neg Q}{Assumption}
%          \assume{as-P}{P}{Assumption}
%            \step{s1}{Q}{$\implies$-elim on \ref{s0} and \ref{as-P}}
%            \step{s2}{\bot}{$\bot$-intro on \ref{as-nQ} and \ref{s1}}
%          \conclude{nP}{\neg P}{$\neg$-intro on \ref{as-P} and \ref{s2}} 
%        \conclude{nQ-nP}{\neg Q \implies \neg P}
%                        {$\implies$-intro on \ref{as-nQ} and \ref{nP}}
%      \end{flagderiv}
% 
%      Note that step~\ref{mt:as-P} is not an intuitive step but necessary 
%      to get $\neg P$ under the assumption $\neg Q$.
%    \end{Example}
% 
%   \subsection{Other options}
%    \DescribeMacro{\caption}\index{usage}
%    \DescribeMacro{\tablehead}\DescribeMacro{\tablefirsthead}
%    A \flags\ environment actually wraps a {\tt longtable}.  This means
%    that at the start of an environment all {\tt longtable} options are
%    available.  This means the use of |\caption|, and setting heads
%    (|\tablehead| and |\tablefirsthead|) and feet (|\endfoot|
%    \DescribeMacro{\endfoot}\DescribeMacro{\endlastfoot}
%    and |\endlastfoot|) but also changing the lengths, etc.
%
%    \DescribeEnv{flagderiv*}
%    Note that this does not hold for the slightly different |flagderiv*|
%    environment, which acts exactly the same but is wrapped in a normal
%    {\tt tabular} and thus can not be split across pages.
%
%    \begin{Example}[gobble=7,frame=single,label=Captioned]
%      \inlcmnts
%      \begin{flagderiv}[reflex]
%        \caption{The reflexivity of $\equiv_n$} \\
%        \introduce*{(1)}{x \in \All}{Assume:}
%          \skipsteps*{\dots \\ \dots}{}
%          \step*{(n - 1)}{x \equiv_n x}{}
%        \conclude*{(n)}{\forall x \in \All: x \equiv_n x}
%                       {$\forall$-intro on (1) and (n - 1):}
%      \end{flagderiv}
%    \end{Example}
% 
%    \DescribeMacro{\\}
%    Additional it is possible to add custom newlines (|\\|) in all
%    step formulas\footnote{This is not possible in introductions or
%    assumptions, but they can often be split into multiple introductions or
%    assumptions.} when the formula gets too long as demonstrated in the 
%    above example.
%
% 
%  \section{Examples\label{sec:ex}}
%   The following are just a few real-life examples of flag-style proofs,
%   where \flags\ provides an elegant means to include these proofs in
%   \LaTeX-documents.
% 
%   \subsection{Examples from \flags}
%    \subsection*{A derivation with auto-numbering and labels/references}
%     \begin{flagderiv}
%      \introduce{intro-xy}{x,y \in \mathbb{N}}{Introduction}
%       \assume{assum:1}{x > 0}{Assumption}
%        \assume{assum:2}{x < y}{Assumption}
%         \step{y-pos}{y > 0}{Transitivity on \ref{assum:1} and \ref{assum:2}}
%         \step{y3-pos}{y^3 > 0}{Arithmetic on \ref{y-pos}}
%        \conclude{imp:1}{x < y \implies y^3 > 0}
%                        {$\implies$-intro on \ref{assum:2} and \ref{y3-pos}}
%       \conclude{imp:2}{x > 0 \implies x < y \implies y^3 > 0}
%                       {$\implies$-intro on \ref{assum:1} and \ref{imp:1}}
%      \conclude{}{\forall x, y \in \Nat: x > 0 \land x < y \implies y^3 > 0}
%                 {$\forall$-intro on \ref{intro-xy} and \ref{imp:2}}
%     \end{flagderiv}
% 
%    \subsection*{A derivation with manual numbering}
%     \begin{flagderiv}
%      \assume*{(m)}{\neg P}{}
%       \skipsteps{1}{\dots}{}
%       \step*{(n - 2)}{False}{}
%      \conclude*{(n - 1)}{\neg\neg P}
%                         {$\implies$-intro on (m) and (n - 2), and Negation}
%      \step*{(n)}{P}{Double negation on (n - 1)}
%     \end{flagderiv}
% 
% 
%   \subsection{Predicate Calculus}
%    \subsubsection*{Idempotence}
%     \begin{flagderiv}[pc-idempotence]
%      \assume{intro:PP}{P \land P}{}
%       \step{and-elim:P}{P}{$\land$-elim on \ref{intro:PP}}
%      \conclude{impl-intro:PP-P}{P \land P \implies P}
%                                {$\implies$-intro on \ref{intro:PP}
%                                 and \ref{and-elim:P}}
%      \assume{intro:P}{P}{}
%       \step{and-intro:P}{P \land P}{$\land$-intro on \ref{intro:P}
%                                     and \ref{intro:P}}
%      \conclude{impl-intro:P-PP}{P \implies P \land P}
%                                {$\implies$-intro on \ref{intro:P}
%                                 and \ref{and-intro:P}}
%      \step{and-intro:result}{(P \land P \implies P) \land
%                              (P \implies P \land P)}
%                             {$\land$-intro on \ref{impl-intro:PP-P}
%                              and \ref{impl-intro:P-PP}}
%      \step{idempotence}{P \land P \equiv P}
%                        {$\equiv$-intro on \ref{and-intro:result}}
%     \end{flagderiv}
% 
%    \subsubsection*{De Morgan}
%     \begin{flagderiv}[pc-demorgan]
%      \assume{intro:NPQ}{\lnot (P \land Q)}{}
%       \assume{intro:NNP}{\lnot\lnot P}{}
%        \assume{intro:Q}{Q}{}
%         \step{notnot-elim:P}{P}{$\lnot\lnot$-elim on \ref{intro:NNP}}
%        \step{and-intro:PQ}{P \land Q}{$\land$-intro on \ref{intro:Q}
%                                       and \ref{notnot-elim:P}}
%         \step{false-intro:2}{\false}
%                             {$\false$-intro on \ref{and-intro:PQ}
%                              and \ref{intro:NPQ}}
%        \conclude{not-intro:Q}{\lnot Q}{$\lnot$-intro on \ref{intro:Q}
%                                        and \ref{false-intro:2}}
%       \conclude{impl-intro:NNP-NQ}{\lnot\lnot P \implies \lnot Q}
%                                   {$\implies$-intro on \ref{intro:NNP}
%                                    and \ref{not-intro:Q}}
%       \step{or-intro:NP-NQ}{\lnot P \lor \lnot Q}
%                            {$\lor$-intro on \ref{impl-intro:NNP-NQ}}
%      \conclude{impl-intro:NPQ-NPNQ}{\lnot (P \land Q) \implies
%                                     \lnot P \lor \lnot Q}
%                                    {$\implies$-intro on \ref{intro:NPQ}
%                                     and \ref{or-intro:NP-NQ}}
%      \assume{intro:NPNQ}{\lnot P \land \lnot Q}{}
%       \assume{intro:PQ}{P \lor Q}{}
%        \step{or-elim:NP-Q}{\lnot P \implies Q}
%                           {$\lor$-elim on \ref{intro:PQ}}
%        \step{and-elim:NP}{\lnot P}{$\land$-elim on \ref{intro:NPNQ}}
%        \step{impl-elim:Q}{Q}{$\implies$-elim on \ref{or-elim:NP-Q}
%                              and \ref{and-elim:NP}}
%        \step{and-elim:NQ}{\lnot Q}{$\land$-elim on \ref{intro:NPNQ}}
%        \step{false-intro:1}{\false}{$\false$-intro on \ref{impl-elim:Q}
%                                     and \ref{and-elim:NQ}}
%       \conclude{impl-intro:PQ}{P \lor Q \implies \false}
%                               {$\implies$-intro on \ref{intro:PQ}
%                                and \ref{false-intro:1}}
%       \step{not-intro:PQ}{\lnot (P \lor Q)}
%                          {$\lnot$-intro on \ref{impl-intro:PQ}}
%      \conclude{impl-intro:NPNQ-NPQ}{\lnot P \land \lnot Q \implies
%                                     \lnot (P \lor Q)}
%                                    {$\implies$-intro on \ref{intro:NPNQ}
%                                     and \ref{not-intro:PQ}}
%      \step{and-intro:result}{(\lnot (P \land Q) \implies
%                               \lnot P \lor \lnot Q)
%                              \land\\
%                              (\lnot P \land \lnot Q \implies
%                               \lnot (P \lor Q))}
%                             {$\land$-intro on \ref{impl-intro:NPQ-NPNQ}
%                              and \ref{impl-intro:NPNQ-NPQ}}
%      \step{demorgan}{\lnot (P \land Q) \equiv \lnot P \lor \lnot Q}
%                     {$\equiv$-intro on \ref{and-intro:result}}
%     \end{flagderiv}
% 
%    \subsubsection*{Transitivity}
%     \begin{flagderiv}[pc-transitivity]
%      \introduce{intro:PQR}{P, Q, R}{}
%       \assume{intro:PQQR}{(P \implies Q) \land (Q \implies R)}{}
%        \assume{intro:P}{P}{}
%         \step{and-elim:PQ}{P \implies Q}
%                           {$\land$-elim on \ref{intro:PQQR}}
%         \step{impl-elim:Q}{Q}{$\implies$-elim on \ref{and-elim:PQ}
%                               and \ref{intro:P}}
%         \step{and-elim:QR}{Q \implies R}
%                           {$\land$-elim on \ref{intro:PQQR}}
%         \step{impl-elim:R}{R}{$\implies$-elim on \ref{and-elim:QR}
%                               and \ref{impl-elim:Q}}
%        \conclude{impl-intro:PR}{P \implies R}
%                                {$\implies$-intro on \ref{intro:P}
%                                 and \ref{impl-elim:R}}
%       \conclude{impl-intro:PQQR-PR}{(P \implies Q) \land (Q \implies R)
%                                     \implies (P \implies R)}
%                                    {$\implies$-intro on \ref{intro:PQQR}
%                                     and \ref{impl-intro:PR}}
%      \conclude{transitivity}{(\forall P, Q, R::
%                               (P \implies Q) \land (Q \implies R)
%                               \implies (P \implies R)
%                              )}
%                             {$\forall$-intro on \ref{intro:PQR}
%                              and \ref{impl-intro:PQQR-PR}}
%     \end{flagderiv}
% 
%   \subsection{Rewrite Systems}
%    \subsubsection*{Mixed confluent $\biimplies$ Church-Rosser}
%     {
%      \renewcommand{\introsymb}{\text{Let}}
%      \begin{flagderiv}
%       \assume*{1}{\CRtext}{hyp}
%        \introduce*{2}{a, b, c\in A, \text{such that}~a\OHA b\land a\THA c}{}
%         \step*{3}{a\OHA b\land a\THA c}{rei (2)}
%         \step*{4}{a\THA b\land a\THA c}{$\forall$-elimination on lemma~\ref{transclosure} and (3)}
%         \step*{5}{(\exists d\in A:: b\THA d\land c\THA d)}{$\forall$-elimination on (1) and (4)}
%        \conclude*{6}{\MCtext}{$\forall$-introduction on (2) and (5)}
%       \conclude*{7}{\CRtext\implies\MCtext}{$\implies$-introduction on (1) and (6)}
%       \assume*{8}{\MCtext}{}
%        \assume*{9}{\text{Induction on the length of}~a\THA b}{}
%         \skipsteps*{\IHbase}{}
%         \introduce*{11}{a, b, c\in A, \text{such that}~a\NHA{0} b\land a\THA c}{}
%          \step*{12}{a=b}{def $\NHA{0}$, (11)}
%          \step*{13}{a\THA c}{$\land$-elimination on (11)}
%          \step*{14}{b\THA c}{(12) applied on (13)}
%          \step*{15}{c\THA c \land b\THA c}{}
%          \step*{16}{(\exists d\in A:: b\THA d\land c\THA d)}{}
%         \renewcommand{\thesteplabel}[1]{#1}
%         \conclude*{}{\IHhyp}{}
%         \step*{18}{\ensuremath{(\forall a, b, c\in A: a\NHA{n} b\land a\THA c:\\ \quad
%                                 (\exists d\in A:: b\THA d\land c\THA d))}}{}
%         \skipsteps*{\IHstep}{}
%         \introduce*{20}{a, b, c\in A, \text{such that}~a\NHA{n+1} b\land a\THA c}{}
%          \step*{21}{a\NHA{n+1} b}{$\land$-elimination on (20)}
%          \step*{22}{a\NHA{n} b'\OHA b}{}
%          \step*{23}{a\THA c}{$\land$-elimination on (20)}
%          \step*{24}{a\NHA{n} b' \land a\THA c}{$\land$-introduction on (22) and (23)}
%          \step*{25}{(\exists d'\in A:: b'\THA d'\land c\THA d')}{$\forall$-elimination on (18) and  (24)}
%          \step*{26}{d', \text{such that}~b'\THA d'\land c\THA d'}{$\exists$-elimination on (25)}
%          \step*{27}{b'\OHA b \land b'\THA d'}{}
%          \step*{28}{(\exists d\in A:: b\THA d\land d'\THA d)}{$\forall$-elimination on (8) and (27)}
%          \step*{29}{d, \text{such that}~b\THA d\land d'\THA d}{$\exists$-elimination on (28)}
%          \step*{30}{a\NHA{n} b'\OHA b\THA d \land a\THA c\THA d'\THA d}{}
%          \step*{31}{b\THA d \land c\THA d}{}
%          \step*{32}{(\exists d\in A:: b\THA d\land c\THA d)}{}
%        \conclude*[2]{33}{\CRtext}{}
%       \conclude*{34}{\MCtext\implies\CRtext}{$\implies$-introduction on (8) and (33)}
%       \step*{35}{\MCtext\biimplies\CRtext}{$\biimplies$-introduction on (7) and (34)}
%      \end{flagderiv}
%     }
% 
%     \begin{lemma}
%      \label{transclosure}
%      \mbox{\ensuremath{(\forall a, b\in A: a\OHA b: a\THA b)}}
%     \end{lemma}
%
% \StopEventually{}
%
% \newpage
% \section{Implementation}
%
%  First some initial code is needed for processing of the options.
%  Note that {\tt ifthen} is used throughout the package.
%    \begin{macrocode}
\RequirePackage{ifthen}
\newboolean{@inlcmnts}
%    \end{macrocode}
% 
%  Processing of the option ({\tt inlcmnts}) for enabling inline comments.
%    \begin{macrocode}
\DeclareOption{inlcmnts}{\setboolean{@inlcmnts}{true}}
\ProcessOptions
%    \end{macrocode}
%
%  Loading of the required packages.
%  Note that \flags\ needs to require a fix/override for the array package, 
%  until the fixed version is available in most distributions.
%    \begin{macrocode}
\RequirePackage{array}
\RequirePackage{longtable}
\long\@namedef{NC@rewrite@*}#1#2{%
   \count@#1\relax
   \loop
   \ifnum\count@>\z@
   \advance\count@\m@ne
   \@temptokena\expandafter{\the\@temptokena#2}%
   \repeat
   \NC@find}
%    \end{macrocode}
%
% \subsection{Generic settings, counters and commands}
%
%  \begin{macro}{fd@flagcount}
%  \begin{macro}{fd@stepcount}
%   The counters keeping the number of opened/nested flags and the number of
%   derivation steps.
%    \begin{macrocode}
\newcounter{fd@flagcount}
\newcounter{fd@stepcount}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%  
%  \begin{macro}{\derivskip}
%   The default space ({\tt 8pt}) between lines in a derivation.  This is 
%   overridable to define  a different default length.
%    \begin{macrocode}
\newlength{\derivskip}
\setlength{\derivskip}{8pt}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\introsymb}
%   The symbol used as variable introduction prefix.  Overridable with
%   |\renewcommand| to insert a different symbol, defaults to: 
%   |\textbf{var}|.
%
%   Ex. |\renewcommand{\introsymb}{\textbf{Let}}|
%    \begin{macrocode}
\newcommand{\introsymb}{\textbf{var}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\thestepcount}
%   This is the command used to generate a step number label (used by 
%   auto-numbering). |\thestepcount| can be overridden to display labels 
%   differently, defaults to: ({\it number}). This exposes the internal
%   |\thefd@stepcount| command.
%
%   Ex. |\renewcommand{\thestepcount}{[\arabic{stepcount}]}|
%    \begin{macrocode}
\newcommand{\thestepcount}{(\arabic{fd@stepcount})}
\renewcommand{\thefd@stepcount}{\thestepcount}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\thesteplabel}
%   Command used to generate a customized step label (used by manual numbering).
%   |\thesteplabel| can be overridden to display manual numbers differently,
%   this command has one argument: \marg{label}.
%   The command does nothing by default (i.e\@. just returns the argument).
%
%   Ex. |\renewcommand{\thesteplabel}[1]{[##1]}|
%    \begin{macrocode}
\newcommand{\thesteplabel}[1]{#1}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\theinlcmnt}
%   The command used to format inline comments (if enabled).  |\theinlcmnt| 
%   can be overridden to display it differently and has one argument: 
%   \marg{comment}. Defaults to: \{ {\em comment text} \}.
%
%   Ex. |\renewcommand{\theinlcmnt}[1]{--#1--}|
%    \begin{macrocode}
\newcommand{\theinlcmnt}[1]{\{ #1 \}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\inlcmnts}
%   Command to switch inline comments explicitly on (and by that, overriding
%   and forgetting the default determined by the absence or presence of the
%   {\tt inlcmnts} package option).
%
%    \begin{macrocode}
\newcommand{\inlcmnts}{\setboolean{@inlcmnts}{true}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\noinlcmnts}
%   Command to switch inline comments explicitly off (and by that, overriding
%   and forgetting the default determined by the absence or presence of the
%   {\tt inlcmnts} package option).
%
%    \begin{macrocode}
\newcommand{\noinlcmnts}{\setboolean{@inlcmnts}{false}}
%    \end{macrocode}
%  \end{macro}
%
% \subsection{The derivation environment}
%
% The following three sections describe the internal commands and constructs
% of the {\tt flagderiv} environment, after which the last section will
% explain how the environment works and how it is put together using the
% internal constructs.
%
% Note that for almost all commands there exists two versions, one used
% for manual numbered derivations and one for automatic numbered/labelled
% derivations (both forms can be mixed within the environment).
%
% \subsubsection{Labels}
%
% Derivation lines are labelled and can be referred to.  This can be done
% with automatically numbered lines where the first argument is a normal
% \LaTeX\ label.  However, to avoid clashing, the labels can be automatically
% prefixed with a namespace. The following two commands handle this prefixing.
%
%  \begin{macro}{\@labelprefix}
%   Command that specifies the label namespace prefix.
%
%    \begin{macrocode}
\newcommand{\@labelprefix}{\relax}
%    \end{macrocode}
%   \end{macro}
%  \begin{macro}{\@derlabel}
%   Internal command to define a label using the prefix (if a label
%   is defined, otherwise do nothing).
%
%    \begin{macrocode}
\newcommand{\@derlabel}[1]{%
  \ifthenelse{\equal{#1}{}}{}{\label{\@labelprefix#1}}%
}
%    \end{macrocode}
%  \end{macro}
%
% \subsubsection{Steps}
% 
% A line of a derivation can be one of five types:
% \begin{enumerate}
%  \item A simple and bare line, only internally accessible,
%  \item a comment line,
%  \item a derivation step (both automatically and manual numbered),
%  \item a line to indicate skipping of steps,
%  \item a flag (handled in the next section).
% \end{enumerate}
%
% This section deals with the first four types.
%
%  \begin{macro}{\@derline}
%   A simple derivation line that uses {\tt fd@flagcount} to remember
%   the number of open flags. This command is used as:
%   |\@derline|\marg{label}\marg{formula}\marg{comment}, with:
%   \begin{description}
%    \item[\marg{label}] a label which can be left empty, to refer back to in 
%     comments of other derivation lines,
%    \item[\marg{formula}] the formula, and
%    \item[\marg{comment}] a comment.
%   \end{description}
%
%    \begin{macrocode}
\newcommand{\@derline}[3]{%
 \mbox{#1} &
 \setlength{\extrarowheight}{\derivskip}%
%    \end{macrocode}
%   If the flag counter is still zero\dots
%    \begin{macrocode}
 \ifthenelse{\value{fd@flagcount}=0}%
%    \end{macrocode}
%   then this line is outside any flag,
%    \begin{macrocode}
 {\begin{array}[t]{@{}l}}%
%    \end{macrocode}
%   otherwise this is inside one or more flag contexts and the amount of
%   open flagpoles equal to the number of open contexts should be inserted.
%
%    \begin{macrocode}
 {\begin{array}[t]{*{\value{fd@flagcount}}{|@{\hspace{2\arraycolsep}}}l}}
  \ensuremath{#2}
 \end{array} &
 \mbox{#3} \\
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@CMNTderline}
%   Command to fold the comment on a separate line before the
%   actual derivation line if the {\tt inlcmnts} option is set for this
%   package, see also |\@derline| for the argument handling.
%
%    \begin{macrocode}
\newcommand{\@CMNTderline}[3]{%
 \ifthenelse{\boolean{@inlcmnts}}{%
%    \end{macrocode}
%   If the inline comments option is enabled, fold the comment before the 
%   actual line.
%    \begin{macrocode}
  \ifthenelse{\equal{#3}{}}{}{% 
%    \end{macrocode}
%   There is comment, insert it inline.
%    \begin{macrocode}
   \@derline{}{\mbox{\theinlcmnt{#3}}}{}%
  }
  \@derline{#1}{#2}{}%
 }{%
%    \end{macrocode}
%   If inline comments are not enabled, pass everything to |\@derline| directly.
%    \begin{macrocode}
  \@derline{#1}{#2}{#3}%
 }%
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@MANstep}
%  \begin{macro}{\@AUTOstep}
%   Command for the manual ({\tt MAN}) and automatically ({\tt AUTO})
%   numbered version of a derivation step.  See also |\@derline|.
%
%    \begin{macrocode}
\newcommand{\@MANstep}[3]{\@CMNTderline{\thesteplabel{#1}}{#2}{#3}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\@AUTOstep}[3]{%
 \refstepcounter{fd@stepcount}%
 \@derlabel{#1}\@CMNTderline{\thestepcount}{#2}{#3}%
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\@MANskipsteps}
%  \begin{macro}{\@AUTOskipsteps}
%   Command for the manual ({\tt MAN}) and automatically ({\tt AUTO})
%   numbered version of skipping derivation steps.  See also |\@derline|.
%
%    \begin{macrocode}
\newcommand{\@MANskipsteps}[2]{\@CMNTderline{}{#1}{#2}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\@AUTOskipsteps}[3]{%
 \addtocounter{fd@stepcount}{#1}%
 \@CMNTderline{}{#2}{#3}%
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
% \subsubsection{Flags}
%
% Flags are actually simple derivation lines with two extras: the formula
% is put into a flag box and the flag counter {\tt fd@flagcount} is incremented
% when one is opened.  Next to flag commands there are also commands to
% to close (and by that decrementing the counter) flags.
%
% There exist three types of flags:
% \begin{enumerate}
%  \item the simple and bare flag,
%  \item the assumption flag (both manual and automatically numbered),
%  \item the variable introduction flag (also manual and automatically numbered).
% \end{enumerate}
%
%  \begin{macro}{\@flagbox}
%   Creates a flag for (a box around) the given text/formula, takes
%   \marg{formula} as an argument.
%    \begin{macrocode}
\newcommand{\@flagbox}[1]{%
 \setlength{\fboxsep}{0.75ex}%
 \fbox{#1}%
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@startflag}
%   Starts a flag context by opening a flag and incrementing the counter.
%   The command is used as:
%   |\@startflag|\marg{label}\marg{formula}\marg{comment},
%   see also |\@derline| for the meaning of these arguments.
%
%    \begin{macrocode}
\newcommand{\@startflag}[3]{%
 \@CMNTderline{#1}{\@flagbox{\ensuremath{#2}}}{#3}%
 \addtocounter{fd@flagcount}{1}%
}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@endflag}
%   Ends a flag by closing it and decrementing the counter.  Takes the
%   number of flags to close \marg{number} as an argument.
%
%    \begin{macrocode}
\newcommand{\@endflag}[1]{\addtocounter{fd@flagcount}{-#1}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@flagclose}
%   Wrapper command for closing a number of flags simultaneously taking \oarg{number}
%   as an optional argument, defaulting to 1. See also |\@endflag|.
%
%    \begin{macrocode}
\newcommand{\@flagclose}[1][1]{\@endflag{#1}}
%    \end{macrocode}
%  \end{macro}
%
%  \begin{macro}{\@MANassume}
%  \begin{macro}{\@AUTOassume}
%   Command for the manual ({\tt MAN}) and automatic ({\tt AUTO}) 
%   numbered version of an assumption flag, see also |\@startflag|.
%
%    \begin{macrocode}
\newcommand{\@MANassume}[3]{\@startflag{\thesteplabel{#1}}{#2}{#3}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\@AUTOassume}[3]{
 \refstepcounter{fd@stepcount}%
 \@derlabel{#1}\@startflag{\thestepcount}{#2}{#3}%
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\@MANintroduction}
%  \begin{macro}{\@AUTOintroduction}
%   Command for the manual ({\tt MAN}) and automatically ({\tt AUTO}) 
%   numbered version of an introduction flag, see also |\@startflag|.
%
%    \begin{macrocode}
\newcommand{\@MANintroduction}[3]{%
  \@startflag{\thesteplabel{#1}}{\introsymb~#2}{#3}%
}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\@AUTOintroduction}[3]{
  \refstepcounter{fd@stepcount}%
  \@derlabel{#1}\@startflag{\thestepcount}{\introsymb~#2}{#3}%
}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\@MANconclude}
%  \begin{macro}{\@AUTOconclude}
%   Command to conclude from one or more open flags in the manual ({\tt MAN}) 
%   and automatically ({\tt AUTO}) numbered version, see also |\@endflag|.  
%   The command is used as: 
%   |\@MANconclude|\oarg{number}\marg{label}\marg{formula}\marg{comment},
%   where the \oarg{number} indicates the number of flags to close,
%   defaulting to 1.
%
%    \begin{macrocode}
\newcommand{\@MANconclude}[4][1]{\@endflag{#1}\step*{#2}{#3}{#4}}
%    \end{macrocode}
%
%    \begin{macrocode}
\newcommand{\@AUTOconclude}[4][1]{\@endflag{#1}\step{#2}{#3}{#4}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
% \subsubsection{The environment itself}
%
%  \begin{environment}{flagderiv}
%   This section explains the main environment for flag style
%   derivations/proofs:  {\tt flagderiv}.  The command has an optional
%   argument \oarg{prefix}, used as label prefix so that labels can be
%   ``scoped'' with a namespace local to the derivation but still 
%   accessible from the outside.
% 
%   Examples:\\
%   |\begin{flagderiv} ... \end{flagderiv}|\\
%   |\begin{flagderiv}[abs] ... \end{flagderiv}|
% 
%    \begin{macrocode}
\newenvironment{flagderiv}[1][]{%
%    \end{macrocode}
%
%  First, set the optional label prefix (if given) and reset the counters.
%
%    \begin{macrocode}
 \ifthenelse{\equal{#1}{}}{%
  \renewcommand{\@labelprefix}{}%
 }{%
  \renewcommand{\@labelprefix}{#1:}%
 }%
 \setcounter{fd@flagcount}{0}%
 \setcounter{fd@stepcount}{0}%
%    \end{macrocode}
%
%  Note: {\em All} of the following five commands have a star version which
%  allows manual numbering, instead of having auto-numbering, \LaTeX\ labels 
%  and references.
%
%  \begin{macro}{\assume}
%  \begin{macro}{\assume*}
%   Opens an assumption flag; command usage:\\[1em]
%   |\assume|\marg{label}\marg{formula}\marg{comment}\\
%   |\assume*|\marg{custom-number}\marg{formula}\marg{comment}
%
%   Examples: \\
%   |\assume{assum:1}{x > 0}{Assumption}|\\
%   |\assume*{l}{\neg P}{}|
%
%    \begin{macrocode}
 \newcommand{\assume}{\@ifstar{\@MANassume}{\@AUTOassume}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\introduce}
%  \begin{macro}{\introduce*}
%   Opens an introduction flag. Command usage: \\[1em]
%   |\introduce|\marg{label}\marg{formula}\marg{comment} \\
%   |\introduce*|\marg{custom-number}\marg{formula}\marg{comment}
%
%   Examples:\\
%   |\introduce{intro-x}{x \in \mathbb{N}}{Introduction}| \\
%   |\introduce*{l - 1}{P \in s_P}{}|
%
%    \begin{macrocode}
 \newcommand{\introduce}{\@ifstar{\@MANintroduction}{\@AUTOintroduction}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\conclude}
%  \begin{macro}{\conclude*}
%   Performs a conclusion/multiple conclusion from one or more
%   introductions and/or assumptions. The command usage:\\[1em]
%   |\conclude|\oarg{number}\marg{label}\marg{formula}\marg{comment}\\
%   |\conclude*|\oarg{number}\marg{custon-number}\marg{formula}\marg{comment}
%
%   The optional argument \oarg{number} indicates how much flags should be 
%   closed with this conclusion, this defaults to 1.
%
%   Examples;\\
%   |\conclude[2]{concl:2}{x > 0 \implies ...}{$\implies$-intro on ...}|\\
%   |\conclude*{n - 1}{\neg\neg P}{$\implies$-intro on (l) ...}|
%
%    \begin{macrocode}
 \newcommand{\conclude}{\@ifstar{\@MANconclude}{\@AUTOconclude}}
%    \end{macrocode}
%   \end{macro}
%   \end{macro}
%
%   \begin{macro}{\step}
%   \begin{macro}{\step*}
%    Performs a derivation step, command usage:\\[1em]
%    |\step|\marg{label}\marg{formula}\marg{comment}\\
%    |\step*|\marg{custom-number}\marg{formula}\marg{comment} 
%
%    Examples:\\
%    |\step{x-elim}{x > 0}{$\forall$-elem on \ref{assum:1}}|\\
%    |\step*{n - 1}{x > 0}{$\forall$-elem on (n - 3)}|
%
%    Note: |\\| (line breaks) may be inserted for splitting
%    large formulas.
%
%    \begin{macrocode}
 \newcommand{\step}{\@ifstar{\@MANstep}{\@AUTOstep}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\skipsteps}
%  \begin{macro}{\skipsteps*}
%    Skips a number of steps (for usage in examples), usage:\\[1em]
%    |\skipsteps|\marg{number}\marg{formula}\marg{comments}\\
%    |\skipsteps*|\marg{formula}\marg{comments}.
%
%    Examples:\\
%    |\skipsteps{3}{\vdots}{Etcetera, etcetera}|\\
%    |\skipsteps*{\vdots}{Etcetera, etcetera}|
%
%    \begin{macrocode}
 \newcommand{\skipsteps}{\@ifstar{\@MANskipsteps}{\@AUTOskipsteps}}
%    \end{macrocode}
%  \end{macro}
%  \end{macro}
%
%  \begin{macro}{\done}
%   Closes a number of flags because contexts have been opened and
%   should be closed without conclusions.  Command usage: \\[1em]
%   |\done|\oarg{number}
%
%   This command takes a number of introductions/assumptions, \oarg{number},
%   to close as an optional argument, which defaults to 1.
%
%   Examples:\\
%   |\done|\\
%   |\done[3]|
%
%    \begin{macrocode}
 \newcommand{\done}{\@flagclose}
%    \end{macrocode}
%  \end{macro}
%
%  Silently override |\ref| to work with the defined namespace.
%    \begin{macrocode}
 \let\origref\ref%
 \renewcommand{\ref}[1]{\origref{\@labelprefix##1}}%
%    \end{macrocode}
%
% The actual flagderiv environment is just a longtable (called
% ``derivation'').
%
%    \begin{macrocode}
 \renewcommand{\tablename}{Derivation}%
 \begin{longtable}[l]{rll}
}{%
 \end{longtable}
}
%    \end{macrocode}
%  \end{environment}
%
%  \begin{environment}{flagderiv*}
%   The |flagderiv*| environment is almost the same as |flagderiv|
%   with the only difference that the resulting derivation will and
%   can not be split across pages.
%
%   Example:\\
%   |\begin{flagderiv*} ... \end{flagderiv*}|\\
% 
%    \begin{macrocode}
\newenvironment{flagderiv*}[1][]{%
 \ifthenelse{\equal{#1}{}}{%
  \renewcommand{\@labelprefix}{}%
 }{%
  \renewcommand{\@labelprefix}{#1:}%
 }%
 \setcounter{fd@flagcount}{0}%
 \setcounter{fd@stepcount}{0}%
 \newcommand{\assume}{\@ifstar{\@MANassume}{\@AUTOassume}}
 \newcommand{\introduce}{\@ifstar{\@MANintroduction}{\@AUTOintroduction}}
 \newcommand{\conclude}{\@ifstar{\@MANconclude}{\@AUTOconclude}}
 \newcommand{\step}{\@ifstar{\@MANstep}{\@AUTOstep}}
 \newcommand{\skipsteps}{\@ifstar{\@MANskipsteps}{\@AUTOskipsteps}}
 \newcommand{\done}{\@flagclose}
 \let\origref\ref%
 \renewcommand{\ref}[1]{\origref{\@labelprefix##1}}%
 \renewcommand{\tablename}{Derivation}
 \begin{tabular}{rll}
}{%
 \end{tabular}
}
%    \end{macrocode}
%  \end{environment}
%
% \PrintIndex
% \PrintChanges
%
% \Finale
\endinput