doc-src/IsarRef/Thy/Document_Preparation.thy
author wenzelm
Thu, 13 Nov 2008 21:45:40 +0100
changeset 28761 9ec4482c9201
parent 28760 cbc435f7b16b
child 30168 9a20be5be90b
permissions -rw-r--r--
updated/refined types of Isar language elements, removed special LaTeX macros;
     1 (* $Id$ *)
     2 
     3 theory Document_Preparation
     4 imports Main
     5 begin
     6 
     7 chapter {* Document preparation \label{ch:document-prep} *}
     8 
     9 text {* Isabelle/Isar provides a simple document preparation system
    10   based on regular {PDF-\LaTeX} technology, with full support for
    11   hyper-links and bookmarks.  Thus the results are well suited for WWW
    12   browsing and as printed copies.
    13 
    14   \medskip Isabelle generates {\LaTeX} output while running a
    15   \emph{logic session} (see also \cite{isabelle-sys}).  Getting
    16   started with a working configuration for common situations is quite
    17   easy by using the Isabelle @{verbatim mkdir} and @{verbatim make}
    18   tools.  First invoke
    19 \begin{ttbox}
    20   isabelle mkdir Foo
    21 \end{ttbox}
    22   to initialize a separate directory for session @{verbatim Foo} (it
    23   is safe to experiment, since @{verbatim "isabelle mkdir"} never
    24   overwrites existing files).  Ensure that @{verbatim "Foo/ROOT.ML"}
    25   holds ML commands to load all theories required for this session;
    26   furthermore @{verbatim "Foo/document/root.tex"} should include any
    27   special {\LaTeX} macro packages required for your document (the
    28   default is usually sufficient as a start).
    29 
    30   The session is controlled by a separate @{verbatim IsaMakefile}
    31   (with crude source dependencies by default).  This file is located
    32   one level up from the @{verbatim Foo} directory location.  Now
    33   invoke
    34 \begin{ttbox}
    35   isabelle make Foo
    36 \end{ttbox}
    37   to run the @{verbatim Foo} session, with browser information and
    38   document preparation enabled.  Unless any errors are reported by
    39   Isabelle or {\LaTeX}, the output will appear inside the directory
    40   defined by the @{verbatim ISABELLE_BROWSER_INFO} setting (as
    41   reported by the batch job in verbose mode).
    42 
    43   \medskip You may also consider to tune the @{verbatim usedir}
    44   options in @{verbatim IsaMakefile}, for example to switch the output
    45   format between @{verbatim pdf} and @{verbatim dvi}, or activate the
    46   @{verbatim "-D"} option to retain a second copy of the generated
    47   {\LaTeX} sources (for manual inspection or separate runs of
    48   @{executable latex}).
    49 
    50   \medskip See \emph{The Isabelle System Manual} \cite{isabelle-sys}
    51   for further details on Isabelle logic sessions and theory
    52   presentation.  The Isabelle/HOL tutorial \cite{isabelle-hol-book}
    53   also covers theory presentation to some extent.
    54 *}
    55 
    56 
    57 section {* Markup commands \label{sec:markup} *}
    58 
    59 text {*
    60   \begin{matharray}{rcl}
    61     @{command_def "header"} & : & @{text "toplevel \<rightarrow> toplevel"} \\[0.5ex]
    62     @{command_def "chapter"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
    63     @{command_def "section"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
    64     @{command_def "subsection"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
    65     @{command_def "subsubsection"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
    66     @{command_def "text"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
    67     @{command_def "text_raw"} & : & @{text "local_theory \<rightarrow> local_theory"} \\[0.5ex]
    68     @{command_def "sect"} & : & @{text "proof \<rightarrow> proof"} \\
    69     @{command_def "subsect"} & : & @{text "proof \<rightarrow> proof"} \\
    70     @{command_def "subsubsect"} & : & @{text "proof \<rightarrow> proof"} \\
    71     @{command_def "txt"} & : & @{text "proof \<rightarrow> proof"} \\
    72     @{command_def "txt_raw"} & : & @{text "proof \<rightarrow> proof"} \\
    73   \end{matharray}
    74 
    75   Markup commands provide a structured way to insert text into the
    76   document generated from a theory.  Each markup command takes a
    77   single @{syntax text} argument, which is passed as argument to a
    78   corresponding {\LaTeX} macro.  The default macros provided by
    79   @{"file" "~~/lib/texinputs/isabelle.sty"} can be redefined according
    80   to the needs of the underlying document and {\LaTeX} styles.
    81 
    82   Note that formal comments (\secref{sec:comments}) are similar to
    83   markup commands, but have a different status within Isabelle/Isar
    84   syntax.
    85 
    86   \begin{rail}
    87     ('chapter' | 'section' | 'subsection' | 'subsubsection' | 'text') target? text
    88     ;
    89     ('header' | 'text\_raw' | 'sect' | 'subsect' | 'subsubsect' | 'txt' | 'txt\_raw') text
    90     ;
    91   \end{rail}
    92 
    93   \begin{description}
    94 
    95   \item @{command header} provides plain text markup just preceding
    96   the formal beginning of a theory.  The corresponding {\LaTeX} macro
    97   is @{verbatim "\\isamarkupheader"}, which acts like @{command
    98   section} by default.
    99   
   100   \item @{command chapter}, @{command section}, @{command subsection},
   101   and @{command subsubsection} mark chapter and section headings
   102   within the main theory body or local theory targets.  The
   103   corresponding {\LaTeX} macros are @{verbatim "\\isamarkupchapter"},
   104   @{verbatim "\\isamarkupsection"}, @{verbatim
   105   "\\isamarkupsubsection"} etc.
   106 
   107   \item @{command sect}, @{command subsect}, and @{command subsubsect}
   108   mark section headings within proofs.  The corresponding {\LaTeX}
   109   macros are @{verbatim "\\isamarkupsect"}, @{verbatim
   110   "\\isamarkupsubsect"} etc.
   111 
   112   \item @{command text} and @{command txt} specify paragraphs of plain
   113   text.  This corresponds to a {\LaTeX} environment @{verbatim
   114   "\\begin{isamarkuptext}"} @{text "\<dots>"} @{verbatim
   115   "\\end{isamarkuptext}"} etc.
   116 
   117   \item @{command text_raw} and @{command txt_raw} insert {\LaTeX}
   118   source into the output, without additional markup.  Thus the full
   119   range of document manipulations becomes available, at the risk of
   120   messing up document output.
   121 
   122   \end{description}
   123 
   124   Except for @{command "text_raw"} and @{command "txt_raw"}, the text
   125   passed to any of the above markup commands may refer to formal
   126   entities via \emph{document antiquotations}, see also
   127   \secref{sec:antiq}.  These are interpreted in the present theory or
   128   proof context, or the named @{text "target"}.
   129 
   130   \medskip The proof markup commands closely resemble those for theory
   131   specifications, but have a different formal status and produce
   132   different {\LaTeX} macros.  The default definitions coincide for
   133   analogous commands such as @{command section} and @{command sect}.
   134 *}
   135 
   136 
   137 section {* Document Antiquotations \label{sec:antiq} *}
   138 
   139 text {*
   140   \begin{matharray}{rcl}
   141     @{antiquotation_def "theory"} & : & @{text antiquotation} \\
   142     @{antiquotation_def "thm"} & : & @{text antiquotation} \\
   143     @{antiquotation_def "lemma"} & : & @{text antiquotation} \\
   144     @{antiquotation_def "prop"} & : & @{text antiquotation} \\
   145     @{antiquotation_def "term"} & : & @{text antiquotation} \\
   146     @{antiquotation_def const} & : & @{text antiquotation} \\
   147     @{antiquotation_def abbrev} & : & @{text antiquotation} \\
   148     @{antiquotation_def typeof} & : & @{text antiquotation} \\
   149     @{antiquotation_def typ} & : & @{text antiquotation} \\
   150     @{antiquotation_def thm_style} & : & @{text antiquotation} \\
   151     @{antiquotation_def term_style} & : & @{text antiquotation} \\
   152     @{antiquotation_def "text"} & : & @{text antiquotation} \\
   153     @{antiquotation_def goals} & : & @{text antiquotation} \\
   154     @{antiquotation_def subgoals} & : & @{text antiquotation} \\
   155     @{antiquotation_def prf} & : & @{text antiquotation} \\
   156     @{antiquotation_def full_prf} & : & @{text antiquotation} \\
   157     @{antiquotation_def ML} & : & @{text antiquotation} \\
   158     @{antiquotation_def ML_type} & : & @{text antiquotation} \\
   159     @{antiquotation_def ML_struct} & : & @{text antiquotation} \\
   160   \end{matharray}
   161 
   162   The overall content of an Isabelle/Isar theory may alternate between
   163   formal and informal text.  The main body consists of formal
   164   specification and proof commands, interspersed with markup commands
   165   (\secref{sec:markup}) or document comments (\secref{sec:comments}).
   166   The argument of markup commands quotes informal text to be printed
   167   in the resulting document, but may again refer to formal entities
   168   via \emph{document antiquotations}.
   169 
   170   For example, embedding of ``@{text [source=false] "@{term [show_types] \"f x = a + x\"}"}''
   171   within a text block makes
   172   \isa{{\isacharparenleft}f{\isasymColon}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isacharparenleft}x{\isasymColon}{\isacharprime}a{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}a{\isasymColon}{\isacharprime}a{\isacharparenright}\ {\isacharplus}\ x} appear in the final {\LaTeX} document.
   173 
   174   Antiquotations usually spare the author tedious typing of logical
   175   entities in full detail.  Even more importantly, some degree of
   176   consistency-checking between the main body of formal text and its
   177   informal explanation is achieved, since terms and types appearing in
   178   antiquotations are checked within the current theory or proof
   179   context.
   180 
   181   \begin{rail}
   182     atsign lbrace antiquotation rbrace
   183     ;
   184 
   185     antiquotation:
   186       'theory' options name |
   187       'thm' options thmrefs |
   188       'lemma' options prop 'by' method |
   189       'prop' options prop |
   190       'term' options term |
   191       'const' options term |
   192       'abbrev' options term |
   193       'typeof' options term |
   194       'typ' options type |
   195       'thm\_style' options name thmref |
   196       'term\_style' options name term |
   197       'text' options name |
   198       'goals' options |
   199       'subgoals' options |
   200       'prf' options thmrefs |
   201       'full\_prf' options thmrefs |
   202       'ML' options name |
   203       'ML\_type' options name |
   204       'ML\_struct' options name
   205     ;
   206     options: '[' (option * ',') ']'
   207     ;
   208     option: name | name '=' name
   209     ;
   210   \end{rail}
   211 
   212   Note that the syntax of antiquotations may \emph{not} include source
   213   comments @{verbatim "(*"}~@{text "\<dots>"}~@{verbatim "*)"} nor verbatim
   214   text @{verbatim "{"}@{verbatim "*"}~@{text "\<dots>"}~@{verbatim
   215   "*"}@{verbatim "}"}.
   216 
   217   \begin{description}
   218   
   219   \item @{text "@{theory A}"} prints the name @{text "A"}, which is
   220   guaranteed to refer to a valid ancestor theory in the current
   221   context.
   222 
   223   \item @{text "@{thm a\<^sub>1 \<dots> a\<^sub>n}"} prints theorems @{text "a\<^sub>1 \<dots> a\<^sub>n"}.
   224   Full fact expressions are allowed here, including attributes
   225   (\secref{sec:syn-att}).
   226 
   227   \item @{text "@{prop \<phi>}"} prints a well-typed proposition @{text
   228   "\<phi>"}.
   229 
   230   \item @{text "@{lemma \<phi> by m}"} proves a well-typed proposition
   231   @{text "\<phi>"} by method @{text m} and prints the original @{text "\<phi>"}.
   232 
   233   \item @{text "@{term t}"} prints a well-typed term @{text "t"}.
   234 
   235   \item @{text "@{const c}"} prints a logical or syntactic constant
   236   @{text "c"}.
   237   
   238   \item @{text "@{abbrev c x\<^sub>1 \<dots> x\<^sub>n}"} prints a constant abbreviation
   239   @{text "c x\<^sub>1 \<dots> x\<^sub>n \<equiv> rhs"} as defined in the current context.
   240 
   241   \item @{text "@{typeof t}"} prints the type of a well-typed term
   242   @{text "t"}.
   243 
   244   \item @{text "@{typ \<tau>}"} prints a well-formed type @{text "\<tau>"}.
   245   
   246   \item @{text "@{thm_style s a}"} prints theorem @{text a},
   247   previously applying a style @{text s} to it (see below).
   248   
   249   \item @{text "@{term_style s t}"} prints a well-typed term @{text t}
   250   after applying a style @{text s} to it (see below).
   251 
   252   \item @{text "@{text s}"} prints uninterpreted source text @{text
   253   s}.  This is particularly useful to print portions of text according
   254   to the Isabelle document style, without demanding well-formedness,
   255   e.g.\ small pieces of terms that should not be parsed or
   256   type-checked yet.
   257 
   258   \item @{text "@{goals}"} prints the current \emph{dynamic} goal
   259   state.  This is mainly for support of tactic-emulation scripts
   260   within Isar.  Presentation of goal states does not conform to the
   261   idea of human-readable proof documents!
   262 
   263   When explaining proofs in detail it is usually better to spell out
   264   the reasoning via proper Isar proof commands, instead of peeking at
   265   the internal machine configuration.
   266   
   267   \item @{text "@{subgoals}"} is similar to @{text "@{goals}"}, but
   268   does not print the main goal.
   269   
   270   \item @{text "@{prf a\<^sub>1 \<dots> a\<^sub>n}"} prints the (compact) proof terms
   271   corresponding to the theorems @{text "a\<^sub>1 \<dots> a\<^sub>n"}. Note that this
   272   requires proof terms to be switched on for the current logic
   273   session.
   274   
   275   \item @{text "@{full_prf a\<^sub>1 \<dots> a\<^sub>n}"} is like @{text "@{prf a\<^sub>1 \<dots>
   276   a\<^sub>n}"}, but prints the full proof terms, i.e.\ also displays
   277   information omitted in the compact proof term, which is denoted by
   278   ``@{text _}'' placeholders there.
   279   
   280   \item @{text "@{ML s}"}, @{text "@{ML_type s}"}, and @{text
   281   "@{ML_struct s}"} check text @{text s} as ML value, type, and
   282   structure, respectively.  The source is printed verbatim.
   283 
   284   \end{description}
   285 *}
   286 
   287 
   288 subsubsection {* Styled antiquotations *}
   289 
   290 text {* Some antiquotations like @{text thm_style} and @{text
   291   term_style} admit an extra \emph{style} specification to modify the
   292   printed result.  The following standard styles are available:
   293 
   294   \begin{description}
   295   
   296   \item @{text lhs} extracts the first argument of any application
   297   form with at least two arguments --- typically meta-level or
   298   object-level equality, or any other binary relation.
   299   
   300   \item @{text rhs} is like @{text lhs}, but extracts the second
   301   argument.
   302   
   303   \item @{text "concl"} extracts the conclusion @{text C} from a rule
   304   in Horn-clause normal form @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> C"}.
   305   
   306   \item @{text "prem1"}, \dots, @{text "prem9"} extract premise number
   307   @{text "1, \<dots>, 9"}, respectively, from from a rule in Horn-clause
   308   normal form @{text "A\<^sub>1 \<Longrightarrow> \<dots> A\<^sub>n \<Longrightarrow> C"}
   309 
   310   \end{description}
   311 *}
   312 
   313 
   314 subsubsection {* General options *}
   315 
   316 text {* The following options are available to tune the printed output
   317   of antiquotations.  Note that many of these coincide with global ML
   318   flags of the same names.
   319 
   320   \begin{description}
   321 
   322   \item @{text "show_types = bool"} and @{text "show_sorts = bool"}
   323   control printing of explicit type and sort constraints.
   324 
   325   \item @{text "show_structs = bool"} controls printing of implicit
   326   structures.
   327 
   328   \item @{text "long_names = bool"} forces names of types and
   329   constants etc.\ to be printed in their fully qualified internal
   330   form.
   331 
   332   \item @{text "short_names = bool"} forces names of types and
   333   constants etc.\ to be printed unqualified.  Note that internalizing
   334   the output again in the current context may well yield a different
   335   result.
   336 
   337   \item @{text "unique_names = bool"} determines whether the printed
   338   version of qualified names should be made sufficiently long to avoid
   339   overlap with names declared further back.  Set to @{text false} for
   340   more concise output.
   341 
   342   \item @{text "eta_contract = bool"} prints terms in @{text
   343   \<eta>}-contracted form.
   344 
   345   \item @{text "display = bool"} indicates if the text is to be output
   346   as multi-line ``display material'', rather than a small piece of
   347   text without line breaks (which is the default).
   348 
   349   In this mode the embedded entities are printed in the same style as
   350   the main theory text.
   351 
   352   \item @{text "break = bool"} controls line breaks in non-display
   353   material.
   354 
   355   \item @{text "quotes = bool"} indicates if the output should be
   356   enclosed in double quotes.
   357 
   358   \item @{text "mode = name"} adds @{text name} to the print mode to
   359   be used for presentation (see also \cite{isabelle-ref}).  Note that
   360   the standard setup for {\LaTeX} output is already present by
   361   default, including the modes @{text latex} and @{text xsymbols}.
   362 
   363   \item @{text "margin = nat"} and @{text "indent = nat"} change the
   364   margin or indentation for pretty printing of display material.
   365 
   366   \item @{text "goals_limit = nat"} determines the maximum number of
   367   goals to be printed (for goal-based antiquotation).
   368 
   369   \item @{text "locale = name"} specifies an alternative locale
   370   context used for evaluating and printing the subsequent argument.
   371 
   372   \item @{text "source = bool"} prints the original source text of the
   373   antiquotation arguments, rather than its internal representation.
   374   Note that formal checking of @{antiquotation "thm"}, @{antiquotation
   375   "term"}, etc. is still enabled; use the @{antiquotation "text"}
   376   antiquotation for unchecked output.
   377 
   378   Regular @{text "term"} and @{text "typ"} antiquotations with @{text
   379   "source = false"} involve a full round-trip from the original source
   380   to an internalized logical entity back to a source form, according
   381   to the syntax of the current context.  Thus the printed output is
   382   not under direct control of the author, it may even fluctuate a bit
   383   as the underlying theory is changed later on.
   384 
   385   In contrast, @{text "source = true"} admits direct printing of the
   386   given source text, with the desirable well-formedness check in the
   387   background, but without modification of the printed text.
   388 
   389   \end{description}
   390 
   391   For boolean flags, ``@{text "name = true"}'' may be abbreviated as
   392   ``@{text name}''.  All of the above flags are disabled by default,
   393   unless changed from ML, say in the @{verbatim "ROOT.ML"} of the
   394   logic session.
   395 *}
   396 
   397 
   398 section {* Markup via command tags \label{sec:tags} *}
   399 
   400 text {* Each Isabelle/Isar command may be decorated by additional
   401   presentation tags, to indicate some modification in the way it is
   402   printed in the document.
   403 
   404   \indexouternonterm{tags}
   405   \begin{rail}
   406     tags: ( tag * )
   407     ;
   408     tag: '\%' (ident | string)
   409   \end{rail}
   410 
   411   Some tags are pre-declared for certain classes of commands, serving
   412   as default markup if no tags are given in the text:
   413 
   414   \medskip
   415   \begin{tabular}{ll}
   416     @{text "theory"} & theory begin/end \\
   417     @{text "proof"} & all proof commands \\
   418     @{text "ML"} & all commands involving ML code \\
   419   \end{tabular}
   420 
   421   \medskip The Isabelle document preparation system
   422   \cite{isabelle-sys} allows tagged command regions to be presented
   423   specifically, e.g.\ to fold proof texts, or drop parts of the text
   424   completely.
   425 
   426   For example ``@{command "by"}~@{text "%invisible auto"}'' causes
   427   that piece of proof to be treated as @{text invisible} instead of
   428   @{text "proof"} (the default), which may be shown or hidden
   429   depending on the document setup.  In contrast, ``@{command
   430   "by"}~@{text "%visible auto"}'' forces this text to be shown
   431   invariably.
   432 
   433   Explicit tag specifications within a proof apply to all subsequent
   434   commands of the same level of nesting.  For example, ``@{command
   435   "proof"}~@{text "%visible \<dots>"}~@{command "qed"}'' forces the whole
   436   sub-proof to be typeset as @{text visible} (unless some of its parts
   437   are tagged differently).
   438 
   439   \medskip Command tags merely produce certain markup environments for
   440   type-setting.  The meaning of these is determined by {\LaTeX}
   441   macros, as defined in @{"file" "~~/lib/texinputs/isabelle.sty"} or
   442   by the document author.  The Isabelle document preparation tools
   443   also provide some high-level options to specify the meaning of
   444   arbitrary tags to ``keep'', ``drop'', or ``fold'' the corresponding
   445   parts of the text.  Logic sessions may also specify ``document
   446   versions'', where given tags are interpreted in some particular way.
   447   Again see \cite{isabelle-sys} for further details.
   448 *}
   449 
   450 
   451 section {* Draft presentation *}
   452 
   453 text {*
   454   \begin{matharray}{rcl}
   455     @{command_def "display_drafts"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
   456     @{command_def "print_drafts"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
   457   \end{matharray}
   458 
   459   \begin{rail}
   460     ('display\_drafts' | 'print\_drafts') (name +)
   461     ;
   462   \end{rail}
   463 
   464   \begin{description}
   465 
   466   \item @{command "display_drafts"}~@{text paths} and @{command
   467   "print_drafts"}~@{text paths} perform simple output of a given list
   468   of raw source files.  Only those symbols that do not require
   469   additional {\LaTeX} packages are displayed properly, everything else
   470   is left verbatim.
   471 
   472   \end{description}
   473 *}
   474 
   475 end