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