src/HOLCF/domain/theorems.ML
author oheimb
Fri May 31 20:25:59 1996 +0200 (1996-05-31)
changeset 1781 cc5f55a0fbd7
parent 1674 33aff4d854e4
child 1829 5a3687398716
permissions -rw-r--r--
adapted use of monofun_cfun_arg
oheimb@1638
     1
 (* theorems.ML
regensbu@1274
     2
   Author : David von Oheimb
regensbu@1274
     3
   Created: 06-Jun-95
regensbu@1274
     4
   Updated: 08-Jun-95 first proof from cterms
regensbu@1274
     5
   Updated: 26-Jun-95 proofs for exhaustion thms
regensbu@1274
     6
   Updated: 27-Jun-95 proofs for discriminators, constructors and selectors
regensbu@1274
     7
   Updated: 06-Jul-95 proofs for distinctness, invertibility and injectivity
regensbu@1274
     8
   Updated: 17-Jul-95 proofs for induction rules
regensbu@1274
     9
   Updated: 19-Jul-95 proof for co-induction rule
regensbu@1274
    10
   Updated: 28-Aug-95 definedness theorems for selectors (completion)
regensbu@1274
    11
   Updated: 05-Sep-95 simultaneous domain equations (main part)
regensbu@1274
    12
   Updated: 11-Sep-95 simultaneous domain equations (coding finished)
regensbu@1274
    13
   Updated: 13-Sep-95 simultaneous domain equations (debugging)
oheimb@1637
    14
   Updated: 26-Oct-95 debugging and enhancement of proofs for take_apps, ind
oheimb@1637
    15
   Updated: 16-Feb-96 bug concerning  domain Triv = triv  fixed
oheimb@1637
    16
   Updated: 01-Mar-96 when functional strictified, copy_def based on when_def
oheimb@1637
    17
   Copyright 1995, 1996 TU Muenchen
regensbu@1274
    18
*)
regensbu@1274
    19
regensbu@1274
    20
structure Domain_Theorems = struct
regensbu@1274
    21
regensbu@1274
    22
local
regensbu@1274
    23
regensbu@1274
    24
open Domain_Library;
regensbu@1274
    25
infixr 0 ===>;infixr 0 ==>;infix 0 == ; 
regensbu@1274
    26
infix 1 ===; infix 1 ~= ; infix 1 <<; infix 1 ~<<;
regensbu@1274
    27
infix 9 `   ; infix 9 `% ; infix 9 `%%; infixr 9 oo;
regensbu@1274
    28
oheimb@1637
    29
(* ----- general proof facilities ------------------------------------------- *)
regensbu@1274
    30
oheimb@1637
    31
fun inferT sg pre_tm = #2 (Sign.infer_types sg (K None) (K None) [] true 
oheimb@1637
    32
			   ([pre_tm],propT));
regensbu@1274
    33
regensbu@1274
    34
fun pg'' thy defs t = let val sg = sign_of thy;
oheimb@1637
    35
		          val ct = Thm.cterm_of sg (inferT sg t);
oheimb@1637
    36
		      in prove_goalw_cterm defs ct end;
regensbu@1274
    37
fun pg'  thy defs t tacsf=pg'' thy defs t (fn []   => tacsf 
oheimb@1637
    38
				| prems=> (cut_facts_tac prems 1)::tacsf);
regensbu@1274
    39
regensbu@1274
    40
fun REPEAT_DETERM_UNTIL p tac = 
regensbu@1274
    41
let fun drep st = if p st then Sequence.single st
oheimb@1638
    42
			  else (case Sequence.pull(tac st) of
oheimb@1637
    43
		                  None        => Sequence.null
oheimb@1637
    44
				| Some(st',_) => drep st')
oheimb@1638
    45
in drep end;
regensbu@1274
    46
val UNTIL_SOLVED = REPEAT_DETERM_UNTIL (has_fewer_prems 1);
regensbu@1274
    47
oheimb@1674
    48
local val trueI2 = prove_goal HOL.thy"f~=x ==> True"(fn _ => [rtac TrueI 1]) in
regensbu@1274
    49
val kill_neq_tac = dtac trueI2 end;
oheimb@1674
    50
fun case_UU_tac rews i v =	case_tac (v^"=UU") i THEN
oheimb@1637
    51
				asm_simp_tac (HOLCF_ss addsimps rews) i;
regensbu@1274
    52
regensbu@1274
    53
val chain_tac = REPEAT_DETERM o resolve_tac 
oheimb@1637
    54
		[is_chain_iterate, ch2ch_fappR, ch2ch_fappL];
oheimb@1637
    55
oheimb@1637
    56
(* ----- general proofs ----------------------------------------------------- *)
regensbu@1274
    57
oheimb@1637
    58
val quant_ss = HOL_ss addsimps (map (fn s => prove_goal HOL.thy s (fn _ =>[
oheimb@1644
    59
		fast_tac HOL_cs 1]))["(!x. P x & Q)=((!x. P x) & Q)",
oheimb@1644
    60
			    	     "(!x. P & Q x) = (P & (!x. Q x))"]);
oheimb@1637
    61
oheimb@1644
    62
val all2E = prove_goal HOL.thy "[| !x y . P x y; P x y ==> R |] ==> R"
oheimb@1644
    63
 (fn prems =>[
oheimb@1637
    64
				resolve_tac prems 1,
oheimb@1637
    65
				cut_facts_tac prems 1,
oheimb@1637
    66
				fast_tac HOL_cs 1]);
regensbu@1274
    67
regensbu@1274
    68
val swap3 = prove_goal HOL.thy "[| Q ==> P; ~P |] ==> ~Q" (fn prems => [
regensbu@1274
    69
                                cut_facts_tac prems 1,
regensbu@1274
    70
                                etac swap 1,
regensbu@1274
    71
                                dtac notnotD 1,
oheimb@1637
    72
				etac (hd prems) 1]);
regensbu@1274
    73
oheimb@1644
    74
val dist_eqI = prove_goal Porder.thy "~ x << y ==> x ~= y" (fn prems => [
oheimb@1637
    75
                                rtac swap3 1,
oheimb@1637
    76
				etac (antisym_less_inverse RS conjunct1) 1,
oheimb@1637
    77
				resolve_tac prems 1]);
oheimb@1644
    78
val cfst_strict  = prove_goal Cprod3.thy "cfst`UU = UU" (fn _ => [
oheimb@1637
    79
			(simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
oheimb@1644
    80
val csnd_strict  = prove_goal Cprod3.thy "csnd`UU = UU" (fn _ => [
oheimb@1637
    81
			(simp_tac (HOLCF_ss addsimps [inst_cprod_pcpo2]) 1)]);
regensbu@1274
    82
regensbu@1274
    83
in
regensbu@1274
    84
regensbu@1274
    85
regensbu@1274
    86
fun theorems thy (((dname,_),cons) : eq, eqs :eq list) =
regensbu@1274
    87
let
regensbu@1274
    88
regensbu@1274
    89
val dummy = writeln ("Proving isomorphism properties of domain "^dname^"...");
regensbu@1274
    90
val pg = pg' thy;
oheimb@1637
    91
(*
oheimb@1637
    92
infixr 0 y;
oheimb@1637
    93
val b = 0;
oheimb@1637
    94
fun _ y t = by t;
oheimb@1637
    95
fun  g  defs t = let val sg = sign_of thy;
oheimb@1637
    96
		     val ct = Thm.cterm_of sg (inferT sg t);
oheimb@1637
    97
		 in goalw_cterm defs ct end;
oheimb@1637
    98
*)
regensbu@1274
    99
oheimb@1637
   100
oheimb@1637
   101
(* ----- getting the axioms and definitions --------------------------------- *)
regensbu@1274
   102
regensbu@1274
   103
local val ga = get_axiom thy in
regensbu@1274
   104
val ax_abs_iso    = ga (dname^"_abs_iso"   );
regensbu@1274
   105
val ax_rep_iso    = ga (dname^"_rep_iso"   );
regensbu@1274
   106
val ax_when_def   = ga (dname^"_when_def"  );
regensbu@1274
   107
val axs_con_def   = map (fn (con,_) => ga (extern_name con ^"_def")) cons;
regensbu@1274
   108
val axs_dis_def   = map (fn (con,_) => ga (   dis_name con ^"_def")) cons;
regensbu@1274
   109
val axs_sel_def   = flat(map (fn (_,args) => 
oheimb@1637
   110
		    map (fn     arg => ga (sel_of arg      ^"_def")) args)cons);
regensbu@1274
   111
val ax_copy_def   = ga (dname^"_copy_def"  );
regensbu@1274
   112
end; (* local *)
regensbu@1274
   113
oheimb@1637
   114
(* ----- theorems concerning the isomorphism -------------------------------- *)
regensbu@1274
   115
regensbu@1274
   116
val dc_abs  = %%(dname^"_abs");
regensbu@1274
   117
val dc_rep  = %%(dname^"_rep");
regensbu@1274
   118
val dc_copy = %%(dname^"_copy");
regensbu@1274
   119
val x_name = "x";
regensbu@1274
   120
regensbu@1274
   121
val (rep_strict, abs_strict) = let 
oheimb@1637
   122
	 val r = ax_rep_iso RS (ax_abs_iso RS (allI  RSN(2,allI RS iso_strict)))
oheimb@1637
   123
	       in (r RS conjunct1, r RS conjunct2) end;
regensbu@1274
   124
val abs_defin' = pg [] ((dc_abs`%x_name === UU) ==> (%x_name === UU)) [
oheimb@1637
   125
			   res_inst_tac [("t",x_name)] (ax_abs_iso RS subst) 1,
oheimb@1637
   126
				etac ssubst 1, rtac rep_strict 1];
