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 *} |
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 |