src/Doc/Isar_Ref/Outer_Syntax.thy
author wenzelm
Fri Nov 13 21:28:57 2015 +0100 (2015-11-13)
changeset 61662 e77def9a63a6
parent 61658 5dce70aecbfc
child 61663 63af76397a60
permissions -rw-r--r--
tuned whitespace;
wenzelm@61656
     1
(*:maxLineLen=78:*)
wenzelm@61656
     2
wenzelm@27037
     3
theory Outer_Syntax
wenzelm@42651
     4
imports Base Main
wenzelm@27037
     5
begin
wenzelm@27037
     6
wenzelm@58618
     7
chapter \<open>Outer syntax --- the theory language \label{ch:outer-syntax}\<close>
wenzelm@27037
     8
wenzelm@58618
     9
text \<open>
wenzelm@61662
    10
  The rather generic framework of Isabelle/Isar syntax emerges from three main
wenzelm@61662
    11
  syntactic categories: \<^emph>\<open>commands\<close> of the top-level Isar engine (covering
wenzelm@61662
    12
  theory and proof elements), \<^emph>\<open>methods\<close> for general goal refinements
wenzelm@61662
    13
  (analogous to traditional ``tactics''), and \<^emph>\<open>attributes\<close> for operations on
wenzelm@61662
    14
  facts (within a certain context). Subsequently we give a reference of basic
wenzelm@61662
    15
  syntactic entities underlying Isabelle/Isar syntax in a bottom-up manner.
wenzelm@61662
    16
  Concrete theory and proof language elements will be introduced later on.
wenzelm@27037
    17
wenzelm@61421
    18
  \<^medskip>
wenzelm@61662
    19
  In order to get started with writing well-formed Isabelle/Isar documents,
wenzelm@61662
    20
  the most important aspect to be noted is the difference of \<^emph>\<open>inner\<close> versus
wenzelm@61662
    21
  \<^emph>\<open>outer\<close> syntax. Inner syntax is that of Isabelle types and terms of the
wenzelm@61662
    22
  logic, while outer syntax is that of Isabelle/Isar theory sources
wenzelm@61662
    23
  (specifications and proofs). As a general rule, inner syntax entities may
wenzelm@61662
    24
  occur only as \<^emph>\<open>atomic entities\<close> within outer syntax. For example, the
wenzelm@61662
    25
  string \<^verbatim>\<open>"x + y"\<close> and identifier \<^verbatim>\<open>z\<close> are legal term specifications within a
wenzelm@61662
    26
  theory, while \<^verbatim>\<open>x + y\<close> without quotes is not.
wenzelm@27037
    27
wenzelm@61662
    28
  Printed theory documents usually omit quotes to gain readability (this is a
wenzelm@61662
    29
  matter of {\LaTeX} macro setup, say via \<^verbatim>\<open>\isabellestyle\<close>, see also @{cite
wenzelm@61662
    30
  "isabelle-system"}). Experienced users of Isabelle/Isar may easily
wenzelm@61662
    31
  reconstruct the lost technical information, while mere readers need not care
wenzelm@61662
    32
  about quotes at all.
wenzelm@58618
    33
\<close>
wenzelm@27037
    34
wenzelm@27037
    35
wenzelm@58618
    36
section \<open>Commands\<close>
wenzelm@50213
    37
wenzelm@58618
    38
text \<open>
wenzelm@50213
    39
  \begin{matharray}{rcl}
wenzelm@61493
    40
    @{command_def "print_commands"}\<open>\<^sup>*\<close> & : & \<open>any \<rightarrow>\<close> \\
wenzelm@61493
    41
    @{command_def "help"}\<open>\<^sup>*\<close> & : & \<open>any \<rightarrow>\<close> \\
wenzelm@50213
    42
  \end{matharray}
wenzelm@50213
    43
wenzelm@55112
    44
  @{rail \<open>
wenzelm@50213
    45
    @@{command help} (@{syntax name} * )
wenzelm@55112
    46
  \<close>}
wenzelm@50213
    47
wenzelm@61439
    48
  \<^descr> @{command "print_commands"} prints all outer syntax keywords
wenzelm@50213
    49
  and commands.
wenzelm@50213
    50
wenzelm@61493
    51
  \<^descr> @{command "help"}~\<open>pats\<close> retrieves outer syntax
wenzelm@50213
    52
  commands according to the specified name patterns.
wenzelm@58618
    53
\<close>
wenzelm@50213
    54
wenzelm@50213
    55
wenzelm@58618
    56
subsubsection \<open>Examples\<close>
wenzelm@50213
    57
wenzelm@61662
    58
text \<open>
wenzelm@61662
    59
  Some common diagnostic commands are retrieved like this (according to usual
wenzelm@61662
    60
  naming conventions):
wenzelm@61662
    61
\<close>
wenzelm@50213
    62
wenzelm@50213
    63
help "print"
wenzelm@50213
    64
help "find"
wenzelm@50213
    65
wenzelm@50213
    66
wenzelm@58618
    67
section \<open>Lexical matters \label{sec:outer-lex}\<close>
wenzelm@27037
    68
wenzelm@61662
    69
text \<open>
wenzelm@61662
    70
  The outer lexical syntax consists of three main categories of syntax tokens:
wenzelm@28775
    71
wenzelm@61662
    72
    \<^enum> \<^emph>\<open>major keywords\<close> --- the command names that are available
wenzelm@61662
    73
    in the present logic session;
wenzelm@28775
    74
wenzelm@61662
    75
    \<^enum> \<^emph>\<open>minor keywords\<close> --- additional literal tokens required
wenzelm@61662
    76
    by the syntax of commands;
wenzelm@27037
    77
wenzelm@61662
    78
    \<^enum> \<^emph>\<open>named tokens\<close> --- various categories of identifiers etc.
wenzelm@28775
    79