regensbu@1274
   127
val rep_defin' = pg [] ((dc_rep`%x_name === UU) ==> (%x_name === UU)) [
oheimb@1637
   128
			   res_inst_tac [("t",x_name)] (ax_rep_iso RS subst) 1,
oheimb@1637
   129
				etac ssubst 1, rtac abs_strict 1];
regensbu@1274
   130
val iso_rews = [ax_abs_iso,ax_rep_iso,abs_strict,rep_strict];
regensbu@1274
   131
regensbu@1274
   132
local 
regensbu@1274
   133
val iso_swap = pg [] (dc_rep`%"x" === %"y" ==> %"x" === dc_abs`%"y") [
oheimb@1637
   134
			    dres_inst_tac [("f",dname^"_abs")] cfun_arg_cong 1,
oheimb@1637
   135
			    etac (ax_rep_iso RS subst) 1];
regensbu@1274
   136
fun exh foldr1 cn quant foldr2 var = let
regensbu@1274
   137
  fun one_con (con,args) = let val vns = map vname args in
regensbu@1274
   138
    foldr quant (vns, foldr2 ((%x_name === con_app2 con (var vns) vns)::
oheimb@1637
   139
			      map (defined o (var vns)) (nonlazy args))) end
regensbu@1274
   140
  in foldr1 ((cn(%x_name===UU))::map one_con cons) end;
regensbu@1274
   141
in
regensbu@1274
   142
val cases = let 
oheimb@1637
   143
	    fun common_tac thm = rtac thm 1 THEN contr_tac 1;
oheimb@1637
   144
	    fun unit_tac true = common_tac liftE1
oheimb@1637
   145
	    |   unit_tac _    = all_tac;
oheimb@1637
   146
	    fun prod_tac []          = common_tac oneE
oheimb@1637
   147
	    |   prod_tac [arg]       = unit_tac (is_lazy arg)
oheimb@1637
   148
	    |   prod_tac (arg::args) = 
oheimb@1637
   149
				common_tac sprodE THEN
oheimb@1637
   150
				kill_neq_tac 1 THEN
oheimb@1637
   151
				unit_tac (is_lazy arg) THEN
oheimb@1637
   152
				prod_tac args;
oheimb@1637
   153
	    fun sum_rest_tac p = SELECT_GOAL(EVERY[
oheimb@1637
   154
				rtac p 1,
oheimb@1637
   155
				rewrite_goals_tac axs_con_def,
oheimb@1637
   156
				dtac iso_swap 1,
oheimb@1637
   157
				simp_tac HOLCF_ss 1,
oheimb@1637
   158
				UNTIL_SOLVED(fast_tac HOL_cs 1)]) 1;
