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