wenzelm@61662
    80
  Major keywords and minor keywords are guaranteed to be disjoint. This helps
wenzelm@61662
    81
  user-interfaces to determine the overall structure of a theory text, without
wenzelm@61662
    82
  knowing the full details of command syntax. Internally, there is some
wenzelm@61662
    83
  additional information about the kind of major keywords, which approximates
wenzelm@61662
    84
  the command type (theory command, proof command etc.).
wenzelm@28775
    85
wenzelm@61662
    86
  Keywords override named tokens. For example, the presence of a command
wenzelm@61662
    87
  called \<^verbatim>\<open>term\<close> inhibits the identifier \<^verbatim>\<open>term\<close>, but the string \<^verbatim>\<open>"term"\<close> can
wenzelm@61662
    88
  be used instead. By convention, the outer syntax always allows quoted
wenzelm@61662
    89
  strings in addition to identifiers, wherever a named entity is expected.
wenzelm@28775
    90
wenzelm@61662
    91
  When tokenizing a given input sequence, the lexer repeatedly takes the
wenzelm@61662
    92
  longest prefix of the input that forms a valid token. Spaces, tabs, newlines
wenzelm@61662
    93
  and formfeeds between tokens serve as explicit separators.
wenzelm@28776
    94
wenzelm@61421
    95
  \<^medskip>
wenzelm@61421
    96
  The categories for named tokens are defined once and for all as follows.
wenzelm@27037
    97
wenzelm@28776
    98
  \begin{center}
wenzelm@28775
    99
  \begin{supertabular}{rcl}
wenzelm@61493
   100
    @{syntax_def ident} & = & \<open>letter (subscript\<^sup>? quasiletter)\<^sup>*\<close> \\
wenzelm@61503
   101
    @{syntax_def longident} & = & \<open>ident(\<close>\<^verbatim>\<open>.\<close>\<open>ident)\<^sup>+\<close> \\
wenzelm@61503
   102
    @{syntax_def symident} & = & \<open>sym\<^sup>+  |\<close>~~\<^verbatim>\<open>\\<close>\<^verbatim>\<open><\<close>\<open>ident\<close>\<^verbatim>\<open>>\<close> \\
wenzelm@61493
   103
    @{syntax_def nat} & = & \<open>digit\<^sup>+\<close> \\
wenzelm@61503
   104
    @{syntax_def float} & = & @{syntax_ref nat}\<^verbatim>\<open>.\<close>@{syntax_ref nat}~~\<open>|\<close>~~\<^verbatim>\<open>-\<close>@{syntax_ref nat}\<^verbatim>\<open>.\<close>@{syntax_ref nat} \\
wenzelm@61503
   105
    @{syntax_def var} & = & \<^verbatim>\<open>?\<close>\<open>ident  |\<close>~~\<^verbatim>\<open>?\<close>\<open>ident\<close>\<^verbatim>\<open>.\<close>\<open>nat\<close> \\
wenzelm@61503
   106
    @{syntax_def typefree} & = & \<^verbatim>\<open>'\<close>\<open>ident\<close> \\
wenzelm@61503
   107
    @{syntax_def typevar} & = & \<^verbatim>\<open>?\<close>\<open>typefree  |\<close>~~\<^verbatim>\<open>?\<close>\<open>typefree\<close>\<^verbatim>\<open>.\<close>\<open>nat\<close> \\
wenzelm@61503
   108
    @{syntax_def string} & = & \<^verbatim>\<open>"\<close> \<open>\<dots>\<close> \<^verbatim>\<open>"\<close> \\
wenzelm@61503
   109
    @{syntax_def altstring} & = & \<^verbatim>\<open>`\<close> \<open>\<dots>\<close> \<^verbatim>\<open>`\<close> \\
wenzelm@61493
   110
    @{syntax_def cartouche} & = & @{verbatim "\<open>"} \<open>\<dots>\<close> @{verbatim "\<close>"} \\
wenzelm@61503
   111
    @{syntax_def verbatim} & = & \<^verbatim>\<open>{*\<close> \<open>\<dots>\<close> \<^verbatim>\<open>*}\<close> \\[1ex]
wenzelm@28775
   112
wenzelm@61503
   113
    \<open>letter\<close> & = & \<open>latin  |\<close>~~\<^verbatim>\<open>\\<close>\<^verbatim>\<open><\<close>\<open>latin\<close>\<^verbatim>\<open>>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\\<close>\<^verbatim>\<open><\<close>\<open>latin latin\<close>\<^verbatim>\<open>>\<close>~~\<open>|  greek  |\<close> \\
wenzelm@61503
   114
    \<open>subscript\<close> & = & \<^verbatim>\<open>\<^sub>\<close> \\
wenzelm@61503
   115
    \<open>quasiletter\<close> & = & \<open>letter  |  digit  |\<close>~~\<^verbatim>\<open>_\<close>~~\<open>|\<close>~~\<^verbatim>\<open>'\<close> \\
wenzelm@61503
   116
    \<open>latin\<close> & = & \<^verbatim>\<open>a\<close>~~\<open>| \<dots> |\<close>~~\<^verbatim>\<open>z\<close>~~\<open>|\<close>~~\<^verbatim>\<open>A\<close>~~\<open>|  \<dots> |\<close>~~\<^verbatim>\<open>Z\<close> \\
wenzelm@61503
   117
    \<open>digit\<close> & = & \<^verbatim>\<open>0\<close>~~\<open>|  \<dots> |\<close>~~\<^verbatim>\<open>9\<close> \\
