src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Sun Mar 07 16:39:31 2010 -0800 (2010-03-07)
changeset 35642 f478d5a9d238
parent 35630 8e562d56d404
child 35654 7a15e181bf3b
permissions -rw-r--r--
generate separate qualified theorem name for each type's reach and take_lemma
haftmann@32126
     1
(*  Title:      HOLCF/Tools/Domain/domain_theorems.ML
wenzelm@23152
     2
    Author:     David von Oheimb
wenzelm@32740
     3
    Author:     Brian Huffman
wenzelm@23152
     4
wenzelm@23152
     5
Proof generator for domain command.
wenzelm@23152
     6
*)
wenzelm@23152
     7
wenzelm@26342
     8
val HOLCF_ss = @{simpset};
wenzelm@23152
     9
huffman@31005
    10
signature DOMAIN_THEOREMS =
huffman@31005
    11
sig
huffman@35444
    12
  val theorems:
huffman@35444
    13
    Domain_Library.eq * Domain_Library.eq list
huffman@35444
    14
    -> typ * (binding * (bool * binding option * typ) list * mixfix) list
huffman@35558
    15
    -> Domain_Take_Proofs.iso_info
huffman@35444
    16
    -> theory -> thm list * theory;
huffman@35444
    17
huffman@31005
    18
  val comp_theorems: bstring * Domain_Library.eq list -> theory -> thm list * theory;
wenzelm@32740
    19
  val quiet_mode: bool Unsynchronized.ref;
wenzelm@32740
    20
  val trace_domain: bool Unsynchronized.ref;
huffman@31005
    21
end;
huffman@31005
    22
huffman@31023
    23
structure Domain_Theorems :> DOMAIN_THEOREMS =
huffman@31005
    24
struct
wenzelm@23152
    25
wenzelm@32740
    26
val quiet_mode = Unsynchronized.ref false;
wenzelm@32740
    27
val trace_domain = Unsynchronized.ref false;
huffman@29402
    28
huffman@29402
    29
fun message s = if !quiet_mode then () else writeln s;
huffman@29402
    30
fun trace s = if !trace_domain then tracing s else ();
huffman@29402
    31
wenzelm@23152
    32
open Domain_Library;
wenzelm@23152
    33
infixr 0 ===>;
wenzelm@23152
    34
infixr 0 ==>;
wenzelm@23152
    35
infix 0 == ; 
wenzelm@23152
    36
infix 1 ===;
wenzelm@23152
    37
infix 1 ~= ;
wenzelm@23152
    38
infix 1 <<;
wenzelm@23152
    39
infix 1 ~<<;
wenzelm@23152
    40
infix 9 `   ;
wenzelm@23152
    41
infix 9 `% ;
wenzelm@23152
    42
