src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Thu Oct 14 13:46:27 2010 -0700 (2010-10-14)
changeset 40017 575d3aa1f3c5
parent 40016 2eff1cbc1ccb
child 40018 bf85fef3cce4
permissions -rw-r--r--
include iso_info as part of constr_info type
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@35657
    12
  val comp_theorems :
huffman@35774
    13
      binding * Domain_Library.eq list ->
huffman@40016
    14
      (binding * (binding * (bool * binding option * typ) list * mixfix) list) list ->
huffman@35657
    15
      Domain_Take_Proofs.take_induct_info ->
huffman@40016
    16
      Domain_Constructors.constr_info list ->
huffman@35657
    17
      theory -> thm list * theory
huffman@35657
    18
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@35800
    47
local
wenzelm@35800
    48
wenzelm@35800
    49
fun map_typ f g (Type (c, Ts)) = Type (g c, map (map_typ f g) Ts)
wenzelm@35800
    50
  | map_typ f _ (TFree (x, S)) = TFree (x, map f S)
wenzelm@35800
    51
  | map_typ f _ (TVar (xi, S)) = TVar (xi, map f S);
wenzelm@35800
    52
wenzelm@35800
    53
fun map_term f g h (Const (c, T)) = Const (h c, map_typ f g T)
wenzelm@35800
    54
  | map_term f g _ (Free (x, T)) = Free (x, map_typ f g T)
wenzelm@35800
    55
  | map_term f g _ (Var (xi, T)) = Var (xi, map_typ f g T)
wenzelm@35800
    56
  | map_term _ _ _ (t as Bound _) = t
wenzelm@35800
    57
  | map_term f g h (Abs (x, T, t)) = Abs (x, map_typ f g T, map_term f g h t)
wenzelm@35800
    58
  | map_term f g h (t $ u) = map_term f g h t $ map_term f g h u;
wenzelm@35800
    59
wenzelm@35800
    60
in
wenzelm@35800
    61
wenzelm@35800
    62
fun intern_term thy =
wenzelm@35800
    63
  map_term (Sign.intern_class thy) (Sign.intern_type thy) (Sign.intern_const thy);
wenzelm@35800
    64
wenzelm@35800
    65
end;
wenzelm@35800
    66
wenzelm@24503
    67
fun legacy_infer_term thy t =
wenzelm@36610
    68
  let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init_global thy)
wenzelm@35800
    69
  in singleton (Syntax.check_terms ctxt) (intern_term thy t) end;
wenzelm@24503
    70
wenzelm@23152
    71
fun pg'' thy defs t tacs =
wenzelm@23152
    72
  let
wenzelm@24503
    73
    val t' = legacy_infer_term thy t;
wenzelm@23152
    74
    val asms = Logic.strip_imp_prems t';
wenzelm@23152
    75
    val prop = Logic.strip_imp_concl t';
wenzelm@26711
    76
    fun tac {prems, context} =
wenzelm@23152
    77
      rewrite_goals_tac defs THEN
wenzelm@27208
    78
      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
wenzelm@23152
    79
  in Goal.prove_global thy [] asms prop tac end;
wenzelm@23152
    80
wenzelm@23152
    81
fun pg' thy defs t tacsf =
wenzelm@23152
    82
  let
wenzelm@27208
    83
    fun tacs {prems, context} =
wenzelm@27208
    84
      if null prems then tacsf context
wenzelm@27208
    85
      else cut_facts_tac prems 1 :: tacsf context;
wenzelm@23152
    86
  in pg'' thy defs t tacs end;
wenzelm@23152
    87
huffman@35443
    88
(* FIXME!!!!!!!!! *)
huffman@35443
    89
(* We should NEVER re-parse variable names as strings! *)
huffman@35443
    90
(* The names can conflict with existing constants or other syntax! *)
wenzelm@27208
    91
fun case_UU_tac ctxt rews i v =
wenzelm@27208
    92
  InductTacs.case_tac ctxt (v^"=UU") i THEN
wenzelm@23152
    93
  asm_simp_tac (HOLCF_ss addsimps rews) i;
