src/Doc/IsarImplementation/Syntax.thy
author wenzelm
Sat, 05 Apr 2014 15:03:40 +0200
changeset 56421 1ffd7eaa778b
parent 52422 93f3f9a2ae91
permissions -rw-r--r--
updated to jedit_build-20140405: Code2HTML.jar, CommonControls.jar, Console.jar, kappalayout.jar, Navigator.jar, SideKick.jar, doc with jEdit manuals (ant dist-manuals);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     1
theory Syntax
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     2
imports Base
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     3
begin
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     4
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
     5
chapter {* Concrete syntax and type-checking *}
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
     6
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
     7
text {* Pure @{text "\<lambda>"}-calculus as introduced in \chref{ch:logic} is
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
     8
  an adequate foundation for logical languages --- in the tradition of
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
     9
  \emph{higher-order abstract syntax} --- but end-users require
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    10
  additional means for reading and printing of terms and types.  This
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    11
  important add-on outside the logical core is called \emph{inner
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    12
  syntax} in Isabelle jargon, as opposed to the \emph{outer syntax} of
46484
50fca9d09528 updated refs;
wenzelm
parents: 45260
diff changeset
    13
  the theory and proof language (cf.\ \cite{isabelle-isar-ref}).
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    14
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    15
  For example, according to \cite{church40} quantifiers are
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    16
  represented as higher-order constants @{text "All :: ('a \<Rightarrow> bool) \<Rightarrow>
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    17
  bool"} such that @{text "All (\<lambda>x::'a. B x)"} faithfully represents
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    18
  the idea that is displayed as @{text "\<forall>x::'a. B x"} via @{keyword
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    19
  "binder"} notation.  Moreover, type-inference in the style of
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    20
  Hindley-Milner \cite{hindleymilner} (and extensions) enables users
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    21
  to write @{text "\<forall>x. B x"} concisely, when the type @{text "'a"} is
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    22
  already clear from the context.\footnote{Type-inference taken to the
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    23
  extreme can easily confuse users, though.  Beginners often stumble
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    24
  over unexpectedly general types inferred by the system.}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    25
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    26
  \medskip The main inner syntax operations are \emph{read} for
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    27
  parsing together with type-checking, and \emph{pretty} for formatted
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    28
  output.  See also \secref{sec:read-print}.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    29
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    30
  Furthermore, the input and output syntax layers are sub-divided into
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    31
  separate phases for \emph{concrete syntax} versus \emph{abstract
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    32
  syntax}, see also \secref{sec:parse-unparse} and
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    33
  \secref{sec:term-check}, respectively.  This results in the
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    34
  following decomposition of the main operations:
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    35
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    36
  \begin{itemize}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    37
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    38
  \item @{text "read = parse; check"}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    39
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    40
  \item @{text "pretty = uncheck; unparse"}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    41
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    42
  \end{itemize}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    43
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    44
  Some specification package might thus intercept syntax processing at
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    45
  a well-defined stage after @{text "parse"}, to a augment the
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    46
  resulting pre-term before full type-reconstruction is performed by
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    47
  @{text "check"}, for example.  Note that the formal status of bound
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    48
  variables, versus free variables, versus constants must not be
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    49
  changed here! *}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    50
30272
2d612824e642 regenerated document;
wenzelm
parents: 30270
diff changeset
    51
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    52
section {* Reading and pretty printing \label{sec:read-print} *}
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
    53
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    54
text {* Read and print operations are roughly dual to each other, such
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    55
  that for the user @{text "s' = pretty (read s)"} looks similar to
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    56
  the original source text @{text "s"}, but the details depend on many
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    57
  side-conditions.  There are also explicit options to control
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    58
  suppressing of type information in the output.  The default
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    59
  configuration routinely looses information, so @{text "t' = read
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    60
  (pretty t)"} might fail, produce a differently typed term, or a
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    61
  completely different term in the face of syntactic overloading!  *}
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
    62
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    63
text %mlref {*
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    64
  \begin{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    65
  @{index_ML Syntax.read_typ: "Proof.context -> string -> typ"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    66
  @{index_ML Syntax.read_term: "Proof.context -> string -> term"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    67
  @{index_ML Syntax.read_prop: "Proof.context -> string -> term"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    68
  @{index_ML Syntax.pretty_typ: "Proof.context -> typ -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    69
  @{index_ML Syntax.pretty_term: "Proof.context -> term -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    70
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    71
52422
wenzelm
parents: 48985
diff changeset
    72
  %FIXME description
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    73
*}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    74
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    75
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    76
section {* Parsing and unparsing \label{sec:parse-unparse} *}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    77
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    78
text {* Parsing and unparsing converts between actual source text and
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    79
  a certain \emph{pre-term} format, where all bindings and scopes are
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    80
  resolved faithfully.  Thus the names of free variables or constants
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    81
  are already determined in the sense of the logical context, but type
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    82
  information might is still missing.  Pre-terms support an explicit
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    83
  language of \emph{type constraints} that may be augmented by user
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    84
  code to guide the later \emph{check} phase, for example.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    85
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    86
  Actual parsing is based on traditional lexical analysis and Earley
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    87
  parsing for arbitrary context-free grammars.  The user can specify
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    88
  this via mixfix annotations.  Moreover, there are \emph{syntax
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    89
  translations} that can be augmented by the user, either
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    90
  declaratively via @{command translations} or programmatically via
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    91
  @{command parse_translation}, @{command print_translation} etc.  The
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    92
  final scope resolution is performed by the system, according to name
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    93
  spaces for types, constants etc.\ determined by the context.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
    94
*}
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    95
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    96
text %mlref {*
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    97
  \begin{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    98
  @{index_ML Syntax.parse_typ: "Proof.context -> string -> typ"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
    99
  @{index_ML Syntax.parse_term: "Proof.context -> string -> term"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   100
  @{index_ML Syntax.parse_prop: "Proof.context -> string -> term"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   101
  @{index_ML Syntax.unparse_typ: "Proof.context -> typ -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   102
  @{index_ML Syntax.unparse_term: "Proof.context -> term -> Pretty.T"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   103
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   104
52422
wenzelm
parents: 48985
diff changeset
   105
  %FIXME description
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   106
*}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   107
39852
9c977f899ebf tuned chapter arrangement;
wenzelm
parents: 34924
diff changeset
   108
34924
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
   109
section {* Checking and unchecking \label{sec:term-check} *}
520727474bbe theory data example;
wenzelm
parents: 30272
diff changeset
   110
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   111
text {* These operations define the transition from pre-terms and
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   112
  fully-annotated terms in the sense of the logical core
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   113
  (\chref{ch:logic}).
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   114
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   115
  The \emph{check} phase is meant to subsume a variety of mechanisms
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   116
  in the manner of ``type-inference'' or ``type-reconstruction'' or
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   117
  ``type-improvement'', not just type-checking in the narrow sense.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   118
  The \emph{uncheck} phase is roughly dual, it prunes type-information
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   119
  before pretty printing.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   120
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   121
  A typical add-on for the check/uncheck syntax layer is the @{command
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   122
  abbreviation} mechanism.  Here the user specifies syntactic
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   123
  definitions that are managed by the system as polymorphic @{text
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   124
  "let"} bindings.  These are expanded during the @{text "check"}
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   125
  phase, and contracted during the @{text "uncheck"} phase, without
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   126
  affecting the type-assignment of the given terms.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   127
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   128
  \medskip The precise meaning of type checking depends on the context
45260
48295059cef3 fixed typo
bulwahn
parents: 45258
diff changeset
   129
  --- additional check/uncheck plugins might be defined in user space!
45258
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   130
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   131
  For example, the @{command class} command defines a context where
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   132
  @{text "check"} treats certain type instances of overloaded
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   133
  constants according to the ``dictionary construction'' of its
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   134
  logical foundation.  This involves ``type improvement''
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   135
  (specialization of slightly too general types) and replacement by
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   136
  certain locale parameters.  See also \cite{Haftmann-Wenzel:2009}.
97f8806c3ed6 some text on inner-syntax;
wenzelm
parents: 42510
diff changeset
   137
*}
39852
9c977f899ebf tuned chapter arrangement;
wenzelm
parents: 34924
diff changeset
   138
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   139
text %mlref {*
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   140
  \begin{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   141
  @{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
   142
  @{index_ML Syntax.check_terms: "Proof.context -> term list -> term list"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   143
  @{index_ML Syntax.check_props: "Proof.context -> term list -> term list"} \\
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   144
  @{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
   145
  @{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
   146
  \end{mldecls}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   147
52422
wenzelm
parents: 48985
diff changeset
   148
  %FIXME description
39876
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   149
*}
1ff9bce085bd preliminary material on "Concrete syntax and type-checking";
wenzelm
parents: 39865
diff changeset
   150
30124
b956bf0dc87c basic setup for chapter "Syntax and type-checking";
wenzelm
parents:
diff changeset
   151
end