infix 9 `%%;
wenzelm@23152
    43
infixr 9 oo;
wenzelm@23152
    44
wenzelm@23152
    45
(* ----- general proof facilities ------------------------------------------- *)
wenzelm@23152
    46
wenzelm@24503
    47
fun legacy_infer_term thy t =
wenzelm@24503
    48
  let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
wenzelm@24503
    49
  in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
wenzelm@24503
    50
wenzelm@23152
    51
fun pg'' thy defs t tacs =
wenzelm@23152
    52
  let
wenzelm@24503
    53
    val t' = legacy_infer_term thy t;
wenzelm@23152
    54
    val asms = Logic.strip_imp_prems t';
wenzelm@23152
    55
    val prop = Logic.strip_imp_concl t';
wenzelm@26711
    56
    fun tac {prems, context} =
wenzelm@23152
    57
      rewrite_goals_tac defs THEN
wenzelm@27208
    58
      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
wenzelm@23152
    59
  in Goal.prove_global thy [] asms prop tac end;
wenzelm@23152
    60
wenzelm@23152
    61
fun pg' thy defs t tacsf =
wenzelm@23152
    62
  let
wenzelm@27208
    63
    fun tacs {prems, context} =
wenzelm@27208
    64
      if null prems then tacsf context
wenzelm@27208
    65
      else cut_facts_tac prems 1 :: tacsf context;
wenzelm@23152
    66
  in pg'' thy defs t tacs end;
wenzelm@23152
    67
huffman@35443
    68
(* FIXME!!!!!!!!! *)
huffman@35443
    69
(* We should NEVER re-parse variable names as strings! *)
huffman@35443
    70
(* The names can conflict with existing constants or other syntax! *)
wenzelm@27208
    71
fun case_UU_tac ctxt rews i v =
wenzelm@27208
    72
  InductTacs.case_tac ctxt (v^"=UU") i THEN
wenzelm@23152
    73
  asm_simp_tac (HOLCF_ss addsimps rews) i;
wenzelm@23152
    74
wenzelm@23152
    75
(* ----- general proofs ----------------------------------------------------- *)
wenzelm@23152
    76
wenzelm@29064
    77
val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
wenzelm@23152
    78
huffman@35444
    79
fun theorems
huffman@35444
    80
    (((dname, _), cons) : eq, eqs : eq list)
huffman@35444
    81
    (dom_eqn : typ * (binding * (bool * binding option * typ) list * mixfix) list)
huffman@35558
    82
    (iso_info : Domain_Take_Proofs.iso_info)
huffman@35444
    83
    (thy : theory) =
wenzelm@23152
    84
let
wenzelm@23152
    85
huffman@29402
    86
val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
huffman@35514
    87
val map_tab = Domain_Take_Proofs.get_map_tab thy;
huffman@33801
    88
wenzelm@23152
    89
wenzelm@23152
    90
(* ----- getting the axioms and definitions --------------------------------- *)
wenzelm@23152
    91
huffman@35558
    92
val ax_abs_iso = #abs_inverse iso_info;
huffman@35558
    93
val ax_rep_iso = #rep_inverse iso_info;
huffman@35558
    94
huffman@35558
    95
val abs_const = #abs_const iso_info;
huffman@35558
    96
val rep_const = #rep_const iso_info;
huffman@35558
    97
wenzelm@23152
    98
local
wenzelm@26343
    99
  fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
wenzelm@23152
   100
in
huffman@35494
   101
  val ax_take_0      = ga "take_0" dname;
huffman@35494
   102
  val ax_take_Suc    = ga "take_Suc" dname;
huffman@35494
   103
  val ax_take_strict = ga "take_strict" dname;
wenzelm@23152
   104
end; (* local *)
wenzelm@23152
   105
huffman@35444
   106
(* ----- define constructors ------------------------------------------------ *)
huffman@35444
   107
huffman@35444
   108
val (result, thy) =
huffman@35444
   109
  Domain_Constructors.add_domain_constructors
huffman@35486
   110
    (Long_Name.base_name dname) (snd dom_eqn) iso_info thy;
huffman@35444
   111
huffman@35451
   112
val con_appls = #con_betas result;
huffman@35457
   113
val {exhaust, casedist, ...} = result;
huffman@35458
   114
val {con_compacts, con_rews, inverts, injects, dist_les, dist_eqs, ...} = result;
huffman@35457
   115
val {sel_rews, ...} = result;
huffman@35459
   116
val when_rews = #cases result;
huffman@35459
   117
val when_strict = hd when_rews;
huffman@35461
   118
val dis_rews = #dis_rews result;
huffman@35466
   119
val mat_rews = #match_rews result;
huffman@35482
   120
val pat_rews = #pat_rews result;
huffman@35444
   121
wenzelm@23152
   122
(* ----- theorems concerning the isomorphism -------------------------------- *)
wenzelm@23152
   123
huffman@35444
   124
val pg = pg' thy;
huffman@35444
   125
huffman@35560
   126
val retraction_strict = @{thm retraction_strict};
wenzelm@23152
   127
val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
wenzelm@23152
   128
val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
wenzelm@35021
   129
val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
wenzelm@23152
   130
wenzelm@23152
   131
(* ----- theorems concerning one induction step ----------------------------- *)
wenzelm@23152
   132
huffman@35494
   133
local
huffman@35494
   134
  fun dc_take dn = %%:(dn^"_take");
huffman@35494
   135
  val dnames = map (fst o fst) eqs;
huffman@35523
   136
  val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
huffman@35523
   137
  fun get_deflation_take dn = PureThy.get_thm thy (dn ^ ".deflation_take");
huffman@35523
   138
  val axs_deflation_take = map get_deflation_take dnames;
wenzelm@23152
   139
huffman@35559
   140
  fun copy_of_dtyp tab r dt =
huffman@35559
   141
      if Datatype_Aux.is_rec_type dt then copy tab r dt else ID
huffman@35559
   142
  and copy tab r (Datatype_Aux.DtRec i) = r i
huffman@35559
   143
    | copy tab r (Datatype_Aux.DtTFree a) = ID
huffman@35559
   144
    | copy tab r (Datatype_Aux.DtType (c, ds)) =
huffman@35559
   145
      case Symtab.lookup tab c of
huffman@35559
   146
        SOME f => list_ccomb (%%:f, map (copy_of_dtyp tab r) ds)
huffman@35559
   147
      | NONE => (warning ("copy_of_dtyp: unknown type constructor " ^ c); ID);
huffman@35559
   148
huffman@35521
   149
  fun one_take_app (con, args) =
wenzelm@23152
   150
    let
huffman@35494
   151
      fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
huffman@31232
   152
      fun one_rhs arg =
haftmann@33971
   153
          if Datatype_Aux.is_rec_type (dtyp_of arg)
huffman@35559
   154
          then copy_of_dtyp map_tab
huffman@35494
   155
                 mk_take (dtyp_of arg) ` (%# arg)
huffman@31232
   156
          else (%# arg);
huffman@35494
   157
      val lhs = (dc_take dname $ (%%:"Suc" $ %:"n"))`(con_app con args);
huffman@31232
   158
      val rhs = con_app2 con one_rhs args;
huffman@35494
   159
      val goal = mk_trp (lhs === rhs);
huffman@35590
   160
      val rules =
huffman@35590
   161
          [ax_take_Suc, ax_abs_iso, @{thm cfcomp2}]
huffman@35590
   162
          @ @{thms take_con_rules ID1 deflation_strict}
huffman@35523
   163
          @ deflation_thms @ axs_deflation_take;
huffman@35590
   164
      val tacs = [simp_tac (HOL_basic_ss addsimps rules) 1];
huffman@35494
   165
    in pg con_appls goal (K tacs) end;
huffman@35557
   166
  val take_apps = map one_take_app cons;
wenzelm@23152
   167
in
huffman@35494
   168
  val take_rews = ax_take_0 :: ax_take_strict :: take_apps;
wenzelm@23152
   169
end;
wenzelm@23152
   170
huffman@35630
   171
val case_ns =
huffman@35630
   172
    "bottom" :: map (fn (b,_,_) => Binding.name_of b) (snd dom_eqn);
huffman@35630
   173
wenzelm@23152
   174
in
wenzelm@23152
   175
  thy
wenzelm@30364
   176
    |> Sign.add_path (Long_Name.base_name dname)
huffman@31004
   177
    |> snd o PureThy.add_thmss [
huffman@31004
   178
        ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
huffman@31004
   179
        ((Binding.name "exhaust"   , [exhaust]   ), []),
huffman@35630
   180
        ((Binding.name "casedist"  , [casedist]  ),
huffman@35630
   181
         [Rule_Cases.case_names case_ns, Induct.cases_type dname]),
huffman@31004
   182
        ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
huffman@31004
   183
        ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
huffman@33427
   184
        ((Binding.name "con_rews"  , con_rews    ),
huffman@33427
   185
         [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
huffman@31004
   186
        ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
huffman@31004
   187
        ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
huffman@31004
   188
        ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
huffman@31004
   189
        ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
huffman@31004
   190
        ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
huffman@31004
   191
        ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
huffman@31004
   192
        ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
huffman@35494
   193
        ((Binding.name "take_rews" , take_rews   ), [Simplifier.simp_add]),
huffman@33427
   194
        ((Binding.name "match_rews", mat_rews    ),
huffman@33427
   195
         [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
wenzelm@24712
   196
    |> Sign.parent_path
haftmann@28536
   197
    |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
huffman@35494
   198
        pat_rews @ dist_les @ dist_eqs)
wenzelm@23152
   199
end; (* let *)
wenzelm@23152
   200
huffman@35585
   201
(******************************************************************************)
huffman@35585
   202
(****************************** induction rules *******************************)
huffman@35585
   203
(******************************************************************************)
huffman@35585
   204
huffman@35585
   205
fun prove_induction
huffman@35585
   206
    (comp_dnam, eqs : eq list)
huffman@35585
   207
    (take_lemmas : thm list)
huffman@35585
   208
    (axs_reach : thm list)
huffman@35585
   209
    (take_rews : thm list)
huffman@35585
   210
    (thy : theory) =
huffman@35585
   211
let
huffman@35585
   212
  val dnames = map (fst o fst) eqs;
huffman@35585
   213
  val conss  = map  snd        eqs;
huffman@35585
   214
  fun dc_take dn = %%:(dn^"_take");
huffman@35585
   215
  val x_name = idx_name dnames "x"; 
huffman@35585
   216
  val P_name = idx_name dnames "P";
huffman@35585
   217
  val pg = pg' thy;
huffman@35585
   218
huffman@35585
   219
  local
huffman@35585
   220
    fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
huffman@35585
   221
    fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
huffman@35585
   222
  in
huffman@35597
   223
    val axs_rep_iso = map (ga "rep_iso") dnames;
huffman@35597
   224
    val axs_abs_iso = map (ga "abs_iso") dnames;
huffman@35585
   225
    val axs_chain_take = map (ga "chain_take") dnames;
huffman@35597
   226
    val lub_take_thms = map (ga "lub_take") dnames;
huffman@35585
   227
    val axs_finite_def = map (ga "finite_def") dnames;
huffman@35597
   228
    val take_0_thms = map (ga "take_0") dnames;
huffman@35597
   229
    val take_Suc_thms = map (ga "take_Suc") dnames;
huffman@35585
   230
    val cases = map (ga  "casedist" ) dnames;
huffman@35585
   231
    val con_rews  = maps (gts "con_rews" ) dnames;
huffman@35585
   232
  end;
huffman@35585
   233
huffman@35585
   234
  fun one_con p (con, args) =
huffman@35585
   235
    let
huffman@35585
   236
      val P_names = map P_name (1 upto (length dnames));
huffman@35585
   237
      val vns = Name.variant_list P_names (map vname args);
huffman@35585
   238
      val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
huffman@35585
   239
      fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
huffman@35585
   240
      val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
huffman@35585
   241
      val t2 = lift ind_hyp (filter is_rec args, t1);
huffman@35585
   242
      val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
huffman@35585
   243
    in Library.foldr mk_All (vns, t3) end;
huffman@35585
   244
huffman@35585
   245
  fun one_eq ((p, cons), concl) =
huffman@35585
   246
    mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
huffman@35585
   247
huffman@35585
   248
  fun ind_term concf = Library.foldr one_eq
huffman@35585
   249
    (mapn (fn n => fn x => (P_name n, x)) 1 conss,
huffman@35585
   250
     mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
huffman@35585
   251
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35585
   252
  fun quant_tac ctxt i = EVERY
huffman@35585
   253
    (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
huffman@35585
   254
huffman@35585
   255
  fun ind_prems_tac prems = EVERY
huffman@35585
   256
    (maps (fn cons =>
huffman@35585
   257
      (resolve_tac prems 1 ::
huffman@35585
   258
        maps (fn (_,args) => 
huffman@35585
   259
          resolve_tac prems 1 ::
huffman@35585
   260
          map (K(atac 1)) (nonlazy args) @
huffman@35585
   261
          map (K(atac 1)) (filter is_rec args))
huffman@35585
   262
        cons))
huffman@35585
   263
      conss);
huffman@35585
   264
  local 
huffman@35585
   265
    (* check whether every/exists constructor of the n-th part of the equation:
huffman@35585
   266
       it has a possibly indirectly recursive argument that isn't/is possibly 
huffman@35585
   267
       indirectly lazy *)
huffman@35585
   268
    fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
huffman@35585
   269
          is_rec arg andalso not(rec_of arg mem ns) andalso
huffman@35585
   270
          ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
huffman@35585
   271
            rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
huffman@35585
   272
              (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
huffman@35585
   273
          ) o snd) cons;
huffman@35585
   274
    fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
huffman@35585
   275
    fun warn (n,cons) =
huffman@35585
   276
      if all_rec_to [] false (n,cons)
huffman@35585
   277
      then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
huffman@35585
   278
      else false;
huffman@35585
   279
    fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
huffman@35585
   280
huffman@35585
   281
  in
huffman@35585
   282
    val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
huffman@35585
   283
    val is_emptys = map warn n__eqs;
huffman@35585
   284
    val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
huffman@35601
   285
    val _ = if is_finite
huffman@35601
   286
            then message ("Proving finiteness rule for domain "^comp_dnam^" ...")
huffman@35601
   287
            else ();
huffman@35585
   288
  end;
huffman@35585
   289
  val _ = trace " Proving finite_ind...";
huffman@35585
   290
  val finite_ind =
huffman@35585
   291
    let
huffman@35585
   292
      fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
huffman@35585
   293
      val goal = ind_term concf;
huffman@35585
   294
huffman@35585
   295
      fun tacf {prems, context} =
huffman@35585
   296
        let
huffman@35585
   297
          val tacs1 = [
huffman@35585
   298
            quant_tac context 1,
huffman@35585
   299
            simp_tac HOL_ss 1,
huffman@35585
   300
            InductTacs.induct_tac context [[SOME "n"]] 1,
huffman@35585
   301
            simp_tac (take_ss addsimps prems) 1,
huffman@35585
   302
            TRY (safe_tac HOL_cs)];
huffman@35585
   303
          fun arg_tac arg =
huffman@35585
   304
                        (* FIXME! case_UU_tac *)
huffman@35585
   305
            case_UU_tac context (prems @ con_rews) 1
huffman@35585
   306
              (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
huffman@35585
   307
          fun con_tacs (con, args) = 
huffman@35585
   308
            asm_simp_tac take_ss 1 ::
huffman@35585
   309
            map arg_tac (filter is_nonlazy_rec args) @
huffman@35585
   310
            [resolve_tac prems 1] @
huffman@35585
   311
            map (K (atac 1)) (nonlazy args) @
huffman@35585
   312
            map (K (etac spec 1)) (filter is_rec args);
huffman@35585
   313
          fun cases_tacs (cons, cases) =
huffman@35585
   314
            res_inst_tac context [(("y", 0), "x")] cases 1 ::
huffman@35585
   315
            asm_simp_tac (take_ss addsimps prems) 1 ::
huffman@35585
   316
            maps con_tacs cons;
huffman@35585
   317
        in
huffman@35585
   318
          tacs1 @ maps cases_tacs (conss ~~ cases)
huffman@35585
   319
        end;
huffman@35585
   320
    in pg'' thy [] goal tacf
huffman@35585
   321
       handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
huffman@35585
   322
    end;
huffman@35585
   323
huffman@35585
   324
(* ----- theorems concerning finiteness and induction ----------------------- *)
huffman@35585
   325
huffman@35585
   326
  val global_ctxt = ProofContext.init thy;
huffman@35585
   327
huffman@35585
   328
  val _ = trace " Proving finites, ind...";
huffman@35585
   329
  val (finites, ind) =
huffman@35585
   330
  (
huffman@35585
   331
    if is_finite
huffman@35585
   332
    then (* finite case *)
huffman@35597
   333
      let
huffman@35597
   334
        val decisive_lemma =
huffman@35585
   335
          let
huffman@35597
   336
            val iso_locale_thms =
huffman@35597
   337
                map2 (fn x => fn y => @{thm iso.intro} OF [x, y])
huffman@35597
   338
                axs_abs_iso axs_rep_iso;
huffman@35597
   339
            val decisive_abs_rep_thms =
huffman@35597
   340
                map (fn x => @{thm decisive_abs_rep} OF [x])
huffman@35597
   341
                iso_locale_thms;
huffman@35597
   342
            val n = Free ("n", @{typ nat});
huffman@35597
   343
            fun mk_decisive t = %%: @{const_name decisive} $ t;
huffman@35597
   344
            fun f dn = mk_decisive (dc_take dn $ n);
huffman@35597
   345
            val goal = mk_trp (foldr1 mk_conj (map f dnames));
huffman@35597
   346
            val rules0 = @{thm decisive_bottom} :: take_0_thms;
huffman@35597
   347
            val rules1 =
huffman@35597
   348
                take_Suc_thms @ decisive_abs_rep_thms
huffman@35597
   349
                @ @{thms decisive_ID decisive_ssum_map decisive_sprod_map};
huffman@35597
   350
            val tacs = [
huffman@35597
   351
                rtac @{thm nat.induct} 1,
huffman@35597
   352
                simp_tac (HOL_ss addsimps rules0) 1,
huffman@35597
   353
                asm_simp_tac (HOL_ss addsimps rules1) 1];
huffman@35597
   354
          in pg [] goal (K tacs) end;
huffman@35597
   355
        fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
huffman@35597
   356
        fun one_finite (dn, decisive_thm) =
huffman@35585
   357
          let
huffman@35585
   358
            val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
huffman@35597
   359
            val tacs = [
huffman@35597
   360
                rtac @{thm lub_ID_finite} 1,
huffman@35597
   361
                resolve_tac axs_chain_take 1,
huffman@35597
   362
                resolve_tac lub_take_thms 1,
huffman@35597
   363
                rtac decisive_thm 1];
huffman@35597
   364
          in pg axs_finite_def goal (K tacs) end;
huffman@35585
   365
huffman@35585
   366
        val _ = trace " Proving finites";
huffman@35597
   367
        val finites = map one_finite (dnames ~~ atomize global_ctxt decisive_lemma);
huffman@35585
   368
        val _ = trace " Proving ind";
huffman@35585
   369
        val ind =
huffman@35585
   370
          let
huffman@35585
   371
            fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35585
   372
            fun tacf {prems, context} =
huffman@35585
   373
              let
huffman@35585
   374
                fun finite_tacs (finite, fin_ind) = [
huffman@35585
   375
                  rtac(rewrite_rule axs_finite_def finite RS exE)1,
huffman@35585
   376
                  etac subst 1,
huffman@35585
   377
                  rtac fin_ind 1,
huffman@35585
   378
                  ind_prems_tac prems];
huffman@35585
   379
              in
huffman@35585
   380
                TRY (safe_tac HOL_cs) ::
huffman@35585
   381
                maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
huffman@35585
   382
              end;
huffman@35585
   383
          in pg'' thy [] (ind_term concf) tacf end;
huffman@35585
   384
      in (finites, ind) end (* let *)
huffman@35585
   385
huffman@35585
   386
    else (* infinite case *)
huffman@35585
   387
      let
huffman@35585
   388
        fun one_finite n dn =
huffman@35585
   389
          read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
huffman@35585
   390
        val finites = mapn one_finite 1 dnames;
huffman@35585
   391
huffman@35585
   392
        val goal =
huffman@35585
   393
          let
huffman@35585
   394
            fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
huffman@35585
   395
            fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35585
   396
          in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
huffman@35585
   397
        val cont_rules =
huffman@35585
   398
            @{thms cont_id cont_const cont2cont_Rep_CFun
huffman@35585
   399
                   cont2cont_fst cont2cont_snd};
huffman@35585
   400
        val subgoal =
huffman@35585
   401
          let fun p n dn = %:(P_name n) $ (dc_take dn $ Bound 0 `%(x_name n));
huffman@35585
   402
          in mk_trp (mk_all ("n", foldr1 mk_conj (mapn p 1 dnames))) end;
huffman@35585
   403
        val subgoal' = legacy_infer_term thy subgoal;
huffman@35585
   404
        fun tacf {prems, context} =
huffman@35585
   405
          let
huffman@35585
   406
            val subtac =
huffman@35585
   407
                EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
huffman@35585
   408
            val subthm = Goal.prove context [] [] subgoal' (K subtac);
huffman@35585
   409
          in
huffman@35585
   410
            map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
huffman@35585
   411
            cut_facts_tac (subthm :: take (length dnames) prems) 1,
huffman@35585
   412
            REPEAT (rtac @{thm conjI} 1 ORELSE
huffman@35585
   413
                    EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
huffman@35585
   414
                           resolve_tac axs_chain_take 1,
huffman@35585
   415
                           asm_simp_tac HOL_basic_ss 1])
