doc-src/LaTeXsugar/Sugar/Sugar.thy
author kleing
Fri Dec 03 07:27:48 2004 +0100 (2004-12-03)
changeset 15367 ac18081228ae
parent 15366 e6f595009734
child 15368 79f624f97f7f
permissions -rw-r--r--
fixed typo
nipkow@15337
     1
(*<*)
nipkow@15337
     2
theory Sugar
kleing@15366
     3
imports LaTeXsugar OptionalSugar
nipkow@15337
     4
begin
nipkow@15337
     5
(*>*)
nipkow@15337
     6
nipkow@15337
     7
section "Introduction"
nipkow@15337
     8
nipkow@15337
     9
text{* This document is for those Isabelle users that have mastered
nipkow@15337
    10
the art of mixing \LaTeX\ text and Isabelle theories and never want to
nipkow@15337
    11
typeset a theorem by hand anymore because they have experienced the
nipkow@15337
    12
bliss of writing \verb!@!\verb!{thm[display]setsum_cartesian_product[no_vars]}!
nipkow@15337
    13
and seeing Isabelle typeset it for them:
nipkow@15337
    14
@{thm[display,eta_contract=false] setsum_cartesian_product[no_vars]}
nipkow@15342
    15
No typos, no omissions, no sweat.
nipkow@15342
    16
If you have not experienced that joy, read Chapter 4, \emph{Presenting
nipkow@15342
    17
Theories}, \cite{LNCS2283} first.
nipkow@15337
    18
nipkow@15337
    19
If you have mastered the art of Isabelle's \emph{antiquotations},
nipkow@15337
    20
i.e.\ things like the above \verb!@!\verb!{thm...}!, beware: in your vanity
nipkow@15337
    21
you may be tempted to think that all readers of the stunning ps or pdf
nipkow@15337
    22
documents you can now produce at the drop of a hat will be struck with
nipkow@15337
    23
awe at the beauty unfolding in front of their eyes. Until one day you
nipkow@15337
    24
come across that very critical of readers known as the ``common referee''.
nipkow@15337
    25
He has the nasty habit of refusing to understand unfamiliar notation
nipkow@15337
    26
like Isabelle's infamous @{text"\<lbrakk> \<rbrakk> \<Longrightarrow>"} no matter how many times you
nipkow@15337
    27
explain it in your paper. Even worse, he thinks that using @{text"\<lbrakk>
nipkow@15337
    28
\<rbrakk>"} for anything other than denotational semantics is a cardinal sin
nipkow@15342
    29
that must be punished by instant rejection.
nipkow@15337
    30
nipkow@15337
    31
nipkow@15337
    32
This document shows you how to make Isabelle and \LaTeX\ cooperate to
nipkow@15337
    33
produce ordinary looking mathematics that hides the fact that it was
nipkow@15342
    34
typeset by a machine. You merely need to import theory
kleing@15366
    35
\texttt{LaTeXsugar} in the header of your own theory and copy the bits of 
kleing@15366
    36
\texttt{OptionalSugar} that you want to use. You may also
nipkow@15342
    37
need additional \LaTeX\ packages. These should be included
nipkow@15342
    38
at the beginning of your \LaTeX\ document, typically in \texttt{root.tex}.
nipkow@15342
    39
*}
nipkow@15342
    40
nipkow@15342
    41
section{* HOL syntax*}
nipkow@15342
    42
nipkow@15342
    43
subsection{* Logic *}
nipkow@15342
    44
nipkow@15342
    45
text{* The predefined constructs @{text"if"}, @{text"let"} and
nipkow@15342
    46
@{text"case"} are set in sans serif font to distinguish them from
nipkow@15342
    47
other functions. This improves readability:
nipkow@15342
    48
\begin{itemize}
nipkow@15342
    49
\item @{term"if b then e\<^isub>1 else e\<^isub>2"} instead of @{text"if b then e\<^isub>1 else e\<^isub>2"}.
nipkow@15342
    50
\item @{term"let x = e\<^isub>1 in e\<^isub>2"} instead of @{text"let x = e\<^isub>1 in e\<^isub>2"}.
nipkow@15342
    51
\item @{term"case x of True \<Rightarrow> e\<^isub>1 | False \<Rightarrow> e\<^isub>2"} instead of\\
nipkow@15342
    52
      @{text"case x of True \<Rightarrow> e\<^isub>1 | False \<Rightarrow> e\<^isub>2"}.
nipkow@15342
    53
\end{itemize}
nipkow@15342
    54
*}
nipkow@15342
    55
nipkow@15342
    56
subsection{* Sets *}
nipkow@15337
    57
nipkow@15342
    58
text{* Although set syntax in HOL is already close to
nipkow@15342
    59
standard, we provide a few further improvements:
nipkow@15342
    60
\begin{itemize}
nipkow@15342
    61
\item @{term"{x. P}"} instead of @{text"{x. P}"}.
nipkow@15342
    62
\item @{term"{}"} instead of @{text"{}"}.
nipkow@15342
    63
\item @{term"insert a (insert b (insert c M))"} instead of @{text"insert a (insert b (insert c M))"}.
nipkow@15342
    64
\end{itemize}
nipkow@15342
    65
*}
nipkow@15342
    66
nipkow@15342
    67
subsection{* Lists *}
nipkow@15342
    68
nipkow@15342
    69
text{* If lists are used heavily, the following notations increase readability:
nipkow@15342
    70
\begin{itemize}
nipkow@15342
    71
\item @{term"x # xs"} instead of @{text"x # xs"}.
nipkow@15342
    72
      Exceptionally, @{term"x # xs"} is also input syntax.
nipkow@15342
    73
If you prefer more space around the $\cdot$ you have to redefine
nipkow@15342
    74
\verb!\isasymcdot! in \LaTeX:
nipkow@15342
    75
\verb!\renewcommand{\isasymcdot}{\isamath{\,\cdot\,}}!
nipkow@15342
    76
nipkow@15342
    77
\item @{term"length xs"} instead of @{text"length xs"}.
nipkow@15342
    78
\item @{term"nth xs n"} instead of @{text"nth xs i"},
nipkow@15342
    79
      the $n$th element of @{text xs}.
nipkow@15342
    80
kleing@15366
    81
\item The @{text"@"} operation associates implicitly to the right,
kleing@15366
    82
which leads to unpleasant line breaks if the term is too long for one
kleing@15366
    83
line. To avoid this, \texttt{OptionalSugar} contains syntax to group
kleing@15366
    84
@{text"@"}-terms to the left before printing, which leads to better
kleing@15366
    85
line breaking behaviour:
kleing@15366
    86
@{term[display]"term\<^isub>0 @ term\<^isub>1 @ term\<^isub>2 @ term\<^isub>3 @ term\<^isub>4 @ term\<^isub>5 @ term\<^isub>6 @ term\<^isub>7 @ term\<^isub>9 @ term\<^isub>1\<^isub>0"}
kleing@15366
    87
kleing@15366
    88
\item The same can be done for @{text"\<and>"}:
kleing@15366
    89
@{term[display]"term\<^isub>0 \<and> term\<^isub>1 \<and> term\<^isub>2 \<and> term\<^isub>3 \<and> term\<^isub>4 \<and> term\<^isub>5 \<and> term\<^isub>6 \<and> term\<^isub>7 \<and> term\<^isub>9 \<and> term\<^isub>1\<^isub>0 \<and> term\<^isub>1\<^isub>1"}
nipkow@15342
    90
nipkow@15342
    91
\end{itemize}
nipkow@15337
    92
*}
nipkow@15337
    93
nipkow@15337
    94
section "Printing theorems"
nipkow@15337
    95
nipkow@15337
    96
subsection "Inference rules"
nipkow@15337
    97
nipkow@15342
    98
text{* To print theorems as inference rules you need to include Didier
nipkow@15342
    99
R\'emy's \texttt{mathpartir} package~\cite{mathpartir}
nipkow@15342
   100
for typesetting inference rules in your \LaTeX\ file.
nipkow@15337
   101
nipkow@15337
   102
Writing \verb!@!\verb!{thm[mode=Rule] conjI[no_vars]}! produces
nipkow@15337
   103
@{thm[mode=Rule] conjI[no_vars]}, even in the middle of a sentence.
nipkow@15342
   104
If you prefer your inference rule on a separate line, maybe with a name,
nipkow@15342
   105
\begin{center}
nipkow@15342
   106
@{thm[mode=Rule] conjI[no_vars]} {\sc conjI}
nipkow@15342
   107
\end{center}
nipkow@15342
   108
is produced by
nipkow@15337
   109
\begin{quote}
nipkow@15337
   110
\verb!\begin{center}!\\
nipkow@15337
   111
\verb!@!\verb!{thm[mode=Rule] conjI[no_vars]} {\sc conjI}!\\
nipkow@15337
   112
\verb!\end{center}!
nipkow@15337
   113
\end{quote}
nipkow@15342
   114
It is not recommended to use the standard \texttt{display} attribute
nipkow@15342
   115
together with \texttt{Rule} because centering does not work and because
nipkow@15342
   116
the line breaking mechanisms of \texttt{display} and \texttt{mathpartir} can
nipkow@15342
   117
clash.
nipkow@15342
   118
nipkow@15337
   119
Of course you can display multiple rules in this fashion:
nipkow@15337
   120
\begin{quote}
nipkow@15337
   121
\verb!\begin{center}\isastyle!\\
nipkow@15337
   122
\verb!@!\verb!{thm[mode=Rule] conjI[no_vars]} {\sc conjI} \\[1ex]!\\
nipkow@15337
   123
\verb!@!\verb!{thm[mode=Rule] conjE[no_vars]} {\sc disjI$_1$} \qquad!\\
nipkow@15337
   124
\verb!@!\verb!{thm[mode=Rule] disjE[no_vars]} {\sc disjI$_2$}!\\
nipkow@15337
   125
\verb!\end{center}!
nipkow@15337
   126
\end{quote}
nipkow@15337
   127
yields
nipkow@15337
   128
\begin{center}\isastyle
nipkow@15337
   129
@{thm[mode=Rule] conjI[no_vars]} {\sc conjI} \\[1ex]
nipkow@15337
   130
@{thm[mode=Rule] disjI1[no_vars]} {\sc disjI$_1$} \qquad
nipkow@15337
   131
@{thm[mode=Rule] disjI2[no_vars]} {\sc disjI$_2$}
nipkow@15337
   132
\end{center}
nipkow@15337
   133
Note that we included \verb!\isastyle! to obtain
nipkow@15337
   134
the smaller font that otherwise comes only with \texttt{display}.
nipkow@15337
   135
nipkow@15342
   136
The \texttt{mathpartir} package copes well if there are too many
nipkow@15342
   137
premises for one line:
nipkow@15342
   138
\begin{center}
nipkow@15342
   139
@{prop[mode=Rule] "\<lbrakk> A \<longrightarrow> B; B \<longrightarrow> C; C \<longrightarrow> D; D \<longrightarrow> E; E \<longrightarrow> F; F \<longrightarrow> G;
nipkow@15342
   140
 G \<longrightarrow> H; H \<longrightarrow> I; I \<longrightarrow> J; J \<longrightarrow> K \<rbrakk> \<Longrightarrow> A \<longrightarrow> K"}
nipkow@15342
   141
\end{center}
nipkow@15342
   142
nipkow@15342
   143
Limitations: premises and conclusion must each not be longer than the line.
nipkow@15337
   144
*}
nipkow@15342
   145
nipkow@15342
   146
subsection{*If-then*}
nipkow@15342
   147
nipkow@15342
   148
text{* If you prefer a fake ``natural language'' style you can produce
nipkow@15342
   149
the body of
nipkow@15342
   150
\newtheorem{theorem}{Theorem}
nipkow@15342
   151
\begin{theorem}
nipkow@15342
   152
@{thm[mode=IfThen,eta_contract=false] setsum_cartesian_product[no_vars]}
nipkow@15342
   153
\end{theorem}
nipkow@15342
   154
by typing
nipkow@15342
   155
\begin{quote}
nipkow@15342
   156
\verb!@!\verb!{thm[mode=IfThen] setsum_cartesian_product[no_vars]}!
nipkow@15342
   157
\end{quote}
nipkow@15342
   158
nipkow@15342
   159
In order to prevent odd line breaks, the premises are put into boxes.
nipkow@15342
   160
At times this is too drastic:
nipkow@15342
   161
\begin{theorem}
nipkow@15342
   162
@{prop[mode=IfThen] "longpremise \<Longrightarrow> longerpremise \<Longrightarrow> P(f(f(f(f(f(f(f(f(f(x)))))))))) \<Longrightarrow> longestpremise \<Longrightarrow> conclusion"}
nipkow@15342
   163
\end{theorem}
nipkow@15342
   164
In which case you should use \texttt{mode=IfThenNoBox} instead of
nipkow@15342
   165
\texttt{mode=IfThen}:
nipkow@15342
   166
\begin{theorem}
nipkow@15342
   167
@{prop[mode=IfThenNoBox] "longpremise \<Longrightarrow> longerpremise \<Longrightarrow> P(f(f(f(f(f(f(f(f(f(x)))))))))) \<Longrightarrow> longestpremise \<Longrightarrow> conclusion"}
nipkow@15342
   168
\end{theorem}
kleing@15366
   169
*}
nipkow@15342
   170
kleing@15366
   171
subsection {*Definitions and Equations*}
kleing@15366
   172
kleing@15366
   173
text {*
kleing@15366
   174
  The \verb!thm! antiquotation works nicely for proper theorems, but
kleing@15366
   175
  sets of equations as used in defintions are more difficult to
kleing@15366
   176
  typeset nicely: for some reason people tend to prefer aligned 
kleing@15366
   177
  @{text "="} signs.
kleing@15366
   178
kleing@15366
   179
  Isabelle2005 will have a nice mechanism for that, namely the two
kleing@15366
   180
  antiquotations \verb!@!\verb!{lhs thm}! and \verb!@!\verb!{rhs thm}!.
kleing@15366
   181
kleing@15366
   182
  \begin{center}
kleing@15366
   183
  \begin{tabular}{l@ {~~@{text "="}~~}l}
kleing@15366
   184
  @{lhs foldl_Nil[no_vars]} & @{rhs foldl_Nil[no_vars]}\\
kleing@15366
   185
  @{lhs foldl_Cons[no_vars]} & @{rhs foldl_Cons[no_vars]}
kleing@15366
   186
  \end{tabular}
kleing@15366
   187
  \end{center}
kleing@15366
   188
kleing@15366
   189
  \noindent 
kleing@15366
   190
  is produced by the following code:
kleing@15366
   191
kleing@15366
   192
\begin{quote}
kleing@15366
   193
  \verb!\begin{center}!\\
kleing@15366
   194
  \verb!\begin{tabular}{l@ {~~!\verb!@!\verb!{text "="}~~}l}!\\
kleing@15366
   195
  \verb!@!\verb!{lhs foldl_Nil[no_vars]} & @!\verb!{rhs foldl_Nil[no_vars]}!\\
kleing@15366
   196
  \verb!@!\verb!{lhs foldl_Cons[no_vars]} & @!\verb!{rhs foldl_Cons[no_vars]}!\\
kleing@15366
   197
  \verb!\end{tabular}!\\
kleing@15366
   198
  \verb!\end{center}!
kleing@15366
   199
\end{quote}
kleing@15366
   200
kleing@15366
   201
  \noindent
kleing@15366
   202
  Note the space between \verb!@! and \verb!{! in the tabular argument.
kleing@15366
   203
  It prevents Isabelle from interpreting \verb!@ {~~...~~}! 
kleing@15366
   204
  as antiquotation. \verb!@!\verb!{lhs thm}! and \verb!@!\verb!{rhs thm}! 
kleing@15366
   205
  try to be smart about the interpretation of the theorem they
kleing@15366
   206
  print, they work just as well for meta equality @{text "\<equiv>"} and other
kleing@15366
   207
  binary operators like @{text "<"}.
kleing@15366
   208
kleing@15366
   209
  Should you lack both the development version of Isabelle and a time
kleing@15366
   210
  machine, you can still try to simulate the effect using the equation syntax
kleing@15366
   211
  in \texttt{sugar.sty} and \texttt{OptionalSugar}.
kleing@15366
   212
  
kleing@15366
   213
  \begin{center}
kleing@15366
   214
  \begin{tabular}{l@ { }l@ { }l}
kleing@15366
   215
  \setcounter{isatabs}{0}%
kleing@15366
   216
  @{thm [mode=tab] foldl_Nil[no_vars]}\nl
kleing@15366
   217
  @{thm [mode=tab] foldl_Cons[no_vars]}
kleing@15366
   218
  \end{tabular}
kleing@15366
   219
  \end{center}
kleing@15366
   220
kleing@15366
   221
  \noindent 
kleing@15366
   222
  is produced by:
kleing@15366
   223
kleing@15366
   224
\begin{quote}
kleing@15366
   225
  \verb!\begin{center}!\\
kleing@15366
   226
  \verb!\begin{tabular}{l@ { }l@ { }l}!\\
kleing@15366
   227
  \verb!\setcounter{isatabs}{0}%!\\
kleing@15366
   228
  \verb!@!\verb!{thm [mode=tab] foldl_Nil[no_vars]}\nl!\\
kleing@15366
   229
  \verb!@!\verb!{thm [mode=tab] foldl_Cons[no_vars]}!\\
kleing@15366
   230
  \verb!\end{tabular}!\\
kleing@15366
   231
  \verb!\end{center}!
kleing@15366
   232
\end{quote}
kleing@15366
   233
kleing@15366
   234
  \noindent
kleing@15366
   235
  These \LaTeX\ macros are not as flexible as the antiquotations
kleing@15366
   236
  above, they only work for proper equations and definitions and they
kleing@15366
   237
  only work correctly if the left hand side does not contain any
kleing@15366
   238
  @{text "="} signs.
kleing@15366
   239
*}
kleing@15366
   240
kleing@15366
   241
subsection "Patterns"
kleing@15366
   242
kleing@15366
   243
text {*
kleing@15366
   244
  Sometimes functions ignore one or more of their
kleing@15366
   245
  arguments and some functional languages have nice 
kleing@15366
   246
  syntax for that as in @{thm hd.simps [where xs=DUMMY,no_vars]}.
kleing@15366
   247
kleing@15366
   248
  You can simulate this in Isabelle by instantiating the @{term xs} in
kleing@15366
   249
  definition \mbox{@{thm hd.simps[no_vars]}} with a constant @{text DUMMY} that
kleing@15366
   250
  is printed as @{term DUMMY}. The code for the pattern above is 
kleing@15366
   251
  \verb!@!\verb!{thm hd.simps [where xs=DUMMY,novars]}!.
kleing@15366
   252
kleing@15366
   253
  You can drive this game even further and extend the syntax of let
kleing@15366
   254
  bindings such that certain functions like @{term fst}, @{term hd}, 
kleing@15366
   255
  etc.\ are printed nicely. \texttt{OptionalSugar} provides the
kleing@15366
   256
  following pretty printing patterns:
kleing@15366
   257
  
kleing@15366
   258
  \begin{center}
kleing@15366
   259
  \begin{tabular}{l@ {~~produced by~~}l}
kleing@15366
   260
  @{term "let x = fst p in t"} & \verb!@!\verb!{term "let x = fst p in t"}!\\
kleing@15366
   261
  @{term "let x = snd p in t"} & \verb!@!\verb!{term "let x = snd p in t"}!\\
kleing@15366
   262
  @{term "let x = hd xs in t"} & \verb!@!\verb!{term "let x = hd xs in t"}!\\
kleing@15366
   263
  @{term "let x = tl xs in t"} & \verb!@!\verb!{term "let x = tl xs in t"}!\\
kleing@15366
   264
  @{term "let x = the y in t"} & \verb!@!\verb!{term "let x = the y in t"}!\\
kleing@15366
   265
  \end{tabular}
kleing@15366
   266
  \end{center}
kleing@15366
   267
*}
kleing@15366
   268
kleing@15366
   269
subsection "Proofs"
kleing@15366
   270
kleing@15366
   271
text {*
kleing@15367
   272
  Full proofs, even if written in beautiful Isar style, are likely to
kleing@15366
   273
  be too long and detailed to be included in conference papers, but
kleing@15366
   274
  some key lemmas might be of interest.
kleing@15366
   275
kleing@15366
   276
  It is usually easiest to put them in figures like the one in Fig.\
kleing@15366
   277
  \ref{fig:proof}. This was achieved with the \isakeyword{text\_raw}
kleing@15366
   278
  command:
kleing@15366
   279
*}
kleing@15366
   280
text_raw {*
kleing@15366
   281
  \begin{figure}
kleing@15366
   282
  \begin{center}\begin{minipage}{0.6\textwidth}  
kleing@15366
   283
  \begin{isabellebody}
kleing@15366
   284
*}
kleing@15366
   285
lemma True
kleing@15366
   286
proof -
kleing@15366
   287
  -- "pretty trivial"
kleing@15366
   288
  show True by force
kleing@15366
   289
qed
kleing@15366
   290
text_raw {*  
kleing@15366
   291
  \end{isabellebody}
kleing@15366
   292
  \end{minipage}\end{center}
kleing@15366
   293
  \caption{Example proof in a figure.}\label{fig:proof}
kleing@15366
   294
  \end{figure}
kleing@15366
   295
*}
kleing@15366
   296
text {*
kleing@15366
   297
kleing@15366
   298
\begin{quote}
kleing@15366
   299
\small
kleing@15366
   300
\verb!text_raw {!\verb!*!\\
kleing@15366
   301
\verb!  \begin{figure}!\\
kleing@15366
   302
\verb!  \begin{center}\begin{minipage}{0.6\textwidth}!\\
kleing@15366
   303
\verb!  \begin{isabellebody}!\\
kleing@15366
   304
\verb!*!\verb!}!\\
kleing@15366
   305
\verb!lemma True!\\
kleing@15366
   306
\verb!proof -!\\
kleing@15366
   307
\verb!  -- "pretty trivial"!\\
kleing@15366
   308
\verb!  show True by force!\\
kleing@15366
   309
\verb!qed!\\
kleing@15366
   310
\verb!text_raw {!\verb!*!\\
kleing@15366
   311
\verb!  \end{isabellebody}!\\
kleing@15366
   312
\verb!  \end{minipage}\end{center}!\\
kleing@15366
   313
\verb!  \caption{Example proof in a figure.}\label{fig:proof}!\\
kleing@15366
   314
\verb!  \end{figure}!\\
kleing@15366
   315
\verb!*!\verb!}!
kleing@15366
   316
\end{quote}
kleing@15366
   317
  
nipkow@15342
   318
*}
nipkow@15342
   319
nipkow@15337
   320
(*<*)
nipkow@15337
   321
end
nipkow@15337
   322
(*>*)