src/Doc/IsarRef/Outer_Syntax.thy
author wenzelm
Sat Jan 18 19:15:12 2014 +0100 (2014-01-18)
changeset 55033 8e8243975860
parent 53059 f4811f3628dc
child 55045 99056d23e05b
permissions -rw-r--r--
support for nested text cartouches;
clarified Symbol.is_symbolic: exclude \<open> and \<close>;
wenzelm@27037
     1
theory Outer_Syntax
wenzelm@42651
     2
imports Base Main
wenzelm@27037
     3
begin
wenzelm@27037
     4
wenzelm@47114
     5
chapter {* Outer syntax --- the theory language \label{ch:outer-syntax} *}
wenzelm@27037
     6
wenzelm@27037
     7
text {*
wenzelm@27037
     8
  The rather generic framework of Isabelle/Isar syntax emerges from
wenzelm@27037
     9
  three main syntactic categories: \emph{commands} of the top-level
wenzelm@27037
    10
  Isar engine (covering theory and proof elements), \emph{methods} for
wenzelm@27037
    11
  general goal refinements (analogous to traditional ``tactics''), and
wenzelm@27037
    12
  \emph{attributes} for operations on facts (within a certain
wenzelm@27037
    13
  context).  Subsequently we give a reference of basic syntactic
wenzelm@27037
    14
  entities underlying Isabelle/Isar syntax in a bottom-up manner.
wenzelm@27037
    15
  Concrete theory and proof language elements will be introduced later
wenzelm@27037
    16
  on.
wenzelm@27037
    17
wenzelm@27037
    18
  \medskip In order to get started with writing well-formed
wenzelm@27037
    19
  Isabelle/Isar documents, the most important aspect to be noted is
wenzelm@27037
    20
  the difference of \emph{inner} versus \emph{outer} syntax.  Inner
wenzelm@27037
    21
  syntax is that of Isabelle types and terms of the logic, while outer
wenzelm@27037
    22
  syntax is that of Isabelle/Isar theory sources (specifications and
wenzelm@27037
    23
  proofs).  As a general rule, inner syntax entities may occur only as
wenzelm@27037
    24
  \emph{atomic entities} within outer syntax.  For example, the string
wenzelm@27037
    25
  @{verbatim "\"x + y\""} and identifier @{verbatim z} are legal term
wenzelm@27037
    26
  specifications within a theory, while @{verbatim "x + y"} without
wenzelm@27037
    27
  quotes is not.
wenzelm@27037
    28
wenzelm@27037
    29
  Printed theory documents usually omit quotes to gain readability
wenzelm@27037
    30
  (this is a matter of {\LaTeX} macro setup, say via @{verbatim
wenzelm@27037
    31
  "\\isabellestyle"}, see also \cite{isabelle-sys}).  Experienced
wenzelm@27037
    32
  users of Isabelle/Isar may easily reconstruct the lost technical
wenzelm@27037
    33
  information, while mere readers need not care about quotes at all.
wenzelm@27037
    34
wenzelm@27037
    35
  \medskip Isabelle/Isar input may contain any number of input
wenzelm@27037
    36
  termination characters ``@{verbatim ";"}'' (semicolon) to separate
wenzelm@27037
    37
  commands explicitly.  This is particularly useful in interactive
wenzelm@27037
    38
  shell sessions to make clear where the current command is intended
wenzelm@27037
    39
  to end.  Otherwise, the interpreter loop will continue to issue a
wenzelm@27037
    40
  secondary prompt ``@{verbatim "#"}'' until an end-of-command is
wenzelm@27037
    41
  clearly recognized from the input syntax, e.g.\ encounter of the
wenzelm@27037
    42
  next command keyword.
wenzelm@27037
    43
wenzelm@51058
    44
  More advanced interfaces such as Isabelle/jEdit \cite{Wenzel:2012}
wenzelm@51058
    45
  and Proof~General \cite{proofgeneral} do not require explicit
wenzelm@51062
    46
  semicolons: command spans are determined by inspecting the content
wenzelm@51062
    47
  of the editor buffer.  In the printed presentation of Isabelle/Isar
wenzelm@51062
    48
  documents semicolons are omitted altogether for readability.
wenzelm@27037
    49
wenzelm@27037
    50
  \begin{warn}
wenzelm@27037
    51
    Proof~General requires certain syntax classification tables in
wenzelm@27037
    52
    order to achieve properly synchronized interaction with the
wenzelm@27037
    53
    Isabelle/Isar process.  These tables need to be consistent with
wenzelm@27037
    54
    the Isabelle version and particular logic image to be used in a
wenzelm@27037
    55
    running session (common object-logics may well change the outer
wenzelm@27037
    56
    syntax).  The standard setup should work correctly with any of the
wenzelm@27037
    57
    ``official'' logic images derived from Isabelle/HOL (including
wenzelm@27037
    58
    HOLCF etc.).  Users of alternative logics may need to tell
wenzelm@27037
    59
    Proof~General explicitly, e.g.\ by giving an option @{verbatim "-k ZF"}
wenzelm@27037
    60
    (in conjunction with @{verbatim "-l ZF"}, to specify the default
wenzelm@27037
    61
    logic image).  Note that option @{verbatim "-L"} does both
wenzelm@27037
    62
    of this at the same time.
wenzelm@27037
    63
  \end{warn}
wenzelm@27037
    64
*}
wenzelm@27037
    65
