src/HOL/Induct/LList.ML
author paulson
Wed, 24 Dec 1997 10:02:30 +0100
changeset 4477 b3e5857d8d99
parent 4160 59826ea67cba
child 4521 c7f56322a84b
permissions -rw-r--r--
New Auto_tac (by Oheimb), and new syntax (without parens), and expandshort
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     1
(*  Title:      HOL/ex/LList
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     2
    ID:         $Id$
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     5
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     6
SHOULD LListD_Fun_CONS_I, etc., be equations (for rewriting)?
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     7
*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     8
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
     9
open LList;
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    10
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    11
bind_thm ("UN1_I", UNIV_I RS UN_I);
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    12
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    13
(** Simplification **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    14
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
    15
simpset_ref() := simpset() addsplits [expand_split, expand_sum_case];
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    16
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    17
(*For adding _eqI rules to a simpset; we must remove Pair_eq because
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    18
  it may turn an instance of reflexivity into a conjunction!*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    19
fun add_eqI ss = ss addsimps [range_eqI, image_eqI] 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    20
                    delsimps [Pair_eq];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    21
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    22
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    23
(*This justifies using llist in other recursive type definitions*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    24
goalw LList.thy llist.defs "!!A B. A<=B ==> llist(A) <= llist(B)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    25
by (rtac gfp_mono 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    26
by (REPEAT (ares_tac basic_monos 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    27
qed "llist_mono";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    28
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    29
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    30
goal LList.thy "llist(A) = {Numb(0)} <+> (A <*> llist(A))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    31
let val rew = rewrite_rule [NIL_def, CONS_def] in  
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
    32
by (fast_tac (claset() addSIs (map rew llist.intrs)
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    33
                      addEs [rew llist.elim]) 1)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    34
end;
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    35
qed "llist_unfold";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    36
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    37
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    38
(*** Type checking by coinduction, using list_Fun 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    39
     THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS!
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    40
***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    41
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    42
goalw LList.thy [list_Fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    43
    "!!M. [| M : X;  X <= list_Fun A (X Un llist(A)) |] ==>  M : llist(A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    44
by (etac llist.coinduct 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    45
by (etac (subsetD RS CollectD) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    46
by (assume_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    47
qed "llist_coinduct";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    48
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    49
goalw LList.thy [list_Fun_def, NIL_def] "NIL: list_Fun A X";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    50
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    51
qed "list_Fun_NIL_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    52
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    53
goalw LList.thy [list_Fun_def,CONS_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    54
    "!!M N. [| M: A;  N: X |] ==> CONS M N : list_Fun A X";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    55
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    56
qed "list_Fun_CONS_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    57
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    58
(*Utilise the "strong" part, i.e. gfp(f)*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    59
goalw LList.thy (llist.defs @ [list_Fun_def])
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    60
    "!!M N. M: llist(A) ==> M : list_Fun A (X Un llist(A))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    61
by (etac (llist.mono RS gfp_fun_UnI2) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    62
qed "list_Fun_llist_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    63
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    64
(*** LList_corec satisfies the desired recurion equation ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    65
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    66
(*A continuity result?*)
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
    67
goalw LList.thy [CONS_def] "CONS M (UN x. f(x)) = (UN x. CONS M (f x))";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
    68
by (simp_tac (simpset() addsimps [In1_UN1, Scons_UN1_y]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    69
qed "CONS_UN1";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    70
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    71
(*UNUSED; obsolete?
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
    72
goal Prod.thy "split p (%x y. UN z. f x y z) = (UN z. split p (%x y. f x y z))";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
    73
by (simp_tac (simpset() addsplits [expand_split]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    74
qed "split_UN1";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    75
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
    76
goal Sum.thy "sum_case s f (%y. UN z. g y z) = (UN z. sum_case s f (%y. g y z))";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
    77
by (simp_tac (simpset() addsplits [expand_sum_case]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    78
qed "sum_case2_UN1";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    79
*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    80
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    81
val prems = goalw LList.thy [CONS_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    82
    "[| M<=M';  N<=N' |] ==> CONS M N <= CONS M' N'";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    83
by (REPEAT (resolve_tac ([In1_mono,Scons_mono]@prems) 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    84
qed "CONS_mono";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    85
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    86
Addsimps [LList_corec_fun_def RS def_nat_rec_0,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    87
          LList_corec_fun_def RS def_nat_rec_Suc];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    88
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    89
(** The directions of the equality are proved separately **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    90
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    91
goalw LList.thy [LList_corec_def]
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
    92
    "LList_corec a f <= sum_case (%u. NIL) \
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    93
\                          (split(%z w. CONS z (LList_corec w f))) (f a)";
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    94
by (rtac UN_least 1);
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    95
by (exhaust_tac "k" 1);
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    96
by (ALLGOALS Asm_simp_tac);
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    97
by (REPEAT (resolve_tac [allI, impI, subset_refl RS CONS_mono, 
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
    98
			 UNIV_I RS UN_upper] 1));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
    99
qed "LList_corec_subset1";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   100
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   101
goalw LList.thy [LList_corec_def]
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   102
    "sum_case (%u. NIL) (split(%z w. CONS z (LList_corec w f))) (f a) <= \
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   103
\    LList_corec a f";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   104
by (simp_tac (simpset() addsimps [CONS_UN1]) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   105
by Safe_tac;
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   106
by (ALLGOALS (res_inst_tac [("a","Suc(?k)")] UN_I));
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   107
by (ALLGOALS Asm_simp_tac);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   108
qed "LList_corec_subset2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   109
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   110
(*the recursion equation for LList_corec -- NOT SUITABLE FOR REWRITING!*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   111
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   112
    "LList_corec a f = sum_case (%u. NIL) \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   113
\                           (split(%z w. CONS z (LList_corec w f))) (f a)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   114
by (REPEAT (resolve_tac [equalityI, LList_corec_subset1, 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   115
                         LList_corec_subset2] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   116
qed "LList_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   117
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   118
(*definitional version of same*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   119
val [rew] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   120
    "[| !!x. h(x) == LList_corec x f |] ==>     \
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   121
\    h(a) = sum_case (%u. NIL) (split(%z w. CONS z (h w))) (f a)";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   122
by (rewtac rew);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   123
by (rtac LList_corec 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   124
qed "def_LList_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   125
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   126
(*A typical use of co-induction to show membership in the gfp. 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   127
  Bisimulation is  range(%x. LList_corec x f) *)
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   128
goal LList.thy "LList_corec a f : llist({u. True})";
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   129
by (res_inst_tac [("X", "range(%x. LList_corec x ?g)")] llist_coinduct 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   130
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   131
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   132
by (stac LList_corec 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   133
by (simp_tac (simpset() addsimps [list_Fun_NIL_I, list_Fun_CONS_I, CollectI]
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   134
                       |> add_eqI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   135
qed "LList_corec_type";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   136
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   137
(*Lemma for the proof of llist_corec*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   138
goal LList.thy
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   139
   "LList_corec a (%z. sum_case Inl (split(%v w. Inr((Leaf(v),w)))) (f z)) : \
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   140
\   llist(range Leaf)";
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   141
by (res_inst_tac [("X", "range(%x. LList_corec x ?g)")] llist_coinduct 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   142
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   143
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   144
by (stac LList_corec 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   145
by (asm_simp_tac (simpset() addsimps [list_Fun_NIL_I]) 1);
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   146
by (fast_tac (claset() addSIs [list_Fun_CONS_I]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   147
qed "LList_corec_type2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   148
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   149
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   150
(**** llist equality as a gfp; the bisimulation principle ****)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   151
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   152
(*This theorem is actually used, unlike the many similar ones in ZF*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   153
goal LList.thy "LListD(r) = diag({Numb(0)}) <++> (r <**> LListD(r))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   154
let val rew = rewrite_rule [NIL_def, CONS_def] in  
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   155
by (fast_tac (claset() addSIs (map rew LListD.intrs)
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   156
                      addEs [rew LListD.elim]) 1)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   157
end;
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   158
qed "LListD_unfold";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   159
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   160
goal LList.thy "!M N. (M,N) : LListD(diag(A)) --> ntrunc k M = ntrunc k N";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   161
by (res_inst_tac [("n", "k")] less_induct 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   162
by (safe_tac ((claset_of Fun.thy) delrules [equalityI]));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   163
by (etac LListD.elim 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   164
by (safe_tac (claset_of Prod.thy delrules [equalityI] addSEs [diagE]));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   165
by (res_inst_tac [("n", "n")] natE 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   166
by (asm_simp_tac (simpset() addsimps [ntrunc_0]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   167
by (rename_tac "n'" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   168
by (res_inst_tac [("n", "n'")] natE 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   169
by (asm_simp_tac (simpset() addsimps [CONS_def, ntrunc_one_In1]) 1);
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   170
by (asm_simp_tac (simpset() addsimps [CONS_def, ntrunc_In1, ntrunc_Scons, less_Suc_eq]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   171
qed "LListD_implies_ntrunc_equality";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   172
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   173
(*The domain of the LListD relation*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   174
goalw LList.thy (llist.defs @ [NIL_def, CONS_def])
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   175
    "fst``LListD(diag(A)) <= llist(A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   176
by (rtac gfp_upperbound 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   177
(*avoids unfolding LListD on the rhs*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   178
by (res_inst_tac [("P", "%x. fst``x <= ?B")] (LListD_unfold RS ssubst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   179
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   180
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   181
qed "fst_image_LListD";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   182
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   183
(*This inclusion justifies the use of coinduction to show M=N*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   184
goal LList.thy "LListD(diag(A)) <= diag(llist(A))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   185
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   186
by (res_inst_tac [("p","x")] PairE 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   187
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   188
by (rtac diag_eqI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   189
by (rtac (LListD_implies_ntrunc_equality RS spec RS spec RS mp RS 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   190
          ntrunc_equality) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   191
by (assume_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   192
by (etac (fst_imageI RS (fst_image_LListD RS subsetD)) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   193
qed "LListD_subset_diag";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   194
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   195
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   196
(** Coinduction, using LListD_Fun
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   197
    THE COINDUCTIVE DEFINITION PACKAGE COULD DO THIS!
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   198
 **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   199
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   200
goalw thy [LListD_Fun_def] "!!A B. A<=B ==> LListD_Fun r A <= LListD_Fun r B";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   201
by (REPEAT (ares_tac basic_monos 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   202
qed "LListD_Fun_mono";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   203
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   204
goalw LList.thy [LListD_Fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   205
    "!!M. [| M : X;  X <= LListD_Fun r (X Un LListD(r)) |] ==>  M : LListD(r)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   206
by (etac LListD.coinduct 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   207
by (etac (subsetD RS CollectD) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   208
by (assume_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   209
qed "LListD_coinduct";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   210
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   211
goalw LList.thy [LListD_Fun_def,NIL_def] "(NIL,NIL) : LListD_Fun r s";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   212
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   213
qed "LListD_Fun_NIL_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   214
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   215
goalw LList.thy [LListD_Fun_def,CONS_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   216
 "!!x. [| x:A;  (M,N):s |] ==> (CONS x M, CONS x N) : LListD_Fun (diag A) s";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   217
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   218
qed "LListD_Fun_CONS_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   219
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   220
(*Utilise the "strong" part, i.e. gfp(f)*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   221
goalw LList.thy (LListD.defs @ [LListD_Fun_def])
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   222
    "!!M N. M: LListD(r) ==> M : LListD_Fun r (X Un LListD(r))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   223
by (etac (LListD.mono RS gfp_fun_UnI2) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   224
qed "LListD_Fun_LListD_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   225
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   226
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   227
(*This converse inclusion helps to strengthen LList_equalityI*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   228
goal LList.thy "diag(llist(A)) <= LListD(diag(A))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   229
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   230
by (etac LListD_coinduct 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   231
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   232
by (etac diagE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   233
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   234
by (eresolve_tac [llist.elim] 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   235
by (ALLGOALS
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   236
    (asm_simp_tac (simpset() addsimps [diagI, LListD_Fun_NIL_I,
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   237
                                      LListD_Fun_CONS_I])));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   238
qed "diag_subset_LListD";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   239
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   240
goal LList.thy "LListD(diag(A)) = diag(llist(A))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   241
by (REPEAT (resolve_tac [equalityI, LListD_subset_diag, 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   242
                         diag_subset_LListD] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   243
qed "LListD_eq_diag";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   244
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   245
goal LList.thy 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   246
    "!!M N. M: llist(A) ==> (M,M) : LListD_Fun (diag A) (X Un diag(llist(A)))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   247
by (rtac (LListD_eq_diag RS subst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   248
by (rtac LListD_Fun_LListD_I 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   249
by (asm_simp_tac (simpset() addsimps [LListD_eq_diag, diagI]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   250
qed "LListD_Fun_diag_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   251
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   252
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   253
(** To show two LLists are equal, exhibit a bisimulation! 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   254
      [also admits true equality]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   255
   Replace "A" by some particular set, like {x.True}??? *)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   256
goal LList.thy 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   257
    "!!r. [| (M,N) : r;  r <= LListD_Fun (diag A) (r Un diag(llist(A))) \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   258
\         |] ==>  M=N";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   259
by (rtac (LListD_subset_diag RS subsetD RS diagE) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   260
by (etac LListD_coinduct 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   261
by (asm_simp_tac (simpset() addsimps [LListD_eq_diag]) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   262
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   263
qed "LList_equalityI";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   264
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   265
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   266
(*** Finality of llist(A): Uniqueness of functions defined by corecursion ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   267
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   268
(*abstract proof using a bisimulation*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   269
val [prem1,prem2] = goal LList.thy
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   270
 "[| !!x. h1(x) = sum_case (%u. NIL) (split(%z w. CONS z (h1 w))) (f x);  \
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   271
\    !!x. h2(x) = sum_case (%u. NIL) (split(%z w. CONS z (h2 w))) (f x) |]\
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   272
\ ==> h1=h2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   273
by (rtac ext 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   274
(*next step avoids an unknown (and flexflex pair) in simplification*)
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   275
by (res_inst_tac [("A", "{u. True}"),
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   276
                  ("r", "range(%u. (h1(u),h2(u)))")] LList_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   277
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   278
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   279
by (stac prem1 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   280
by (stac prem2 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   281
by (simp_tac (simpset() addsimps [LListD_Fun_NIL_I,
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   282
                                 CollectI RS LListD_Fun_CONS_I]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   283
                       |> add_eqI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   284
qed "LList_corec_unique";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   285
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   286
val [prem] = goal LList.thy
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   287
 "[| !!x. h(x) = sum_case (%u. NIL) (split(%z w. CONS z (h w))) (f x) |] \
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   288
\ ==> h = (%x. LList_corec x f)";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   289
by (rtac (LList_corec RS (prem RS LList_corec_unique)) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   290
qed "equals_LList_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   291
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   292
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   293
(** Obsolete LList_corec_unique proof: complete induction, not coinduction **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   294
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   295
goalw LList.thy [CONS_def] "ntrunc (Suc 0) (CONS M N) = {}";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   296
by (rtac ntrunc_one_In1 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   297
qed "ntrunc_one_CONS";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   298
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   299
goalw LList.thy [CONS_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   300
    "ntrunc (Suc(Suc(k))) (CONS M N) = CONS (ntrunc k M) (ntrunc k N)";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   301
by (simp_tac (simpset() addsimps [ntrunc_Scons,ntrunc_In1]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   302
qed "ntrunc_CONS";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   303
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   304
val [prem1,prem2] = goal LList.thy
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   305
 "[| !!x. h1(x) = sum_case (%u. NIL) (split(%z w. CONS z (h1 w))) (f x);  \
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   306
\    !!x. h2(x) = sum_case (%u. NIL) (split(%z w. CONS z (h2 w))) (f x) |]\
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   307
\ ==> h1=h2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   308
by (rtac (ntrunc_equality RS ext) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   309
by (rename_tac "x k" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   310
by (res_inst_tac [("x", "x")] spec 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   311
by (res_inst_tac [("n", "k")] less_induct 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   312
by (rename_tac "n" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   313
by (rtac allI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   314
by (rename_tac "y" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   315
by (stac prem1 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   316
by (stac prem2 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   317
by (simp_tac (simpset() addsplits [expand_sum_case]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   318
by (strip_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   319
by (res_inst_tac [("n", "n")] natE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   320
by (rename_tac "m" 2);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   321
by (res_inst_tac [("n", "m")] natE 2);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   322
by (ALLGOALS(asm_simp_tac(simpset() addsimps
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   323
            [ntrunc_0,ntrunc_one_CONS,ntrunc_CONS, less_Suc_eq])));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   324
result();
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   325
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   326
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   327
(*** Lconst -- defined directly using lfp, but equivalent to a LList_corec ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   328
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   329
goal LList.thy "mono(CONS(M))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   330
by (REPEAT (ares_tac [monoI, subset_refl, CONS_mono] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   331
qed "Lconst_fun_mono";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   332
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   333
(* Lconst(M) = CONS M (Lconst M) *)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   334
bind_thm ("Lconst", (Lconst_fun_mono RS (Lconst_def RS def_lfp_Tarski)));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   335
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   336
(*A typical use of co-induction to show membership in the gfp.
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   337
  The containing set is simply the singleton {Lconst(M)}. *)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   338
goal LList.thy "!!M A. M:A ==> Lconst(M): llist(A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   339
by (rtac (singletonI RS llist_coinduct) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   340
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   341
by (res_inst_tac [("P", "%u. u: ?A")] (Lconst RS ssubst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   342
by (REPEAT (ares_tac [list_Fun_CONS_I, singletonI, UnI1] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   343
qed "Lconst_type";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   344
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   345
goal LList.thy "Lconst(M) = LList_corec M (%x. Inr((x,x)))";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   346
by (rtac (equals_LList_corec RS fun_cong) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   347
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   348
by (rtac Lconst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   349
qed "Lconst_eq_LList_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   350
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   351
(*Thus we could have used gfp in the definition of Lconst*)
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   352
goal LList.thy "gfp(%N. CONS M N) = LList_corec M (%x. Inr((x,x)))";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   353
by (rtac (equals_LList_corec RS fun_cong) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   354
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   355
by (rtac (Lconst_fun_mono RS gfp_Tarski) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   356
qed "gfp_Lconst_eq_LList_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   357
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   358
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   359
(*** Isomorphisms ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   360
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   361
goal LList.thy "inj(Rep_llist)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   362
by (rtac inj_inverseI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   363
by (rtac Rep_llist_inverse 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   364
qed "inj_Rep_llist";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   365
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   366
goal LList.thy "inj_onto Abs_llist (llist(range Leaf))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   367
by (rtac inj_onto_inverseI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   368
by (etac Abs_llist_inverse 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   369
qed "inj_onto_Abs_llist";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   370
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   371
(** Distinctness of constructors **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   372
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   373
goalw LList.thy [LNil_def,LCons_def] "~ LCons x xs = LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   374
by (rtac (CONS_not_NIL RS (inj_onto_Abs_llist RS inj_onto_contraD)) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   375
by (REPEAT (resolve_tac (llist.intrs @ [rangeI, Rep_llist]) 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   376
qed "LCons_not_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   377
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   378
bind_thm ("LNil_not_LCons", LCons_not_LNil RS not_sym);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   379
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   380
AddIffs [LCons_not_LNil, LNil_not_LCons];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   381
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   382
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   383
(** llist constructors **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   384
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   385
goalw LList.thy [LNil_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   386
    "Rep_llist(LNil) = NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   387
by (rtac (llist.NIL_I RS Abs_llist_inverse) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   388
qed "Rep_llist_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   389
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   390
goalw LList.thy [LCons_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   391
    "Rep_llist(LCons x l) = CONS (Leaf x) (Rep_llist l)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   392
by (REPEAT (resolve_tac [llist.CONS_I RS Abs_llist_inverse,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   393
                         rangeI, Rep_llist] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   394
qed "Rep_llist_LCons";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   395
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   396
(** Injectiveness of CONS and LCons **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   397
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   398
goalw LList.thy [CONS_def] "(CONS M N=CONS M' N') = (M=M' & N=N')";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   399
by (fast_tac (claset() addSEs [Scons_inject]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   400
qed "CONS_CONS_eq2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   401
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   402
bind_thm ("CONS_inject", (CONS_CONS_eq RS iffD1 RS conjE));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   403
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   404
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   405
(*For reasoning about abstract llist constructors*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   406
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   407
AddIs ([Rep_llist]@llist.intrs);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   408
AddSDs [inj_onto_Abs_llist RS inj_ontoD,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   409
        inj_Rep_llist RS injD, Leaf_inject];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   410
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   411
goalw LList.thy [LCons_def] "(LCons x xs=LCons y ys) = (x=y & xs=ys)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   412
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   413
qed "LCons_LCons_eq";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   414
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   415
AddIffs [LCons_LCons_eq];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   416
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   417
val [major] = goal LList.thy "CONS M N: llist(A) ==> M: A & N: llist(A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   418
by (rtac (major RS llist.elim) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   419
by (etac CONS_neq_NIL 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   420
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   421
qed "CONS_D2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   422
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   423
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   424
(****** Reasoning about llist(A) ******)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   425
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   426
Addsimps [List_case_NIL, List_case_CONS];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   427
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   428
(*A special case of list_equality for functions over lazy lists*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   429
val [Mlist,gMlist,NILcase,CONScase] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   430
 "[| M: llist(A); g(NIL): llist(A);                             \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   431
\    f(NIL)=g(NIL);                                             \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   432
\    !!x l. [| x:A;  l: llist(A) |] ==>                         \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   433
\           (f(CONS x l),g(CONS x l)) :                         \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   434
\               LListD_Fun (diag A) ((%u.(f(u),g(u)))``llist(A) Un  \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   435
\                                   diag(llist(A)))             \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   436
\ |] ==> f(M) = g(M)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   437
by (rtac LList_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   438
by (rtac (Mlist RS imageI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   439
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   440
by (etac imageE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   441
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   442
by (etac llist.elim 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   443
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   444
by (stac NILcase 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   445
by (rtac (gMlist RS LListD_Fun_diag_I) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   446
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   447
by (REPEAT (ares_tac [CONScase] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   448
qed "LList_fun_equalityI";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   449
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   450
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   451
(*** The functional "Lmap" ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   452
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   453
goal LList.thy "Lmap f NIL = NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   454
by (rtac (Lmap_def RS def_LList_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   455
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   456
qed "Lmap_NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   457
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   458
goal LList.thy "Lmap f (CONS M N) = CONS (f M) (Lmap f N)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   459
by (rtac (Lmap_def RS def_LList_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   460
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   461
qed "Lmap_CONS";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   462
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   463
(*Another type-checking proof by coinduction*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   464
val [major,minor] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   465
    "[| M: llist(A);  !!x. x:A ==> f(x):B |] ==> Lmap f M: llist(B)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   466
by (rtac (major RS imageI RS llist_coinduct) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   467
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   468
by (etac llist.elim 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   469
by (ALLGOALS (asm_simp_tac (simpset() addsimps [Lmap_NIL,Lmap_CONS])));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   470
by (REPEAT (ares_tac [list_Fun_NIL_I, list_Fun_CONS_I, 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   471
                      minor, imageI, UnI1] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   472
qed "Lmap_type";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   473
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   474
(*This type checking rule synthesises a sufficiently large set for f*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   475
val [major] = goal LList.thy  "M: llist(A) ==> Lmap f M: llist(f``A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   476
by (rtac (major RS Lmap_type) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   477
by (etac imageI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   478
qed "Lmap_type2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   479
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   480
(** Two easy results about Lmap **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   481
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   482
val [prem] = goalw LList.thy [o_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   483
    "M: llist(A) ==> Lmap (f o g) M = Lmap f (Lmap g M)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   484
by (rtac (prem RS imageI RS LList_equalityI) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   485
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   486
by (etac llist.elim 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   487
by (ALLGOALS (asm_simp_tac (simpset() addsimps [Lmap_NIL,Lmap_CONS])));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   488
by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI, UnI1,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   489
                      rangeI RS LListD_Fun_CONS_I] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   490
qed "Lmap_compose";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   491
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   492
val [prem] = goal LList.thy "M: llist(A) ==> Lmap (%x. x) M = M";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   493
by (rtac (prem RS imageI RS LList_equalityI) 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   494
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   495
by (etac llist.elim 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   496
by (ALLGOALS (asm_simp_tac (simpset() addsimps [Lmap_NIL,Lmap_CONS])));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   497
by (REPEAT (ares_tac [LListD_Fun_NIL_I, imageI RS UnI1,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   498
                      rangeI RS LListD_Fun_CONS_I] 1));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   499
qed "Lmap_ident";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   500
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   501
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   502
(*** Lappend -- its two arguments cause some complications! ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   503
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   504
goalw LList.thy [Lappend_def] "Lappend NIL NIL = NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   505
by (rtac (LList_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   506
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   507
qed "Lappend_NIL_NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   508
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   509
goalw LList.thy [Lappend_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   510
    "Lappend NIL (CONS N N') = CONS N (Lappend NIL N')";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   511
by (rtac (LList_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   512
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   513
qed "Lappend_NIL_CONS";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   514
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   515
goalw LList.thy [Lappend_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   516
    "Lappend (CONS M M') N = CONS M (Lappend M' N)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   517
by (rtac (LList_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   518
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   519
qed "Lappend_CONS";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   520
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   521
Addsimps [llist.NIL_I, Lappend_NIL_NIL, Lappend_NIL_CONS,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   522
          Lappend_CONS, LListD_Fun_CONS_I, range_eqI, image_eqI];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   523
Delsimps [Pair_eq];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   524
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   525
goal LList.thy "!!M. M: llist(A) ==> Lappend NIL M = M";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   526
by (etac LList_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   527
by (ALLGOALS Asm_simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   528
qed "Lappend_NIL";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   529
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   530
goal LList.thy "!!M. M: llist(A) ==> Lappend M NIL = M";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   531
by (etac LList_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   532
by (ALLGOALS Asm_simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   533
qed "Lappend_NIL2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   534
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   535
(** Alternative type-checking proofs for Lappend **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   536
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   537
(*weak co-induction: bisimulation and case analysis on both variables*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   538
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   539
    "!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend M N: llist(A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   540
by (res_inst_tac
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   541
    [("X", "UN u:llist(A). UN v: llist(A). {Lappend u v}")] llist_coinduct 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   542
by (Fast_tac 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   543
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   544
by (eres_inst_tac [("a", "u")] llist.elim 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   545
by (eres_inst_tac [("a", "v")] llist.elim 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   546
by (ALLGOALS
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   547
    (Asm_simp_tac THEN'
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   548
     fast_tac (claset() addSIs [llist.NIL_I, list_Fun_NIL_I, list_Fun_CONS_I])));
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   549
qed "Lappend_type";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   550
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   551
(*strong co-induction: bisimulation and case analysis on one variable*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   552
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   553
    "!!M N. [| M: llist(A); N: llist(A) |] ==> Lappend M N: llist(A)";
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   554
by (res_inst_tac [("X", "(%u. Lappend u N)``llist(A)")] llist_coinduct 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   555
by (etac imageI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   556
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   557
by (etac imageE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   558
by (eres_inst_tac [("a", "u")] llist.elim 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   559
by (asm_simp_tac (simpset() addsimps [Lappend_NIL, list_Fun_llist_I]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   560
by (Asm_simp_tac 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   561
by (fast_tac (claset() addSIs [list_Fun_CONS_I]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   562
qed "Lappend_type";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   563
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   564
(**** Lazy lists as the type 'a llist -- strongly typed versions of above ****)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   565
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   566
(** llist_case: case analysis for 'a llist **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   567
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   568
Addsimps ([Abs_llist_inverse, Rep_llist_inverse,
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   569
           Rep_llist, rangeI, inj_Leaf, inv_f_f] @ llist.intrs);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   570
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   571
goalw LList.thy [llist_case_def,LNil_def]  "llist_case c d LNil = c";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   572
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   573
qed "llist_case_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   574
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   575
goalw LList.thy [llist_case_def,LCons_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   576
    "llist_case c d (LCons M N) = d M N";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   577
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   578
qed "llist_case_LCons";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   579
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   580
(*Elimination is case analysis, not induction.*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   581
val [prem1,prem2] = goalw LList.thy [NIL_def,CONS_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   582
    "[| l=LNil ==> P;  !!x l'. l=LCons x l' ==> P \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   583
\    |] ==> P";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   584
by (rtac (Rep_llist RS llist.elim) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   585
by (rtac (inj_Rep_llist RS injD RS prem1) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   586
by (stac Rep_llist_LNil 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   587
by (assume_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   588
by (etac rangeE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   589
by (rtac (inj_Rep_llist RS injD RS prem2) 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   590
by (asm_simp_tac (simpset() delsimps [CONS_CONS_eq] addsimps [Rep_llist_LCons]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   591
by (etac (Abs_llist_inverse RS ssubst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   592
by (rtac refl 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   593
qed "llistE";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   594
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   595
(** llist_corec: corecursion for 'a llist **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   596
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   597
goalw LList.thy [llist_corec_def,LNil_def,LCons_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   598
    "llist_corec a f = sum_case (%u. LNil) \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   599
\                           (split(%z w. LCons z (llist_corec w f))) (f a)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   600
by (stac LList_corec 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   601
by (res_inst_tac [("s","f(a)")] sumE 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   602
by (asm_simp_tac (simpset() addsimps [LList_corec_type2]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   603
by (res_inst_tac [("p","y")] PairE 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   604
by (asm_simp_tac (simpset() addsimps [LList_corec_type2]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   605
(*FIXME: correct case splits usd to be found automatically:
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   606
by (ASM_SIMP_TAC(simpset() addsimps [LList_corec_type2]) 1);*)
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   607
qed "llist_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   608
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   609
(*definitional version of same*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   610
val [rew] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   611
    "[| !!x. h(x) == llist_corec x f |] ==>     \
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   612
\    h(a) = sum_case (%u. LNil) (split(%z w. LCons z (h w))) (f a)";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   613
by (rewtac rew);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   614
by (rtac llist_corec 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   615
qed "def_llist_corec";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   616
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   617
(**** Proofs about type 'a llist functions ****)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   618
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   619
(*** Deriving llist_equalityI -- llist equality is a bisimulation ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   620
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   621
goalw LList.thy [LListD_Fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   622
    "!!r A. r <= (llist A) Times (llist A) ==> \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   623
\           LListD_Fun (diag A) r <= (llist A) Times (llist A)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   624
by (stac llist_unfold 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   625
by (simp_tac (simpset() addsimps [NIL_def, CONS_def]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   626
by (Fast_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   627
qed "LListD_Fun_subset_Sigma_llist";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   628
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   629
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   630
    "prod_fun Rep_llist Rep_llist `` r <= \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   631
\    (llist(range Leaf)) Times (llist(range Leaf))";
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   632
by (fast_tac (claset() addIs [Rep_llist]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   633
qed "subset_Sigma_llist";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   634
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   635
val [prem] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   636
    "r <= (llist(range Leaf)) Times (llist(range Leaf)) ==> \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   637
\    prod_fun (Rep_llist o Abs_llist) (Rep_llist o Abs_llist) `` r <= r";
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   638
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   639
by (rtac (prem RS subsetD RS SigmaE2) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   640
by (assume_tac 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   641
by (asm_simp_tac (simpset() addsimps [o_def,prod_fun,Abs_llist_inverse]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   642
qed "prod_fun_lemma";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   643
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   644
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   645
    "prod_fun Rep_llist  Rep_llist `` range(%x. (x, x)) = \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   646
\    diag(llist(range Leaf))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   647
by (rtac equalityI 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   648
by (fast_tac (claset() addIs [Rep_llist]) 1);
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   649
by (fast_tac (claset() addSEs [Abs_llist_inverse RS subst]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   650
qed "prod_fun_range_eq_diag";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   651
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   652
(*Surprisingly hard to prove.  Used with lfilter*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   653
goalw thy [llistD_Fun_def, prod_fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   654
    "!!A B. A<=B ==> llistD_Fun A <= llistD_Fun B";
4477
b3e5857d8d99 New Auto_tac (by Oheimb), and new syntax (without parens), and expandshort
paulson
parents: 4160
diff changeset
   655
by Auto_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   656
by (rtac image_eqI 1);
4089
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   657
by (fast_tac (claset() addss (simpset())) 1);
96fba19bcbe2 isatool fixclasimp;
wenzelm
parents: 3919
diff changeset
   658
by (blast_tac (claset() addIs [impOfSubs LListD_Fun_mono]) 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   659
qed "llistD_Fun_mono";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   660
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   661
(** To show two llists are equal, exhibit a bisimulation! 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   662
      [also admits true equality] **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   663
val [prem1,prem2] = goalw LList.thy [llistD_Fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   664
    "[| (l1,l2) : r;  r <= llistD_Fun(r Un range(%x.(x,x))) |] ==> l1=l2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   665
by (rtac (inj_Rep_llist RS injD) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   666
by (res_inst_tac [("r", "prod_fun Rep_llist Rep_llist ``r"),
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   667
                  ("A", "range(Leaf)")] 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   668
        LList_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   669
by (rtac (prem1 RS prod_fun_imageI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   670
by (rtac (prem2 RS image_mono RS subset_trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   671
by (rtac (image_compose RS subst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   672
by (rtac (prod_fun_compose RS subst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   673
by (stac image_Un 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   674
by (stac prod_fun_range_eq_diag 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   675
by (rtac (LListD_Fun_subset_Sigma_llist RS prod_fun_lemma) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   676
by (rtac (subset_Sigma_llist RS Un_least) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   677
by (rtac diag_subset_Sigma 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   678
qed "llist_equalityI";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   679
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   680
(** Rules to prove the 2nd premise of llist_equalityI **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   681
goalw LList.thy [llistD_Fun_def,LNil_def] "(LNil,LNil) : llistD_Fun(r)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   682
by (rtac (LListD_Fun_NIL_I RS prod_fun_imageI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   683
qed "llistD_Fun_LNil_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   684
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   685
val [prem] = goalw LList.thy [llistD_Fun_def,LCons_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   686
    "(l1,l2):r ==> (LCons x l1, LCons x l2) : llistD_Fun(r)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   687
by (rtac (rangeI RS LListD_Fun_CONS_I RS prod_fun_imageI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   688
by (rtac (prem RS prod_fun_imageI) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   689
qed "llistD_Fun_LCons_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   690
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   691
(*Utilise the "strong" part, i.e. gfp(f)*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   692
goalw LList.thy [llistD_Fun_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   693
     "!!l. (l,l) : llistD_Fun(r Un range(%x.(x,x)))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   694
by (rtac (Rep_llist_inverse RS subst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   695
by (rtac prod_fun_imageI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   696
by (stac image_Un 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   697
by (stac prod_fun_range_eq_diag 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   698
by (rtac (Rep_llist RS LListD_Fun_diag_I) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   699
qed "llistD_Fun_range_I";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   700
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   701
(*A special case of list_equality for functions over lazy lists*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   702
val [prem1,prem2] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   703
    "[| f(LNil)=g(LNil);                                                \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   704
\       !!x l. (f(LCons x l),g(LCons x l)) :                            \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   705
\              llistD_Fun(range(%u. (f(u),g(u))) Un range(%v. (v,v)))   \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   706
\    |] ==> f(l) = (g(l :: 'a llist) :: 'b llist)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   707
by (res_inst_tac [("r", "range(%u. (f(u),g(u)))")] llist_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   708
by (rtac rangeI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   709
by (rtac subsetI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   710
by (etac rangeE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   711
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   712
by (res_inst_tac [("l", "u")] llistE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   713
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   714
by (stac prem1 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   715
by (rtac llistD_Fun_range_I 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   716
by (etac ssubst 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   717
by (rtac prem2 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   718
qed "llist_fun_equalityI";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   719
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   720
(*simpset for llist bisimulations*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   721
Addsimps [llist_case_LNil, llist_case_LCons, 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   722
          llistD_Fun_LNil_I, llistD_Fun_LCons_I];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   723
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   724
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   725
(*** The functional "lmap" ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   726
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   727
goal LList.thy "lmap f LNil = LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   728
by (rtac (lmap_def RS def_llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   729
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   730
qed "lmap_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   731
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   732
goal LList.thy "lmap f (LCons M N) = LCons (f M) (lmap f N)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   733
by (rtac (lmap_def RS def_llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   734
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   735
qed "lmap_LCons";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   736
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   737
Addsimps [lmap_LNil, lmap_LCons];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   738
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   739
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   740
(** Two easy results about lmap **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   741
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   742
goal LList.thy "lmap (f o g) l = lmap f (lmap g l)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   743
by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   744
by (ALLGOALS Simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   745
qed "lmap_compose";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   746
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   747
goal LList.thy "lmap (%x. x) l = l";
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   748
by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   749
by (ALLGOALS Simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   750
qed "lmap_ident";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   751
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   752
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   753
(*** iterates -- llist_fun_equalityI cannot be used! ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   754
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   755
goal LList.thy "iterates f x = LCons x (iterates f (f x))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   756
by (rtac (iterates_def RS def_llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   757
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   758
qed "iterates";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   759
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   760
goal LList.thy "lmap f (iterates f x) = iterates f (f x)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   761
by (res_inst_tac [("r", "range(%u.(lmap f (iterates f u),iterates f (f u)))")] 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   762
    llist_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   763
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   764
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   765
by (res_inst_tac [("x1", "f(u)")] (iterates RS ssubst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   766
by (res_inst_tac [("x1", "u")] (iterates RS ssubst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   767
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   768
qed "lmap_iterates";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   769
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   770
goal LList.thy "iterates f x = LCons x (lmap f (iterates f x))";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   771
by (stac lmap_iterates 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   772
by (rtac iterates 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   773
qed "iterates_lmap";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   774
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   775
(*** A rather complex proof about iterates -- cf Andy Pitts ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   776
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   777
(** Two lemmas about natrec n x (%m.g), which is essentially (g^n)(x) **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   778
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   779
goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   780
    "nat_rec (LCons b l) (%m. lmap(f)) n =      \
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   781
\    LCons (nat_rec b (%m. f) n) (nat_rec l (%m. lmap(f)) n)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   782
by (nat_ind_tac "n" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   783
by (ALLGOALS Asm_simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   784
qed "fun_power_lmap";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   785
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   786
goal Nat.thy "nat_rec (g x) (%m. g) n = nat_rec x (%m. g) (Suc n)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   787
by (nat_ind_tac "n" 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   788
by (ALLGOALS Asm_simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   789
qed "fun_power_Suc";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   790
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   791
val Pair_cong = read_instantiate_sg (sign_of Prod.thy)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   792
 [("f","Pair")] (standard(refl RS cong RS cong));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   793
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   794
(*The bisimulation consists of {(lmap(f)^n (h(u)), lmap(f)^n (iterates(f,u)))}
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   795
  for all u and all n::nat.*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   796
val [prem] = goal LList.thy
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   797
    "(!!x. h(x) = LCons x (lmap f (h x))) ==> h = iterates(f)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   798
by (rtac ext 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   799
by (res_inst_tac [("r", 
3842
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   800
   "UN u. range(%n. (nat_rec (h u) (%m y. lmap f y) n, \
b55686a7b22c fixed dots;
wenzelm
parents: 3427
diff changeset
   801
\                    nat_rec (iterates f u) (%m y. lmap f y) n))")] 
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   802
    llist_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   803
by (REPEAT (resolve_tac [UN1_I, range_eqI, Pair_cong, nat_rec_0 RS sym] 1));
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   804
by (Clarify_tac 1);
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   805
by (stac iterates 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   806
by (stac prem 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   807
by (stac fun_power_lmap 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   808
by (stac fun_power_lmap 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   809
by (rtac llistD_Fun_LCons_I 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   810
by (rtac (lmap_iterates RS subst) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   811
by (stac fun_power_Suc 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   812
by (stac fun_power_Suc 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   813
by (rtac (UN1_I RS UnI1) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   814
by (rtac rangeI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   815
qed "iterates_equality";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   816
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   817
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   818
(*** lappend -- its two arguments cause some complications! ***)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   819
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   820
goalw LList.thy [lappend_def] "lappend LNil LNil = LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   821
by (rtac (llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   822
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   823
qed "lappend_LNil_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   824
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   825
goalw LList.thy [lappend_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   826
    "lappend LNil (LCons l l') = LCons l (lappend LNil l')";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   827
by (rtac (llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   828
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   829
qed "lappend_LNil_LCons";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   830
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   831
goalw LList.thy [lappend_def]
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   832
    "lappend (LCons l l') N = LCons l (lappend l' N)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   833
by (rtac (llist_corec RS trans) 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   834
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   835
qed "lappend_LCons";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   836
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   837
Addsimps [lappend_LNil_LNil, lappend_LNil_LCons, lappend_LCons];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   838
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   839
goal LList.thy "lappend LNil l = l";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   840
by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   841
by (ALLGOALS Simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   842
qed "lappend_LNil";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   843
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   844
goal LList.thy "lappend l LNil = l";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   845
by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   846
by (ALLGOALS Simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   847
qed "lappend_LNil2";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   848
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   849
Addsimps [lappend_LNil, lappend_LNil2];
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   850
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   851
(*The infinite first argument blocks the second*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   852
goal LList.thy "lappend (iterates f x) N = iterates f x";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   853
by (res_inst_tac [("r", "range(%u.(lappend (iterates f u) N,iterates f u))")] 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   854
    llist_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   855
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   856
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   857
by (stac iterates 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   858
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   859
qed "lappend_iterates";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   860
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   861
(** Two proofs that lmap distributes over lappend **)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   862
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   863
(*Long proof requiring case analysis on both both arguments*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   864
goal LList.thy "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   865
by (res_inst_tac 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   866
    [("r",  
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   867
      "UN n. range(%l.(lmap f (lappend l n),lappend (lmap f l) (lmap f n)))")] 
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   868
    llist_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   869
by (rtac UN1_I 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   870
by (rtac rangeI 1);
4160
59826ea67cba Adapted to removal of UN1_I, etc
paulson
parents: 4089
diff changeset
   871
by Safe_tac;
3120
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   872
by (res_inst_tac [("l", "l")] llistE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   873
by (res_inst_tac [("l", "n")] llistE 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   874
by (ALLGOALS Asm_simp_tac);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   875
by (REPEAT_SOME (ares_tac [llistD_Fun_LCons_I, UN1_I RS UnI1, rangeI]));
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   876
qed "lmap_lappend_distrib";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   877
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   878
(*Shorter proof of theorem above using llist_equalityI as strong coinduction*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   879
goal LList.thy "lmap f (lappend l n) = lappend (lmap f l) (lmap f n)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   880
by (res_inst_tac [("l","l")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   881
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   882
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   883
qed "lmap_lappend_distrib";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   884
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   885
(*Without strong coinduction, three case analyses might be needed*)
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   886
goal LList.thy "lappend (lappend l1 l2) l3 = lappend l1 (lappend l2 l3)";
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   887
by (res_inst_tac [("l","l1")] llist_fun_equalityI 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   888
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   889
by (Simp_tac 1);
c58423c20740 New directory to contain examples of (co)inductive definitions
paulson
parents:
diff changeset
   890
qed "lappend_assoc";