huffman@35585
   416
            ]
huffman@35585
   417
          end;
huffman@35585
   418
        val ind = (pg'' thy [] goal tacf
huffman@35585
   419
          handle ERROR _ =>
huffman@35585
   420
            (warning "Cannot prove infinite induction rule"; TrueI)
huffman@35585
   421
                  );
huffman@35585
   422
      in (finites, ind) end
huffman@35585
   423
  )
huffman@35585
   424
      handle THM _ =>
huffman@35585
   425
             (warning "Induction proofs failed (THM raised)."; ([], TrueI))
huffman@35585
   426
           | ERROR _ =>
huffman@35585
   427
             (warning "Cannot prove induction rule"; ([], TrueI));
huffman@35585
   428
huffman@35630
   429
val case_ns =
huffman@35630
   430
  let
huffman@35630
   431
    val bottoms =
huffman@35630
   432
        if length dnames = 1 then ["bottom"] else
huffman@35630
   433
        map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
huffman@35630
   434
    fun one_eq bot (_,cons) =
huffman@35630
   435
          bot :: map (fn (c,_) => Long_Name.base_name c) cons;
huffman@35630
   436
  in flat (map2 one_eq bottoms eqs) end;
huffman@35630
   437
huffman@35585
   438
val inducts = Project_Rule.projections (ProofContext.init thy) ind;
huffman@35630
   439
