src/Doc/Datatypes/Datatypes.thy
author blanchet
Tue, 30 Jul 2013 19:59:17 +0200
changeset 52795 126ee2abed9b
parent 52794 aae782070611
child 52805 7f2f42046361
permissions -rw-r--r--
removed spurious headings
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     1
(*  Title:      Doc/Datatypes/Datatypes.thy
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     3
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     4
Tutorial for (co)datatype definitions with the new package.
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     5
*)
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     6
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     7
theory Datatypes
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     8
imports BNF
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
     9
begin
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    10
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    11
section {* Introduction *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    12
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    13
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    14
The 2013 edition of Isabelle introduced new definitional package for datatypes
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    15
and codatatypes. The datatype support is similar to that provided by the earlier
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    16
package due to Berghofer and Wenzel \cite{Berghofer-Wenzel:1999:TPHOL};
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    17
indeed, replacing \cmd{datatype} by \cmd{datatype\_new} is usually sufficient
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    18
to port existing specifications to the new package. What makes the new package
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    19
attractive is that it supports definitions with recursion through a large class
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    20
of non-datatypes, notably finite sets:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    21
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    22
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    23
datatype_new 'a tree = Node 'a "('a tree fset)"
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    24
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    25
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    26
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    27
Another advantage of the new package is that it supports local definitions:
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    28
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    29
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    30
context linorder
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    31
begin
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    32
  datatype_new flag = Less | Eq | Greater
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    33
end
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    34
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    35
text {*
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    36
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    37
Finally, the package also provides some convenience, notably automatically
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    38
generated destructors. For example, the command
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    39
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    40
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    41
(*<*)hide_const Nil Cons hd tl(*>*)
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    42
datatype_new 'a list = null: Nil | Cons (hd: 'a) (tl: "'a list")
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    43
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    44
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    45
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    46
introduces a discriminator @{const null} and a pair of selectors @{const hd} and
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    47
@{const tl} characterized as follows:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    48
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    49
@{thm [display] list.collapse[no_vars]}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    50
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    51
The command \cmd{datatype\_new} is expected to displace \cmd{datatype} in a future
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    52
release. Authors of new theories are encouraged to use \cmd{datatype\_new}, and
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    53
maintainers of older theories might want to consider upgrading now.
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    54
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    55
The package also provides codatatypes (or ``coinductive datatypes''), which may
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    56
have infinite values. The following command introduces a type of infinite
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    57
streams:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    58
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    59
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    60
codatatype 'a stream = Stream 'a "('a stream)"
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    61
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    62
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    63
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    64
Mixed inductive--coinductive recursion is possible via nesting.
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    65
Compare the following four examples:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    66
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    67
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    68
datatype_new 'a treeFF = TreeFF 'a "('a treeFF list)"
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    69
datatype_new 'a treeFI = TreeFI 'a "('a treeFF stream)"
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    70
codatatype 'a treeIF = TreeIF 'a "('a treeFF list)"
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    71
codatatype 'a treeII = TreeII 'a "('a treeFF stream)"
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    72
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    73
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    74
To use the package, it is necessary to import the @{theory BNF} theory, which
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    75
can be precompiled as the \textit{HOL-BNF}:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    76
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    77
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    78
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    79
\noindent
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    80
\texttt{isabelle build -b HOL-BNF}
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
  * TODO: LCF tradition
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    85
  * internals: LICS paper
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    86
*}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
    87
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    88
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    89
This tutorial is organized as follows:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    90
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    91
  * datatypes
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    92
  * primitive recursive functions
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    93
  * codatatypes
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    94
  * primitive corecursive functions
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    95
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    96
the following sections focus on more technical aspects:
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    97
how to register bounded natural functors (BNFs), i.e., type
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    98
constructors via which (co)datatypes can (co)recurse,
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
    99
and how to derive convenience theorems for free constructors,
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   100
as performed internally by \cmd{datatype\_new} and \cmd{codatatype}.
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   101
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   102
then: Standard ML interface
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   103
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   104
interaction with other tools
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   105
  * code generator (and Quickcheck)
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   106
  * Nitpick
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   107
*}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   108
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   109
section {* Defining Datatypes *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   110
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   111
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   112
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   113
subsubsection {* Nonrecursive Types *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   114
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   115
subsubsection {* Simple Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   116
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   117
subsubsection {* Mutual Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   118
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   119
subsubsection {* Nested Recursion *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   120
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   121
subsubsection {* Auxiliary Constants *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   122
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   123
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   124
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   125
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   126
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   127
subsection {* Compatibility Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   128
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   129
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   130
section {* Defining Primitive Recursive Functions *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   131
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   132
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   133
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   134
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   135
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   136
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   137
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   138
subsection {* Compatibility Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   139
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   140
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   141
section {* Defining Codatatypes *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   142
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   143
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   144
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   145
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   146
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   147
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   148
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   149
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   150
section {* Defining Primitive Corecursive Functions *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   151
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   152
subsection {* Introductory Examples *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   153
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   154
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   155
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   156
subsection {* Characteristic Theorems *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   157
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   158
subsection {* Compatibility Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   159
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   160
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   161
section {* Registering Bounded Natural Functors *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   162
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   163
subsection {* Introductory Example *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   164
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   165
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   166
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   167
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   168
section {* Generating Free Constructor Theorems *}
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   169
52794
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   170
subsection {* Introductory Example *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   171
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   172
subsection {* General Syntax *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   173
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   174
section {* Registering Bounded Natural Functors *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   175
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   176
section {* Standard ML Interface *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   177
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   178
section {* Interoperability Issues *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   179
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   180
subsection {* Transfer and Lifting *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   181
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   182
subsection {* Code Generator *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   183
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   184
subsection {* Quickcheck *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   185
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   186
subsection {* Nitpick *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   187
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   188
subsection {* Nominal Isabelle *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   189
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   190
section {* Known Bugs and Limitations *}
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   191
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   192
text {*
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   193
* slow n-ary mutual (co)datatype, avoid as much as possible (e.g. using nesting)
aae782070611 more (co)datatype documentation
blanchet
parents: 52792
diff changeset
   194
*}
52792
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   195
3e651be14fcd sketched documentation for new (co)datatype package
blanchet
parents:
diff changeset
   196
end