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