src/Doc/Implementation/Syntax.thy
author wenzelm
Tue, 20 Oct 2015 23:53:40 +0200
changeset 61493 0debd22f0c0e
parent 61477 e467ae7aa808
child 61572 ddb3ac3fef45
permissions -rw-r--r--
isabelle update_cartouches -t;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57344
wenzelm
parents: 56420
diff changeset
     1
(*:wrap=hard:maxLineLen=78:*)
wenzelm
parents: 56420
diff changeset
     2
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     3
theory Syntax
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     4
imports Base
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     5
begin
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     6
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
     7
chapter \<open>Concrete syntax and type-checking\<close>
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     8
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
     9
text \<open>Pure \<open>\<lambda>\<close>-calculus as introduced in \chref{ch:logic} is
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    10
  an adequate foundation for logical languages --- in the tradition of
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    11
  \<^emph>\<open>higher-order abstract syntax\<close> --- but end-users require
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    12
  additional means for reading and printing of terms and types.  This
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    13
  important add-on outside the logical core is called \<^emph>\<open>inner
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    14
  syntax\<close> in Isabelle jargon, as opposed to the \<^emph>\<open>outer syntax\<close> of
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
    15
  the theory and proof language @{cite "isabelle-isar-ref"}.
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    16
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
    17
  For example, according to @{cite church40} quantifiers are represented as
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    18
  higher-order constants \<open>All :: ('a \<Rightarrow> bool) \<Rightarrow> bool\<close> such that \<open>All (\<lambda>x::'a. B x)\<close> faithfully represents the idea that is displayed in
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    19
  Isabelle as \<open>\<forall>x::'a. B x\<close> via @{keyword "binder"} notation.
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
    20
  Moreover, type-inference in the style of Hindley-Milner @{cite hindleymilner}
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    21
  (and extensions) enables users to write \<open>\<forall>x. B x\<close> concisely, when
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    22
  the type \<open>'a\<close> is already clear from the
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    23
  context.\footnote{Type-inference taken to the extreme can easily confuse
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    24
  users. Beginners often stumble over unexpectedly general types inferred by
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    25
  the system.}
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    26
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
    27
  \<^medskip>
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    28
  The main inner syntax operations are \<^emph>\<open>read\<close> for
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    29
  parsing together with type-checking, and \<^emph>\<open>pretty\<close> for formatted
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    30
  output.  See also \secref{sec:read-print}.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    31
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    32
  Furthermore, the input and output syntax layers are sub-divided into
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    33
  separate phases for \<^emph>\<open>concrete syntax\<close> versus \<^emph>\<open>abstract
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
    34
  syntax\<close>, see also \secref{sec:parse-unparse} and
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    35
  \secref{sec:term-check}, respectively.  This results in the
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    36
  following decomposition of the main operations:
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    37
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    38
  \<^item> \<open>read = parse; check\<close>
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    39
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    40
  \<^item> \<open>pretty = uncheck; unparse\<close>
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    41
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    42
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    43
  For example, some specification package might thus intercept syntax
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    44
  processing at a well-defined stage after \<open>parse\<close>, to a augment the
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    45
  resulting pre-term before full type-reconstruction is performed by \<open>check\<close>. Note that the formal status of bound variables, versus free
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    46
  variables, versus constants must not be changed between these phases.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    47
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
    48
  \<^medskip>
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    49
  In general, \<open>check\<close> and \<open>uncheck\<close> operate
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    50
  simultaneously on a list of terms. This is particular important for
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    51
  type-checking, to reconstruct types for several terms of the same context
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    52
  and scope. In contrast, \<open>parse\<close> and \<open>unparse\<close> operate separately
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    53
  on single terms.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    54
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    55
  There are analogous operations to read and print types, with the same
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    56
  sub-division into phases.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    57
\<close>
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    58
30272
2d612824e642 regenerated document;
wenzelm
parents: 30270
diff changeset
    59
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    60
section \<open>Reading and pretty printing \label{sec:read-print}\<close>
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
    61
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    62
text \<open>
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    63
  Read and print operations are roughly dual to each other, such that for the
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    64
  user \<open>s' = pretty (read s)\<close> looks similar to the original source
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    65
  text \<open>s\<close>, but the details depend on many side-conditions. There are
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    66
  also explicit options to control the removal of type information in the
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    67
  output. The default configuration routinely looses information, so \<open>t' = read (pretty t)\<close> might fail, or produce a differently typed term, or
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    68
  a completely different term in the face of syntactic overloading.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    69