wenzelm@23152
    94
huffman@40013
    95
(******************************************************************************)
huffman@40013
    96
(***************************** proofs about take ******************************)
huffman@40013
    97
(******************************************************************************)
wenzelm@23152
    98
huffman@40013
    99
fun take_theorems
huffman@40016
   100
    (specs : (binding * (binding * (bool * binding option * typ) list * mixfix) list) list)
huffman@35775
   101
    (take_info : Domain_Take_Proofs.take_induct_info)
huffman@40016
   102
    (constr_infos : Domain_Constructors.constr_info list)
huffman@40016
   103
    (thy : theory) : thm list list * theory =
wenzelm@23152
   104
let
huffman@40016
   105
  open HOLCF_Library;
huffman@35558
   106
huffman@40016
   107
  val {take_consts, take_Suc_thms, deflation_take_thms, ...} = take_info;
huffman@35523
   108
  val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
wenzelm@23152
   109
huffman@40016
   110
  val n = Free ("n", @{typ nat});
huffman@40016
   111
  val n' = @{const Suc} $ n;
huffman@35559
   112
huffman@40016
   113
  local
huffman@40017
   114
    val newTs = map (#absT o #iso_info) constr_infos;
huffman@40016
   115
    val subs = newTs ~~ map (fn t => t $ n) take_consts;
huffman@40016
   116
    fun is_ID (Const (c, _)) = (c = @{const_name ID})
huffman@40016
   117
      | is_ID _              = false;
huffman@40016
   118
  in
huffman@40016
   119
    fun map_of_arg v T =
huffman@40016
   120
      let val m = Domain_Take_Proofs.map_of_typ thy subs T;
huffman@40016
   121
      in if is_ID m then v else mk_capply (m, v) end;
huffman@40016
   122
  end
huffman@40016
   123
huffman@40016
   124
  fun prove_take_apps
huffman@40017
   125
      (((dbind, spec), take_const), constr_info) thy =
wenzelm@23152
   126
    let
huffman@40017
   127
      val {iso_info, con_consts, con_betas, ...} = constr_info;
huffman@40016
   128
      val {abs_inverse, ...} = iso_info;
huffman@40016
   129
      fun prove_take_app (con_const : term) (bind, args, mx) =
huffman@40016
   130
        let
huffman@40016
   131
          val Ts = map (fn (_, _, T) => T) args;
huffman@40016
   132
          val ns = Name.variant_list ["n"] (Datatype_Prop.make_tnames Ts);
huffman@40016
   133
          val vs = map Free (ns ~~ Ts);
huffman@40016
   134
          val lhs = mk_capply (take_const $ n', list_ccomb (con_const, vs));
huffman@40016
   135
          val rhs = list_ccomb (con_const, map2 map_of_arg vs Ts);
huffman@40016
   136
          val goal = mk_trp (mk_eq (lhs, rhs));
huffman@40016
   137
          val rules =
huffman@40016
   138
              [abs_inverse] @ con_betas @ @{thms take_con_rules}
huffman@40016
   139
              @ take_Suc_thms @ deflation_thms @ deflation_take_thms;
huffman@40016
   140
          val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
huffman@40016
   141
        in
huffman@40016
   142
          Goal.prove_global thy [] [] goal (K tac)
huffman@40016
   143
        end;
huffman@40016
   144
      val take_apps = map2 prove_take_app con_consts spec;
huffman@40016
   145
    in
huffman@40016
   146
      yield_singleton Global_Theory.add_thmss
huffman@40016
   147
        ((Binding.qualified true "take_rews" dbind, take_apps),
huffman@40016
   148
        [Simplifier.simp_add]) thy
huffman@40016
   149
    end;
wenzelm@23152
   150
in
huffman@40016
   151
  fold_map prove_take_apps
huffman@40017
   152
    (specs ~~ take_consts ~~ constr_infos) thy
wenzelm@23152
   153
end;
wenzelm@23152
   154
huffman@40013
   155
(* ----- general proofs ----------------------------------------------------- *)
huffman@40013
   156
huffman@40013
   157
val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
huffman@40013
   158
huffman@35585
   159
(******************************************************************************)
huffman@35585
   160
(****************************** induction rules *******************************)
huffman@35585
   161
(******************************************************************************)
huffman@35585
   162
huffman@35585
   163
fun prove_induction
huffman@35774
   164
    (comp_dbind : binding, eqs : eq list)
huffman@35585
   165
    (take_rews : thm list)
huffman@35659
   166
    (take_info : Domain_Take_Proofs.take_induct_info)
huffman@35585
   167
    (thy : theory) =
huffman@35585
   168
let
huffman@35774
   169
  val comp_dname = Sign.full_name thy comp_dbind;
huffman@35585
   170
  val dnames = map (fst o fst) eqs;
huffman@35585
   171
  val conss  = map  snd        eqs;
huffman@35585
   172
  fun dc_take dn = %%:(dn^"_take");
huffman@35662
   173
  val x_name = idx_name dnames "x";
huffman@35585
   174
  val P_name = idx_name dnames "P";
huffman@35585
   175
  val pg = pg' thy;
huffman@35585
   176
huffman@35585
   177
  local
wenzelm@39557
   178
    fun ga s dn = Global_Theory.get_thm thy (dn ^ "." ^ s);
wenzelm@39557
   179
    fun gts s dn = Global_Theory.get_thms thy (dn ^ "." ^ s);
huffman@35585
   180
  in
huffman@35597
   181
    val axs_rep_iso = map (ga "rep_iso") dnames;
huffman@35597
   182
    val axs_abs_iso = map (ga "abs_iso") dnames;
huffman@35781
   183
    val exhausts = map (ga  "exhaust" ) dnames;
huffman@35585
   184
    val con_rews  = maps (gts "con_rews" ) dnames;
huffman@35585
   185
  end;
huffman@35585
   186
huffman@35662
   187
  val {take_consts, ...} = take_info;
huffman@35659
   188
  val {take_0_thms, take_Suc_thms, chain_take_thms, ...} = take_info;
huffman@35660
   189
  val {lub_take_thms, finite_defs, reach_thms, ...} = take_info;
huffman@35661
   190
  val {take_induct_thms, ...} = take_info;
huffman@35658
   191
huffman@35585
   192
  fun one_con p (con, args) =
huffman@35585
   193
    let
huffman@35585
   194
      val P_names = map P_name (1 upto (length dnames));
huffman@35585
   195
      val vns = Name.variant_list P_names (map vname args);
huffman@35585
   196
      val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
huffman@35585
   197
      fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
huffman@35585
   198
      val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
huffman@35585
   199
      val t2 = lift ind_hyp (filter is_rec args, t1);
huffman@35585
   200
      val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
huffman@35585
   201
    in Library.foldr mk_All (vns, t3) end;
huffman@35585
   202
huffman@35585
   203
  fun one_eq ((p, cons), concl) =
huffman@35585
   204
    mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
huffman@35585
   205
huffman@35585
   206
  fun ind_term concf = Library.foldr one_eq
huffman@35585
   207
    (mapn (fn n => fn x => (P_name n, x)) 1 conss,
huffman@35585
   208
     mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
huffman@35585
   209
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35585
   210
  fun quant_tac ctxt i = EVERY
huffman@35585
   211
    (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
huffman@35585
   212
huffman@35585
   213
  fun ind_prems_tac prems = EVERY
huffman@35585
   214
    (maps (fn cons =>
huffman@35585
   215
      (resolve_tac prems 1 ::
huffman@35585
   216
        maps (fn (_,args) => 
huffman@35585
   217
          resolve_tac prems 1 ::
huffman@35585
   218
          map (K(atac 1)) (nonlazy args) @
huffman@35585
   219
          map (K(atac 1)) (filter is_rec args))
huffman@35585
   220
        cons))
huffman@35585
   221
      conss);
huffman@36837
   222
  local
huffman@36837
   223
    fun rec_to ns lazy_rec (n,cons) = forall (exists (fn arg => 
haftmann@36692
   224
          is_rec arg andalso not (member (op =) ns (rec_of arg)) andalso
huffman@36837
   225
          ((rec_of arg =  n andalso not (lazy_rec orelse is_lazy arg)) orelse 
huffman@36837
   226
            rec_of arg <> n andalso rec_to (rec_of arg::ns) 
huffman@35585
   227
              (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
huffman@35585
   228
          ) o snd) cons;
huffman@35585
   229
    fun warn (n,cons) =
huffman@36837
   230
      if rec_to [] false (n,cons)
huffman@35585
   231
      then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
huffman@35585
   232
      else false;
huffman@35585
   233
huffman@35585
   234
  in
huffman@35585
   235
    val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
huffman@35585
   236
    val is_emptys = map warn n__eqs;
huffman@35661
   237
    val is_finite = #is_finite take_info;
huffman@35601
   238
    val _ = if is_finite
huffman@35774
   239
            then message ("Proving finiteness rule for domain "^comp_dname^" ...")
huffman@35601
   240
            else ();
huffman@35585
   241
  end;
huffman@35585
   242
  val _ = trace " Proving finite_ind...";
huffman@35585
   243
  val finite_ind =
huffman@35585
   244
    let
huffman@35585
   245
      fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
huffman@35585
   246
      val goal = ind_term concf;
huffman@35585
   247
huffman@35585
   248
      fun tacf {prems, context} =
huffman@35585
   249
        let
huffman@35585
   250
          val tacs1 = [
huffman@35585
   251
            quant_tac context 1,
huffman@35585
   252
            simp_tac HOL_ss 1,
huffman@35585
   253
            InductTacs.induct_tac context [[SOME "n"]] 1,
huffman@35585
   254
            simp_tac (take_ss addsimps prems) 1,
huffman@35585
   255
            TRY (safe_tac HOL_cs)];
huffman@35585
   256
          fun arg_tac arg =
huffman@35585
   257
                        (* FIXME! case_UU_tac *)
huffman@35585
   258
            case_UU_tac context (prems @ con_rews) 1
huffman@35585
   259
              (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
huffman@35585
   260
          fun con_tacs (con, args) = 
huffman@35585
   261
            asm_simp_tac take_ss 1 ::
huffman@35585
   262
            map arg_tac (filter is_nonlazy_rec args) @
huffman@35585
   263
            [resolve_tac prems 1] @
huffman@35585
   264
            map (K (atac 1)) (nonlazy args) @
huffman@35585
   265
            map (K (etac spec 1)) (filter is_rec args);
huffman@35781
   266
          fun cases_tacs (cons, exhaust) =
huffman@35781
   267
            res_inst_tac context [(("y", 0), "x")] exhaust 1 ::
huffman@35585
   268
            asm_simp_tac (take_ss addsimps prems) 1 ::
huffman@35585
   269
            maps con_tacs cons;
huffman@35585
   270
        in
huffman@35781
   271
          tacs1 @ maps cases_tacs (conss ~~ exhausts)
huffman@35585
   272
        end;
huffman@35663
   273
    in pg'' thy [] goal tacf end;
huffman@35585
   274
huffman@35585
   275
(* ----- theorems concerning finiteness and induction ----------------------- *)
huffman@35585
   276
wenzelm@36610
   277
  val global_ctxt = ProofContext.init_global thy;
huffman@35585
   278
huffman@35661
   279
  val _ = trace " Proving ind...";
huffman@35661
   280
  val ind =
huffman@35585
   281
    if is_finite
huffman@35585
   282
    then (* finite case *)
huffman@35597
   283
      let
huffman@35661
   284
        fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35661
   285
        fun tacf {prems, context} =
huffman@35585
   286
          let
huffman@35661
   287
            fun finite_tacs (take_induct, fin_ind) = [
huffman@35661
   288
                rtac take_induct 1,
huffman@35661
   289
                rtac fin_ind 1,
huffman@35661
   290
                ind_prems_tac prems];
huffman@35661
   291
          in
huffman@35661
   292
            TRY (safe_tac HOL_cs) ::
huffman@35661
   293
            maps finite_tacs (take_induct_thms ~~ atomize global_ctxt finite_ind)
huffman@35661
   294
          end;
huffman@35661
   295
      in pg'' thy [] (ind_term concf) tacf end
huffman@35585
   296
huffman@35585
   297
    else (* infinite case *)
huffman@35585
   298
      let
huffman@35585
   299
        val goal =
huffman@35585
   300
          let
huffman@35585
   301
            fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
huffman@35585
   302
            fun concf n dn = %:(P_name n) $ %:(x_name n);
huffman@35585
   303
          in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
huffman@35585
   304
        val cont_rules =
huffman@35585
   305
            @{thms cont_id cont_const cont2cont_Rep_CFun
huffman@35585
   306
                   cont2cont_fst cont2cont_snd};
huffman@35585
   307
        val subgoal =
huffman@35662
   308
          let
huffman@35662
   309
            val Ts = map (Type o fst) eqs;
huffman@35662
   310
            val P_names = Datatype_Prop.indexify_names (map (K "P") dnames);
huffman@35662
   311
            val x_names = Datatype_Prop.indexify_names (map (K "x") dnames);
huffman@35662
   312
            val P_types = map (fn T => T --> HOLogic.boolT) Ts;
huffman@35662
   313
            val Ps = map Free (P_names ~~ P_types);
huffman@35662
   314
            val xs = map Free (x_names ~~ Ts);
huffman@35662
   315
            val n = Free ("n", HOLogic.natT);
huffman@35662
   316
            val goals =
huffman@35662
   317
                map (fn ((P,t),x) => P $ HOLCF_Library.mk_capply (t $ n, x))
huffman@35662
   318
                  (Ps ~~ take_consts ~~ xs);
huffman@35662
   319
          in
huffman@35662
   320
            HOLogic.mk_Trueprop
huffman@35662
   321
            (HOLogic.mk_all ("n", HOLogic.natT, foldr1 HOLogic.mk_conj goals))
huffman@35662
   322
          end;
huffman@35585
   323
        fun tacf {prems, context} =
huffman@35585
   324
          let
huffman@35585
   325
            val subtac =
huffman@35585
   326
                EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
huffman@35662
   327
            val subthm = Goal.prove context [] [] subgoal (K subtac);
huffman@35585
   328
          in
huffman@35660
   329
            map (fn ax_reach => rtac (ax_reach RS subst) 1) reach_thms @ [
huffman@35585
   330
            cut_facts_tac (subthm :: take (length dnames) prems) 1,
huffman@35585
   331
            REPEAT (rtac @{thm conjI} 1 ORELSE
huffman@35585
   332
                    EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
huffman@35659
   333
                           resolve_tac chain_take_thms 1,
huffman@35585
   334
                           asm_simp_tac HOL_basic_ss 1])
huffman@35585
   335
            ]
huffman@35585
   336
          end;
huffman@35663
   337
      in pg'' thy [] goal tacf end;
huffman@35585
   338
huffman@35630
   339
val case_ns =
huffman@35630
   340
  let
huffman@35782
   341
    val adms =
huffman@35782
   342
        if is_finite then [] else
huffman@35782
   343
        if length dnames = 1 then ["adm"] else
huffman@35782
   344
        map (fn s => "adm_" ^ Long_Name.base_name s) dnames;
huffman@35630
   345
    val bottoms =
huffman@35630
   346
        if length dnames = 1 then ["bottom"] else
huffman@35630
   347
        map (fn s => "bottom_" ^ Long_Name.base_name s) dnames;
huffman@35630
   348
    fun one_eq bot (_,cons) =
huffman@35630
   349
          bot :: map (fn (c,_) => Long_Name.base_name c) cons;
huffman@35782
   350
  in adms @ flat (map2 one_eq bottoms eqs) end;
huffman@35630
   351
wenzelm@36610
   352
val inducts = Project_Rule.projections (ProofContext.init_global thy) ind;
huffman@35630
   353
fun ind_rule (dname, rule) =
huffman@35630
   354
    ((Binding.empty, [rule]),
huffman@35630
   355
     [Rule_Cases.case_names case_ns, Induct.induct_type dname]);
huffman@35630
   356
huffman@35774
   357
in
huffman@35774
   358
  thy
wenzelm@39557
   359
  |> snd o Global_Theory.add_thmss [
huffman@35781
   360
     ((Binding.qualified true "finite_induct" comp_dbind, [finite_ind]), []),
huffman@35781
   361
     ((Binding.qualified true "induct"        comp_dbind, [ind]       ), [])]
wenzelm@39557
   362
  |> (snd o Global_Theory.add_thmss (map ind_rule (dnames ~~ inducts)))
huffman@35585
   363
end; (* prove_induction *)
huffman@35585
   364
huffman@35585
   365
(******************************************************************************)
huffman@35585
   366
(************************ bisimulation and coinduction ************************)
huffman@35585
   367
(******************************************************************************)
huffman@35585
   368
huffman@35574
   369
fun prove_coinduction
huffman@35774
   370
    (comp_dbind : binding, eqs : eq list)
huffman@40016
   371
    (take_rews : thm list)
huffman@35574
   372
    (take_lemmas : thm list)
huffman@35599
   373
    (thy : theory) : theory =
wenzelm@23152
   374
let
wenzelm@27232
   375
wenzelm@23152
   376
val dnames = map (fst o fst) eqs;
huffman@35774
   377
val comp_dname = Sign.full_name thy comp_dbind;
huffman@35574
   378
fun dc_take dn = %%:(dn^"_take");
huffman@35574
   379
val x_name = idx_name dnames "x"; 
huffman@35574
   380
val n_eqs = length eqs;
wenzelm@23152
   381
huffman@35497
   382
(* ----- define bisimulation predicate -------------------------------------- *)
huffman@35497
   383
huffman@35497
   384
local
huffman@35497
   385
  open HOLCF_Library
huffman@35497
   386
  val dtypes  = map (Type o fst) eqs;
huffman@35497
   387
  val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
huffman@35774
   388
  val bisim_bind = Binding.suffix_name "_bisim" comp_dbind;
huffman@35497
   389
  val bisim_type = relprod --> boolT;
huffman@35497
   390
in
huffman@35497
   391
  val (bisim_const, thy) =
huffman@35497
   392
      Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
huffman@35497
   393
end;
huffman@35497
   394
huffman@35497
   395
local
huffman@35497
   396
huffman@35497
   397
  fun legacy_infer_term thy t =
wenzelm@36610
   398
      singleton (Syntax.check_terms (ProofContext.init_global thy)) (intern_term thy t);
wenzelm@39288
   399
  fun legacy_infer_prop thy t = legacy_infer_term thy (Type.constraint propT t);
huffman@35497
   400
  fun infer_props thy = map (apsnd (legacy_infer_prop thy));
wenzelm@39557
   401
  fun add_defs_i x = Global_Theory.add_defs false (map Thm.no_attributes x);
huffman@35497
   402
  fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
huffman@35497
   403
huffman@35521
   404
  fun one_con (con, args) =
huffman@35497
   405
    let
huffman@35497
   406
      val nonrec_args = filter_out is_rec args;
huffman@35497
   407
      val    rec_args = filter is_rec args;
huffman@35497
   408
      val    recs_cnt = length rec_args;
huffman@35497
   409
      val allargs     = nonrec_args @ rec_args
huffman@35497
   410
                        @ map (upd_vname (fn s=> s^"'")) rec_args;
huffman@35497
   411
      val allvns      = map vname allargs;
huffman@35497
   412
      fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
huffman@35497
   413
      val vns1        = map (vname_arg "" ) args;
huffman@35497
   414
      val vns2        = map (vname_arg "'") args;
huffman@35497
   415
      val allargs_cnt = length nonrec_args + 2*recs_cnt;
huffman@35497
   416
      val rec_idxs    = (recs_cnt-1) downto 0;
huffman@35497
   417
      val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
huffman@35497
   418
                                             (allargs~~((allargs_cnt-1) downto 0)));
huffman@35497
   419
      fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
huffman@35497
   420
                              Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
huffman@35497
   421
      val capps =
huffman@35497
   422
          List.foldr
huffman@35497
   423
            mk_conj
huffman@35497
   424
            (mk_conj(
huffman@35497
   425
             Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
huffman@35497
   426
             Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
huffman@35497
   427
            (mapn rel_app 1 rec_args);
huffman@35497
   428
    in
huffman@35497
   429
      List.foldr
huffman@35497
   430
        mk_ex
huffman@35497
   431
        (Library.foldr mk_conj
huffman@35497
   432
                       (map (defined o Bound) nonlazy_idxs,capps)) allvns
huffman@35497
   433
    end;
huffman@35497
   434
  fun one_comp n (_,cons) =
huffman@35497
   435
      mk_all (x_name(n+1),
huffman@35497
   436
      mk_all (x_name(n+1)^"'",
huffman@35497
   437
      mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
huffman@35497
   438
      foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
huffman@35497
   439
                      ::map one_con cons))));
huffman@35497
   440
  val bisim_eqn =
huffman@35497
   441
      %%:(comp_dname^"_bisim") ==
huffman@35497
   442
         mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
huffman@35497
   443
huffman@35497
   444
in
huffman@35774
   445
  val (ax_bisim_def, thy) =
huffman@35774
   446
      yield_singleton add_defs_infer
huffman@35774
   447
        (Binding.qualified true "bisim_def" comp_dbind, bisim_eqn) thy;
huffman@35497
   448
end; (* local *)
huffman@35497
   449
huffman@35574
   450
(* ----- theorem concerning coinduction ------------------------------------- *)
huffman@35574
   451
huffman@35574
   452
local
huffman@35574
   453
  val pg = pg' thy;
huffman@35574
   454
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
huffman@35574
   455
  fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
huffman@35574
   456
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
huffman@35574
   457
  val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
huffman@35574
   458
  val _ = trace " Proving coind_lemma...";
huffman@35574
   459
  val coind_lemma =
huffman@35574
   460
    let
huffman@35574
   461
      fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
huffman@35574
   462
      fun mk_eqn n dn =
huffman@35574
   463
        (dc_take dn $ %:"n" ` bnd_arg n 0) ===
huffman@35574
   464
        (dc_take dn $ %:"n" ` bnd_arg n 1);
huffman@35574
   465
      fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
huffman@35574
   466
      val goal =
huffman@35574
   467
        mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
huffman@35574
   468
          Library.foldr mk_all2 (xs,
huffman@35574
   469
            Library.foldr mk_imp (mapn mk_prj 0 dnames,
huffman@35574
   470
              foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
huffman@35574
   471
      fun x_tacs ctxt n x = [
huffman@35574
   472
        rotate_tac (n+1) 1,
huffman@35574
   473
        etac all2E 1,
huffman@35574
   474
        eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
huffman@35574
   475
        TRY (safe_tac HOL_cs),
huffman@35574
   476
        REPEAT (CHANGED (asm_simp_tac take_ss 1))];
huffman@35574
   477
      fun tacs ctxt = [
huffman@35574
   478
        rtac impI 1,
huffman@35574
   479
        InductTacs.induct_tac ctxt [[SOME "n"]] 1,
huffman@35574
   480
        simp_tac take_ss 1,
huffman@35574
   481
        safe_tac HOL_cs] @
huffman@35574
   482
        flat (mapn (x_tacs ctxt) 0 xs);
huffman@35574
   483
    in pg [ax_bisim_def] goal tacs end;
huffman@35574
   484
in
huffman@35574
   485
  val _ = trace " Proving coind...";
huffman@35574
   486
  val coind = 
huffman@35574
   487
    let
huffman@35574
   488
      fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
huffman@35574
   489
      fun mk_eqn x = %:x === %:(x^"'");
huffman@35574
   490
      val goal =
huffman@35574
   491
        mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
huffman@35574
   492
          Logic.list_implies (mapn mk_prj 0 xs,
huffman@35574
   493
            mk_trp (foldr1 mk_conj (map mk_eqn xs)));
huffman@35574
   494
      val tacs =
huffman@35574
   495
        TRY (safe_tac HOL_cs) ::
huffman@35574
   496
        maps (fn take_lemma => [
huffman@35574
   497
          rtac take_lemma 1,
huffman@35574
   498
          cut_facts_tac [coind_lemma] 1,
huffman@35574
   499
          fast_tac HOL_cs 1])
huffman@35574
   500
        take_lemmas;
huffman@35574
   501
    in pg [] goal (K tacs) end;
huffman@35574
   502
end; (* local *)
huffman@35574
   503
wenzelm@39557
   504
in thy |> snd o Global_Theory.add_thmss
huffman@35781
   505
    [((Binding.qualified true "coinduct" comp_dbind, [coind]), [])]
huffman@35599
   506
end; (* let *)
huffman@35574
   507
huffman@35657
   508
fun comp_theorems
huffman@35774
   509
    (comp_dbind : binding, eqs : eq list)
huffman@40016
   510
    (specs : (binding * (binding * (bool * binding option * typ) list * mixfix) list) list)
huffman@35659
   511
    (take_info : Domain_Take_Proofs.take_induct_info)
huffman@40016
   512
    (constr_infos : Domain_Constructors.constr_info list)
huffman@35657
   513
    (thy : theory) =
huffman@35574
   514
let
huffman@35574
   515
val map_tab = Domain_Take_Proofs.get_map_tab thy;
huffman@35574
   516
huffman@35574
   517
val dnames = map (fst o fst) eqs;
huffman@35774
   518
val comp_dname = Sign.full_name thy comp_dbind;
huffman@35574
   519
huffman@35585
   520
(* ----- getting the composite axiom and definitions ------------------------ *)
wenzelm@23152
   521
huffman@35585
   522
(* Test for indirect recursion *)
huffman@35585
   523
local
huffman@35585
   524
  fun indirect_arg arg =
huffman@35585
   525
      rec_of arg = ~1 andalso Datatype_Aux.is_rec_type (dtyp_of arg);
huffman@35585
   526
  fun indirect_con (_, args) = exists indirect_arg args;
huffman@35585
   527
  fun indirect_eq (_, cons) = exists indirect_con cons;
huffman@35585
   528
in
huffman@35585
   529
  val is_indirect = exists indirect_eq eqs;
huffman@35599
   530
  val _ =
huffman@35599
   531
      if is_indirect
huffman@35599
   532
      then message "Indirect recursion detected, skipping proofs of (co)induction rules"
huffman@35599
   533
      else message ("Proving induction properties of domain "^comp_dname^" ...");
huffman@35585
   534
end;
huffman@35585
   535
huffman@35585
   536
(* theorems about take *)
wenzelm@23152
   537
huffman@40016
   538
val (take_rewss, thy) =
huffman@40017
   539
    take_theorems specs take_info constr_infos thy;
wenzelm@23152
   540
huffman@40016
   541
val {take_lemma_thms, take_0_thms, take_strict_thms, ...} = take_info;
huffman@40016
   542
huffman@40016
   543
val take_rews = take_0_thms @ take_strict_thms @ flat take_rewss;
wenzelm@23152
   544
huffman@35585
   545
(* prove induction rules, unless definition is indirect recursive *)
huffman@35585
   546
val thy =
huffman@35585
   547
    if is_indirect then thy else
huffman@35774
   548
    prove_induction (comp_dbind, eqs) take_rews take_info thy;
wenzelm@23152
   549
huffman@35599
   550
val thy =
huffman@35599
   551
    if is_indirect then thy else
huffman@40016
   552
    prove_coinduction (comp_dbind, eqs) take_rews take_lemma_thms thy;
wenzelm@23152
   553
huffman@35642
   554
in
huffman@35642
   555
  (take_rews, thy)
wenzelm@23152
   556
end; (* let *)
wenzelm@23152
   557
end; (* struct *)