oheimb@1637
   159
	    fun sum_tac [(_,args)]       [p]        = 
oheimb@1637
   160
				prod_tac args THEN sum_rest_tac p
oheimb@1637
   161
	    |   sum_tac ((_,args)::cons') (p::prems) = DETERM(
oheimb@1637
   162
				common_tac ssumE THEN
oheimb@1637
   163
				kill_neq_tac 1 THEN kill_neq_tac 2 THEN
oheimb@1637
   164
				prod_tac args THEN sum_rest_tac p) THEN
oheimb@1637
   165
				sum_tac cons' prems
oheimb@1637
   166
	    |   sum_tac _ _ = Imposs "theorems:sum_tac";
oheimb@1637
   167
	  in pg'' thy [] (exh (fn l => foldr (op ===>) (l,mk_trp(%"P")))
oheimb@1637
   168
			      (fn T => T ==> %"P") mk_All
oheimb@1637
   169
			      (fn l => foldr (op ===>) (map mk_trp l,
oheimb@1637
   170
							    mk_trp(%"P")))
oheimb@1637
   171
			      bound_arg)
oheimb@1637
   172
			     (fn prems => [
oheimb@1637
   173
				cut_facts_tac [excluded_middle] 1,
oheimb@1637
   174
				etac disjE 1,
oheimb@1637
   175
				rtac (hd prems) 2,
oheimb@1637
   176
				etac rep_defin' 2,
oheimb@1637
   177
				if length cons = 1 andalso 
oheimb@1637
   178
				   length (snd(hd cons)) = 1 andalso 
oheimb@1637
   179
				   not(is_lazy(hd(snd(hd cons))))
oheimb@1637
   180
				then rtac (hd (tl prems)) 1 THEN atac 2 THEN
oheimb@1637
   181
				     rewrite_goals_tac axs_con_def THEN
oheimb@1637
   182
				     simp_tac (HOLCF_ss addsimps [ax_rep_iso]) 1
oheimb@1637
   183
				else sum_tac cons (tl prems)])end;
oheimb@1637
   184
val exhaust= pg[](mk_trp(exh (foldr' mk_disj) Id mk_ex (foldr' mk_conj) (K %)))[
oheimb@1637
   185
				rtac cases 1,
oheimb@1637
   186
				UNTIL_SOLVED(fast_tac HOL_cs 1)];
regensbu@1274
   187
end;
regensbu@1274
   188
regensbu@1274
   189
local 
oheimb@1637
   190
  val when_app  = foldl (op `) (%%(dname^"_when"), map % (when_funs cons));
oheimb@1637
   191
  val when_appl = pg [ax_when_def] (mk_trp(when_app`%x_name===when_body cons 
oheimb@1637
   192
		(fn (_,n)=> %(nth_elem(n-1,when_funs cons)))`(dc_rep`%x_name)))[
oheimb@1637
   193
				simp_tac HOLCF_ss 1];
regensbu@1274
   194
in
oheimb@1637
   195
val when_strict = pg [] (mk_trp(strict when_app)) [
oheimb@1637
   196
			simp_tac(HOLCF_ss addsimps [when_appl,rep_strict]) 1];
oheimb@1637
   197
val when_apps = let fun one_when n (con,args) = pg axs_con_def (lift_defined % 
oheimb@1637
   198
   (nonlazy args, mk_trp(when_app`(con_app con args) ===
oheimb@1637
   199
	 mk_cfapp(%(nth_elem(n,when_funs cons)),map %# args))))[
oheimb@1637
   200
		asm_simp_tac (HOLCF_ss addsimps [when_appl,ax_abs_iso]) 1];
oheimb@1637
   201
	in mapn one_when 0 cons end;
regensbu@1274
   202
end;
regensbu@1274
   203
val when_rews = when_strict::when_apps;
regensbu@1274
   204
oheimb@1637
   205
(* ----- theorems concerning the constructors, discriminators and selectors - *)
regensbu@1274
   206
oheimb@1637
   207
val dis_rews = let
oheimb@1637
   208
  val dis_stricts = map (fn (con,_) => pg axs_dis_def (mk_trp(
oheimb@1637
   209
		      	     strict(%%(dis_name con)))) [
oheimb@1637
   210
				simp_tac (HOLCF_ss addsimps when_rews) 1]) cons;
oheimb@1637
   211
  val dis_apps = let fun one_dis c (con,args)= pg axs_dis_def
oheimb@1637
   212
		   (lift_defined % (nonlazy args,
oheimb@1637
   213
			(mk_trp((%%(dis_name c))`(con_app con args) ===
oheimb@1637
   214
			      %%(if con=c then "TT" else "FF"))))) [
oheimb@1637
   215
				asm_simp_tac (HOLCF_ss addsimps when_rews) 1];
oheimb@1637
   216
	in flat(map (fn (c,_) => map (one_dis c) cons) cons) end;
