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