\<close>
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
    70
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
    71
text %mlref \<open>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    72
  \begin{mldecls}
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    73
  @{index_ML Syntax.read_typs: "Proof.context -> string list -> typ list"} \\
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    74
  @{index_ML Syntax.read_terms: "Proof.context -> string list -> term list"} \\
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    75
  @{index_ML Syntax.read_props: "Proof.context -> string list -> term list"} \\[0.5ex]
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    76
  @{index_ML Syntax.read_typ: "Proof.context -> string -> typ"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    77
  @{index_ML Syntax.read_term: "Proof.context -> string -> term"} \\
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    78
  @{index_ML Syntax.read_prop: "Proof.context -> string -> term"} \\[0.5ex]
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    79
  @{index_ML Syntax.pretty_typ: "Proof.context -> typ -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    80
  @{index_ML Syntax.pretty_term: "Proof.context -> term -> Pretty.T"} \\
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    81
  @{index_ML Syntax.string_of_typ: "Proof.context -> typ -> string"} \\
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    82
  @{index_ML Syntax.string_of_term: "Proof.context -> term -> string"} \\
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    83
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    84
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    85
  \<^descr> @{ML Syntax.read_typs}~\<open>ctxt strs\<close> parses and checks a
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    86
  simultaneous list of source strings as types of the logic.
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    87
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    88
  \<^descr> @{ML Syntax.read_terms}~\<open>ctxt strs\<close> parses and checks a
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    89
  simultaneous list of source strings as terms of the logic.
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
    90
  Type-reconstruction puts all parsed terms into the same scope: types of
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
    91
  free variables ultimately need to coincide.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    92
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    93
  If particular type-constraints are required for some of the arguments, the
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
    94
  read operations needs to be split into its parse and check phases. Then it
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
    95
  is possible to use @{ML Type.constraint} on the intermediate pre-terms
57846
wenzelm
parents: 57496
diff changeset
    96
  (\secref{sec:term-check}).
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
    97
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
    98
  \<^descr> @{ML Syntax.read_props}~\<open>ctxt strs\<close> parses and checks a
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
    99
  simultaneous list of source strings as terms of the logic, with an implicit
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   100
  type-constraint for each argument to enforce type @{typ prop}; this also
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   101
  affects the inner syntax for parsing. The remaining type-reconstruction
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   102
  works as for @{ML Syntax.read_terms}.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   103
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   104
  \<^descr> @{ML Syntax.read_typ}, @{ML Syntax.read_term}, @{ML Syntax.read_prop}
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   105
  are like the simultaneous versions, but operate on a single argument only.
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   106
  This convenient shorthand is adequate in situations where a single item in
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   107
  its own scope is processed. Do not use @{ML "map o Syntax.read_term"} where
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   108
  @{ML Syntax.read_terms} is actually intended!
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   109
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   110
  \<^descr> @{ML Syntax.pretty_typ}~\<open>ctxt T\<close> and @{ML
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   111
  Syntax.pretty_term}~\<open>ctxt t\<close> uncheck and pretty-print the given type
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   112
  or term, respectively. Although the uncheck phase acts on a simultaneous
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   113
  list as well, this is rarely used in practice, so only the singleton case is
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   114
  provided as combined pretty operation. There is no distinction of term vs.\
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   115
  proposition.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   116
61439
2bf52eec4e8a more symbols;
wenzelm
parents: 61416
diff changeset
   117
  \<^descr> @{ML Syntax.string_of_typ} and @{ML Syntax.string_of_term} are
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   118
  convenient compositions of @{ML Syntax.pretty_typ} and @{ML
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   119
  Syntax.pretty_term} with @{ML Pretty.string_of} for output. The result may
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   120
  be concatenated with other strings, as long as there is no further
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   121
  formatting and line-breaking involved.
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   122
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   123
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   124
  @{ML Syntax.read_term}, @{ML Syntax.read_prop}, and @{ML
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   125
  Syntax.string_of_term} are the most important operations in practice.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   126
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
   127
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
   128
  Note that the string values that are passed in and out are
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   129
  annotated by the system, to carry further markup that is relevant for the
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   130
  Prover IDE @{cite "isabelle-jedit"}. User code should neither compose its
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   131
  own input strings, nor try to analyze the output strings. Conceptually this
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   132
  is an abstract datatype, encoded as concrete string for historical reasons.
57345
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   133
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   134
  The standard way to provide the required position markup for input works via
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   135
  the outer syntax parser wrapper @{ML Parse.inner_syntax}, which is already
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   136
  part of @{ML Parse.typ}, @{ML Parse.term}, @{ML Parse.prop}. So a string
8a9639888639 more on syntax phases;
wenzelm
parents: 57344
diff changeset
   137
  obtained from one of the latter may be directly passed to the corresponding
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   138
  read operation: this yields PIDE markup of the input and precise positions
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   139
  for warning and error messages.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   140
\<close>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   141
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   142
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   143
section \<open>Parsing and unparsing \label{sec:parse-unparse}\<close>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   144
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   145
text \<open>
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   146
  Parsing and unparsing converts between actual source text and a certain
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   147
  \<^emph>\<open>pre-term\<close> format, where all bindings and scopes are already resolved
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   148
  faithfully. Thus the names of free variables or constants are determined in
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   149
  the sense of the logical context, but type information might be still
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   150
  missing. Pre-terms support an explicit language of \<^emph>\<open>type constraints\<close>
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   151
  that may be augmented by user code to guide the later \<^emph>\<open>check\<close> phase.
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   152
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   153
  Actual parsing is based on traditional lexical analysis and Earley parsing
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   154
  for arbitrary context-free grammars. The user can specify the grammar
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   155
  declaratively via mixfix annotations. Moreover, there are \<^emph>\<open>syntax
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   156
  translations\<close> that can be augmented by the user, either declaratively via
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   157
  @{command translations} or programmatically via @{command
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   158
  parse_translation}, @{command print_translation} @{cite
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   159
  "isabelle-isar-ref"}. The final scope-resolution is performed by the system,
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   160
  according to name spaces for types, term variables and constants determined
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   161
  by the context.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   162
\<close>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   163
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   164
text %mlref \<open>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   165
  \begin{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   166
  @{index_ML Syntax.parse_typ: "Proof.context -> string -> typ"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   167
  @{index_ML Syntax.parse_term: "Proof.context -> string -> term"} \\
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   168
  @{index_ML Syntax.parse_prop: "Proof.context -> string -> term"} \\[0.5ex]
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   169
  @{index_ML Syntax.unparse_typ: "Proof.context -> typ -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   170
  @{index_ML Syntax.unparse_term: "Proof.context -> term -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   171
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   172
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   173
  \<^descr> @{ML Syntax.parse_typ}~\<open>ctxt str\<close> parses a source string as
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   174
  pre-type that is ready to be used with subsequent check operations.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   175
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   176
  \<^descr> @{ML Syntax.parse_term}~\<open>ctxt str\<close> parses a source string as
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   177
  pre-term that is ready to be used with subsequent check operations.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   178
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   179
  \<^descr> @{ML Syntax.parse_prop}~\<open>ctxt str\<close> parses a source string as
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   180
  pre-term that is ready to be used with subsequent check operations. The
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   181
  inner syntax category is @{typ prop} and a suitable type-constraint is
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   182
  included to ensure that this information is observed in subsequent type
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   183
  reconstruction.
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   184
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   185
  \<^descr> @{ML Syntax.unparse_typ}~\<open>ctxt T\<close> unparses a type after
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   186
  uncheck operations, to turn it into a pretty tree.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   187
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   188
  \<^descr> @{ML Syntax.unparse_term}~\<open>ctxt T\<close> unparses a term after
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   189
  uncheck operations, to turn it into a pretty tree. There is no distinction
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   190
  for propositions here.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   191
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   192
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   193
  These operations always operate on a single item; use the combinator @{ML
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   194
  map} to apply them to a list.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   195
\<close>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   196
39852
9c977f899ebf tuned chapter arrangement;
wenzelm
parents: 34924
diff changeset
   197
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   198
section \<open>Checking and unchecking \label{sec:term-check}\<close>
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
   199
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   200
text \<open>These operations define the transition from pre-terms and
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   201
  fully-annotated terms in the sense of the logical core
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   202
  (\chref{ch:logic}).
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   203
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   204
  The \<^emph>\<open>check\<close> phase is meant to subsume a variety of mechanisms
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   205
  in the manner of ``type-inference'' or ``type-reconstruction'' or
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   206
  ``type-improvement'', not just type-checking in the narrow sense.
61477
e467ae7aa808 more control symbols;
wenzelm
parents: 61458
diff changeset
   207
  The \<^emph>\<open>uncheck\<close> phase is roughly dual, it prunes type-information
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   208
  before pretty printing.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   209
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   210
  A typical add-on for the check/uncheck syntax layer is the @{command
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   211
  abbreviation} mechanism @{cite "isabelle-isar-ref"}. Here the user specifies
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   212
  syntactic definitions that are managed by the system as polymorphic \<open>let\<close> bindings. These are expanded during the \<open>check\<close> phase, and
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   213
  contracted during the \<open>uncheck\<close> phase, without affecting the
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   214
  type-assignment of the given terms.
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   215
61416
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
   216
  \<^medskip>
b9a3324e4e62 more symbols;
wenzelm
parents: 58618
diff changeset
   217
  The precise meaning of type checking depends on the context ---
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   218
  additional check/uncheck modules might be defined in user space.
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   219
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   220
  For example, the @{command class} command defines a context where
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   221
  \<open>check\<close> treats certain type instances of overloaded
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   222
  constants according to the ``dictionary construction'' of its
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   223
  logical foundation.  This involves ``type improvement''
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   224
  (specialization of slightly too general types) and replacement by
58555
7975676c08c0 prefer @{cite} antiquotation;
wenzelm
parents: 57846
diff changeset
   225
  certain locale parameters.  See also @{cite "Haftmann-Wenzel:2009"}.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   226
\<close>
39852
9c977f899ebf tuned chapter arrangement;
wenzelm
parents: 34924
diff changeset
   227
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   228
text %mlref \<open>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   229
  \begin{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   230
  @{index_ML Syntax.check_typs: "Proof.context -> typ list -> typ list"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   231
  @{index_ML Syntax.check_terms: "Proof.context -> term list -> term list"} \\
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   232
  @{index_ML Syntax.check_props: "Proof.context -> term list -> term list"} \\[0.5ex]
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   233
  @{index_ML Syntax.uncheck_typs: "Proof.context -> typ list -> typ list"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   234
  @{index_ML Syntax.uncheck_terms: "Proof.context -> term list -> term list"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   235
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   236
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   237
  \<^descr> @{ML Syntax.check_typs}~\<open>ctxt Ts\<close> checks a simultaneous list
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   238
  of pre-types as types of the logic.  Typically, this involves normalization
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   239
  of type synonyms.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   240
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   241
  \<^descr> @{ML Syntax.check_terms}~\<open>ctxt ts\<close> checks a simultaneous list
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   242
  of pre-terms as terms of the logic. Typically, this involves type-inference
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   243
  and normalization term abbreviations. The types within the given terms are
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   244
  treated in the same way as for @{ML Syntax.check_typs}.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   245
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   246
  Applications sometimes need to check several types and terms together. The
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   247
  standard approach uses @{ML Logic.mk_type} to embed the language of types
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   248
  into that of terms; all arguments are appended into one list of terms that
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   249
  is checked; afterwards the type arguments are recovered with @{ML
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   250
  Logic.dest_type}.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   251
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   252
  \<^descr> @{ML Syntax.check_props}~\<open>ctxt ts\<close> checks a simultaneous list
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   253
  of pre-terms as terms of the logic, such that all terms are constrained by
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   254
  type @{typ prop}. The remaining check operation works as @{ML
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   255
  Syntax.check_terms} above.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   256
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   257
  \<^descr> @{ML Syntax.uncheck_typs}~\<open>ctxt Ts\<close> unchecks a simultaneous
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   258
  list of types of the logic, in preparation of pretty printing.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   259
61493
0debd22f0c0e isabelle update_cartouches -t;
wenzelm
parents: 61477
diff changeset
   260
  \<^descr> @{ML Syntax.uncheck_terms}~\<open>ctxt ts\<close> unchecks a simultaneous
57346
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   261
  list of terms of the logic, in preparation of pretty printing. There is no
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   262
  distinction for propositions here.
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   263
1d6d44a0583f more on syntax phases;
wenzelm
parents: 57345
diff changeset
   264
57496
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   265
  These operations always operate simultaneously on a list; use the combinator
94596c573b38 misc tuning;
wenzelm
parents: 57346
diff changeset
   266
  @{ML singleton} to apply them to a single item.
58618
782f0b662cae more cartouches;
wenzelm
parents: 58555
diff changeset
   267
\<close>
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   268
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
   269
end