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