oheimb@1637
   217
  val dis_defins = map (fn (con,args) => pg [] (defined(%x_name) ==> 
oheimb@1637
   218
		      defined(%%(dis_name con)`%x_name)) [
oheimb@1637
   219
				rtac cases 1,
oheimb@1637
   220
				contr_tac 1,
oheimb@1637
   221
				UNTIL_SOLVED (CHANGED(asm_simp_tac 
oheimb@1637
   222
				        (HOLCF_ss addsimps dis_apps) 1))]) cons;
oheimb@1637
   223
in dis_stricts @ dis_defins @ dis_apps end;
regensbu@1274
   224
regensbu@1274
   225
val con_stricts = flat(map (fn (con,args) => map (fn vn =>
oheimb@1637
   226
			pg (axs_con_def) 
oheimb@1637
   227
			   (mk_trp(con_app2 con (fn arg => if vname arg = vn 
oheimb@1637
   228
					then UU else %# arg) args === UU))[
oheimb@1637
   229
				asm_simp_tac (HOLCF_ss addsimps [abs_strict]) 1]
oheimb@1637
   230
			) (nonlazy args)) cons);
regensbu@1274
   231
val con_defins = map (fn (con,args) => pg []
oheimb@1637
   232
			(lift_defined % (nonlazy args,
oheimb@1637
   233
				mk_trp(defined(con_app con args)))) ([
oheimb@1637
   234
			  rtac swap3 1, 
oheimb@1637
   235
			  eres_inst_tac [("f",dis_name con)] cfun_arg_cong 1,
oheimb@1637
   236
			  asm_simp_tac (HOLCF_ss addsimps dis_rews) 1] )) cons;
regensbu@1274
   237
val con_rews = con_stricts @ con_defins;
regensbu@1274
   238
regensbu@1274
   239
val sel_stricts = let fun one_sel sel = pg axs_sel_def (mk_trp(strict(%%sel))) [
oheimb@1637
   240
				simp_tac (HOLCF_ss addsimps when_rews) 1];
oheimb@1637
   241
in flat(map (fn (_,args) =>map (fn arg => one_sel (sel_of arg)) args) cons) end;
regensbu@1274
   242
val sel_apps = let fun one_sel c n sel = map (fn (con,args) => 
oheimb@1637
   243
		let val nlas = nonlazy args;
oheimb@1637
   244
		    val vns  = map vname args;
oheimb@1637
   245
		in pg axs_sel_def (lift_defined %
oheimb@1637
   246
		   (filter (fn v => con=c andalso (v<>nth_elem(n,vns))) nlas,
oheimb@1637
   247
				mk_trp((%%sel)`(con_app con args) === 
oheimb@1637
   248
				(if con=c then %(nth_elem(n,vns)) else UU))))
oheimb@1637
   249
			    ( (if con=c then [] 
oheimb@1637
   250
		       else map(case_UU_tac(when_rews@con_stricts)1) nlas)
oheimb@1637
   251
		     @(if con=c andalso ((nth_elem(n,vns)) mem nlas)
oheimb@1637
   252
				 then[case_UU_tac (when_rews @ con_stricts) 1 
oheimb@1637
   253
						  (nth_elem(n,vns))] else [])
oheimb@1637
   254
		     @ [asm_simp_tac(HOLCF_ss addsimps when_rews)1])end) cons;
regensbu@1274
   255
in flat(map  (fn (c,args) => 
oheimb@1637
   256
     flat(mapn (fn n => fn arg => one_sel c n (sel_of arg)) 0 args)) cons) end;
oheimb@1637
   257
val sel_defins = if length cons=1 then map (fn arg => pg [](defined(%x_name)==> 
oheimb@1637
   258
			defined(%%(sel_of arg)`%x_name)) [
oheimb@1637
   259
				rtac cases 1,
oheimb@1637
   260
				contr_tac 1,
oheimb@1637
   261
				UNTIL_SOLVED (CHANGED(asm_simp_tac 
oheimb@1637
   262
				             (HOLCF_ss addsimps sel_apps) 1))]) 
oheimb@1637
   263
		 (filter_out is_lazy (snd(hd cons))) else [];
regensbu@1274
   264
val sel_rews = sel_stricts @ sel_defins @ sel_apps;
regensbu@1274
   265
regensbu@1274
   266
val distincts_le = let
regensbu@1274
   267
    fun dist (con1, args1) (con2, args2) = pg []
oheimb@1637
   268
	      (lift_defined % ((nonlazy args1),
oheimb@1637
   269
			(mk_trp (con_app con1 args1 ~<< con_app con2 args2))))([
oheimb@1637
   270
			rtac swap3 1,
oheimb@1781
   271
			eres_inst_tac[("fo",dis_name con1)] monofun_cfun_arg 1]
oheimb@1637
   272
		      @map(case_UU_tac (con_stricts @ dis_rews)1)(nonlazy args2)
oheimb@1637
   273
		      @[asm_simp_tac (HOLCF_ss addsimps dis_rews) 1]);
regensbu@1274
   274
    fun distinct (con1,args1) (con2,args2) =
oheimb@1637
   275
	let val arg1 = (con1, args1);
oheimb@1637
   276
	    val arg2 = (con2, (map (fn (arg,vn) => upd_vname (K vn) arg)
oheimb@1637
   277
			(args2~~variantlist(map vname args2,map vname args1))));
oheimb@1637
   278
	in [dist arg1 arg2, dist arg2 arg1] end;
regensbu@1274
   279
    fun distincts []      = []
regensbu@1274
   280
    |   distincts (c::cs) = (map (distinct c) cs) :: distincts cs;
regensbu@1274
   281
in distincts cons end;
regensbu@1274
   282
val dists_le = flat (flat distincts_le);
regensbu@1274
   283
val dists_eq = let
regensbu@1274
   284
    fun distinct (_,args1) ((_,args2),leqs) = let
oheimb@1637
   285
	val (le1,le2) = (hd leqs, hd(tl leqs));
oheimb@1637
   286
	val (eq1,eq2) = (le1 RS dist_eqI, le2 RS dist_eqI) in
oheimb@1637
   287
	if nonlazy args1 = [] then [eq1, eq1 RS not_sym] else
oheimb@1637
   288
	if nonlazy args2 = [] then [eq2, eq2 RS not_sym] else
oheimb@1637
   289
					[eq1, eq2] end;
regensbu@1274
   290
    fun distincts []      = []
regensbu@1274
   291
    |   distincts ((c,leqs)::cs) = flat(map (distinct c) ((map fst cs)~~leqs)) @
oheimb@1637
   292
				   distincts cs;
regensbu@1274
   293
    in distincts (cons~~distincts_le) end;
regensbu@1274
   294
regensbu@1274
   295
local 
regensbu@1274
   296
  fun pgterm rel con args = let
oheimb@1637
   297
		fun append s = upd_vname(fn v => v^s);
oheimb@1637
   298
		val (largs,rargs) = (args, map (append "'") args);
oheimb@1637
   299
		in pg [] (mk_trp (rel(con_app con largs,con_app con rargs)) ===>
oheimb@1637
   300
		      lift_defined % ((nonlazy largs),lift_defined % ((nonlazy rargs),
oheimb@1637
   301
			    mk_trp (foldr' mk_conj 
oheimb@1637
   302
				(map rel (map %# largs ~~ map %# rargs)))))) end;
regensbu@1274
   303
  val cons' = filter (fn (_,args) => args<>[]) cons;
regensbu@1274
   304
in
regensbu@1274
   305
val inverts = map (fn (con,args) => 
oheimb@1637
   306
		pgterm (op <<) con args (flat(map (fn arg => [
oheimb@1637
   307
				TRY(rtac conjI 1),
oheimb@1781
   308
				dres_inst_tac [("fo",sel_of arg)] monofun_cfun_arg 1,
oheimb@1637
   309
				asm_full_simp_tac (HOLCF_ss addsimps sel_apps) 1]
oheimb@1637
   310
			     			      ) args))) cons';
regensbu@1274
   311
val injects = map (fn ((con,args),inv_thm) => 
oheimb@1637
   312
			   pgterm (op ===) con args [
oheimb@1637
   313
				etac (antisym_less_inverse RS conjE) 1,
oheimb@1637
   314
				dtac inv_thm 1, REPEAT(atac 1),
oheimb@1637
   315
				dtac inv_thm 1, REPEAT(atac 1),
oheimb@1637
   316
				TRY(safe_tac HOL_cs),
oheimb@1637
   317
				REPEAT(rtac antisym_less 1 ORELSE atac 1)] )
oheimb@1637
   318
		  (cons'~~inverts);
regensbu@1274
   319
end;
regensbu@1274
   320
oheimb@1637
   321
(* ----- theorems concerning one induction step ----------------------------- *)
regensbu@1274
   322
oheimb@1637
   323
val copy_strict = pg[ax_copy_def](mk_trp(strict(dc_copy`%"f"))) [
oheimb@1637
   324
		   asm_simp_tac(HOLCF_ss addsimps [abs_strict, when_strict,
oheimb@1637
   325
						   cfst_strict,csnd_strict]) 1];
