src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Fri Mar 05 15:59:48 2010 -0800 (2010-03-05)
changeset 35601 50ba5010b876
parent 35599 20670f5564e9
child 35630 8e562d56d404
permissions -rw-r--r--
print message when finiteness of domain definition is detected
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
wenzelm@23152
   171
in
wenzelm@23152
   172
  thy
wenzelm@30364
   173
    |> Sign.add_path (Long_Name.base_name dname)
huffman@31004
   174
    |> snd o PureThy.add_thmss [
huffman@31004
   175
        ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
huffman@31004
   176
        ((Binding.name "exhaust"   , [exhaust]   ), []),
huffman@31004
   177
        ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
huffman@31004
   178
        ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
huffman@31004
   179
        ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
huffman@33427
   180
        ((Binding.name "con_rews"  , con_rews    ),
huffman@33427
   181
         [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
huffman@31004
   182
        ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
huffman@31004
   183
        ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
huffman@31004
   184
        ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
huffman@31004
   185
        ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
huffman@31004
   186
        ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
huffman@31004
   187
        ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
huffman@31004
   188
        ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
huffman@35494
   189
        ((Binding.name "take_rews" , take_rews   ), [Simplifier.simp_add]),
huffman@33427
   190
        ((Binding.name "match_rews", mat_rews    ),
huffman@33427
   191
         [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
wenzelm@24712
   192
    |> Sign.parent_path
haftmann@28536
   193
    |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
huffman@35494
   194
        pat_rews @ dist_les @ dist_eqs)
wenzelm@23152
   195
end; (* let *)
wenzelm@23152
   196
huffman@35585
   197
(******************************************************************************)
huffman@35585
   198
(****************************** induction rules *******************************)
huffman@35585
   199
(******************************************************************************)
huffman@35585
   200
huffman@35585
   201
fun prove_induction
huffman@35585
   202
    (comp_dnam, eqs : eq list)
huffman@35585
   203
    (take_lemmas : thm list)
huffman@35585
   204
    (axs_reach : thm list)
huffman@35585
   205
    (take_rews : thm list)
huffman@35585
   206
    (thy : theory) =
huffman@35585
   207
let
huffman@35585
   208
  val dnames = map (fst o fst) eqs;
huffman@35585
   209
  val conss  = map  snd        eqs;
huffman@35585
   210
  fun dc_take dn = %%:(dn^"_take");
huffman@35585
   211
  val x_name = idx_name dnames "x"; 
huffman@35585
   212
  val P_name = idx_name dnames "P";
huffman@35585
   213
  val pg = pg' thy;
huffman@35585
   214
huffman@35585
   215
  local
huffman@35585
   216
    fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
huffman@35585
   217
    fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
huffman@35585
   218
  in
huffman@35597
   219
    val axs_rep_iso = map (ga "rep_iso") dnames;
huffman@35597
   220
    val axs_abs_iso = map (ga "abs_iso") dnames;
huffman@35585
   221
    val axs_chain_take = map (ga "chain_take") dnames;
huffman@35597
   222
    val lub_take_thms = map (ga "lub_take") dnames;
huffman@35585
   223
    val axs_finite_def = map (ga "finite_def") dnames;
huffman@35597
   224
    val take_0_thms = map (ga "take_0") dnames;
huffman@35597
   225
    val take_Suc_thms = map (ga "take_Suc") dnames;
huffman@35585
   226
    val cases = map (ga  "casedist" ) dnames;
huffman@35585
   227
    val con_rews  = maps (gts "con_rews" ) dnames;
huffman@35585
   228
  end;
huffman@35585
   229
huffman@35585
   230
  fun one_con p (con, args) =
huffman@35585
   231
    let
huffman@35585
   232
      val P_names = map P_name (1 upto (length dnames));
huffman@35585
   233
      val vns = Name.variant_list P_names (map vname args);
huffman@35585
   234
      val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
huffman@35585
   235
      fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
huffman@35585
   236
      val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
huffman@35585
   237
      val t2 = lift ind_hyp (filter is_rec args, t1);
huffman@35585
   238
      val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
huffman@35585
   239
    in Library.foldr mk_All (vns, t3) end;
huffman@35585
   240
huffman@35585
   241
  fun one_eq ((p, cons), concl) =
huffman@35585
   242
    mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
huffman@35585
   243
huffman@35585
   244
  fun ind_term concf = Library.foldr one_eq
huffman@35585
   245
    (mapn (fn n => fn x => (P_name n, x)) 1 conss,
huffman@35585
   246
     mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
huffman@35585
   247
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35585
   248
  fun quant_tac ctxt i = EVERY
huffman@35585
   249
    (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
huffman@35585
   250
huffman@35585
   251
  fun ind_prems_tac prems = EVERY
huffman@35585
   252
    (maps (fn cons =>
huffman@35585
   253
      (resolve_tac prems 1 ::
huffman@35585
   254
        maps (fn (_,args) => 
huffman@35585
   255
          resolve_tac prems 1 ::
huffman@35585
   256
          map (K(atac 1)) (nonlazy args) @
huffman@35585
   257
          map (K(atac 1)) (filter is_rec args))
huffman@35585
   258
        cons))
huffman@35585
   259
      conss);
huffman@35585
   260
  local 
huffman@35585
   261
    (* check whether every/exists constructor of the n-th part of the equation:
huffman@35585
   262
       it has a possibly indirectly recursive argument that isn't/is possibly 
huffman@35585
   263
       indirectly lazy *)
huffman@35585
   264
    fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
huffman@35585
   265
          is_rec arg andalso not(rec_of arg mem ns) andalso
huffman@35585
   266
          ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
huffman@35585
   267
            rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
huffman@35585
   268
              (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
huffman@35585
   269
          ) o snd) cons;
huffman@35585
   270
    fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
huffman@35585
   271
    fun warn (n,cons) =
huffman@35585
   272
      if all_rec_to [] false (n,cons)
huffman@35585
   273
      then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
huffman@35585
   274
      else false;
huffman@35585
   275
    fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
huffman@35585
   276
huffman@35585
   277
  in
huffman@35585
   278
    val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
huffman@35585
   279
    val is_emptys = map warn n__eqs;
huffman@35585
   280
    val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
huffman@35601
   281
    val _ = if is_finite
huffman@35601
   282
            then message ("Proving finiteness rule for domain "^comp_dnam^" ...")
huffman@35601
   283
            else ();
huffman@35585
   284
  end;
huffman@35585
   285
  val _ = trace " Proving finite_ind...";
huffman@35585
   286
  val finite_ind =
huffman@35585
   287
    let
huffman@35585
   288
      fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
huffman@35585
   289
      val goal = ind_term concf;
huffman@35585
   290
huffman@35585
   291
      fun tacf {prems, context} =
huffman@35585
   292
        let
huffman@35585
   293
          val tacs1 = [
huffman@35585
   294
            quant_tac context 1,
huffman@35585
   295
            simp_tac HOL_ss 1,
huffman@35585
   296
            InductTacs.induct_tac context [[SOME "n"]] 1,
huffman@35585
   297
            simp_tac (take_ss addsimps prems) 1,
huffman@35585
   298
            TRY (safe_tac HOL_cs)];
huffman@35585
   299
          fun arg_tac arg =
huffman@35585
   300
                        (* FIXME! case_UU_tac *)
huffman@35585
   301
            case_UU_tac context (prems @ con_rews) 1
huffman@35585
   302
              (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
huffman@35585
   303
          fun con_tacs (con, args) = 
huffman@35585
   304
            asm_simp_tac take_ss 1 ::
huffman@35585
   305
            map arg_tac (filter is_nonlazy_rec args) @
huffman@35585
   306
            [resolve_tac prems 1] @
huffman@35585
   307
            map (K (atac 1)) (nonlazy args) @
huffman@35585
   308
            map (K (etac spec 1)) (filter is_rec args);
huffman@35585
   309
          fun cases_tacs (cons, cases) =
huffman@35585
   310
            res_inst_tac context [(("y", 0), "x")] cases 1 ::
huffman@35585
   311
            asm_simp_tac (take_ss addsimps prems) 1 ::
huffman@35585
   312
            maps con_tacs cons;
huffman@35585
   313
        in
huffman@35585
   314
          tacs1 @ maps cases_tacs (conss ~~ cases)
huffman@35585
   315
        end;
huffman@35585
   316
    in pg'' thy [] goal tacf
huffman@35585
   317
       handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
huffman@35585
   318
    end;
huffman@35585
   319
huffman@35585
   320
(* ----- theorems concerning finiteness and induction ----------------------- *)
huffman@35585
   321
huffman@35585
   322
  val global_ctxt = ProofContext.init thy;
huffman@35585
   323
huffman@35585
   324
  val _ = trace " Proving finites, ind...";
huffman@35585
   325
  val (finites, ind) =
huffman@35585
   326
  (
huffman@35585
   327
    if is_finite
huffman@35585
   328
    then (* finite case *)
huffman@35597
   329
      let
huffman@35597
   330
        val decisive_lemma =
huffman@35585
   331
          let
huffman@35597
   332
            val iso_locale_thms =
huffman@35597
   333
                map2 (fn x => fn y => @{thm iso.intro} OF [x, y])
huffman@35597
   334
                axs_abs_iso axs_rep_iso;
huffman@35597
   335
            val decisive_abs_rep_thms =
huffman@35597
   336
                map (fn x => @{thm decisive_abs_rep} OF [x])
huffman@35597
   337
                iso_locale_thms;
huffman@35597
   338
            val n = Free ("n", @{typ nat});
huffman@35597
   339
            fun mk_decisive t = %%: @{const_name decisive} $ t;
huffman@35597
   340
            fun f dn = mk_decisive (dc_take dn $ n);
huffman@35597
   341
            val goal = mk_trp (foldr1 mk_conj (map f dnames));
huffman@35597
   342
            val rules0 = @{thm decisive_bottom} :: take_0_thms;
huffman@35597
   343
            val rules1 =
huffman@35597
   344
                take_Suc_thms @ decisive_abs_rep_thms
huffman@35597
   345
                @ @{thms decisive_ID decisive_ssum_map decisive_sprod_map};
huffman@35597
   346
            val tacs = [
huffman@35597
   347
                rtac @{thm nat.induct} 1,
huffman@35597
   348
                simp_tac (HOL_ss addsimps rules0) 1,
huffman@35597
   349
                asm_simp_tac (HOL_ss addsimps rules1) 1];
huffman@35597
   350
          in pg [] goal (K tacs) end;
huffman@35597
   351
        fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
huffman@35597
   352
        fun one_finite (dn, decisive_thm) =
huffman@35585
   353
          let
huffman@35585
   354
            val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
huffman@35597
   355
            val tacs = [
huffman@35597
   356
                rtac @{thm lub_ID_finite} 1,
huffman@35597
   357
                resolve_tac axs_chain_take 1,
huffman@35597
   358
                resolve_tac lub_take_thms 1,
huffman@35597
   359
                rtac decisive_thm 1];
huffman@35597
   360
          in pg axs_finite_def goal (K tacs) end;
huffman@35585
   361
huffman@35585
   362
        val _ = trace " Proving finites";
huffman@35597
   363
        val finites = map one_finite (dnames ~~ atomize global_ctxt decisive_lemma);
huffman@35585
   364
        val _ = trace " Proving ind";
huffman@35585
   365
        val ind =
huffman@35585
   366
          let
huffman@35585
   367
            fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35585
   368
            fun tacf {prems, context} =
huffman@35585
   369
              let
huffman@35585
   370
                fun finite_tacs (finite, fin_ind) = [
huffman@35585
   371
                  rtac(rewrite_rule axs_finite_def finite RS exE)1,
huffman@35585
   372
                  etac subst 1,
huffman@35585
   373
                  rtac fin_ind 1,
huffman@35585
   374
                  ind_prems_tac prems];
huffman@35585
   375
              in
huffman@35585
   376
                TRY (safe_tac HOL_cs) ::
huffman@35585
   377
                maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
huffman@35585
   378
              end;
huffman@35585
   379
          in pg'' thy [] (ind_term concf) tacf end;
huffman@35585
   380
      in (finites, ind) end (* let *)
huffman@35585
   381
huffman@35585
   382
    else (* infinite case *)
huffman@35585
   383
      let
huffman@35585
   384
        fun one_finite n dn =
huffman@35585
   385
          read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
huffman@35585
   386
        val finites = mapn one_finite 1 dnames;
huffman@35585
   387
huffman@35585
   388
        val goal =
huffman@35585
   389
          let
huffman@35585
   390
            fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
huffman@35585
   391
            fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35585
   392
          in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
huffman@35585
   393
        val cont_rules =
huffman@35585
   394
            @{thms cont_id cont_const cont2cont_Rep_CFun
huffman@35585
   395
                   cont2cont_fst cont2cont_snd};
huffman@35585
   396
        val subgoal =
huffman@35585
   397
          let fun p n dn = %:(P_name n) $ (dc_take dn $ Bound 0 `%(x_name n));
huffman@35585
   398
          in mk_trp (mk_all ("n", foldr1 mk_conj (mapn p 1 dnames))) end;
huffman@35585
   399
        val subgoal' = legacy_infer_term thy subgoal;
huffman@35585
   400
        fun tacf {prems, context} =
huffman@35585
   401
          let
huffman@35585
   402
            val subtac =
huffman@35585
   403
                EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
huffman@35585
   404
            val subthm = Goal.prove context [] [] subgoal' (K subtac);
huffman@35585
   405
          in
huffman@35585
   406
            map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
huffman@35585
   407
            cut_facts_tac (subthm :: take (length dnames) prems) 1,
huffman@35585
   408
            REPEAT (rtac @{thm conjI} 1 ORELSE
huffman@35585
   409
                    EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
huffman@35585
   410
                           resolve_tac axs_chain_take 1,
huffman@35585
   411
                           asm_simp_tac HOL_basic_ss 1])
huffman@35585
   412
            ]
huffman@35585
   413
          end;
huffman@35585
   414
        val ind = (pg'' thy [] goal tacf
huffman@35585
   415
          handle ERROR _ =>
huffman@35585
   416
            (warning "Cannot prove infinite induction rule"; TrueI)
huffman@35585
   417
                  );
huffman@35585
   418
      in (finites, ind) end
huffman@35585
   419
  )
huffman@35585
   420
      handle THM _ =>
huffman@35585
   421
             (warning "Induction proofs failed (THM raised)."; ([], TrueI))
huffman@35585
   422
           | ERROR _ =>
huffman@35585
   423
             (warning "Cannot prove induction rule"; ([], TrueI));
huffman@35585
   424
huffman@35585
   425
val inducts = Project_Rule.projections (ProofContext.init thy) ind;
huffman@35585
   426
fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
huffman@35585
   427
val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
huffman@35585
   428
huffman@35585
   429
in thy |> Sign.add_path comp_dnam
huffman@35585
   430
       |> snd o PureThy.add_thmss [
huffman@35585
   431
           ((Binding.name "finites"    , finites     ), []),
huffman@35585
   432
           ((Binding.name "finite_ind" , [finite_ind]), []),
huffman@35585
   433
           ((Binding.name "ind"        , [ind]       ), [])]
huffman@35585
   434
       |> (if induct_failed then I
huffman@35585
   435
           else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
huffman@35585
   436
       |> Sign.parent_path
huffman@35585
   437
end; (* prove_induction *)
huffman@35585
   438
huffman@35585
   439
(******************************************************************************)
huffman@35585
   440
(************************ bisimulation and coinduction ************************)
huffman@35585
   441
(******************************************************************************)
huffman@35585
   442
huffman@35574
   443
fun prove_coinduction
huffman@35574
   444
    (comp_dnam, eqs : eq list)
huffman@35574
   445
    (take_lemmas : thm list)
huffman@35599
   446
    (thy : theory) : theory =
wenzelm@23152
   447
let
wenzelm@27232
   448
wenzelm@23152
   449
val dnames = map (fst o fst) eqs;
haftmann@28965
   450
val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35574
   451
fun dc_take dn = %%:(dn^"_take");
huffman@35574
   452
val x_name = idx_name dnames "x"; 
huffman@35574
   453
val n_eqs = length eqs;
wenzelm@23152
   454
huffman@35574
   455
val take_rews =
huffman@35574
   456
    maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
huffman@35497
   457
huffman@35497
   458
(* ----- define bisimulation predicate -------------------------------------- *)
huffman@35497
   459
huffman@35497
   460
local
huffman@35497
   461
  open HOLCF_Library
huffman@35497
   462
  val dtypes  = map (Type o fst) eqs;
huffman@35497
   463
  val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
huffman@35497
   464
  val bisim_bind = Binding.name (comp_dnam ^ "_bisim");
huffman@35497
   465
  val bisim_type = relprod --> boolT;
huffman@35497
   466
in
huffman@35497
   467
  val (bisim_const, thy) =
huffman@35497
   468
      Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
huffman@35497
   469
end;
huffman@35497
   470
huffman@35497
   471
local
huffman@35497
   472
huffman@35497
   473
  fun legacy_infer_term thy t =
huffman@35497
   474
      singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
huffman@35497
   475
  fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
huffman@35497
   476
  fun infer_props thy = map (apsnd (legacy_infer_prop thy));
huffman@35497
   477
  fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
huffman@35497
   478
  fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
huffman@35497
   479
huffman@35497
   480
  val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35497
   481
  val dnames = map (fst o fst) eqs;
huffman@35497
   482
  val x_name = idx_name dnames "x"; 
huffman@35497
   483
huffman@35521
   484
  fun one_con (con, args) =
huffman@35497
   485
    let
huffman@35497
   486
      val nonrec_args = filter_out is_rec args;
huffman@35497
   487
      val    rec_args = filter is_rec args;
huffman@35497
   488
      val    recs_cnt = length rec_args;
huffman@35497
   489
      val allargs     = nonrec_args @ rec_args
huffman@35497
   490
                        @ map (upd_vname (fn s=> s^"'")) rec_args;
huffman@35497
   491
      val allvns      = map vname allargs;
huffman@35497
   492
      fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
huffman@35497
   493
      val vns1        = map (vname_arg "" ) args;
huffman@35497
   494
      val vns2        = map (vname_arg "'") args;
huffman@35497
   495
      val allargs_cnt = length nonrec_args + 2*recs_cnt;
huffman@35497
   496
      val rec_idxs    = (recs_cnt-1) downto 0;
huffman@35497
   497
      val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
huffman@35497
   498
                                             (allargs~~((allargs_cnt-1) downto 0)));
huffman@35497
   499
      fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
huffman@35497
   500
                              Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
huffman@35497
   501
      val capps =
huffman@35497
   502
          List.foldr
huffman@35497
   503
            mk_conj
huffman@35497
   504
            (mk_conj(
huffman@35497
   505
             Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
huffman@35497
   506
             Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
huffman@35497
   507
            (mapn rel_app 1 rec_args);
huffman@35497
   508
    in
huffman@35497
   509
      List.foldr
huffman@35497
   510
        mk_ex
huffman@35497
   511
        (Library.foldr mk_conj
huffman@35497
   512
                       (map (defined o Bound) nonlazy_idxs,capps)) allvns
huffman@35497
   513
    end;
huffman@35497
   514
  fun one_comp n (_,cons) =
huffman@35497
   515
      mk_all (x_name(n+1),
huffman@35497
   516
      mk_all (x_name(n+1)^"'",
huffman@35497
   517
      mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
huffman@35497
   518
      foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
huffman@35497
   519
                      ::map one_con cons))));
huffman@35497
   520
  val bisim_eqn =
huffman@35497
   521
      %%:(comp_dname^"_bisim") ==
huffman@35497
   522
         mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
huffman@35497
   523
huffman@35497
   524
in
huffman@35497
   525
  val ([ax_bisim_def], thy) =
huffman@35497
   526
      thy
huffman@35497
   527
        |> Sign.add_path comp_dnam
huffman@35497
   528
        |> add_defs_infer [(Binding.name "bisim_def", bisim_eqn)]
huffman@35497
   529
        ||> Sign.parent_path;
huffman@35497
   530
end; (* local *)
huffman@35497
   531
huffman@35574
   532
(* ----- theorem concerning coinduction ------------------------------------- *)
huffman@35574
   533
huffman@35574
   534
local
huffman@35574
   535
  val pg = pg' thy;
huffman@35574
   536
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
huffman@35574
   537
  fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
huffman@35574
   538
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35574
   539
  val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
huffman@35574
   540
  val _ = trace " Proving coind_lemma...";
huffman@35574
   541
  val coind_lemma =
huffman@35574
   542
    let
huffman@35574
   543
      fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
huffman@35574
   544
      fun mk_eqn n dn =
huffman@35574
   545
        (dc_take dn $ %:"n" ` bnd_arg n 0) ===
huffman@35574
   546
        (dc_take dn $ %:"n" ` bnd_arg n 1);
huffman@35574
   547
      fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
huffman@35574
   548
      val goal =
huffman@35574
   549
        mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
huffman@35574
   550
          Library.foldr mk_all2 (xs,
huffman@35574
   551
            Library.foldr mk_imp (mapn mk_prj 0 dnames,
huffman@35574
   552
              foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
huffman@35574
   553
      fun x_tacs ctxt n x = [
huffman@35574
   554
        rotate_tac (n+1) 1,
huffman@35574
   555
        etac all2E 1,
huffman@35574
   556
        eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
huffman@35574
   557
        TRY (safe_tac HOL_cs),
huffman@35574
   558
        REPEAT (CHANGED (asm_simp_tac take_ss 1))];
huffman@35574
   559
      fun tacs ctxt = [
huffman@35574
   560
        rtac impI 1,
huffman@35574
   561
        InductTacs.induct_tac ctxt [[SOME "n"]] 1,
huffman@35574
   562
        simp_tac take_ss 1,
huffman@35574
   563
        safe_tac HOL_cs] @
huffman@35574
   564
        flat (mapn (x_tacs ctxt) 0 xs);
huffman@35574
   565
    in pg [ax_bisim_def] goal tacs end;
huffman@35574
   566
in
huffman@35574
   567
  val _ = trace " Proving coind...";
huffman@35574
   568
  val coind = 
huffman@35574
   569
    let
huffman@35574
   570
      fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
huffman@35574
   571
      fun mk_eqn x = %:x === %:(x^"'");
huffman@35574
   572
      val goal =
huffman@35574
   573
        mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
huffman@35574
   574
          Logic.list_implies (mapn mk_prj 0 xs,
huffman@35574
   575
            mk_trp (foldr1 mk_conj (map mk_eqn xs)));
huffman@35574
   576
      val tacs =
huffman@35574
   577
        TRY (safe_tac HOL_cs) ::
huffman@35574
   578
        maps (fn take_lemma => [
huffman@35574
   579
          rtac take_lemma 1,
huffman@35574
   580
          cut_facts_tac [coind_lemma] 1,
huffman@35574
   581
          fast_tac HOL_cs 1])
huffman@35574
   582
        take_lemmas;
huffman@35574
   583
    in pg [] goal (K tacs) end;
huffman@35574
   584
end; (* local *)
huffman@35574
   585
huffman@35599
   586
in thy |> Sign.add_path comp_dnam
huffman@35599
   587
       |> snd o PureThy.add_thmss [((Binding.name "coind", [coind]), [])]
huffman@35599
   588
       |> Sign.parent_path
huffman@35599
   589
end; (* let *)
huffman@35574
   590
huffman@35574
   591
fun comp_theorems (comp_dnam, eqs: eq list) thy =
huffman@35574
   592
let
huffman@35574
   593
val map_tab = Domain_Take_Proofs.get_map_tab thy;
huffman@35574
   594
huffman@35574
   595
val dnames = map (fst o fst) eqs;
huffman@35574
   596
val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35574
   597
huffman@35585
   598
(* ----- getting the composite axiom and definitions ------------------------ *)
wenzelm@23152
   599
huffman@35585
   600
(* Test for indirect recursion *)
huffman@35585
   601
local
huffman@35585
   602
  fun indirect_arg arg =
huffman@35585
   603
      rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
huffman@35585
   604
  fun indirect_con (_, args) = exists indirect_arg args;
huffman@35585
   605
  fun indirect_eq (_, cons) = exists indirect_con cons;
huffman@35585
   606
in
huffman@35585
   607
  val is_indirect = exists indirect_eq eqs;
huffman@35599
   608
  val _ =
huffman@35599
   609
      if is_indirect
huffman@35599
   610
      then message "Indirect recursion detected, skipping proofs of (co)induction rules"
huffman@35599
   611
      else message ("Proving induction properties of domain "^comp_dname^" ...");
huffman@35585
   612
end;
huffman@35585
   613
huffman@35585
   614
(* theorems about take *)
wenzelm@23152
   615
wenzelm@23152
   616
local
wenzelm@26343
   617
  fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
huffman@35494
   618
  val axs_chain_take = map (ga "chain_take") dnames;
huffman@35494
   619
  val axs_lub_take   = map (ga "lub_take"  ) dnames;
wenzelm@23152
   620
in
huffman@29402
   621
  val _ = trace " Proving take_lemmas...";
wenzelm@23152
   622
  val take_lemmas =
wenzelm@23152
   623
    let
huffman@35494
   624
      fun take_lemma (ax_chain_take, ax_lub_take) =
huffman@35585
   625
          Drule.export_without_context
huffman@35585
   626
            (@{thm lub_ID_take_lemma} OF [ax_chain_take, ax_lub_take]);
huffman@35494
   627
    in map take_lemma (axs_chain_take ~~ axs_lub_take) end;
huffman@35494
   628
  val axs_reach =
huffman@35494
   629
    let
huffman@35494
   630
      fun reach (ax_chain_take, ax_lub_take) =
huffman@35585
   631
          Drule.export_without_context
huffman@35585
   632
            (@{thm lub_ID_reach} OF [ax_chain_take, ax_lub_take]);
huffman@35494
   633
    in map reach (axs_chain_take ~~ axs_lub_take) end;
huffman@35585
   634
end;
wenzelm@23152
   635
huffman@35585
   636
val take_rews =
huffman@35585
   637
    maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
wenzelm@23152
   638
huffman@35585
   639
(* prove induction rules, unless definition is indirect recursive *)
huffman@35585
   640
val thy =
huffman@35585
   641
    if is_indirect then thy else
huffman@35585
   642
    prove_induction (comp_dnam, eqs) take_lemmas axs_reach take_rews thy;
wenzelm@23152
   643
huffman@35599
   644
val thy =
huffman@35599
   645
    if is_indirect then thy else
huffman@35599
   646
    prove_coinduction (comp_dnam, eqs) take_lemmas thy;
wenzelm@23152
   647
wenzelm@24712
   648
in thy |> Sign.add_path comp_dnam
huffman@31004
   649
       |> snd o PureThy.add_thmss [
huffman@31004
   650
           ((Binding.name "take_lemmas", take_lemmas ), []),
huffman@35599
   651
           ((Binding.name "reach"      , axs_reach   ), [])]
haftmann@28536
   652
       |> Sign.parent_path |> pair take_rews
wenzelm@23152
   653
end; (* let *)
wenzelm@23152
   654
end; (* struct *)