wenzelm@61503
   118
    \<open>sym\<close> & = & \<^verbatim>\<open>!\<close>~~\<open>|\<close>~~\<^verbatim>\<open>#\<close>~~\<open>|\<close>~~\<^verbatim>\<open>$\<close>~~\<open>|\<close>~~\<^verbatim>\<open>%\<close>~~\<open>|\<close>~~\<^verbatim>\<open>&\<close>~~\<open>|\<close>~~\<^verbatim>\<open>*\<close>~~\<open>|\<close>~~\<^verbatim>\<open>+\<close>~~\<open>|\<close>~~\<^verbatim>\<open>-\<close>~~\<open>|\<close>~~\<^verbatim>\<open>/\<close>~~\<open>|\<close> \\
wenzelm@61503
   119
    & & \<^verbatim>\<open><\<close>~~\<open>|\<close>~~\<^verbatim>\<open>=\<close>~~\<open>|\<close>~~\<^verbatim>\<open>>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>?\<close>~~\<open>|\<close>~~\<^verbatim>\<open>@\<close>~~\<open>|\<close>~~\<^verbatim>\<open>^\<close>~~\<open>|\<close>~~\<^verbatim>\<open>_\<close>~~\<open>|\<close>~~\<^verbatim>\<open>|\<close>~~\<open>|\<close>~~\<^verbatim>\<open>~\<close> \\
wenzelm@61503
   120
    \<open>greek\<close> & = & \<^verbatim>\<open>\<alpha>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<beta>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<gamma>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<delta>\<close>~~\<open>|\<close> \\
wenzelm@61503
   121
          &   & \<^verbatim>\<open>\<epsilon>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<zeta>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<eta>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<theta>\<close>~~\<open>|\<close> \\
wenzelm@61503
   122
          &   & \<^verbatim>\<open>\<iota>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<kappa>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<mu>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<nu>\<close>~~\<open>|\<close> \\
wenzelm@61503
   123
          &   & \<^verbatim>\<open>\<xi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<pi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<rho>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<sigma>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<tau>\<close>~~\<open>|\<close> \\
wenzelm@61503
   124
          &   & \<^verbatim>\<open>\<upsilon>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<phi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<chi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<psi>\<close>~~\<open>|\<close> \\
wenzelm@61503
   125
          &   & \<^verbatim>\<open>\<omega>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Gamma>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Delta>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Theta>\<close>~~\<open>|\<close> \\
wenzelm@61503
   126
          &   & \<^verbatim>\<open>\<Lambda>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Xi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Pi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Sigma>\<close>~~\<open>|\<close> \\
wenzelm@61503
   127
          &   & \<^verbatim>\<open>\<Upsilon>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Phi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Psi>\<close>~~\<open>|\<close>~~\<^verbatim>\<open>\<Omega>\<close> \\
wenzelm@28775
   128
  \end{supertabular}
wenzelm@28776
   129
  \end{center}
wenzelm@27037
   130
wenzelm@61662
   131
  A @{syntax_ref var} or @{syntax_ref typevar} describes an unknown, which is
wenzelm@61662
   132
  internally a pair of base name and index (ML type @{ML_type indexname}).
wenzelm@61662
   133
  These components are either separated by a dot as in \<open>?x.1\<close> or \<open>?x7.3\<close> or
wenzelm@61662
   134
  run together as in \<open>?x1\<close>. The latter form is possible if the base name does
wenzelm@61662
   135
  not end with digits. If the index is 0, it may be dropped altogether: \<open>?x\<close>
wenzelm@61662
   136
  and \<open>?x0\<close> and \<open>?x.0\<close> all refer to the same unknown, with basename \<open>x\<close> and
wenzelm@61662
   137
  index 0.
wenzelm@28778
   138
wenzelm@28778
   139
  The syntax of @{syntax_ref string} admits any characters, including
wenzelm@61662
   140
  newlines; ``\<^verbatim>\<open>"\<close>'' (double-quote) and ``\<^verbatim>\<open>\\<close>'' (backslash) need to be
wenzelm@61662
   141
  escaped by a backslash; arbitrary character codes may be specified as
wenzelm@61662
   142
  ``\<^verbatim>\<open>\\<close>\<open>ddd\<close>'', with three decimal digits. Alternative strings according to
wenzelm@61662
   143
  @{syntax_ref altstring} are analogous, using single back-quotes instead.
wenzelm@28778
   144
wenzelm@58725
   145
  The body of @{syntax_ref verbatim} may consist of any text not containing
wenzelm@61662
   146
  ``\<^verbatim>\<open>*}\<close>''; this allows to include quotes without further escapes, but there
wenzelm@61662
   147
  is no way to escape ``\<^verbatim>\<open>*}\<close>''. Cartouches do not have this limitation.
wenzelm@28778
   148
wenzelm@61662
   149
  A @{syntax_ref cartouche} consists of arbitrary text, with properly balanced
wenzelm@61662
   150
  blocks of ``@{verbatim "\<open>"}~\<open>\<dots>\<close>~@{verbatim "\<close>"}''. Note that the rendering
wenzelm@61662
   151
  of cartouche delimiters is usually like this: ``\<open>\<open> \<dots> \<close>\<close>''.
wenzelm@55033
   152
wenzelm@61662
   153
  Source comments take the form \<^verbatim>\<open>(*\<close>~\<open>\<dots>\<close>~\<^verbatim>\<open>*)\<close> and may be nested, although
wenzelm@61662
   154
  the user-interface might prevent this. Note that this form indicates source
wenzelm@61662
   155
  comments only, which are stripped after lexical analysis of the input. The
wenzelm@61662
   156
  Isar syntax also provides proper \<^emph>\<open>document comments\<close> that are considered as
wenzelm@61662
   157
  part of the text (see \secref{sec:comments}).
wenzelm@27037
   158
wenzelm@61662
   159
  Common mathematical symbols such as \<open>\<forall>\<close> are represented in Isabelle as \<^verbatim>\<open>\<forall>\<close>.
wenzelm@61662
   160
  There are infinitely many Isabelle symbols like this, although proper
