src/Doc/Datatypes/Datatypes.thy
changeset 52840 a0da63cec918
parent 52829 591e76f2651e
child 52841 87a66bad0796
equal deleted inserted replaced
52839:2c0e1a84dcc7 52840:a0da63cec918
     4 Tutorial for (co)datatype definitions with the new package.
     4 Tutorial for (co)datatype definitions with the new package.
     5 *)
     5 *)
     6 
     6 
     7 theory Datatypes
     7 theory Datatypes
     8 imports Setup
     8 imports Setup
       
     9 keywords
       
    10   "primrec_new" :: thy_decl and
       
    11   "primcorec" :: thy_decl
     9 begin
    12 begin
    10 
    13 
    11 (*
    14 (*<*)
    12 text {*
    15 (* FIXME: Evil setup until "primrec_new" and "primcorec" are in place. *)
    13 
    16 ML_command {*
    14   primrec_new <fixes>
    17 fun add_dummy_cmd _ _ lthy = lthy;
    15 
    18 
    16 *}
    19 val _ = Outer_Syntax.local_theory @{command_spec "primrec_new"} ""
    17 *)
    20   (Parse.fixes -- Parse_Spec.where_alt_specs >> uncurry add_dummy_cmd);
       
    21 
       
    22 val _ = Outer_Syntax.local_theory @{command_spec "primcorec"} ""
       
    23   (Parse.fixes -- Parse_Spec.where_alt_specs >> uncurry add_dummy_cmd);
       
    24 *}
       
    25 (*>*)
       
    26 
    18 
    27 
    19 section {* Introduction
    28 section {* Introduction
    20   \label{sec:introduction} *}
    29   \label{sec:introduction} *}
    21 
    30 
    22 text {*
    31 text {*
    23 The 2013 edition of Isabelle introduced new definitional package for datatypes
    32 The 2013 edition of Isabelle introduced new definitional package for datatypes
    24 and codatatypes. The datatype support is similar to that provided by the earlier
    33 and codatatypes. The datatype support is similar to that provided by the earlier
    25 package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL},
    34 package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL},
    26 documented in the Isar reference manual \cite{isabelle-isar-ref};
    35 documented in the Isar reference manual \cite{isabelle-isar-ref};
    27 indeed, replacing @{command datatype} by @{command datatype_new} is usually
    36 indeed, replacing the keyword @{command datatype} by @{command datatype_new} is
    28 sufficient to port existing specifications to the new package. What makes the
    37 usually all that is needed to port existing theories to use the new package.
    29 new package attractive is that it supports definitions with recursion through a
    38 
    30 large class of non-datatypes, notably finite sets:
    39 Perhaps the main advantage of the new package is that it supports definitions
    31 *}
    40 with recursion through a large class of non-datatypes, notably finite sets:
    32 
    41 *}
    33     datatype_new 'a treeFS = TreeFS 'a "'a treeFS fset"
    42 
    34 
    43     datatype_new 'a treeFS = NodeFS 'a "'a treeFS fset"
    35 text {*
    44 
    36 \noindent
    45 text {*
    37 Another advantage of the new package is that it supports local definitions:
    46 \noindent
       
    47 Another strong point is that the package supports local definitions:
    38 *}
    48 *}
    39 
    49 
    40     context linorder
    50     context linorder
    41     begin
    51     begin
    42       datatype_new flag = Less | Eq | Greater
    52       datatype_new flag = Less | Eq | Greater
    43     end
    53     end
    44 
    54 
    45 text {*
    55 text {*
    46 \noindent
    56 \noindent
    47 Finally, the package also provides some convenience, notably automatically
    57 The package also provides some convenience, notably automatically generated
    48 generated destructors.
    58 destructors.
    49 
    59 
    50 The command @{command datatype_new} is expected to displace @{command datatype} in a future
    60 The command @{command datatype_new} is expected to displace @{command datatype}
    51 release. Authors of new theories are encouraged to use @{command datatype_new}, and
    61 in a future release. Authors of new theories are encouraged to use
    52 maintainers of older theories may want to consider upgrading in the coming months.
    62 @{command datatype_new}, and maintainers of older theories may want to consider
    53 
    63 upgrading in the coming months.
    54 The package also provides codatatypes (or ``coinductive datatypes''), which may
    64 
    55 have infinite values. The following command introduces a codatatype of infinite
    65 In addition to plain inductive datatypes, the package supports coinductive
    56 streams:
    66 datatypes, or \emph{codatatypes}, which may have infinite values. For example,
       
    67 the following command introduces a codatatype of infinite streams:
    57 *}
    68 *}
    58 
    69 
    59     codatatype 'a stream = Stream 'a "'a stream"
    70     codatatype 'a stream = Stream 'a "'a stream"
    60 
    71 
    61 text {*
    72 text {*
    62 \noindent
    73 \noindent
    63 Mixed inductive--coinductive recursion is possible via nesting.
    74 Mixed inductive--coinductive recursion is possible via nesting. Compare the
    64 Compare the following four examples:
    75 following four examples:
    65 *}
    76 *}
    66 
    77 
    67     datatype_new 'a treeFF = TreeFF 'a "'a treeFF list"
    78     datatype_new 'a treeFF = NodeFF 'a "'a treeFF list"
    68     datatype_new 'a treeFI = TreeFI 'a "'a treeFF stream"
    79     datatype_new 'a treeFI = NodeFI 'a "'a treeFF stream"
    69     codatatype 'a treeIF = TreeIF 'a "'a treeFF list"
    80     codatatype 'a treeIF = NodeIF 'a "'a treeFF list"
    70     codatatype 'a treeII = TreeII 'a "'a treeFF stream"
    81     codatatype 'a treeII = NodeII 'a "'a treeFF stream"
    71 
    82 
    72 text {*
    83 text {*
       
    84 The first two tree types allow only finite branches, whereas the last two allow
       
    85 infinite branches. Orthogonally, the nodes in the first and third types have
       
    86 finite branching, whereas those of the second and fourth have infinitely many
       
    87 direct subtrees.
       
    88 
    73 To use the package, it is necessary to import the @{theory BNF} theory, which
    89 To use the package, it is necessary to import the @{theory BNF} theory, which
    74 can be precompiled as the \textit{HOL-BNF} image. The following commands show
    90 can be precompiled as the \textit{HOL-BNF} image. The following commands show
    75 how to launch jEdit/PIDE with the image loaded and how to build the image
    91 how to launch jEdit/PIDE with the image loaded and how to build the image
    76 without launching jEdit:
    92 without launching jEdit:
    77 *}
    93 *}
   168 different flavors of recursion. More examples can be found in the directory
   184 different flavors of recursion. More examples can be found in the directory
   169 \verb|~~/src/HOL/BNF/Examples|.
   185 \verb|~~/src/HOL/BNF/Examples|.
   170 *}
   186 *}
   171 
   187 
   172 
   188 
   173 subsection {* Introductory Examples
   189 subsection {* Examples
   174   \label{ssec:datatype-introductory-examples} *}
   190   \label{ssec:datatype-examples} *}
   175 
   191 
   176 subsubsection {* Nonrecursive Types *}
   192 subsubsection {* Nonrecursive Types *}
   177 
   193 
   178 text {*
   194 text {*
   179 enumeration type:
   195 enumeration type:
   248 
   264 
   249 More complex example, which reuses our maybe and triple types:
   265 More complex example, which reuses our maybe and triple types:
   250 *}
   266 *}
   251 
   267 
   252     datatype_new 'a triple_tree =
   268     datatype_new 'a triple_tree =
   253       Triple_Tree "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
   269       Triple_Node "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
   254 
   270 
   255 text {*
   271 text {*
   256 Recursion may not be arbitrary; e.g. impossible to define
   272 Recursion may not be arbitrary; e.g. impossible to define
   257 *}
   273 *}
   258 
   274 
   351     datatype_new (set_: 'a) list_ =
   367     datatype_new (set_: 'a) list_ =
   352       null: Nil ("[]")
   368       null: Nil ("[]")
   353     | Cons (hd: 'a) (tl: "'a list_") (infixr "#" 65)
   369     | Cons (hd: 'a) (tl: "'a list_") (infixr "#" 65)
   354 
   370 
   355 
   371 
   356 subsection {* General Syntax
   372 subsection {* Syntax
   357   \label{ssec:datatype-general-syntax} *}
   373   \label{ssec:datatype-syntax} *}
   358 
   374 
   359 text {*
   375 text {*
   360 Datatype definitions have the following general syntax:
   376 Datatype definitions have the following general syntax:
   361 
   377 
   362 @{rail "
   378 @{rail "
   408 names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
   424 names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
   409 Inside a mutually recursive datatype specification, all defined datatypes must
   425 Inside a mutually recursive datatype specification, all defined datatypes must
   410 specify exactly the same type variables in the same order.
   426 specify exactly the same type variables in the same order.
   411 
   427 
   412 @{rail "
   428 @{rail "
   413   @{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} *) \\
   429   @{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} * ) \\
   414     @{syntax sel_defaults}? @{syntax mixfix}?
   430     @{syntax sel_defaults}? @{syntax mixfix}?
   415 "}
   431 "}
   416 
   432 
   417 \noindent
   433 \noindent
   418 The main constituents of a constructor specification is the name of the
   434 The main constituents of a constructor specification is the name of the
   442 associated with other constructors. The specified default value must have type
   458 associated with other constructors. The specified default value must have type
   443 @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"}
   459 @{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"}
   444 (i.e., it may dependend on @{text C}'s arguments).
   460 (i.e., it may dependend on @{text C}'s arguments).
   445 *}
   461 *}
   446 
   462 
   447 subsection {* Characteristic Theorems
   463 subsection {* Generated Theorems
   448   \label{ssec:datatype-characteristic-theorems} *}
   464   \label{ssec:datatype-generated-theorems} *}
   449 
   465 
   450 text {*
   466 text {*
   451   * free ctor theorems
   467   * free ctor theorems
   452     * case syntax
   468     * case syntax
   453 
   469 
   518 
   534 
   519 text {*
   535 text {*
   520 More examples in \verb|~~/src/HOL/BNF/Examples|.
   536 More examples in \verb|~~/src/HOL/BNF/Examples|.
   521 *}
   537 *}
   522 
   538 
   523 subsection {* Introductory Examples
   539 subsection {* Examples
   524   \label{ssec:primrec-introductory-examples} *}
   540   \label{ssec:primrec-examples} *}
   525 
   541 
   526 subsubsection {* Nonrecursive Types *}
   542 subsubsection {* Nonrecursive Types *}
   527 
   543 
   528 
   544 
   529 subsubsection {* Simple Recursion *}
   545 subsubsection {* Simple Recursion *}
   536 
   552 
   537 
   553 
   538 subsubsection {* Nested-as-Mutual Recursion *}
   554 subsubsection {* Nested-as-Mutual Recursion *}
   539 
   555 
   540 
   556 
   541 subsection {* General Syntax
   557 subsection {* Syntax
   542   \label{ssec:primrec-general-syntax} *}
   558   \label{ssec:primrec-syntax} *}
   543 
   559 
   544 text {*
   560 text {*
   545 
   561 Primitive recursive functions have the following general syntax:
   546 *}
   562 
   547 
   563 @{rail "
   548 subsection {* Characteristic Theorems
   564   @@{command primrec_new} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
   549   \label{ssec:primrec-characteristic-theorems} *}
   565     (@{syntax primrec_equation} + '|')
       
   566   ;
       
   567   @{syntax_def primrec_equation}: @{syntax thmdecl}? @{syntax prop}
       
   568 "}
       
   569 *}
       
   570 
       
   571 
       
   572 subsection {* Generated Theorems
       
   573   \label{ssec:primrec-generated-theorems} *}
   550 
   574 
   551 text {*
   575 text {*
   552   * synthesized nonrecursive definition
   576   * synthesized nonrecursive definition
   553   * user specification is rederived from it, exactly as entered
   577   * user specification is rederived from it, exactly as entered
   554 
   578 
   557     * without some needless induction hypotheses if not used
   581     * without some needless induction hypotheses if not used
   558   * fold, rec
   582   * fold, rec
   559     * mutualized
   583     * mutualized
   560 *}
   584 *}
   561 
   585 
   562 subsection {* Recursive Default Values
   586 subsection {* Recursive Default Values for Selectors
   563   \label{ssec:recursive-default-values} *}
   587   \label{ssec:recursive-default-values-for-selectors} *}
   564 
   588 
   565 text {*
   589 text {*
   566 A datatype selector @{text un_D} can have a default value for each constructor
   590 A datatype selector @{text un_D} can have a default value for each constructor
   567 on which it is not otherwise specified. Occasionally, it is useful to have the
   591 on which it is not otherwise specified. Occasionally, it is useful to have the
   568 default value be defined recursively. This produces a chicken-and-egg situation
   592 default value be defined recursively. This produces a chicken-and-egg situation
   643 This section describes how to specify codatatypes using the @{command codatatype}
   667 This section describes how to specify codatatypes using the @{command codatatype}
   644 command.
   668 command.
   645 *}
   669 *}
   646 
   670 
   647 
   671 
   648 subsection {* Introductory Examples
   672 subsection {* Examples
   649   \label{ssec:codatatype-introductory-examples} *}
   673   \label{ssec:codatatype-examples} *}
   650 
   674 
   651 text {*
   675 text {*
   652 More examples in \verb|~~/src/HOL/BNF/Examples|.
   676 More examples in \verb|~~/src/HOL/BNF/Examples|.
   653 *}
   677 *}
   654 
   678 
   655 
   679 
   656 subsection {* General Syntax
   680 subsection {* Syntax
   657   \label{ssec:codatatype-general-syntax} *}
   681   \label{ssec:codatatype-syntax} *}
   658 
   682 
   659 text {*
   683 text {*
   660 Definitions of codatatypes have almost exactly the same syntax as for datatypes
   684 Definitions of codatatypes have almost exactly the same syntax as for datatypes
   661 (Section~\ref{ssec:datatype-general-syntax}), with two exceptions: The command
   685 (Section~\ref{ssec:datatype-syntax}), with two exceptions: The command is called
   662 is called @{command codatatype}; the \keyw{no\_dests} option is not
   686 @{command codatatype}; the \keyw{no\_dests} option is not available, because
   663 available, because destructors are a central notion for codatatypes.
   687 destructors are a central notion for codatatypes.
   664 *}
   688 *}
   665 
   689 
   666 subsection {* Characteristic Theorems
   690 subsection {* Generated Theorems
   667   \label{ssec:codatatype-characteristic-theorems} *}
   691   \label{ssec:codatatype-generated-theorems} *}
   668 
   692 
   669 
   693 
   670 section {* Defining Corecursive Functions
   694 section {* Defining Corecursive Functions
   671   \label{sec:defining-corecursive-functions} *}
   695   \label{sec:defining-corecursive-functions} *}
   672 
   696 
   677 %%% TODO: partial_function? E.g. for defining tail recursive function on lazy
   701 %%% TODO: partial_function? E.g. for defining tail recursive function on lazy
   678 %%% lists (cf. terminal0 in TLList.thy)
   702 %%% lists (cf. terminal0 in TLList.thy)
   679 *}
   703 *}
   680 
   704 
   681 
   705 
   682 subsection {* Introductory Examples
   706 subsection {* Examples
   683   \label{ssec:primcorec-introductory-examples} *}
   707   \label{ssec:primcorec-examples} *}
   684 
   708 
   685 text {*
   709 text {*
   686 More examples in \verb|~~/src/HOL/BNF/Examples|.
   710 More examples in \verb|~~/src/HOL/BNF/Examples|.
   687 
   711 
   688 Also, for default values, the same trick as for datatypes is possible for
   712 Also, for default values, the same trick as for datatypes is possible for
   689 codatatypes (Section~\ref{ssec:recursive-default-values}).
   713 codatatypes (Section~\ref{ssec:recursive-default-values-for-selectors}).
   690 *}
   714 *}
   691 
   715 
   692 
   716 
   693 subsection {* General Syntax
   717 subsection {* Syntax
   694   \label{ssec:primcorec-general-syntax} *}
   718   \label{ssec:primcorec-syntax} *}
   695 
   719 
   696 
   720 text {*
   697 subsection {* Characteristic Theorems
   721 Primitive corecrusvie definitions have the following general syntax:
   698   \label{ssec:primcorec-characteristic-theorems} *}
   722 
       
   723 @{rail "
       
   724   @@{command primcorec} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
       
   725     (@{syntax primcorec_formula} + '|')
       
   726   ;
       
   727   @{syntax_def primcorec_formula}: @{syntax thmdecl}? @{syntax prop}
       
   728     (@'of' (@{syntax term} * ))?
       
   729 "}
       
   730 *}
       
   731 
       
   732 
       
   733 subsection {* Generated Theorems
       
   734   \label{ssec:primcorec-generated-theorems} *}
   699 
   735 
   700 
   736 
   701 section {* Registering Bounded Natural Functors
   737 section {* Registering Bounded Natural Functors
   702   \label{sec:registering-bounded-natural-functors} *}
   738   \label{sec:registering-bounded-natural-functors} *}
   703 
   739 
   709     * @{command bnf}
   745     * @{command bnf}
   710     * @{command print_bnfs}
   746     * @{command print_bnfs}
   711 *}
   747 *}
   712 
   748 
   713 
   749 
   714 subsection {* Introductory Example
   750 subsection {* Example
   715   \label{ssec:bnf-introductory-examples} *}
   751   \label{ssec:bnf-examples} *}
   716 
   752 
   717 text {*
   753 text {*
   718 More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
   754 More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
   719 \verb|~~/src/HOL/BNF/More_BNFs.thy|.
   755 \verb|~~/src/HOL/BNF/More_BNFs.thy|.
   720 
   756 
   721 Mention distinction between live and dead type arguments;
   757 Mention distinction between live and dead type arguments;
   722 mention =>.
   758 mention =>.
   723 *}
   759 *}
   724 
   760 
   725 
   761 
   726 subsection {* General Syntax
   762 subsection {* Syntax
   727   \label{ssec:bnf-general-syntax} *}
   763   \label{ssec:bnf-syntax} *}
   728 
   764 
   729 
   765 
   730 section {* Generating Free Constructor Theorems
   766 section {* Generating Free Constructor Theorems
   731   \label{sec:generating-free-constructor-theorems} *}
   767   \label{sec:generating-free-constructor-theorems} *}
   732 
   768 
   743   * @{command wrap_free_constructors}
   779   * @{command wrap_free_constructors}
   744     * \keyw{no\_dests}, \keyw{rep\_compat}
   780     * \keyw{no\_dests}, \keyw{rep\_compat}
   745 *}
   781 *}
   746 
   782 
   747 
   783 
   748 subsection {* Introductory Example
   784 subsection {* Example
   749   \label{ssec:ctors-introductory-examples} *}
   785   \label{ssec:ctors-examples} *}
   750 
   786 
   751 
   787 
   752 subsection {* General Syntax
   788 subsection {* Syntax
   753   \label{ssec:ctors-general-syntax} *}
   789   \label{ssec:ctors-syntax} *}
   754 
   790 
   755 
   791 
   756 subsection {* Characteristic Theorems
   792 subsection {* Generated Theorems
   757   \label{ssec:ctors-characteristic-theorems} *}
   793   \label{ssec:ctors-generated-theorems} *}
   758 
   794 
   759 
   795 
   760 section {* Standard ML Interface
   796 section {* Standard ML Interface
   761   \label{sec:standard-ml-interface} *}
   797   \label{sec:standard-ml-interface} *}
   762 
   798