doc-src/IsarRef/Thy/Inner_Syntax.thy
author wenzelm
Thu, 13 Nov 2008 21:52:09 +0100
changeset 28766 accab7594b8e
parent 28765 da8f6f4a74be
child 28767 f09ceb800d00
permissions -rw-r--r--
misc tuning and rearrangement of section "Printing logical entities";
wenzelm@28762
     1
(* $Id$ *)
wenzelm@28762
     2
wenzelm@28762
     3
theory Inner_Syntax
wenzelm@28762
     4
imports Main
wenzelm@28762
     5
begin
wenzelm@28762
     6
wenzelm@28762
     7
chapter {* Inner syntax --- the term language *}
wenzelm@28762
     8
wenzelm@28762
     9
section {* Printing logical entities *}
wenzelm@28762
    10
wenzelm@28762
    11
subsection {* Diagnostic commands *}
wenzelm@28762
    12
wenzelm@28762
    13
text {*
wenzelm@28762
    14
  \begin{matharray}{rcl}
wenzelm@28766
    15
    @{command_def "typ"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    16
    @{command_def "term"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    17
    @{command_def "prop"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28766
    18
    @{command_def "thm"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    19
    @{command_def "prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    20
    @{command_def "full_prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28766
    21
    @{command_def "pr"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28762
    22
  \end{matharray}
wenzelm@28762
    23
wenzelm@28762
    24
  These diagnostic commands assist interactive development by printing
wenzelm@28762
    25
  internal logical entities in a human-readable fashion.
wenzelm@28762
    26
wenzelm@28762
    27
  \begin{rail}
wenzelm@28766
    28
    'typ' modes? type
wenzelm@28762
    29
    ;
wenzelm@28762
    30
    'term' modes? term
wenzelm@28762
    31
    ;
wenzelm@28762
    32
    'prop' modes? prop
wenzelm@28762
    33
    ;
wenzelm@28766
    34
    'thm' modes? thmrefs
wenzelm@28762
    35
    ;
wenzelm@28766
    36
    ( 'prf' | 'full\_prf' ) modes? thmrefs?
wenzelm@28762
    37
    ;
wenzelm@28766
    38
    'pr' modes? nat? (',' nat)?
wenzelm@28762
    39
    ;
wenzelm@28762
    40
wenzelm@28762
    41
    modes: '(' (name + ) ')'
wenzelm@28762
    42
    ;
wenzelm@28762
    43
  \end{rail}
wenzelm@28762
    44
wenzelm@28762
    45
  \begin{description}
wenzelm@28762
    46
wenzelm@28766
    47
  \item @{command "typ"}~@{text \<tau>} reads and prints types of the
wenzelm@28766
    48
  meta-logic according to the current theory or proof context.
wenzelm@28766
    49
wenzelm@28766
    50
  \item @{command "term"}~@{text t} and @{command "prop"}~@{text \<phi>}
wenzelm@28766
    51
  read, type-check and print terms or propositions according to the
wenzelm@28766
    52
  current theory or proof context; the inferred type of @{text t} is
wenzelm@28766
    53
  output as well.  Note that these commands are also useful in
wenzelm@28766
    54
  inspecting the current environment of term abbreviations.
wenzelm@28762
    55
wenzelm@28762
    56
  \item @{command "thm"}~@{text "a\<^sub>1 \<dots> a\<^sub>n"} retrieves
wenzelm@28762
    57
  theorems from the current theory or proof context.  Note that any
wenzelm@28762
    58
  attributes included in the theorem specifications are applied to a
wenzelm@28762
    59
  temporary context derived from the current theory or proof; the
wenzelm@28762
    60
  result is discarded, i.e.\ attributes involved in @{text "a\<^sub>1,
wenzelm@28762
    61
  \<dots>, a\<^sub>n"} do not have any permanent effect.
wenzelm@28762
    62
wenzelm@28762
    63
  \item @{command "prf"} displays the (compact) proof term of the
wenzelm@28762
    64
  current proof state (if present), or of the given theorems. Note
wenzelm@28762
    65
  that this requires proof terms to be switched on for the current
wenzelm@28762
    66
  object logic (see the ``Proof terms'' section of the Isabelle
wenzelm@28762
    67
  reference manual for information on how to do this).
wenzelm@28762
    68
wenzelm@28762
    69
  \item @{command "full_prf"} is like @{command "prf"}, but displays
wenzelm@28762
    70
  the full proof term, i.e.\ also displays information omitted in the
wenzelm@28762
    71
  compact proof term, which is denoted by ``@{text _}'' placeholders
wenzelm@28762
    72
  there.
wenzelm@28762
    73
wenzelm@28766
    74
  \item @{command "pr"}~@{text "goals, prems"} prints the current
wenzelm@28766
    75
  proof state (if present), including the proof context, current facts
wenzelm@28766
    76
  and goals.  The optional limit arguments affect the number of goals
wenzelm@28766
    77
  and premises to be displayed, which is initially 10 for both.
wenzelm@28766
    78
  Omitting limit values leaves the current setting unchanged.
wenzelm@28766
    79
wenzelm@28762
    80
  \end{description}
wenzelm@28762
    81
wenzelm@28762
    82
  All of the diagnostic commands above admit a list of @{text modes}
wenzelm@28762
    83
  to be specified, which is appended to the current print mode (see
wenzelm@28762
    84
  also \cite{isabelle-ref}).  Thus the output behavior may be modified
wenzelm@28762
    85
  according particular print mode features.  For example, @{command
wenzelm@28762
    86
  "pr"}~@{text "(latex xsymbols)"} would print the current proof state
wenzelm@28762
    87
  with mathematical symbols and special characters represented in
wenzelm@28762
    88
  {\LaTeX} source, according to the Isabelle style
wenzelm@28762
    89
  \cite{isabelle-sys}.
wenzelm@28762
    90
wenzelm@28762
    91
  Note that antiquotations (cf.\ \secref{sec:antiq}) provide a more
wenzelm@28762
    92
  systematic way to include formal items into the printed text
wenzelm@28762
    93
  document.
wenzelm@28762
    94
*}
wenzelm@28762
    95
wenzelm@28762
    96
wenzelm@28763
    97
subsection {* Details of printed content *}
wenzelm@28763
    98
wenzelm@28763
    99
text {*
wenzelm@28763
   100
  \begin{mldecls} 
wenzelm@28763
   101
    @{index_ML show_types: "bool ref"} & default @{ML false} \\
wenzelm@28763
   102
    @{index_ML show_sorts: "bool ref"} & default @{ML false} \\
wenzelm@28763
   103
    @{index_ML show_consts: "bool ref"} & default @{ML false} \\
wenzelm@28763
   104
    @{index_ML long_names: "bool ref"} & default @{ML false} \\
wenzelm@28763
   105
    @{index_ML short_names: "bool ref"} & default @{ML false} \\
wenzelm@28763
   106
    @{index_ML unique_names: "bool ref"} & default @{ML true} \\
wenzelm@28763
   107
    @{index_ML show_brackets: "bool ref"} & default @{ML false} \\
wenzelm@28765
   108
    @{index_ML eta_contract: "bool ref"} & default @{ML true} \\
wenzelm@28763
   109
    @{index_ML goals_limit: "int ref"} & default @{ML 10} \\
wenzelm@28763
   110
    @{index_ML Proof.show_main_goal: "bool ref"} & default @{ML false} \\
wenzelm@28763
   111
    @{index_ML show_hyps: "bool ref"} & default @{ML false} \\
wenzelm@28763
   112
    @{index_ML show_tags: "bool ref"} & default @{ML false} \\
wenzelm@28765
   113
    @{index_ML show_question_marks: "bool ref"} & default @{ML true} \\
wenzelm@28763
   114
  \end{mldecls}
wenzelm@28763
   115
wenzelm@28763
   116
  These global ML variables control the detail of information that is
wenzelm@28763
   117
  displayed for types, terms, theorems, goals etc.
wenzelm@28763
   118
wenzelm@28765
   119
  In interactive sessions, the user interface usually manages these
wenzelm@28765
   120
  global parameters of the Isabelle process, even with some concept of
wenzelm@28765
   121
  persistence.  Nonetheless it is occasionally useful to manipulate ML
wenzelm@28765
   122
  variables directly, e.g.\ using @{command "ML_val"} or @{command
wenzelm@28765
   123
  "ML_command"}.
wenzelm@28765
   124
wenzelm@28765
   125
  Batch-mode logic sessions may be configured by putting appropriate
wenzelm@28765
   126
  ML text directly into the @{verbatim ROOT.ML} file.
wenzelm@28765
   127
wenzelm@28763
   128
  \begin{description}
wenzelm@28763
   129
wenzelm@28763
   130
  \item @{ML show_types} and @{ML show_sorts} control printing of type
wenzelm@28763
   131
  constraints for term variables, and sort constraints for type
wenzelm@28763
   132
  variables.  By default, neither of these are shown in output.  If
wenzelm@28763
   133
  @{ML show_sorts} is set to @{ML true}, types are always shown as
wenzelm@28763
   134
  well.
wenzelm@28763
   135
wenzelm@28763
   136
  Note that displaying types and sorts may explain why a polymorphic
wenzelm@28763
   137
  inference rule fails to resolve with some goal, or why a rewrite
wenzelm@28763
   138
  rule does not apply as expected.
wenzelm@28763
   139
wenzelm@28763
   140
  \item @{ML show_consts} controls printing of types of constants when
wenzelm@28765
   141
  displaying a goal state.
wenzelm@28765
   142
wenzelm@28765
   143
  Note that the output can be enormous, because polymorphic constants
wenzelm@28765
   144
  often occur at several different type instances.
wenzelm@28763
   145
wenzelm@28763
   146
  \item @{ML long_names}, @{ML short_names}, and @{ML unique_names}
wenzelm@28765
   147
  control the way of printing fully qualified internal names in
wenzelm@28765
   148
  external form.  See also \secref{sec:antiq} for the document
wenzelm@28765
   149
  antiquotation options of the same names.
wenzelm@28763
   150
wenzelm@28765
   151
  \item @{ML show_brackets} controls bracketing in pretty printed
wenzelm@28765
   152
  output.  If set to @{ML true}, all sub-expressions of the pretty
wenzelm@28765
   153
  printing tree will be parenthesized, even if this produces malformed
wenzelm@28765
   154
  term syntax!  This crude way of showing the internal structure of
wenzelm@28765
   155
  pretty printed entities may occasionally help to diagnose problems
wenzelm@28765
   156
  with operator priorities, for example.
wenzelm@28763
   157
wenzelm@28763
   158
  \item @{ML eta_contract} controls @{text "\<eta>"}-contracted printing of
wenzelm@28763
   159
  terms.
wenzelm@28763
   160
wenzelm@28763
   161
  The @{text \<eta>}-contraction law asserts @{prop "(\<lambda>x. f x) \<equiv> f"},
wenzelm@28763
   162
  provided @{text x} is not free in @{text f}.  It asserts
wenzelm@28763
   163
  \emph{extensionality} of functions: @{prop "f \<equiv> g"} if @{prop "f x \<equiv>
wenzelm@28763
   164
  g x"} for all @{text x}.  Higher-order unification frequently puts
wenzelm@28763
   165
  terms into a fully @{text \<eta>}-expanded form.  For example, if @{text
wenzelm@28763
   166
  F} has type @{text "(\<tau> \<Rightarrow> \<tau>) \<Rightarrow> \<tau>"} then its expanded form is @{term
wenzelm@28763
   167
  "\<lambda>h. F (\<lambda>x. h x)"}.
wenzelm@28763
   168
wenzelm@28763
   169
  Setting @{ML eta_contract} makes Isabelle perform @{text
wenzelm@28763
   170
  \<eta>}-contractions before printing, so that @{term "\<lambda>h. F (\<lambda>x. h x)"}
wenzelm@28763
   171
  appears simply as @{text F}.
wenzelm@28763
   172
wenzelm@28763
   173
  Note that the distinction between a term and its @{text \<eta>}-expanded
wenzelm@28765
   174
  form occasionally matters.  While higher-order resolution and
wenzelm@28765
   175
  rewriting operate modulo @{text "\<alpha>\<beta>\<eta>"}-conversion, some other tools
wenzelm@28765
   176
  might look at terms more discretely.
wenzelm@28763
   177
wenzelm@28763
   178
  \item @{ML goals_limit} controls the maximum number of subgoals to
wenzelm@28765
   179
  be shown in goal output.
wenzelm@28763
   180
wenzelm@28763
   181
  \item @{ML Proof.show_main_goal} controls whether the main result to
wenzelm@28763
   182
  be proven should be displayed.  This information might be relevant
wenzelm@28765
   183
  for schematic goals, to inspect the current claim that has been
wenzelm@28765
   184
  synthesized so far.
wenzelm@28763
   185
wenzelm@28763
   186
  \item @{ML show_hyps} controls printing of implicit hypotheses of
wenzelm@28763
   187
  local facts.  Normally, only those hypotheses are displayed that are
wenzelm@28763
   188
  \emph{not} covered by the assumptions of the current context: this
wenzelm@28763
   189
  situation indicates a fault in some tool being used.
wenzelm@28763
   190
wenzelm@28765
   191
  By setting @{ML show_hyps} to @{ML true}, output of \emph{all}
wenzelm@28765
   192
  hypotheses can be enforced, which is occasionally useful for
wenzelm@28765
   193
  diagnostic purposes.
wenzelm@28763
   194
wenzelm@28763
   195
  \item @{ML show_tags} controls printing of extra annotations within
wenzelm@28765
   196
  theorems, such as internal position information, or the case names
wenzelm@28765
   197
  being attached by the attribute @{attribute case_names}.
wenzelm@28765
   198
wenzelm@28765
   199
  Note that the @{attribute tagged} and @{attribute untagged}
wenzelm@28765
   200
  attributes provide low-level access to the collection of tags
wenzelm@28765
   201
  associated with a theorem.
wenzelm@28765
   202
wenzelm@28765
   203
  \item @{ML show_question_marks} controls printing of question marks
wenzelm@28765
   204
  for schematic variables, such as @{text ?x}.  Only the leading
wenzelm@28765
   205
  question mark is affected, the remaining text is unchanged
wenzelm@28765
   206
  (including proper markup for schematic variables that might be
wenzelm@28765
   207
  relevant for user interfaces).
wenzelm@28765
   208
wenzelm@28765
   209
  \end{description}
wenzelm@28765
   210
*}
wenzelm@28765
   211
wenzelm@28765
   212
wenzelm@28765
   213
subsection {* Printing limits *}
wenzelm@28765
   214
wenzelm@28765
   215
text {*
wenzelm@28765
   216
  \begin{mldecls}
wenzelm@28765
   217
    @{index_ML Pretty.setdepth: "int -> unit"} \\
wenzelm@28765
   218
    @{index_ML Pretty.setmargin: "int -> unit"} \\
wenzelm@28765
   219
    @{index_ML print_depth: "int -> unit"} \\
wenzelm@28765
   220
  \end{mldecls}
wenzelm@28765
   221
wenzelm@28765
   222
  These ML functions set limits for pretty printed text.
wenzelm@28765
   223
wenzelm@28765
   224
  \begin{description}
wenzelm@28765
   225
wenzelm@28765
   226
  \item @{ML Pretty.setdepth}~@{text d} tells the pretty printer to
wenzelm@28765
   227
  limit the printing depth to @{text d}.  This affects the display of
wenzelm@28765
   228
  types, terms, theorems etc.  The default value is 0, which permits
wenzelm@28765
   229
  printing to an arbitrary depth.  Other useful values for @{text d}
wenzelm@28765
   230
  are 10 and 20.
wenzelm@28765
   231
wenzelm@28765
   232
  \item @{ML Pretty.setmargin}~@{text m} tells the pretty printer to
wenzelm@28765
   233
  assume a right margin (page width) of @{text m}.  The initial margin
wenzelm@28765
   234
  is 76, but user interfaces might adapt the margin automatically when
wenzelm@28765
   235
  resizing windows.
wenzelm@28765
   236
wenzelm@28765
   237
  \item @{ML print_depth}~@{text n} limits the printing depth of the
wenzelm@28765
   238
  ML toplevel pretty printer; the precise effect depends on the ML
wenzelm@28765
   239
  compiler and run-time system.  Typically @{text n} should be less
wenzelm@28765
   240
  than 10.  Bigger values such as 100--1000 are useful for debugging.
wenzelm@28763
   241
wenzelm@28763
   242
  \end{description}
wenzelm@28763
   243
*}
wenzelm@28763
   244
wenzelm@28763
   245
wenzelm@28762
   246
section {* Mixfix annotations *}
wenzelm@28762
   247
wenzelm@28762
   248
text {* Mixfix annotations specify concrete \emph{inner syntax} of
wenzelm@28762
   249
  Isabelle types and terms.  Some commands such as @{command "types"}
wenzelm@28762
   250
  (see \secref{sec:types-pure}) admit infixes only, while @{command
wenzelm@28762
   251
  "consts"} (see \secref{sec:consts}) and @{command "syntax"} (see
wenzelm@28762
   252
  \secref{sec:syn-trans}) support the full range of general mixfixes
wenzelm@28762
   253
  and binders.
wenzelm@28762
   254
wenzelm@28762
   255
  \indexouternonterm{infix}\indexouternonterm{mixfix}\indexouternonterm{structmixfix}
wenzelm@28762
   256
  \begin{rail}
wenzelm@28762
   257
    infix: '(' ('infix' | 'infixl' | 'infixr') string nat ')'
wenzelm@28762
   258
    ;
wenzelm@28762
   259
    mixfix: infix | '(' string prios? nat? ')' | '(' 'binder' string prios? nat ')'
wenzelm@28762
   260
    ;
wenzelm@28762
   261
    structmixfix: mixfix | '(' 'structure' ')'
wenzelm@28762
   262
    ;
wenzelm@28762
   263
wenzelm@28762
   264
    prios: '[' (nat + ',') ']'
wenzelm@28762
   265
    ;
wenzelm@28762
   266
  \end{rail}
wenzelm@28762
   267
wenzelm@28762
   268
  Here the \railtok{string} specifications refer to the actual mixfix
wenzelm@28762
   269
  template, which may include literal text, spacing, blocks, and
wenzelm@28762
   270
  arguments (denoted by ``@{text _}''); the special symbol
wenzelm@28762
   271
  ``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'') represents an index
wenzelm@28762
   272
  argument that specifies an implicit structure reference (see also
wenzelm@28762
   273
  \secref{sec:locale}).  Infix and binder declarations provide common
wenzelm@28762
   274
  abbreviations for particular mixfix declarations.  So in practice,
wenzelm@28762
   275
  mixfix templates mostly degenerate to literal text for concrete
wenzelm@28762
   276
  syntax, such as ``@{verbatim "++"}'' for an infix symbol.
wenzelm@28762
   277
wenzelm@28762
   278
  \medskip In full generality, mixfix declarations work as follows.
wenzelm@28762
   279
  Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is
wenzelm@28762
   280
  annotated by @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text
wenzelm@28762
   281
  "mixfix"} is a string @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of
wenzelm@28762
   282
  delimiters that surround argument positions as indicated by
wenzelm@28762
   283
  underscores.
wenzelm@28762
   284
wenzelm@28762
   285
  Altogether this determines a production for a context-free priority
wenzelm@28762
   286
  grammar, where for each argument @{text "i"} the syntactic category
wenzelm@28762
   287
  is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and
wenzelm@28762
   288
  the result category is determined from @{text "\<tau>"} (with
wenzelm@28762
   289
  priority @{text "p"}).  Priority specifications are optional, with
wenzelm@28762
   290
  default 0 for arguments and 1000 for the result.
wenzelm@28762
   291
wenzelm@28762
   292
  Since @{text "\<tau>"} may be again a function type, the constant
wenzelm@28762
   293
  type scheme may have more argument positions than the mixfix
wenzelm@28762
   294
  pattern.  Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for
wenzelm@28762
   295
  @{text "m > n"} works by attaching concrete notation only to the
wenzelm@28762
   296
  innermost part, essentially by printing @{text "(c t\<^sub>1 \<dots> t\<^sub>n) \<dots> t\<^sub>m"}
wenzelm@28762
   297
  instead.  If a term has fewer arguments than specified in the mixfix
wenzelm@28762
   298
  template, the concrete syntax is ignored.
wenzelm@28762
   299
wenzelm@28762
   300
  \medskip A mixfix template may also contain additional directives
wenzelm@28762
   301
  for pretty printing, notably spaces, blocks, and breaks.  The
wenzelm@28762
   302
  general template format is a sequence over any of the following
wenzelm@28762
   303
  entities.
wenzelm@28762
   304
wenzelm@28762
   305
  \begin{itemize}
wenzelm@28762
   306
wenzelm@28762
   307
  \item @{text "\<^bold>d"} is a delimiter, namely a non-empty
wenzelm@28762
   308
  sequence of characters other than the special characters @{text "'"}
wenzelm@28762
   309
  (single quote), @{text "_"} (underscore), @{text "\<index>"} (index
wenzelm@28762
   310
  symbol), @{text "/"} (slash), @{text "("} and @{text ")"}
wenzelm@28762
   311
  (parentheses).
wenzelm@28762
   312
wenzelm@28762
   313
  A single quote escapes the special meaning of these meta-characters,
wenzelm@28762
   314
  producing a literal version of the following character, unless that
wenzelm@28762
   315
  is a blank.  A single quote followed by a blank separates
wenzelm@28762
   316
  delimiters, without affecting printing, but input tokens may have
wenzelm@28762
   317
  additional white space here.
wenzelm@28762
   318
wenzelm@28762
   319
  \item @{text "_"} is an argument position, which stands for a
wenzelm@28762
   320
  certain syntactic category in the underlying grammar.
wenzelm@28762
   321
wenzelm@28762
   322
  \item @{text "\<index>"} is an indexed argument position; this is
wenzelm@28762
   323
  the place where implicit structure arguments can be attached.
wenzelm@28762
   324
wenzelm@28762
   325
  \item @{text "\<^bold>s"} is a non-empty sequence of spaces for
wenzelm@28762
   326
  printing.  This and the following specifications do not affect
wenzelm@28762
   327
  parsing at all.
wenzelm@28762
   328
wenzelm@28762
   329
  \item @{text "(\<^bold>n"} opens a pretty printing block.  The
wenzelm@28762
   330
  optional number specifies how much indentation to add when a line
wenzelm@28762
   331
  break occurs within the block.  If the parenthesis is not followed
wenzelm@28762
   332
  by digits, the indentation defaults to 0.  A block specified via
wenzelm@28762
   333
  @{text "(00"} is unbreakable.
wenzelm@28762
   334
wenzelm@28762
   335
  \item @{text ")"} closes a pretty printing block.
wenzelm@28762
   336
wenzelm@28762
   337
  \item @{text "//"} forces a line break.
wenzelm@28762
   338
wenzelm@28762
   339
  \item @{text "/\<^bold>s"} allows a line break.  Here @{text
wenzelm@28762
   340
  "\<^bold>s"} stands for the string of spaces (zero or more) right
wenzelm@28762
   341
  after the slash.  These spaces are printed if the break is
wenzelm@28762
   342
  \emph{not} taken.
wenzelm@28762
   343
wenzelm@28762
   344
  \end{itemize}
wenzelm@28762
   345
wenzelm@28762
   346
  For example, the template @{text "(_ +/ _)"} specifies an infix
wenzelm@28762
   347
  operator.  There are two argument positions; the delimiter @{text
wenzelm@28762
   348
  "+"} is preceded by a space and followed by a space or line break;
wenzelm@28762
   349
  the entire phrase is a pretty printing block.
wenzelm@28762
   350
wenzelm@28762
   351
  The general idea of pretty printing with blocks and breaks is also
wenzelm@28762
   352
  described in \cite{paulson-ml2}.
wenzelm@28762
   353
*}
wenzelm@28762
   354
wenzelm@28762
   355
wenzelm@28766
   356
section {* Explicit term notation *}
wenzelm@28762
   357
wenzelm@28762
   358
text {*
wenzelm@28762
   359
  \begin{matharray}{rcll}
wenzelm@28762
   360
    @{command_def "notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   361
    @{command_def "no_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   362
  \end{matharray}
wenzelm@28762
   363
wenzelm@28762
   364
  \begin{rail}
wenzelm@28762
   365
    ('notation' | 'no\_notation') target? mode? (nameref structmixfix + 'and')
wenzelm@28762
   366
    ;
wenzelm@28762
   367
  \end{rail}
wenzelm@28762
   368
wenzelm@28762
   369
  \begin{description}
wenzelm@28762
   370
wenzelm@28762
   371
  \item @{command "notation"}~@{text "c (mx)"} associates mixfix
wenzelm@28762
   372
  syntax with an existing constant or fixed variable.  This is a
wenzelm@28762
   373
  robust interface to the underlying @{command "syntax"} primitive
wenzelm@28762
   374
  (\secref{sec:syn-trans}).  Type declaration and internal syntactic
wenzelm@28762
   375
  representation of the given entity is retrieved from the context.
wenzelm@28762
   376
  
wenzelm@28762
   377
  \item @{command "no_notation"} is similar to @{command "notation"},
wenzelm@28762
   378
  but removes the specified syntax annotation from the present
wenzelm@28762
   379
  context.
wenzelm@28762
   380
wenzelm@28762
   381
  \end{description}
wenzelm@28762
   382
*}
wenzelm@28762
   383
wenzelm@28762
   384
section {* Syntax and translations \label{sec:syn-trans} *}
wenzelm@28762
   385
wenzelm@28762
   386
text {*
wenzelm@28762
   387
  \begin{matharray}{rcl}
wenzelm@28762
   388
    @{command_def "nonterminals"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   389
    @{command_def "syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   390
    @{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   391
    @{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   392
    @{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   393
  \end{matharray}
wenzelm@28762
   394
wenzelm@28762
   395
  \begin{rail}
wenzelm@28762
   396
    'nonterminals' (name +)
wenzelm@28762
   397
    ;
wenzelm@28762
   398
    ('syntax' | 'no\_syntax') mode? (constdecl +)
wenzelm@28762
   399
    ;
wenzelm@28762
   400
    ('translations' | 'no\_translations') (transpat ('==' | '=>' | '<=' | rightleftharpoons | rightharpoonup | leftharpoondown) transpat +)
wenzelm@28762
   401
    ;
wenzelm@28762
   402
wenzelm@28762
   403
    mode: ('(' ( name | 'output' | name 'output' ) ')')
wenzelm@28762
   404
    ;
wenzelm@28762
   405
    transpat: ('(' nameref ')')? string
wenzelm@28762
   406
    ;
wenzelm@28762
   407
  \end{rail}
wenzelm@28762
   408
wenzelm@28762
   409
  \begin{description}
wenzelm@28762
   410
  
wenzelm@28762
   411
  \item @{command "nonterminals"}~@{text c} declares a type
wenzelm@28762
   412
  constructor @{text c} (without arguments) to act as purely syntactic
wenzelm@28762
   413
  type: a nonterminal symbol of the inner syntax.
wenzelm@28762
   414
wenzelm@28762
   415
  \item @{command "syntax"}~@{text "(mode) decls"} is similar to
wenzelm@28762
   416
  @{command "consts"}~@{text decls}, except that the actual logical
wenzelm@28762
   417
  signature extension is omitted.  Thus the context free grammar of
wenzelm@28762
   418
  Isabelle's inner syntax may be augmented in arbitrary ways,
wenzelm@28762
   419
  independently of the logic.  The @{text mode} argument refers to the
wenzelm@28762
   420
  print mode that the grammar rules belong; unless the @{keyword_ref
wenzelm@28762
   421
  "output"} indicator is given, all productions are added both to the
wenzelm@28762
   422
  input and output grammar.
wenzelm@28762
   423
  
wenzelm@28762
   424
  \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
wenzelm@28762
   425
  declarations (and translations) resulting from @{text decls}, which
wenzelm@28762
   426
  are interpreted in the same manner as for @{command "syntax"} above.
wenzelm@28762
   427
  
wenzelm@28762
   428
  \item @{command "translations"}~@{text rules} specifies syntactic
wenzelm@28762
   429
  translation rules (i.e.\ macros): parse~/ print rules (@{text "\<rightleftharpoons>"}),
wenzelm@28762
   430
  parse rules (@{text "\<rightharpoonup>"}), or print rules (@{text "\<leftharpoondown>"}).
wenzelm@28762
   431
  Translation patterns may be prefixed by the syntactic category to be
wenzelm@28762
   432
  used for parsing; the default is @{text logic}.
wenzelm@28762
   433
  
wenzelm@28762
   434
  \item @{command "no_translations"}~@{text rules} removes syntactic
wenzelm@28762
   435
  translation rules, which are interpreted in the same manner as for
wenzelm@28762
   436
  @{command "translations"} above.
wenzelm@28762
   437
wenzelm@28762
   438
  \end{description}
wenzelm@28762
   439
*}
wenzelm@28762
   440
wenzelm@28762
   441
wenzelm@28762
   442
section {* Syntax translation functions *}
wenzelm@28762
   443
wenzelm@28762
   444
text {*
wenzelm@28762
   445
  \begin{matharray}{rcl}
wenzelm@28762
   446
    @{command_def "parse_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   447
    @{command_def "parse_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   448
    @{command_def "print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   449
    @{command_def "typed_print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   450
    @{command_def "print_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   451
  \end{matharray}
wenzelm@28762
   452
wenzelm@28762
   453
  \begin{rail}
wenzelm@28762
   454
  ( 'parse\_ast\_translation' | 'parse\_translation' | 'print\_translation' |
wenzelm@28762
   455
    'typed\_print\_translation' | 'print\_ast\_translation' ) ('(advanced)')? text
wenzelm@28762
   456
  ;
wenzelm@28762
   457
  \end{rail}
wenzelm@28762
   458
wenzelm@28762
   459
  Syntax translation functions written in ML admit almost arbitrary
wenzelm@28762
   460
  manipulations of Isabelle's inner syntax.  Any of the above commands
wenzelm@28762
   461
  have a single \railqtok{text} argument that refers to an ML
wenzelm@28762
   462
  expression of appropriate type, which are as follows by default:
wenzelm@28762
   463
wenzelm@28762
   464
%FIXME proper antiquotations
wenzelm@28762
   465
\begin{ttbox}
wenzelm@28762
   466
val parse_ast_translation   : (string * (ast list -> ast)) list
wenzelm@28762
   467
val parse_translation       : (string * (term list -> term)) list
wenzelm@28762
   468
val print_translation       : (string * (term list -> term)) list
wenzelm@28762
   469
val typed_print_translation :
wenzelm@28762
   470
  (string * (bool -> typ -> term list -> term)) list
wenzelm@28762
   471
val print_ast_translation   : (string * (ast list -> ast)) list
wenzelm@28762
   472
\end{ttbox}
wenzelm@28762
   473
wenzelm@28762
   474
  If the @{text "(advanced)"} option is given, the corresponding
wenzelm@28762
   475
  translation functions may depend on the current theory or proof
wenzelm@28762
   476
  context.  This allows to implement advanced syntax mechanisms, as
wenzelm@28762
   477
  translations functions may refer to specific theory declarations or
wenzelm@28762
   478
  auxiliary proof data.
wenzelm@28762
   479
wenzelm@28762
   480
  See also \cite[\S8]{isabelle-ref} for more information on the
wenzelm@28762
   481
  general concept of syntax transformations in Isabelle.
wenzelm@28762
   482
wenzelm@28762
   483
%FIXME proper antiquotations
wenzelm@28762
   484
\begin{ttbox}
wenzelm@28762
   485
val parse_ast_translation:
wenzelm@28762
   486
  (string * (Proof.context -> ast list -> ast)) list
wenzelm@28762
   487
val parse_translation:
wenzelm@28762
   488
  (string * (Proof.context -> term list -> term)) list
wenzelm@28762
   489
val print_translation:
wenzelm@28762
   490
  (string * (Proof.context -> term list -> term)) list
wenzelm@28762
   491
val typed_print_translation:
wenzelm@28762
   492
  (string * (Proof.context -> bool -> typ -> term list -> term)) list
wenzelm@28762
   493
val print_ast_translation:
wenzelm@28762
   494
  (string * (Proof.context -> ast list -> ast)) list
wenzelm@28762
   495
\end{ttbox}
wenzelm@28762
   496
*}
wenzelm@28762
   497
wenzelm@28762
   498
end