src/Doc/Datatypes/Datatypes.thy
author blanchet
Wed, 31 Jul 2013 11:28:59 +0200
changeset 52805 7f2f42046361
parent 52795 126ee2abed9b
child 52806 146ce45c3619
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
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     8
imports BNF
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     9
begin
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    10
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    11
section {* Introduction *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    12
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    13
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    14
The 2013 edition of Isabelle introduced new definitional package for datatypes
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    15
and codatatypes. The datatype support is similar to that provided by the earlier
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    16
package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL};
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    17
indeed, replacing \cmd{datatype} by \cmd{datatype\_new} is usually sufficient
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    18
to port existing specifications to the new package. What makes the new package
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    19
attractive is that it supports definitions with recursion through a large class
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    20
of non-datatypes, notably finite sets:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    21
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    22
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    23
    datatype_new 'a treeFS = TreeFS 'a "'a treeFS fset"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    24
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    25
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    26
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    27
Another advantage of the new package is that it supports local definitions:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    28
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    29
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    30
    context linorder
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    31
    begin
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    32
      datatype_new flag = Less | Eq | Greater
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    33
    end
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    34
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    35
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    36
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    37
Finally, the package also provides some convenience, notably automatically
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    38
generated destructors. For example, the command
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    39
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    40
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    41
(*<*)hide_const Nil Cons hd tl(*>*)
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    42
    datatype_new 'a list = null: Nil | Cons (hd: 'a) (tl: "'a list")
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    43
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    44
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    45
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    46
introduces a discriminator @{const null} and a pair of selectors @{const hd} and
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    47
@{const tl} characterized as follows:
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    48
%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    49
\[@{thm list.collapse(1)[no_vars]}\qquad @{thm list.collapse(2)[no_vars]}\]
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    50
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    51
The command \cmd{datatype\_new} is expected to displace \cmd{datatype} in a future
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    52
release. Authors of new theories are encouraged to use \cmd{datatype\_new}, and
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    53
maintainers of older theories may want to consider upgrading in the coming months.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    54
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    55
The package also provides codatatypes (or ``coinductive datatypes''), which may
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    56
have infinite values. The following command introduces a codatatype of infinite
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    57
streams:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    58
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    59
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    60
    codatatype 'a stream = Stream 'a "'a stream"
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    61
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    62
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    63
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    64
Mixed inductive--coinductive recursion is possible via nesting.
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    65
Compare the following four examples:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    66
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    67
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    68
    datatype_new 'a treeFF = TreeFF 'a "'a treeFF list"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    69
    datatype_new 'a treeFI = TreeFI 'a "'a treeFF stream"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    70
    codatatype 'a treeIF = TreeIF 'a "'a treeFF list"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    71
    codatatype 'a treeII = TreeII 'a "'a treeFF stream"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    72
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    73
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    74
To use the package, it is necessary to import the @{theory BNF} theory, which
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    75
can be precompiled as the \textit{HOL-BNF} image:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    76
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    77
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    78
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    79
\noindent
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    80
\ \ \ \ \texttt{isabelle build -b HOL-BNF}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    81
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    82
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    83
text {*
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    84
The package, like its predecessor, fully adheres to the LCF philosophy
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    85
\cite{mgordon79}: The characteristic theorems associated with the specified
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    86
(co)datatypes are derived rather than introduced axiomatically.%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    87
\footnote{Nonetheless, if the \textit{quick\_and\_dirty} option is enabled, some
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    88
of the internal constructions and most of the internal proof obligations are
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    89
skipped.}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    90
The package's metatheory is described in a pair of papers
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    91
\cite{traytel-et-al-2012,blanchette-et-al-wit}.
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    92
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    93
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    94
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    95
This tutorial is organized as follows:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    96
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    97
\begin{itemize}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    98
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    99
describes how to specify datatypes using the \cmd{datatype\_new} command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   100
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   101
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   102
Functions,'' describes how to specify recursive functions using
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   103
\cmd{primrec\_new}, \cmd{fun}, and \cmd{function}.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   104
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   105
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   106
describes how to specify codatatypes using the \cmd{codatatype} command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   107
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   108
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   109
Functions,'' describes how to specify corecursive functions using the
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   110
\cmd{primcorec} command.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   111
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   112
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   113
Bounded Natural Functors,'' explains how to set up the (co)datatype package to
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   114
allow nested recursion through custom well-behaved type constructors.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   115
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   116
\item Section \ref{sec:generating-free-constructor-theorems}, ``Generating Free 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   117
Constructor Theorems,'' explains how to derive convenience theorems for free
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   118
constructors, as performed internally by \cmd{datatype\_new} and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   119
\cmd{codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   120
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   121
\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   122
describes the package's programmatic interface.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   123
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   124
\item Section \ref{sec:interoperability}, ``Interoperability,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   125
is concerned with the packages' interaction with other Isabelle packages and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   126
tools, such as the code generator and the counterexample generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   127
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   128
\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   129
Limitations,'' concludes with open issues.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   130
\end{itemize}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   131
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   132
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   133
section {* Defining Datatypes%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   134
  \label{sec:defining-datatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   135
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   136
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   137
This section describes how to specify datatypes using the \cmd{datatype\_new}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   138
command. The command is first illustrated through concrete examples featuring
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   139
different flavors of recursion. More examples are available in
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   140
\verb|~~/src/HOL/BNF/Examples|. The syntax is largely modeled after that of the
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   141
existing \cmd{datatype} command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   142
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   143
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   144
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   145
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   146
subsubsection {* Nonrecursive Types *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   147
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   148
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   149
enumeration type:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   150
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   151
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   152
    datatype_new trool = Truue | Faalse | Maaybe
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   153
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   154
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   155
Haskell-style option type:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   156
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   157
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   158
    datatype_new 'a maybe = Nothing | Just 'a
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   159
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   160
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   161
triple:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   162
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   163
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   164
    datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   165
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   166
subsubsection {* Simple Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   167
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   168
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   169
simplest recursive type: natural numbers
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   170
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   171
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   172
    datatype_new nat = Zero | Suc nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   173
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   174
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   175
lists were shown in the introduction
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   176
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   177
terminated lists are a variant:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   178
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   179
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   180
    datatype_new ('a, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   181
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   182
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   183
On the right-hand side of the equal sign, the usual Isabelle conventions apply:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   184
Nonatomic types must be enclosed in double quotes.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   185
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   186
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   187
subsubsection {* Mutual Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   188
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   189
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   190
Mutual recursion = Define several types simultaneously, referring to each other.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   191
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   192
Simple example: distinction between even and odd natural numbers:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   193
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   194
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   195
    datatype_new even_nat = Zero | Even_Suc odd_nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   196
    and odd_nat = Odd_Suc even_nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   197
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   198
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   199
More complex, and more realistic, example:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   200
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   201
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   202
    datatype_new ('a, 'b) expr =
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   203
      Term "('a, 'b) term" | Sum "('a, 'b) term" "('a, 'b) expr"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   204
    and ('a, 'b) trm =
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   205
      Factor "('a, 'b) factor" | Prod "('a, 'b) factor" "('a, 'b) trm"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   206
    and ('a, 'b) factor =
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   207
      Const 'a | Var 'b | Sub_Expr "('a, 'b) expr"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   208
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   209
subsubsection {* Nested Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   210
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   211
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   212
Nested recursion = Have recursion through a type constructor.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   213
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   214
The introduction showed some examples of trees with nesting through lists.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   215
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   216
More complex example, which reuses our maybe and triple types:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   217
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   218
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   219
    datatype_new 'a triple_tree =
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   220
      Triple_Tree "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   221
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   222
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   223
Recursion may not be arbitrary; e.g. impossible to define
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   224
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   225
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   226
(*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   227
    datatype_new 'a foo = Foo (*<*) datatype_new 'a bar = Bar  "'a foo \<Rightarrow> 'a foo"
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
subsubsection {* Auxiliary Constants and Syntaxes *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   231
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   232
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   233
* destructors
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   234
* also mention special syntaxes
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   235
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   236
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   237
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   238
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   239
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   240
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   241
subsection {* Compatibility Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   242
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   243
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   244
section {* Defining Recursive Functions%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   245
  \label{sec:defining-recursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   246
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   247
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   248
This describes how to specify recursive functions over datatypes
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   249
specified using \cmd{datatype\_new}. The focus in on the \cmd{primrec\_new}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   250
command, which supports primitive recursion. A few examples feature the
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   251
\cmd{fun} and \cmd{function} commands, described in a separate tutorial  
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   252
\cite{isabelle-function}.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   253
%%% TODO: partial_function?
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   254
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   255
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   256
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   257
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   258
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   259
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   260
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   261
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   262
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   263
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   264
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   265
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   266
subsection {* Compatibility Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   267
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   268
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   269
section {* Defining Codatatypes%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   270
  \label{sec:defining-codatatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   271
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   272
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   273
This section describes how to specify codatatypes using the \cmd{codatatype}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   274
command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   275
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   276
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   277
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   278
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   279
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   280
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   281
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   282
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   283
subsection {* General Syntax *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   284
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   285
subsection {* Characteristic Theorems *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   286
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   287
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   288
section {* Defining Corecursive Functions%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   289
  \label{sec:defining-corecursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   290
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   291
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   292
This section describes how to specify corecursive functions using the
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   293
\cmd{primcorec} command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   294
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   295
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   296
subsection {* Introductory Examples *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   297
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   298
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   299
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   300
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   301
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   302
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   303
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   304
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   305
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   306
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   307
section {* Registering Bounded Natural Functors%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   308
  \label{sec:registering-bounded-natural-functors} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   309
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   310
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   311
This section explains how to set up the (co)datatype package to allow nested
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   312
recursion through custom well-behaved type constructors. The key concept is that
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   313
of a bounded natural functor (BNF).
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   314
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   315
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   316
subsection {* Introductory Example *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   317
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   318
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   319
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   320
\verb|~~/src/HOL/BNF/More_BNFs.thy|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   321
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   322
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   323
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   324
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   325
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   326
section {* Generating Free Constructor Theorems%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   327
  \label{sec:generating-free-constructor-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   328
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   329
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   330
This section explains how to derive convenience theorems for free constructors,
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   331
as performed internally by \cmd{datatype\_new} and \cmd{codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   332
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   333
  * 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
   334
    a type not introduced by ...
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   335
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   336
  * also useful for compatibility with old package, e.g. add destructors to
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   337
    old \cmd{datatype}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   338
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   339
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   340
subsection {* Introductory Example *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   341
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   342
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   343
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   344
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   345
section {* Standard ML Interface%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   346
  \label{sec:standard-ml-interface} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   347
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   348
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   349
This section describes the package's programmatic interface.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   350
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   351
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   352
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   353
section {* Interoperability%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   354
  \label{sec:interoperability} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   355
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   356
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   357
This section is concerned with the packages' interaction with other Isabelle
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   358
packages and tools, such as the code generator and the counterexample
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   359
generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   360
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   361
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   362
subsection {* Transfer and Lifting *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   363
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   364
subsection {* Code Generator *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   365
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   366
subsection {* Quickcheck *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   367
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   368
subsection {* Nitpick *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   369
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   370
subsection {* Nominal Isabelle *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   371
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   372
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   373
section {* Known Bugs and Limitations%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   374
  \label{sec:known-bugs-and-limitations} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   375
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   376
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   377
This section lists known open issues of the package.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   378
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   379
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   380
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   381
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   382
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   383
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   384
end