wenzelm@61662
   161
  presentation is left to front-end tools such as {\LaTeX} or Isabelle/jEdit.
wenzelm@61662
   162
  A list of predefined Isabelle symbols that work well with these tools is
wenzelm@61662
   163
  given in \appref{app:symbols}. Note that \<^verbatim>\<open>\<lambda>\<close> does not belong to the
wenzelm@61662
   164
  \<open>letter\<close> category, since it is already used differently in the Pure term
wenzelm@61662
   165
  language.
wenzelm@61662
   166
\<close>
wenzelm@27037
   167
wenzelm@27037
   168
wenzelm@58618
   169
section \<open>Common syntax entities\<close>
wenzelm@27037
   170
wenzelm@58618
   171
text \<open>
wenzelm@61662
   172
  We now introduce several basic syntactic entities, such as names, terms, and
wenzelm@61662
   173
  theorem specifications, which are factored out of the actual Isar language
wenzelm@61662
   174
  elements to be described later.
wenzelm@58618
   175
\<close>
wenzelm@27037
   176
wenzelm@27037
   177
wenzelm@58618
   178
subsection \<open>Names\<close>
wenzelm@27037
   179
wenzelm@61662
   180
text \<open>
wenzelm@61662
   181
  Entity @{syntax name} usually refers to any name of types, constants,
wenzelm@61662
   182
  theorems etc.\ that are to be \<^emph>\<open>declared\<close> or \<^emph>\<open>defined\<close> (so qualified
wenzelm@61662
   183
  identifiers are excluded here). Quoted strings provide an escape for
wenzelm@61662
   184
  non-identifier names or those ruled out by outer syntax keywords (e.g.\
wenzelm@61662
   185
  quoted \<^verbatim>\<open>"let"\<close>). Already existing objects are usually referenced by
wenzelm@61662
   186
  @{syntax nameref}.
wenzelm@27037
   187
wenzelm@55112
   188
  @{rail \<open>
wenzelm@42596
   189
    @{syntax_def name}: @{syntax ident} | @{syntax symident} |
wenzelm@42596
   190
      @{syntax string} | @{syntax nat}
wenzelm@27037
   191
    ;
wenzelm@60131
   192
    @{syntax_def par_name}: '(' @{syntax name} ')'
wenzelm@27037
   193
    ;
wenzelm@42596
   194
    @{syntax_def nameref}: @{syntax name} | @{syntax longident}
wenzelm@55112
   195
  \<close>}
wenzelm@58618
   196
\<close>
wenzelm@40296
   197
wenzelm@40296
   198
wenzelm@58618
   199
subsection \<open>Numbers\<close>
wenzelm@40296
   200
wenzelm@61662
   201
text \<open>
wenzelm@61662
   202
  The outer lexical syntax (\secref{sec:outer-lex}) admits natural numbers and
wenzelm@61662
   203
  floating point numbers. These are combined as @{syntax int} and @{syntax
wenzelm@61662
   204
  real} as follows.
wenzelm@40296
   205
wenzelm@55112
   206
  @{rail \<open>
wenzelm@42596
   207
    @{syntax_def int}: @{syntax nat} | '-' @{syntax nat}
wenzelm@27037
   208
    ;
wenzelm@42596
   209
    @{syntax_def real}: @{syntax float} | @{syntax int}
wenzelm@55112
   210
  \<close>}
wenzelm@40296
   211
wenzelm@61662
   212
  Note that there is an overlap with the category @{syntax name}, which also
wenzelm@61662
   213
  includes @{syntax nat}.
wenzelm@58618
   214
\<close>
wenzelm@27037
   215
wenzelm@27037
   216
wenzelm@58618
   217
subsection \<open>Comments \label{sec:comments}\<close>
wenzelm@27037
   218
wenzelm@61579
   219
text \<open>
wenzelm@61579
   220
  Large chunks of plain @{syntax text} are usually given @{syntax verbatim},
wenzelm@61579
   221
  i.e.\ enclosed in \<^verbatim>\<open>{*\<close>~\<open>\<dots>\<close>~\<^verbatim>\<open>*}\<close>, or as @{syntax cartouche} \<open>\<open>\<dots>\<close>\<close>. For
wenzelm@61579
   222
  convenience, any of the smaller text units conforming to @{syntax nameref}
wenzelm@61579
   223
  are admitted as well. A marginal @{syntax comment} is of the form
wenzelm@61579
   224
  \<^verbatim>\<open>--\<close>~@{syntax text} or \<^verbatim>\<open>\<comment>\<close>~@{syntax text}. Any number of these may occur
wenzelm@61579
   225
  within Isabelle/Isar commands.
wenzelm@27037
   226
wenzelm@55112
   227
  @{rail \<open>
wenzelm@56499
   228
    @{syntax_def text}: @{syntax verbatim} | @{syntax cartouche} | @{syntax nameref}
wenzelm@27037
   229
    ;
wenzelm@61579
   230
    @{syntax_def comment}: ('--' | @'\<comment>') @{syntax text}
wenzelm@55112
   231
  \<close>}
wenzelm@58618
   232
\<close>
wenzelm@27037
   233
wenzelm@27037
   234
wenzelm@58618
   235
subsection \<open>Type classes, sorts and arities\<close>
wenzelm@27037
   236
wenzelm@58618
   237
text \<open>
wenzelm@61662
   238
  Classes are specified by plain names. Sorts have a very simple inner syntax,
wenzelm@61662
   239
  which is either a single class name \<open>c\<close> or a list \<open>{c\<^sub>1, \<dots>, c\<^sub>n}\<close> referring
wenzelm@61662
   240
  to the intersection of these classes. The syntax of type arities is given
wenzelm@27037
   241
  directly at the outer level.
wenzelm@27037
   242
