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