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