wenzelm@55112
   243
  @{rail \<open>
wenzelm@42596
   244
    @{syntax_def classdecl}: @{syntax name} (('<' | '\<subseteq>') (@{syntax nameref} + ','))?
wenzelm@27037
   245
    ;
wenzelm@42596
   246
    @{syntax_def sort}: @{syntax nameref}
wenzelm@27037
   247
    ;
wenzelm@42596
   248
    @{syntax_def arity}: ('(' (@{syntax sort} + ',') ')')? @{syntax sort}
wenzelm@55112
   249
  \<close>}
wenzelm@58618
   250
\<close>
wenzelm@27037
   251
wenzelm@27037
   252
wenzelm@58618
   253
subsection \<open>Types and terms \label{sec:types-terms}\<close>
wenzelm@27037
   254
wenzelm@58618
   255
text \<open>
wenzelm@61662
   256
  The actual inner Isabelle syntax, that of types and terms of the logic, is
wenzelm@61662
   257
  far too sophisticated in order to be modelled explicitly at the outer theory
wenzelm@61662
   258
  level. Basically, any such entity has to be quoted to turn it into a single
wenzelm@61662
   259
  token (the parsing and type-checking is performed internally later). For
wenzelm@61662
   260
  convenience, a slightly more liberal convention is adopted: quotes may be
wenzelm@61662
   261
  omitted for any type or term that is already atomic at the outer level. For
wenzelm@61662
   262
  example, one may just write \<^verbatim>\<open>x\<close> instead of quoted \<^verbatim>\<open>"x"\<close>. Note that
