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