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