src/Doc/IsarRef/Inner_Syntax.thy
author wenzelm
Mon May 13 13:23:13 2013 +0200 (2013-05-13)
changeset 51960 61ac1efe02c3
parent 51657 3db1bbc82d8d
child 52143 36ffe23b25f8
permissions -rw-r--r--
option "goals_limit", with more uniform description;
wenzelm@28762
     1
theory Inner_Syntax
wenzelm@42651
     2
imports Base Main
wenzelm@28762
     3
begin
wenzelm@28762
     4
wenzelm@28778
     5
chapter {* Inner syntax --- the term language \label{ch:inner-syntax} *}
wenzelm@28762
     6
wenzelm@46282
     7
text {* The inner syntax of Isabelle provides concrete notation for
wenzelm@46282
     8
  the main entities of the logical framework, notably @{text
wenzelm@46282
     9
  "\<lambda>"}-terms with types and type classes.  Applications may either
wenzelm@46282
    10
  extend existing syntactic categories by additional notation, or
wenzelm@46282
    11
  define new sub-languages that are linked to the standard term
wenzelm@46282
    12
  language via some explicit markers.  For example @{verbatim
wenzelm@46282
    13
  FOO}~@{text "foo"} could embed the syntax corresponding for some
wenzelm@46282
    14
  user-defined nonterminal @{text "foo"} --- within the bounds of the
wenzelm@46282
    15
  given lexical syntax of Isabelle/Pure.
wenzelm@46282
    16
wenzelm@46282
    17
  The most basic way to specify concrete syntax for logical entities
wenzelm@46282
    18
  works via mixfix annotations (\secref{sec:mixfix}), which may be
wenzelm@46282
    19
  usually given as part of the original declaration or via explicit
wenzelm@46282
    20
  notation commands later on (\secref{sec:notation}).  This already
wenzelm@46282
    21
  covers many needs of concrete syntax without having to understand
wenzelm@46282
    22
  the full complexity of inner syntax layers.
wenzelm@46282
    23
wenzelm@46282
    24
  Further details of the syntax engine involves the classical
wenzelm@46282
    25
  distinction of lexical language versus context-free grammar (see
wenzelm@46282
    26
  \secref{sec:pure-syntax}), and various mechanisms for \emph{syntax
wenzelm@48113
    27
  transformations} (see \secref{sec:syntax-transformations}).
wenzelm@46282
    28
*}
wenzelm@46282
    29
wenzelm@46282
    30
wenzelm@28762
    31
section {* Printing logical entities *}
wenzelm@28762
    32
wenzelm@46284
    33
subsection {* Diagnostic commands \label{sec:print-diag} *}
wenzelm@28762
    34
wenzelm@28762
    35
