src/Doc/Corec/Corec.thy
author blanchet
Tue, 29 Mar 2016 19:11:03 +0200
changeset 62745 257a022f7e7b
parent 62742 bfb5a70e4319
child 62747 f65ef4723aca
permissions -rw-r--r--
more 'corec' docs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     1
(*  Title:      Doc/Corec/Corec.thy
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     3
    Author:     Aymeric Bouzy, Ecole polytechnique
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     4
    Author:     Andreas Lochbihler, ETH Zuerich
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     5
    Author:     Andrei Popescu, Middlesex University
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     6
    Author:     Dmitriy Traytel, ETH Zuerich
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     7
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     8
Tutorial for nonprimitively corecursive definitions.
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
     9
*)
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    10
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    11
theory Corec
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    12
imports
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    13
  GCD
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    14
  "../Datatypes/Setup"
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    15
  "~~/src/HOL/Library/BNF_Corec"
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    16
  "~~/src/HOL/Library/FSet"
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    17
begin
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    18
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    19
section \<open>Introduction
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    20
  \label{sec:introduction}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    21
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    22
text \<open>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    23
...
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    24
\cite{isabelle-datatypes}
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    25
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    26
* friend
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    27
* up to
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    28
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
    29
* versioning
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
    30
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    31
BNF
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    32
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    33
link to papers
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    34
\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    35
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    36
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    37
section \<open>Introductory Examples
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    38
  \label{sec:introductory-examples}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    39
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    40
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    41
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    42
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    43
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    44
subsection \<open>Simple Corecursion
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    45
  \label{ssec:simple-corecursion}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
    46
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    47
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    48
The case studies by Rutten~\cite{rutten05} and Hinze~\cite{hinze10} on stream
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    49
calculi serve as our starting point. Streams can be defined as follows
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    50
(cf. @{file "~~/src/HOL/Library/Stream.thy"}):
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    51
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    52
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    53
    codatatype (sset: 'a) stream =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    54
      SCons (shd: 'a) (stl: "'a stream")
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    55
    for
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    56
      map: smap
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    57
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    58
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    59
The @{command corec} command makes it possible to define functions where the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    60
corecursive call occurs under two or more constructors:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    61
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    62
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    63
    corec oneTwos :: "nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    64
      "oneTwos = SCons 1 (SCons 2 oneTwos)"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    65
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
    66
thm oneTwos.cong_intros
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
    67
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    68
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    69
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    70
This is already beyond the syntactic fragment supported by \keyw{primcorec}.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    71
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    72
The following definition of pointwise sum can be performed with either
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    73
\keyw{primcorec} or @{command corec}:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    74
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    75
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    76
    primcorec ssum :: "('a :: plus) stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    77
      "ssum xs ys = SCons (shd xs + shd ys) (ssum (stl xs) (stl ys))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    78
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    79
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    80
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    81
Pointwise sum meets the friendliness criterion. We register it as a friend using
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    82
the @{command friend_of_corec} command. The command requires us to give a
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    83
specification of @{const ssum} where a constructor (@{const SCons}) occurs at
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    84
the outermost position on the right-hand side. Here, we can simply reuse the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    85
\keyw{primcorec} specification above:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    86
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    87
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    88
    friend_of_corec ssum :: "('a :: plus) stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    89
      "ssum xs ys = SCons (shd xs + shd ys) (ssum (stl xs) (stl ys))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    90
       apply (rule ssum.code)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    91
      by transfer_prover
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    92
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    93
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    94
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    95
The command emits two proof goals. The first one corresponds to the equation we
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    96
specified and is trivial to discharge. The second one is a parametricity goal
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    97
and can usually be discharged using the @{text transfer_prover} proof method.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    98
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
    99
After registering @{const ssum} as a friend, we can use it in the corecursive
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   100
call context, either inside or outside the constructor guard:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   101
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   102
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   103
    corec fibA :: "nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   104
      "fibA = SCons 0 (ssum (SCons 1 fibA) fibA)"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   105
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   106
text \<open>\blankline\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   107
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   108
    corec fibB :: "nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   109
      "fibB = ssum (SCons 0 (SCons 1 fibB)) (SCons 0 fibB)"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   110
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   111
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   112
Using the @{text "friend"} option, we can simultaneously define a function and
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   113
register it as a friend:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   114
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   115
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   116
    corec (friend)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   117
      sprod :: "('a :: {plus,times}) stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   118
    where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   119
      "sprod xs ys =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   120
       SCons (shd xs * shd ys) (ssum (sprod xs (stl ys)) (sprod (stl xs) ys))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   121
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   122
text \<open>\blankline\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   123
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   124
    corec (friend) sexp :: "nat stream \<Rightarrow> nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   125
      "sexp xs = SCons (2 ^^ shd xs) (sprod (stl xs) (sexp xs))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   126
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   127
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   128
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   129
The parametricity proof goal is given to @{text transfer_prover}.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   130
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   131
The @{const sprod} and @{const sexp} functions provide shuffle product and
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   132
exponentiation on streams. We can use them to define the stream of factorial
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   133
numbers in two different ways:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   134
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   135
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   136
    corec factA :: "nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   137
      "factA = (let zs = SCons 1 factA in sprod zs zs)"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   138
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   139
    corec factB :: "nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   140
      "factB = sexp (SCons 0 factB)"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   141
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   142
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   143
The arguments of friendly operations can be of complex types involving the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   144
target codatatype. The following example defines the supremum of a finite set of
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   145
streams by primitive corecursion and registers it as friendly:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   146
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   147
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   148
    corec (friend) sfsup :: "nat stream fset \<Rightarrow> nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   149
      "sfsup X = SCons (Sup (fset (fimage shd X))) (sfsup (fimage stl X))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   150
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   151
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   152
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   153
In general, the arguments may be any BNF, with the restriction that the target
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   154
codatatype (@{typ "nat stream"}) may occur only in a live position of the BNF.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   155
For this reason, the following operation, on unbounded sets, cannot be
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   156
registered as a friend:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   157
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   158
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   159
    corec ssup :: "nat stream set \<Rightarrow> nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   160
      "ssup X = SCons (Sup (image shd X)) (ssup (image stl X))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   161
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   162
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   163
subsection \<open>Nested Corecursion
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   164
  \label{ssec:nested-corecursion}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   165
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   166
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   167
The package generally supports arbitrary codatatypes with multiple constructors
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   168
and nesting through other type constructors (BNFs). Consider the following type
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   169
of finitely branching Rose trees of potentially infinite depth:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   170
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   171
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   172
    codatatype 'a tree =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   173
      Node (lab: 'a) (sub: "'a tree list")
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   174
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   175
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   176
We first define the pointwise sum of two trees analogously to @{const ssum}:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   177
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   178
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   179
    corec (friend) tplus :: "('a :: plus) tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   180
      "tplus t u =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   181
       Node (lab t + lab u) (map (\<lambda>(t', u'). tplus t' u') (zip (sub t) (sub u)))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   182
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   183
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   184
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   185
Here, @{const map} is the standard map function on lists, and @{const zip}
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   186
converts two parallel lists into a list of pairs. The @{const tplus} function is
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   187
primitively corecursive. Instead of @{text "corec (friend)"}, we could also have
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   188
used \keyw{primcorec} and @{command friend_of_corec}, as we did for
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   189
@{const ssum}.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   190
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   191
Once @{const tplus} is registered as friendly, we can use it in the corecursive
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   192
call context:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   193
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   194
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   195
    corec (friend) ttimes :: "('a :: {plus,times}) tree \<Rightarrow> 'a tree \<Rightarrow> 'a tree" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   196
      "ttimes t u = Node (lab t * lab u)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   197
         (map (\<lambda>(t', u'). tplus (ttimes t u') (ttimes t' u)) (zip (sub t) (sub u)))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   198
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   199
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   200
subsection \<open>Mixed Recursion--Corecursion
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   201
  \label{ssec:mixed-recursion-corecursion}\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   202
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   203
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   204
It is often convenient to let a corecursive function perform some finite
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   205
computation before producing a constructor. With mixed recursion--corecursion, a
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   206
finite number of unguarded recursive calls perform this calculation before
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   207
reaching a guarded corecursive call.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   208
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   209
Intuitively, the unguarded recursive call can be unfolded to arbitrary finite
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   210
depth, ultimately yielding a purely corecursive definition. An example is the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   211
@{term primes} function from Di Gianantonio and Miculan
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   212
\cite{di-gianantonio-miculan-2003}:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   213
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   214
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   215
    corecursive primes :: "nat \<Rightarrow> nat \<Rightarrow> nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   216
      "primes m n =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   217
       (if (m = 0 \<and> n > 1) \<or> coprime m n then
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   218
          SCons n (primes (m * n) (n + 1))
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   219
        else
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   220
          primes m (n + 1))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   221
      apply (relation "measure (\<lambda>(m, n).
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   222
        if n = 0 then 1 else if coprime m n then 0 else m - n mod m)")
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   223
       apply (auto simp: mod_Suc intro: Suc_lessI)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   224
       apply (metis One_nat_def coprime_Suc_nat gcd.commute gcd_red_nat)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   225
      by (metis diff_less_mono2 lessI mod_less_divisor)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   226
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   227
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   228
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   229
The @{command corecursive} command is a variant of @{command corec} that allows
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   230
us to specify a termination argument for any unguarded self-call.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   231
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   232
When called with @{term "m = 1"} and @{term "n = 2"}, the @{const primes}
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   233
function computes the stream of prime numbers. The unguarded call in the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   234
@{text else} branch increments @{term n} until it is coprime to the first
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   235
argument @{term m} (i.e., the greatest common divisor of @{term m} and
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   236
@{term n} is @{term 1}).
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   237
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   238
For any positive integers @{term m} and @{term n}, the numbers @{term m} and
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   239
@{term "m * n + 1"} are coprime, yielding an upper bound on the number of times
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   240
@{term n} is increased. Hence, the function will take the @{text else} branch at
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   241
most finitely often before taking the then branch and producing one constructor.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   242
There is a slight complication when @{term "m = 0 \<and> n > 1"}: Without the first
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   243
disjunct in the @{text "if"} condition, the function could stall. (This corner
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   244
case was overlooked in the original example \cite{di-gianantonio-miculan-2003}.)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   245
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   246
In the following example, which defines the stream of Catalan numbers,
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   247
termination is discharged automatically using @{text lexicographic_order}:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   248
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   249
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   250
    corec catalan :: "nat \<Rightarrow> nat stream" where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   251
      "catalan n =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   252
       (if n > 0 then ssum (catalan (n - 1)) (SCons 0 (catalan (n + 1)))
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   253
        else SCons 1 (catalan 1))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   254
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   255
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   256
A more elaborate case study, revolving around the filter function on lazy lists,
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   257
is presented in @{file "~~/src/HOL/Corec_Examples/LFilter.thy"}.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   258
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   259
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   260
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   261
subsection \<open>Self-Friendship
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   262
  \label{ssec:self-friendship}\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   263
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   264
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   265
Paradoxically, the package allows us to simultaneously define a function and use
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   266
it as its own friend, as in the following definition of a ``skewed product'':
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   267
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   268
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   269
    corec (friend)
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   270
      sskew :: "('a :: {plus,times}) stream \<Rightarrow> 'a stream \<Rightarrow> 'a stream"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   271
    where
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   272
      "sskew xs ys =
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   273
       SCons (shd xs * shd ys) (sskew (sskew xs (stl ys)) (sskew (stl xs) ys))"
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   274
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   275
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   276
\noindent
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   277
Such definitions, with nested self-calls on the right-hand side, cannot be
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   278
separated into a @{command corec} part and a @{command friend_of_corec} part.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   279
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   280
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   281
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   282
subsection \<open>Coinduction
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   283
  \label{ssec:coinduction}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   284
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   285
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   286
Once a corecursive specification has been accepted, we normally want to reason
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   287
about it. The @{text codatatype} command generates a structural coinduction
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   288
principle that matches primitively corecursive functions. For nonprimitive
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   289
specifications, our package provides the more advanced proof principle of
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   290
\emph{coinduction up to congruence}---or simply \emph{coinduction up-to}.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   291
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   292
The structural coinduction principle for @{typ "'a stream"}, called
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   293
@{thm [source] stream.coinduct}, is as follows:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   294
%
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   295
\[@{thm stream.coinduct[no_vars]}\]
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   296
%
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   297
Coinduction allows us to prove an equality @{text "l = r"} on streams by
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   298
providing a relation @{text R} that relates @{text l} and @{text r} (first
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   299
premise) and that constitutes a bisimulation (second premise). Streams that are
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   300
related by a bisimulation cannot be distinguished by taking observations (via
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   301
the selectors @{const shd} and @{const stl}); hence they must be equal.
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   302
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   303
The coinduction up-to principle after registering @{const sskew} as friendly is
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   304
available as @{thm [source] sskew.coinduct} or
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   305
@{thm [source] stream.coinduct_upto(2)}:
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   306
%
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   307
\[@{thm sfsup.coinduct[no_vars]}\]
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   308
%
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   309
This rule is almost identical to structural coinduction, except that the
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   310
corecursive application of @{term R} is replaced by
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   311
@{term "stream.v5.congclp R"}. The @{const stream.v5.congclp} predicate is
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   312
equipped with the following introduction rules:
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   313
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   314
\begin{indentblock}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   315
\begin{description}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   316
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   317
\item[@{thm [source] sskew.cong_base}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   318
@{thm sskew.cong_base[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   319
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   320
\item[@{thm [source] sskew.cong_refl}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   321
@{thm sskew.cong_refl[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   322
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   323
\item[@{thm [source] sskew.cong_sym}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   324
@{thm sskew.cong_sym[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   325
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   326
\item[@{thm [source] sskew.cong_trans}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   327
@{thm sskew.cong_trans[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   328
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   329
\item[@{thm [source] sskew.cong_SCons}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   330
@{thm sskew.cong_SCons[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   331
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   332
\item[@{thm [source] sskew.cong_ssum}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   333
@{thm sskew.cong_ssum[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   334
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   335
\item[@{thm [source] sskew.cong_sprod}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   336
@{thm sskew.cong_sprod[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   337
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   338
\item[@{thm [source] sskew.cong_sskew}\rm:] ~ \\
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   339
@{thm sskew.cong_sskew[no_vars]}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   340
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   341
\end{description}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   342
\end{indentblock}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   343
%
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   344
The introduction rules are also available as
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   345
@{thm [source] sskew.cong_intros}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   346
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   347
Notice that there is no introduction rule corresponding to @{const sexp},
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   348
because @{const sexp} has a more restrictive result type than @{const sskew}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   349
(@{typ "nat stream"} vs. @{typ "('a :: {plus,times}) stream"}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   350
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   351
Since the package maintains a set of incomparable corecursors, there is also a
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   352
set of associated coinduction principles and a set of sets of introduction
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   353
rules. A technically subtle point is to make Isabelle choose the right rules in
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   354
most situations. For this purpose, the package maintains the collection
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   355
@{thm [source] stream.coinduct_upto} of coinduction principles ordered by
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   356
increasing generality, which works well with Isabelle's philosophy of applying
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   357
the first rule that matches. For example, after registering @{const ssum} as a
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   358
friend, proving the equality @{term "l = r"} on @{typ "nat stream"} might
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   359
require coinduction principle for @{term "nat stream"}, which is up to
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   360
@{const ssum}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   361
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   362
The collection @{thm [source] stream.coinduct_upto} is guaranteed to be complete
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   363
and up to date with respect to the type instances of definitions considered so
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   364
far, but occasionally it may be necessary to take the union of two incomparable
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   365
coinduction principles. This can be done using the @{command coinduction_upto}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   366
command. Consider the following definitions:
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   367
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   368
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   369
    codatatype (tset: 'a, 'b) tllist =
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   370
      TNil (terminal : 'b)
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   371
    | TCons (thd : 'a) (ttl : "('a, 'b) tllist")
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   372
    for
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   373
      map: tmap
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   374
      rel: tllist_all2
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   375
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   376
    corec (friend) square_elems :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   377
      "square_elems xs =
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   378
       (case xs of
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   379
         TNil z \<Rightarrow> TNil z
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   380
       | TCons y ys \<Rightarrow> TCons (y ^^ 2) (square_elems ys))"
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   381
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   382
    corec (friend) square_terminal :: "('a, int) tllist \<Rightarrow> ('a, int) tllist" where
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   383
      "square_terminal xs =
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   384
       (case xs of
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   385
         TNil z \<Rightarrow> TNil (z ^^ 2)
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   386
       | TCons y ys \<Rightarrow> TCons y (square_terminal ys))"
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   387
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   388
text \<open>
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   389
At this point, @{thm [source] tllist.coinduct_upto} contains three variants of the
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   390
coinduction principles:
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   391
%
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   392
\begin{itemize}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   393
\item @{typ "('a, int) tllist"} up to @{const TNil}, @{const TCons}, and
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   394
  @{const square_terminal};
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   395
\item @{typ "(nat, 'b) tllist"} up to @{const TNil}, @{const TCons}, and
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   396
  @{const square_elems};
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   397
\item @{typ "('a, 'b) tllist"} up to @{const TNil} and @{const TCons}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   398
\end{itemize}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   399
%
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   400
The following variant is missing:
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   401
%
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   402
\begin{itemize}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   403
\item @{typ "(nat, int) tllist"} up to @{const TNil}, @{const TCons},
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   404
  @{const square_elems}, and @{const square_terminal}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   405
\end{itemize}
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   406
%
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   407
To generate it, without having to define a new function with @{command corec},
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   408
we can use the following command:
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   409
\<close>
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   410
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   411
    coinduction_upto nat_int_tllist: "(nat, int) tllist"
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   412
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   413
text \<open>
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   414
This produces the theorems @{thm [source] nat_int_tllist.coinduct_upto} and
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   415
@{thm [source] nat_int_tllist.cong_intros} (as well as the individually named
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   416
introduction rules), and extends @{thm [source] tllist.coinduct_upto}.
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   417
\<close>
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   418
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   419
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   420
subsection \<open>Uniqueness Reasoning
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   421
  \label{ssec:uniqueness-reasoning}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   422
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   423
text \<open>
62745
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   424
t is sometimes possible to achieve better automation by using a more specialized
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   425
proof method than coinduction. Uniqueness principles maintain a good balance
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   426
between expressiveness and automation. They exploit the property that a
257a022f7e7b more 'corec' docs
blanchet
parents: 62742
diff changeset
   427
corecursive specification is the unique solution to a fixpoint equation.
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   428
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   429
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   430
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   431
section \<open>Command Syntax
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   432
  \label{sec:command-syntax}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   433
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   434
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   435
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   436
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   437
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   438
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   439
subsection \<open>\keyw{corec} and \keyw{corecursive}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   440
  \label{ssec:corec-and-corecursive}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   441
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   442
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   443
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   444
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   445
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   446
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   447
subsection \<open>\keyw{friend_of_corec}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   448
  \label{ssec:friend-of-corec}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   449
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   450
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   451
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   452
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   453
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   454
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   455
subsection \<open>\keyw{coinduction_upto}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   456
  \label{ssec:coinduction-upto}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   457
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   458
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   459
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   460
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   461
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   462
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   463
section \<open>Generated Theorems
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   464
  \label{sec:generated-theorems}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   465
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   466
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   467
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   468
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   469
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   470
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   471
subsection \<open>\keyw{corec} and \keyw{corecursive}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   472
  \label{ssec:corec-and-corecursive}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   473
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   474
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   475
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   476
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   477
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   478
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   479
subsection \<open>\keyw{friend_of_corec}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   480
  \label{ssec:friend-of-corec}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   481
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   482
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   483
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   484
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   485
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   486
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   487
subsection \<open>\keyw{coinduction_upto}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   488
  \label{ssec:coinduction-upto}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   489
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   490
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   491
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   492
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   493
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   494
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   495
section \<open>Proof Method
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   496
  \label{sec:proof-method}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   497
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   498
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   499
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   500
\<close>
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   501
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   502
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   503
subsection \<open>\textit{corec_unique}
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   504
  \label{ssec:corec-unique}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   505
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   506
text \<open>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   507
...
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   508
\<close>
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   509
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   510
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   511
section \<open>Known Bugs and Limitations
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   512
  \label{sec:known-bugs-and-limitations}\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   513
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   514
text \<open>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   515
This section lists the known bugs and limitations of the corecursion package at
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   516
the time of this writing.
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   517
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   518
\begin{enumerate}
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   519
\setlength{\itemsep}{0pt}
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   520
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   521
\item
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   522
\emph{TODO.} TODO.
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   523
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   524
  * no mutual types
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   525
  * limitation on type of friend
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   526
  * unfinished tactics
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   527
  * polymorphism of corecUU_transfer
62742
bfb5a70e4319 more 'corec' documentation
blanchet
parents: 62739
diff changeset
   528
  * alternative views
62739
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   529
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   530
\end{enumerate}
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   531
\<close>
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   532
628c97d39627 added sketchy 'corec' documentation
blanchet
parents:
diff changeset
   533
end