oheimb@1637
   326
val copy_apps = map (fn (con,args) => pg [ax_copy_def]
oheimb@1637
   327
		    (lift_defined % (nonlazy_rec args,
oheimb@1637
   328
			mk_trp(dc_copy`%"f"`(con_app con args) ===
oheimb@1637
   329
		(con_app2 con (app_rec_arg (cproj (%"f") (length eqs))) args))))
oheimb@1637
   330
			(map (case_UU_tac (abs_strict::when_strict::con_stricts)
oheimb@1637
   331
				 1 o vname)
oheimb@1637
   332
			 (filter (fn a => not (is_rec a orelse is_lazy a)) args)
oheimb@1637
   333
			@[asm_simp_tac (HOLCF_ss addsimps when_apps) 1,
oheimb@1637
   334
		          simp_tac (HOLCF_ss addsimps axs_con_def) 1]))cons;
oheimb@1637
   335
val copy_stricts = map (fn (con,args) => pg [] (mk_trp(dc_copy`UU`
oheimb@1637
   336
					(con_app con args) ===UU))
oheimb@1637
   337
     (let val rews = cfst_strict::csnd_strict::copy_strict::copy_apps@con_rews
oheimb@1637
   338
			 in map (case_UU_tac rews 1) (nonlazy args) @ [
oheimb@1637
   339
			     asm_simp_tac (HOLCF_ss addsimps rews) 1] end))
oheimb@1637
   340
  		        (filter (fn (_,args)=>exists is_nonlazy_rec args) cons);
regensbu@1274
   341
val copy_rews = copy_strict::copy_apps @ copy_stricts;
regensbu@1274
   342
regensbu@1274
   343
in     (iso_rews, exhaust, cases, when_rews,
oheimb@1637
   344
	con_rews, sel_rews, dis_rews, dists_le, dists_eq, inverts, injects,
oheimb@1637
   345
	copy_rews)
regensbu@1274
   346
end; (* let *)
regensbu@1274
   347
regensbu@1274
   348
regensbu@1274
   349
fun comp_theorems thy (comp_dname, eqs: eq list, casess, con_rews, copy_rews) =
regensbu@1274
   350
let
regensbu@1274
   351
oheimb@1637
   352
val dummy = writeln("Proving induction properties of domain "^comp_dname^"...");
regensbu@1274
   353
val pg = pg' thy;
regensbu@1274
   354
regensbu@1274
   355
val dnames = map (fst o fst) eqs;
regensbu@1274
   356
val conss  = map  snd        eqs;
regensbu@1274
   357
oheimb@1637
   358
(* ----- getting the composite axiom and definitions ------------------------ *)
regensbu@1274
   359
regensbu@1274
   360
local val ga = get_axiom thy in
regensbu@1274
   361
val axs_reach      = map (fn dn => ga (dn ^  "_reach"   )) dnames;
regensbu@1274
   362
val axs_take_def   = map (fn dn => ga (dn ^  "_take_def")) dnames;
regensbu@1274
   363
val axs_finite_def = map (fn dn => ga (dn ^"_finite_def")) dnames;
regensbu@1274
   364
val ax_copy2_def   = ga (comp_dname^ "_copy_def");
regensbu@1274
   365
val ax_bisim_def   = ga (comp_dname^"_bisim_def");
regensbu@1274
   366
end; (* local *)
regensbu@1274
   367
regensbu@1274
   368
fun dc_take dn = %%(dn^"_take");
regensbu@1274
   369
val x_name = idx_name dnames "x"; 
regensbu@1274
   370
val P_name = idx_name dnames "P";
oheimb@1637
   371
val n_eqs = length eqs;
oheimb@1637
   372
oheimb@1637
   373
(* ----- theorems concerning finite approximation and finite induction ------ *)
regensbu@1274
   374
regensbu@1274
   375
local
oheimb@1637
   376
  val iterate_Cprod_ss = simpset_of "Fix"
oheimb@1637
   377
			 addsimps [cfst_strict, csnd_strict]addsimps Cprod_rews;
regensbu@1274
   378
  val copy_con_rews  = copy_rews @ con_rews;
oheimb@1637
   379
  val copy_take_defs =(if n_eqs = 1 then [] else [ax_copy2_def]) @ axs_take_def;
