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