fun ind_rule (dname, rule) =
huffman@35630
   440
    ((Binding.empty, [rule]),
huffman@35630
   441
     [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
huffman@35630
   442
huffman@35585
   443
val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
huffman@35585
   444
huffman@35585
   445
in thy |> Sign.add_path comp_dnam
huffman@35585
   446
       |> snd o PureThy.add_thmss [
huffman@35585
   447
           ((Binding.name "finites"    , finites     ), []),
huffman@35585
   448
           ((Binding.name "finite_ind" , [finite_ind]), []),
huffman@35585
   449
           ((Binding.name "ind"        , [ind]       ), [])]
huffman@35585
   450
       |> (if induct_failed then I
huffman@35585
   451
           else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
huffman@35585
   452
       |> Sign.parent_path
huffman@35585
   453
end; (* prove_induction *)
huffman@35585
   454
huffman@35585
   455
(******************************************************************************)
huffman@35585
   456
(************************ bisimulation and coinduction ************************)
huffman@35585
   457
(******************************************************************************)
huffman@35585
   458
huffman@35574
   459
fun prove_coinduction
huffman@35574
   460
    (comp_dnam, eqs : eq list)
huffman@35574
   461
    (take_lemmas : thm list)
huffman@35599
   462
    (thy : theory) : theory =
wenzelm@23152
   463
let
wenzelm@27232
   464
wenzelm@23152
   465
val dnames = map (fst o fst) eqs;
haftmann@28965
   466
val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35574
   467
fun dc_take dn = %%:(dn^"_take");
huffman@35574
   468
val x_name = idx_name dnames "x"; 
huffman@35574
   469
val n_eqs = length eqs;
wenzelm@23152
   470
huffman@35574
   471
val take_rews =
huffman@35574
   472
    maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
huffman@35497
   473
huffman@35497
   474
(* ----- define bisimulation predicate -------------------------------------- *)
huffman@35497
   475
huffman@35497
   476
local
huffman@35497
   477
  open HOLCF_Library
huffman@35497
   478
  val dtypes  = map (Type o fst) eqs;
huffman@35497
   479
  val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
huffman@35497
   480
  val bisim_bind = Binding.name (comp_dnam ^ "_bisim");
huffman@35497
   481
  val bisim_type = relprod --> boolT;
huffman@35497
   482
in
huffman@35497
   483
  val (bisim_const, thy) =
huffman@35497
   484
      Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
huffman@35497
   485
end;
huffman@35497
   486
huffman@35497
   487
local
huffman@35497
   488
huffman@35497
   489
  fun legacy_infer_term thy t =
huffman@35497
   490
      singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
huffman@35497
   491
  fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
huffman@35497
   492
  fun infer_props thy = map (apsnd (legacy_infer_prop thy));
huffman@35497
   493
  fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
huffman@35497
   494
  fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
huffman@35497
   495
huffman@35497
   496
  val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35497
   497
  val dnames = map (fst o fst) eqs;
huffman@35497
   498
  val x_name = idx_name dnames "x"; 
huffman@35497
   499
huffman@35521
   500
  fun one_con (con, args) =
huffman@35497
   501
    let
huffman@35497
   502
      val nonrec_args = filter_out is_rec args;
huffman@35497
   503
      val    rec_args = filter is_rec args;
huffman@35497
   504
      val    recs_cnt = length rec_args;
huffman@35497
   505
      val allargs     = nonrec_args @ rec_args
huffman@35497
   506
                        @ map (upd_vname (fn s=> s^"'")) rec_args;
huffman@35497
   507
      val allvns      = map vname allargs;
huffman@35497
   508
      fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
huffman@35497
   509
      val vns1        = map (vname_arg "" ) args;
huffman@35497
   510
      val vns2        = map (vname_arg "'") args;
huffman@35497
   511
      val allargs_cnt = length nonrec_args + 2*recs_cnt;
huffman@35497
   512
      val rec_idxs    = (recs_cnt-1) downto 0;
huffman@35497
   513
      val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
huffman@35497
   514
                                             (allargs~~((allargs_cnt-1) downto 0)));
huffman@35497
   515
      fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
huffman@35497
   516
                              Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
huffman@35497
   517
      val capps =
huffman@35497
   518
          List.foldr
huffman@35497
   519
            mk_conj
huffman@35497
   520
            (mk_conj(
huffman@35497
   521
             Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
huffman@35497
   522
             Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
huffman@35497
   523
            (mapn rel_app 1 rec_args);
huffman@35497
   524
    in
huffman@35497
   525
      List.foldr
huffman@35497
   526
        mk_ex
huffman@35497
   527
        (Library.foldr mk_conj
huffman@35497
   528
                       (map (defined o Bound) nonlazy_idxs,capps)) allvns
huffman@35497
   529
    end;
huffman@35497
   530
  fun one_comp n (_,cons) =
huffman@35497
   531
      mk_all (x_name(n+1),
huffman@35497
   532
      mk_all (x_name(n+1)^"'",
huffman@35497
   533
      mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
huffman@35497
   534
      foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
huffman@35497
   535
                      ::map one_con cons))));
huffman@35497
   536
  val bisim_eqn =
huffman@35497
   537
      %%:(comp_dname^"_bisim") ==
huffman@35497
   538
         mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
huffman@35497
   539
huffman@35497
   540
in
huffman@35497
   541
  val ([ax_bisim_def], thy) =
huffman@35497
   542
      thy
huffman@35497
   543
        |> Sign.add_path comp_dnam
huffman@35497
   544
        |> add_defs_infer [(Binding.name "bisim_def", bisim_eqn)]
huffman@35497
   545
        ||> Sign.parent_path;
huffman@35497
   546
end; (* local *)
huffman@35497
   547
huffman@35574
   548
(* ----- theorem concerning coinduction ------------------------------------- *)
huffman@35574
   549
huffman@35574
   550
local
huffman@35574
   551
  val pg = pg' thy;
huffman@35574
   552
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
huffman@35574
   553
  fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
huffman@35574
   554
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35574
   555
  val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
huffman@35574
   556
  val _ = trace " Proving coind_lemma...";
huffman@35574
   557
  val coind_lemma =
huffman@35574
   558
    let
huffman@35574
   559
      fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
huffman@35574
   560
      fun mk_eqn n dn =
huffman@35574
   561
        (dc_take dn $ %:"n" ` bnd_arg n 0) ===
huffman@35574
   562
        (dc_take dn $ %:"n" ` bnd_arg n 1);
huffman@35574
   563
      fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
huffman@35574
   564
      val goal =
huffman@35574
   565
        mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
huffman@35574
   566
          Library.foldr mk_all2 (xs,
huffman@35574
   567
            Library.foldr mk_imp (mapn mk_prj 0 dnames,
huffman@35574
   568
              foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
huffman@35574
   569
      fun x_tacs ctxt n x = [
huffman@35574
   570
        rotate_tac (n+1) 1,
huffman@35574
   571
        etac all2E 1,
huffman@35574
   572
        eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
huffman@35574
   573
        TRY (safe_tac HOL_cs),
huffman@35574
   574
        REPEAT (CHANGED (asm_simp_tac take_ss 1))];
huffman@35574
   575
      fun tacs ctxt = [
huffman@35574
   576
        rtac impI 1,
huffman@35574
   577
        InductTacs.induct_tac ctxt [[SOME "n"]] 1,
huffman@35574
   578
        simp_tac take_ss 1,
huffman@35574
   579
        safe_tac HOL_cs] @
huffman@35574
   580
        flat (mapn (x_tacs ctxt) 0 xs);
huffman@35574
   581
    in pg [ax_bisim_def] goal tacs end;
huffman@35574
   582
in
huffman@35574
   583
  val _ = trace " Proving coind...";
huffman@35574
   584
  val coind = 
huffman@35574
   585
    let
huffman@35574
   586
      fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
huffman@35574
   587
      fun mk_eqn x = %:x === %:(x^"'");
huffman@35574
   588
      val goal =
huffman@35574
   589
        mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
huffman@35574
   590
          Logic.list_implies (mapn mk_prj 0 xs,
huffman@35574
   591
            mk_trp (foldr1 mk_conj (map mk_eqn xs)));
huffman@35574
   592
      val tacs =
huffman@35574
   593
        TRY (safe_tac HOL_cs) ::
huffman@35574
   594
        maps (fn take_lemma => [
huffman@35574
   595
          rtac take_lemma 1,
huffman@35574
   596
          cut_facts_tac [coind_lemma] 1,
huffman@35574
   597
          fast_tac HOL_cs 1])
huffman@35574
   598
        take_lemmas;
huffman@35574
   599
    in pg [] goal (K tacs) end;
huffman@35574
   600
end; (* local *)
huffman@35574
   601
huffman@35599
   602
in thy |> Sign.add_path comp_dnam
huffman@35599
   603
       |> snd o PureThy.add_thmss [((Binding.name "coind", [coind]), [])]
huffman@35599
   604
       |> Sign.parent_path
huffman@35599
   605
end; (* let *)
huffman@35574
   606
huffman@35574
   607
fun comp_theorems (comp_dnam, eqs: eq list) thy =
huffman@35574
   608
let
huffman@35574
   609
val map_tab = Domain_Take_Proofs.get_map_tab thy;
huffman@35574
   610
huffman@35574
   611
val dnames = map (fst o fst) eqs;
huffman@35574
   612
val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35574
   613
huffman@35585
   614
(* ----- getting the composite axiom and definitions ------------------------ *)
wenzelm@23152
   615
huffman@35585
   616
(* Test for indirect recursion *)
huffman@35585
   617
local
huffman@35585
   618
  fun indirect_arg arg =
huffman@35585
   619
      rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
huffman@35585
   620
  fun indirect_con (_, args) = exists indirect_arg args;
huffman@35585
   621
  fun indirect_eq (_, cons) = exists indirect_con cons;
huffman@35585
   622
in
huffman@35585
   623
  val is_indirect = exists indirect_eq eqs;
huffman@35599
   624
  val _ =
huffman@35599
   625
      if is_indirect
huffman@35599
   626
      then message "Indirect recursion detected, skipping proofs of (co)induction rules"
huffman@35599
   627
      else message ("Proving induction properties of domain "^comp_dname^" ...");
huffman@35585
   628
end;
huffman@35585
   629
huffman@35585
   630
(* theorems about take *)
wenzelm@23152
   631
wenzelm@23152
   632
local
wenzelm@26343
   633
  fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
huffman@35494
   634
  val axs_chain_take = map (ga "chain_take") dnames;
huffman@35494
   635
  val axs_lub_take   = map (ga "lub_take"  ) dnames;
huffman@35642
   636
  fun take_thms ((ax_chain_take, ax_lub_take), dname) thy =
wenzelm@23152
   637
    let
huffman@35642
   638
      val dnam = Long_Name.base_name dname;
huffman@35642
   639
      val take_lemma =
huffman@35585
   640
          Drule.export_without_context
huffman@35585
   641
            (@{thm lub_ID_take_lemma} OF [ax_chain_take, ax_lub_take]);
huffman@35642
   642
      val reach =
huffman@35585
   643
          Drule.export_without_context
huffman@35585
   644
            (@{thm lub_ID_reach} OF [ax_chain_take, ax_lub_take]);
huffman@35642
   645
      val thy =
huffman@35642
   646
          thy |> Sign.add_path dnam
huffman@35642
   647
              |> snd o PureThy.add_thms [
huffman@35642
   648
                 ((Binding.name "take_lemma", take_lemma), []),
huffman@35642
   649
                 ((Binding.name "reach"     , reach     ), [])]
huffman@35642
   650
              |> Sign.parent_path;
huffman@35642
   651
    in ((take_lemma, reach), thy) end;
huffman@35642
   652
in
huffman@35642
   653
  val ((take_lemmas, axs_reach), thy) =
huffman@35642
   654
      fold_map take_thms (axs_chain_take ~~ axs_lub_take ~~ dnames) thy
huffman@35642
   655
      |>> ListPair.unzip;
huffman@35585
   656
end;
wenzelm@23152
   657
huffman@35585
   658
val take_rews =
huffman@35585
   659
    maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
wenzelm@23152
   660
huffman@35585
   661
(* prove induction rules, unless definition is indirect recursive *)
huffman@35585
   662
val thy =
huffman@35585
   663
    if is_indirect then thy else
huffman@35585
   664
    prove_induction (comp_dnam, eqs) take_lemmas axs_reach take_rews thy;
wenzelm@23152
   665
huffman@35599
   666
val thy =
huffman@35599
   667
    if is_indirect then thy else
huffman@35599
   668
    prove_coinduction (comp_dnam, eqs) take_lemmas thy;
wenzelm@23152
   669
huffman@35642
   670
in
huffman@35642
   671
  (take_rews, thy)
wenzelm@23152
   672
end; (* let *)
wenzelm@23152
   673
end; (* struct *)