CookBook/Package/Ind_Interface.thy
author Christian Urban <urbanc@in.tum.de>
Thu, 05 Feb 2009 16:34:05 +0000
changeset 96 018bfa718982
parent 88 ebbd0dd008c8
child 102 5e309df58557
permissions -rw-r--r--
(re)defined the document antiquotation @subgoal in order to be closer to what is actually printed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     1
theory Ind_Interface
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
     2
imports "../Base" Simple_Inductive_Package
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     3
begin
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     4
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
     5
section{* The Interface \label{sec:ind-interface} *}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     6
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
     7
text {*
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
     8
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
     9
  In order to add a new inductive predicate to a theory with the help of our
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    10
  package, the user must \emph{invoke} it. For every package, there are
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    11
  essentially two different ways of invoking it, which we will refer to as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    12
  \emph{external} and \emph{internal}. By external invocation we mean that the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    13
  package is called from within a theory document. In this case, the type of
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    14
  the inductive predicate, as well as its introduction rules, are given as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    15
  strings by the user. Before the package can actually make the definition,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    16
  the type and introduction rules have to be parsed. In contrast, internal
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    17
  invocation means that the package is called by some other package. For
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    18
  example, the function definition package \cite{Krauss-IJCAR06} calls the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    19
  inductive definition package to define the graph of the function. However,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    20
  it is not a good idea for the function definition package to pass the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    21
  introduction rules for the function graph to the inductive definition
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    22
  package as strings. In this case, it is better to directly pass the rules to
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    23
  the package as a list of terms, which is more robust than handling strings
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    24
  that are lacking the additional structure of terms. These two ways of
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    25
  invoking the package are reflected in its ML programming interface, which
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    26
  consists of two functions:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    27
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    28
  @{ML_chunk [display] SIMPLE_INDUCTIVE_PACKAGE}
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    29
*}
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    30
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    31
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    32
  The function for external invocation of the package is called @{ML
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    33
  add_inductive in SimpleInductivePackage}, whereas the one for internal
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    34
  invocation is called @{ML add_inductive_i in SimpleInductivePackage}. Both
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    35
  of these functions take as arguments the names and types of the inductive
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    36
  predicates, the names and types of their parameters, the actual introduction
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    37
  rules and a \emph{local theory}.  They return a local theory containing the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    38
  definition, together with a tuple containing the introduction and induction
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    39
  rules, which are stored in the local theory, too.  In contrast to an
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    40
  ordinary theory, which simply consists of a type signature, as well as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    41
  tables for constants, axioms and theorems, a local theory also contains
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    42
  additional context information, such as locally fixed variables and local
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    43
  assumptions that may be used by the package. The type @{ML_type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    44
  local_theory} is identical to the type of \emph{proof contexts} @{ML_type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    45
  "Proof.context"}, although not every proof context constitutes a valid local
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    46
  theory.  Note that @{ML add_inductive_i in SimpleInductivePackage} expects
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    47
  the types of the predicates and parameters to be specified using the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    48
  datatype @{ML_type typ} of Isabelle's logical framework, whereas @{ML
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    49
  add_inductive in SimpleInductivePackage} expects them to be given as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    50
  optional strings. If no string is given for a particular predicate or
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    51
  parameter, this means that the type should be inferred by the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    52
  package. Additional \emph{mixfix syntax} may be associated with the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    53
  predicates and parameters as well. Note that @{ML add_inductive_i in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    54
  SimpleInductivePackage} does not allow mixfix syntax to be associated with
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    55
  parameters, since it can only be used for parsing. The names of the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    56
  predicates, parameters and rules are represented by the type @{ML_type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    57
  Binding.binding}. Strings can be turned into elements of the type @{ML_type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    58
  Binding.binding} using the function @{ML [display] "Binding.name : string ->
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    59
  Binding.binding"} Each introduction rule is given as a tuple containing its
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    60
  name, a list of \emph{attributes} and a logical formula. Note that the type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    61
  @{ML_type Attrib.binding} used in the list of introduction rules is just a
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    62
  shorthand for the type @{ML_type "Binding.binding * Attrib.src list"}.  The
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    63
  function @{ML add_inductive_i in SimpleInductivePackage} expects the formula
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    64
  to be specified using the datatype @{ML_type term}, whereas @{ML
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    65
  add_inductive in SimpleInductivePackage} expects it to be given as a string.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    66
  An attribute specifies additional actions and transformations that should be
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    67
  applied to a theorem, such as storing it in the rule databases used by
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    68
  automatic tactics like the simplifier. The code of the package, which will
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    69
  be described in the following section, will mostly treat attributes as a
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    70
  black box and just forward them to other functions for storing theorems in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    71
  local theories.  The implementation of the function @{ML add_inductive in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    72
  SimpleInductivePackage} for external invocation of the package is quite
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    73
  simple. Essentially, it just parses the introduction rules and then passes
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    74
  them on to @{ML add_inductive_i in SimpleInductivePackage}:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    75
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    76
  @{ML_chunk [display] add_inductive}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    77
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    78
  For parsing and type checking the introduction rules, we use the function
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    79
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    80
  @{ML [display] "Specification.read_specification:
76
b99fa5fa63fc adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
    81
  (Binding.binding * string option * mixfix) list ->  (*{variables}*)
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    82
  (Attrib.binding * string list) list list ->  (*{rules}*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    83
  local_theory ->
76
b99fa5fa63fc adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
    84
  (((Binding.binding * typ) * mixfix) list *
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    85
   (Attrib.binding * term list) list) *
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
    86
  local_theory"}
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    87
*}
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    88
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
    89
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    90
  During parsing, both predicates and parameters are treated as variables, so
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    91
  the lists \verb!preds_syn! and \verb!params_syn! are just appended
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    92
  before being passed to @{ML read_specification in Specification}. Note that the format
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    93
  for rules supported by @{ML read_specification in Specification} is more general than
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    94
  what is required for our package. It allows several rules to be associated
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    95
  with one name, and the list of rules can be partitioned into several
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    96
  sublists. In order for the list \verb!intro_srcs! of introduction rules
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    97
  to be acceptable as an input for @{ML read_specification in Specification}, we first
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    98
  have to turn it into a list of singleton lists. This transformation
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
    99
  has to be reversed later on by applying the function
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   100
  @{ML [display] "the_single: 'a list -> 'a"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   101
  to the list \verb!specs! containing the parsed introduction rules.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   102
  The function @{ML read_specification in Specification} also returns the list \verb!vars!
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   103
  of predicates and parameters that contains the inferred types as well.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   104
  This list has to be chopped into the two lists \verb!preds_syn'! and
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   105
  \verb!params_syn'! for predicates and parameters, respectively.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   106
  All variables occurring in a rule but not in the list of variables passed to
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   107
  @{ML read_specification in Specification} will be bound by a meta-level universal
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   108
  quantifier.
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   109
*}
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   110
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   111
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   112
  Finally, @{ML read_specification in Specification} also returns another local theory,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   113
  but we can safely discard it. As an example, let us look at how we can use this
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   114
  function to parse the introduction rules of the @{text trcl} predicate:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   115
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   116
  @{ML_response [display]
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   117
"Specification.read_specification
55
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   118
  [(Binding.name \"trcl\", NONE, NoSyn),
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   119
   (Binding.name \"r\", SOME \"'a \<Rightarrow> 'a \<Rightarrow> bool\", NoSyn)]
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   120
  [[((Binding.name \"base\", []), [\"trcl r x x\"])],
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   121
   [((Binding.name \"step\", []), [\"trcl r x y \<Longrightarrow> r y z \<Longrightarrow> trcl r x z\"])]]
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   122
  @{context}"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   123
"((\<dots>,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   124
  [(\<dots>,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   125
    [Const (\"all\", \<dots>) $ Abs (\"x\", TFree (\"'a\", \<dots>),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   126
       Const (\"Trueprop\", \<dots>) $
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   127
         (Free (\"trcl\", \<dots>) $ Free (\"r\", \<dots>) $ Bound 0 $ Bound 0))]),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   128
   (\<dots>,
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   129
    [Const (\"all\", \<dots>) $ Abs (\"x\", TFree (\"'a\", \<dots>),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   130
       Const (\"all\", \<dots>) $ Abs (\"y\", TFree (\"'a\", \<dots>),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   131
         Const (\"all\", \<dots>) $ Abs (\"z\", TFree (\"'a\", \<dots>),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   132
           Const (\"==>\", \<dots>) $
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   133
             (Const (\"Trueprop\", \<dots>) $
42
cd612b489504 tuned mostly antiquotation and text
Christian Urban <urbanc@in.tum.de>
parents: 35
diff changeset
   134
               (Free (\"trcl\", \<dots>) $ Free (\"r\", \<dots>) $ Bound 2 $ Bound 1)) $
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   135
             (Const (\"==>\", \<dots>) $ \<dots> $ \<dots>))))])]),
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   136
 \<dots>)
76
b99fa5fa63fc adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   137
: (((Binding.binding * typ) * mixfix) list *
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   138
   (Attrib.binding * term list) list) * local_theory"}
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   139
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   140
  In the list of variables passed to @{ML read_specification in Specification}, we have
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   141
  used the mixfix annotation @{ML NoSyn} to indicate that we do not want to associate any
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   142
  mixfix syntax with the variable. Moreover, we have only specified the type of \texttt{r},
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   143
  whereas the type of \texttt{trcl} is computed using type inference.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   144
  The local variables \texttt{x}, \texttt{y} and \texttt{z} of the introduction rules
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   145
  are turned into bound variables with the de Bruijn indices,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   146
  whereas \texttt{trcl} and \texttt{r} remain free variables.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   147
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   148
*}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   149
53
0c3580c831a4 removed the @{ML ...} antiquotation in favour of @{ML_open ...x}
Christian Urban <urbanc@in.tum.de>
parents: 42
diff changeset
   150
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   151
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   152
  \paragraph{Parsers for theory syntax}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   153
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   154
  Although the function @{ML add_inductive in SimpleInductivePackage} parses terms and types, it still
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   155
  cannot be used to invoke the package directly from within a theory document.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   156
  In order to do this, we have to write another parser. Before we describe
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   157
  the process of writing parsers for theory syntax in more detail, we first
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   158
  show some examples of how we would like to use the inductive definition
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   159
  package.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   160
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   161
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   162
  The definition of the transitive closure should look as follows:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   163
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   164
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   165
simple_inductive
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   166
  trcl for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   167
where
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   168
  base: "trcl r x x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   169
| step: "trcl r x y \<Longrightarrow> r y z \<Longrightarrow> trcl r x z"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   170
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   171
thm trcl_def
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   172
thm trcl.induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   173
thm base
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   174
thm step
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   175
thm trcl.intros
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   176
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   177
lemma trcl_strong_induct:
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   178
  assumes trcl: "trcl r x y"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   179
  and I1: "\<And>x. P x x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   180
  and I2: "\<And>x y z. P x y \<Longrightarrow> trcl r x y \<Longrightarrow> r y z \<Longrightarrow> P x z"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   181
  shows "P x y" 
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   182
proof -
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   183
  from trcl
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   184
  have "P x y \<and> trcl r x y"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   185
  proof induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   186
    case (base x)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   187
    from I1 and trcl.base show ?case ..
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   188
  next
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   189
    case (step x y z)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   190
    then have "P x y" and "trcl r x y" by simp_all
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   191
    from `P x y` `trcl r x y` `r y z` have "P x z"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   192
      by (rule I2)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   193
    moreover from `trcl r x y` `r y z` have "trcl r x z"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   194
      by (rule trcl.step)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   195
    ultimately show ?case ..
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   196
  qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   197
  then show ?thesis ..
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   198
qed
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   199
(*>*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   200
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   201
text {* Even and odd numbers can be defined by *}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   202
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   203
simple_inductive
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   204
  even and odd
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   205
where
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   206
  even0: "even 0"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   207
| evenS: "odd n \<Longrightarrow> even (Suc n)"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   208
| oddS: "even n \<Longrightarrow> odd (Suc n)"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   209
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   210
thm even_def odd_def
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   211
thm even.induct odd.induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   212
thm even0
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   213
thm evenS
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   214
thm oddS
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   215
thm even_odd.intros
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   216
(*>*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   217
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   218
text {* The accessible part of a relation can be introduced as follows: *}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   219
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   220
simple_inductive
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   221
  accpart for r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   222
where
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   223
  accpartI: "(\<And>y. r y x \<Longrightarrow> accpart r y) \<Longrightarrow> accpart r x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   224
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   225
thm accpart_def
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   226
thm accpart.induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   227
thm accpartI
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   228
(*>*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   229
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   230
text {*
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   231
  Moreover, it should also be possible to define the accessible part
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   232
  inside a locale fixing the relation @{text r}:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   233
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   234
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   235
locale rel =
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   236
  fixes r :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   237
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   238
simple_inductive (in rel) accpart'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   239
where
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   240
  accpartI': "\<And>x. (\<And>y. r y x \<Longrightarrow> accpart' y) \<Longrightarrow> accpart' x"
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   241
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   242
context rel
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   243
begin
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   244
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   245
thm accpartI'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   246
thm accpart'.induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   247
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   248
end
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   249
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   250
thm rel.accpartI'
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   251
thm rel.accpart'.induct
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   252
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   253
ML{*val (result, lthy) = SimpleInductivePackage.add_inductive
55
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   254
  [(Binding.name "trcl'", NONE, NoSyn)] [(Binding.name "r", SOME "'a \<Rightarrow> 'a \<Rightarrow> bool", NoSyn)]
0b55402ae95e Adapted to changes in binding module.
berghofe
parents: 53
diff changeset
   255
  [((Binding.name "base", []), "\<And>x. trcl' r x x"), ((Binding.name "step", []), "\<And>x y z. trcl' r x y \<Longrightarrow> r y z \<Longrightarrow> trcl' r x z")]
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   256
  (TheoryTarget.init NONE @{theory})
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   257
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   258
(*>*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   259
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   260
text {*
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   261
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   262
  In this context, it is important to note that Isabelle distinguishes
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   263
  between \emph{outer} and \emph{inner} syntax. Theory commands such as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   264
  \isa{\isacommand{simple{\isacharunderscore}inductive} $\ldots$ \isacommand{for} $\ldots$ \isacommand{where} $\ldots$}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   265
  belong to the outer syntax, whereas items in quotation marks, in particular
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   266
  terms such as @{text [source] "trcl r x x"} and types such as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   267
  @{text [source] "'a \<Rightarrow> 'a \<Rightarrow> bool"} belong to the inner syntax.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   268
  Separating the two layers of outer and inner syntax greatly simplifies
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   269
  matters, because the parser for terms and types does not have to know
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   270
  anything about the possible syntax of theory commands, and the parser
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   271
  for theory commands need not be concerned about the syntactic structure
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   272
  of terms and types.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   273
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   274
  \medskip
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   275
  \noindent
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   276
  The syntax of the \isa{\isacommand{simple{\isacharunderscore}inductive}} command
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   277
  can be described by the following railroad diagram:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   278
  \begin{rail}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   279
  'simple\_inductive' target? fixes ('for' fixes)? \\
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   280
  ('where' (thmdecl? prop + '|'))?
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   281
  ;
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   282
  \end{rail}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   283
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   284
  \paragraph{Functional parsers}
71
14c3dd5ee2ad removed mytable from root-file
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   285
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   286
  For parsing terms and types, Isabelle uses a rather general and sophisticated
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   287
  algorithm due to Earley, which is driven by \emph{priority grammars}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   288
  In contrast, parsers for theory syntax are built up using a set of combinators.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   289
  Functional parsing using combinators is a well-established technique, which
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   290
  has been described by many authors, including Paulson \cite{paulson-ML-91}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   291
  and Wadler \cite{Wadler-AFP95}. 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   292
  The central idea is that a parser is a function of type @{ML_type "'a list -> 'b * 'a list"},
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   293
  where @{ML_type "'a"} is a type of \emph{tokens}, and @{ML_type "'b"} is a type for
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   294
  encoding items that the parser has recognized. When a parser is applied to a
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   295
  list of tokens whose prefix it can recognize, it returns an encoding of the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   296
  prefix as an element of type @{ML_type "'b"}, together with the suffix of the list
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   297
  containing the remaining tokens. Otherwise, the parser raises an exception
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   298
  indicating a syntax error. The library for writing functional parsers in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   299
  Isabelle can roughly be split up into two parts. The first part consists of a
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   300
  collection of generic parser combinators that are contained in the structure
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   301
  @{ML_struct Scan} defined in the file @{ML_file "Pure/General/scan.ML"} in the Isabelle
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   302
  sources. While these combinators do not make any assumptions about the concrete
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   303
  structure of the tokens used, the second part of the library consists of combinators
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   304
  for dealing with specific token types.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   305
  The following is an excerpt from the signature of @{ML_struct Scan}:
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   306
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   307
  \begin{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   308
  @{ML "|| : ('a -> 'b) * ('a -> 'b) -> 'a -> 'b"} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   309
  @{ML "-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e"} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   310
  @{ML "|-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e"} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   311
  @{ML "--| : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'b * 'e"} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   312
  @{ML "optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a" in Scan} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   313
  @{ML "repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" in Scan} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   314
  @{ML "repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" in Scan} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   315
  @{ML ">> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c"} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   316
  @{ML "!! : ('a * string option -> string) -> ('a -> 'b) -> 'a -> 'b"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   317
  \end{table}
71
14c3dd5ee2ad removed mytable from root-file
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   318
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   319
  Interestingly, the functions shown above are so generic that they do not
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   320
  even rely on the input and output of the parser being a list of tokens.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   321
  If \texttt{p} succeeds, i.e.\ does not raise an exception, the parser
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   322
  @{ML "p || q" for p q} returns the result of \texttt{p}, otherwise it returns
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   323
  the result of \texttt{q}. The parser @{ML "p -- q" for p q} first parses an
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   324
  item of type @{ML_type "'b"} using \texttt{p}, then passes the remaining tokens
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   325
  of type @{ML_type "'c"} to \texttt{q}, which parses an item of type @{ML_type "'d"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   326
  and returns the remaining tokens of type @{ML_type "'e"}, which are finally
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   327
  returned together with a pair of type @{ML_type "'b * 'd"} containing the two
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   328
  parsed items. The parsers @{ML "p |-- q" for p q} and @{ML "p --| q" for p q}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   329
  work in a similar way as the previous one, with the difference that they
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   330
  discard the item parsed by the first and the second parser, respectively.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   331
  If \texttt{p} succeeds, the parser @{ML "optional p x" for p x in Scan} returns the result
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   332
  of \texttt{p}, otherwise it returns the default value \texttt{x}. The parser
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   333
  @{ML "repeat p" for p in Scan} applies \texttt{p} as often as it can, returning a possibly
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   334
  empty list of parsed items. The parser @{ML "repeat1 p" for p in Scan} is similar,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   335
  but requires \texttt{p} to succeed at least once. The parser
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   336
  @{ML "p >> f" for p f} uses \texttt{p} to parse an item of type @{ML_type "'b"}, to which
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   337
  it applies the function \texttt{f} yielding a value of type @{ML_type "'d"}, which
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   338
  is returned together with the remaining tokens of type @{ML_type "'c"}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   339
  Finally, @{ML "!!"} is used for transforming exceptions produced by parsers.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   340
  If \texttt{p} raises an exception indicating that it cannot parse a given input,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   341
  then an enclosing parser such as
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   342
  @{ML [display] "q -- p || r" for p q r}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   343
  will try the alternative parser \texttt{r}. By writing
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   344
  @{ML [display] "q -- !! err p || r" for err p q r}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   345
  instead, one can achieve that a failure of \texttt{p} causes the whole parser to abort.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   346
  The @{ML "!!"} operator is similar to the \emph{cut} operator in Prolog, which prevents
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   347
  the interpreter from backtracking. The \texttt{err} function supplied as an argument
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   348
  to @{ML "!!"} can be used to produce an error message depending on the current
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   349
  state of the parser, as well as the optional error message returned by \texttt{p}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   350
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   351
  So far, we have only looked at combinators that construct more complex parsers
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   352
  from simpler parsers. In order for these combinators to be useful, we also need
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   353
  some basic parsers. As an example, we consider the following two parsers
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   354
  defined in @{ML_struct Scan}:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   355
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   356
  \begin{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   357
  @{ML "one: ('a -> bool) -> 'a list -> 'a * 'a list" in Scan} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   358
  @{ML "$$ : string -> string list -> string * string list"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   359
  \end{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   360
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   361
  The parser @{ML "one pred" for pred in Scan} parses exactly one token that
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   362
  satisfies the predicate \texttt{pred}, whereas @{ML "$$ s" for s} only
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   363
  accepts a token that equals the string \texttt{s}. Note that we can easily
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   364
  express @{ML "$$ s" for s} using @{ML "one" in Scan}:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   365
  @{ML [display] "one (fn s' => s' = s)" for s in Scan}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   366
  As an example, let us look at how we can use @{ML "$$"} and @{ML "--"} to parse
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   367
  the prefix ``\texttt{hello}'' of the character list ``\texttt{hello world}'':
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   368
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   369
  @{ML_response [display]
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   370
  "($$ \"h\" -- $$ \"e\" -- $$ \"l\" -- $$ \"l\" -- $$ \"o\")
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   371
  [\"h\", \"e\", \"l\", \"l\", \"o\", \" \", \"w\", \"o\", \"r\", \"l\", \"d\"]"
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   372
  "(((((\"h\", \"e\"), \"l\"), \"l\"), \"o\"), [\" \", \"w\", \"o\", \"r\", \"l\", \"d\"])
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   373
  : ((((string * string) * string) * string) * string) * string list"}
71
14c3dd5ee2ad removed mytable from root-file
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   374
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   375
  Most of the time, however, we will have to deal with tokens that are not just strings.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   376
  The parsers for the theory syntax, as well as the parsers for the argument syntax
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   377
  of proof methods and attributes use the token type @{ML_type OuterParse.token},
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   378
  which is identical to @{ML_type OuterLex.token}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   379
  The parser functions for the theory syntax are contained in the structure
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   380
  @{ML_struct OuterParse} defined in the file @{ML_file "Pure/Isar/outer_parse.ML"}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   381
  In our parser, we will use the following functions:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   382
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   383
  \begin{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   384
  @{ML "$$$ : string -> token list -> string * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   385
  @{ML "enum1: string -> (token list -> 'a * token list) -> token list ->
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   386
  'a list * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   387
  @{ML "prop: token list -> string * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   388
  @{ML "opt_target: token list -> string option * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   389
  @{ML "fixes: token list ->
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   390
  (Binding.binding * string option * mixfix) list * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   391
  @{ML "for_fixes: token list ->
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   392
  (Binding.binding * string option * mixfix) list * token list" in OuterParse} \\
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   393
  @{ML "!!! : (token list -> 'a) -> token list -> 'a" in OuterParse}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   394
  \end{table}
71
14c3dd5ee2ad removed mytable from root-file
Christian Urban <urbanc@in.tum.de>
parents: 60
diff changeset
   395
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   396
  The parsers @{ML "$$$" in OuterParse} and @{ML "!!!" in OuterParse} are
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   397
  defined using the parsers @{ML "one" in Scan} and @{ML "!!"} from
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   398
  @{ML_struct Scan}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   399
  The parser @{ML "enum1 s p" for s p in OuterParse} parses a non-emtpy list of items
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   400
  recognized by the parser \texttt{p}, where the items are separated by \texttt{s}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   401
  A proposition can be parsed using the function @{ML prop in OuterParse}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   402
  Essentially, a proposition is just a string or an identifier, but using the
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   403
  specific parser function @{ML prop in OuterParse} leads to more instructive
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   404
  error messages, since the parser will complain that a proposition was expected
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   405
  when something else than a string or identifier is found.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   406
  An optional locale target specification of the form \isa{(\isacommand{in}\ $\ldots$)}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   407
  can be parsed using @{ML opt_target in OuterParse}.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   408
  The lists of names of the predicates and parameters, together with optional
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   409
  types and syntax, are parsed using the functions @{ML "fixes" in OuterParse}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   410
  and @{ML for_fixes in OuterParse}, respectively.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   411
  In addition, the following function from @{ML_struct SpecParse} for parsing
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   412
  an optional theorem name and attribute, followed by a delimiter, will be useful:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   413
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   414
  \begin{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   415
  @{ML "opt_thm_name:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   416
  string -> token list -> Attrib.binding * token list" in SpecParse}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   417
  \end{table}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   418
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   419
  We now have all the necessary tools to write the parser for our
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   420
  \isa{\isacommand{simple{\isacharunderscore}inductive}} command:
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   421
  
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   422
  @{ML_chunk [display] syntax}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   423
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   424
  The definition of the parser \verb!ind_decl! closely follows the railroad
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   425
  diagram shown above. In order to make the code more readable, the structures
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   426
  @{ML_struct OuterParse} and @{ML_struct OuterKeyword} are abbreviated by
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   427
  \texttt{P} and \texttt{K}, respectively. Note how the parser combinator
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   428
  @{ML "!!!" in OuterParse} is used: once the keyword \texttt{where}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   429
  has been parsed, a non-empty list of introduction rules must follow.
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   430
  Had we not used the combinator @{ML "!!!" in OuterParse}, a
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   431
  \texttt{where} not followed by a list of rules would have caused the parser
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   432
  to respond with the somewhat misleading error message
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   433
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   434
  \begin{verbatim}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   435
  Outer syntax error: end of input expected, but keyword where was found
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   436
  \end{verbatim}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   437
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   438
  rather than with the more instructive message
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   439
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   440
  \begin{verbatim}
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   441
  Outer syntax error: proposition expected, but terminator was found
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   442
  \end{verbatim}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   443
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   444
  Once all arguments of the command have been parsed, we apply the function
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   445
  @{ML add_inductive in SimpleInductivePackage}, which yields a local theory
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   446
  transformer of type @{ML_type "local_theory -> local_theory"}. Commands in
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   447
  Isabelle/Isar are realized by transition transformers of type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   448
  @{ML_type [display] "Toplevel.transition -> Toplevel.transition"}
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   449
  We can turn a local theory transformer into a transition transformer by using
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   450
  the function
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   451
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   452
  @{ML [display] "Toplevel.local_theory : string option ->
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   453
  (local_theory -> local_theory) ->
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   454
  Toplevel.transition -> Toplevel.transition"}
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   455
 
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   456
  which, apart from the local theory transformer, takes an optional name of a locale
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   457
  to be used as a basis for the local theory. 
60
5b9c6010897b doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   458
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   459
  (FIXME : needs to be adjusted to new parser type)
60
5b9c6010897b doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   460
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   461
  {\it
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   462
  The whole parser for our command has type
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   463
  @{ML_text [display] "OuterLex.token list ->
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   464
  (Toplevel.transition -> Toplevel.transition) * OuterLex.token list"}
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   465
  which is abbreviated by @{ML_text OuterSyntax.parser_fn}. The new command can be added
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   466
  to the system via the function
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   467
  @{ML_text [display] "OuterSyntax.command :
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   468
  string -> string -> OuterKeyword.T -> OuterSyntax.parser_fn -> unit"}
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   469
  which imperatively updates the parser table behind the scenes. }
60
5b9c6010897b doem tuning and made the cookbook work again with recent changes (CookBook/Package/Ind_Interface.thy needs to be looked at to see what the problem with the new parser type is)
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   470
88
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   471
  In addition to the parser, this
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   472
  function takes two strings representing the name of the command and a short description,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   473
  as well as an element of type @{ML_type OuterKeyword.T} describing which \emph{kind} of
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   474
  command we intend to add. Since we want to add a command for declaring new concepts,
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   475
  we choose the kind @{ML "OuterKeyword.thy_decl"}. Other kinds include
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   476
  @{ML "OuterKeyword.thy_goal"}, which is similar to @{ML thy_decl in OuterKeyword},
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   477
  but requires the user to prove a goal before making the declaration, or
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   478
  @{ML "OuterKeyword.diag"}, which corresponds to a purely diagnostic command that does
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   479
  not change the context. For example, the @{ML thy_goal in OuterKeyword} kind is used
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   480
  by the \isa{\isacommand{function}} command \cite{Krauss-IJCAR06}, which requires the user
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   481
  to prove that a given set of equations is non-overlapping and covers all cases. The kind
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   482
  of the command should be chosen with care, since selecting the wrong one can cause strange
ebbd0dd008c8 adaptation of the package chapter to fit the rest
Christian Urban <urbanc@in.tum.de>
parents: 76
diff changeset
   483
  behaviour of the user interface, such as failure of the undo mechanism.
32
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   484
*}
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   485
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   486
(*<*)
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   487
end
5bb2d29553c2 Added new chapter about writing packages.
berghofe
parents:
diff changeset
   488
(*>*)