doc-src/IsarRef/Thy/Inner_Syntax.thy
author wenzelm
Thu, 13 Nov 2008 21:48:19 +0100
changeset 28762 f5d79aeffd81
child 28763 b5e6122ff575
permissions -rw-r--r--
separate chapter "Inner syntax --- the term language";
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@28762
    15
    @{command_def "pr"}@{text "\<^sup>*"} & : & @{text "any \<rightarrow>"} \\
wenzelm@28762
    16
    @{command_def "thm"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    17
    @{command_def "term"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    18
    @{command_def "prop"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    19
    @{command_def "typ"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    20
    @{command_def "prf"}@{text "\<^sup>*"} & : & @{text "context \<rightarrow>"} \\
wenzelm@28762
    21
    @{command_def "full_prf"}@{text "\<^sup>*"} & : & @{text "context \<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@28762
    28
    'pr' modes? nat? (',' nat)?
wenzelm@28762
    29
    ;
wenzelm@28762
    30
    'thm' modes? thmrefs
wenzelm@28762
    31
    ;
wenzelm@28762
    32
    'term' modes? term
wenzelm@28762
    33
    ;
wenzelm@28762
    34
    'prop' modes? prop
wenzelm@28762
    35
    ;
wenzelm@28762
    36
    'typ' modes? type
wenzelm@28762
    37
    ;
wenzelm@28762
    38
    'prf' modes? thmrefs?
wenzelm@28762
    39
    ;
wenzelm@28762
    40
    'full\_prf' modes? thmrefs?
wenzelm@28762
    41
    ;
wenzelm@28762
    42
wenzelm@28762
    43
    modes: '(' (name + ) ')'
wenzelm@28762
    44
    ;
wenzelm@28762
    45
  \end{rail}
wenzelm@28762
    46
wenzelm@28762
    47
  \begin{description}
wenzelm@28762
    48
wenzelm@28762
    49
  \item @{command "pr"}~@{text "goals, prems"} prints the current
wenzelm@28762
    50
  proof state (if present), including the proof context, current facts
wenzelm@28762
    51
  and goals.  The optional limit arguments affect the number of goals
wenzelm@28762
    52
  and premises to be displayed, which is initially 10 for both.
wenzelm@28762
    53
  Omitting limit values leaves the current setting unchanged.
wenzelm@28762
    54
wenzelm@28762
    55
  \item @{command "thm"}~@{text "a\<^sub>1 \<dots> a\<^sub>n"} retrieves
wenzelm@28762
    56
  theorems from the current theory or proof context.  Note that any
wenzelm@28762
    57
  attributes included in the theorem specifications are applied to a
wenzelm@28762
    58
  temporary context derived from the current theory or proof; the
wenzelm@28762
    59
  result is discarded, i.e.\ attributes involved in @{text "a\<^sub>1,
wenzelm@28762
    60
  \<dots>, a\<^sub>n"} do not have any permanent effect.
wenzelm@28762
    61
wenzelm@28762
    62
  \item @{command "term"}~@{text t} and @{command "prop"}~@{text \<phi>}
wenzelm@28762
    63
  read, type-check and print terms or propositions according to the
wenzelm@28762
    64
  current theory or proof context; the inferred type of @{text t} is
wenzelm@28762
    65
  output as well.  Note that these commands are also useful in
wenzelm@28762
    66
  inspecting the current environment of term abbreviations.
wenzelm@28762
    67
wenzelm@28762
    68
  \item @{command "typ"}~@{text \<tau>} reads and prints types of the
wenzelm@28762
    69
  meta-logic according to the current theory or proof context.
wenzelm@28762
    70
wenzelm@28762
    71
  \item @{command "prf"} displays the (compact) proof term of the
wenzelm@28762
    72
  current proof state (if present), or of the given theorems. Note
wenzelm@28762
    73
  that this requires proof terms to be switched on for the current
wenzelm@28762
    74
  object logic (see the ``Proof terms'' section of the Isabelle
wenzelm@28762
    75
  reference manual for information on how to do this).
wenzelm@28762
    76
wenzelm@28762
    77
  \item @{command "full_prf"} is like @{command "prf"}, but displays
wenzelm@28762
    78
  the full proof term, i.e.\ also displays information omitted in the
wenzelm@28762
    79
  compact proof term, which is denoted by ``@{text _}'' placeholders
wenzelm@28762
    80
  there.
wenzelm@28762
    81
wenzelm@28762
    82
  \end{description}
wenzelm@28762
    83
wenzelm@28762
    84
  All of the diagnostic commands above admit a list of @{text modes}
wenzelm@28762
    85
  to be specified, which is appended to the current print mode (see
wenzelm@28762
    86
  also \cite{isabelle-ref}).  Thus the output behavior may be modified
wenzelm@28762
    87
  according particular print mode features.  For example, @{command
wenzelm@28762
    88
  "pr"}~@{text "(latex xsymbols)"} would print the current proof state
wenzelm@28762
    89
  with mathematical symbols and special characters represented in
wenzelm@28762
    90
  {\LaTeX} source, according to the Isabelle style
wenzelm@28762
    91
  \cite{isabelle-sys}.
wenzelm@28762
    92
wenzelm@28762
    93
  Note that antiquotations (cf.\ \secref{sec:antiq}) provide a more
wenzelm@28762
    94
  systematic way to include formal items into the printed text
wenzelm@28762
    95
  document.
wenzelm@28762
    96
*}
wenzelm@28762
    97
wenzelm@28762
    98
wenzelm@28762
    99
section {* Mixfix annotations *}
wenzelm@28762
   100
wenzelm@28762
   101
text {* Mixfix annotations specify concrete \emph{inner syntax} of
wenzelm@28762
   102
  Isabelle types and terms.  Some commands such as @{command "types"}
wenzelm@28762
   103
  (see \secref{sec:types-pure}) admit infixes only, while @{command
wenzelm@28762
   104
  "consts"} (see \secref{sec:consts}) and @{command "syntax"} (see
wenzelm@28762
   105
  \secref{sec:syn-trans}) support the full range of general mixfixes
wenzelm@28762
   106
  and binders.
wenzelm@28762
   107
wenzelm@28762
   108
  \indexouternonterm{infix}\indexouternonterm{mixfix}\indexouternonterm{structmixfix}
wenzelm@28762
   109
  \begin{rail}
wenzelm@28762
   110
    infix: '(' ('infix' | 'infixl' | 'infixr') string nat ')'
wenzelm@28762
   111
    ;
wenzelm@28762
   112
    mixfix: infix | '(' string prios? nat? ')' | '(' 'binder' string prios? nat ')'
wenzelm@28762
   113
    ;
wenzelm@28762
   114
    structmixfix: mixfix | '(' 'structure' ')'
wenzelm@28762
   115
    ;
wenzelm@28762
   116
wenzelm@28762
   117
    prios: '[' (nat + ',') ']'
wenzelm@28762
   118
    ;
wenzelm@28762
   119
  \end{rail}
wenzelm@28762
   120
wenzelm@28762
   121
  Here the \railtok{string} specifications refer to the actual mixfix
wenzelm@28762
   122
  template, which may include literal text, spacing, blocks, and
wenzelm@28762
   123
  arguments (denoted by ``@{text _}''); the special symbol
wenzelm@28762
   124
  ``@{verbatim "\<index>"}'' (printed as ``@{text "\<index>"}'') represents an index
wenzelm@28762
   125
  argument that specifies an implicit structure reference (see also
wenzelm@28762
   126
  \secref{sec:locale}).  Infix and binder declarations provide common
wenzelm@28762
   127
  abbreviations for particular mixfix declarations.  So in practice,
wenzelm@28762
   128
  mixfix templates mostly degenerate to literal text for concrete
wenzelm@28762
   129
  syntax, such as ``@{verbatim "++"}'' for an infix symbol.
wenzelm@28762
   130
wenzelm@28762
   131
  \medskip In full generality, mixfix declarations work as follows.
wenzelm@28762
   132
  Suppose a constant @{text "c :: \<tau>\<^sub>1 \<Rightarrow> \<dots> \<tau>\<^sub>n \<Rightarrow> \<tau>"} is
wenzelm@28762
   133
  annotated by @{text "(mixfix [p\<^sub>1, \<dots>, p\<^sub>n] p)"}, where @{text
wenzelm@28762
   134
  "mixfix"} is a string @{text "d\<^sub>0 _ d\<^sub>1 _ \<dots> _ d\<^sub>n"} consisting of
wenzelm@28762
   135
  delimiters that surround argument positions as indicated by
wenzelm@28762
   136
  underscores.
wenzelm@28762
   137
wenzelm@28762
   138
  Altogether this determines a production for a context-free priority
wenzelm@28762
   139
  grammar, where for each argument @{text "i"} the syntactic category
wenzelm@28762
   140
  is determined by @{text "\<tau>\<^sub>i"} (with priority @{text "p\<^sub>i"}), and
wenzelm@28762
   141
  the result category is determined from @{text "\<tau>"} (with
wenzelm@28762
   142
  priority @{text "p"}).  Priority specifications are optional, with
wenzelm@28762
   143
  default 0 for arguments and 1000 for the result.
wenzelm@28762
   144
wenzelm@28762
   145
  Since @{text "\<tau>"} may be again a function type, the constant
wenzelm@28762
   146
  type scheme may have more argument positions than the mixfix
wenzelm@28762
   147
  pattern.  Printing a nested application @{text "c t\<^sub>1 \<dots> t\<^sub>m"} for
wenzelm@28762
   148
  @{text "m > n"} works by attaching concrete notation only to the
wenzelm@28762
   149
  innermost part, essentially by printing @{text "(c t\<^sub>1 \<dots> t\<^sub>n) \<dots> t\<^sub>m"}
wenzelm@28762
   150
  instead.  If a term has fewer arguments than specified in the mixfix
wenzelm@28762
   151
  template, the concrete syntax is ignored.
wenzelm@28762
   152
wenzelm@28762
   153
  \medskip A mixfix template may also contain additional directives
wenzelm@28762
   154
  for pretty printing, notably spaces, blocks, and breaks.  The
wenzelm@28762
   155
  general template format is a sequence over any of the following
wenzelm@28762
   156
  entities.
wenzelm@28762
   157
wenzelm@28762
   158
  \begin{itemize}
wenzelm@28762
   159
wenzelm@28762
   160
  \item @{text "\<^bold>d"} is a delimiter, namely a non-empty
wenzelm@28762
   161
  sequence of characters other than the special characters @{text "'"}
wenzelm@28762
   162
  (single quote), @{text "_"} (underscore), @{text "\<index>"} (index
wenzelm@28762
   163
  symbol), @{text "/"} (slash), @{text "("} and @{text ")"}
wenzelm@28762
   164
  (parentheses).
wenzelm@28762
   165
wenzelm@28762
   166
  A single quote escapes the special meaning of these meta-characters,
wenzelm@28762
   167
  producing a literal version of the following character, unless that
wenzelm@28762
   168
  is a blank.  A single quote followed by a blank separates
wenzelm@28762
   169
  delimiters, without affecting printing, but input tokens may have
wenzelm@28762
   170
  additional white space here.
wenzelm@28762
   171
wenzelm@28762
   172
  \item @{text "_"} is an argument position, which stands for a
wenzelm@28762
   173
  certain syntactic category in the underlying grammar.
wenzelm@28762
   174
wenzelm@28762
   175
  \item @{text "\<index>"} is an indexed argument position; this is
wenzelm@28762
   176
  the place where implicit structure arguments can be attached.
wenzelm@28762
   177
wenzelm@28762
   178
  \item @{text "\<^bold>s"} is a non-empty sequence of spaces for
wenzelm@28762
   179
  printing.  This and the following specifications do not affect
wenzelm@28762
   180
  parsing at all.
wenzelm@28762
   181
wenzelm@28762
   182
  \item @{text "(\<^bold>n"} opens a pretty printing block.  The
wenzelm@28762
   183
  optional number specifies how much indentation to add when a line
wenzelm@28762
   184
  break occurs within the block.  If the parenthesis is not followed
wenzelm@28762
   185
  by digits, the indentation defaults to 0.  A block specified via
wenzelm@28762
   186
  @{text "(00"} is unbreakable.
wenzelm@28762
   187
wenzelm@28762
   188
  \item @{text ")"} closes a pretty printing block.
wenzelm@28762
   189
wenzelm@28762
   190
  \item @{text "//"} forces a line break.
wenzelm@28762
   191
wenzelm@28762
   192
  \item @{text "/\<^bold>s"} allows a line break.  Here @{text
wenzelm@28762
   193
  "\<^bold>s"} stands for the string of spaces (zero or more) right
wenzelm@28762
   194
  after the slash.  These spaces are printed if the break is
wenzelm@28762
   195
  \emph{not} taken.
wenzelm@28762
   196
wenzelm@28762
   197
  \end{itemize}
wenzelm@28762
   198
wenzelm@28762
   199
  For example, the template @{text "(_ +/ _)"} specifies an infix
wenzelm@28762
   200
  operator.  There are two argument positions; the delimiter @{text
wenzelm@28762
   201
  "+"} is preceded by a space and followed by a space or line break;
wenzelm@28762
   202
  the entire phrase is a pretty printing block.
wenzelm@28762
   203
wenzelm@28762
   204
  The general idea of pretty printing with blocks and breaks is also
wenzelm@28762
   205
  described in \cite{paulson-ml2}.
wenzelm@28762
   206
*}
wenzelm@28762
   207
wenzelm@28762
   208
wenzelm@28762
   209
section {* Additional term notation *}
wenzelm@28762
   210
wenzelm@28762
   211
text {*
wenzelm@28762
   212
  \begin{matharray}{rcll}
wenzelm@28762
   213
    @{command_def "notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   214
    @{command_def "no_notation"} & : & @{text "local_theory \<rightarrow> local_theory"} \\
wenzelm@28762
   215
  \end{matharray}
wenzelm@28762
   216
wenzelm@28762
   217
  \begin{rail}
wenzelm@28762
   218
    ('notation' | 'no\_notation') target? mode? (nameref structmixfix + 'and')
wenzelm@28762
   219
    ;
wenzelm@28762
   220
  \end{rail}
wenzelm@28762
   221
wenzelm@28762
   222
  \begin{description}
wenzelm@28762
   223
wenzelm@28762
   224
  \item @{command "notation"}~@{text "c (mx)"} associates mixfix
wenzelm@28762
   225
  syntax with an existing constant or fixed variable.  This is a
wenzelm@28762
   226
  robust interface to the underlying @{command "syntax"} primitive
wenzelm@28762
   227
  (\secref{sec:syn-trans}).  Type declaration and internal syntactic
wenzelm@28762
   228
  representation of the given entity is retrieved from the context.
wenzelm@28762
   229
  
wenzelm@28762
   230
  \item @{command "no_notation"} is similar to @{command "notation"},
wenzelm@28762
   231
  but removes the specified syntax annotation from the present
wenzelm@28762
   232
  context.
wenzelm@28762
   233
wenzelm@28762
   234
  \end{description}
wenzelm@28762
   235
*}
wenzelm@28762
   236
wenzelm@28762
   237
section {* Syntax and translations \label{sec:syn-trans} *}
wenzelm@28762
   238
wenzelm@28762
   239
text {*
wenzelm@28762
   240
  \begin{matharray}{rcl}
wenzelm@28762
   241
    @{command_def "nonterminals"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   242
    @{command_def "syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   243
    @{command_def "no_syntax"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   244
    @{command_def "translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   245
    @{command_def "no_translations"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   246
  \end{matharray}
wenzelm@28762
   247
wenzelm@28762
   248
  \begin{rail}
wenzelm@28762
   249
    'nonterminals' (name +)
wenzelm@28762
   250
    ;
wenzelm@28762
   251
    ('syntax' | 'no\_syntax') mode? (constdecl +)
wenzelm@28762
   252
    ;
wenzelm@28762
   253
    ('translations' | 'no\_translations') (transpat ('==' | '=>' | '<=' | rightleftharpoons | rightharpoonup | leftharpoondown) transpat +)
wenzelm@28762
   254
    ;
wenzelm@28762
   255
wenzelm@28762
   256
    mode: ('(' ( name | 'output' | name 'output' ) ')')
wenzelm@28762
   257
    ;
wenzelm@28762
   258
    transpat: ('(' nameref ')')? string
wenzelm@28762
   259
    ;
wenzelm@28762
   260
  \end{rail}
wenzelm@28762
   261
wenzelm@28762
   262
  \begin{description}
wenzelm@28762
   263
  
wenzelm@28762
   264
  \item @{command "nonterminals"}~@{text c} declares a type
wenzelm@28762
   265
  constructor @{text c} (without arguments) to act as purely syntactic
wenzelm@28762
   266
  type: a nonterminal symbol of the inner syntax.
wenzelm@28762
   267
wenzelm@28762
   268
  \item @{command "syntax"}~@{text "(mode) decls"} is similar to
wenzelm@28762
   269
  @{command "consts"}~@{text decls}, except that the actual logical
wenzelm@28762
   270
  signature extension is omitted.  Thus the context free grammar of
wenzelm@28762
   271
  Isabelle's inner syntax may be augmented in arbitrary ways,
wenzelm@28762
   272
  independently of the logic.  The @{text mode} argument refers to the
wenzelm@28762
   273
  print mode that the grammar rules belong; unless the @{keyword_ref
wenzelm@28762
   274
  "output"} indicator is given, all productions are added both to the
wenzelm@28762
   275
  input and output grammar.
wenzelm@28762
   276
  
wenzelm@28762
   277
  \item @{command "no_syntax"}~@{text "(mode) decls"} removes grammar
wenzelm@28762
   278
  declarations (and translations) resulting from @{text decls}, which
wenzelm@28762
   279
  are interpreted in the same manner as for @{command "syntax"} above.
wenzelm@28762
   280
  
wenzelm@28762
   281
  \item @{command "translations"}~@{text rules} specifies syntactic
wenzelm@28762
   282
  translation rules (i.e.\ macros): parse~/ print rules (@{text "\<rightleftharpoons>"}),
wenzelm@28762
   283
  parse rules (@{text "\<rightharpoonup>"}), or print rules (@{text "\<leftharpoondown>"}).
wenzelm@28762
   284
  Translation patterns may be prefixed by the syntactic category to be
wenzelm@28762
   285
  used for parsing; the default is @{text logic}.
wenzelm@28762
   286
  
wenzelm@28762
   287
  \item @{command "no_translations"}~@{text rules} removes syntactic
wenzelm@28762
   288
  translation rules, which are interpreted in the same manner as for
wenzelm@28762
   289
  @{command "translations"} above.
wenzelm@28762
   290
wenzelm@28762
   291
  \end{description}
wenzelm@28762
   292
*}
wenzelm@28762
   293
wenzelm@28762
   294
wenzelm@28762
   295
section {* Syntax translation functions *}
wenzelm@28762
   296
wenzelm@28762
   297
text {*
wenzelm@28762
   298
  \begin{matharray}{rcl}
wenzelm@28762
   299
    @{command_def "parse_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   300
    @{command_def "parse_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   301
    @{command_def "print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   302
    @{command_def "typed_print_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   303
    @{command_def "print_ast_translation"} & : & @{text "theory \<rightarrow> theory"} \\
wenzelm@28762
   304
  \end{matharray}
wenzelm@28762
   305
wenzelm@28762
   306
  \begin{rail}
wenzelm@28762
   307
  ( 'parse\_ast\_translation' | 'parse\_translation' | 'print\_translation' |
wenzelm@28762
   308
    'typed\_print\_translation' | 'print\_ast\_translation' ) ('(advanced)')? text
wenzelm@28762
   309
  ;
wenzelm@28762
   310
  \end{rail}
wenzelm@28762
   311
wenzelm@28762
   312
  Syntax translation functions written in ML admit almost arbitrary
wenzelm@28762
   313
  manipulations of Isabelle's inner syntax.  Any of the above commands
wenzelm@28762
   314
  have a single \railqtok{text} argument that refers to an ML
wenzelm@28762
   315
  expression of appropriate type, which are as follows by default:
wenzelm@28762
   316
wenzelm@28762
   317
%FIXME proper antiquotations
wenzelm@28762
   318
\begin{ttbox}
wenzelm@28762
   319
val parse_ast_translation   : (string * (ast list -> ast)) list
wenzelm@28762
   320
val parse_translation       : (string * (term list -> term)) list
wenzelm@28762
   321
val print_translation       : (string * (term list -> term)) list
wenzelm@28762
   322
val typed_print_translation :
wenzelm@28762
   323
  (string * (bool -> typ -> term list -> term)) list
wenzelm@28762
   324
val print_ast_translation   : (string * (ast list -> ast)) list
wenzelm@28762
   325
\end{ttbox}
wenzelm@28762
   326
wenzelm@28762
   327
  If the @{text "(advanced)"} option is given, the corresponding
wenzelm@28762
   328
  translation functions may depend on the current theory or proof
wenzelm@28762
   329
  context.  This allows to implement advanced syntax mechanisms, as
wenzelm@28762
   330
  translations functions may refer to specific theory declarations or
wenzelm@28762
   331
  auxiliary proof data.
wenzelm@28762
   332
wenzelm@28762
   333
  See also \cite[\S8]{isabelle-ref} for more information on the
wenzelm@28762
   334
  general concept of syntax transformations in Isabelle.
wenzelm@28762
   335
wenzelm@28762
   336
%FIXME proper antiquotations
wenzelm@28762
   337
\begin{ttbox}
wenzelm@28762
   338
val parse_ast_translation:
wenzelm@28762
   339
  (string * (Proof.context -> ast list -> ast)) list
wenzelm@28762
   340
val parse_translation:
wenzelm@28762
   341
  (string * (Proof.context -> term list -> term)) list
wenzelm@28762
   342
val print_translation:
wenzelm@28762
   343
  (string * (Proof.context -> term list -> term)) list
wenzelm@28762
   344
val typed_print_translation:
wenzelm@28762
   345
  (string * (Proof.context -> bool -> typ -> term list -> term)) list
wenzelm@28762
   346
val print_ast_translation:
wenzelm@28762
   347
  (string * (Proof.context -> ast list -> ast)) list
wenzelm@28762
   348
\end{ttbox}
wenzelm@28762
   349
*}
wenzelm@28762
   350
wenzelm@28762
   351
end