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