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