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