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