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