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