text {*
wenzelm@28762
    36
  \begin{matharray}{rcl}
wenzelm@28766
    37
    @{command_def "typ"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    38
    @{command_def "term"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    39
    @{command_def "prop"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28766
    40
    @{command_def "thm"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    41
    @{command_def "prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    42
    @{command_def "full_prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28766
    43
    @{command_def "pr"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28762
    44
  \end{matharray}
wenzelm@28762
    45
wenzelm@28762
    46
  These diagnostic commands assist interactive development by printing
wenzelm@28762
    47
  internal logical entities in a human-readable fashion.
wenzelm@28762
    48
wenzelm@42596
    49
  @{rail "
wenzelm@48792
    50
    @@{command typ} @{syntax modes}? @{syntax type} ('::' @{syntax sort})?
wenzelm@28762
    51
    ;
wenzelm@42596
    52
    @@{command term} @{syntax modes}? @{syntax term}
wenzelm@28762
    53
    ;
wenzelm@42596
    54
    @@{command prop} @{syntax modes}? @{syntax prop}
wenzelm@28762
    55
    ;
wenzelm@42596
    56
    @@{command thm} @{syntax modes}? @{syntax thmrefs}
wenzelm@28762
    57
    ;
wenzelm@42596
    58
    ( @@{command prf} | @@{command full_prf} ) @{syntax modes}? @{syntax thmrefs}?
wenzelm@28762
    59
    ;
wenzelm@42596
    60
    @@{command pr} @{syntax modes}? @{syntax nat}?
wenzelm@28762
    61
    ;
wenzelm@28762
    62
wenzelm@42596
    63
    @{syntax_def modes}: '(' (@{syntax name} + ) ')'
wenzelm@42596
    64
  "}
wenzelm@28762
    65
wenzelm@28762
    66
  \begin{description}
wenzelm@28762
    67
wenzelm@48792
    68
  \item @{command "typ"}~@{text \<tau>} reads and prints a type expression
wenzelm@48792
    69
  according to the current context.
wenzelm@48792
    70
wenzelm@48792
    71
  \item @{command "typ"}~@{text "\<tau> :: s"} uses type-inference to
wenzelm@48792
    72
  determine the most general way to make @{text "\<tau>"} conform to sort
wenzelm@48792
    73
  @{text "s"}.  For concrete @{text "\<tau>"} this checks if the type
wenzelm@48792
    74
  belongs to that sort.  Dummy type parameters ``@{text "_"}''
wenzelm@48792
    75
  (underscore) are assigned to fresh type variables with most general
wenzelm@48792
    76
  sorts, according the the principles of type-inference.
wenzelm@28766
    77
wenzelm@28766
    78
  \item @{command "term"}~@{text t} and @{command "prop"}~@{text \<phi>}
wenzelm@28766
    79
  read, type-check and print terms or propositions according to the
wenzelm@28766
    80
  current theory or proof context; the inferred type of @{text t} is
wenzelm@28766
    81
  output as well.  Note that these commands are also useful in
wenzelm@28766
    82
  inspecting the current environment of term abbreviations.
wenzelm@28762
    83
wenzelm@28762
    84
  \item @{command "thm"}~@{text "a\<^sub>1 \<dots> a\<^sub>n"} retrieves
wenzelm@28762
    85
  theorems from the current theory or proof context.  Note that any
wenzelm@28762
    86
  attributes included in the theorem specifications are applied to a
wenzelm@28762
    87
  temporary context derived from the current theory or proof; the
wenzelm@28762
    88
  result is discarded, i.e.\ attributes involved in @{text "a\<^sub>1,
wenzelm@28762
    89
  \<dots>, a\<^sub>n"} do not have any permanent effect.
wenzelm@28762
    90
wenzelm@28762
    91
  \item @{command "prf"} displays the (compact) proof term of the
wenzelm@28762
    92
  current proof state (if present), or of the given theorems. Note
wenzelm@28762
    93
  that this requires proof terms to be switched on for the current
wenzelm@28762
    94
  object logic (see the ``Proof terms'' section of the Isabelle
wenzelm@28762
    95
  reference manual for information on how to do this).
wenzelm@28762
    96
wenzelm@28762
    97
  \item @{command "full_prf"} is like @{command "prf"}, but displays
wenzelm@28762
    98
  the full proof term, i.e.\ also displays information omitted in the
wenzelm@28762
    99
  compact proof term, which is denoted by ``@{text _}'' placeholders
wenzelm@28762
   100
  there.
wenzelm@28762
   101
wenzelm@39165
   102
  \item @{command "pr"}~@{text "goals"} prints the current proof state
wenzelm@39165
   103
  (if present), including current facts and goals.  The optional limit
wenzelm@39165
   104
  arguments affect the number of goals to be displayed, which is
wenzelm@39165
   105
  initially 10.  Omitting limit value leaves the current setting
wenzelm@39165
   106
  unchanged.
wenzelm@28766
   107
wenzelm@28762
   108
  \end{description}
wenzelm@28762
   109
wenzelm@28762
   110
  All of the diagnostic commands above admit a list of @{text modes}
wenzelm@42926
   111
  to be specified, which is appended to the current print mode; see
wenzelm@46284
   112
  also \secref{sec:print-modes}.  Thus the output behavior may be
wenzelm@46284
   113
  modified according particular print mode features.  For example,
wenzelm@46284
   114
  @{command "pr"}~@{text "(latex xsymbols)"} would print the current
wenzelm@46284
   115
  proof state with mathematical symbols and special characters
wenzelm@46284
   116
  represented in {\LaTeX} source, according to the Isabelle style
wenzelm@28762
   117
  \cite{isabelle-sys}.
wenzelm@28762
   118
wenzelm@28762
   119
  Note that antiquotations (cf.\ \secref{sec:antiq}) provide a more
wenzelm@28762
   120
  systematic way to include formal items into the printed text
wenzelm@28762
   121
  document.
wenzelm@28762
   122
*}
wenzelm@28762
   123
wenzelm@28762
   124
wenzelm@28763
   125
subsection {* Details of printed content *}
wenzelm@28763
   126
wenzelm@28763
   127
text {*
wenzelm@42655
   128
  \begin{tabular}{rcll}
wenzelm@49699
   129
    @{attribute_def show_markup} & : & @{text attribute} \\
wenzelm@42655
   130
    @{attribute_def show_types} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   131
    @{attribute_def show_sorts} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   132
    @{attribute_def show_consts} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   133
    @{attribute_def show_abbrevs} & : & @{text attribute} & default @{text true} \\
wenzelm@42655
   134
    @{attribute_def show_brackets} & : & @{text attribute} & default @{text false} \\
wenzelm@42669
   135
    @{attribute_def names_long} & : & @{text attribute} & default @{text false} \\
wenzelm@42669
   136
    @{attribute_def names_short} & : & @{text attribute} & default @{text false} \\
wenzelm@42669
   137
    @{attribute_def names_unique} & : & @{text attribute} & default @{text true} \\
wenzelm@42655
   138
    @{attribute_def eta_contract} & : & @{text attribute} & default @{text true} \\
wenzelm@42655
   139
    @{attribute_def goals_limit} & : & @{text attribute} & default @{text 10} \\
wenzelm@42655
   140
    @{attribute_def show_main_goal} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   141
    @{attribute_def show_hyps} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   142
    @{attribute_def show_tags} & : & @{text attribute} & default @{text false} \\
wenzelm@42655
   143
    @{attribute_def show_question_marks} & : & @{text attribute} & default @{text true} \\
wenzelm@42655
   144
  \end{tabular}
wenzelm@42655
   145
  \medskip
wenzelm@28763
   146
wenzelm@42655
   147
  These configuration options control the detail of information that
wenzelm@42655
   148
  is displayed for types, terms, theorems, goals etc.  See also
wenzelm@42655
   149
  \secref{sec:config}.
wenzelm@28765
   150
wenzelm@28763
   151
  \begin{description}
wenzelm@28763
   152
wenzelm@49699
   153
  \item @{attribute show_markup} controls direct inlining of markup
wenzelm@49699
   154
  into the printed representation of formal entities --- notably type
wenzelm@49699
   155
  and sort constraints.  This enables Prover IDE users to retrieve
wenzelm@49699
   156
  that information via tooltips or popups while hovering with the
wenzelm@49699
   157
  mouse over the output window, for example.  Consequently, this
wenzelm@49699
   158
  option is enabled by default for Isabelle/jEdit, but disabled for
wenzelm@49699
   159
  TTY and Proof~General~/Emacs where document markup would not work.
wenzelm@49699
   160
wenzelm@42655
   161
  \item @{attribute show_types} and @{attribute show_sorts} control
wenzelm@42655
   162
  printing of type constraints for term variables, and sort
wenzelm@42655
   163
  constraints for type variables.  By default, neither of these are
wenzelm@42655
   164
  shown in output.  If @{attribute show_sorts} is enabled, types are
wenzelm@49699
   165
  always shown as well.  In Isabelle/jEdit, manual setting of these
wenzelm@49699
   166
  options is normally not required thanks to @{attribute show_markup}
wenzelm@49699
   167
  above.
wenzelm@28763
   168
wenzelm@28763
   169
  Note that displaying types and sorts may explain why a polymorphic
wenzelm@28763
   170
  inference rule fails to resolve with some goal, or why a rewrite
wenzelm@28763
   171
  rule does not apply as expected.
wenzelm@28763
   172
wenzelm@42655
   173
  \item @{attribute show_consts} controls printing of types of
wenzelm@42655
   174
  constants when displaying a goal state.
wenzelm@28765
   175
wenzelm@28765
   176
  Note that the output can be enormous, because polymorphic constants
wenzelm@28765
   177
  often occur at several different type instances.
wenzelm@28763
   178
wenzelm@42655
   179
  \item @{attribute show_abbrevs} controls folding of constant
wenzelm@42655
   180
  abbreviations.
wenzelm@40879
   181
wenzelm@42655
   182
  \item @{attribute show_brackets} controls bracketing in pretty
wenzelm@42655
   183
  printed output.  If enabled, all sub-expressions of the pretty
wenzelm@28765
   184
  printing tree will be parenthesized, even if this produces malformed
wenzelm@28765
   185
  term syntax!  This crude way of showing the internal structure of
wenzelm@28765
   186
  pretty printed entities may occasionally help to diagnose problems
wenzelm@28765
   187
  with operator priorities, for example.
wenzelm@28763
   188
wenzelm@42669
   189
  \item @{attribute names_long}, @{attribute names_short}, and
wenzelm@42669
   190
  @{attribute names_unique} control the way of printing fully
wenzelm@42358
   191
  qualified internal names in external form.  See also
wenzelm@42358
   192
  \secref{sec:antiq} for the document antiquotation options of the
wenzelm@42358
   193
  same names.
wenzelm@42358
   194
wenzelm@42655
   195
  \item @{attribute eta_contract} controls @{text "\<eta>"}-contracted
wenzelm@42655
   196
  printing of terms.
wenzelm@28763
   197
wenzelm@28763
   198
  The @{text \<eta>}-contraction law asserts @{prop "(\<lambda>x. f x) \<equiv> f"},
wenzelm@28763
   199
  provided @{text x} is not free in @{text f}.  It asserts
wenzelm@28763
   200
  \emph{extensionality} of functions: @{prop "f \<equiv> g"} if @{prop "f x \<equiv>
wenzelm@28763
   201
  g x"} for all @{text x}.  Higher-order unification frequently puts
wenzelm@28763
   202
  terms into a fully @{text \<eta>}-expanded form.  For example, if @{text
wenzelm@28763
   203
  F} has type @{text "(\<tau> \<Rightarrow> \<tau>) \<Rightarrow> \<tau>"} then its expanded form is @{term
wenzelm@28763
   204
  "\<lambda>h. F (\<lambda>x. h x)"}.
wenzelm@28763
   205
wenzelm@42655
   206
  Enabling @{attribute eta_contract} makes Isabelle perform @{text
wenzelm@28763
   207
  \<eta>}-contractions before printing, so that @{term "\<lambda>h. F (\<lambda>x. h x)"}
wenzelm@28763
   208
  appears simply as @{text F}.
wenzelm@28763
   209
wenzelm@28763
   210
  Note that the distinction between a term and its @{text \<eta>}-expanded
wenzelm@28765
   211
  form occasionally matters.  While higher-order resolution and
wenzelm@28765
   212
  rewriting operate modulo @{text "\<alpha>\<beta>\<eta>"}-conversion, some other tools
wenzelm@28765
   213
  might look at terms more discretely.
wenzelm@28763
   214
wenzelm@42655
   215
  \item @{attribute goals_limit} controls the maximum number of
wenzelm@51960
   216
  subgoals to be printed.
wenzelm@28763
   217
wenzelm@42655
   218
  \item @{attribute show_main_goal} controls whether the main result
wenzelm@42655
   219
  to be proven should be displayed.  This information might be
wenzelm@39130
   220
  relevant for schematic goals, to inspect the current claim that has
wenzelm@39130
   221
  been synthesized so far.
wenzelm@28763
   222
wenzelm@42655
   223
  \item @{attribute show_hyps} controls printing of implicit
wenzelm@42655
   224
  hypotheses of local facts.  Normally, only those hypotheses are
wenzelm@42655
   225
  displayed that are \emph{not} covered by the assumptions of the
wenzelm@42655
   226
  current context: this situation indicates a fault in some tool being
wenzelm@42655
   227
  used.
wenzelm@28763
   228
wenzelm@42655
   229
  By enabling @{attribute show_hyps}, output of \emph{all} hypotheses
wenzelm@42655
   230
  can be enforced, which is occasionally useful for diagnostic
wenzelm@42655
   231
  purposes.
wenzelm@28763
   232
wenzelm@42655
   233
  \item @{attribute show_tags} controls printing of extra annotations
wenzelm@42655
   234
  within theorems, such as internal position information, or the case
wenzelm@42655
   235
  names being attached by the attribute @{attribute case_names}.
wenzelm@28765
   236
wenzelm@28765
   237
  Note that the @{attribute tagged} and @{attribute untagged}
wenzelm@28765
   238
  attributes provide low-level access to the collection of tags
wenzelm@28765
   239
  associated with a theorem.
wenzelm@28765
   240
wenzelm@42655
   241
  \item @{attribute show_question_marks} controls printing of question
wenzelm@42655
   242
  marks for schematic variables, such as @{text ?x}.  Only the leading
wenzelm@28765
   243
  question mark is affected, the remaining text is unchanged
wenzelm@28765
   244
  (including proper markup for schematic variables that might be
wenzelm@28765
   245
  relevant for user interfaces).
wenzelm@28765
   246
wenzelm@28765
   247
  \end{description}
wenzelm@28765
   248
*}
wenzelm@28765
   249
wenzelm@28765
   250
wenzelm@46284
   251
subsection {* Alternative print modes \label{sec:print-modes} *}
wenzelm@46284
   252
wenzelm@46284
   253
text {*
wenzelm@46284
   254
  \begin{mldecls}
wenzelm@46284
   255
    @{index_ML print_mode_value: "unit -> string list"} \\
wenzelm@46284
   256
    @{index_ML Print_Mode.with_modes: "string list -> ('a -> 'b) -> 'a -> 'b"} \\
wenzelm@46284
   257
  \end{mldecls}
wenzelm@46284
   258
wenzelm@46284
   259
  The \emph{print mode} facility allows to modify various operations
wenzelm@46284
   260
  for printing.  Commands like @{command typ}, @{command term},
wenzelm@46284
   261
  @{command thm} (see \secref{sec:print-diag}) take additional print
wenzelm@46284
   262
  modes as optional argument.  The underlying ML operations are as
wenzelm@46284
   263
  follows.
wenzelm@46284
   264
wenzelm@46284
   265
  \begin{description}
wenzelm@46284
   266
wenzelm@46284
   267
  \item @{ML "print_mode_value ()"} yields the list of currently
wenzelm@46284
   268
  active print mode names.  This should be understood as symbolic
wenzelm@46284
   269
  representation of certain individual features for printing (with
wenzelm@46284
   270
  precedence from left to right).
wenzelm@46284
   271
wenzelm@46284
   272
  \item @{ML Print_Mode.with_modes}~@{text "modes f x"} evaluates
wenzelm@46284
   273
  @{text "f x"} in an execution context where the print mode is
wenzelm@46284
   274
  prepended by the given @{text "modes"}.  This provides a thread-safe
wenzelm@46284
   275
  way to augment print modes.  It is also monotonic in the set of mode
wenzelm@46284
   276
  names: it retains the default print mode that certain
wenzelm@46284
   277
  user-interfaces might have installed for their proper functioning!
wenzelm@46284
   278
wenzelm@46284
   279
  \end{description}
wenzelm@46284
   280
wenzelm@46284
   281
  \begin{warn}
wenzelm@46284
   282
  The old global reference @{ML print_mode} should never be used
wenzelm@46284
   283
  directly in applications.  Its main reason for being publicly
wenzelm@46284
   284
  accessible is to support historic versions of Proof~General.
wenzelm@46284
   285
  \end{warn}
wenzelm@46284
   286
wenzelm@46284
   287
  \medskip The pretty printer for inner syntax maintains alternative
wenzelm@46284
   288
  mixfix productions for any print mode name invented by the user, say
wenzelm@46284
   289
  in commands like @{command notation} or @{command abbreviation}.
wenzelm@46284
   290
  Mode names can be arbitrary, but the following ones have a specific
wenzelm@46284
   291
  meaning by convention:
wenzelm@46284
   292
wenzelm@46284
   293
  \begin{itemize}
wenzelm@46284
   294
wenzelm@46284
   295
  \item @{verbatim "\"\""} (the empty string): default mode;
wenzelm@46284
   296
  implicitly active as last element in the list of modes.
wenzelm@46284
   297
wenzelm@46284
   298
  \item @{verbatim input}: dummy print mode that is never active; may
wenzelm@46284
   299
  be used to specify notation that is only available for input.
wenzelm@46284
   300
wenzelm@46284
   301
  \item @{verbatim internal} dummy print mode that is never active;
wenzelm@46284
   302
  used internally in Isabelle/Pure.
wenzelm@46284
   303
wenzelm@46284
   304
  \item @{verbatim xsymbols}: enable proper mathematical symbols
wenzelm@46284
   305
  instead of ASCII art.\footnote{This traditional mode name stems from
wenzelm@46284
   306
  the ``X-Symbol'' package for old versions Proof~General with XEmacs,
wenzelm@46284
   307
  although that package has been superseded by Unicode in recent
wenzelm@46284
   308
  years.}
wenzelm@46284
   309
wenzelm@46284
   310
  \item @{verbatim HTML}: additional mode that is active in HTML
wenzelm@46284
   311
  presentation of Isabelle theory sources; allows to provide
wenzelm@46284
   312
  alternative output notation.
wenzelm@46284
   313
wenzelm@46284
   314
  \item @{verbatim latex}: additional mode that is active in {\LaTeX}
wenzelm@46284
   315
  document preparation of Isabelle theory sources; allows to provide
wenzelm@46284
   316
  alternative output notation.
wenzelm@46284
   317
wenzelm@46284
   318
  \end{itemize}
wenzelm@46284
   319
*}
wenzelm@46284
   320
wenzelm@46284
   321
wenzelm@28765
   322
subsection {* Printing limits *}
wenzelm@28765
   323
wenzelm@28765
   324
text {*
wenzelm@28765
   325
  \begin{mldecls}
wenzelm@36745
   326
    @{index_ML Pretty.margin_default: "int Unsynchronized.ref"} \\
wenzelm@28765
   327
    @{index_ML print_depth: "int -> unit"} \\
wenzelm@28765
   328
  \end{mldecls}
wenzelm@28765
   329
wenzelm@28765
   330
  These ML functions set limits for pretty printed text.
wenzelm@28765
   331
wenzelm@28765
   332
  \begin{description}
wenzelm@28765
   333
wenzelm@36745
   334
  \item @{ML Pretty.margin_default} indicates the global default for
wenzelm@36745
   335
  the right margin of the built-in pretty printer, with initial value
wenzelm@36745
   336
  76.  Note that user-interfaces typically control margins
wenzelm@36745
   337
  automatically when resizing windows, or even bypass the formatting
wenzelm@36745
   338
  engine of Isabelle/ML altogether and do it within the front end via
wenzelm@36745
   339
  Isabelle/Scala.
wenzelm@28765
   340
wenzelm@28765
   341
  \item @{ML print_depth}~@{text n} limits the printing depth of the
wenzelm@28765
   342
  ML toplevel pretty printer; the precise effect depends on the ML
wenzelm@28765
   343
  compiler and run-time system.  Typically @{text n} should be less
wenzelm@28765
   344
  than 10.  Bigger values such as 100--1000 are useful for debugging.
wenzelm@28763
   345
wenzelm@28763
   346
  \end{description}
wenzelm@28763
   347
*}
wenzelm@28763
   348
wenzelm@28763
   349
wenzelm@46282
   350
section {* Mixfix annotations \label{sec:mixfix} *}
wenzelm@28762
   351
wenzelm@28762
   352
text {* Mixfix annotations specify concrete \emph{inner syntax} of
wenzelm@35351
   353
  Isabelle types and terms.  Locally fixed parameters in toplevel
wenzelm@46290
   354
  theorem statements, locale and class specifications also admit
wenzelm@46290
   355
  mixfix annotations in a fairly uniform manner.  A mixfix annotation
wenzelm@50635
   356
  describes the concrete syntax, the translation to abstract
wenzelm@46290
   357
  syntax, and the pretty printing.  Special case annotations provide a
wenzelm@46290
   358
  simple means of specifying infix operators and binders.
wenzelm@46290
   359
wenzelm@46290
   360
  Isabelle mixfix syntax is inspired by {\OBJ} \cite{OBJ}.  It allows
wenzelm@46290
   361
  to specify any context-free priority grammar, which is more general
wenzelm@46290
   362
  than the fixity declarations of ML and Prolog.
wenzelm@28762
   363
wenzelm@42596
   364
  @{rail "
wenzelm@51654
   365
    @{syntax_def mixfix}: '('
wenzelm@51654
   366
      @{syntax template} prios? @{syntax nat}? |
wenzelm@46290
   367
      (@'infix' | @'infixl' | @'infixr') @{syntax template} @{syntax nat} |
wenzelm@51654
   368
      @'binder' @{syntax template} prios? @{syntax nat} |
wenzelm@51654
   369
      @'structure'
wenzelm@51654
   370
    ')'
wenzelm@46290
   371
    ;
wenzelm@46290
   372
    template: string
wenzelm@46289
   373
    ;
wenzelm@42596
   374
    prios: '[' (@{syntax nat} + ',') ']'
wenzelm@42596
   375
  "}
wenzelm@28762
   376
wenzelm@46290
   377
  The string given as @{text template} may include literal text,
wenzelm@46290
   378
  spacing, blocks, and arguments (denoted by ``@{text _}''); the
wenzelm@46290
   379
  special symbol ``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'')
wenzelm@51657
   380
  represents an index argument that specifies an implicit @{keyword
wenzelm@51657
   381
  "structure"} reference (see also \secref{sec:locale}).  Only locally
wenzelm@51657
   382
  fixed variables may be declared as @{keyword "structure"}.
wenzelm@51657
   383
wenzelm@51657
   384
  Infix and binder declarations provide common abbreviations for
wenzelm@51657
   385
  particular mixfix declarations.  So in practice, mixfix templates
wenzelm@51657
   386
  mostly degenerate to literal text for concrete syntax, such as
wenzelm@51657
   387
  ``@{verbatim "++"}'' for an infix symbol.  *}
wenzelm@28762
   388
wenzelm@46290
   389
wenzelm@46290
   390
subsection {* The general mixfix form *}
wenzelm@46290
   391
wenzelm@46290
   392
text {* In full generality, mixfix declarations work as follows.
wenzelm@46290
   393
  Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is annotated by
wenzelm@46290
   394
  @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text "mixfix"} is a string
wenzelm@46290
   395
  @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of delimiters that surround
wenzelm@46290
   396
  argument positions as indicated by underscores.
wenzelm@28762
   397
wenzelm@28762
   398
  Altogether this determines a production for a context-free priority
wenzelm@28762
   399
  grammar, where for each argument @{text "i"} the syntactic category
wenzelm@46292
   400
  is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and the
wenzelm@46292
   401
  result category is determined from @{text "\<tau>"} (with priority @{text
wenzelm@46292
   402
  "p"}).  Priority specifications are optional, with default 0 for
wenzelm@46292
   403
  arguments and 1000 for the result.\footnote{Omitting priorities is
wenzelm@46292
   404
  prone to syntactic ambiguities unless the delimiter tokens determine
wenzelm@46292
   405
  fully bracketed notation, as in @{text "if _ then _ else _ fi"}.}
wenzelm@28762
   406
wenzelm@28762
   407
  Since @{text "\<tau>"} may be again a function type, the constant
wenzelm@28762
   408
  type scheme may have more argument positions than the mixfix
wenzelm@28762
   409
  pattern.  Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for
wenzelm@28762
   410
  @{text "m > n"} works by attaching concrete notation only to the
wenzelm@28762
   411
  innermost part, essentially by printing @{text "(c t\<^sub>1 \<dots> t\<^sub>n) \<dots> t\<^sub>m"}
wenzelm@28762
   412
  instead.  If a term has fewer arguments than specified in the mixfix
wenzelm@28762
   413
  template, the concrete syntax is ignored.
wenzelm@28762
   414
wenzelm@28762
   415
  \medskip A mixfix template may also contain additional directives
wenzelm@28762
   416
  for pretty printing, notably spaces, blocks, and breaks.  The
wenzelm@28762
   417
  general template format is a sequence over any of the following
wenzelm@28762
   418
  entities.
wenzelm@28762
   419
wenzelm@28778
   420
  \begin{description}
wenzelm@28762
   421
wenzelm@28771
   422
  \item @{text "d"} is a delimiter, namely a non-empty sequence of
wenzelm@28771
   423
  characters other than the following special characters:
wenzelm@28762
   424
wenzelm@28771
   425
  \smallskip
wenzelm@28771
   426
  \begin{tabular}{ll}
wenzelm@28771
   427
    @{verbatim "'"} & single quote \\
wenzelm@28771
   428
    @{verbatim "_"} & underscore \\
wenzelm@28771
   429
    @{text "\<index>"} & index symbol \\
wenzelm@28771
   430
    @{verbatim "("} & open parenthesis \\
wenzelm@28771
   431
    @{verbatim ")"} & close parenthesis \\
wenzelm@28771
   432
    @{verbatim "/"} & slash \\
wenzelm@28771
   433
  \end{tabular}
wenzelm@28771
   434
  \medskip
wenzelm@28762
   435
wenzelm@28771
   436
  \item @{verbatim "'"} escapes the special meaning of these
wenzelm@28771
   437
  meta-characters, producing a literal version of the following
wenzelm@28771
   438
  character, unless that is a blank.
wenzelm@28771
   439
wenzelm@28771
   440
  A single quote followed by a blank separates delimiters, without
wenzelm@28771
   441
  affecting printing, but input tokens may have additional white space
wenzelm@28771
   442
  here.
wenzelm@28771
   443
wenzelm@28771
   444
  \item @{verbatim "_"} is an argument position, which stands for a
wenzelm@28762
   445
  certain syntactic category in the underlying grammar.
wenzelm@28762
   446
wenzelm@28771
   447
  \item @{text "\<index>"} is an indexed argument position; this is the place
wenzelm@28771
   448
  where implicit structure arguments can be attached.
wenzelm@28762
   449
wenzelm@28771
   450
  \item @{text "s"} is a non-empty sequence of spaces for printing.
wenzelm@28771
   451
  This and the following specifications do not affect parsing at all.
wenzelm@28762
   452
wenzelm@28771
   453
  \item @{verbatim "("}@{text n} opens a pretty printing block.  The
wenzelm@28762
   454
  optional number specifies how much indentation to add when a line
wenzelm@28762
   455
  break occurs within the block.  If the parenthesis is not followed
wenzelm@28762
   456
  by digits, the indentation defaults to 0.  A block specified via
wenzelm@28771
   457
  @{verbatim "(00"} is unbreakable.
wenzelm@28762
   458
wenzelm@28771
   459
  \item @{verbatim ")"} closes a pretty printing block.
wenzelm@28762
   460
wenzelm@28771
   461
  \item @{verbatim "//"} forces a line break.
wenzelm@28762
   462
wenzelm@28771
   463
  \item @{verbatim "/"}@{text s} allows a line break.  Here @{text s}
wenzelm@28771
   464
  stands for the string of spaces (zero or more) right after the
wenzelm@28771
   465
  slash.  These spaces are printed if the break is \emph{not} taken.
wenzelm@28762
   466
wenzelm@28778
   467
  \end{description}
wenzelm@28762
   468
wenzelm@28762
   469
  The general idea of pretty printing with blocks and breaks is also
wenzelm@46286
   470
  described in \cite{paulson-ml2}; it goes back to \cite{Oppen:1980}.
wenzelm@28762
   471
*}
wenzelm@28762
   472
wenzelm@28762
   473
wenzelm@46290
   474
subsection {* Infixes *}
wenzelm@46290
   475
wenzelm@46290
   476
text {* Infix operators are specified by convenient short forms that
wenzelm@46290
   477
  abbreviate general mixfix annotations as follows:
wenzelm@46290
   478
wenzelm@46290
   479
  \begin{center}
wenzelm@46290
   480
  \begin{tabular}{lll}
wenzelm@46290
   481
wenzelm@46292
   482
  @{verbatim "("}@{keyword_def "infix"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
wenzelm@46290
   483
  & @{text "\<mapsto>"} &
wenzelm@46290
   484
  @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
wenzelm@46292
   485
  @{verbatim "("}@{keyword_def "infixl"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
wenzelm@46290
   486
  & @{text "\<mapsto>"} &
wenzelm@46290
   487
  @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p"}@{verbatim ", "}@{text "p + 1"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
wenzelm@46292
   488
  @{verbatim "("}@{keyword_def "infixr"}~@{verbatim "\""}@{text sy}@{verbatim "\""} @{text "p"}@{verbatim ")"}
wenzelm@46290
   489
  & @{text "\<mapsto>"} &
wenzelm@46290
   490
  @{verbatim "(\"(_ "}@{text sy}@{verbatim "/ _)\" ["}@{text "p + 1"}@{verbatim ", "}@{text "p"}@{verbatim "]"}@{text " p"}@{verbatim ")"} \\
wenzelm@46290
   491
wenzelm@46290
   492
  \end{tabular}
wenzelm@46290
   493
  \end{center}
wenzelm@46290
   494
wenzelm@46292
   495
  The mixfix template @{verbatim "\"(_ "}@{text sy}@{verbatim "/ _)\""}
wenzelm@46292
   496
  specifies two argument positions; the delimiter is preceded by a
wenzelm@46292
   497
  space and followed by a space or line break; the entire phrase is a
wenzelm@46292
   498
  pretty printing block.
wenzelm@46290
   499
wenzelm@46290
   500
  The alternative notation @{verbatim "op"}~@{text sy} is introduced
wenzelm@46290
   501
  in addition.  Thus any infix operator may be written in prefix form
wenzelm@46290
   502
  (as in ML), independently of the number of arguments in the term.
wenzelm@46290
   503
*}
wenzelm@46290
   504
wenzelm@46290
   505
wenzelm@46290
   506
subsection {* Binders *}
wenzelm@46290
   507
wenzelm@46290
   508
text {* A \emph{binder} is a variable-binding construct such as a
wenzelm@46290
   509
  quantifier.  The idea to formalize @{text "\<forall>x. b"} as @{text "All
wenzelm@46290
   510
  (\<lambda>x. b)"} for @{text "All :: ('a \<Rightarrow> bool) \<Rightarrow> bool"} already goes back
wenzelm@46290
   511
  to \cite{church40}.  Isabelle declarations of certain higher-order
wenzelm@46292
   512
  operators may be annotated with @{keyword_def "binder"} annotations
wenzelm@46292
   513
  as follows:
wenzelm@46290
   514
wenzelm@46290
   515
  \begin{center}
wenzelm@46290
   516
  @{text "c :: "}@{verbatim "\""}@{text "(\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2) \<Rightarrow> \<tau>\<^sub>3"}@{verbatim "\"  ("}@{keyword "binder"}@{verbatim " \""}@{text "sy"}@{verbatim "\" ["}@{text "p"}@{verbatim "] "}@{text "q"}@{verbatim ")"}
wenzelm@46290
   517
  \end{center}
wenzelm@46290
   518
wenzelm@46290
   519
  This introduces concrete binder syntax @{text "sy x. b"}, where
wenzelm@46290
   520
  @{text x} is a bound variable of type @{text "\<tau>\<^sub>1"}, the body @{text
wenzelm@46290
   521
  b} has type @{text "\<tau>\<^sub>2"} and the whole term has type @{text "\<tau>\<^sub>3"}.
wenzelm@46290
   522
  The optional integer @{text p} specifies the syntactic priority of
wenzelm@46290
   523
  the body; the default is @{text "q"}, which is also the priority of
wenzelm@46290
   524
  the whole construct.
wenzelm@46290
   525
wenzelm@46290
   526
  Internally, the binder syntax is expanded to something like this:
wenzelm@46290
   527
  \begin{center}
wenzelm@46290
   528
  @{text "c_binder :: "}@{verbatim "\""}@{text "idts \<Rightarrow> \<tau>\<^sub>2 \<Rightarrow> \<tau>\<^sub>3"}@{verbatim "\"  (\"(3"}@{text sy}@{verbatim "_./ _)\" [0, "}@{text "p"}@{verbatim "] "}@{text "q"}@{verbatim ")"}
wenzelm@46290
   529
  \end{center}
wenzelm@46290
   530
wenzelm@46290
   531
  Here @{syntax (inner) idts} is the nonterminal symbol for a list of
wenzelm@46290
   532
  identifiers with optional type constraints (see also
wenzelm@46290
   533
  \secref{sec:pure-grammar}).  The mixfix template @{verbatim
wenzelm@46290
   534
  "\"(3"}@{text sy}@{verbatim "_./ _)\""} defines argument positions
wenzelm@46290
   535
  for the bound identifiers and the body, separated by a dot with
wenzelm@46290
   536
  optional line break; the entire phrase is a pretty printing block of
wenzelm@46290
   537
  indentation level 3.  Note that there is no extra space after @{text
wenzelm@46290
   538
  "sy"}, so it needs to be included user specification if the binder
wenzelm@46290
   539
  syntax ends with a token that may be continued by an identifier
wenzelm@46290
   540
  token at the start of @{syntax (inner) idts}.
wenzelm@46290
   541
wenzelm@46290
   542
  Furthermore, a syntax translation to transforms @{text "c_binder x\<^sub>1
wenzelm@46290
   543
  \<dots> x\<^sub>n b"} into iterated application @{text "c (\<lambda>x\<^sub>1. \<dots> c (\<lambda>x\<^sub>n. b)\<dots>)"}.
wenzelm@46290
   544
  This works in both directions, for parsing and printing.  *}
wenzelm@46290
   545
wenzelm@46290
   546
wenzelm@46282
   547
section {* Explicit notation \label{sec:notation} *}
wenzelm@28762
   548
wenzelm@28762
   549
text {*
wenzelm@28762
   550
  \begin{matharray}{rcll}
wenzelm@35413
   551
    @{command_def "type_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@35413
   552
    @{command_def "no_type_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   553
    @{command_def "notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   554
    @{command_def "no_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@36508
   555
    @{command_def "write"} & : & @{text "proof(state) \<rightarrow> proof(state)"} \\
wenzelm@28762
   556
  \end{matharray}
wenzelm@28762
   557
wenzelm@46288
   558
  Commands that introduce new logical entities (terms or types)
wenzelm@46288
   559
  usually allow to provide mixfix annotations on the spot, which is
wenzelm@46288
   560
  convenient for default notation.  Nonetheless, the syntax may be
wenzelm@46288
   561
  modified later on by declarations for explicit notation.  This
wenzelm@46288
   562
  allows to add or delete mixfix annotations for of existing logical
wenzelm@46288
   563
  entities within the current context.
wenzelm@46288
   564
wenzelm@42596
   565
  @{rail "
wenzelm@42596
   566
    (@@{command type_notation} | @@{command no_type_notation}) @{syntax target}?
wenzelm@42596
   567
      @{syntax mode}? \\ (@{syntax nameref} @{syntax mixfix} + @'and')
wenzelm@35413
   568
    ;
wenzelm@42596
   569
    (@@{command notation} | @@{command no_notation}) @{syntax target}? @{syntax mode}? \\
wenzelm@51654
   570
      (@{syntax nameref} @{syntax mixfix} + @'and')
wenzelm@28762
   571
    ;
wenzelm@51654
   572
    @@{command write} @{syntax mode}? (@{syntax nameref} @{syntax mixfix} + @'and')
wenzelm@42596
   573
  "}
wenzelm@28762
   574
wenzelm@28762
   575
  \begin{description}
wenzelm@28762
   576
wenzelm@35413
   577
  \item @{command "type_notation"}~@{text "c (mx)"} associates mixfix
wenzelm@35413
   578
  syntax with an existing type constructor.  The arity of the
wenzelm@35413
   579
  constructor is retrieved from the context.
wenzelm@46282
   580
wenzelm@35413
   581
  \item @{command "no_type_notation"} is similar to @{command
wenzelm@35413
   582
  "type_notation"}, but removes the specified syntax annotation from
wenzelm@35413
   583
  the present context.
wenzelm@35413
   584
wenzelm@28762
   585
  \item @{command "notation"}~@{text "c (mx)"} associates mixfix
wenzelm@35413
   586
  syntax with an existing constant or fixed variable.  The type
wenzelm@35413
   587
  declaration of the given entity is retrieved from the context.
wenzelm@46282
   588
wenzelm@28762
   589
  \item @{command "no_notation"} is similar to @{command "notation"},
wenzelm@28762
   590
  but removes the specified syntax annotation from the present
wenzelm@28762
   591
  context.
wenzelm@28762
   592
wenzelm@36508
   593
  \item @{command "write"} is similar to @{command "notation"}, but
wenzelm@36508
   594
  works within an Isar proof body.
wenzelm@36508
   595
wenzelm@28762
   596
  \end{description}
wenzelm@28762
   597
*}
wenzelm@28762
   598
wenzelm@28778
   599
wenzelm@28778
   600
section {* The Pure syntax \label{sec:pure-syntax} *}
wenzelm@28769
   601
wenzelm@46282
   602
subsection {* Lexical matters \label{sec:inner-lex} *}
wenzelm@46282
   603
wenzelm@46282
   604
text {* The inner lexical syntax vaguely resembles the outer one
wenzelm@46282
   605
  (\secref{sec:outer-lex}), but some details are different.  There are
wenzelm@46282
   606
  two main categories of inner syntax tokens:
wenzelm@46282
   607
wenzelm@46282
   608
  \begin{enumerate}
wenzelm@46282
   609
wenzelm@46282
   610
  \item \emph{delimiters} --- the literal tokens occurring in
wenzelm@46282
   611
  productions of the given priority grammar (cf.\
wenzelm@46282
   612
  \secref{sec:priority-grammar});
wenzelm@46282
   613
wenzelm@46282
   614
  \item \emph{named tokens} --- various categories of identifiers etc.
wenzelm@46282
   615
wenzelm@46282
   616
  \end{enumerate}
wenzelm@46282
   617
wenzelm@46282
   618
  Delimiters override named tokens and may thus render certain
wenzelm@46282
   619
  identifiers inaccessible.  Sometimes the logical context admits
wenzelm@46282
   620
  alternative ways to refer to the same entity, potentially via
wenzelm@46282
   621
  qualified names.
wenzelm@46282
   622
wenzelm@46282
   623
  \medskip The categories for named tokens are defined once and for
wenzelm@46282
   624
  all as follows, reusing some categories of the outer token syntax
wenzelm@46282
   625
  (\secref{sec:outer-lex}).
wenzelm@46282
   626
wenzelm@46282
   627
  \begin{center}
wenzelm@46282
   628
  \begin{supertabular}{rcl}
wenzelm@46282
   629
    @{syntax_def (inner) id} & = & @{syntax_ref ident} \\
wenzelm@46282
   630
    @{syntax_def (inner) longid} & = & @{syntax_ref longident} \\
wenzelm@46282
   631
    @{syntax_def (inner) var} & = & @{syntax_ref var} \\
wenzelm@46282
   632
    @{syntax_def (inner) tid} & = & @{syntax_ref typefree} \\
wenzelm@46282
   633
    @{syntax_def (inner) tvar} & = & @{syntax_ref typevar} \\
wenzelm@46282
   634
    @{syntax_def (inner) num_token} & = & @{syntax_ref nat}@{text "  |  "}@{verbatim "-"}@{syntax_ref nat} \\
wenzelm@46282
   635
    @{syntax_def (inner) float_token} & = & @{syntax_ref nat}@{verbatim "."}@{syntax_ref nat}@{text "  |  "}@{verbatim "-"}@{syntax_ref nat}@{verbatim "."}@{syntax_ref nat} \\
wenzelm@46282
   636
    @{syntax_def (inner) xnum_token} & = & @{verbatim "#"}@{syntax_ref nat}@{text "  |  "}@{verbatim "#-"}@{syntax_ref nat} \\
wenzelm@46282
   637
wenzelm@46483
   638
    @{syntax_def (inner) str_token} & = & @{verbatim "''"} @{text "\<dots>"} @{verbatim "''"} \\
wenzelm@46282
   639
  \end{supertabular}
wenzelm@46282
   640
  \end{center}
wenzelm@46282
   641
wenzelm@46282
   642
  The token categories @{syntax (inner) num_token}, @{syntax (inner)
wenzelm@46282
   643
  float_token}, @{syntax (inner) xnum_token}, and @{syntax (inner)
wenzelm@46483
   644
  str_token} are not used in Pure.  Object-logics may implement numerals
wenzelm@46282
   645
  and string constants by adding appropriate syntax declarations,
wenzelm@46282
   646
  together with some translation functions (e.g.\ see Isabelle/HOL).
wenzelm@46282
   647
wenzelm@46282
   648
  The derived categories @{syntax_def (inner) num_const}, @{syntax_def
wenzelm@46282
   649
  (inner) float_const}, and @{syntax_def (inner) num_const} provide
wenzelm@46282
   650
  robust access to the respective tokens: the syntax tree holds a
wenzelm@46282
   651
  syntactic constant instead of a free variable.
wenzelm@46282
   652
*}
wenzelm@46282
   653
wenzelm@46282
   654
wenzelm@28777
   655
subsection {* Priority grammars \label{sec:priority-grammar} *}
wenzelm@28769
   656
wenzelm@28769
   657
text {* A context-free grammar consists of a set of \emph{terminal
wenzelm@28769
   658
  symbols}, a set of \emph{nonterminal symbols} and a set of
wenzelm@28769
   659
  \emph{productions}.  Productions have the form @{text "A = \<gamma>"},
wenzelm@28769
   660
  where @{text A} is a nonterminal and @{text \<gamma>} is a string of
wenzelm@28769
   661
  terminals and nonterminals.  One designated nonterminal is called
wenzelm@28769
   662
  the \emph{root symbol}.  The language defined by the grammar
wenzelm@28769
   663
  consists of all strings of terminals that can be derived from the
wenzelm@28769
   664
  root symbol by applying productions as rewrite rules.
wenzelm@28769
   665
wenzelm@28769
   666
  The standard Isabelle parser for inner syntax uses a \emph{priority
wenzelm@28769
   667
  grammar}.  Each nonterminal is decorated by an integer priority:
wenzelm@28769
   668
  @{text "A\<^sup>(\<^sup>p\<^sup>)"}.  In a derivation, @{text "A\<^sup>(\<^sup>p\<^sup>)"} may be rewritten
wenzelm@28769
   669
  using a production @{text "A\<^sup>(\<^sup>q\<^sup>) = \<gamma>"} only if @{text "p \<le> q"}.  Any
wenzelm@28769
   670
  priority grammar can be translated into a normal context-free
wenzelm@28769
   671
  grammar by introducing new nonterminals and productions.
wenzelm@28769
   672
wenzelm@28769
   673
  \medskip Formally, a set of context free productions @{text G}
wenzelm@28769
   674
  induces a derivation relation @{text "\<longrightarrow>\<^sub>G"} as follows.  Let @{text
wenzelm@28769
   675
  \<alpha>} and @{text \<beta>} denote strings of terminal or nonterminal symbols.
wenzelm@28774
   676
  Then @{text "\<alpha> A\<^sup>(\<^sup>p\<^sup>) \<beta> \<longrightarrow>\<^sub>G \<alpha> \<gamma> \<beta>"} holds if and only if @{text G}
wenzelm@28774
   677
  contains some production @{text "A\<^sup>(\<^sup>q\<^sup>) = \<gamma>"} for @{text "p \<le> q"}.
wenzelm@28769
   678
wenzelm@28769
   679
  \medskip The following grammar for arithmetic expressions
wenzelm@28769
   680
  demonstrates how binding power and associativity of operators can be
wenzelm@28769
   681
  enforced by priorities.
wenzelm@28769
   682
wenzelm@28769
   683
  \begin{center}
wenzelm@28769
   684
  \begin{tabular}{rclr}
wenzelm@28774
   685
  @{text "A\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)"} & @{text "="} & @{verbatim "("} @{text "A\<^sup>(\<^sup>0\<^sup>)"} @{verbatim ")"} \\
wenzelm@28769
   686
  @{text "A\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)"} & @{text "="} & @{verbatim 0} \\
wenzelm@28769
   687
  @{text "A\<^sup>(\<^sup>0\<^sup>)"} & @{text "="} & @{text "A\<^sup>(\<^sup>0\<^sup>)"} @{verbatim "+"} @{text "A\<^sup>(\<^sup>1\<^sup>)"} \\
wenzelm@28769
   688
  @{text "A\<^sup>(\<^sup>2\<^sup>)"} & @{text "="} & @{text "A\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "*"} @{text "A\<^sup>(\<^sup>2\<^sup>)"} \\
wenzelm@28769
   689
  @{text "A\<^sup>(\<^sup>3\<^sup>)"} & @{text "="} & @{verbatim "-"} @{text "A\<^sup>(\<^sup>3\<^sup>)"} \\
wenzelm@28769
   690
  \end{tabular}
wenzelm@28769
   691
  \end{center}
wenzelm@28769
   692
  The choice of priorities determines that @{verbatim "-"} binds
wenzelm@28769
   693
  tighter than @{verbatim "*"}, which binds tighter than @{verbatim
wenzelm@28769
   694
  "+"}.  Furthermore @{verbatim "+"} associates to the left and
wenzelm@28769
   695
  @{verbatim "*"} to the right.
wenzelm@28769
   696
wenzelm@28769
   697
  \medskip For clarity, grammars obey these conventions:
wenzelm@28769
   698
  \begin{itemize}
wenzelm@28769
   699
wenzelm@28769
   700
  \item All priorities must lie between 0 and 1000.
wenzelm@28769
   701
wenzelm@28769
   702
  \item Priority 0 on the right-hand side and priority 1000 on the
wenzelm@28769
   703
  left-hand side may be omitted.
wenzelm@28769
   704
wenzelm@28769
   705
  \item The production @{text "A\<^sup>(\<^sup>p\<^sup>) = \<alpha>"} is written as @{text "A = \<alpha>
wenzelm@28769
   706
  (p)"}, i.e.\ the priority of the left-hand side actually appears in
wenzelm@28769
   707
  a column on the far right.
wenzelm@28769
   708
wenzelm@28769
   709
  \item Alternatives are separated by @{text "|"}.
wenzelm@28769
   710
wenzelm@28769
   711
  \item Repetition is indicated by dots @{text "(\<dots>)"} in an informal
wenzelm@28769
   712
  but obvious way.
wenzelm@28769
   713
wenzelm@28769
   714
  \end{itemize}
wenzelm@28769
   715
wenzelm@28769
   716
  Using these conventions, the example grammar specification above
wenzelm@28769
   717
  takes the form:
wenzelm@28769
   718
  \begin{center}
wenzelm@28769
   719
  \begin{tabular}{rclc}
wenzelm@28774
   720
    @{text A} & @{text "="} & @{verbatim "("} @{text A} @{verbatim ")"} \\
wenzelm@28774
   721
              & @{text "|"} & @{verbatim 0} & \qquad\qquad \\
wenzelm@28769
   722
              & @{text "|"} & @{text A} @{verbatim "+"} @{text "A\<^sup>(\<^sup>1\<^sup>)"} & @{text "(0)"} \\
wenzelm@28769
   723
              & @{text "|"} & @{text "A\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "*"} @{text "A\<^sup>(\<^sup>2\<^sup>)"} & @{text "(2)"} \\
wenzelm@28769
   724
              & @{text "|"} & @{verbatim "-"} @{text "A\<^sup>(\<^sup>3\<^sup>)"} & @{text "(3)"} \\
wenzelm@28769
   725
  \end{tabular}
wenzelm@28769
   726
  \end{center}
wenzelm@28769
   727
*}
wenzelm@28769
   728
wenzelm@28769
   729
wenzelm@46290
   730
subsection {* The Pure grammar \label{sec:pure-grammar} *}
wenzelm@28770
   731
wenzelm@46287
   732
text {* The priority grammar of the @{text "Pure"} theory is defined
wenzelm@46287
   733
  approximately like this:
wenzelm@28774
   734
wenzelm@28770
   735
  \begin{center}
wenzelm@28773
   736
  \begin{supertabular}{rclr}
wenzelm@28770
   737
wenzelm@28778
   738
  @{syntax_def (inner) any} & = & @{text "prop  |  logic"} \\\\
wenzelm@28772
   739
wenzelm@28778
   740
  @{syntax_def (inner) prop} & = & @{verbatim "("} @{text prop} @{verbatim ")"} \\
wenzelm@28772
   741
    & @{text "|"} & @{text "prop\<^sup>(\<^sup>4\<^sup>)"} @{verbatim "::"} @{text type} & @{text "(3)"} \\
wenzelm@50636
   742
    & @{text "|"} & @{text "any\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "=="} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(2)"} \\
wenzelm@50636
   743
    & @{text "|"} & @{text "any\<^sup>(\<^sup>3\<^sup>)"} @{text "\<equiv>"} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(2)"} \\
wenzelm@28856
   744
    & @{text "|"} & @{text "prop\<^sup>(\<^sup>3\<^sup>)"} @{verbatim "&&&"} @{text "prop\<^sup>(\<^sup>2\<^sup>)"} & @{text "(2)"} \\
wenzelm@28772
   745
    & @{text "|"} & @{text "prop\<^sup>(\<^sup>2\<^sup>)"} @{verbatim "==>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\
wenzelm@28773
   746
    & @{text "|"} & @{text "prop\<^sup>(\<^sup>2\<^sup>)"} @{text "\<Longrightarrow>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\
wenzelm@28772
   747
    & @{text "|"} & @{verbatim "[|"} @{text prop} @{verbatim ";"} @{text "\<dots>"} @{verbatim ";"} @{text prop} @{verbatim "|]"} @{verbatim "==>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\
wenzelm@28773
   748
    & @{text "|"} & @{text "\<lbrakk>"} @{text prop} @{verbatim ";"} @{text "\<dots>"} @{verbatim ";"} @{text prop} @{text "\<rbrakk>"} @{text "\<Longrightarrow>"} @{text "prop\<^sup>(\<^sup>1\<^sup>)"} & @{text "(1)"} \\
wenzelm@28772
   749
    & @{text "|"} & @{verbatim "!!"} @{text idts} @{verbatim "."} @{text prop} & @{text "(0)"} \\
wenzelm@28773
   750
    & @{text "|"} & @{text "\<And>"} @{text idts} @{verbatim "."} @{text prop} & @{text "(0)"} \\
wenzelm@28773
   751
    & @{text "|"} & @{verbatim OFCLASS} @{verbatim "("} @{text type} @{verbatim ","} @{text logic} @{verbatim ")"} \\
wenzelm@28773
   752
    & @{text "|"} & @{verbatim SORT_CONSTRAINT} @{verbatim "("} @{text type} @{verbatim ")"} \\
wenzelm@28856
   753
    & @{text "|"} & @{verbatim TERM} @{text logic} \\
wenzelm@28773
   754
    & @{text "|"} & @{verbatim PROP} @{text aprop} \\\\
wenzelm@28772
   755
wenzelm@28856
   756
  @{syntax_def (inner) aprop} & = & @{verbatim "("} @{text aprop} @{verbatim ")"} \\
wenzelm@28856
   757
    & @{text "|"} & @{text "id  |  longid  |  var  |  "}@{verbatim "_"}@{text "  |  "}@{verbatim "..."} \\
wenzelm@28856
   758
    & @{text "|"} & @{verbatim CONST} @{text "id  |  "}@{verbatim CONST} @{text "longid"} \\
wenzelm@46287
   759
    & @{text "|"} & @{verbatim XCONST} @{text "id  |  "}@{verbatim XCONST} @{text "longid"} \\
wenzelm@28773
   760
    & @{text "|"} & @{text "logic\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)  any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) \<dots> any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)"} & @{text "(999)"} \\\\
wenzelm@28770
   761
wenzelm@28778
   762
  @{syntax_def (inner) logic} & = & @{verbatim "("} @{text logic} @{verbatim ")"} \\
wenzelm@28772
   763
    & @{text "|"} & @{text "logic\<^sup>(\<^sup>4\<^sup>)"} @{verbatim "::"} @{text type} & @{text "(3)"} \\
wenzelm@28773
   764
    & @{text "|"} & @{text "id  |  longid  |  var  |  "}@{verbatim "_"}@{text "  |  "}@{verbatim "..."} \\
wenzelm@28856
   765
    & @{text "|"} & @{verbatim CONST} @{text "id  |  "}@{verbatim CONST} @{text "longid"} \\
wenzelm@46287
   766
    & @{text "|"} & @{verbatim XCONST} @{text "id  |  "}@{verbatim XCONST} @{text "longid"} \\
wenzelm@28773
   767
    & @{text "|"} & @{text "logic\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)  any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) \<dots> any\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)"} & @{text "(999)"} \\
wenzelm@46287
   768
    & @{text "|"} & @{text "\<struct> index\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>)"} \\
wenzelm@28772
   769
    & @{text "|"} & @{verbatim "%"} @{text pttrns} @{verbatim "."} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(3)"} \\
wenzelm@28773
   770
    & @{text "|"} & @{text \<lambda>} @{text pttrns} @{verbatim "."} @{text "any\<^sup>(\<^sup>3\<^sup>)"} & @{text "(3)"} \\
wenzelm@46287
   771
    & @{text "|"} & @{verbatim op} @{verbatim "=="}@{text "  |  "}@{verbatim op} @{text "\<equiv>"}@{text "  |  "}@{verbatim op} @{verbatim "&&&"} \\
wenzelm@46287
   772
    & @{text "|"} & @{verbatim op} @{verbatim "==>"}@{text "  |  "}@{verbatim op} @{text "\<Longrightarrow>"} \\
wenzelm@28772
   773
    & @{text "|"} & @{verbatim TYPE} @{verbatim "("} @{text type} @{verbatim ")"} \\\\
wenzelm@28772
   774
wenzelm@28778
   775
  @{syntax_def (inner) idt} & = & @{verbatim "("} @{text idt} @{verbatim ")"}@{text "  |  id  |  "}@{verbatim "_"} \\
wenzelm@28773
   776
    & @{text "|"} & @{text id} @{verbatim "::"} @{text type} & @{text "(0)"} \\
wenzelm@28773
   777
    & @{text "|"} & @{verbatim "_"} @{verbatim "::"} @{text type} & @{text "(0)"} \\\\
wenzelm@28772
   778
wenzelm@46287
   779
  @{syntax_def (inner) index} & = & @{verbatim "\<^bsub>"} @{text "logic\<^sup>(\<^sup>0\<^sup>)"} @{verbatim "\<^esub>"}@{text "  |  |  \<index>"} \\\\
wenzelm@46287
   780
wenzelm@28778
   781
  @{syntax_def (inner) idts} & = & @{text "idt  |  idt\<^sup>(\<^sup>1\<^sup>) idts"} & @{text "(0)"} \\\\
wenzelm@28772
   782
wenzelm@28778
   783
  @{syntax_def (inner) pttrn} & = & @{text idt} \\\\
wenzelm@28772
   784
wenzelm@28778
   785
  @{syntax_def (inner) pttrns} & = & @{text "pttrn  |  pttrn\<^sup>(\<^sup>1\<^sup>) pttrns"} & @{text "(0)"} \\\\
wenzelm@28774
   786
wenzelm@28778
   787
  @{syntax_def (inner) type} & = & @{verbatim "("} @{text type} @{verbatim ")"} \\
wenzelm@28773
   788
    & @{text "|"} & @{text "tid  |  tvar  |  "}@{verbatim "_"} \\
wenzelm@28773
   789
    & @{text "|"} & @{text "tid"} @{verbatim "::"} @{text "sort  |  tvar  "}@{verbatim "::"} @{text "sort  |  "}@{verbatim "_"} @{verbatim "::"} @{text "sort"} \\
wenzelm@46287
   790
    & @{text "|"} & @{text "type_name  |  type\<^sup>(\<^sup>1\<^sup>0\<^sup>0\<^sup>0\<^sup>) type_name"} \\
wenzelm@46287
   791
    & @{text "|"} & @{verbatim "("} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim ")"} @{text type_name} \\
wenzelm@28772
   792
    & @{text "|"} & @{text "type\<^sup>(\<^sup>1\<^sup>)"} @{verbatim "=>"} @{text type} & @{text "(0)"} \\
wenzelm@28773
   793
    & @{text "|"} & @{text "type\<^sup>(\<^sup>1\<^sup>)"} @{text "\<Rightarrow>"} @{text type} & @{text "(0)"} \\
wenzelm@28773
   794
    & @{text "|"} & @{verbatim "["} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim "]"} @{verbatim "=>"} @{text type} & @{text "(0)"} \\
wenzelm@46287
   795
    & @{text "|"} & @{verbatim "["} @{text type} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{text type} @{verbatim "]"} @{text "\<Rightarrow>"} @{text type} & @{text "(0)"} \\
wenzelm@46287
   796
  @{syntax_def (inner) type_name} & = & @{text "id  |  longid"} \\\\
wenzelm@28772
   797
wenzelm@46287
   798
  @{syntax_def (inner) sort} & = & @{syntax class_name}~@{text "  |  "}@{verbatim "{}"} \\
wenzelm@46287
   799
    & @{text "|"} & @{verbatim "{"} @{syntax class_name} @{verbatim ","} @{text "\<dots>"} @{verbatim ","} @{syntax class_name} @{verbatim "}"} \\
wenzelm@46287
   800
  @{syntax_def (inner) class_name} & = & @{text "id  |  longid"} \\
wenzelm@28773
   801
  \end{supertabular}
wenzelm@28770
   802
  \end{center}
wenzelm@28770
   803
wenzelm@28774
   804
  \medskip Here literal terminals are printed @{verbatim "verbatim"};
wenzelm@28774
   805
  see also \secref{sec:inner-lex} for further token categories of the
wenzelm@28774
   806
  inner syntax.  The meaning of the nonterminals defined by the above
wenzelm@28774
   807
  grammar is as follows:
wenzelm@28770
   808
wenzelm@28770
   809
  \begin{description}
wenzelm@28770
   810
wenzelm@28778
   811
  \item @{syntax_ref (inner) any} denotes any term.
wenzelm@28770
   812
wenzelm@28778
   813
  \item @{syntax_ref (inner) prop} denotes meta-level propositions,
wenzelm@28778
   814
  which are terms of type @{typ prop}.  The syntax of such formulae of
wenzelm@28778
   815
  the meta-logic is carefully distinguished from usual conventions for
wenzelm@28778
   816
  object-logics.  In particular, plain @{text "\<lambda>"}-term notation is
wenzelm@28778
   817
  \emph{not} recognized as @{syntax (inner) prop}.
wenzelm@28770
   818
wenzelm@28778
   819
  \item @{syntax_ref (inner) aprop} denotes atomic propositions, which
wenzelm@28778
   820
  are embedded into regular @{syntax (inner) prop} by means of an
wenzelm@28778
   821
  explicit @{verbatim PROP} token.
wenzelm@28770
   822
wenzelm@28770
   823
  Terms of type @{typ prop} with non-constant head, e.g.\ a plain
wenzelm@28770
   824
  variable, are printed in this form.  Constants that yield type @{typ
wenzelm@28770
   825
  prop} are expected to provide their own concrete syntax; otherwise
wenzelm@28778
   826
  the printed version will appear like @{syntax (inner) logic} and
wenzelm@28778
   827
  cannot be parsed again as @{syntax (inner) prop}.
wenzelm@28770
   828
wenzelm@28778
   829
  \item @{syntax_ref (inner) logic} denotes arbitrary terms of a
wenzelm@28778
   830
  logical type, excluding type @{typ prop}.  This is the main
wenzelm@28778
   831
  syntactic category of object-logic entities, covering plain @{text
wenzelm@28778
   832
  \<lambda>}-term notation (variables, abstraction, application), plus
wenzelm@28778
   833
  anything defined by the user.
wenzelm@28770
   834
wenzelm@28770
   835
  When specifying notation for logical entities, all logical types
wenzelm@28770
   836
  (excluding @{typ prop}) are \emph{collapsed} to this single category
wenzelm@28778
   837
  of @{syntax (inner) logic}.
wenzelm@28770
   838
wenzelm@46287
   839
  \item @{syntax_ref (inner) index} denotes an optional index term for
wenzelm@51657
   840
  indexed syntax.  If omitted, it refers to the first @{keyword_ref
wenzelm@46287
   841
  "structure"} variable in the context.  The special dummy ``@{text
wenzelm@46287
   842
  "\<index>"}'' serves as pattern variable in mixfix annotations that
wenzelm@46287
   843
  introduce indexed notation.
wenzelm@46287
   844
wenzelm@28778
   845
  \item @{syntax_ref (inner) idt} denotes identifiers, possibly
wenzelm@28778
   846
  constrained by types.
wenzelm@28770
   847
wenzelm@28778
   848
  \item @{syntax_ref (inner) idts} denotes a sequence of @{syntax_ref
wenzelm@28778
   849
  (inner) idt}.  This is the most basic category for variables in
wenzelm@28778
   850
  iterated binders, such as @{text "\<lambda>"} or @{text "\<And>"}.
wenzelm@28770
   851
wenzelm@28778
   852
  \item @{syntax_ref (inner) pttrn} and @{syntax_ref (inner) pttrns}
wenzelm@28778
   853
  denote patterns for abstraction, cases bindings etc.  In Pure, these
wenzelm@28778
   854
  categories start as a merely copy of @{syntax (inner) idt} and
wenzelm@28778
   855
  @{syntax (inner) idts}, respectively.  Object-logics may add
wenzelm@28778
   856
  additional productions for binding forms.
wenzelm@28770
   857
wenzelm@28778
   858
  \item @{syntax_ref (inner) type} denotes types of the meta-logic.
wenzelm@28770
   859
wenzelm@28778
   860
  \item @{syntax_ref (inner) sort} denotes meta-level sorts.
wenzelm@28770
   861
wenzelm@28770
   862
  \end{description}
wenzelm@28770
   863
wenzelm@28774
   864
  Here are some further explanations of certain syntax features.
wenzelm@28773
   865
wenzelm@28773
   866
  \begin{itemize}
wenzelm@28770
   867
wenzelm@28778
   868
  \item In @{syntax (inner) idts}, note that @{text "x :: nat y"} is
wenzelm@28778
   869
  parsed as @{text "x :: (nat y)"}, treating @{text y} like a type
wenzelm@28778
   870
  constructor applied to @{text nat}.  To avoid this interpretation,
wenzelm@28778
   871
  write @{text "(x :: nat) y"} with explicit parentheses.
wenzelm@28773
   872
wenzelm@28773
   873
  \item Similarly, @{text "x :: nat y :: nat"} is parsed as @{text "x ::
wenzelm@28770
   874
  (nat y :: nat)"}.  The correct form is @{text "(x :: nat) (y ::
wenzelm@28770
   875
  nat)"}, or @{text "(x :: nat) y :: nat"} if @{text y} is last in the
wenzelm@28770
   876
  sequence of identifiers.
wenzelm@28773
   877
wenzelm@28773
   878
  \item Type constraints for terms bind very weakly.  For example,
wenzelm@28773
   879
  @{text "x < y :: nat"} is normally parsed as @{text "(x < y) ::
wenzelm@28773
   880
  nat"}, unless @{text "<"} has a very low priority, in which case the
wenzelm@28773
   881
  input is likely to be ambiguous.  The correct form is @{text "x < (y
wenzelm@28773
   882
  :: nat)"}.
wenzelm@28773
   883
wenzelm@28773
   884
  \item Constraints may be either written with two literal colons
wenzelm@28773
   885
  ``@{verbatim "::"}'' or the double-colon symbol @{verbatim "\<Colon>"},
wenzelm@28774
   886
  which actually looks exactly the same in some {\LaTeX} styles.
wenzelm@28773
   887
wenzelm@28774
   888
  \item Dummy variables (written as underscore) may occur in different
wenzelm@28774
   889
  roles.
wenzelm@28773
   890
wenzelm@28773
   891
  \begin{description}
wenzelm@28773
   892
wenzelm@28774
   893
  \item A type ``@{text "_"}'' or ``@{text "_ :: sort"}'' acts like an
wenzelm@28774
   894
  anonymous inference parameter, which is filled-in according to the
wenzelm@28774
   895
  most general type produced by the type-checking phase.
wenzelm@28770
   896
wenzelm@28774
   897
  \item A bound ``@{text "_"}'' refers to a vacuous abstraction, where
wenzelm@28774
   898
  the body does not refer to the binding introduced here.  As in the
wenzelm@28774
   899
  term @{term "\<lambda>x _. x"}, which is @{text "\<alpha>"}-equivalent to @{text
wenzelm@28774
   900
  "\<lambda>x y. x"}.
wenzelm@28773
   901
wenzelm@28774
   902
  \item A free ``@{text "_"}'' refers to an implicit outer binding.
wenzelm@28774
   903
  Higher definitional packages usually allow forms like @{text "f x _
wenzelm@28774
   904
  = x"}.
wenzelm@28773
   905
wenzelm@28774
   906
  \item A schematic ``@{text "_"}'' (within a term pattern, see
wenzelm@28774
   907
  \secref{sec:term-decls}) refers to an anonymous variable that is
wenzelm@28774
   908
  implicitly abstracted over its context of locally bound variables.
wenzelm@28774
   909
  For example, this allows pattern matching of @{text "{x. f x = g
wenzelm@28774
   910
  x}"} against @{text "{x. _ = _}"}, or even @{text "{_. _ = _}"} by
wenzelm@28774
   911
  using both bound and schematic dummies.
wenzelm@28773
   912
wenzelm@28773
   913
  \end{description}
wenzelm@28773
   914
wenzelm@28774
   915
  \item The three literal dots ``@{verbatim "..."}'' may be also
wenzelm@28774
   916
  written as ellipsis symbol @{verbatim "\<dots>"}.  In both cases this
wenzelm@28774
   917
  refers to a special schematic variable, which is bound in the
wenzelm@28774
   918
  context.  This special term abbreviation works nicely with
wenzelm@28774
   919
  calculational reasoning (\secref{sec:calculation}).
wenzelm@28774
   920
wenzelm@46287
   921
  \item @{verbatim CONST} ensures that the given identifier is treated
wenzelm@46287
   922
  as constant term, and passed through the parse tree in fully
wenzelm@46287
   923
  internalized form.  This is particularly relevant for translation
wenzelm@46287
   924
  rules (\secref{sec:syn-trans}), notably on the RHS.
wenzelm@46287
   925
wenzelm@46287
   926
  \item @{verbatim XCONST} is similar to @{verbatim CONST}, but
wenzelm@46287
   927
  retains the constant name as given.  This is only relevant to
wenzelm@46287
   928
  translation rules (\secref{sec:syn-trans}), notably on the LHS.
wenzelm@46287
   929
wenzelm@28773
   930
  \end{itemize}
wenzelm@28770
   931
*}
wenzelm@28770
   932
wenzelm@28777
   933
wenzelm@46282
   934
subsection {* Inspecting the syntax *}
wenzelm@28777
   935
wenzelm@46282
   936
text {*
wenzelm@46282
   937
  \begin{matharray}{rcl}
wenzelm@46282
   938
    @{command_def "print_syntax"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@46282
   939
  \end{matharray}
wenzelm@28777
   940
wenzelm@46282
   941
  \begin{description}
wenzelm@46282
   942
wenzelm@46282
   943
  \item @{command "print_syntax"} prints the inner syntax of the
wenzelm@46282
   944
  current context.  The output can be quite large; the most important
wenzelm@46282
   945
  sections are explained below.
wenzelm@28777
   946
wenzelm@46282
   947
  \begin{description}
wenzelm@28777
   948
wenzelm@46282
   949
  \item @{text "lexicon"} lists the delimiters of the inner token
wenzelm@46282
   950
  language; see \secref{sec:inner-lex}.
wenzelm@28777
   951
wenzelm@46282
   952
  \item @{text "prods"} lists the productions of the underlying
wenzelm@46282
   953
  priority grammar; see \secref{sec:priority-grammar}.
wenzelm@28777
   954
wenzelm@46282
   955
  The nonterminal @{text "A\<^sup>(\<^sup>p\<^sup>)"} is rendered in plain text as @{text
wenzelm@46282
   956
  "A[p]"}; delimiters are quoted.  Many productions have an extra
wenzelm@46282
   957
  @{text "\<dots> => name"}.  These names later become the heads of parse
wenzelm@46282
   958
  trees; they also guide the pretty printer.
wenzelm@28777
   959
wenzelm@46282
   960
  Productions without such parse tree names are called \emph{copy
wenzelm@46282
   961
  productions}.  Their right-hand side must have exactly one
wenzelm@46282
   962
  nonterminal symbol (or named token).  The parser does not create a
wenzelm@46282
   963
  new parse tree node for copy productions, but simply returns the
wenzelm@46282
   964
  parse tree of the right-hand symbol.
wenzelm@46282
   965
wenzelm@46282
   966
  If the right-hand side of a copy production consists of a single
wenzelm@46282
   967
  nonterminal without any delimiters, then it is called a \emph{chain
wenzelm@46282
   968
  production}.  Chain productions act as abbreviations: conceptually,
wenzelm@46282
   969
  they are removed from the grammar by adding new productions.
wenzelm@46282
   970
  Priority information attached to chain productions is ignored; only
wenzelm@46282
   971
  the dummy value @{text "-1"} is displayed.
wenzelm@46282
   972
wenzelm@46282
   973
  \item @{text "print modes"} lists the alternative print modes
wenzelm@46282
   974
  provided by this grammar; see \secref{sec:print-modes}.
wenzelm@28777
   975
wenzelm@46282
   976
  \item @{text "parse_rules"} and @{text "print_rules"} relate to
wenzelm@46282
   977
  syntax translations (macros); see \secref{sec:syn-trans}.
wenzelm@46282
   978
wenzelm@46282
   979
  \item @{text "parse_ast_translation"} and @{text
wenzelm@46282
   980
  "print_ast_translation"} list sets of constants that invoke
wenzelm@46282
   981
  translation functions for abstract syntax trees, which are only
wenzelm@46282
   982
  required in very special situations; see \secref{sec:tr-funs}.
wenzelm@28777
   983
wenzelm@46282
   984
  \item @{text "parse_translation"} and @{text "print_translation"}
wenzelm@46282
   985
  list the sets of constants that invoke regular translation
wenzelm@46282
   986
  functions; see \secref{sec:tr-funs}.
wenzelm@29157
   987
wenzelm@46282
   988
  \end{description}
wenzelm@46282
   989
wenzelm@46282
   990
  \end{description}
wenzelm@28777
   991
*}
wenzelm@28774
   992
wenzelm@28770
   993
wenzelm@46291
   994
subsection {* Ambiguity of parsed expressions *}
wenzelm@46291
   995
wenzelm@46291
   996
text {*
wenzelm@46291
   997
  \begin{tabular}{rcll}
wenzelm@46512
   998
    @{attribute_def syntax_ambiguity_warning} & : & @{text attribute} & default @{text true} \\
wenzelm@46506
   999
    @{attribute_def syntax_ambiguity_limit} & : & @{text attribute} & default @{text 10} \\
wenzelm@46291
  1000
  \end{tabular}
wenzelm@46291
  1001
wenzelm@46291
  1002
  Depending on the grammar and the given input, parsing may be
wenzelm@46291
  1003
  ambiguous.  Isabelle lets the Earley parser enumerate all possible
wenzelm@46291
  1004
  parse trees, and then tries to make the best out of the situation.
wenzelm@46291
  1005
  Terms that cannot be type-checked are filtered out, which often
wenzelm@46291
  1006
  leads to a unique result in the end.  Unlike regular type
wenzelm@46291
  1007
  reconstruction, which is applied to the whole collection of input
wenzelm@46291
  1008
  terms simultaneously, the filtering stage only treats each given
wenzelm@46291
  1009
  term in isolation.  Filtering is also not attempted for individual
wenzelm@46291
  1010
  types or raw ASTs (as required for @{command translations}).
wenzelm@46291
  1011
wenzelm@46291
  1012
  Certain warning or error messages are printed, depending on the
wenzelm@46291
  1013
  situation and the given configuration options.  Parsing ultimately
wenzelm@46291
  1014
  fails, if multiple results remain after the filtering phase.
wenzelm@46291
  1015
wenzelm@46291
  1016
  \begin{description}
wenzelm@46291
  1017
wenzelm@46512
  1018
  \item @{attribute syntax_ambiguity_warning} controls output of
wenzelm@46512
  1019
  explicit warning messages about syntax ambiguity.
wenzelm@46291
  1020
wenzelm@46506
  1021
  \item @{attribute syntax_ambiguity_limit} determines the number of
wenzelm@46291
  1022
  resulting parse trees that are shown as part of the printed message
wenzelm@46291
  1023
  in case of an ambiguity.
wenzelm@46291
  1024
wenzelm@46291
  1025
  \end{description}
wenzelm@46291
  1026
*}
wenzelm@46291
  1027
wenzelm@46291
  1028
wenzelm@48113
  1029
section {* Syntax transformations \label{sec:syntax-transformations} *}
wenzelm@48113
  1030
wenzelm@48113
  1031
text {* The inner syntax engine of Isabelle provides separate
wenzelm@48113
  1032
  mechanisms to transform parse trees either as rewrite systems on
wenzelm@48113
  1033
  first-order ASTs (\secref{sec:syn-trans}), or ML functions on ASTs
wenzelm@48113
  1034
  or syntactic @{text "\<lambda>"}-terms (\secref{sec:tr-funs}).  This works
wenzelm@48113
  1035
  both for parsing and printing, as outlined in
wenzelm@48113
  1036
  \figref{fig:parse-print}.
wenzelm@48113
  1037
wenzelm@48113
  1038
  \begin{figure}[htbp]
wenzelm@48113
  1039
  \begin{center}
wenzelm@48113
  1040
  \begin{tabular}{cl}
wenzelm@48113
  1041
  string          & \\
wenzelm@48113
  1042
  @{text "\<down>"}     & lexer + parser \\
wenzelm@48113
  1043
  parse tree      & \\
wenzelm@48113
  1044
  @{text "\<down>"}     & parse AST translation \\
wenzelm@48113
  1045
  AST             & \\
wenzelm@48113
  1046
  @{text "\<down>"}     & AST rewriting (macros) \\
wenzelm@48113
  1047
  AST             & \\
wenzelm@48113
  1048
  @{text "\<down>"}     & parse translation \\
wenzelm@48113
  1049
  --- pre-term ---    & \\
wenzelm@48113
  1050
  @{text "\<down>"}     & print translation \\
wenzelm@48113
  1051
  AST             & \\
wenzelm@48113
  1052
  @{text "\<down>"}     & AST rewriting (macros) \\
wenzelm@48113
  1053
  AST             & \\
wenzelm@48113
  1054
  @{text "\<down>"}     & print AST translation \\
wenzelm@48113
  1055
  string          &
wenzelm@48113
  1056
  \end{tabular}
wenzelm@48113
  1057
  \end{center}
wenzelm@48113
  1058
  \caption{Parsing and printing with translations}\label{fig:parse-print}
wenzelm@48113
  1059
  \end{figure}
wenzelm@48113
  1060
wenzelm@48113
  1061
  These intermediate syntax tree formats eventually lead to a pre-term
wenzelm@48113
  1062
  with all names and binding scopes resolved, but most type
wenzelm@48113
  1063
  information still missing.  Explicit type constraints might be given by
wenzelm@48113
  1064
  the user, or implicit position information by the system --- both
wenzelm@48816
  1065
  need to be passed-through carefully by syntax transformations.
wenzelm@48113
  1066
wenzelm@48113
  1067
  Pre-terms are further processed by the so-called \emph{check} and
wenzelm@48113
  1068
  \emph{unckeck} phases that are intertwined with type-inference (see
wenzelm@48113
  1069
  also \cite{isabelle-implementation}).  The latter allows to operate
wenzelm@48113
  1070
  on higher-order abstract syntax with proper binding and type
wenzelm@48113
  1071
  information already available.
wenzelm@48113
  1072
wenzelm@48113
  1073
  As a rule of thumb, anything that manipulates bindings of variables
wenzelm@48113
  1074
  or constants needs to be implemented as syntax transformation (see
wenzelm@48113
  1075
  below).  Anything else is better done via check/uncheck: a prominent
wenzelm@48113
  1076
  example application is the @{command abbreviation} concept of
wenzelm@48113
  1077
  Isabelle/Pure. *}
wenzelm@48113
  1078
wenzelm@48113
  1079
wenzelm@48115
  1080
subsection {* Abstract syntax trees \label{sec:ast} *}
wenzelm@48113
  1081
wenzelm@48114
  1082
text {* The ML datatype @{ML_type Ast.ast} explicitly represents the
wenzelm@48114
  1083
  intermediate AST format that is used for syntax rewriting
wenzelm@48114
  1084
  (\secref{sec:syn-trans}).  It is defined in ML as follows:
wenzelm@48114
  1085
  \begin{ttbox}
wenzelm@48114
  1086
  datatype ast =
wenzelm@48114
  1087
    Constant of string |
wenzelm@48114
  1088
    Variable of string |
wenzelm@48114
  1089
    Appl of ast list
wenzelm@48114
  1090
  \end{ttbox}
wenzelm@48114
  1091
wenzelm@48114
  1092
  An AST is either an atom (constant or variable) or a list of (at
wenzelm@48114
  1093
  least two) subtrees.  Occasional diagnostic output of ASTs uses
wenzelm@48114
  1094
  notation that resembles S-expression of LISP.  Constant atoms are
wenzelm@48114
  1095
  shown as quoted strings, variable atoms as non-quoted strings and
wenzelm@48114
  1096
  applications as a parenthesized list of subtrees.  For example, the
wenzelm@48114
  1097
  AST
wenzelm@48114
  1098
  @{ML [display] "Ast.Appl
wenzelm@48114
  1099
  [Ast.Constant \"_abs\", Ast.Variable \"x\", Ast.Variable \"t\"]"}
wenzelm@48114
  1100
  is pretty-printed as @{verbatim "(\"_abs\" x t)"}.  Note that
wenzelm@48114
  1101
  @{verbatim "()"} and @{verbatim "(x)"} are excluded as ASTs, because
wenzelm@48114
  1102
  they have too few subtrees.
wenzelm@48114
  1103
wenzelm@48114
  1104
  \medskip AST application is merely a pro-forma mechanism to indicate
wenzelm@48114
  1105
  certain syntactic structures.  Thus @{verbatim "(c a b)"} could mean
wenzelm@48114
  1106
  either term application or type application, depending on the
wenzelm@48114
  1107
  syntactic context.
wenzelm@48114
  1108
wenzelm@48114
  1109
  Nested application like @{verbatim "((\"_abs\" x t) u)"} is also
wenzelm@48114
  1110
  possible, but ASTs are definitely first-order: the syntax constant
wenzelm@48114
  1111
  @{verbatim "\"_abs\""} does not bind the @{verbatim x} in any way.
wenzelm@48114
  1112
  Proper bindings are introduced in later stages of the term syntax,
wenzelm@48114
  1113
  where @{verbatim "(\"_abs\" x t)"} becomes an @{ML Abs} node and
wenzelm@48114
  1114
  occurrences of @{verbatim x} in @{verbatim t} are replaced by bound
wenzelm@48114
  1115
  variables (represented as de-Bruijn indices).
wenzelm@48113
  1116
*}
wenzelm@48113
  1117
wenzelm@48113
  1118
wenzelm@48115
  1119
subsubsection {* AST constants versus variables *}
wenzelm@48114
  1120
wenzelm@48114
  1121
text {* Depending on the situation --- input syntax, output syntax,
wenzelm@48114
  1122
  translation patterns --- the distinction of atomic asts as @{ML
wenzelm@48114
  1123
  Ast.Constant} versus @{ML Ast.Variable} serves slightly different
wenzelm@48114
  1124
  purposes.
wenzelm@48114
  1125
wenzelm@48114
  1126
  Input syntax of a term such as @{text "f a b = c"} does not yet
wenzelm@48114
  1127
  indicate the scopes of atomic entities @{text "f, a, b, c"}: they
wenzelm@48114
  1128
  could be global constants or local variables, even bound ones
wenzelm@48114
  1129
  depending on the context of the term.  @{ML Ast.Variable} leaves
wenzelm@48114
  1130
  this choice still open: later syntax layers (or translation
wenzelm@48114
  1131
  functions) may capture such a variable to determine its role
wenzelm@48114
  1132
  specifically, to make it a constant, bound variable, free variable
wenzelm@48114
  1133
  etc.  In contrast, syntax translations that introduce already known
wenzelm@48114
  1134
  constants would rather do it via @{ML Ast.Constant} to prevent
wenzelm@48114
  1135
  accidental re-interpretation later on.
wenzelm@48114
  1136
wenzelm@48114
  1137
  Output syntax turns term constants into @{ML Ast.Constant} and
wenzelm@48114
  1138
  variables (free or schematic) into @{ML Ast.Variable}.  This
wenzelm@48114
  1139
  information is precise when printing fully formal @{text "\<lambda>"}-terms.
wenzelm@48114
  1140
wenzelm@48114
  1141
  In AST translation patterns (\secref{sec:syn-trans}) the system
wenzelm@48114
  1142
  guesses from the current theory context which atoms should be
wenzelm@48114
  1143
  treated as constant versus variable for the matching process.
wenzelm@48114
  1144
  Sometimes this needs to be indicated more explicitly using @{text
wenzelm@48114
  1145
  "CONST c"} inside the term language.  It is also possible to use
wenzelm@48114
  1146
  @{command syntax} declarations (without mixfix annotation) to
wenzelm@48114
  1147
  enforce that certain unqualified names are always treated as
wenzelm@48114
  1148
  constant within the syntax machinery.
wenzelm@48114
  1149
wenzelm@48114
  1150
  \medskip For ASTs that represent the language of types or sorts, the
wenzelm@48114
  1151
  situation is much simpler, since the concrete syntax already
wenzelm@48114
  1152
  distinguishes type variables from type constants (constructors).  So
wenzelm@48114
  1153
  @{text "('a, 'b) foo"} corresponds to an AST application of some
wenzelm@48114
  1154
  constant for @{text foo} and variable arguments for @{text "'a"} and
wenzelm@48114
  1155
  @{text "'b"}.  Note that the postfix application is merely a feature
wenzelm@48114
  1156
  of the concrete syntax, while in the AST the constructor occurs in
wenzelm@48114
  1157
  head position.  *}
wenzelm@48114
  1158
wenzelm@48114
  1159
wenzelm@48114
  1160
subsubsection {* Authentic syntax names *}
wenzelm@48114
  1161
wenzelm@48114
  1162
text {* Naming constant entities within ASTs is another delicate
wenzelm@48114
  1163
  issue.  Unqualified names are looked up in the name space tables in
wenzelm@48114
  1164
  the last stage of parsing, after all translations have been applied.
wenzelm@48114
  1165
  Since syntax transformations do not know about this later name
wenzelm@48114
  1166
  resolution yet, there can be surprises in boundary cases.
wenzelm@48114
  1167
wenzelm@48114
  1168
  \emph{Authentic syntax names} for @{ML Ast.Constant} avoid this
wenzelm@48114
  1169
  problem: the fully-qualified constant name with a special prefix for
wenzelm@48114
  1170
  its formal category (@{text "class"}, @{text "type"}, @{text
wenzelm@48114
  1171
  "const"}, @{text "fixed"}) represents the information faithfully
wenzelm@48114
  1172
  within the untyped AST format.  Accidental overlap with free or
wenzelm@48114
  1173
  bound variables is excluded as well.  Authentic syntax names work
wenzelm@48114
  1174
  implicitly in the following situations:
wenzelm@48114
  1175
wenzelm@48114
  1176
  \begin{itemize}
wenzelm@48114
  1177
wenzelm@48114
  1178
  \item Input of term constants (or fixed variables) that are
wenzelm@48114
  1179
  introduced by concrete syntax via @{command notation}: the
wenzelm@48114
  1180
  correspondence of a particular grammar production to some known term
wenzelm@48114
  1181
  entity is preserved.
wenzelm@48114
  1182
wenzelm@48816
  1183
  \item Input of type constants (constructors) and type classes ---
wenzelm@48114
  1184
  thanks to explicit syntactic distinction independently on the
wenzelm@48114
  1185
  context.
wenzelm@48114
  1186
wenzelm@48114
  1187
  \item Output of term constants, type constants, type classes ---
wenzelm@48114
  1188
  this information is already available from the internal term to be
wenzelm@48114
  1189
  printed.
wenzelm@48114
  1190
wenzelm@48114
  1191
  \end{itemize}
wenzelm@48114
  1192
wenzelm@48114
  1193
  In other words, syntax transformations that operate on input terms
wenzelm@48816
  1194
  written as prefix applications are difficult to make robust.
wenzelm@48816
  1195
  Luckily, this case rarely occurs in practice, because syntax forms
wenzelm@48816
  1196
  to be translated usually correspond to some bits of concrete
wenzelm@48816
  1197
  notation. *}
wenzelm@48114
  1198
wenzelm@48114
  1199
wenzelm@48113
  1200
subsection {* Raw syntax and translations \label{sec:syn-trans} *}
wenzelm@28762
  1201
wenzelm@28762
  1202
text {*
wenzelm@48117
  1203
  \begin{tabular}{rcll}
wenzelm@41229
  1204
    @{command_def "nonterminal"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1205
    @{command_def "syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1206
    @{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1207
    @{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1208
    @{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@48117
  1209
    @{attribute_def syntax_ast_trace} & : & @{text attribute} & default @{text false} \\
wenzelm@48117
  1210
    @{attribute_def syntax_ast_stats} & : & @{text attribute} & default @{text false} \\
wenzelm@48117
  1211
  \end{tabular}
wenzelm@28762
  1212
wenzelm@46292
  1213
  Unlike mixfix notation for existing formal entities
wenzelm@46292
  1214
  (\secref{sec:notation}), raw syntax declarations provide full access
wenzelm@48115
  1215
  to the priority grammar of the inner syntax, without any sanity
wenzelm@48115
  1216
  checks.  This includes additional syntactic categories (via
wenzelm@48115
  1217
  @{command nonterminal}) and free-form grammar productions (via
wenzelm@48115
  1218
  @{command syntax}).  Additional syntax translations (or macros, via
wenzelm@48115
  1219
  @{command translations}) are required to turn resulting parse trees
wenzelm@48115
  1220
  into proper representations of formal entities again.
wenzelm@46292
  1221
wenzelm@42596
  1222
  @{rail "
wenzelm@42596
  1223
    @@{command nonterminal} (@{syntax name} + @'and')
wenzelm@28762
  1224
    ;
wenzelm@46494
  1225
    (@@{command syntax} | @@{command no_syntax}) @{syntax mode}? (constdecl +)
wenzelm@28762
  1226
    ;
wenzelm@42596
  1227
    (@@{command translations} | @@{command no_translations})
wenzelm@42596
  1228
      (transpat ('==' | '=>' | '<=' | '\<rightleftharpoons>' | '\<rightharpoonup>' | '\<leftharpoondown>') transpat +)
wenzelm@28762
  1229
    ;
wenzelm@28762
  1230
wenzelm@46494
  1231
    constdecl: @{syntax name} '::' @{syntax type} @{syntax mixfix}?
wenzelm@46494
  1232
    ;
wenzelm@42596
  1233
    mode: ('(' ( @{syntax name} | @'output' | @{syntax name} @'output' ) ')')
wenzelm@28762
  1234
    ;
wenzelm@42596
  1235
    transpat: ('(' @{syntax nameref} ')')? @{syntax string}
wenzelm@42596
  1236
  "}
wenzelm@28762
  1237
wenzelm@28762
  1238
  \begin{description}
wenzelm@46282
  1239
wenzelm@41229
  1240
  \item @{command "nonterminal"}~@{text c} declares a type
wenzelm@28762
  1241
  constructor @{text c} (without arguments) to act as purely syntactic
wenzelm@28762
  1242
  type: a nonterminal symbol of the inner syntax.
wenzelm@28762
  1243
wenzelm@46292
  1244
  \item @{command "syntax"}~@{text "(mode) c :: \<sigma> (mx)"} augments the
wenzelm@46292
  1245
  priority grammar and the pretty printer table for the given print
wenzelm@46292
  1246
  mode (default @{verbatim "\"\""}). An optional keyword @{keyword_ref
wenzelm@46292
  1247
  "output"} means that only the pretty printer table is affected.
wenzelm@46292
  1248
wenzelm@46292
  1249
  Following \secref{sec:mixfix}, the mixfix annotation @{text "mx =
wenzelm@46292
  1250
  template ps q"} together with type @{text "\<sigma> = \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} and
wenzelm@46292
  1251
  specify a grammar production.  The @{text template} contains
wenzelm@46292
  1252
  delimiter tokens that surround @{text "n"} argument positions
wenzelm@46292
  1253
  (@{verbatim "_"}).  The latter correspond to nonterminal symbols
wenzelm@46292
  1254
  @{text "A\<^sub>i"} derived from the argument types @{text "\<tau>\<^sub>i"} as
wenzelm@46292
  1255
  follows:
wenzelm@46292
  1256
  \begin{itemize}
wenzelm@46292
  1257
wenzelm@46292
  1258
  \item @{text "prop"} if @{text "\<tau>\<^sub>i = prop"}
wenzelm@46292
  1259
wenzelm@46292
  1260
  \item @{text "logic"} if @{text "\<tau>\<^sub>i = (\<dots>)\<kappa>"} for logical type
wenzelm@46292
  1261
  constructor @{text "\<kappa> \<noteq> prop"}
wenzelm@46292
  1262
wenzelm@46292
  1263
  \item @{text any} if @{text "\<tau>\<^sub>i = \<alpha>"} for type variables
wenzelm@46292
  1264
wenzelm@46292
  1265
  \item @{text "\<kappa>"} if @{text "\<tau>\<^sub>i = \<kappa>"} for nonterminal @{text "\<kappa>"}
wenzelm@46292
  1266
  (syntactic type constructor)
wenzelm@46292
  1267
wenzelm@46292
  1268
  \end{itemize}
wenzelm@46292
  1269
wenzelm@46292
  1270
  Each @{text "A\<^sub>i"} is decorated by priority @{text "p\<^sub>i"} from the
wenzelm@46292
  1271
  given list @{text "ps"}; misssing priorities default to 0.
wenzelm@46292
  1272
wenzelm@46292
  1273
  The resulting nonterminal of the production is determined similarly
wenzelm@46292
  1274
  from type @{text "\<tau>"}, with priority @{text "q"} and default 1000.
wenzelm@46292
  1275
wenzelm@46292
  1276
  \medskip Parsing via this production produces parse trees @{text
wenzelm@46292
  1277
  "t\<^sub>1, \<dots>, t\<^sub>n"} for the argument slots.  The resulting parse tree is
wenzelm@46292
  1278
  composed as @{text "c t\<^sub>1 \<dots> t\<^sub>n"}, by using the syntax constant @{text
wenzelm@46292
  1279
  "c"} of the syntax declaration.
wenzelm@46292
  1280
wenzelm@46292
  1281
  Such syntactic constants are invented on the spot, without formal
wenzelm@46292
  1282
  check wrt.\ existing declarations.  It is conventional to use plain
wenzelm@46292
  1283
  identifiers prefixed by a single underscore (e.g.\ @{text
wenzelm@46292
  1284
  "_foobar"}).  Names should be chosen with care, to avoid clashes
wenzelm@48816
  1285
  with other syntax declarations.
wenzelm@46292
  1286
wenzelm@46292
  1287
  \medskip The special case of copy production is specified by @{text
wenzelm@46292
  1288
  "c = "}@{verbatim "\"\""} (empty string).  It means that the
wenzelm@46292
  1289
  resulting parse tree @{text "t"} is copied directly, without any
wenzelm@46292
  1290
  further decoration.
wenzelm@46282
  1291
wenzelm@28762
  1292
  \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
wenzelm@28762
  1293
  declarations (and translations) resulting from @{text decls}, which
wenzelm@28762
  1294
  are interpreted in the same manner as for @{command "syntax"} above.
wenzelm@46282
  1295
wenzelm@28762
  1296
  \item @{command "translations"}~@{text rules} specifies syntactic
wenzelm@48115
  1297
  translation rules (i.e.\ macros) as first-order rewrite rules on
wenzelm@48816
  1298
  ASTs (\secref{sec:ast}).  The theory context maintains two
wenzelm@48115
  1299
  independent lists translation rules: parse rules (@{verbatim "=>"}
wenzelm@48115
  1300
  or @{text "\<rightharpoonup>"}) and print rules (@{verbatim "<="} or @{text "\<leftharpoondown>"}).
wenzelm@48115
  1301
  For convenience, both can be specified simultaneously as parse~/
wenzelm@48115
  1302
  print rules (@{verbatim "=="} or @{text "\<rightleftharpoons>"}).
wenzelm@48115
  1303
wenzelm@28762
  1304
  Translation patterns may be prefixed by the syntactic category to be
wenzelm@48115
  1305
  used for parsing; the default is @{text logic} which means that
wenzelm@48115
  1306
  regular term syntax is used.  Both sides of the syntax translation
wenzelm@48115
  1307
  rule undergo parsing and parse AST translations
wenzelm@48115
  1308
  \secref{sec:tr-funs}, in order to perform some fundamental
wenzelm@48115
  1309
  normalization like @{text "\<lambda>x y. b \<leadsto> \<lambda>x. \<lambda>y. b"}, but other AST
wenzelm@48115
  1310
  translation rules are \emph{not} applied recursively here.
wenzelm@48115
  1311
wenzelm@48115
  1312
  When processing AST patterns, the inner syntax lexer runs in a
wenzelm@48115
  1313
  different mode that allows identifiers to start with underscore.
wenzelm@48115
  1314
  This accommodates the usual naming convention for auxiliary syntax
wenzelm@48115
  1315
  constants --- those that do not have a logical counter part --- by
wenzelm@48115
  1316
  allowing to specify arbitrary AST applications within the term
wenzelm@48115
  1317
  syntax, independently of the corresponding concrete syntax.
wenzelm@48115
  1318
wenzelm@48115
  1319
  Atomic ASTs are distinguished as @{ML Ast.Constant} versus @{ML
wenzelm@48115
  1320
  Ast.Variable} as follows: a qualified name or syntax constant
wenzelm@48115
  1321
  declared via @{command syntax}, or parse tree head of concrete
wenzelm@48115
  1322
  notation becomes @{ML Ast.Constant}, anything else @{ML
wenzelm@48115
  1323
  Ast.Variable}.  Note that @{text CONST} and @{text XCONST} within
wenzelm@48115
  1324
  the term language (\secref{sec:pure-grammar}) allow to enforce
wenzelm@48115
  1325
  treatment as constants.
wenzelm@48115
  1326
wenzelm@48115
  1327
  AST rewrite rules @{text "(lhs, rhs)"} need to obey the following
wenzelm@48115
  1328
  side-conditions:
wenzelm@48115
  1329
wenzelm@48115
  1330
  \begin{itemize}
wenzelm@48115
  1331
wenzelm@48115
  1332
  \item Rules must be left linear: @{text "lhs"} must not contain
wenzelm@48115
  1333
  repeated variables.\footnote{The deeper reason for this is that AST
wenzelm@48115
  1334
  equality is not well-defined: different occurrences of the ``same''
wenzelm@48115
  1335
  AST could be decorated differently by accidental type-constraints or
wenzelm@48115
  1336
  source position information, for example.}
wenzelm@48115
  1337
wenzelm@48115
  1338
  \item Every variable in @{text "rhs"} must also occur in @{text
wenzelm@48115
  1339
  "lhs"}.
wenzelm@48115
  1340
wenzelm@48115
  1341
  \end{itemize}
wenzelm@46282
  1342
wenzelm@28762
  1343
  \item @{command "no_translations"}~@{text rules} removes syntactic
wenzelm@28762
  1344
  translation rules, which are interpreted in the same manner as for
wenzelm@28762
  1345
  @{command "translations"} above.
wenzelm@28762
  1346
wenzelm@48117
  1347
  \item @{attribute syntax_ast_trace} and @{attribute
wenzelm@48117
  1348
  syntax_ast_stats} control diagnostic output in the AST normalization
wenzelm@48117
  1349
  process, when translation rules are applied to concrete input or
wenzelm@48117
  1350
  output.
wenzelm@48117
  1351
wenzelm@28762
  1352
  \end{description}
wenzelm@46293
  1353
wenzelm@46293
  1354
  Raw syntax and translations provides a slightly more low-level
wenzelm@46293
  1355
  access to the grammar and the form of resulting parse trees.  It is
wenzelm@46293
  1356
  often possible to avoid this untyped macro mechanism, and use
wenzelm@46293
  1357
  type-safe @{command abbreviation} or @{command notation} instead.
wenzelm@46293
  1358
  Some important situations where @{command syntax} and @{command
wenzelm@46293
  1359
  translations} are really need are as follows:
wenzelm@46293
  1360
wenzelm@46293
  1361
  \begin{itemize}
wenzelm@46293
  1362
wenzelm@46293
  1363
  \item Iterated replacement via recursive @{command translations}.
wenzelm@46293
  1364
  For example, consider list enumeration @{term "[a, b, c, d]"} as
wenzelm@46293
  1365
  defined in theory @{theory List} in Isabelle/HOL.
wenzelm@46293
  1366
wenzelm@46293
  1367
  \item Change of binding status of variables: anything beyond the
wenzelm@46293
  1368
  built-in @{keyword "binder"} mixfix annotation requires explicit
wenzelm@46293
  1369
  syntax translations.  For example, consider list filter
wenzelm@46293
  1370
  comprehension @{term "[x \<leftarrow> xs . P]"} as defined in theory @{theory
wenzelm@46293
  1371
  List} in Isabelle/HOL.
wenzelm@46293
  1372
wenzelm@46293
  1373
  \end{itemize}
wenzelm@28762
  1374
*}
wenzelm@28762
  1375
wenzelm@48117
  1376
subsubsection {* Applying translation rules *}
wenzelm@48117
  1377
wenzelm@48117
  1378
text {* As a term is being parsed or printed, an AST is generated as
wenzelm@48117
  1379
  an intermediate form according to \figref{fig:parse-print}.  The AST
wenzelm@48117
  1380
  is normalized by applying translation rules in the manner of a
wenzelm@48117
  1381
  first-order term rewriting system.  We first examine how a single
wenzelm@48117
  1382
  rule is applied.
wenzelm@48117
  1383
wenzelm@48117
  1384
  Let @{text "t"} be the abstract syntax tree to be normalized and
wenzelm@48117
  1385
  @{text "(lhs, rhs)"} some translation rule.  A subtree @{text "u"}
wenzelm@48117
  1386
  of @{text "t"} is called \emph{redex} if it is an instance of @{text
wenzelm@48117
  1387
  "lhs"}; in this case the pattern @{text "lhs"} is said to match the
wenzelm@48117
  1388
  object @{text "u"}.  A redex matched by @{text "lhs"} may be
wenzelm@48117
  1389
  replaced by the corresponding instance of @{text "rhs"}, thus
wenzelm@48117
  1390
  \emph{rewriting} the AST @{text "t"}.  Matching requires some notion
wenzelm@48117
  1391
  of \emph{place-holders} in rule patterns: @{ML Ast.Variable} serves
wenzelm@48117
  1392
  this purpose.
wenzelm@48117
  1393
wenzelm@48117
  1394
  More precisely, the matching of the object @{text "u"} against the
wenzelm@48117
  1395
  pattern @{text "lhs"} is performed as follows:
wenzelm@48117
  1396
wenzelm@48117
  1397
  \begin{itemize}
wenzelm@48117
  1398
wenzelm@48117
  1399
  \item Objects of the form @{ML Ast.Variable}~@{text "x"} or @{ML
wenzelm@48117
  1400
  Ast.Constant}~@{text "x"} are matched by pattern @{ML
wenzelm@48117
  1401
  Ast.Constant}~@{text "x"}.  Thus all atomic ASTs in the object are
wenzelm@48117
  1402
  treated as (potential) constants, and a successful match makes them
wenzelm@48117
  1403
  actual constants even before name space resolution (see also
wenzelm@48117
  1404
  \secref{sec:ast}).
wenzelm@48117
  1405
wenzelm@48117
  1406
  \item Object @{text "u"} is matched by pattern @{ML
wenzelm@48117
  1407
  Ast.Variable}~@{text "x"}, binding @{text "x"} to @{text "u"}.
wenzelm@48117
  1408
wenzelm@48117
  1409
  \item Object @{ML Ast.Appl}~@{text "us"} is matched by @{ML
wenzelm@48117
  1410
  Ast.Appl}~@{text "ts"} if @{text "us"} and @{text "ts"} have the
wenzelm@48117
  1411
  same length and each corresponding subtree matches.
wenzelm@48117
  1412
wenzelm@48117
  1413
  \item In every other case, matching fails.
wenzelm@48117
  1414
wenzelm@48117
  1415
  \end{itemize}
wenzelm@48117
  1416
wenzelm@48117
  1417
  A successful match yields a substitution that is applied to @{text
wenzelm@48117
  1418
  "rhs"}, generating the instance that replaces @{text "u"}.
wenzelm@48117
  1419
wenzelm@48117
  1420
  Normalizing an AST involves repeatedly applying translation rules
wenzelm@48117
  1421
  until none are applicable.  This works yoyo-like: top-down,
wenzelm@48117
  1422
  bottom-up, top-down, etc.  At each subtree position, rules are
wenzelm@48117
  1423
  chosen in order of appearance in the theory definitions.
wenzelm@48117
  1424
wenzelm@48117
  1425
  The configuration options @{attribute syntax_ast_trace} and
wenzelm@48816
  1426
  @{attribute syntax_ast_stats} might help to understand this process
wenzelm@48117
  1427
  and diagnose problems.
wenzelm@48117
  1428
wenzelm@48117
  1429
  \begin{warn}
wenzelm@48117
  1430
  If syntax translation rules work incorrectly, the output of
wenzelm@48118
  1431
  @{command_ref print_syntax} with its \emph{rules} sections reveals the
wenzelm@48117
  1432
  actual internal forms of AST pattern, without potentially confusing
wenzelm@48117
  1433
  concrete syntax.  Recall that AST constants appear as quoted strings
wenzelm@48117
  1434
  and variables without quotes.
wenzelm@48117
  1435
  \end{warn}
wenzelm@48117
  1436
wenzelm@48117
  1437
  \begin{warn}
wenzelm@48117
  1438
  If @{attribute_ref eta_contract} is set to @{text "true"}, terms
wenzelm@48117
  1439
  will be @{text "\<eta>"}-contracted \emph{before} the AST rewriter sees
wenzelm@48117
  1440
  them.  Thus some abstraction nodes needed for print rules to match
wenzelm@48117
  1441
  may vanish.  For example, @{text "Ball A (\<lambda>x. P x)"} would contract
wenzelm@48117
  1442
  to @{text "Ball A P"} and the standard print rule would fail to
wenzelm@48117
  1443
  apply.  This problem can be avoided by hand-written ML translation
wenzelm@48117
  1444
  functions (see also \secref{sec:tr-funs}), which is in fact the same
wenzelm@48117
  1445
  mechanism used in built-in @{keyword "binder"} declarations.
wenzelm@48117
  1446
  \end{warn}
wenzelm@48117
  1447
*}
wenzelm@48117
  1448
wenzelm@28762
  1449
wenzelm@48113
  1450
subsection {* Syntax translation functions \label{sec:tr-funs} *}
wenzelm@28762
  1451
wenzelm@28762
  1452
text {*
wenzelm@28762
  1453
  \begin{matharray}{rcl}
wenzelm@28762
  1454
    @{command_def "parse_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1455
    @{command_def "parse_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1456
    @{command_def "print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1457
    @{command_def "typed_print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
  1458
    @{command_def "print_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@48119
  1459
    @{ML_antiquotation_def "class_syntax"} & : & @{text ML_antiquotation} \\
wenzelm@48119
  1460
    @{ML_antiquotation_def "type_syntax"} & : & @{text ML_antiquotation} \\
wenzelm@48119
  1461
    @{ML_antiquotation_def "const_syntax"} & : & @{text ML_antiquotation} \\
wenzelm@48119
  1462
    @{ML_antiquotation_def "syntax_const"} & : & @{text ML_antiquotation} \\
wenzelm@28762
  1463
  \end{matharray}
wenzelm@28762
  1464
wenzelm@48118
  1465
  Syntax translation functions written in ML admit almost arbitrary
wenzelm@48118
  1466
  manipulations of inner syntax, at the expense of some complexity and
wenzelm@48118
  1467
  obscurity in the implementation.
wenzelm@48118
  1468
wenzelm@42596
  1469
  @{rail "
wenzelm@42596
  1470
  ( @@{command parse_ast_translation} | @@{command parse_translation} |
wenzelm@42596
  1471
    @@{command print_translation} | @@{command typed_print_translation} |
wenzelm@42596
  1472
    @@{command print_ast_translation}) ('(' @'advanced' ')')? @{syntax text}
wenzelm@48119
  1473
  ;
wenzelm@48119
  1474
  (@@{ML_antiquotation class_syntax} |
wenzelm@48119
  1475
   @@{ML_antiquotation type_syntax} |
wenzelm@48119
  1476
   @@{ML_antiquotation const_syntax} |
wenzelm@48119
  1477
   @@{ML_antiquotation syntax_const}) name
wenzelm@42596
  1478
  "}
wenzelm@28762
  1479
wenzelm@48119
  1480
  \begin{description}
wenzelm@48119
  1481
wenzelm@48119
  1482
  \item @{command parse_translation} etc. declare syntax translation
wenzelm@48119
  1483
  functions to the theory.  Any of these commands have a single
wenzelm@48119
  1484
  @{syntax text} argument that refers to an ML expression of
wenzelm@48119
  1485
  appropriate type, which are as follows by default:
wenzelm@48118
  1486
wenzelm@48118
  1487
  \medskip
wenzelm@48119
  1488
  {\footnotesize
wenzelm@48118
  1489
  \begin{tabular}{ll}
wenzelm@48118
  1490
  @{command parse_ast_translation} & : @{ML_type "(string * (Ast.ast list -> Ast.ast)) list"} \\
wenzelm@48118
  1491
  @{command parse_translation} & : @{ML_type "(string * (term list -> term)) list"} \\
wenzelm@48118
  1492
  @{command print_translation} & : @{ML_type "(string * (term list -> term)) list"} \\
wenzelm@48118
  1493
  @{command typed_print_translation} & : @{ML_type "(string * (typ -> term list -> term)) list"} \\
wenzelm@48118
  1494
  @{command print_ast_translation} & : @{ML_type "(string * (Ast.ast list -> Ast.ast)) list"} \\
wenzelm@48118
  1495
  \end{tabular}}
wenzelm@48118
  1496
  \medskip
wenzelm@28762
  1497
wenzelm@48816
  1498
  The argument list consists of @{text "(c, tr)"} pairs, where @{text
wenzelm@48816
  1499
  "c"} is the syntax name of the formal entity involved, and @{text
wenzelm@48816
  1500
  "tr"} a function that translates a syntax form @{text "c args"} into
wenzelm@48816
  1501
  @{text "tr args"}.  The ML naming convention for parse translations
wenzelm@48816
  1502
  is @{text "c_tr"} and for print translations @{text "c_tr'"}.
wenzelm@48118
  1503
wenzelm@48118
  1504
  The @{command_ref print_syntax} command displays the sets of names
wenzelm@48118
  1505
  associated with the translation functions of a theory under @{text
wenzelm@48118
  1506
  "parse_ast_translation"} etc.
wenzelm@48118
  1507
wenzelm@48118
  1508
  If the @{verbatim "("}@{keyword "advanced"}@{verbatim ")"} option is
wenzelm@48118
  1509
  given, the corresponding translation functions depend on the current
wenzelm@48118
  1510
  theory or proof context as additional argument.  This allows to
wenzelm@48118
  1511
  implement advanced syntax mechanisms, as translations functions may
wenzelm@48118
  1512
  refer to specific theory declarations or auxiliary proof data.
wenzelm@48119
  1513
wenzelm@48119
  1514
  \item @{text "@{class_syntax c}"}, @{text "@{type_syntax c}"},
wenzelm@48119
  1515
  @{text "@{const_syntax c}"} inline the authentic syntax name of the
wenzelm@48119
  1516
  given formal entities into the ML source.  This is the
wenzelm@48119
  1517
  fully-qualified logical name prefixed by a special marker to
wenzelm@48119
  1518
  indicate its kind: thus different logical name spaces are properly
wenzelm@48119
  1519
  distinguished within parse trees.
wenzelm@48119
  1520
wenzelm@48119
  1521
  \item @{text "@{const_syntax c}"} inlines the name @{text "c"} of
wenzelm@48119
  1522
  the given syntax constant, having checked that it has been declared
wenzelm@48119
  1523
  via some @{command syntax} commands within the theory context.  Note
wenzelm@48119
  1524
  that the usual naming convention makes syntax constants start with
wenzelm@48119
  1525
  underscore, to reduce the chance of accidental clashes with other
wenzelm@48119
  1526
  names occurring in parse trees (unqualified constants etc.).
wenzelm@48119
  1527
wenzelm@48119
  1528
  \end{description}
wenzelm@48118
  1529
*}
wenzelm@48118
  1530
wenzelm@48119
  1531
wenzelm@48118
  1532
subsubsection {* The translation strategy *}
wenzelm@28762
  1533
wenzelm@48816
  1534
text {* The different kinds of translation functions are invoked during
wenzelm@48118
  1535
  the transformations between parse trees, ASTs and syntactic terms
wenzelm@48118
  1536
  (cf.\ \figref{fig:parse-print}).  Whenever a combination of the form
wenzelm@48118
  1537
  @{text "c x\<^sub>1 \<dots> x\<^sub>n"} is encountered, and a translation function
wenzelm@48118
  1538
  @{text "f"} of appropriate kind is declared for @{text "c"}, the
wenzelm@48118
  1539
  result is produced by evaluation of @{text "f [x\<^sub>1, \<dots>, x\<^sub>n]"} in ML.
wenzelm@48118
  1540
wenzelm@48118
  1541
  For AST translations, the arguments @{text "x\<^sub>1, \<dots>, x\<^sub>n"} are ASTs.  A
wenzelm@48118
  1542
  combination has the form @{ML "Ast.Constant"}~@{text "c"} or @{ML
wenzelm@48118
  1543
  "Ast.Appl"}~@{text "["}@{ML Ast.Constant}~@{text "c, x\<^sub>1, \<dots>, x\<^sub>n]"}.
wenzelm@48118
  1544
  For term translations, the arguments are terms and a combination has
wenzelm@48118
  1545
  the form @{ML Const}~@{text "(c, \<tau>)"} or @{ML Const}~@{text "(c, \<tau>)
wenzelm@48118
  1546
  $ x\<^sub>1 $ \<dots> $ x\<^sub>n"}.  Terms allow more sophisticated transformations
wenzelm@48118
  1547
  than ASTs do, typically involving abstractions and bound
wenzelm@48118
  1548
  variables. \emph{Typed} print translations may even peek at the type
wenzelm@48816
  1549
  @{text "\<tau>"} of the constant they are invoked on, although that information
wenzelm@48816
  1550
  may be inaccurate.
wenzelm@48118
  1551
wenzelm@48118
  1552
  Regardless of whether they act on ASTs or terms, translation
wenzelm@48118
  1553
  functions called during the parsing process differ from those for
wenzelm@48118
  1554
  printing in their overall behaviour:
wenzelm@48118
  1555
wenzelm@48118
  1556
  \begin{description}
wenzelm@28762
  1557
wenzelm@48118
  1558
  \item [Parse translations] are applied bottom-up.  The arguments are
wenzelm@48118
  1559
  already in translated form.  The translations must not fail;
wenzelm@48118
  1560
  exceptions trigger an error message.  There may be at most one
wenzelm@48118
  1561
  function associated with any syntactic name.
wenzelm@46294
  1562
wenzelm@48118
  1563
  \item [Print translations] are applied top-down.  They are supplied
wenzelm@48118
  1564
  with arguments that are partly still in internal form.  The result
wenzelm@48118
  1565
  again undergoes translation; therefore a print translation should
wenzelm@48118
  1566
  not introduce as head the very constant that invoked it.  The
wenzelm@48118
  1567
  function may raise exception @{ML Match} to indicate failure; in
wenzelm@48118
  1568
  this event it has no effect.  Multiple functions associated with
wenzelm@48118
  1569
  some syntactic name are tried in the order of declaration in the
wenzelm@48118
  1570
  theory.
wenzelm@48118
  1571
wenzelm@48118
  1572
  \end{description}
wenzelm@48118
  1573
wenzelm@48118
  1574
  Only constant atoms --- constructor @{ML Ast.Constant} for ASTs and
wenzelm@48118
  1575
  @{ML Const} for terms --- can invoke translation functions.  This
wenzelm@48118
  1576
  means that parse translations can only be associated with parse tree
wenzelm@48118
  1577
  heads of concrete syntax, or syntactic constants introduced via
wenzelm@48118
  1578
  other translations.  For plain identifiers within the term language,
wenzelm@48118
  1579
  the status of constant versus variable is not yet know during
wenzelm@48118
  1580
  parsing.  This is in contrast to print translations, where constants
wenzelm@48118
  1581
  are explicitly known from the given term in its fully internal form.
wenzelm@28762
  1582
*}
wenzelm@28762
  1583
wenzelm@28762
  1584
end