oheimb@1637
   380
  val take_stricts=pg copy_take_defs(mk_trp(foldr' mk_conj(map(fn((dn,args),_)=>
oheimb@1637
   381
	    (dc_take dn $ %"n")`UU === mk_constrain(Type(dn,args),UU)) eqs)))([
oheimb@1637
   382
			nat_ind_tac "n" 1,
oheimb@1637
   383
			simp_tac iterate_Cprod_ss 1,
oheimb@1637
   384
			asm_simp_tac (iterate_Cprod_ss addsimps copy_rews)1]);
regensbu@1274
   385
  val take_stricts' = rewrite_rule copy_take_defs take_stricts;
oheimb@1637
   386
  val take_0s = mapn(fn n=> fn dn => pg axs_take_def(mk_trp((dc_take dn $ %%"0")
oheimb@1637
   387
							`%x_name n === UU))[
oheimb@1637
   388
				simp_tac iterate_Cprod_ss 1]) 1 dnames;
oheimb@1637
   389
  val c_UU_tac = case_UU_tac (take_stricts'::copy_con_rews) 1;
regensbu@1274
   390
  val take_apps = pg copy_take_defs (mk_trp(foldr' mk_conj 
oheimb@1637
   391
	    (flat(map (fn ((dn,_),cons) => map (fn (con,args) => foldr mk_all 
oheimb@1637
   392
	(map vname args,(dc_take dn $ (%%"Suc" $ %"n"))`(con_app con args) ===
oheimb@1637
   393
  	 con_app2 con (app_rec_arg (fn n=>dc_take (nth_elem(n,dnames))$ %"n"))
oheimb@1637
   394
			      args)) cons) eqs)))) ([
oheimb@1637
   395
				simp_tac iterate_Cprod_ss 1,
oheimb@1637
   396
				nat_ind_tac "n" 1,
oheimb@1637
   397
			    simp_tac(iterate_Cprod_ss addsimps copy_con_rews) 1,
oheimb@1637
   398
				asm_full_simp_tac (HOLCF_ss addsimps 
oheimb@1637
   399
				      (filter (has_fewer_prems 1) copy_rews)) 1,
oheimb@1637
   400
				TRY(safe_tac HOL_cs)] @
oheimb@1637
   401
			(flat(map (fn ((dn,_),cons) => map (fn (con,args) => 
oheimb@1637
   402
				if nonlazy_rec args = [] then all_tac else
oheimb@1637
   403
				EVERY(map c_UU_tac (nonlazy_rec args)) THEN
oheimb@1637
   404
				asm_full_simp_tac (HOLCF_ss addsimps copy_rews)1
oheimb@1637
   405
		 					   ) cons) eqs)));
regensbu@1274
   406
in
regensbu@1274
   407
val take_rews = atomize take_stricts @ take_0s @ atomize take_apps;
regensbu@1274
   408
end; (* local *)
regensbu@1274
   409
regensbu@1274
   410
local
regensbu@1274
   411
  fun one_con p (con,args) = foldr mk_All (map vname args,
oheimb@1637
   412
	lift_defined (bound_arg (map vname args)) (nonlazy args,
oheimb@1637
   413
	lift (fn arg => %(P_name (1+rec_of arg)) $ bound_arg args arg)
oheimb@1637
   414
         (filter is_rec args,mk_trp(%p $ con_app2 con (bound_arg args) args))));
regensbu@1274
   415
  fun one_eq ((p,cons),concl) = (mk_trp(%p $ UU) ===> 
oheimb@1637
   416
			   foldr (op ===>) (map (one_con p) cons,concl));
oheimb@1637
   417
  fun ind_term concf = foldr one_eq (mapn (fn n => fn x => (P_name n, x))1conss,
oheimb@1637
   418
			mk_trp(foldr' mk_conj (mapn concf 1 dnames)));
regensbu@1274
   419
  val take_ss = HOL_ss addsimps take_rews;
oheimb@1637
   420
  fun quant_tac i = EVERY(mapn(fn n=> fn _=> res_inst_tac[("x",x_name n)]spec i)
oheimb@1637
   421
			       1 dnames);
oheimb@1637
   422
  fun ind_prems_tac prems = EVERY(flat (map (fn cons => (
oheimb@1637
   423
				     resolve_tac prems 1 ::
oheimb@1637
   424
				     flat (map (fn (_,args) => 
oheimb@1637
   425
				       resolve_tac prems 1 ::
oheimb@1637
   426
				       map (K(atac 1)) (nonlazy args) @
oheimb@1637
   427
				       map (K(atac 1)) (filter is_rec args))
oheimb@1637
   428
				     cons))) conss));
regensbu@1274
   429
  local 
oheimb@1637
   430
    (* check whether every/exists constructor of the n-th part of the equation:
oheimb@1637
   431
       it has a possibly indirectly recursive argument that isn't/is possibly 
oheimb@1637
   432
       indirectly lazy *)
oheimb@1637
   433
    fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
oheimb@1637
   434
	  is_rec arg andalso not(rec_of arg mem ns) andalso
oheimb@1637
   435
	  ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
oheimb@1637
   436
	    rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
oheimb@1637
   437
	      (lazy_rec orelse is_lazy arg) (n, (nth_elem(rec_of arg,conss))))
oheimb@1637
   438
	  ) o snd) cons;
oheimb@1637
   439
    fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
oheimb@1637
   440
    fun warn (n,cons)  = if all_rec_to [] false (n,cons) then (writeln 
oheimb@1637
   441
        ("WARNING: domain "^nth_elem(n,dnames)^" is empty!"); true) else false;
oheimb@1637
   442
    fun lazy_rec_to ns = rec_to exists Id  lazy_rec_to ns;
oheimb@1637
   443
oheimb@1637
   444
  in val n__eqs     = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
oheimb@1637
   445
     val is_emptys = map warn n__eqs;
oheimb@1637
   446
     val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
regensbu@1274
   447
  end;
oheimb@1637
   448
in (* local *)
oheimb@1637
   449
val finite_ind = pg'' thy [] (ind_term (fn n => fn dn => %(P_name n)$
oheimb@1637
   450
			     (dc_take dn $ %"n" `%(x_name n)))) (fn prems => [
oheimb@1637
   451
				quant_tac 1,
oheimb@1637
   452
				simp_tac quant_ss 1,
oheimb@1637
   453
				nat_ind_tac "n" 1,
oheimb@1637
   454
				simp_tac (take_ss addsimps prems) 1,
oheimb@1637
   455
				TRY(safe_tac HOL_cs)]
oheimb@1637
   456
				@ flat(map (fn (cons,cases) => [
oheimb@1637
   457
				 res_inst_tac [("x","x")] cases 1,
oheimb@1637
   458
				 asm_simp_tac (take_ss addsimps prems) 1]
oheimb@1637
   459
				 @ flat(map (fn (con,args) => 
oheimb@1637
   460
				  asm_simp_tac take_ss 1 ::
oheimb@1637
   461
				  map (fn arg =>
oheimb@1637
   462
				   case_UU_tac (prems@con_rews) 1 (
oheimb@1637
   463
			   nth_elem(rec_of arg,dnames)^"_take n1`"^vname arg))
oheimb@1637
   464
				  (filter is_nonlazy_rec args) @ [
oheimb@1637
   465
				  resolve_tac prems 1] @
oheimb@1637
   466
				  map (K (atac 1))      (nonlazy args) @
oheimb@1637
   467
				  map (K (etac spec 1)) (filter is_rec args)) 
oheimb@1637
   468
				 cons))
oheimb@1637
   469
				(conss~~casess)));
oheimb@1637
   470
oheimb@1637
   471
val take_lemmas =mapn(fn n=> fn(dn,ax_reach)=> pg'' thy axs_take_def(mk_All("n",
oheimb@1637
   472
		mk_trp(dc_take dn $ Bound 0 `%(x_name n) === 
oheimb@1637
   473
		       dc_take dn $ Bound 0 `%(x_name n^"'")))
oheimb@1637
   474
	   ===> mk_trp(%(x_name n) === %(x_name n^"'"))) (fn prems => [
oheimb@1637
   475
			res_inst_tac[("t",x_name n    )](ax_reach RS subst) 1,
oheimb@1637
   476
			res_inst_tac[("t",x_name n^"'")](ax_reach RS subst) 1,
oheimb@1637
   477
				rtac (fix_def2 RS ssubst) 1,
oheimb@1637
   478
				REPEAT(CHANGED(rtac(contlub_cfun_arg RS ssubst)1
oheimb@1637
   479
					       THEN chain_tac 1)),
oheimb@1637
   480
				rtac (contlub_cfun_fun RS ssubst) 1,
oheimb@1637
   481
				rtac (contlub_cfun_fun RS ssubst) 2,
oheimb@1637
   482
				rtac lub_equal 3,
oheimb@1637
   483
				chain_tac 1,
oheimb@1637
   484
				rtac allI 1,
oheimb@1637
   485
				resolve_tac prems 1])) 1 (dnames~~axs_reach);
oheimb@1637
   486
oheimb@1637
   487
(* ----- theorems concerning finiteness and induction ----------------------- *)
regensbu@1274
   488
regensbu@1274
   489
val (finites,ind) = if is_finite then
oheimb@1637
   490
  let 
oheimb@1637
   491
    fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %"x" === %"x");
oheimb@1637
   492
    val finite_lemmas1a = map (fn dn => pg [] (mk_trp(defined (%"x")) ===> 
oheimb@1637
   493
	mk_trp(mk_disj(mk_all("n",dc_take dn $ Bound 0 ` %"x" === UU),
oheimb@1637
   494
	take_enough dn)) ===> mk_trp(take_enough dn)) [
oheimb@1637
   495
				etac disjE 1,
oheimb@1637
   496
				etac notE 1,
oheimb@1637
   497
				resolve_tac take_lemmas 1,
oheimb@1637
   498
				asm_simp_tac take_ss 1,
oheimb@1637
   499
				atac 1]) dnames;
oheimb@1637
   500
    val finite_lemma1b = pg [] (mk_trp (mk_all("n",foldr' mk_conj (mapn 
oheimb@1637
   501
	(fn n => fn ((dn,args),_) => mk_constrainall(x_name n,Type(dn,args),
oheimb@1637
   502
	 mk_disj(dc_take dn $ Bound 1 ` Bound 0 === UU,
oheimb@1637
   503
		 dc_take dn $ Bound 1 ` Bound 0 === Bound 0))) 1 eqs)))) ([
oheimb@1637
   504
				rtac allI 1,
oheimb@1637
   505
				nat_ind_tac "n" 1,
oheimb@1637
   506
				simp_tac take_ss 1,
oheimb@1637
   507
			TRY(safe_tac(empty_cs addSEs[conjE] addSIs[conjI]))] @
oheimb@1637
   508
				flat(mapn (fn n => fn (cons,cases) => [
oheimb@1637
   509
				  simp_tac take_ss 1,
oheimb@1637
   510
				  rtac allI 1,
oheimb@1637
   511
				  res_inst_tac [("x",x_name n)] cases 1,
oheimb@1637
   512
				  asm_simp_tac take_ss 1] @ 
oheimb@1637
   513
				  flat(map (fn (con,args) => 
oheimb@1637
   514
				    asm_simp_tac take_ss 1 ::
oheimb@1637
   515
				    flat(map (fn vn => [
oheimb@1637
   516
				      eres_inst_tac [("x",vn)] all_dupE 1,
oheimb@1637
   517
				      etac disjE 1,
oheimb@1637
   518
				      asm_simp_tac (HOL_ss addsimps con_rews) 1,
oheimb@1637
   519
				      asm_simp_tac take_ss 1])
oheimb@1637
   520
				    (nonlazy_rec args)))
oheimb@1637
   521
				  cons))
oheimb@1637
   522
				1 (conss~~casess))) handle ERROR => raise ERROR;
oheimb@1637
   523
    val finites = map (fn (dn,l1b) => pg axs_finite_def (mk_trp(
oheimb@1637
   524
						%%(dn^"_finite") $ %"x"))[
oheimb@1637
   525
				case_UU_tac take_rews 1 "x",
oheimb@1637
   526
				eresolve_tac finite_lemmas1a 1,
oheimb@1637
   527
				step_tac HOL_cs 1,
oheimb@1637
   528
				step_tac HOL_cs 1,
oheimb@1637
   529
				cut_facts_tac [l1b] 1,
oheimb@1637
   530
			fast_tac HOL_cs 1]) (dnames~~atomize finite_lemma1b);
oheimb@1637
   531
  in
oheimb@1637
   532
  (finites,
oheimb@1637
   533
   pg'' thy[](ind_term (fn n => fn dn => %(P_name n) $ %(x_name n)))(fn prems =>
oheimb@1637
   534
				TRY(safe_tac HOL_cs) ::
oheimb@1637
   535
			 flat (map (fn (finite,fin_ind) => [
oheimb@1637
   536
			       rtac(rewrite_rule axs_finite_def finite RS exE)1,
oheimb@1637
   537
				etac subst 1,
oheimb@1637
   538
				rtac fin_ind 1,
oheimb@1637
   539
				ind_prems_tac prems]) 
oheimb@1637
   540
			           (finites~~(atomize finite_ind)) ))
regensbu@1274
   541
) end (* let *) else
oheimb@1637
   542
  (mapn (fn n => fn dn => read_instantiate_sg (sign_of thy) 
oheimb@1637
   543
	  	    [("P",dn^"_finite "^x_name n)] excluded_middle) 1 dnames,
oheimb@1637
   544
   pg'' thy [] (foldr (op ===>) (mapn (fn n => K(mk_trp(%%"adm" $ %(P_name n))))
oheimb@1637
   545
	       1 dnames, ind_term (fn n => fn dn => %(P_name n) $ %(x_name n))))
oheimb@1637
   546
		   (fn prems => map (fn ax_reach => rtac (ax_reach RS subst) 1) 
oheimb@1637
   547
				    axs_reach @ [
oheimb@1637
   548
				quant_tac 1,
oheimb@1637
   549
				rtac (adm_impl_admw RS wfix_ind) 1,
oheimb@1637
   550
				REPEAT_DETERM(rtac adm_all2 1),
oheimb@1637
   551
				REPEAT_DETERM(TRY(rtac adm_conj 1) THEN 
oheimb@1637
   552
						  rtac adm_subst 1 THEN 
oheimb@1637
   553
					cont_tacR 1 THEN resolve_tac prems 1),
oheimb@1637
   554
				strip_tac 1,
oheimb@1637
   555
				rtac (rewrite_rule axs_take_def finite_ind) 1,
oheimb@1637
   556
				ind_prems_tac prems])
regensbu@1274
   557
)
regensbu@1274
   558
end; (* local *)
regensbu@1274
   559
oheimb@1637
   560
(* ----- theorem concerning coinduction ------------------------------------- *)
oheimb@1637
   561
regensbu@1274
   562
local
regensbu@1274
   563
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
oheimb@1637
   564
  fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
regensbu@1274
   565
  val take_ss = HOL_ss addsimps take_rews;
oheimb@1637
   566
  val sproj   = prj (fn s => "fst("^s^")") (fn s => "snd("^s^")");
oheimb@1637
   567
  val coind_lemma=pg[ax_bisim_def](mk_trp(mk_imp(%%(comp_dname^"_bisim") $ %"R",
oheimb@1637
   568
		foldr (fn (x,t)=> mk_all(x,mk_all(x^"'",t))) (xs,
oheimb@1637
   569
		  foldr mk_imp (mapn (fn n => K(proj (%"R") n_eqs n $ 
oheimb@1637
   570
				      bnd_arg n 0 $ bnd_arg n 1)) 0 dnames,
oheimb@1637
   571
		    foldr' mk_conj (mapn (fn n => fn dn => 
oheimb@1637
   572
				(dc_take dn $ %"n" `bnd_arg n 0 === 
oheimb@1637
   573
				(dc_take dn $ %"n" `bnd_arg n 1)))0 dnames))))))
oheimb@1637
   574
			     ([ rtac impI 1,
oheimb@1637
   575
				nat_ind_tac "n" 1,
oheimb@1637
   576
				simp_tac take_ss 1,
oheimb@1637
   577
				safe_tac HOL_cs] @
oheimb@1637
   578
				flat(mapn (fn n => fn x => [
oheimb@1637
   579
				  rotate_tac (n+1) 1,
oheimb@1637
   580
				  etac all2E 1,
oheimb@1637
   581
				  eres_inst_tac [("P1", sproj "R" n_eqs n^
oheimb@1637
   582
					" "^x^" "^x^"'")](mp RS disjE) 1,
oheimb@1637
   583
				  TRY(safe_tac HOL_cs),
oheimb@1637
   584
				  REPEAT(CHANGED(asm_simp_tac take_ss 1))]) 
oheimb@1637
   585
				0 xs));
regensbu@1274
   586
in
regensbu@1274
   587
val coind = pg [] (mk_trp(%%(comp_dname^"_bisim") $ %"R") ===>
oheimb@1637
   588
		foldr (op ===>) (mapn (fn n => fn x => 
oheimb@1637
   589
		  mk_trp(proj (%"R") n_eqs n $ %x $ %(x^"'"))) 0 xs,
oheimb@1637
   590
		  mk_trp(foldr' mk_conj (map (fn x => %x === %(x^"'")) xs)))) ([
oheimb@1637
   591
				TRY(safe_tac HOL_cs)] @
oheimb@1637
   592
				flat(map (fn take_lemma => [
oheimb@1637
   593
				  rtac take_lemma 1,
oheimb@1637
   594
				  cut_facts_tac [coind_lemma] 1,
oheimb@1637
   595
				  fast_tac HOL_cs 1])
oheimb@1637
   596
				take_lemmas));
regensbu@1274
   597
end; (* local *)
regensbu@1274
   598
regensbu@1274
   599
regensbu@1274
   600
in (take_rews, take_lemmas, finites, finite_ind, ind, coind)
regensbu@1274
   601
regensbu@1274
   602
end; (* let *)
regensbu@1274
   603
end; (* local *)
regensbu@1274
   604
end; (* struct *)