src/Doc/Datatypes/Datatypes.thy
author blanchet
Fri, 02 Aug 2013 17:56:44 +0200
changeset 52840 a0da63cec918
parent 52829 591e76f2651e
child 52841 87a66bad0796
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 {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    32
The 2013 edition of Isabelle introduced new definitional package for datatypes
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    33
and codatatypes. The datatype support is similar to that provided by the earlier
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    34
package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL},
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    35
documented in the Isar reference manual \cite{isabelle-isar-ref};
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    36
indeed, replacing the keyword @{command datatype} by @{command datatype_new} is
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    37
usually all that is needed to port existing theories to use the new package.
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    38
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    39
Perhaps the main advantage of the new package is that it supports definitions
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    40
with recursion through a large class of non-datatypes, notably finite sets:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    41
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    42
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    43
    datatype_new 'a treeFS = NodeFS 'a "'a treeFS fset"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    44
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    45
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    46
\noindent
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    47
Another strong point is that the package supports local definitions:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    48
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    49
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    50
    context linorder
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    51
    begin
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    52
      datatype_new flag = Less | Eq | Greater
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    53
    end
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    54
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    55
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    56
\noindent
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    57
The package also provides some convenience, notably automatically generated
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    58
destructors.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    59
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    60
The command @{command datatype_new} is expected to displace @{command datatype}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    61
in a future release. Authors of new theories are encouraged to use
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    62
@{command datatype_new}, and maintainers of older theories may want to consider
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    63
upgrading in the coming months.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    64
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    65
In addition to plain inductive datatypes, the package supports coinductive
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    66
datatypes, or \emph{codatatypes}, which may have infinite values. For example,
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    67
the following command introduces a codatatype of infinite streams:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    68
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    69
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
    70
    codatatype 'a stream = Stream 'a "'a stream"
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    71
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    72
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    73
\noindent
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    74
Mixed inductive--coinductive recursion is possible via nesting. Compare the
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    75
following four examples:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    76
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    77
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    78
    datatype_new 'a treeFF = NodeFF 'a "'a treeFF list"
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    79
    datatype_new 'a treeFI = NodeFI 'a "'a treeFF stream"
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    80
    codatatype 'a treeIF = NodeIF 'a "'a treeFF list"
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    81
    codatatype 'a treeII = NodeII 'a "'a treeFF stream"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    82
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    83
text {*
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    84
The first two tree types allow only finite branches, whereas the last two allow
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    85
infinite branches. Orthogonally, the nodes in the first and third types have
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    86
finite branching, whereas those of the second and fourth have infinitely many
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    87
direct subtrees.
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
    88
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    89
To use the package, it is necessary to import the @{theory BNF} theory, which
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
    90
can be precompiled as the \textit{HOL-BNF} image. The following commands show
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
    91
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
    92
without launching jEdit:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    93
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    94
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    95
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    96
\noindent
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
    97
\ \ \ \ \texttt{isabelle jedit -l HOL-BNF} \\
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
    98
\noindent
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
    99
\hbox{}\ \ \ \ \texttt{isabelle build -b HOL-BNF}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   100
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   101
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   102
text {*
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   103
The package, like its predecessor, fully adheres to the LCF philosophy
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   104
\cite{mgordon79}: The characteristic theorems associated with the specified
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   105
(co)datatypes are derived rather than introduced axiomatically.%
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   106
\footnote{Nonetheless, if the \textit{quick\_and\_dirty} option is enabled, some
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   107
of the internal constructions and most of the internal proof obligations are
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   108
skipped.}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   109
The package's metatheory is described in a pair of papers
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   110
\cite{traytel-et-al-2012,blanchette-et-al-wit}.
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   111
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   112
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   113
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   114
This tutorial is organized as follows:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   115
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   116
\begin{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   117
\setlength{\itemsep}{0pt}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   118
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   119
\item Section \ref{sec:defining-datatypes}, ``Defining Datatypes,''
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   120
describes how to specify datatypes using the @{command datatype_new} command.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   121
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   122
\item Section \ref{sec:defining-recursive-functions}, ``Defining Recursive 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   123
Functions,'' describes how to specify recursive functions using
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   124
\keyw{primrec\_new}, @{command fun}, and @{command function}.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   125
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   126
\item Section \ref{sec:defining-codatatypes}, ``Defining Codatatypes,''
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   127
describes how to specify codatatypes using the @{command codatatype} command.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   128
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   129
\item Section \ref{sec:defining-corecursive-functions}, ``Defining Corecursive 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   130
Functions,'' describes how to specify corecursive functions using the
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   131
\keyw{primcorec} command.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   132
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   133
\item Section \ref{sec:registering-bounded-natural-functors}, ``Registering 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   134
Bounded Natural Functors,'' explains how to set up the (co)datatype package to
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   135
allow nested recursion through custom well-behaved type constructors.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   136
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   137
\item Section \ref{sec:generating-free-constructor-theorems}, ``Generating Free 
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   138
Constructor Theorems,'' explains how to derive convenience theorems for free
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   139
constructors, as performed internally by @{command datatype_new} and
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   140
@{command codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   141
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   142
\item Section \ref{sec:standard-ml-interface}, ``Standard ML Interface,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   143
describes the package's programmatic interface.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   144
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   145
\item Section \ref{sec:interoperability}, ``Interoperability,''
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   146
is concerned with the packages' interaction with other Isabelle packages and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   147
tools, such as the code generator and the counterexample generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   148
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   149
\item Section \ref{sec:known-bugs-and-limitations}, ``Known Bugs and 
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   150
Limitations,'' concludes with known open issues at the time of writing.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   151
\end{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   152
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   153
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   154
\newbox\boxA
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   155
\setbox\boxA=\hbox{\texttt{nospam}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   156
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   157
\newcommand\authoremaili{\texttt{blan{\color{white}nospam}\kern-\wd\boxA{}chette@\allowbreak
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   158
in.\allowbreak tum.\allowbreak de}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   159
\newcommand\authoremailii{\texttt{pope{\color{white}nospam}\kern-\wd\boxA{}scua@\allowbreak
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   160
in.\allowbreak tum.\allowbreak de}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   161
\newcommand\authoremailiii{\texttt{tray{\color{white}nospam}\kern-\wd\boxA{}tel@\allowbreak
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   162
in.\allowbreak tum.\allowbreak de}}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   163
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   164
\noindent
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   165
Comments and bug reports concerning either
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   166
the tool or the manual should be directed to the authors at
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   167
\authoremaili, \authoremailii, and \authoremailiii.
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   168
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   169
\begin{framed}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   170
\noindent
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   171
\textbf{Warning:} This document is under heavy construction. Please apologise
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   172
for its appearance and come back in a few months. If you have ideas regarding
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   173
material that should be included, please let the authors know.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   174
\end{framed}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   175
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   176
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   177
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   178
section {* Defining Datatypes
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   179
  \label{sec:defining-datatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   180
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   181
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   182
This section describes how to specify datatypes using the @{command datatype_new}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   183
command. The command is first illustrated through concrete examples featuring
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   184
different flavors of recursion. More examples can be found in the directory
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   185
\verb|~~/src/HOL/BNF/Examples|.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   186
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   187
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   188
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   189
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   190
  \label{ssec:datatype-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   191
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   192
subsubsection {* Nonrecursive Types *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   193
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   194
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   195
enumeration type:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   196
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   197
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   198
    datatype_new trool = Truue | Faalse | Perhaaps
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   199
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   200
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   201
Haskell-style option type:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   202
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   203
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   204
    datatype_new 'a maybe = Nothing | Just 'a
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   205
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   206
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   207
triple:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   208
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   209
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   210
    datatype_new ('a, 'b, 'c) triple = Triple 'a 'b 'c
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   211
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   212
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   213
subsubsection {* Simple Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   214
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   215
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   216
simplest recursive type: natural numbers
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 nat = Zero | Suc nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   220
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   221
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   222
lists were shown in the introduction
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   223
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   224
terminated lists are a variant:
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, 'b) tlist = TNil 'b | TCons 'a "('a, 'b) tlist"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   228
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   229
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   230
On the right-hand side of the equal sign, the usual Isabelle conventions apply:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   231
Nonatomic types must be enclosed in double quotes.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   232
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   233
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   234
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   235
subsubsection {* Mutual Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   236
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   237
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   238
Mutual recursion = Define several types simultaneously, referring to each other.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   239
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   240
Simple example: distinction between even and odd natural numbers:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   241
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   242
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   243
    datatype_new even_nat = Zero | Even_Suc odd_nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   244
    and odd_nat = Odd_Suc even_nat
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   245
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   246
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   247
More complex, and more realistic, example:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   248
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   249
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   250
    datatype_new ('a, 'b) expr =
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   251
      Term "('a, 'b) trm" | Sum "('a, 'b) trm" "('a, 'b) expr"
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   252
    and ('a, 'b) trm =
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   253
      Factor "('a, 'b) fact" | Prod "('a, 'b) fact" "('a, 'b) trm"
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   254
    and ('a, 'b) fact =
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   255
      Const 'a | Var 'b | Sub_Expr "('a, 'b) expr"
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   256
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   257
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   258
subsubsection {* Nested Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   259
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   260
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   261
Nested recursion = Have recursion through a type constructor.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   262
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   263
The introduction showed some examples of trees with nesting through lists.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   264
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   265
More complex example, which reuses our maybe and triple types:
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   266
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   267
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   268
    datatype_new 'a triple_tree =
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   269
      Triple_Node "('a triple_tree maybe, bool, 'a triple_tree maybe) triple"
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   270
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   271
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   272
Recursion may not be arbitrary; e.g. impossible to define
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   273
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   274
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   275
(*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   276
    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
   277
*)
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   278
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   279
    datatype_new 'a evil = Evil (*<*)'a
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   280
    typ (*>*)"'a evil \<Rightarrow> 'a evil"
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   281
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   282
text {*
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   283
Issue: => allows recursion only on its right-hand side.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   284
This issue is inherited by polymorphic datatypes (and codatatypes)
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   285
defined in terms of =>.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   286
In general, type constructors "('a1, ..., 'an) k" allow recursion on a subset
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   287
of their type arguments.
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   288
*}
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   289
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   290
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   291
subsubsection {* Auxiliary Constants and Syntaxes *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   292
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   293
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   294
The @{command datatype_new} command introduces various constants in addition to the
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   295
constructors. Given a type @{text "('a1,\<dots>,'aM) t"} with constructors
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   296
@{text "t.C\<^sub>1"}, \ldots, @{text "t.C\<^sub>m"}, the following auxiliary
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   297
constants are introduced (among others):
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   298
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   299
\begin{itemize}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   300
\setlength{\itemsep}{0pt}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   301
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   302
\item \emph{Set functions} (\emph{natural transformations}):
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   303
@{text t_set1}, \ldots, @{text t_setM}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   304
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   305
\item \emph{Map function} (\emph{functorial action}): @{text t_map}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   306
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   307
\item \emph{Relator}: @{text t_rel}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   308
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   309
\item \emph{Iterator}: @{text t_fold}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   310
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   311
\item \emph{Recursor}: @{text t_rec}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   312
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   313
\item \emph{Discriminators}: @{text "t.is_C\<^sub>1"}, \ldots,
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   314
@{text "t.is_C\<^sub>m"}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   315
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   316
\item \emph{Selectors}:
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   317
@{text t.un_C}$_{11}$, \ldots, @{text t.un_C}$_{1n_1}$, \ldots,
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   318
@{text t.un_C}$_{m1}$, \ldots, @{text t.un_C}$_{mn_m}$
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   319
\end{itemize}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   320
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   321
The discriminators and selectors are collectively called \emph{destructors}. The
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   322
@{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
   323
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   324
The set functions, map function, relator, discriminators, and selectors can be
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   325
given custom names, as in the example below:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   326
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   327
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   328
(*<*)hide_const Nil Cons hd tl(*>*)
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   329
    datatype_new (set: 'a) list (map: map rel: list_all2) =
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   330
      null: Nil (defaults tl: Nil)
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   331
    | Cons (hd: 'a) (tl: "'a list")
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   332
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   333
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   334
\noindent
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   335
The command introduces a discriminator @{const null} and a pair of selectors
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   336
@{const hd} and @{const tl} characterized as follows:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   337
%
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   338
\[@{thm list.collapse(1)[of xs, no_vars]}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   339
  \qquad @{thm list.collapse(2)[of xs, no_vars]}\]
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   340
%
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   341
For two-constructor datatypes, a single discriminator constant suffices. The
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   342
discriminator associated with @{const Cons} is simply @{text "\<not> null"}.
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   343
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   344
The \keyw{defaults} keyword following the @{const Nil} constructor specifies a
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   345
default value for selectors associated with other constructors. Here, it is
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   346
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
   347
being unspecified).
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   348
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   349
Because @{const Nil} is a nullary constructor, it is also possible to use @{text
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   350
"= Nil"} as a discriminator. This is specified by specifying @{text "="} instead
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   351
of the identifier @{const null} in the declaration above. Although this may look
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   352
appealing, the mixture of constructors and selectors in the resulting
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   353
characteristic theorems can lead Isabelle's automation to switch between the
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   354
constructor and the destructor view in surprising ways.
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   355
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   356
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   357
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   358
The usual mixfix syntaxes are available for both types and constructors. For example:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   359
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   360
%%% FIXME: remove trailing underscore and use locale trick instead once this is
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   361
%%% supported.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   362
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   363
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   364
    datatype_new ('a, 'b) prod (infixr "*" 20) =
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   365
      Pair 'a 'b
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   366
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   367
    datatype_new (set_: 'a) list_ =
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   368
      null: Nil ("[]")
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   369
    | Cons (hd: 'a) (tl: "'a list_") (infixr "#" 65)
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   370
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   371
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   372
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   373
  \label{ssec:datatype-syntax} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   374
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   375
text {*
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   376
Datatype definitions have the following general syntax:
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   377
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   378
@{rail "
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   379
  @@{command datatype_new} @{syntax target}? @{syntax dt_options}? \\
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   380
    (@{syntax dt_name} '=' (@{syntax ctor} + '|') + @'and')
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   381
  ;
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   382
  @{syntax_def dt_options}: '(' ((@'no_dests' | @'rep_compat') + ',') ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   383
"}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   384
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   385
The syntactic quantity @{syntax target} can be used to specify a local context
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   386
(e.g., @{text "(in linorder)"}). It is documented in the Isar reference manual
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   387
\cite{isabelle-isar-ref}.
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   388
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   389
The optional target is followed by optional options:
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   390
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   391
\begin{itemize}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   392
\setlength{\itemsep}{0pt}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   393
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   394
\item
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   395
The \keyw{no\_dests} option indicates that no destructors (i.e.,
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   396
discriminators and selectors) should be generated.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   397
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   398
\item
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   399
The \keyw{rep\_compat} option indicates that the names generated by the
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   400
package should contain optional (and normally not displayed) @{text "new."}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   401
components to prevent clashes with a later call to @{command rep_datatype}. See
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   402
Section~\ref{ssec:datatype-compatibility-issues} for details.
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   403
\end{itemize}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   404
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   405
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
   406
define, its type parameters, and optional additional information:
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   407
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   408
@{rail "
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   409
  @{syntax_def dt_name}: @{syntax tyargs}? @{syntax name}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   410
    @{syntax map_rel}? @{syntax mixfix}?
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   411
  ;
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   412
  @{syntax_def tyargs}: @{syntax typefree} | '(' ((@{syntax name} ':')? @{syntax typefree} + ',') ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   413
  ;
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   414
  @{syntax_def map_rel}: '(' ((('map' | 'rel') ':' @{syntax name}) +) ')'
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   415
"}
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   416
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   417
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   418
The syntactic quantity @{syntax name} denotes an identifier, @{syntax typefree}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   419
denotes fixed type variable (@{typ 'a}, @{typ 'b}, \ldots), and @{syntax
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   420
mixfix} denotes the usual parenthesized mixfix notation. They are documented in
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   421
the Isar reference manual \cite{isabelle-isar-ref}.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   422
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   423
The optional names preceding the type variables allow to override the default
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   424
names of the set functions (@{text t_set1}, \ldots, @{text t_setM}).
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   425
Inside a mutually recursive datatype specification, all defined datatypes must
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   426
specify exactly the same type variables in the same order.
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   427
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   428
@{rail "
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   429
  @{syntax_def ctor}: (@{syntax name} ':')? @{syntax name} (@{syntax ctor_arg} * ) \\
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   430
    @{syntax sel_defaults}? @{syntax mixfix}?
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   431
"}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   432
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   433
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   434
The main constituents of a constructor specification is the name of the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   435
constructor and the list of its argument types. An optional discriminator name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   436
can be supplied at the front to override the default name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   437
(@{text t.un_C}$_{ij}$).
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   438
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   439
@{rail "
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   440
  @{syntax_def ctor_arg}: @{syntax type} | '(' @{syntax name} ':' @{syntax type} ')'
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   441
"}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   442
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   443
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   444
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
   445
name for the corresponding selector to override the default name
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   446
(@{text t.un_C}$_{ij}$). The same selector names can be reused for several
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   447
constructors as long as they have the same type.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   448
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   449
@{rail "
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   450
  @{syntax_def sel_defaults}: '(' @'defaults' (@{syntax name} ':' @{syntax term} +) ')'
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   451
"}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   452
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   453
\noindent
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   454
Given a constructor
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   455
@{text "C \<Colon> \<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<sigma>"},
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   456
default values can be specified for any selector
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   457
@{text "un_D \<Colon> \<sigma> \<Rightarrow> \<tau>"}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   458
associated with other constructors. The specified default value must have type
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   459
@{text "\<sigma>\<^sub>1 \<Rightarrow> \<dots> \<Rightarrow> \<sigma>\<^sub>p \<Rightarrow> \<tau>"}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   460
(i.e., it may dependend on @{text C}'s arguments).
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   461
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   462
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   463
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   464
  \label{ssec:datatype-generated-theorems} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   465
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   466
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   467
  * free ctor theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   468
    * case syntax
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   469
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   470
  * per-type theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   471
    * sets, map, rel
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   472
    * induct, fold, rec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   473
    * simps
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   474
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   475
  * multi-type (``common'') theorems
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   476
    * induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   477
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   478
  * mention what is registered with which attribute
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   479
    * and also nameless safes
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   480
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   481
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   482
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   483
subsection {* Compatibility Issues
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   484
  \label{ssec:datatype-compatibility-issues} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   485
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   486
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   487
  * main incompabilities between two packages
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   488
    * differences in theorem names (e.g. singular vs. plural for some props?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   489
    * differences in "simps"?
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   490
    * different recursor/induction in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   491
        * discussed in Section~\ref{sec:defining-recursive-functions}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   492
    * different ML interfaces / extension mechanisms
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   493
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   494
  * register new datatype as old datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   495
    * motivation:
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   496
      * do recursion through new datatype in old datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   497
        (e.g. useful when porting to the new package one type at a time)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   498
      * use old primrec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   499
      * use fun
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   500
      * use extensions like size (needed for fun), the AFP order, Quickcheck,
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   501
        Nitpick
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   502
      * provide exactly the same theorems with the same names (compatibility)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   503
    * option 1
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   504
      * \keyw{rep\_compat}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   505
      * \keyw{rep\_datatype}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   506
      * has some limitations
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   507
        * mutually recursive datatypes? (fails with rep\_datatype?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   508
        * nested datatypes? (fails with datatype\_new?)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   509
    * option 2
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   510
      * \keyw{datatype\_compat}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   511
      * not fully implemented yet?
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   512
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   513
  * register old datatype as new datatype
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   514
    * no clean way yet
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   515
    * if the goal is to do recursion through old datatypes, can register it as
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   516
      a BNF (Section XXX)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   517
    * can also derive destructors etc. using @{command wrap_free_constructors}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   518
      (Section XXX)
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   519
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   520
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   521
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   522
section {* Defining Recursive Functions
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   523
  \label{sec:defining-recursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   524
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   525
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   526
This describes how to specify recursive functions over datatypes
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   527
specified using @{command datatype_new}. The focus in on the \keyw{primrec\_new}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   528
command, which supports primitive recursion. A few examples feature the
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   529
@{command fun} and @{command function} commands, described in a separate
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   530
tutorial \cite{isabelle-function}.
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   531
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   532
%%% TODO: partial_function?
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   533
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   534
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   535
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   536
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   537
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   538
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   539
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   540
  \label{ssec:primrec-examples} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   541
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   542
subsubsection {* Nonrecursive Types *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   543
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   544
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   545
subsubsection {* Simple Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   546
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   547
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   548
subsubsection {* Mutual Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   549
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   550
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   551
subsubsection {* Nested Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   552
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   553
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   554
subsubsection {* Nested-as-Mutual Recursion *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   555
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   556
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   557
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   558
  \label{ssec:primrec-syntax} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   559
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   560
text {*
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   561
Primitive recursive functions have the following general syntax:
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   562
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   563
@{rail "
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   564
  @@{command primrec_new} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   565
    (@{syntax primrec_equation} + '|')
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   566
  ;
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   567
  @{syntax_def primrec_equation}: @{syntax thmdecl}? @{syntax prop}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   568
"}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   569
*}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   570
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   571
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   572
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   573
  \label{ssec:primrec-generated-theorems} *}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   574
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   575
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   576
  * synthesized nonrecursive definition
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   577
  * user specification is rederived from it, exactly as entered
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   578
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   579
  * induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   580
    * mutualized
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   581
    * without some needless induction hypotheses if not used
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   582
  * fold, rec
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   583
    * mutualized
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   584
*}
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   585
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   586
subsection {* Recursive Default Values for Selectors
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   587
  \label{ssec:recursive-default-values-for-selectors} *}
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   588
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   589
text {*
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   590
A datatype selector @{text un_D} can have a default value for each constructor
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   591
on which it is not otherwise specified. Occasionally, it is useful to have the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   592
default value be defined recursively. This produces a chicken-and-egg situation
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   593
that appears unsolvable, because the datatype is not introduced yet at the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   594
moment when the selectors are introduced. Of course, we can always define the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   595
selectors manually afterward, but we then have to state and prove all the
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   596
characteristic theorems ourselves instead of letting the package do it.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   597
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   598
Fortunately, there is a fairly elegant workaround that relies on overloading and
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   599
that avoids the tedium of manual derivations:
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   600
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   601
\begin{enumerate}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   602
\setlength{\itemsep}{0pt}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   603
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   604
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   605
Introduce a fully unspecified constant @{text "un_D\<^sub>0 \<Colon> 'a"} using
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   606
@{keyword consts}.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   607
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   608
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   609
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
   610
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   611
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   612
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
   613
using the @{command overloading} command.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   614
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   615
\item
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   616
Derive the desired equation on @{text un_D} from the characteristic equations
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   617
for @{text "un_D\<^sub>0"}.
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   618
\end{enumerate}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   619
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   620
The following example illustrates this procedure:
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   621
*}
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   622
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   623
    consts termi\<^sub>0 :: 'a
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   624
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   625
    datatype_new (*<*)(rep_compat) (*>*)('a, 'b) tlist_ =
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   626
      TNil (termi: 'b) (defaults ttl: TNil)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   627
    | 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
   628
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   629
(*<*)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   630
    rep_datatype TNil TCons
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   631
    by (erule tlist_.induct, assumption) auto
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   632
(*>*)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   633
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   634
    overloading
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   635
      termi\<^sub>0 \<equiv> "termi\<^sub>0 \<Colon> ('a, 'b) tlist_ \<Rightarrow> 'b"
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   636
    begin
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   637
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   638
(*<*)(*FIXME: use primrec_new and avoid rep_datatype*)(*>*)
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   639
    fun termi\<^sub>0 :: "('a, 'b) tlist_ \<Rightarrow> 'b" where
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   640
    "termi\<^sub>0 (TNil y) = y" |
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   641
    "termi\<^sub>0 (TCons x xs) = termi\<^sub>0 xs"
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   642
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   643
    end
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   644
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   645
    lemma terminal_TCons[simp]: "termi (TCons x xs) = termi xs"
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   646
    by (cases xs) auto
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   647
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   648
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   649
subsection {* Compatibility Issues
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   650
  \label{ssec:datatype-compatibility-issues} *}
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   651
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   652
text {*
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   653
  * different induction in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   654
    * solution: define nested-as-mutual functions with primrec,
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   655
      and look at .induct
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   656
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   657
  * different induction and recursor in nested case
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   658
    * only matters to low-level users; they can define a dummy function to force
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   659
      generation of mutualized recursor
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   660
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   661
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   662
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   663
section {* Defining Codatatypes
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   664
  \label{sec:defining-codatatypes} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   665
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   666
text {*
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   667
This section describes how to specify codatatypes using the @{command codatatype}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   668
command.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   669
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   670
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   671
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   672
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   673
  \label{ssec:codatatype-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   674
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   675
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   676
More examples in \verb|~~/src/HOL/BNF/Examples|.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   677
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   678
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   679
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   680
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   681
  \label{ssec:codatatype-syntax} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   682
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   683
text {*
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   684
Definitions of codatatypes have almost exactly the same syntax as for datatypes
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   685
(Section~\ref{ssec:datatype-syntax}), with two exceptions: The command is called
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   686
@{command codatatype}; the \keyw{no\_dests} option is not available, because
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   687
destructors are a central notion for codatatypes.
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   688
*}
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   689
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   690
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   691
  \label{ssec:codatatype-generated-theorems} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   692
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   693
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   694
section {* Defining Corecursive Functions
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   695
  \label{sec:defining-corecursive-functions} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   696
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   697
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   698
This section describes how to specify corecursive functions using the
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   699
\keyw{primcorec} command.
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   700
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   701
%%% TODO: partial_function? E.g. for defining tail recursive function on lazy
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   702
%%% lists (cf. terminal0 in TLList.thy)
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   703
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   704
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   705
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   706
subsection {* Examples
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   707
  \label{ssec:primcorec-examples} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   708
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   709
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   710
More examples in \verb|~~/src/HOL/BNF/Examples|.
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   711
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   712
Also, for default values, the same trick as for datatypes is possible for
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   713
codatatypes (Section~\ref{ssec:recursive-default-values-for-selectors}).
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   714
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   715
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   716
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   717
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   718
  \label{ssec:primcorec-syntax} *}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   719
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   720
text {*
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   721
Primitive corecrusvie definitions have the following general syntax:
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   722
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   723
@{rail "
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   724
  @@{command primcorec} @{syntax target}? @{syntax \"fixes\"} \\ @'where'
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   725
    (@{syntax primcorec_formula} + '|')
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   726
  ;
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   727
  @{syntax_def primcorec_formula}: @{syntax thmdecl}? @{syntax prop}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   728
    (@'of' (@{syntax term} * ))?
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   729
"}
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   730
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   731
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   732
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   733
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   734
  \label{ssec:primcorec-generated-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   735
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   736
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   737
section {* Registering Bounded Natural Functors
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   738
  \label{sec:registering-bounded-natural-functors} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   739
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   740
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   741
This section explains how to set up the (co)datatype package to allow nested
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   742
recursion through custom well-behaved type constructors. The key concept is that
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   743
of a bounded natural functor (BNF).
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   744
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   745
    * @{command bnf}
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   746
    * @{command print_bnfs}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   747
*}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   748
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   749
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   750
subsection {* Example
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   751
  \label{ssec:bnf-examples} *}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   752
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   753
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   754
More examples in \verb|~~/src/HOL/BNF/Basic_BNFs.thy| and
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   755
\verb|~~/src/HOL/BNF/More_BNFs.thy|.
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   756
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   757
Mention distinction between live and dead type arguments;
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   758
mention =>.
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   759
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   760
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   761
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   762
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   763
  \label{ssec:bnf-syntax} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   764
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   765
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   766
section {* Generating Free Constructor Theorems
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   767
  \label{sec:generating-free-constructor-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   768
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   769
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   770
This section explains how to derive convenience theorems for free constructors,
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   771
as performed internally by @{command datatype_new} and @{command codatatype}.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   772
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   773
  * 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
   774
    a type not introduced by ...
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   775
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   776
  * also useful for compatibility with old package, e.g. add destructors to
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   777
    old @{command datatype}
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   778
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   779
  * @{command wrap_free_constructors}
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   780
    * \keyw{no\_dests}, \keyw{rep\_compat}
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   781
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   782
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   783
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   784
subsection {* Example
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   785
  \label{ssec:ctors-examples} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   786
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   787
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   788
subsection {* Syntax
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   789
  \label{ssec:ctors-syntax} *}
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   790
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   791
52840
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   792
subsection {* Generated Theorems
a0da63cec918 more (co)datatype documentation
blanchet
parents: 52829
diff changeset
   793
  \label{ssec:ctors-generated-theorems} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   795
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   796
section {* Standard ML Interface
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   797
  \label{sec:standard-ml-interface} *}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   798
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   799
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   800
This section describes the package's programmatic interface.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   801
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   802
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   803
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   804
section {* Interoperability
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   805
  \label{sec:interoperability} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   806
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   807
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   808
This section is concerned with the packages' interaction with other Isabelle
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   809
packages and tools, such as the code generator and the counterexample
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   810
generators.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   811
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   812
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   813
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   814
subsection {* Transfer and Lifting
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   815
  \label{ssec:transfer-and-lifting} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   816
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   817
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   818
subsection {* Code Generator
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   819
  \label{ssec:code-generator} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   820
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   821
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   822
subsection {* Quickcheck
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   823
  \label{ssec:quickcheck} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   824
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   825
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   826
subsection {* Nitpick
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   827
  \label{ssec:nitpick} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   828
52824
b7a83845bc93 more (co)datatype documentation
blanchet
parents: 52822
diff changeset
   829
52828
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   830
subsection {* Nominal Isabelle
e1c6fa322d96 more (co)datatype docs
blanchet
parents: 52827
diff changeset
   831
  \label{ssec:nominal-isabelle} *}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   832
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   833
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   834
section {* Known Bugs and Limitations
52805
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   835
  \label{sec:known-bugs-and-limitations} *}
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   836
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   837
text {*
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   838
This section lists known open issues of the package.
7f2f42046361 more (co)datatype documentation
blanchet
parents: 52795
diff changeset
   839
*}
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   840
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   841
text {*
52806
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   842
* primrec\_new and primcorec are vaporware
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   843
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   844
* 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
   845
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   846
* issues with HOL-Proofs?
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   847
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   848
* partial documentation
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   849
146ce45c3619 more work on (co)datatype docs
blanchet
parents: 52805
diff changeset
   850
* too much output by commands like "datatype_new" and "codatatype"
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   851
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   852
* no direct way to define recursive functions for default values -- but show trick
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   853
  based on overloading
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   854
*}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   855
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   856
52827
395d3df496ed more (co)datatype documentation
blanchet
parents: 52824
diff changeset
   857
section {* Acknowledgments
52822
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   858
  \label{sec:acknowledgments} *}
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   859
ae938ac9a721 more (co)datatype docs
blanchet
parents: 52806
diff changeset
   860
text {*
52829
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   861
Tobias Nipkow and Makarius Wenzel made this work possible. Andreas Lochbihler
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   862
provided lots of comments on earlier versions of the package, especially for the
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   863
coinductive part. Brian Huffman suggested major simplifications to the internal
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   864
constructions, much of which has yet to be implemented. Florian Haftmann and
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   865
Christian Urban provided general advice advice on Isabelle and package writing.
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   866
Stefan Milius and Lutz Schr\"oder suggested an elegant prove to eliminate one of
591e76f2651e minor doc fixes
blanchet
parents: 52828
diff changeset
   867
the BNF assumptions.
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   868
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   869
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   870
end