wenzelm@27037
    66
wenzelm@50213
    67
section {* Commands *}
wenzelm@50213
    68
wenzelm@50213
    69
text {*
wenzelm@50213
    70
  \begin{matharray}{rcl}
wenzelm@50213
    71
    @{command_def "print_commands"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@50213
    72
    @{command_def "help"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@50213
    73
  \end{matharray}
wenzelm@50213
    74
wenzelm@50213
    75
  @{rail "
wenzelm@50213
    76
    @@{command help} (@{syntax name} * )
wenzelm@50213
    77
  "}
wenzelm@50213
    78
wenzelm@50213
    79
  \begin{description}
wenzelm@50213
    80
wenzelm@50213
    81
  \item @{command "print_commands"} prints all outer syntax keywords
wenzelm@50213
    82
  and commands.
wenzelm@50213
    83
wenzelm@50213
    84
  \item @{command "help"}~@{text "pats"} retrieves outer syntax
wenzelm@50213
    85
  commands according to the specified name patterns.
wenzelm@50213
    86
wenzelm@50213
    87
  \end{description}
wenzelm@50213
    88
*}
wenzelm@50213
    89
wenzelm@50213
    90
wenzelm@50213
    91
subsubsection {* Examples *}
wenzelm@50213
    92
wenzelm@50213
    93
text {* Some common diagnostic commands are retrieved like this
wenzelm@50213
    94
  (according to usual naming conventions): *}
wenzelm@50213
    95
wenzelm@50213
    96
help "print"
wenzelm@50213
    97
help "find"
wenzelm@50213
    98
wenzelm@50213
    99
wenzelm@28774
   100
section {* Lexical matters \label{sec:outer-lex} *}
wenzelm@27037
   101
wenzelm@28775
   102
text {* The outer lexical syntax consists of three main categories of
wenzelm@28776
   103
  syntax tokens:
wenzelm@28775
   104
wenzelm@28775
   105
  \begin{enumerate}
wenzelm@28775
   106
wenzelm@28775
   107
  \item \emph{major keywords} --- the command names that are available
wenzelm@28775
   108
  in the present logic session;
wenzelm@28775
   109
wenzelm@28775
   110
  \item \emph{minor keywords} --- additional literal tokens required
wenzelm@28775
   111
  by the syntax of commands;
wenzelm@28775
   112
wenzelm@28776
   113
  \item \emph{named tokens} --- various categories of identifiers etc.
wenzelm@27037
   114
wenzelm@28775
   115
  \end{enumerate}
wenzelm@28775
   116
wenzelm@28776
   117
  Major keywords and minor keywords are guaranteed to be disjoint.
wenzelm@28775
   118
  This helps user-interfaces to determine the overall structure of a
wenzelm@28775
   119
  theory text, without knowing the full details of command syntax.
wenzelm@28776
   120
  Internally, there is some additional information about the kind of
wenzelm@28776
   121
  major keywords, which approximates the command type (theory command,
wenzelm@28776
   122
  proof command etc.).
wenzelm@28775
   123
wenzelm@28775
   124
  Keywords override named tokens.  For example, the presence of a
wenzelm@28775
   125
  command called @{verbatim term} inhibits the identifier @{verbatim
wenzelm@28775
   126
  term}, but the string @{verbatim "\"term\""} can be used instead.
wenzelm@28775
   127
  By convention, the outer syntax always allows quoted strings in
wenzelm@28775
   128
  addition to identifiers, wherever a named entity is expected.
wenzelm@28775
   129
wenzelm@28776
   130
  When tokenizing a given input sequence, the lexer repeatedly takes
wenzelm@28776
   131
  the longest prefix of the input that forms a valid token.  Spaces,
wenzelm@28776
   132
  tabs, newlines and formfeeds between tokens serve as explicit
wenzelm@28776
   133
  separators.
wenzelm@28776
   134
wenzelm@28775
   135
  \medskip The categories for named tokens are defined once and for
wenzelm@28775
   136
  all as follows.
wenzelm@27037
   137
wenzelm@28776
   138
  \begin{center}
wenzelm@28775
   139
  \begin{supertabular}{rcl}
wenzelm@53059
   140
    @{syntax_def ident} & = & @{text "letter (subscript\<^sup>? quasiletter)\<^sup>*"} \\
wenzelm@28775
   141
    @{syntax_def longident} & = & @{text "ident("}@{verbatim "."}@{text "ident)\<^sup>+"} \\
wenzelm@28775
   142
    @{syntax_def symident} & = & @{text "sym\<^sup>+  |  "}@{verbatim "\\"}@{verbatim "<"}@{text ident}@{verbatim ">"} \\
wenzelm@28775
   143
    @{syntax_def nat} & = & @{text "digit\<^sup>+"} \\
wenzelm@40290
   144
    @{syntax_def float} & = & @{syntax_ref nat}@{verbatim "."}@{syntax_ref nat}@{text "  |  "}@{verbatim "-"}@{syntax_ref nat}@{verbatim "."}@{syntax_ref nat} \\
wenzelm@28775
   145
    @{syntax_def var} & = & @{verbatim "?"}@{text "ident  |  "}@{verbatim "?"}@{text ident}@{verbatim "."}@{text nat} \\
wenzelm@28775
   146
    @{syntax_def typefree} & = & @{verbatim "'"}@{text ident} \\
wenzelm@28775
   147
    @{syntax_def typevar} & = & @{verbatim "?"}@{text "typefree  |  "}@{verbatim "?"}@{text typefree}@{verbatim "."}@{text nat} \\
wenzelm@28775
   148
    @{syntax_def string} & = & @{verbatim "\""} @{text "\<dots>"} @{verbatim "\""} \\
wenzelm@28775
   149
    @{syntax_def altstring} & = & @{verbatim "`"} @{text "\<dots>"} @{verbatim "`"} \\
wenzelm@55033
   150
    @{syntax_def cartouche} & = & @{verbatim "\<open>"} @{text "\<dots>"} @{verbatim "\<close>"} \\
wenzelm@28775
   151
    @{syntax_def verbatim} & = & @{verbatim "{*"} @{text "\<dots>"} @{verbatim "*"}@{verbatim "}"} \\[1ex]
wenzelm@28775
   152
wenzelm@28775
   153
    @{text letter} & = & @{text "latin  |  "}@{verbatim "\\"}@{verbatim "<"}@{text latin}@{verbatim ">"}@{text "  |  "}@{verbatim "\\"}@{verbatim "<"}@{text "latin latin"}@{verbatim ">"}@{text "  |  greek  |"} \\
wenzelm@53059
   154
    @{text subscript} & = & @{verbatim "\<^sub>"} \\
wenzelm@28775
   155
    @{text quasiletter} & = & @{text "letter  |  digit  |  "}@{verbatim "_"}@{text "  |  "}@{verbatim "'"} \\
wenzelm@28775
   156
    @{text latin} & = & @{verbatim a}@{text "  | \<dots> |  "}@{verbatim z}@{text "  |  "}@{verbatim A}@{text "  |  \<dots> |  "}@{verbatim Z} \\
wenzelm@28775
   157
    @{text digit} & = & @{verbatim "0"}@{text "  |  \<dots> |  "}@{verbatim "9"} \\
wenzelm@28775
   158
    @{text sym} & = & @{verbatim "!"}@{text "  |  "}@{verbatim "#"}@{text "  |  "}@{verbatim "$"}@{text "  |  "}@{verbatim "%"}@{text "  |  "}@{verbatim "&"}@{text "  |  "}@{verbatim "*"}@{text "  |  "}@{verbatim "+"}@{text "  |  "}@{verbatim "-"}@{text "  |  "}@{verbatim "/"}@{text "  |"} \\
wenzelm@28775
   159
    & & @{verbatim "<"}@{text "  |  "}@{verbatim "="}@{text "  |  "}@{verbatim ">"}@{text "  |  "}@{verbatim "?"}@{text "  |  "}@{verbatim "@"}@{text "  |  "}@{verbatim "^"}@{text "  |  "}@{verbatim "_"}@{text "  |  "}@{verbatim "|"}@{text "  |  "}@{verbatim "~"} \\
wenzelm@28775
   160
    @{text greek} & = & @{verbatim "\<alpha>"}@{text "  |  "}@{verbatim "\<beta>"}@{text "  |  "}@{verbatim "\<gamma>"}@{text "  |  "}@{verbatim "\<delta>"}@{text "  |"} \\
wenzelm@28775
   161
          &   & @{verbatim "\<epsilon>"}@{text "  |  "}@{verbatim "\<zeta>"}@{text "  |  "}@{verbatim "\<eta>"}@{text "  |  "}@{verbatim "\<theta>"}@{text "  |"} \\
wenzelm@28775
   162
          &   & @{verbatim "\<iota>"}@{text "  |  "}@{verbatim "\<kappa>"}@{text "  |  "}@{verbatim "\<mu>"}@{text "  |  "}@{verbatim "\<nu>"}@{text "  |"} \\
wenzelm@28775
   163
          &   & @{verbatim "\<xi>"}@{text "  |  "}@{verbatim "\<pi>"}@{text "  |  "}@{verbatim "\<rho>"}@{text "  |  "}@{verbatim "\<sigma>"}@{text "  |  "}@{verbatim "\<tau>"}@{text "  |"} \\
wenzelm@28775
   164
          &   & @{verbatim "\<upsilon>"}@{text "  |  "}@{verbatim "\<phi>"}@{text "  |  "}@{verbatim "\<chi>"}@{text "  |  "}@{verbatim "\<psi>"}@{text "  |"} \\
wenzelm@28775
   165
          &   & @{verbatim "\<omega>"}@{text "  |  "}@{verbatim "\<Gamma>"}@{text "  |  "}@{verbatim "\<Delta>"}@{text "  |  "}@{verbatim "\<Theta>"}@{text "  |"} \\
wenzelm@28775
   166
          &   & @{verbatim "\<Lambda>"}@{text "  |  "}@{verbatim "\<Xi>"}@{text "  |  "}@{verbatim "\<Pi>"}@{text "  |  "}@{verbatim "\<Sigma>"}@{text "  |"} \\
wenzelm@28775
   167
          &   & @{verbatim "\<Upsilon>"}@{text "  |  "}@{verbatim "\<Phi>"}@{text "  |  "}@{verbatim "\<Psi>"}@{text "  |  "}@{verbatim "\<Omega>"} \\
wenzelm@28775
   168
  \end{supertabular}
wenzelm@28776
   169
  \end{center}
wenzelm@27037
   170
wenzelm@28778
   171
  A @{syntax_ref var} or @{syntax_ref typevar} describes an unknown,
wenzelm@28778
   172
  which is internally a pair of base name and index (ML type @{ML_type
wenzelm@28778
   173
  indexname}).  These components are either separated by a dot as in
wenzelm@28778
   174
  @{text "?x.1"} or @{text "?x7.3"} or run together as in @{text
wenzelm@28778
   175
  "?x1"}.  The latter form is possible if the base name does not end
wenzelm@28778
   176
  with digits.  If the index is 0, it may be dropped altogether:
wenzelm@28778
   177
  @{text "?x"} and @{text "?x0"} and @{text "?x.0"} all refer to the
wenzelm@28778
   178
  same unknown, with basename @{text "x"} and index 0.
wenzelm@28778
   179
wenzelm@28778
   180
  The syntax of @{syntax_ref string} admits any characters, including
wenzelm@27037
   181
  newlines; ``@{verbatim "\""}'' (double-quote) and ``@{verbatim
wenzelm@27037
   182
  "\\"}'' (backslash) need to be escaped by a backslash; arbitrary
wenzelm@27037
   183
  character codes may be specified as ``@{verbatim "\\"}@{text ddd}'',
wenzelm@27037
   184
  with three decimal digits.  Alternative strings according to
wenzelm@28778
   185
  @{syntax_ref altstring} are analogous, using single back-quotes
wenzelm@28778
   186
  instead.
wenzelm@28778
   187
wenzelm@28778
   188
  The body of @{syntax_ref verbatim} may consist of any text not
wenzelm@27037
   189
  containing ``@{verbatim "*"}@{verbatim "}"}''; this allows
wenzelm@28778
   190
  convenient inclusion of quotes without further escapes.  There is no
wenzelm@28778
   191
  way to escape ``@{verbatim "*"}@{verbatim "}"}''.  If the quoted
wenzelm@28778
   192
  text is {\LaTeX} source, one may usually add some blank or comment
wenzelm@28778
   193
  to avoid the critical character sequence.
wenzelm@28778
   194
wenzelm@55033
   195
  A @{syntax_ref cartouche} consists of arbitrary text, with properly
wenzelm@55033
   196
  balanced blocks of ``@{verbatim "\<open>"}~@{text "\<dots>"}~@{verbatim
wenzelm@55033
   197
  "\<close>"}''.  Note that the rendering of cartouche delimiters is
wenzelm@55033
   198
  usually like this: ``@{text "\<open> \<dots> \<close>"}''.
wenzelm@55033
   199
wenzelm@28778
   200
  Source comments take the form @{verbatim "(*"}~@{text
wenzelm@28778
   201
  "\<dots>"}~@{verbatim "*)"} and may be nested, although the user-interface
wenzelm@28778
   202
  might prevent this.  Note that this form indicates source comments
wenzelm@28778
   203
  only, which are stripped after lexical analysis of the input.  The
wenzelm@28778
   204
  Isar syntax also provides proper \emph{document comments} that are
wenzelm@28778
   205
  considered as part of the text (see \secref{sec:comments}).
wenzelm@27037
   206
wenzelm@27037
   207
  Common mathematical symbols such as @{text \<forall>} are represented in
wenzelm@27037
   208
  Isabelle as @{verbatim \<forall>}.  There are infinitely many Isabelle
wenzelm@27037
   209
  symbols like this, although proper presentation is left to front-end
wenzelm@47822
   210
  tools such as {\LaTeX}, Proof~General, or Isabelle/jEdit.  A list of
wenzelm@47822
   211
  predefined Isabelle symbols that work well with these tools is given
wenzelm@47822
   212
  in \appref{app:symbols}.  Note that @{verbatim "\<lambda>"} does not belong
wenzelm@47822
   213
  to the @{text letter} category, since it is already used differently
wenzelm@47822
   214
  in the Pure term language.  *}
wenzelm@27037
   215
wenzelm@27037
   216
wenzelm@27037
   217
section {* Common syntax entities *}
wenzelm@27037
   218
wenzelm@27037
   219
text {*
wenzelm@27037
   220
  We now introduce several basic syntactic entities, such as names,
wenzelm@27037
   221
  terms, and theorem specifications, which are factored out of the
wenzelm@27037
   222
  actual Isar language elements to be described later.
wenzelm@27037
   223
*}
wenzelm@27037
   224
wenzelm@27037
   225
wenzelm@27037
   226
subsection {* Names *}
wenzelm@27037
   227
wenzelm@42596
   228
text {* Entity @{syntax name} usually refers to any name of types,
wenzelm@27037
   229
  constants, theorems etc.\ that are to be \emph{declared} or
wenzelm@27037
   230
  \emph{defined} (so qualified identifiers are excluded here).  Quoted
wenzelm@27037
   231
  strings provide an escape for non-identifier names or those ruled
wenzelm@27037
   232
  out by outer syntax keywords (e.g.\ quoted @{verbatim "\"let\""}).
wenzelm@42596
   233
  Already existing objects are usually referenced by @{syntax
wenzelm@42596
   234
  nameref}.
wenzelm@27037
   235
wenzelm@42596
   236
  @{rail "
wenzelm@42596
   237
    @{syntax_def name}: @{syntax ident} | @{syntax symident} |
wenzelm@42596
   238
      @{syntax string} | @{syntax nat}
wenzelm@27037
   239
    ;
wenzelm@42596
   240
    @{syntax_def parname}: '(' @{syntax name} ')'
wenzelm@27037
   241
    ;
wenzelm@42596
   242
    @{syntax_def nameref}: @{syntax name} | @{syntax longident}
wenzelm@42596
   243
  "}
wenzelm@40296
   244
*}
wenzelm@40296
   245
wenzelm@40296
   246
wenzelm@40296
   247
subsection {* Numbers *}
wenzelm@40296
   248
wenzelm@40296
   249
text {* The outer lexical syntax (\secref{sec:outer-lex}) admits
wenzelm@40296
   250
  natural numbers and floating point numbers.  These are combined as
wenzelm@40296
   251
  @{syntax int} and @{syntax real} as follows.
wenzelm@40296
   252
wenzelm@42596
   253
  @{rail "
wenzelm@42596
   254
    @{syntax_def int}: @{syntax nat} | '-' @{syntax nat}
wenzelm@27037
   255
    ;
wenzelm@42596
   256
    @{syntax_def real}: @{syntax float} | @{syntax int}
wenzelm@42596
   257
  "}
wenzelm@40296
   258
wenzelm@42596
   259
  Note that there is an overlap with the category @{syntax name},
wenzelm@40296
   260
  which also includes @{syntax nat}.
wenzelm@27037
   261
*}
wenzelm@27037
   262
wenzelm@27037
   263
wenzelm@27037
   264
subsection {* Comments \label{sec:comments} *}
wenzelm@27037
   265
wenzelm@42596
   266
text {* Large chunks of plain @{syntax text} are usually given
wenzelm@42596
   267
  @{syntax verbatim}, i.e.\ enclosed in @{verbatim "{"}@{verbatim
wenzelm@27037
   268
  "*"}~@{text "\<dots>"}~@{verbatim "*"}@{verbatim "}"}.  For convenience,
wenzelm@42596
   269
  any of the smaller text units conforming to @{syntax nameref} are
wenzelm@42596
   270
  admitted as well.  A marginal @{syntax comment} is of the form
wenzelm@42596
   271
  @{verbatim "--"}~@{syntax text}.  Any number of these may occur
wenzelm@27037
   272
  within Isabelle/Isar commands.
wenzelm@27037
   273
wenzelm@42596
   274
  @{rail "
wenzelm@42596
   275
    @{syntax_def text}: @{syntax verbatim} | @{syntax nameref}
wenzelm@27037
   276
    ;
wenzelm@42596
   277
    @{syntax_def comment}: '--' @{syntax text}
wenzelm@42596
   278
  "}
wenzelm@27037
   279
*}
wenzelm@27037
   280
wenzelm@27037
   281
wenzelm@27037
   282
subsection {* Type classes, sorts and arities *}
wenzelm@27037
   283
wenzelm@27037
   284
text {*
wenzelm@27037
   285
  Classes are specified by plain names.  Sorts have a very simple
wenzelm@27037
   286
  inner syntax, which is either a single class name @{text c} or a
wenzelm@27037
   287
  list @{text "{c\<^sub>1, \<dots>, c\<^sub>n}"} referring to the
wenzelm@27037
   288
  intersection of these classes.  The syntax of type arities is given
wenzelm@27037
   289
  directly at the outer level.
wenzelm@27037
   290
wenzelm@42596
   291
  @{rail "
wenzelm@42596
   292
    @{syntax_def classdecl}: @{syntax name} (('<' | '\<subseteq>') (@{syntax nameref} + ','))?
wenzelm@27037
   293
    ;
wenzelm@42596
   294
    @{syntax_def sort}: @{syntax nameref}
wenzelm@27037
   295
    ;
wenzelm@42596
   296
    @{syntax_def arity}: ('(' (@{syntax sort} + ',') ')')? @{syntax sort}
wenzelm@42596
   297
  "}
wenzelm@27037
   298
*}
wenzelm@27037
   299
wenzelm@27037
   300
wenzelm@27037
   301
subsection {* Types and terms \label{sec:types-terms} *}
wenzelm@27037
   302
wenzelm@27037
   303
text {*
wenzelm@27037
   304
  The actual inner Isabelle syntax, that of types and terms of the
wenzelm@27037
   305
  logic, is far too sophisticated in order to be modelled explicitly
wenzelm@27037
   306
  at the outer theory level.  Basically, any such entity has to be
wenzelm@27037
   307
  quoted to turn it into a single token (the parsing and type-checking
wenzelm@27037
   308
  is performed internally later).  For convenience, a slightly more
wenzelm@27037
   309
  liberal convention is adopted: quotes may be omitted for any type or
wenzelm@27037
   310
  term that is already atomic at the outer level.  For example, one
wenzelm@27037
   311
  may just write @{verbatim x} instead of quoted @{verbatim "\"x\""}.
wenzelm@27037
   312
  Note that symbolic identifiers (e.g.\ @{verbatim "++"} or @{text
wenzelm@27037
   313
  "\<forall>"} are available as well, provided these have not been superseded
wenzelm@27037
   314
  by commands or other keywords already (such as @{verbatim "="} or
wenzelm@27037
   315
  @{verbatim "+"}).
wenzelm@27037
   316
wenzelm@42596
   317
  @{rail "
wenzelm@42596
   318
    @{syntax_def type}: @{syntax nameref} | @{syntax typefree} |
wenzelm@42596
   319
      @{syntax typevar}
wenzelm@27037
   320
    ;
wenzelm@42596
   321
    @{syntax_def term}: @{syntax nameref} | @{syntax var}
wenzelm@27037
   322
    ;
wenzelm@42596
   323
    @{syntax_def prop}: @{syntax term}
wenzelm@42596
   324
  "}
wenzelm@27037
   325
wenzelm@27037
   326
  Positional instantiations are indicated by giving a sequence of
wenzelm@27037
   327
  terms, or the placeholder ``@{text _}'' (underscore), which means to
wenzelm@27037
   328
  skip a position.
wenzelm@27037
   329
wenzelm@42596
   330
  @{rail "
wenzelm@42596
   331
    @{syntax_def inst}: '_' | @{syntax term}
wenzelm@27037
   332
    ;
wenzelm@42596
   333
    @{syntax_def insts}: (@{syntax inst} *)
wenzelm@42596
   334
  "}
wenzelm@27037
   335
wenzelm@42596
   336
  Type declarations and definitions usually refer to @{syntax
wenzelm@42596
   337
  typespec} on the left-hand side.  This models basic type constructor
wenzelm@42596
   338
  application at the outer syntax level.  Note that only plain postfix
wenzelm@42596
   339
  notation is available here, but no infixes.
wenzelm@27037
   340
wenzelm@42596
   341
  @{rail "
wenzelm@42596
   342
    @{syntax_def typespec}:
wenzelm@42596
   343
      (() | @{syntax typefree} | '(' ( @{syntax typefree} + ',' ) ')') @{syntax name}
wenzelm@27037
   344
    ;
wenzelm@42705
   345
    @{syntax_def typespec_sorts}:
wenzelm@42596
   346
      (() | (@{syntax typefree} ('::' @{syntax sort})?) |
wenzelm@42596
   347
        '(' ( (@{syntax typefree} ('::' @{syntax sort})?) + ',' ) ')') @{syntax name}
wenzelm@42596
   348
  "}
wenzelm@27037
   349
*}
wenzelm@27037
   350
wenzelm@27037
   351
wenzelm@28754
   352
subsection {* Term patterns and declarations \label{sec:term-decls} *}
wenzelm@28754
   353
wenzelm@42596
   354
text {* Wherever explicit propositions (or term fragments) occur in a
wenzelm@42596
   355
  proof text, casual binding of schematic term variables may be given
wenzelm@42596
   356
  specified via patterns of the form ``@{text "(\<IS> p\<^sub>1 \<dots> p\<^sub>n)"}''.
wenzelm@42596
   357
  This works both for @{syntax term} and @{syntax prop}.
wenzelm@28754
   358
wenzelm@42596
   359
  @{rail "
wenzelm@42705
   360
    @{syntax_def term_pat}: '(' (@'is' @{syntax term} +) ')'
wenzelm@28754
   361
    ;
wenzelm@42705
   362
    @{syntax_def prop_pat}: '(' (@'is' @{syntax prop} +) ')'
wenzelm@42596
   363
  "}
wenzelm@28754
   364
wenzelm@28754
   365
  \medskip Declarations of local variables @{text "x :: \<tau>"} and
wenzelm@28754
   366
  logical propositions @{text "a : \<phi>"} represent different views on
wenzelm@28754
   367
  the same principle of introducing a local scope.  In practice, one
wenzelm@42596
   368
  may usually omit the typing of @{syntax vars} (due to
wenzelm@28754
   369
  type-inference), and the naming of propositions (due to implicit
wenzelm@28754
   370
  references of current facts).  In any case, Isar proof elements
wenzelm@28754
   371
  usually admit to introduce multiple such items simultaneously.
wenzelm@28754
   372
wenzelm@42596
   373
  @{rail "
wenzelm@42596
   374
    @{syntax_def vars}: (@{syntax name} +) ('::' @{syntax type})?
wenzelm@28754
   375
    ;
wenzelm@42705
   376
    @{syntax_def props}: @{syntax thmdecl}? (@{syntax prop} @{syntax prop_pat}? +)
wenzelm@42596
   377
  "}
wenzelm@28754
   378
wenzelm@28754
   379
  The treatment of multiple declarations corresponds to the
wenzelm@42596
   380
  complementary focus of @{syntax vars} versus @{syntax props}.  In
wenzelm@42596
   381
  ``@{text "x\<^sub>1 \<dots> x\<^sub>n :: \<tau>"}'' the typing refers to all variables, while
wenzelm@42596
   382
  in @{text "a: \<phi>\<^sub>1 \<dots> \<phi>\<^sub>n"} the naming refers to all propositions
wenzelm@42596
   383
  collectively.  Isar language elements that refer to @{syntax vars}
wenzelm@42596
   384
  or @{syntax props} typically admit separate typings or namings via
wenzelm@28754
   385
  another level of iteration, with explicit @{keyword_ref "and"}
wenzelm@28754
   386
  separators; e.g.\ see @{command "fix"} and @{command "assume"} in
wenzelm@28754
   387
  \secref{sec:proof-context}.
wenzelm@28754
   388
*}
wenzelm@28754
   389
wenzelm@28754
   390
wenzelm@27037
   391
subsection {* Attributes and theorems \label{sec:syn-att} *}
wenzelm@27037
   392
wenzelm@28754
   393
text {* Attributes have their own ``semi-inner'' syntax, in the sense
wenzelm@42596
   394
  that input conforming to @{syntax args} below is parsed by the
wenzelm@28754
   395
  attribute a second time.  The attribute argument specifications may
wenzelm@28754
   396
  be any sequence of atomic entities (identifiers, strings etc.), or
wenzelm@42596
   397
  properly bracketed argument lists.  Below @{syntax atom} refers to
wenzelm@42596
   398
  any atomic entity, including any @{syntax keyword} conforming to
wenzelm@42596
   399
  @{syntax symident}.
wenzelm@27037
   400
wenzelm@42596
   401
  @{rail "
wenzelm@42596
   402
    @{syntax_def atom}: @{syntax nameref} | @{syntax typefree} |
wenzelm@42596
   403
      @{syntax typevar} | @{syntax var} | @{syntax nat} | @{syntax float} |
wenzelm@42596
   404
      @{syntax keyword}
wenzelm@27037
   405
    ;
wenzelm@42596
   406
    arg: @{syntax atom} | '(' @{syntax args} ')' | '[' @{syntax args} ']'
wenzelm@27037
   407
    ;
wenzelm@42596
   408
    @{syntax_def args}: arg *
wenzelm@27037
   409
    ;
wenzelm@42596
   410
    @{syntax_def attributes}: '[' (@{syntax nameref} @{syntax args} * ',') ']'
wenzelm@42596
   411
  "}
wenzelm@27037
   412
wenzelm@42596
   413
  Theorem specifications come in several flavors: @{syntax axmdecl}
wenzelm@42596
   414
  and @{syntax thmdecl} usually refer to axioms, assumptions or
wenzelm@42596
   415
  results of goal statements, while @{syntax thmdef} collects lists of
wenzelm@42596
   416
  existing theorems.  Existing theorems are given by @{syntax thmref}
wenzelm@42596
   417
  and @{syntax thmrefs}, the former requires an actual singleton
wenzelm@27037
   418
  result.
wenzelm@27037
   419
wenzelm@27037
   420
  There are three forms of theorem references:
wenzelm@27037
   421
  \begin{enumerate}
wenzelm@27037
   422
  
wenzelm@27037
   423
  \item named facts @{text "a"},
wenzelm@27037
   424
wenzelm@27037
   425
  \item selections from named facts @{text "a(i)"} or @{text "a(j - k)"},
wenzelm@27037
   426
wenzelm@27037
   427
  \item literal fact propositions using @{syntax_ref altstring} syntax
wenzelm@27037
   428
  @{verbatim "`"}@{text "\<phi>"}@{verbatim "`"} (see also method
wenzelm@28754
   429
  @{method_ref fact}).
wenzelm@27037
   430
wenzelm@27037
   431
  \end{enumerate}
wenzelm@27037
   432
wenzelm@27037
   433
  Any kind of theorem specification may include lists of attributes
wenzelm@27037
   434
  both on the left and right hand sides; attributes are applied to any
wenzelm@27037
   435
  immediately preceding fact.  If names are omitted, the theorems are
wenzelm@27037
   436
  not stored within the theorem database of the theory or proof
wenzelm@27037
   437
  context, but any given attributes are applied nonetheless.
wenzelm@27037
   438
wenzelm@27037
   439
  An extra pair of brackets around attributes (like ``@{text
wenzelm@27037
   440
  "[[simproc a]]"}'') abbreviates a theorem reference involving an
wenzelm@27037
   441
  internal dummy fact, which will be ignored later on.  So only the
wenzelm@27037
   442
  effect of the attribute on the background context will persist.
wenzelm@27037
   443
  This form of in-place declarations is particularly useful with
wenzelm@27037
   444
  commands like @{command "declare"} and @{command "using"}.
wenzelm@27037
   445
wenzelm@42596
   446
  @{rail "
wenzelm@42596
   447
    @{syntax_def axmdecl}: @{syntax name} @{syntax attributes}? ':'
wenzelm@42596
   448
    ;
wenzelm@42596
   449
    @{syntax_def thmdecl}: thmbind ':'
wenzelm@27037
   450
    ;
wenzelm@42596
   451
    @{syntax_def thmdef}: thmbind '='
wenzelm@27037
   452
    ;
wenzelm@42596
   453
    @{syntax_def thmref}:
wenzelm@42596
   454
      (@{syntax nameref} selection? | @{syntax altstring}) @{syntax attributes}? |
wenzelm@42596
   455
      '[' @{syntax attributes} ']'
wenzelm@27037
   456
    ;
wenzelm@42596
   457
    @{syntax_def thmrefs}: @{syntax thmref} +
wenzelm@27037
   458
    ;
wenzelm@27037
   459
wenzelm@42596
   460
    thmbind: @{syntax name} @{syntax attributes} | @{syntax name} | @{syntax attributes}
wenzelm@27037
   461
    ;
wenzelm@42596
   462
    selection: '(' ((@{syntax nat} | @{syntax nat} '-' @{syntax nat}?) + ',') ')'
wenzelm@42596
   463
  "}
wenzelm@27037
   464
*}
wenzelm@27037
   465
wenzelm@27037
   466
end