src/Doc/Datatypes/Datatypes.thy
author blanchet
Wed, 14 Aug 2013 13:15:28 +0200
changeset 53028 a1e64c804c35
parent 53025 c820c9e9e8f4
child 53136 98a2c33d5d1b
permissions -rw-r--r--
more (co)datatype documentation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     1
(*  Title:      Doc/Datatypes/Datatypes.thy
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     3
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     4
Tutorial for (co)datatype definitions with the new package.
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     5
*)
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     6
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     7
theory Datatypes
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
     8
imports Setup
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
     9
keywords
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    10
  "primrec_new" :: thy_decl and
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    11
  "primcorec" :: thy_decl
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    12
begin
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    13
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    14
(*<*)
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    15
(* FIXME: Evil setup until "primrec_new" and "primcorec" are in place. *)
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    16
ML_command {*
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    17
fun add_dummy_cmd _ _ lthy = lthy;
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    18
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    19
val _ = Outer_Syntax.local_theory @{command_spec "primrec_new"} ""
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    20
  (Parse.fixes -- Parse_Spec.where_alt_specs >> uncurry add_dummy_cmd);
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    21
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    22
val _ = Outer_Syntax.local_theory @{command_spec "primcorec"} ""
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    23
  (Parse.fixes -- Parse_Spec.where_alt_specs >> uncurry add_dummy_cmd);
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    24
*}
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    25
(*>*)
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    26
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    27
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    28
section {* Introduction
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    29
  \label{sec:introduction} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    30
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    31
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    32
The 2013 edition of Isabelle introduced a new definitional package for freely
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    33
generated datatypes and codatatypes. The datatype support is similar to that
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    34
provided by the earlier package due to Berghofer and Wenzel
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    35
\cite{Berghofer-Wenzel:1999:TPHOL}, documented in the Isar reference manual
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    36
\cite{isabelle-isar-ref}; indeed, replacing the keyword @{command datatype} by
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    37
@{command datatype_new} is usually all that is needed to port existing theories
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    38
to use the new package.
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    39
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
    40
Perhaps the main advantage of the new package is that it supports recursion
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
    41
through a large class of non-datatypes, comprising finite sets:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    42
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    43
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    44
    datatype_new 'a tree\<^sub>f\<^sub>s = Node\<^sub>f\<^sub>s 'a "'a tree\<^sub>f\<^sub>s fset"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    45
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    46
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    47
\noindent
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    48
Another strong point is the support for local definitions:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    49
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    50
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    51
    context linorder
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    52
    begin
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
    53
    datatype_new flag = Less | Eq | Greater
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    54
    end
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    55
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    56
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    57
\noindent
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    58
The package also provides some convenience, notably automatically generated
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
    59
destructors (discriminators and selectors).
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    60
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    61
In addition to plain inductive datatypes, the new package supports coinductive
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    62
datatypes, or \emph{codatatypes}, which may have infinite values. For example,
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    63
the following command introduces the type of lazy lists, which comprises both
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    64
finite and infinite values:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    65
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    66
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    67
    codatatype 'a llist (*<*)(map: lmap) (*>*)= LNil | LCons 'a "'a llist"
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    68
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    69
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    70
\noindent
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    71
Mixed inductive--coinductive recursion is possible via nesting. Compare the
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    72
following four Rose tree examples:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    73
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    74
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    75
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    76
    locale dummy_tree
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    77
    begin
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    78
(*>*)
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    79
    datatype_new 'a tree\<^sub>f\<^sub>f = Node\<^sub>f\<^sub>f 'a "'a tree\<^sub>f\<^sub>f list"
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    80
    datatype_new 'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i 'a "'a tree\<^sub>f\<^sub>i llist"
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    81
    codatatype 'a tree\<^sub>i\<^sub>f = Node\<^sub>i\<^sub>f 'a "'a tree\<^sub>i\<^sub>f list"
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    82
    codatatype 'a tree\<^sub>i\<^sub>i = Node\<^sub>i\<^sub>i 'a "'a tree\<^sub>i\<^sub>i llist"
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    83
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    84
    end
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
    85
(*>*)
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    86
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    87
text {*
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    88
The first two tree types allow only finite branches, whereas the last two allow
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    89
branches of infinite length. Orthogonally, the nodes in the first and third
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    90
types have finite branching, whereas those of the second and fourth may have
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    91
infinitely many direct subtrees.
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    92
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    93
To use the package, it is necessary to import the @{theory BNF} theory, which
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
    94
can be precompiled into the \textit{HOL-BNF} image. The following commands show
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
    95
how to launch jEdit/PIDE with the image loaded and how to build the image
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
    96
without launching jEdit:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    97
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    98
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    99
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   100
\noindent
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   101
\ \ \ \ \texttt{isabelle jedit -l HOL-BNF} \\
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   102
\noindent
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   103
\hbox{}\ \ \ \ \texttt{isabelle build -b HOL-BNF}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   104
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   105
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   106
text {*
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   107
The package, like its predecessor, fully adheres to the LCF philosophy
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   108
\cite{mgordon79}: The characteristic theorems associated with the specified
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   109
(co)datatypes are derived rather than introduced axiomatically.%
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   110
\footnote{If the \textit{quick\_and\_dirty} option is enabled, some of the
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   111
internal constructions and most of the internal proof obligations are skipped.}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   112
The package's metatheory is described in a pair of papers
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   113
\cite{traytel-et-al-2012,blanchette-et-al-wit}.
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   114
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   115
This tutorial is organized as follows:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   116
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   117
\begin{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   118
\setlength{\itemsep}{0pt}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   119
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   120
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,''
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   121
describes how to specify datatypes using the @{command datatype_new} command.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   122
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   123
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   124
Functions,'' describes how to specify recursive functions using
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   125
\keyw{primrec\_new}, @{command fun}, and @{command function}.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   126
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   127
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,''
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   128
describes how to specify codatatypes using the @{command codatatype} command.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   129
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   130
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   131
Functions,'' describes how to specify corecursive functions using the
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   132
\keyw{primcorec} command.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   133
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   134
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   135
Bounded Natural Functors,'' explains how to set up the package to allow nested
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   136
recursion through custom well-behaved type constructors.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   137
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   138
\item Section \ref{sec:generating-free-constructor-theorems}, ``Generating Free
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   139
Constructor Theorems,'' explains how to derive convenience theorems for free
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   140
constructors, as performed internally by @{command datatype_new} and
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   141
@{command codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   142
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   143
\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   144
describes the package's programmatic interface.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   145
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   146
\item Section \ref{sec:interoperability}, ``Interoperability,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   147
is concerned with the packages' interaction with other Isabelle packages and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   148
tools, such as the code generator and the counterexample generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   149
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   150
\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   151
Limitations,'' concludes with known open issues at the time of writing.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   152
\end{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   153
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   154
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   155
\newbox\boxA
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   156
\setbox\boxA=\hbox{\texttt{nospam}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   157
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   158
\newcommand\authoremaili{\texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@\allowbreak
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   159
in.\allowbreak tum.\allowbreak de}}
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   160
\newcommand\authoremailii{\texttt{lore{\color{white}nospam}\kern-\wd\boxA{}nz.panny@\allowbreak
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   161
\allowbreak tum.\allowbreak de}}
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   162
\newcommand\authoremailiii{\texttt{pope{\color{white}nospam}\kern-\wd\boxA{}scua@\allowbreak
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   163
in.\allowbreak tum.\allowbreak de}}
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   164
\newcommand\authoremailiv{\texttt{tray{\color{white}nospam}\kern-\wd\boxA{}tel@\allowbreak
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   165
in.\allowbreak tum.\allowbreak de}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   166
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   167
The commands @{command datatype_new} and @{command primrec_new} are expected to
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   168
displace @{command datatype} and @{command primrec} in a future release. Authors
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   169
of new theories are encouraged to use the new commands, and maintainers of older
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   170
theories may want to consider upgrading.
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   171
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   172
Comments and bug reports concerning either the tool or this tutorial should be
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   173
directed to the authors at \authoremaili, \authoremailii, \authoremailiii,
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   174
and \authoremailiv.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   175
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   176
\begin{framed}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   177
\noindent
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   178
\textbf{Warning:} This tutorial is under heavy construction. Please apologise
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   179
for its appearance. If you have ideas regarding material that should be
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   180
included, please let the authors know.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   181
\end{framed}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   182
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   183
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   184
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   185
section {* Defining Datatypes
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   186
  \label{sec:defining-datatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   187
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   188
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   189
This section describes how to specify datatypes using the @{command datatype_new}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   190
command. The command is first illustrated through concrete examples featuring
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   191
different flavors of recursion. More examples can be found in the directory
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   192
\verb|~~/src/HOL/BNF/Examples|.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   193
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   194
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   195
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   196
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   197
  \label{ssec:datatype-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   198
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   199
subsubsection {* Nonrecursive Types *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   200
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   201
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   202
Datatypes are introduced by specifying the desired names and argument types for
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   203
their constructors. \emph{Enumeration types} are the simplest form of datatype:
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   204
All their constructors are nullary:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   205
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   206
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   207
    datatype_new trool = Truue | Faalse | Perhaaps
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   208
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   209
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   210
\noindent
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   211
All three constructors have the type @{typ trool}.
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   212
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   213
Polymorphic types are possible, such as the following option type, modeled after
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   214
its homologue from the @{theory Option} theory:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   215
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   216
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   217
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   218
    hide_const None Some
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   219
(*>*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   220
    datatype_new 'a option = None | Some 'a
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   221
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   222
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   223
\noindent
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   224
The constructors are @{term "None :: 'a option"} and
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   225
@{term "Some :: 'a \<Rightarrow> 'a option"}.
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   226
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   227
The next example has three type parameters:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   228
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   229
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   230
    datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   231
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   232
text {*
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   233
\noindent
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   234
The constructor is
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   235
@{term "Triple :: 'a \<Rightarrow> 'b \<Rightarrow> 'c \<Rightarrow> ('a, 'b, 'c) triple"}.
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   236
Unlike in Standard ML, curried constructors are supported. The uncurried variant
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   237
is also possible:
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   238
*}
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   239
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   240
    datatype_new ('a, 'b, 'c) triple\<^sub>u = Triple\<^sub>u "'a * 'b * 'c"
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   241
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   242
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   243
subsubsection {* Simple Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   244
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   245
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   246
simplest recursive type: copy of the natural numbers:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   247
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   248
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   249
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   250
    (* FIXME: remove "rep_compat" once "datatype_new" is integrated with "fun" *)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   251
(*>*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   252
    datatype_new (*<*)(rep_compat) (*>*)nat = Zero | Suc nat
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   253
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   254
text {*
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   255
lists were shown in the introduction; terminated lists are a variant:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   256
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   257
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   258
    datatype_new ('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   259
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   260
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   261
On the right-hand side of the equal sign, the usual Isabelle conventions apply:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   262
Nonatomic types must be enclosed in double quotes.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   263
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   264
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   265
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   266
subsubsection {* Mutual Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   267
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   268
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   269
Mutual recursion = Define several types simultaneously, referring to each other.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   270
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   271
Simple example: distinction between even and odd natural numbers:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   272
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   273
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   274
    datatype_new enat = EZero | ESuc onat
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   275
    and onat = OSuc enat
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   276
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   277
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   278
More complex, and more realistic, example:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   279
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   280
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   281
    datatype_new ('a, 'b) exp =
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   282
      Term "('a, 'b) trm" | Sum "('a, 'b) trm" "('a, 'b) exp"
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   283
    and ('a, 'b) trm =
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   284
      Factor "('a, 'b) fct" | Prod "('a, 'b) fct" "('a, 'b) trm"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   285
    and ('a, 'b) fct =
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   286
      Const 'a | Var 'b | Expr "('a, 'b) exp"
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   287
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   288
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   289
subsubsection {* Nested Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   290
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   291
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   292
Nested recursion = Have recursion through a type constructor.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   293
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   294
The introduction showed some examples of trees with nesting through lists.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   295
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   296
More complex example, which reuses our option type:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   297
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   298
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   299
    datatype_new 'a btree =
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   300
      BNode 'a "'a btree option" "'a btree option"
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   301
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   302
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   303
Recursion may not be arbitrary; e.g. impossible to define
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   304
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   305
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   306
    datatype_new 'a evil = Evil (*<*)'a
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   307
    typ (*>*)"'a evil \<Rightarrow> 'a evil"
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   308
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   309
text {*
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   310
Issue: => allows recursion only on its right-hand side.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   311
This issue is inherited by polymorphic datatypes (and codatatypes)
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   312
defined in terms of =>.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   313
In general, type constructors "('a1, ..., 'an) k" allow recursion on a subset
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   314
of their type arguments.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   315
*}
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   316
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   317
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   318
subsubsection {* Auxiliary Constants and Syntaxes *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   319
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   320
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   321
The @{command datatype_new} command introduces various constants in addition to the
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   322
constructors. Given a type @{text "('a1,\<dots>,'aM) t"} with constructors
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   323
@{text "t.C\<^sub>1"}, \ldots, @{text "t.C\<^sub>m"}, the following auxiliary
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   324
constants are introduced (among others):
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   325
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   326
\begin{itemize}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   327
\setlength{\itemsep}{0pt}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   328
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   329
\item \emph{Set functions} (\emph{natural transformations}):
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   330
@{text t_set1}, \ldots, @{text t_setM}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   331
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   332
\item \emph{Map function} (\emph{functorial action}): @{text t_map}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   333
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   334
\item \emph{Relator}: @{text t_rel}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   335
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   336
\item \emph{Iterator}: @{text t_fold}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   337
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   338
\item \emph{Recursor}: @{text t_rec}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   339
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   340
\item \emph{Discriminators}: @{text "t.is_C\<^sub>1"}, \ldots,
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   341
@{text "t.is_C\<^sub>m"}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   342
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   343
\item \emph{Selectors}:
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   344
@{text t.un_C}$_{11}$, \ldots, @{text t.un_C}$_{1n_1}$, \ldots,
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   345
@{text t.un_C}$_{m1}$, \ldots, @{text t.un_C}$_{mn_m}$
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   346
\end{itemize}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   347
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   348
The discriminators and selectors are collectively called \emph{destructors}. The
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   349
@{text "t."} prefix is an optional component of the name and is normally hidden.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   350
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   351
The set functions, map function, relator, discriminators, and selectors can be
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   352
given custom names, as in the example below:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   353
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   354
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   355
(*<*)
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   356
    no_translations
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   357
      "[x, xs]" == "x # [xs]"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   358
      "[x]" == "x # []"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   359
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   360
    no_notation
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   361
      Nil ("[]") and
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   362
      Cons (infixr "#" 65)
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   363
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   364
    hide_const Nil Cons hd tl map
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   365
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   366
    locale dummy_list
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   367
    begin
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   368
(*>*)
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   369
    datatype_new (set: 'a) list (map: map rel: list_all2) =
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   370
      null: Nil (defaults tl: Nil)
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   371
    | Cons (hd: 'a) (tl: "'a list")
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   372
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   373
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   374
\noindent
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   375
The command introduces a discriminator @{const null} and a pair of selectors
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   376
@{const hd} and @{const tl} characterized as follows:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   377
%
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   378
\[@{thm list.collapse(1)[of xs, no_vars]}
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   379
  \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   380
%
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   381
For two-constructor datatypes, a single discriminator constant suffices. The
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   382
discriminator associated with @{const Cons} is simply @{text "\<not> null"}.
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   383
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   384
The \keyw{defaults} keyword following the @{const Nil} constructor specifies a
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   385
default value for selectors associated with other constructors. Here, it is
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   386
used to specify that the tail of the empty list is the empty list (instead of
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   387
being unspecified).
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   388
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   389
Because @{const Nil} is a nullary constructor, it is also possible to use @{text
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   390
"= Nil"} as a discriminator. This is specified by specifying @{text "="} instead
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   391
of the identifier @{const null} in the declaration above. Although this may look
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   392
appealing, the mixture of constructors and selectors in the resulting
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   393
characteristic theorems can lead Isabelle's automation to switch between the
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   394
constructor and the destructor view in surprising ways.
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   395
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   396
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   397
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   398
The usual mixfix syntaxes are available for both types and constructors. For example:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   399
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   400
%%% FIXME: remove trailing underscore and use locale trick instead once this is
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   401
%%% supported.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   402
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   403
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   404
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   405
    end
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   406
(*>*)
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   407
    datatype_new ('a, 'b) prod (infixr "*" 20) =
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   408
      Pair 'a 'b
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   409
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   410
    datatype_new (set: 'a) list (map: map rel: list_all2) =
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   411
      null: Nil ("[]")
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   412
    | Cons (hd: 'a) (tl: "'a list") (infixr "#" 65)
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   413
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   414
text {*
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   415
Incidentally, this is how the traditional syntaxes can be set up:
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   416
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   417
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   418
    syntax "_list" :: "args \<Rightarrow> 'a list" ("[(_)]")
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   419
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   420
    translations
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   421
      "[x, xs]" == "x # [xs]"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   422
      "[x]" == "x # []"
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   423
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   424
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   425
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   426
  \label{ssec:datatype-syntax} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   427
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   428
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   429
Datatype definitions have the following general syntax:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   430
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   431
@{rail "
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   432
  @@{command datatype_new} @{syntax target}? @{syntax dt_options}? \\
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   433
    (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   434
  ;
52969
f2df0730f8ac clarified option name (since case/fold/rec are also destructors)
blanchet
parents: 52868
diff changeset
   435
  @{syntax_def dt_options}: '(' ((@'no_discs_sels' | @'rep_compat') + ',') ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   436
"}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   437
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   438
The syntactic quantity @{syntax target} can be used to specify a local context
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   439
(e.g., @{text "(in linorder)"}). It is documented in the Isar reference manual
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   440
\cite{isabelle-isar-ref}.
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   441
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   442
The optional target is followed by optional options:
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   443
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   444
\begin{itemize}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   445
\setlength{\itemsep}{0pt}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   446
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   447
\item
52969
f2df0730f8ac clarified option name (since case/fold/rec are also destructors)
blanchet
parents: 52868
diff changeset
   448
The \keyw{no\_discs\_sels} option indicates that no destructors (i.e.,
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   449
discriminators and selectors) should be generated.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   450
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   451
\item
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   452
The \keyw{rep\_compat} option indicates that the names generated by the
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   453
package should contain optional (and normally not displayed) @{text "new."}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   454
components to prevent clashes with a later call to @{command rep_datatype}. See
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   455
Section~\ref{ssec:datatype-compatibility-issues} for details.
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   456
\end{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   457
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   458
The left-hand sides of the datatype equations specify the name of the type to
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   459
define, its type parameters, and optional additional information:
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   460
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   461
@{rail "
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   462
  @{syntax_def dt_name}: @{syntax tyargs}? @{syntax name}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   463
    @{syntax map_rel}? @{syntax mixfix}?
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   464
  ;
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   465
  @{syntax_def tyargs}: @{syntax typefree} | '(' ((@{syntax name} ':')? @{syntax typefree} + ',') ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   466
  ;
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   467
  @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' @{syntax name}) +) ')'
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   468
"}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   469
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   470
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   471
The syntactic quantity @{syntax name} denotes an identifier, @{syntax typefree}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   472
denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and @{syntax
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   473
mixfix} denotes the usual parenthesized mixfix notation. They are documented in
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   474
the Isar reference manual \cite{isabelle-isar-ref}.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   475
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   476
The optional names preceding the type variables allow to override the default
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   477
names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   478
Inside a mutually recursive datatype specification, all defined datatypes must
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   479
specify exactly the same type variables in the same order.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   480
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   481
@{rail "
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   482
  @{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} * ) \\
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   483
    @{syntax dt_sel_defaults}? @{syntax mixfix}?
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   484
"}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   485
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   486
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   487
The main constituents of a constructor specification is the name of the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   488
constructor and the list of its argument types. An optional discriminator name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   489
can be supplied at the front to override the default name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   490
(@{text t.un_C}$_{ij}$).
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   491
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   492
@{rail "
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   493
  @{syntax_def ctor_arg}: @{syntax type} | '(' @{syntax name} ':' @{syntax type} ')'
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   494
"}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   495
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   496
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   497
In addition to the type of a constructor argument, it is possible to specify a
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   498
name for the corresponding selector to override the default name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   499
(@{text t.un_C}$_{ij}$). The same selector names can be reused for several
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   500
constructors as long as they have the same type.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   501
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   502
@{rail "
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   503
  @{syntax_def dt_sel_defaults}: '(' @'defaults' (@{syntax name} ':' @{syntax term} +) ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   504
"}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   505
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   506
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   507
Given a constructor
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   508
@{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   509
default values can be specified for any selector
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   510
@{text "un_D \<Colon> \<sigma> \<Rightarrow> \<tau>"}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   511
associated with other constructors. The specified default value must have type
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   512
@{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   513
(i.e., it may dependend on @{text C}'s arguments).
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   514
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   515
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   516
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   517
  \label{ssec:datatype-generated-theorems} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   518
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   519
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   520
  * free ctor theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   521
    * case syntax
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   522
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   523
  * per-type theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   524
    * sets, map, rel
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   525
    * induct, fold, rec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   526
    * simps
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   527
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   528
  * multi-type (``common'') theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   529
    * induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   530
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   531
  * mention what is registered with which attribute
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   532
    * and also nameless safes
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   533
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   534
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   535
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   536
subsection {* Compatibility Issues
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   537
  \label{ssec:datatype-compatibility-issues} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   538
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   539
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   540
  * main incompabilities between two packages
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   541
    * differences in theorem names (e.g. singular vs. plural for some props?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   542
    * differences in "simps"?
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   543
    * different recursor/induction in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   544
        * discussed in Section~\ref{sec:defining-recursive-functions}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   545
    * different ML interfaces / extension mechanisms
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   546
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   547
  * register new datatype as old datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   548
    * motivation:
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   549
      * do recursion through new datatype in old datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   550
        (e.g. useful when porting to the new package one type at a time)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   551
      * use old primrec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   552
      * use fun
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   553
      * use extensions like size (needed for fun), the AFP order, Quickcheck,
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   554
        Nitpick
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   555
      * provide exactly the same theorems with the same names (compatibility)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   556
    * option 1
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   557
      * \keyw{rep\_compat}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   558
      * \keyw{rep\_datatype}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   559
      * has some limitations
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   560
        * mutually recursive datatypes? (fails with rep\_datatype?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   561
        * nested datatypes? (fails with datatype\_new?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   562
    * option 2
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   563
      * \keyw{datatype\_compat}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   564
      * not fully implemented yet?
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   565
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   566
  * register old datatype as new datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   567
    * no clean way yet
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   568
    * if the goal is to do recursion through old datatypes, can register it as
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   569
      a BNF (Section XXX)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   570
    * can also derive destructors etc. using @{command wrap_free_constructors}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   571
      (Section XXX)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   572
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   573
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   574
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   575
section {* Defining Recursive Functions
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   576
  \label{sec:defining-recursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   577
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   578
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   579
This describes how to specify recursive functions over datatypes
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   580
specified using @{command datatype_new}. The focus in on the \keyw{primrec\_new}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   581
command, which supports primitive recursion. A few examples feature the
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   582
@{command fun} and @{command function} commands, described in a separate
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   583
tutorial \cite{isabelle-function}.
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   584
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   585
%%% TODO: partial_function?
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   586
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   587
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   588
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   589
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   590
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   591
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   592
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   593
  \label{ssec:primrec-examples} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   594
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   595
subsubsection {* Nonrecursive Types *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   596
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   597
text {*
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   598
  * simple (depth 1) pattern matching on the left-hand side
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   599
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   600
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   601
    primrec_new bool_of_trool :: "trool \<Rightarrow> bool" where
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   602
      "bool_of_trool Faalse = False" |
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   603
      "bool_of_trool Truue = True"
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   604
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   605
text {*
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   606
  * OK to specify the cases in a different order
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   607
  * OK to leave out some case (but get a warning -- maybe we need a "quiet"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   608
    or "silent" flag?)
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   609
    * case is then unspecified
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   610
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   611
More examples:
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   612
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   613
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   614
    primrec_new the_list :: "'a option \<Rightarrow> 'a list" where
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   615
      "the_list None = []" |
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   616
      "the_list (Some a) = [a]"
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   617
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   618
    primrec_new the_default :: "'a \<Rightarrow> 'a option \<Rightarrow> 'a" where
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   619
      "the_default d None = d" |
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   620
      "the_default _ (Some a) = a"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   621
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   622
    primrec_new mirrror :: "('a, 'b, 'c) triple \<Rightarrow> ('c, 'b, 'a) triple" where
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   623
      "mirrror (Triple a b c) = Triple c b a"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   624
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   625
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   626
subsubsection {* Simple Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   627
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   628
text {*
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   629
again, simple pattern matching on left-hand side, but possibility
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   630
to call a function recursively on an argument to a constructor:
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   631
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   632
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   633
    primrec_new replicate :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list" where
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   634
      "rep Zero _ = []" |
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   635
      "rep (Suc n) a = a # rep n a"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   636
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   637
text {*
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   638
we don't like the confusing name @{const nth}:
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   639
*}
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   640
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   641
    primrec_new at :: "'a list \<Rightarrow> nat \<Rightarrow> 'a" where
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   642
      "at (a # as) j =
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   643
         (case j of
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   644
            Zero \<Rightarrow> a
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   645
          | Suc j' \<Rightarrow> at as j')"
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   646
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   647
    primrec_new tfold :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> ('a, 'b) tlist \<Rightarrow> 'b" where
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   648
      "tfold _ (TNil b) = b" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   649
      "tfold f (TCons a as) = f a (tfold f as)"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   650
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   651
text {*
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   652
Show one example where fun is needed.
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   653
*}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   654
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   655
subsubsection {* Mutual Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   656
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   657
text {*
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   658
E.g., converting even/odd naturals to plain old naturals:
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   659
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   660
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   661
    primrec_new
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   662
      nat_of_enat :: "enat \<Rightarrow> nat" and
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   663
      nat_of_onat :: "onat => nat"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   664
    where
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   665
      "nat_of_enat EZero = Zero" |
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   666
      "nat_of_enat (ESuc n) = Suc (nat_of_onat n)" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   667
      "nat_of_onat (OSuc n) = Suc (nat_of_enat n)"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   668
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   669
text {*
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   670
Mutual recursion is be possible within a single type, but currently only using fun:
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   671
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   672
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   673
(*<*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   674
    (* FIXME: remove hack once "primrec_new" is in place *)
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   675
    rep_datatype Zero Suc
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   676
    by (erule nat.induct, assumption) auto
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   677
(*>*)
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   678
    fun
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   679
      even :: "nat \<Rightarrow> bool" and
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   680
      odd :: "nat \<Rightarrow> bool"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   681
    where
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   682
      "even Zero = True" |
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   683
      "even (Suc n) = odd n" |
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   684
      "odd Zero = False" |
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   685
      "odd (Suc n) = even n"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   686
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   687
text {*
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   688
More elaborate example that works with primrec_new:
52841
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   689
*}
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   690
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   691
    primrec_new
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   692
      eval\<^sub>e :: "('a, 'b) exp \<Rightarrow> real" and
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   693
      eval\<^sub>t :: "('a, 'b) trm \<Rightarrow> real" and
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   694
      eval\<^sub>f :: "('a, 'b) fct \<Rightarrow> real"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   695
    where
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   696
      "eval\<^sub>e \<gamma> \<xi> (Term t) = eval\<^sub>t \<gamma> \<xi> t" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   697
      "eval\<^sub>e \<gamma> \<xi> (Sum t e) = eval\<^sub>t \<gamma> \<xi> t + eval\<^sub>e \<gamma> \<xi> e" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   698
      "eval\<^sub>t \<gamma> \<xi> (Factor f) = eval\<^sub>f \<gamma> \<xi> f)" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   699
      "eval\<^sub>t \<gamma> \<xi> (Prod f t) = eval\<^sub>f \<gamma> \<xi> f + eval\<^sub>t \<gamma> \<xi> t" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   700
      "eval\<^sub>f \<gamma> _ (Const a) = \<gamma> a" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   701
      "eval\<^sub>f _ \<xi> (Var b) = \<xi> b" |
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   702
      "eval\<^sub>f \<gamma> \<xi> (Expr e) = eval\<^sub>e \<gamma> \<xi> e"
87a66bad0796 more (co)datatype documentation
blanchet
parents: 52840
diff changeset
   703
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   704
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   705
subsubsection {* Nested Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   706
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   707
(*<*)
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   708
    datatype_new 'a tree\<^sub>f\<^sub>f = Node\<^sub>f\<^sub>f 'a "'a tree\<^sub>f\<^sub>f list"
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   709
    datatype_new 'a tree\<^sub>f\<^sub>i = Node\<^sub>f\<^sub>i 'a "'a tree\<^sub>f\<^sub>i llist"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   710
(*>*)
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   711
    primrec_new at\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" where
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   712
      "at\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js =
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   713
         (case js of
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   714
            [] \<Rightarrow> a
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   715
          | j # js' \<Rightarrow> at (map (\<lambda>t. at\<^sub>f\<^sub>f t js') ts) j)"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   716
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   717
    primrec_new at\<^sub>f\<^sub>i :: "'a tree\<^sub>f\<^sub>i \<Rightarrow> nat list \<Rightarrow> 'a" where
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   718
      "at\<^sub>f\<^sub>i (Node\<^sub>f\<^sub>i a ts) js =
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   719
         (case js of
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   720
            [] \<Rightarrow> a
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   721
          | j # js' \<Rightarrow> at (lmap (\<lambda>t. at\<^sub>f\<^sub>i t js') ts) j)"
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   722
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   723
text {*
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   724
Explain @{const lmap}.
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   725
*}
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   726
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   727
    primrec_new sum_btree :: "('a\<Colon>plus) btree \<Rightarrow> 'a" where
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   728
      "sum_btree (BNode a lt rt) =
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   729
         a + the_default Zero (option_map sum_btree lt) +
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   730
           the_default Zero (option_map sum_btree rt)"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   731
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   732
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   733
subsubsection {* Nested-as-Mutual Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   734
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   735
text {*
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   736
  * can pretend a nested type is mutually recursive
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   737
  * avoids the higher-order map
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   738
  * e.g.
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   739
*}
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   740
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   741
    primrec_new
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   742
      at_tree\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f \<Rightarrow> nat list \<Rightarrow> 'a" and
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   743
      at_trees\<^sub>f\<^sub>f :: "'a tree\<^sub>f\<^sub>f list \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> 'a"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   744
    where
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   745
      "at_tree\<^sub>f\<^sub>f (Node\<^sub>f\<^sub>f a ts) js =
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   746
         (case js of
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   747
            [] \<Rightarrow> a
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   748
          | j # js' \<Rightarrow> at_trees\<^sub>f\<^sub>f ts j js')" |
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   749
      "at_trees\<^sub>f\<^sub>f (t # ts) j =
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   750
         (case j of
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   751
            Zero \<Rightarrow> at_tree\<^sub>f\<^sub>f t
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   752
          | Suc j' \<Rightarrow> at_trees\<^sub>f\<^sub>f ts j')"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   753
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   754
    primrec_new
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   755
      sum_btree :: "('a\<Colon>plus) btree \<Rightarrow> 'a" and
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   756
      sum_btree_option :: "('a\<Colon>plus) btree option \<Rightarrow> 'a"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   757
    where
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   758
      "sum_btree (BNode a lt rt) =
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   759
         a + sum_btree_option lt + sum_btree_option rt" |
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   760
      "sum_btree_option None = Zero" |
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
   761
      "sum_btree_option (Some t) = sum_btree t"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   762
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   763
text {*
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   764
  * this can always be avoided;
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   765
     * e.g. in our previous example, we first mapped the recursive
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   766
       calls, then we used a generic at function to retrieve the result
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   767
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   768
  * there's no hard-and-fast rule of when to use one or the other,
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   769
    just like there's no rule when to use fold and when to use
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   770
    primrec_new
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   771
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   772
  * higher-order approach, considering nesting as nesting, is more
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   773
    compositional -- e.g. we saw how we could reuse an existing polymorphic
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   774
    at or the_default, whereas @{text at_trees\<^sub>f\<^sub>f} is much more specific
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   775
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   776
  * but:
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   777
     * is perhaps less intuitive, because it requires higher-order thinking
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   778
     * may seem inefficient, and indeed with the code generator the
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   779
       mutually recursive version might be nicer
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   780
     * is somewhat indirect -- must apply a map first, then compute a result
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   781
       (cannot mix)
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   782
     * the auxiliary functions like @{text at_trees\<^sub>f\<^sub>f} are sometimes useful in own right
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   783
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   784
  * impact on automation unclear
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   785
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
   786
%%% TODO: change text antiquotation to const once the real primrec is used
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   787
*}
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   788
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   789
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   790
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   791
  \label{ssec:primrec-syntax} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   792
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   793
text {*
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   794
Primitive recursive functions have the following general syntax:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   795
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   796
@{rail "
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   797
  @@{command primrec_new} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   798
    (@{syntax primrec_equation} + '|')
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   799
  ;
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   800
  @{syntax_def primrec_equation}: @{syntax thmdecl}? @{syntax prop}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   801
"}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   802
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   803
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   804
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   805
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   806
  \label{ssec:primrec-generated-theorems} *}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   807
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   808
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   809
  * synthesized nonrecursive definition
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   810
  * user specification is rederived from it, exactly as entered
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   811
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   812
  * induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   813
    * mutualized
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   814
    * without some needless induction hypotheses if not used
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   815
  * fold, rec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   816
    * mutualized
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   817
*}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   818
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   819
subsection {* Recursive Default Values for Selectors
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   820
  \label{ssec:recursive-default-values-for-selectors} *}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   821
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   822
text {*
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   823
A datatype selector @{text un_D} can have a default value for each constructor
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   824
on which it is not otherwise specified. Occasionally, it is useful to have the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   825
default value be defined recursively. This produces a chicken-and-egg situation
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   826
that appears unsolvable, because the datatype is not introduced yet at the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   827
moment when the selectors are introduced. Of course, we can always define the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   828
selectors manually afterward, but we then have to state and prove all the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   829
characteristic theorems ourselves instead of letting the package do it.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   830
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   831
Fortunately, there is a fairly elegant workaround that relies on overloading and
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   832
that avoids the tedium of manual derivations:
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   833
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   834
\begin{enumerate}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   835
\setlength{\itemsep}{0pt}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   836
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   837
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   838
Introduce a fully unspecified constant @{text "un_D\<^sub>0 \<Colon> 'a"} using
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   839
@{keyword consts}.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   840
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   841
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   842
Define the datatype, specifying @{text "un_D\<^sub>0"} as the selector's default value.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   843
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   844
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   845
Define the behavior of @{text "un_D\<^sub>0"} on values of the newly introduced datatype
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   846
using the @{command overloading} command.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   847
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   848
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   849
Derive the desired equation on @{text un_D} from the characteristic equations
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   850
for @{text "un_D\<^sub>0"}.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   851
\end{enumerate}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   852
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   853
The following example illustrates this procedure:
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   854
*}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   855
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   856
    consts termi\<^sub>0 :: 'a
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   857
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   858
    datatype_new (*<*)(rep_compat) (*>*)('a, 'b) tlist_ =
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   859
      TNil (termi: 'b) (defaults ttl: TNil)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   860
    | TCons (thd: 'a) (ttl : "('a, 'b) tlist_") (defaults termi: "\<lambda>_ xs. termi\<^sub>0 xs")
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   861
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   862
(*<*)
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   863
    (* FIXME: remove hack once "primrec_new" is in place *)
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   864
    rep_datatype TNil TCons
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   865
    by (erule tlist_.induct, assumption) auto
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   866
(*>*)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   867
    overloading
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   868
      termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist_ \<Rightarrow> 'b"
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   869
    begin
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   870
(*<*)
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   871
    (* FIXME: remove hack once "primrec_new" is in place *)
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   872
    fun termi\<^sub>0 :: "('a, 'b) tlist_ \<Rightarrow> 'b" where
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   873
    "termi\<^sub>0 (TNil y) = y" |
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   874
    "termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs"
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   875
(*>*)
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   876
    primrec_new termi\<^sub>0 :: "('a, 'b) tlist_ \<Rightarrow> 'b" where
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   877
    "termi\<^sub>0 (TNil y) = y" |
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   878
    "termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs"
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   879
    end
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   880
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   881
    lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs"
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   882
    by (cases xs) auto
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   883
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   884
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   885
subsection {* Compatibility Issues
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   886
  \label{ssec:datatype-compatibility-issues} *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   887
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   888
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   889
  * different induction in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   890
    * solution: define nested-as-mutual functions with primrec,
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   891
      and look at .induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   892
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   893
  * different induction and recursor in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   894
    * only matters to low-level users; they can define a dummy function to force
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   895
      generation of mutualized recursor
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   896
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   897
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   898
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   899
section {* Defining Codatatypes
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   900
  \label{sec:defining-codatatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   901
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   902
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   903
This section describes how to specify codatatypes using the @{command codatatype}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   904
command.
52843
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   905
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   906
  * libraries include some useful codatatypes, notably lazy lists;
ea95702328cf more (co)datatype docs
blanchet
parents: 52841
diff changeset
   907
    see the ``Coinductive'' AFP entry \cite{xxx} for an elaborate library
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   908
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   909
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   910
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   911
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   912
  \label{ssec:codatatype-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   913
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   914
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   915
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   916
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   917
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   918
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   919
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   920
  \label{ssec:codatatype-syntax} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   921
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   922
text {*
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   923
Definitions of codatatypes have almost exactly the same syntax as for datatypes
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   924
(Section~\ref{ssec:datatype-syntax}), with two exceptions: The command is called
52969
f2df0730f8ac clarified option name (since case/fold/rec are also destructors)
blanchet
parents: 52868
diff changeset
   925
@{command codatatype}; the \keyw{no\_discs\_sels} option is not available, because
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   926
destructors are a central notion for codatatypes.
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   927
*}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   928
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   929
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   930
  \label{ssec:codatatype-generated-theorems} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   931
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   932
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   933
section {* Defining Corecursive Functions
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   934
  \label{sec:defining-corecursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   935
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   936
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   937
This section describes how to specify corecursive functions using the
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   938
\keyw{primcorec} command.
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   939
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   940
%%% TODO: partial_function? E.g. for defining tail recursive function on lazy
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   941
%%% lists (cf. terminal0 in TLList.thy)
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   942
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   943
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   944
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   945
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   946
  \label{ssec:primcorec-examples} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   947
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   948
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   949
More examples in \verb|~~/src/HOL/BNF/Examples|.
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   950
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   951
Also, for default values, the same trick as for datatypes is possible for
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   952
codatatypes (Section~\ref{ssec:recursive-default-values-for-selectors}).
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   953
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   954
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   955
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   956
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   957
  \label{ssec:primcorec-syntax} *}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   958
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   959
text {*
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
   960
Primitive corecursive definitions have the following general syntax:
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   961
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   962
@{rail "
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   963
  @@{command primcorec} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   964
    (@{syntax primcorec_formula} + '|')
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   965
  ;
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   966
  @{syntax_def primcorec_formula}: @{syntax thmdecl}? @{syntax prop}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   967
    (@'of' (@{syntax term} * ))?
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   968
"}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   969
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   970
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   971
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   972
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   973
  \label{ssec:primcorec-generated-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   974
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   975
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   976
section {* Registering Bounded Natural Functors
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   977
  \label{sec:registering-bounded-natural-functors} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   978
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   979
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   980
This section explains how to set up the (co)datatype package to allow nested
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   981
recursion through custom well-behaved type constructors. The key concept is that
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   982
of a bounded natural functor (BNF).
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   983
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   984
    * @{command bnf}
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   985
    * @{command print_bnfs}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   986
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   987
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   988
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   989
subsection {* Example
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   990
  \label{ssec:bnf-examples} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   991
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   992
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   993
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   994
\verb|~~/src/HOL/BNF/More_BNFs.thy|.
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   995
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   996
Mention distinction between live and dead type arguments;
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   997
mention =>.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   998
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   999
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1000
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1001
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1002
  \label{ssec:bnf-syntax} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1003
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1004
text {*
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1005
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1006
@{rail "
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1007
  @@{command bnf} @{syntax target}? (@{syntax name} ':')? @{syntax term} \\
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1008
    @{syntax term_list} @{syntax term} @{syntax term_list} @{syntax term}?
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1009
  ;
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1010
  @{syntax_def X_list}: '[' (@{syntax X} + ',') ']'
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1011
"}
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1012
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1013
options: no_discs_sels rep_compat
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1014
*}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1015
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
  1016
section {* Generating Free Constructor Theorems
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1017
  \label{sec:generating-free-constructor-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1018
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1019
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1020
This section explains how to derive convenience theorems for free constructors,
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1021
as performed internally by @{command datatype_new} and @{command codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1022
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1023
  * need for this is rare but may arise if you want e.g. to add destructors to
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1024
    a type not introduced by ...
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1025
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1026
  * also useful for compatibility with old package, e.g. add destructors to
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1027
    old @{command datatype}
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1028
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1029
  * @{command wrap_free_constructors}
52969
f2df0730f8ac clarified option name (since case/fold/rec are also destructors)
blanchet
parents: 52868
diff changeset
  1030
    * \keyw{no\_discs\_sels}, \keyw{rep\_compat}
f2df0730f8ac clarified option name (since case/fold/rec are also destructors)
blanchet
parents: 52868
diff changeset
  1031
    * hack to have both co and nonco view via locale (cf. ext nats)
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1032
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
  1033
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1034
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1035
subsection {* Example
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1036
  \label{ssec:ctors-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1037
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1038
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1039
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1040
  \label{ssec:ctors-syntax} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1041
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1042
text {*
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1043
Free constructor wrapping has the following general syntax:
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1044
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1045
@{rail "
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1046
  @@{command wrap_free_constructors} @{syntax target}? @{syntax dt_options} \\
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1047
    @{syntax term_list} @{syntax name} @{syntax fc_discs_sels}?
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1048
  ;
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1049
  @{syntax_def fc_discs_sels}: @{syntax name_list} (@{syntax name_list_list} @{syntax name_term_list_list}? )?
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1050
  ;
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1051
  @{syntax_def name_term}: (@{syntax name} ':' @{syntax term})
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1052
"}
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1053
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1054
options: no_discs_sels rep_compat
53028
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1055
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1056
X_list is as for BNF
a1e64c804c35 more (co)datatype documentation
blanchet
parents: 53025
diff changeset
  1057
53018
11ebef554439 added rail diagram
blanchet
parents: 52969
diff changeset
  1058
*}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1059
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1060
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
  1061
  \label{ssec:ctors-generated-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1062
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1063
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
  1064
section {* Standard ML Interface
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1065
  \label{sec:standard-ml-interface} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
  1066
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1067
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1068
This section describes the package's programmatic interface.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1069
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1070
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1071
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
  1072
section {* Interoperability
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1073
  \label{sec:interoperability} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1074
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1075
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1076
This section is concerned with the packages' interaction with other Isabelle
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1077
packages and tools, such as the code generator and the counterexample
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1078
generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1079
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1080
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1081
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1082
subsection {* Transfer and Lifting
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1083
  \label{ssec:transfer-and-lifting} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1084
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1085
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1086
subsection {* Code Generator
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1087
  \label{ssec:code-generator} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1088
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1089
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1090
subsection {* Quickcheck
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1091
  \label{ssec:quickcheck} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1092
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1093
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1094
subsection {* Nitpick
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1095
  \label{ssec:nitpick} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1096
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
  1097
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1098
subsection {* Nominal Isabelle
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
  1099
  \label{ssec:nominal-isabelle} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1100
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1101
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
  1102
section {* Known Bugs and Limitations
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1103
  \label{sec:known-bugs-and-limitations} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1104
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1105
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1106
This section lists known open issues of the package.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
  1107
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1108
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1109
text {*
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1110
* primrec\_new and primcorec are vaporware
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1111
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1112
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1113
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1114
* issues with HOL-Proofs?
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1115
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1116
* partial documentation
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1117
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
  1118
* too much output by commands like "datatype_new" and "codatatype"
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1119
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1120
* no direct way to define recursive functions for default values -- but show trick
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1121
  based on overloading
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1122
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1123
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1124
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
  1125
section {* Acknowledgments
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1126
  \label{sec:acknowledgments} *}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1127
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
  1128
text {*
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1129
Tobias Nipkow and Makarius Wenzel made this work possible. Andreas Lochbihler
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1130
provided lots of comments on earlier versions of the package, especially for the
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1131
coinductive part. Brian Huffman suggested major simplifications to the internal
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1132
constructions, much of which has yet to be implemented. Florian Haftmann and
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1133
Christian Urban provided general advice advice on Isabelle and package writing.
53025
c820c9e9e8f4 more work on (co)datatype docs
blanchet
parents: 53018
diff changeset
  1134
Stefan Milius and Lutz Schr\"oder suggested an elegant proof to eliminate one of
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
  1135
the BNF assumptions.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
  1136
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
  1137
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
  1138
end