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