wenzelm@61662
   263
  symbolic identifiers (e.g.\ \<^verbatim>\<open>++\<close> or \<open>\<forall>\<close> are available as well, provided
wenzelm@61662
   264
  these have not been superseded by commands or other keywords already (such
wenzelm@61662
   265
  as \<^verbatim>\<open>=\<close> or \<^verbatim>\<open>+\<close>).
wenzelm@27037
   266
wenzelm@55112
   267
  @{rail \<open>
wenzelm@42596
   268
    @{syntax_def type}: @{syntax nameref} | @{syntax typefree} |
wenzelm@42596
   269
      @{syntax typevar}
wenzelm@27037
   270
    ;
wenzelm@42596
   271
    @{syntax_def term}: @{syntax nameref} | @{syntax var}
wenzelm@27037
   272
    ;
wenzelm@42596
   273
    @{syntax_def prop}: @{syntax term}
wenzelm@55112
   274
  \<close>}
wenzelm@27037
   275
wenzelm@59853
   276
  Positional instantiations are specified as a sequence of terms, or the
wenzelm@61493
   277
  placeholder ``\<open>_\<close>'' (underscore), which means to skip a position.
wenzelm@27037
   278
wenzelm@55112
   279
  @{rail \<open>
wenzelm@42596
   280
    @{syntax_def inst}: '_' | @{syntax term}
wenzelm@27037
   281
    ;
wenzelm@42596
   282
    @{syntax_def insts}: (@{syntax inst} *)
wenzelm@55112
   283
  \<close>}
wenzelm@27037
   284
wenzelm@61662
   285
  Named instantiations are specified as pairs of assignments \<open>v = t\<close>, which
wenzelm@61662
   286
  refer to schematic variables in some theorem that is instantiated. Both type
wenzelm@61662
   287
  and terms instantiations are admitted, and distinguished by the usual syntax
wenzelm@61662
   288
  of variable names.
wenzelm@59853
   289
wenzelm@59853
   290
  @{rail \<open>
wenzelm@59853
   291
    @{syntax_def named_inst}: variable '=' (type | term)
wenzelm@59853
   292
    ;
wenzelm@59853
   293
    @{syntax_def named_insts}: (named_inst @'and' +)
wenzelm@59853
   294
    ;
wenzelm@59853
   295
    variable: @{syntax name} | @{syntax var} | @{syntax typefree} | @{syntax typevar}
wenzelm@59853
   296
  \<close>}
wenzelm@59853
   297
wenzelm@61662
   298
  Type declarations and definitions usually refer to @{syntax typespec} on the
wenzelm@61662
   299
  left-hand side. This models basic type constructor application at the outer
wenzelm@61662
   300
  syntax level. Note that only plain postfix notation is available here, but
wenzelm@61662
   301
  no infixes.
wenzelm@27037
   302
wenzelm@55112
   303
  @{rail \<open>
wenzelm@42596
   304
    @{syntax_def typespec}:
wenzelm@42596
   305
      (() | @{syntax typefree} | '(' ( @{syntax typefree} + ',' ) ')') @{syntax name}
wenzelm@27037
   306
    ;
wenzelm@42705
   307
    @{syntax_def typespec_sorts}:
wenzelm@42596
   308
      (() | (@{syntax typefree} ('::' @{syntax sort})?) |
wenzelm@42596
   309
        '(' ( (@{syntax typefree} ('::' @{syntax sort})?) + ',' ) ')') @{syntax name}
wenzelm@55112
   310
  \<close>}
wenzelm@58618
   311
\<close>
wenzelm@27037
   312
wenzelm@27037
   313
wenzelm@58618
   314
subsection \<open>Term patterns and declarations \label{sec:term-decls}\<close>
wenzelm@28754
   315
wenzelm@61662
   316
text \<open>
wenzelm@61662
   317
  Wherever explicit propositions (or term fragments) occur in a proof text,
wenzelm@61662
   318
  casual binding of schematic term variables may be given specified via
wenzelm@61662
   319
  patterns of the form ``\<open>(\<IS> p\<^sub>1 \<dots> p\<^sub>n)\<close>''. This works both for @{syntax
wenzelm@61662
   320
  term} and @{syntax prop}.
wenzelm@28754
   321
wenzelm@55112
   322
  @{rail \<open>
wenzelm@42705
   323
    @{syntax_def term_pat}: '(' (@'is' @{syntax term} +) ')'
wenzelm@28754
   324
    ;
wenzelm@42705
   325
    @{syntax_def prop_pat}: '(' (@'is' @{syntax prop} +) ')'
wenzelm@55112
   326
  \<close>}
wenzelm@28754
   327
wenzelm@61421
   328
  \<^medskip>
wenzelm@61662
   329
  Declarations of local variables \<open>x :: \<tau>\<close> and logical propositions \<open>a : \<phi>\<close>
wenzelm@61662
   330
  represent different views on the same principle of introducing a local
wenzelm@61662
   331
  scope. In practice, one may usually omit the typing of @{syntax vars} (due
wenzelm@61662
   332
  to type-inference), and the naming of propositions (due to implicit
wenzelm@61662
   333
  references of current facts). In any case, Isar proof elements usually admit
wenzelm@61662
   334
  to introduce multiple such items simultaneously.
wenzelm@28754
   335
wenzelm@55112
   336
  @{rail \<open>
wenzelm@42596
   337
    @{syntax_def vars}: (@{syntax name} +) ('::' @{syntax type})?
wenzelm@28754
   338
    ;
wenzelm@42705
   339
    @{syntax_def props}: @{syntax thmdecl}? (@{syntax prop} @{syntax prop_pat}? +)
wenzelm@61658
   340
    ;
wenzelm@61658
   341
    @{syntax_def props'}: (@{syntax prop} @{syntax prop_pat}? +)
wenzelm@55112
   342
  \<close>}
wenzelm@28754
   343
wenzelm@61662
   344
  The treatment of multiple declarations corresponds to the complementary
wenzelm@61662
   345
  focus of @{syntax vars} versus @{syntax props}. In ``\<open>x\<^sub>1 \<dots> x\<^sub>n :: \<tau>\<close>'' the
wenzelm@61662
   346
  typing refers to all variables, while in \<open>a: \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n\<close> the naming refers to
wenzelm@61662
   347
  all propositions collectively. Isar language elements that refer to @{syntax
wenzelm@61662
   348
  vars} or @{syntax props} typically admit separate typings or namings via
wenzelm@61662
   349
  another level of iteration, with explicit @{keyword_ref "and"} separators;
wenzelm@61662
   350
  e.g.\ see @{command "fix"} and @{command "assume"} in
wenzelm@28754
   351
  \secref{sec:proof-context}.
wenzelm@59785
   352
wenzelm@59785
   353
  @{rail \<open>
wenzelm@59785
   354
    @{syntax_def "fixes"}:
wenzelm@59785
   355
      ((@{syntax name} ('::' @{syntax type})? @{syntax mixfix}? | @{syntax vars}) + @'and')
wenzelm@59785
   356
    ;
wenzelm@59785
   357
    @{syntax_def "for_fixes"}: (@'for' @{syntax "fixes"})?
wenzelm@59785
   358
  \<close>}
wenzelm@59785
   359
wenzelm@59785
   360
  The category @{syntax "fixes"} is a richer variant of @{syntax vars}: it
wenzelm@59785
   361
  admits specification of mixfix syntax for the entities that are introduced
wenzelm@61662
   362
  into the context. An optional suffix ``@{keyword "for"}~\<open>fixes\<close>'' is
wenzelm@61662
   363
  admitted in many situations to indicate a so-called ``eigen-context'' of a
wenzelm@61662
   364
  formal element: the result will be exported and thus generalized over the
wenzelm@61662
   365
  given variables.
wenzelm@61662
   366
\<close>
wenzelm@28754
   367
wenzelm@28754
   368
wenzelm@58618
   369
subsection \<open>Attributes and theorems \label{sec:syn-att}\<close>
wenzelm@27037
   370
wenzelm@61662
   371
text \<open>
wenzelm@61662
   372
  Attributes have their own ``semi-inner'' syntax, in the sense that input
wenzelm@61662
   373
  conforming to @{syntax args} below is parsed by the attribute a second time.
wenzelm@61662
   374
  The attribute argument specifications may be any sequence of atomic entities
wenzelm@61662
   375
  (identifiers, strings etc.), or properly bracketed argument lists. Below
wenzelm@61662
   376
  @{syntax atom} refers to any atomic entity, including any @{syntax keyword}
wenzelm@61662
   377
  conforming to @{syntax symident}.
wenzelm@27037
   378
wenzelm@55112
   379
  @{rail \<open>
wenzelm@42596
   380
    @{syntax_def atom}: @{syntax nameref} | @{syntax typefree} |
wenzelm@42596
   381
      @{syntax typevar} | @{syntax var} | @{syntax nat} | @{syntax float} |
wenzelm@55045
   382
      @{syntax keyword} | @{syntax cartouche}
wenzelm@27037
   383
    ;
wenzelm@42596
   384
    arg: @{syntax atom} | '(' @{syntax args} ')' | '[' @{syntax args} ']'
wenzelm@27037
   385
    ;
wenzelm@42596
   386
    @{syntax_def args}: arg *
wenzelm@27037
   387
    ;
wenzelm@42596
   388
    @{syntax_def attributes}: '[' (@{syntax nameref} @{syntax args} * ',') ']'
wenzelm@55112
   389
  \<close>}
wenzelm@27037
   390
wenzelm@61662
   391
  Theorem specifications come in several flavors: @{syntax axmdecl} and
wenzelm@61662
   392
  @{syntax thmdecl} usually refer to axioms, assumptions or results of goal
wenzelm@61662
   393
  statements, while @{syntax thmdef} collects lists of existing theorems.
wenzelm@61662
   394
  Existing theorems are given by @{syntax thmref} and @{syntax thmrefs}, the
wenzelm@61662
   395
  former requires an actual singleton result.
wenzelm@27037
   396
wenzelm@27037
   397
  There are three forms of theorem references:
wenzelm@60674
   398
wenzelm@61662
   399
    \<^enum> named facts \<open>a\<close>,
wenzelm@27037
   400
wenzelm@61662
   401
    \<^enum> selections from named facts \<open>a(i)\<close> or \<open>a(j - k)\<close>,
wenzelm@27037
   402
wenzelm@61662
   403
    \<^enum> literal fact propositions using token syntax @{syntax_ref altstring}
wenzelm@61662
   404
    \<^verbatim>\<open>`\<close>\<open>\<phi>\<close>\<^verbatim>\<open>`\<close> or @{syntax_ref cartouche}
wenzelm@61662
   405
    \<open>\<open>\<phi>\<close>\<close> (see also method @{method_ref fact}).
wenzelm@27037
   406
wenzelm@61662
   407
  Any kind of theorem specification may include lists of attributes both on
wenzelm@61662
   408
  the left and right hand sides; attributes are applied to any immediately
wenzelm@61662
   409
  preceding fact. If names are omitted, the theorems are not stored within the
wenzelm@61662
   410
  theorem database of the theory or proof context, but any given attributes
wenzelm@61662
   411
  are applied nonetheless.
wenzelm@27037
   412
wenzelm@61662
   413
  An extra pair of brackets around attributes (like ``\<open>[[simproc a]]\<close>'')
wenzelm@61662
   414
  abbreviates a theorem reference involving an internal dummy fact, which will
wenzelm@61662
   415
  be ignored later on. So only the effect of the attribute on the background
wenzelm@61662
   416
  context will persist. This form of in-place declarations is particularly
wenzelm@61662
   417
  useful with commands like @{command "declare"} and @{command "using"}.
wenzelm@27037
   418
wenzelm@55112
   419
  @{rail \<open>
wenzelm@42596
   420
    @{syntax_def axmdecl}: @{syntax name} @{syntax attributes}? ':'
wenzelm@42596
   421
    ;
wenzelm@60631
   422
    @{syntax_def thmbind}:
wenzelm@60631
   423
      @{syntax name} @{syntax attributes} | @{syntax name} | @{syntax attributes}
wenzelm@60631
   424
    ;
wenzelm@42596
   425
    @{syntax_def thmdecl}: thmbind ':'
wenzelm@27037
   426
    ;
wenzelm@42596
   427
    @{syntax_def thmdef}: thmbind '='
wenzelm@27037
   428
    ;
wenzelm@42596
   429
    @{syntax_def thmref}:
wenzelm@56499
   430
      (@{syntax nameref} selection? | @{syntax altstring} | @{syntax cartouche})
wenzelm@56499
   431
        @{syntax attributes}? |
wenzelm@42596
   432
      '[' @{syntax attributes} ']'
wenzelm@27037
   433
    ;
wenzelm@42596
   434
    @{syntax_def thmrefs}: @{syntax thmref} +
wenzelm@27037
   435
    ;
wenzelm@42596
   436
    selection: '(' ((@{syntax nat} | @{syntax nat} '-' @{syntax nat}?) + ',') ')'
wenzelm@55112
   437
  \<close>}
wenzelm@58618
   438
\<close>
wenzelm@27037
   439
wenzelm@60674
   440
wenzelm@60674
   441
section \<open>Diagnostic commands\<close>
wenzelm@60674
   442
wenzelm@60674
   443
text \<open>
wenzelm@60674
   444
  \begin{matharray}{rcl}
wenzelm@61493
   445
    @{command_def "print_theory"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   446
    @{command_def "print_definitions"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   447
    @{command_def "print_methods"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   448
    @{command_def "print_attributes"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   449
    @{command_def "print_theorems"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   450
    @{command_def "find_theorems"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   451
    @{command_def "find_consts"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   452
    @{command_def "thm_deps"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   453
    @{command_def "unused_thms"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   454
    @{command_def "print_facts"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@61493
   455
    @{command_def "print_term_bindings"}\<open>\<^sup>*\<close> & : & \<open>context \<rightarrow>\<close> \\
wenzelm@60674
   456
  \end{matharray}
wenzelm@60674
   457
wenzelm@60674
   458
  @{rail \<open>
wenzelm@60674
   459
    (@@{command print_theory} |
wenzelm@61252
   460
      @@{command print_definitions} |
wenzelm@60674
   461
      @@{command print_methods} |
wenzelm@60674
   462
      @@{command print_attributes} |
wenzelm@60674
   463
      @@{command print_theorems} |
wenzelm@60674
   464
      @@{command print_facts}) ('!'?)
wenzelm@60674
   465
    ;
wenzelm@60674
   466
    @@{command find_theorems} ('(' @{syntax nat}? 'with_dups'? ')')? \<newline> (thm_criterion*)
wenzelm@60674
   467
    ;
wenzelm@60674
   468
    thm_criterion: ('-'?) ('name' ':' @{syntax nameref} | 'intro' | 'elim' | 'dest' |
wenzelm@60674
   469
      'solves' | 'simp' ':' @{syntax term} | @{syntax term})
wenzelm@60674
   470
    ;
wenzelm@60674
   471
    @@{command find_consts} (const_criterion*)
wenzelm@60674
   472
    ;
wenzelm@60674
   473
    const_criterion: ('-'?)
wenzelm@60674
   474
      ('name' ':' @{syntax nameref} | 'strict' ':' @{syntax type} | @{syntax type})
wenzelm@60674
   475
    ;
wenzelm@60674
   476
    @@{command thm_deps} @{syntax thmrefs}
wenzelm@60674
   477
    ;
wenzelm@60674
   478
    @@{command unused_thms} ((@{syntax name} +) '-' (@{syntax name} * ))?
wenzelm@60674
   479
  \<close>}
wenzelm@60674
   480
wenzelm@61662
   481
  These commands print certain parts of the theory and proof context. Note
wenzelm@61662
   482
  that there are some further ones available, such as for the set of rules
wenzelm@61662
   483
  declared for simplifications.
wenzelm@60674
   484
wenzelm@61439
   485
  \<^descr> @{command "print_theory"} prints the main logical content of the
wenzelm@61493
   486
  background theory; the ``\<open>!\<close>'' option indicates extra verbosity.
wenzelm@60674
   487
wenzelm@61439
   488
  \<^descr> @{command "print_definitions"} prints dependencies of definitional
wenzelm@61252
   489
  specifications within the background theory, which may be constants
wenzelm@61252
   490
  \secref{sec:consts} or types (\secref{sec:types-pure},
wenzelm@61662
   491
  \secref{sec:hol-typedef}); the ``\<open>!\<close>'' option indicates extra verbosity.
wenzelm@61252
   492
wenzelm@61439
   493
  \<^descr> @{command "print_methods"} prints all proof methods available in the
wenzelm@61662
   494
  current theory context; the ``\<open>!\<close>'' option indicates extra verbosity.
wenzelm@60674
   495
wenzelm@61439
   496
  \<^descr> @{command "print_attributes"} prints all attributes available in the
wenzelm@61662
   497
  current theory context; the ``\<open>!\<close>'' option indicates extra verbosity.
wenzelm@60674
   498
wenzelm@61439
   499
  \<^descr> @{command "print_theorems"} prints theorems of the background theory
wenzelm@61662
   500
  resulting from the last command; the ``\<open>!\<close>'' option indicates extra
wenzelm@61662
   501
  verbosity.
wenzelm@60674
   502
wenzelm@61662
   503
  \<^descr> @{command "print_facts"} prints all local facts of the current context,
wenzelm@61662
   504
  both named and unnamed ones; the ``\<open>!\<close>'' option indicates extra verbosity.
wenzelm@60674
   505
wenzelm@61662
   506
  \<^descr> @{command "print_term_bindings"} prints all term bindings that are present
wenzelm@61662
   507
  in the context.
wenzelm@60674
   508
wenzelm@61662
   509
  \<^descr> @{command "find_theorems"}~\<open>criteria\<close> retrieves facts from the theory or
wenzelm@61662
   510
  proof context matching all of given search criteria. The criterion \<open>name: p\<close>
wenzelm@61662
   511
  selects all theorems whose fully qualified name matches pattern \<open>p\<close>, which
wenzelm@61662
   512
  may contain ``\<open>*\<close>'' wildcards. The criteria \<open>intro\<close>, \<open>elim\<close>, and \<open>dest\<close>
wenzelm@61662
   513
  select theorems that match the current goal as introduction, elimination or
wenzelm@61662
   514
  destruction rules, respectively. The criterion \<open>solves\<close> returns all rules
wenzelm@61662
   515
  that would directly solve the current goal. The criterion \<open>simp: t\<close> selects
wenzelm@61662
   516
  all rewrite rules whose left-hand side matches the given term. The criterion
wenzelm@61662
   517
  term \<open>t\<close> selects all theorems that contain the pattern \<open>t\<close> -- as usual,
wenzelm@61662
   518
  patterns may contain occurrences of the dummy ``\<open>_\<close>'', schematic variables,
wenzelm@61662
   519
  and type constraints.
wenzelm@60674
   520
wenzelm@61662
   521
  Criteria can be preceded by ``\<open>-\<close>'' to select theorems that do \<^emph>\<open>not\<close> match.
wenzelm@61662
   522
  Note that giving the empty list of criteria yields \<^emph>\<open>all\<close> currently known
wenzelm@61662
   523
  facts. An optional limit for the number of printed facts may be given; the
wenzelm@61662
   524
  default is 40. By default, duplicates are removed from the search result.
wenzelm@61662
   525
  Use \<open>with_dups\<close> to display duplicates.
wenzelm@60674
   526
wenzelm@61662
   527
  \<^descr> @{command "find_consts"}~\<open>criteria\<close> prints all constants whose type meets
wenzelm@61662
   528
  all of the given criteria. The criterion \<open>strict: ty\<close> is met by any type
wenzelm@61662
   529
  that matches the type pattern \<open>ty\<close>. Patterns may contain both the dummy type
wenzelm@61662
   530
  ``\<open>_\<close>'' and sort constraints. The criterion \<open>ty\<close> is similar, but it also
wenzelm@61662
   531
  matches against subtypes. The criterion \<open>name: p\<close> and the prefix ``\<open>-\<close>''
wenzelm@61662
   532
  function as described for @{command "find_theorems"}.
wenzelm@60674
   533
wenzelm@61662
   534
  \<^descr> @{command "thm_deps"}~\<open>a\<^sub>1 \<dots> a\<^sub>n\<close> visualizes dependencies of facts, using
wenzelm@61662
   535
  Isabelle's graph browser tool (see also @{cite "isabelle-system"}).
wenzelm@60674
   536
wenzelm@61662
   537
  \<^descr> @{command "unused_thms"}~\<open>A\<^sub>1 \<dots> A\<^sub>m - B\<^sub>1 \<dots> B\<^sub>n\<close> displays all theorems
wenzelm@61662
   538
  that are proved in theories \<open>B\<^sub>1 \<dots> B\<^sub>n\<close> or their parents but not in \<open>A\<^sub>1 \<dots>
wenzelm@61662
   539
  A\<^sub>m\<close> or their parents and that are never used. If \<open>n\<close> is \<open>0\<close>, the end of the
wenzelm@61662
   540
  range of theories defaults to the current theory. If no range is specified,
wenzelm@60674
   541
  only the unused theorems in the current theory are displayed.
wenzelm@60674
   542
\<close>
wenzelm@60674
   543
wenzelm@27037
   544
end