CookBook/Package/Ind_Interface.thy
changeset 53 0c3580c831a4
parent 42 cd612b489504
child 55 0b55402ae95e
--- a/CookBook/Package/Ind_Interface.thy	Fri Nov 28 05:56:28 2008 +0100
+++ b/CookBook/Package/Ind_Interface.thy	Sat Nov 29 21:20:18 2008 +0000
@@ -27,9 +27,13 @@
 handling strings that are lacking the additional structure of terms. These two ways of
 invoking the package are reflected in its ML programming interface, which consists of two
 functions:
+
 @{ML_chunk [display] SIMPLE_INDUCTIVE_PACKAGE}
-The function for external invocation of the package is called @{ML_open add_inductive (SIP)},
-whereas the one for internal invocation is called @{ML_open add_inductive_i (SIP)}. Both
+*}
+
+text {*
+The function for external invocation of the package is called @{ML add_inductive in SIP},
+whereas the one for internal invocation is called @{ML add_inductive_i in SIP}. Both
 of these functions take as arguments the names and types of the inductive predicates, the
 names and types of their parameters, the actual introduction rules and a \emph{local theory}.
 They return a local theory containing the definition, together with a tuple containing
@@ -40,13 +44,13 @@
 that may be used by the package. The type @{ML_type local_theory} is identical to the
 type of \emph{proof contexts} @{ML_type "Proof.context"}, although not every proof context
 constitutes a valid local theory.
-Note that @{ML_open add_inductive_i (SIP)} expects the types
+Note that @{ML add_inductive_i in SIP} expects the types
 of the predicates and parameters to be specified using the datatype @{ML_type typ} of Isabelle's
-logical framework, whereas @{ML_open add_inductive (SIP)}
+logical framework, whereas @{ML add_inductive in SIP}
 expects them to be given as optional strings. If no string is
 given for a particular predicate or parameter, this means that the type should be
 inferred by the package. Additional \emph{mixfix syntax} may be associated with
-the predicates and parameters as well. Note that @{ML_open add_inductive_i (SIP)} does not
+the predicates and parameters as well. Note that @{ML add_inductive_i in SIP} does not
 allow mixfix syntax to be associated with parameters, since it can only be used
 for parsing. The names of the predicates, parameters and rules are represented by the
 type @{ML_type Name.binding}. Strings can be turned into elements of the type
@@ -55,45 +59,51 @@
 Each introduction rule is given as a tuple containing its name, a list of \emph{attributes}
 and a logical formula. Note that the type @{ML_type Attrib.binding} used in the list of
 introduction rules is just a shorthand for the type @{ML_type "Name.binding * Attrib.src list"}.
-The function @{ML_open add_inductive_i (SIP)} expects the formula to be specified using the datatype
-@{ML_type term}, whereas @{ML_open add_inductive (SIP)} expects it to be given as a string.
+The function @{ML add_inductive_i in SIP} expects the formula to be specified using the datatype
+@{ML_type term}, whereas @{ML add_inductive in SIP} expects it to be given as a string.
 An attribute specifies additional actions and transformations that should be applied to
 a theorem, such as storing it in the rule databases used by automatic tactics
 like the simplifier. The code of the package, which will be described in the following
 section, will mostly treat attributes as a black box and just forward them to other
 functions for storing theorems in local theories.
-The implementation of the function @{ML_open add_inductive (SIP)} for external invocation
+The implementation of the function @{ML add_inductive in SIP} for external invocation
 of the package is quite simple. Essentially, it just parses the introduction rules
-and then passes them on to @{ML_open add_inductive_i (SIP)}:
+and then passes them on to @{ML add_inductive_i in SIP}:
 @{ML_chunk [display] add_inductive}
 For parsing and type checking the introduction rules, we use the function
-@{ML_open [display] "Specification.read_specification:
+@{ML [display] "Specification.read_specification:
   (Name.binding * string option * mixfix) list ->  (*{variables}*)
   (Attrib.binding * string list) list list ->  (*{rules}*)
   local_theory ->
   (((Name.binding * typ) * mixfix) list *
    (Attrib.binding * term list) list) *
   local_theory"}
+*}
+
+text {*
 During parsing, both predicates and parameters are treated as variables, so
 the lists \verb!preds_syn! and \verb!params_syn! are just appended
-before being passed to @{ML_open read_specification (Specification)}. Note that the format
-for rules supported by @{ML_open read_specification (Specification)} is more general than
+before being passed to @{ML read_specification in Specification}. Note that the format
+for rules supported by @{ML read_specification in Specification} is more general than
 what is required for our package. It allows several rules to be associated
 with one name, and the list of rules can be partitioned into several
 sublists. In order for the list \verb!intro_srcs! of introduction rules
-to be acceptable as an input for @{ML_open read_specification (Specification)}, we first
+to be acceptable as an input for @{ML read_specification in Specification}, we first
 have to turn it into a list of singleton lists. This transformation
 has to be reversed later on by applying the function
 @{ML [display] "the_single: 'a list -> 'a"}
 to the list \verb!specs! containing the parsed introduction rules.
-The function @{ML_open read_specification (Specification)} also returns the list \verb!vars!
+The function @{ML read_specification in Specification} also returns the list \verb!vars!
 of predicates and parameters that contains the inferred types as well.
 This list has to be chopped into the two lists \verb!preds_syn'! and
 \verb!params_syn'! for predicates and parameters, respectively.
 All variables occurring in a rule but not in the list of variables passed to
-@{ML_open read_specification (Specification)} will be bound by a meta-level universal
+@{ML read_specification in Specification} will be bound by a meta-level universal
 quantifier.
-Finally, @{ML_open read_specification (Specification)} also returns another local theory,
+*}
+
+text {*
+Finally, @{ML read_specification in Specification} also returns another local theory,
 but we can safely discard it. As an example, let us look at how we can use this
 function to parse the introduction rules of the @{text trcl} predicate:
 @{ML_response [display]
@@ -119,17 +129,19 @@
  \<dots>)
 : (((Name.binding * typ) * mixfix) list *
    (Attrib.binding * term list) list) * local_theory"}
-In the list of variables passed to @{ML_open read_specification (Specification)}, we have
+In the list of variables passed to @{ML read_specification in Specification}, we have
 used the mixfix annotation @{ML NoSyn} to indicate that we do not want to associate any
 mixfix syntax with the variable. Moreover, we have only specified the type of \texttt{r},
 whereas the type of \texttt{trcl} is computed using type inference.
 The local variables \texttt{x}, \texttt{y} and \texttt{z} of the introduction rules
 are turned into bound variables with the de Bruijn indices,
 whereas \texttt{trcl} and \texttt{r} remain free variables.
+*}
 
+text {*
 \paragraph{Parsers for theory syntax}
 
-Although the function @{ML_open add_inductive (SIP)} parses terms and types, it still
+Although the function @{ML add_inductive in SIP} parses terms and types, it still
 cannot be used to invoke the package directly from within a theory document.
 In order to do this, we have to write another parser. Before we describe
 the process of writing parsers for theory syntax in more detail, we first
@@ -294,38 +306,38 @@
 @{ML "-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e"} \\
 @{ML "|-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e"} \\
 @{ML "--| : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'b * 'e"} \\
-@{ML_open "optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a" (Scan)} \\
-@{ML_open "repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" (Scan)} \\
-@{ML_open "repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" (Scan)} \\
+@{ML "optional: ('a -> 'b * 'a) -> 'b -> 'a -> 'b * 'a" in Scan} \\
+@{ML "repeat: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" in Scan} \\
+@{ML "repeat1: ('a -> 'b * 'a) -> 'a -> 'b list * 'a" in Scan} \\
 @{ML ">> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c"} \\
 @{ML "!! : ('a * string option -> string) -> ('a -> 'b) -> 'a -> 'b"}
 \end{mytable}
 Interestingly, the functions shown above are so generic that they do not
 even rely on the input and output of the parser being a list of tokens.
 If \texttt{p} succeeds, i.e.\ does not raise an exception, the parser
-@{ML_open "p || q" for p q} returns the result of \texttt{p}, otherwise it returns
-the result of \texttt{q}. The parser @{ML_open "p -- q" for p q} first parses an
+@{ML "p || q" for p q} returns the result of \texttt{p}, otherwise it returns
+the result of \texttt{q}. The parser @{ML "p -- q" for p q} first parses an
 item of type @{ML_type "'b"} using \texttt{p}, then passes the remaining tokens
 of type @{ML_type "'c"} to \texttt{q}, which parses an item of type @{ML_type "'d"}
 and returns the remaining tokens of type @{ML_type "'e"}, which are finally
 returned together with a pair of type @{ML_type "'b * 'd"} containing the two
-parsed items. The parsers @{ML_open "p |-- q" for p q} and @{ML_open "p --| q" for p q}
+parsed items. The parsers @{ML "p |-- q" for p q} and @{ML "p --| q" for p q}
 work in a similar way as the previous one, with the difference that they
 discard the item parsed by the first and the second parser, respectively.
-If \texttt{p} succeeds, the parser @{ML_open "optional p x" for p x (Scan)} returns the result
+If \texttt{p} succeeds, the parser @{ML "optional p x" for p x in Scan} returns the result
 of \texttt{p}, otherwise it returns the default value \texttt{x}. The parser
-@{ML_open "repeat p" for p (Scan)} applies \texttt{p} as often as it can, returning a possibly
-empty list of parsed items. The parser @{ML_open "repeat1 p" for p (Scan)} is similar,
+@{ML "repeat p" for p in Scan} applies \texttt{p} as often as it can, returning a possibly
+empty list of parsed items. The parser @{ML "repeat1 p" for p in Scan} is similar,
 but requires \texttt{p} to succeed at least once. The parser
-@{ML_open "p >> f" for p f} uses \texttt{p} to parse an item of type @{ML_type "'b"}, to which
+@{ML "p >> f" for p f} uses \texttt{p} to parse an item of type @{ML_type "'b"}, to which
 it applies the function \texttt{f} yielding a value of type @{ML_type "'d"}, which
 is returned together with the remaining tokens of type @{ML_type "'c"}.
 Finally, @{ML "!!"} is used for transforming exceptions produced by parsers.
 If \texttt{p} raises an exception indicating that it cannot parse a given input,
 then an enclosing parser such as
-@{ML_open [display] "q -- p || r" for p q r}
+@{ML [display] "q -- p || r" for p q r}
 will try the alternative parser \texttt{r}. By writing
-@{ML_open [display] "q -- !! err p || r" for err p q r}
+@{ML [display] "q -- !! err p || r" for err p q r}
 instead, one can achieve that a failure of \texttt{p} causes the whole parser to abort.
 The @{ML "!!"} operator is similar to the \emph{cut} operator in Prolog, which prevents
 the interpreter from backtracking. The \texttt{err} function supplied as an argument
@@ -337,14 +349,14 @@
 some basic parsers. As an example, we consider the following two parsers
 defined in @{ML_struct Scan}:
 \begin{mytable}
-@{ML_open "one: ('a -> bool) -> 'a list -> 'a * 'a list" (Scan)} \\
-@{ML_open "$$ : string -> string list -> string * string list"}
+@{ML "one: ('a -> bool) -> 'a list -> 'a * 'a list" in Scan} \\
+@{ML "$$ : string -> string list -> string * string list"}
 \end{mytable}
-The parser @{ML_open "one pred" for pred (Scan)} parses exactly one token that
-satisfies the predicate \texttt{pred}, whereas @{ML_open "$$ s" for s} only
+The parser @{ML "one pred" for pred in Scan} parses exactly one token that
+satisfies the predicate \texttt{pred}, whereas @{ML "$$ s" for s} only
 accepts a token that equals the string \texttt{s}. Note that we can easily
-express @{ML_open "$$ s" for s} using @{ML_open "one" (Scan)}:
-@{ML_open [display] "one (fn s' => s' = s)" for s (Scan)}
+express @{ML "$$ s" for s} using @{ML "one" in Scan}:
+@{ML [display] "one (fn s' => s' = s)" for s in Scan}
 As an example, let us look at how we can use @{ML "$$"} and @{ML "--"} to parse
 the prefix ``\texttt{hello}'' of the character list ``\texttt{hello world}'':
 @{ML_response [display]
@@ -360,37 +372,37 @@
 @{ML_struct OuterParse} defined in the file @{ML_file "Pure/Isar/outer_parse.ML"}.
 In our parser, we will use the following functions:
 \begin{mytable}
-@{ML_open "$$$ : string -> token list -> string * token list" (OuterParse)} \\
-@{ML_open "enum1: string -> (token list -> 'a * token list) -> token list ->
-  'a list * token list" (OuterParse)} \\
-@{ML_open "prop: token list -> string * token list" (OuterParse)} \\
-@{ML_open "opt_target: token list -> string option * token list" (OuterParse)} \\
-@{ML_open "fixes: token list ->
-  (Name.binding * string option * mixfix) list * token list" (OuterParse)} \\
-@{ML_open "for_fixes: token list ->
-  (Name.binding * string option * mixfix) list * token list" (OuterParse)} \\
-@{ML_open "!!! : (token list -> 'a) -> token list -> 'a" (OuterParse)}
+@{ML "$$$ : string -> token list -> string * token list" in OuterParse} \\
+@{ML "enum1: string -> (token list -> 'a * token list) -> token list ->
+  'a list * token list" in OuterParse} \\
+@{ML "prop: token list -> string * token list" in OuterParse} \\
+@{ML "opt_target: token list -> string option * token list" in OuterParse} \\
+@{ML "fixes: token list ->
+  (Name.binding * string option * mixfix) list * token list" in OuterParse} \\
+@{ML "for_fixes: token list ->
+  (Name.binding * string option * mixfix) list * token list" in OuterParse} \\
+@{ML "!!! : (token list -> 'a) -> token list -> 'a" in OuterParse}
 \end{mytable}
-The parsers @{ML_open "$$$" (OuterParse)} and @{ML_open "!!!" (OuterParse)} are
-defined using the parsers @{ML_open "one" (Scan)} and @{ML "!!"} from
+The parsers @{ML "$$$" in OuterParse} and @{ML "!!!" in OuterParse} are
+defined using the parsers @{ML "one" in Scan} and @{ML "!!"} from
 @{ML_struct Scan}.
-The parser @{ML_open "enum1 s p" for s p (OuterParse)} parses a non-emtpy list of items
+The parser @{ML "enum1 s p" for s p in OuterParse} parses a non-emtpy list of items
 recognized by the parser \texttt{p}, where the items are separated by \texttt{s}.
-A proposition can be parsed using the function @{ML_open prop (OuterParse)}.
+A proposition can be parsed using the function @{ML prop in OuterParse}.
 Essentially, a proposition is just a string or an identifier, but using the
-specific parser function @{ML_open prop (OuterParse)} leads to more instructive
+specific parser function @{ML prop in OuterParse} leads to more instructive
 error messages, since the parser will complain that a proposition was expected
 when something else than a string or identifier is found.
 An optional locale target specification of the form \isa{(\isacommand{in}\ $\ldots$)}
-can be parsed using @{ML_open opt_target (OuterParse)}.
+can be parsed using @{ML opt_target in OuterParse}.
 The lists of names of the predicates and parameters, together with optional
-types and syntax, are parsed using the functions @{ML_open "fixes" (OuterParse)}
-and @{ML_open for_fixes (OuterParse)}, respectively.
+types and syntax, are parsed using the functions @{ML "fixes" in OuterParse}
+and @{ML for_fixes in OuterParse}, respectively.
 In addition, the following function from @{ML_struct SpecParse} for parsing
 an optional theorem name and attribute, followed by a delimiter, will be useful:
 \begin{mytable}
-@{ML_open "opt_thm_name:
-  string -> token list -> Attrib.binding * token list" (SpecParse)}
+@{ML "opt_thm_name:
+  string -> token list -> Attrib.binding * token list" in SpecParse}
 \end{mytable}
 We now have all the necessary tools to write the parser for our
 \isa{\isacommand{simple{\isacharunderscore}inductive}} command:
@@ -399,9 +411,9 @@
 diagram shown above. In order to make the code more readable, the structures
 @{ML_struct OuterParse} and @{ML_struct OuterKeyword} are abbreviated by
 \texttt{P} and \texttt{K}, respectively. Note how the parser combinator
-@{ML_open "!!!" (OuterParse)} is used: once the keyword \texttt{where}
+@{ML "!!!" in OuterParse} is used: once the keyword \texttt{where}
 has been parsed, a non-empty list of introduction rules must follow.
-Had we not used the combinator @{ML_open "!!!" (OuterParse)}, a
+Had we not used the combinator @{ML "!!!" in OuterParse}, a
 \texttt{where} not followed by a list of rules would have caused the parser
 to respond with the somewhat misleading error message
 \begin{verbatim}
@@ -412,7 +424,7 @@
   Outer syntax error: proposition expected, but terminator was found
 \end{verbatim}
 Once all arguments of the command have been parsed, we apply the function
-@{ML_open add_inductive (SimpleInductivePackage)}, which yields a local theory
+@{ML add_inductive in SimpleInductivePackage}, which yields a local theory
 transformer of type @{ML_type "local_theory -> local_theory"}. Commands in
 Isabelle/Isar are realized by transition transformers of type
 @{ML_type [display] "Toplevel.transition -> Toplevel.transition"}
@@ -434,10 +446,10 @@
 as well as an element of type @{ML_type OuterKeyword.T} describing which \emph{kind} of
 command we intend to add. Since we want to add a command for declaring new concepts,
 we choose the kind @{ML "OuterKeyword.thy_decl"}. Other kinds include
-@{ML "OuterKeyword.thy_goal"}, which is similar to @{ML_open thy_decl (OuterKeyword)},
+@{ML "OuterKeyword.thy_goal"}, which is similar to @{ML thy_decl in OuterKeyword},
 but requires the user to prove a goal before making the declaration, or
 @{ML "OuterKeyword.diag"}, which corresponds to a purely diagnostic command that does
-not change the context. For example, the @{ML_open thy_goal (OuterKeyword)} kind is used
+not change the context. For example, the @{ML thy_goal in OuterKeyword} kind is used
 by the \isa{\isacommand{function}} command \cite{Krauss-IJCAR06}, which requires the user
 to prove that a given set of equations is non-overlapping and covers all cases. The kind
 of the command should be chosen with care, since selecting the wrong one can cause strange