src/HOLCF/Tools/Domain/domain_theorems.ML
author huffman
Tue Mar 02 19:45:37 2010 -0800 (2010-03-02)
changeset 35528 297e801b5465
parent 35523 cc57f4a274a3
child 35557 5da670d57118
permissions -rw-r--r--
proof scripts use variable name y for casedist
haftmann@32126
     1
(*  Title:      HOLCF/Tools/Domain/domain_theorems.ML
wenzelm@23152
     2
    Author:     David von Oheimb
wenzelm@32740
     3
    Author:     Brian Huffman
wenzelm@23152
     4
wenzelm@23152
     5
Proof generator for domain command.
wenzelm@23152
     6
*)
wenzelm@23152
     7
wenzelm@26342
     8
val HOLCF_ss = @{simpset};
wenzelm@23152
     9
huffman@31005
    10
signature DOMAIN_THEOREMS =
huffman@31005
    11
sig
huffman@35444
    12
  val theorems:
huffman@35444
    13
    Domain_Library.eq * Domain_Library.eq list
huffman@35444
    14
    -> typ * (binding * (bool * binding option * typ) list * mixfix) list
huffman@35444
    15
    -> theory -> thm list * theory;
huffman@35444
    16
huffman@31005
    17
  val comp_theorems: bstring * Domain_Library.eq list -> theory -> thm list * theory;
wenzelm@32740
    18
  val quiet_mode: bool Unsynchronized.ref;
wenzelm@32740
    19
  val trace_domain: bool Unsynchronized.ref;
huffman@31005
    20
end;
huffman@31005
    21
huffman@31023
    22
structure Domain_Theorems :> DOMAIN_THEOREMS =
huffman@31005
    23
struct
wenzelm@23152
    24
wenzelm@32740
    25
val quiet_mode = Unsynchronized.ref false;
wenzelm@32740
    26
val trace_domain = Unsynchronized.ref false;
huffman@29402
    27
huffman@29402
    28
fun message s = if !quiet_mode then () else writeln s;
huffman@29402
    29
fun trace s = if !trace_domain then tracing s else ();
huffman@29402
    30
huffman@25805
    31
val adm_impl_admw = @{thm adm_impl_admw};
huffman@25895
    32
val adm_all = @{thm adm_all};
huffman@25805
    33
val adm_conj = @{thm adm_conj};
huffman@25805
    34
val adm_subst = @{thm adm_subst};
huffman@33396
    35
val ch2ch_fst = @{thm ch2ch_fst};
huffman@33396
    36
val ch2ch_snd = @{thm ch2ch_snd};
huffman@25805
    37
val ch2ch_Rep_CFunL = @{thm ch2ch_Rep_CFunL};
huffman@25805
    38
val ch2ch_Rep_CFunR = @{thm ch2ch_Rep_CFunR};
huffman@25805
    39
val chain_iterate = @{thm chain_iterate};
huffman@25805
    40
val contlub_cfun_fun = @{thm contlub_cfun_fun};
huffman@33396
    41
val contlub_fst = @{thm contlub_fst};
huffman@33396
    42
val contlub_snd = @{thm contlub_snd};
huffman@33396
    43
val contlubE = @{thm contlubE};
huffman@33396
    44
val cont_const = @{thm cont_const};
huffman@33396
    45
val cont_id = @{thm cont_id};
huffman@33396
    46
val cont2cont_fst = @{thm cont2cont_fst};
huffman@33396
    47
val cont2cont_snd = @{thm cont2cont_snd};
huffman@33396
    48
val cont2cont_Rep_CFun = @{thm cont2cont_Rep_CFun};
huffman@25805
    49
val fix_def2 = @{thm fix_def2};
huffman@25805
    50
val lub_equal = @{thm lub_equal};
huffman@25805
    51
val retraction_strict = @{thm retraction_strict};
huffman@25805
    52
val wfix_ind = @{thm wfix_ind};
huffman@35464
    53
val iso_intro = @{thm iso.intro};
wenzelm@23152
    54
wenzelm@23152
    55
open Domain_Library;
wenzelm@23152
    56
infixr 0 ===>;
wenzelm@23152
    57
infixr 0 ==>;
wenzelm@23152
    58
infix 0 == ; 
wenzelm@23152
    59
infix 1 ===;
wenzelm@23152
    60
infix 1 ~= ;
wenzelm@23152
    61
infix 1 <<;
wenzelm@23152
    62
infix 1 ~<<;
wenzelm@23152
    63
infix 9 `   ;
wenzelm@23152
    64
infix 9 `% ;
wenzelm@23152
    65
infix 9 `%%;
wenzelm@23152
    66
infixr 9 oo;
wenzelm@23152
    67
wenzelm@23152
    68
(* ----- general proof facilities ------------------------------------------- *)
wenzelm@23152
    69
wenzelm@24503
    70
fun legacy_infer_term thy t =
wenzelm@24503
    71
  let val ctxt = ProofContext.set_mode ProofContext.mode_schematic (ProofContext.init thy)
wenzelm@24503
    72
  in singleton (Syntax.check_terms ctxt) (Sign.intern_term thy t) end;
wenzelm@24503
    73
wenzelm@23152
    74
fun pg'' thy defs t tacs =
wenzelm@23152
    75
  let
wenzelm@24503
    76
    val t' = legacy_infer_term thy t;
wenzelm@23152
    77
    val asms = Logic.strip_imp_prems t';
wenzelm@23152
    78
    val prop = Logic.strip_imp_concl t';
wenzelm@26711
    79
    fun tac {prems, context} =
wenzelm@23152
    80
      rewrite_goals_tac defs THEN
wenzelm@27208
    81
      EVERY (tacs {prems = map (rewrite_rule defs) prems, context = context})
wenzelm@23152
    82
  in Goal.prove_global thy [] asms prop tac end;
wenzelm@23152
    83
wenzelm@23152
    84
fun pg' thy defs t tacsf =
wenzelm@23152
    85
  let
wenzelm@27208
    86
    fun tacs {prems, context} =
wenzelm@27208
    87
      if null prems then tacsf context
wenzelm@27208
    88
      else cut_facts_tac prems 1 :: tacsf context;
wenzelm@23152
    89
  in pg'' thy defs t tacs end;
wenzelm@23152
    90
huffman@35443
    91
(* FIXME!!!!!!!!! *)
huffman@35443
    92
(* We should NEVER re-parse variable names as strings! *)
huffman@35443
    93
(* The names can conflict with existing constants or other syntax! *)
wenzelm@27208
    94
fun case_UU_tac ctxt rews i v =
wenzelm@27208
    95
  InductTacs.case_tac ctxt (v^"=UU") i THEN
wenzelm@23152
    96
  asm_simp_tac (HOLCF_ss addsimps rews) i;
wenzelm@23152
    97
wenzelm@23152
    98
(* ----- general proofs ----------------------------------------------------- *)
wenzelm@23152
    99
wenzelm@29064
   100
val all2E = @{lemma "!x y . P x y ==> (P x y ==> R) ==> R" by simp}
wenzelm@23152
   101
huffman@35444
   102
fun theorems
huffman@35444
   103
    (((dname, _), cons) : eq, eqs : eq list)
huffman@35444
   104
    (dom_eqn : typ * (binding * (bool * binding option * typ) list * mixfix) list)
huffman@35444
   105
    (thy : theory) =
wenzelm@23152
   106
let
wenzelm@23152
   107
huffman@29402
   108
val _ = message ("Proving isomorphism properties of domain "^dname^" ...");
huffman@35514
   109
val map_tab = Domain_Take_Proofs.get_map_tab thy;
huffman@33801
   110
wenzelm@23152
   111
wenzelm@23152
   112
(* ----- getting the axioms and definitions --------------------------------- *)
wenzelm@23152
   113
wenzelm@23152
   114
local
wenzelm@26343
   115
  fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
wenzelm@23152
   116
in
wenzelm@23152
   117
  val ax_abs_iso  = ga "abs_iso"  dname;
wenzelm@23152
   118
  val ax_rep_iso  = ga "rep_iso"  dname;
huffman@35494
   119
  val ax_take_0      = ga "take_0" dname;
huffman@35494
   120
  val ax_take_Suc    = ga "take_Suc" dname;
huffman@35494
   121
  val ax_take_strict = ga "take_strict" dname;
wenzelm@23152
   122
end; (* local *)
wenzelm@23152
   123
huffman@35444
   124
(* ----- define constructors ------------------------------------------------ *)
huffman@35444
   125
huffman@35444
   126
val lhsT = fst dom_eqn;
huffman@35444
   127
huffman@35444
   128
val rhsT =
huffman@35444
   129
  let
huffman@35444
   130
    fun mk_arg_typ (lazy, sel, T) = if lazy then mk_uT T else T;
huffman@35444
   131
    fun mk_con_typ (bind, args, mx) =
huffman@35444
   132
        if null args then oneT else foldr1 mk_sprodT (map mk_arg_typ args);
huffman@35444
   133
    fun mk_eq_typ (_, cons) = foldr1 mk_ssumT (map mk_con_typ cons);
huffman@35444
   134
  in
huffman@35444
   135
    mk_eq_typ dom_eqn
huffman@35444
   136
  end;
huffman@35444
   137
huffman@35444
   138
val rep_const = Const(dname^"_rep", lhsT ->> rhsT);
huffman@35444
   139
huffman@35444
   140
val abs_const = Const(dname^"_abs", rhsT ->> lhsT);
huffman@35444
   141
huffman@35514
   142
val iso_info : Domain_Take_Proofs.iso_info =
huffman@35481
   143
  {
huffman@35481
   144
    absT = lhsT,
huffman@35481
   145
    repT = rhsT,
huffman@35481
   146
    abs_const = abs_const,
huffman@35481
   147
    rep_const = rep_const,
huffman@35481
   148
    abs_inverse = ax_abs_iso,
huffman@35481
   149
    rep_inverse = ax_rep_iso
huffman@35481
   150
  };
huffman@35481
   151
huffman@35444
   152
val (result, thy) =
huffman@35444
   153
  Domain_Constructors.add_domain_constructors
huffman@35486
   154
    (Long_Name.base_name dname) (snd dom_eqn) iso_info thy;
huffman@35444
   155
huffman@35451
   156
val con_appls = #con_betas result;
huffman@35457
   157
val {exhaust, casedist, ...} = result;
huffman@35458
   158
val {con_compacts, con_rews, inverts, injects, dist_les, dist_eqs, ...} = result;
huffman@35457
   159
val {sel_rews, ...} = result;
huffman@35459
   160
val when_rews = #cases result;
huffman@35459
   161
val when_strict = hd when_rews;
huffman@35461
   162
val dis_rews = #dis_rews result;
huffman@35466
   163
val mat_rews = #match_rews result;
huffman@35482
   164
val pat_rews = #pat_rews result;
huffman@35444
   165
wenzelm@23152
   166
(* ----- theorems concerning the isomorphism -------------------------------- *)
wenzelm@23152
   167
huffman@35444
   168
val pg = pg' thy;
huffman@35444
   169
wenzelm@23152
   170
val abs_strict = ax_rep_iso RS (allI RS retraction_strict);
wenzelm@23152
   171
val rep_strict = ax_abs_iso RS (allI RS retraction_strict);
wenzelm@35021
   172
val iso_rews = map Drule.export_without_context [ax_abs_iso, ax_rep_iso, abs_strict, rep_strict];
wenzelm@23152
   173
wenzelm@23152
   174
(* ----- theorems concerning one induction step ----------------------------- *)
wenzelm@23152
   175
huffman@35494
   176
local
huffman@35494
   177
  fun dc_take dn = %%:(dn^"_take");
huffman@35494
   178
  val dnames = map (fst o fst) eqs;
huffman@35523
   179
  val deflation_thms = Domain_Take_Proofs.get_deflation_thms thy;
huffman@35523
   180
  fun get_deflation_take dn = PureThy.get_thm thy (dn ^ ".deflation_take");
huffman@35523
   181
  val axs_deflation_take = map get_deflation_take dnames;
wenzelm@23152
   182
huffman@35521
   183
  fun one_take_app (con, args) =
wenzelm@23152
   184
    let
huffman@35494
   185
      fun mk_take n = dc_take (List.nth (dnames, n)) $ %:"n";
huffman@31232
   186
      fun one_rhs arg =
haftmann@33971
   187
          if Datatype_Aux.is_rec_type (dtyp_of arg)
huffman@33801
   188
          then Domain_Axioms.copy_of_dtyp map_tab
huffman@35494
   189
                 mk_take (dtyp_of arg) ` (%# arg)
huffman@31232
   190
          else (%# arg);
huffman@35494
   191
      val lhs = (dc_take dname $ (%%:"Suc" $ %:"n"))`(con_app con args);
huffman@31232
   192
      val rhs = con_app2 con one_rhs args;
huffman@35494
   193
      val goal = mk_trp (lhs === rhs);
huffman@35494
   194
      val rules = [ax_take_Suc, ax_abs_iso, @{thm cfcomp2}];
huffman@35523
   195
      val rules2 =
huffman@35523
   196
          @{thms take_con_rules ID1 deflation_strict}
huffman@35523
   197
          @ deflation_thms @ axs_deflation_take;
huffman@35494
   198
      val tacs =
huffman@35494
   199
          [simp_tac (HOL_basic_ss addsimps rules) 1,
huffman@35494
   200
           asm_simp_tac (HOL_basic_ss addsimps rules2) 1];
huffman@35494
   201
    in pg con_appls goal (K tacs) end;
huffman@35494
   202
  val take_apps = map (Drule.export_without_context o one_take_app) cons;
wenzelm@23152
   203
in
huffman@35494
   204
  val take_rews = ax_take_0 :: ax_take_strict :: take_apps;
wenzelm@23152
   205
end;
wenzelm@23152
   206
wenzelm@23152
   207
in
wenzelm@23152
   208
  thy
wenzelm@30364
   209
    |> Sign.add_path (Long_Name.base_name dname)
huffman@31004
   210
    |> snd o PureThy.add_thmss [
huffman@31004
   211
        ((Binding.name "iso_rews"  , iso_rews    ), [Simplifier.simp_add]),
huffman@31004
   212
        ((Binding.name "exhaust"   , [exhaust]   ), []),
huffman@31004
   213
        ((Binding.name "casedist"  , [casedist]  ), [Induct.cases_type dname]),
huffman@31004
   214
        ((Binding.name "when_rews" , when_rews   ), [Simplifier.simp_add]),
huffman@31004
   215
        ((Binding.name "compacts"  , con_compacts), [Simplifier.simp_add]),
huffman@33427
   216
        ((Binding.name "con_rews"  , con_rews    ),
huffman@33427
   217
         [Simplifier.simp_add, Fixrec.fixrec_simp_add]),
huffman@31004
   218
        ((Binding.name "sel_rews"  , sel_rews    ), [Simplifier.simp_add]),
huffman@31004
   219
        ((Binding.name "dis_rews"  , dis_rews    ), [Simplifier.simp_add]),
huffman@31004
   220
        ((Binding.name "pat_rews"  , pat_rews    ), [Simplifier.simp_add]),
huffman@31004
   221
        ((Binding.name "dist_les"  , dist_les    ), [Simplifier.simp_add]),
huffman@31004
   222
        ((Binding.name "dist_eqs"  , dist_eqs    ), [Simplifier.simp_add]),
huffman@31004
   223
        ((Binding.name "inverts"   , inverts     ), [Simplifier.simp_add]),
huffman@31004
   224
        ((Binding.name "injects"   , injects     ), [Simplifier.simp_add]),
huffman@35494
   225
        ((Binding.name "take_rews" , take_rews   ), [Simplifier.simp_add]),
huffman@33427
   226
        ((Binding.name "match_rews", mat_rews    ),
huffman@33427
   227
         [Simplifier.simp_add, Fixrec.fixrec_simp_add])]
wenzelm@24712
   228
    |> Sign.parent_path
haftmann@28536
   229
    |> pair (iso_rews @ when_rews @ con_rews @ sel_rews @ dis_rews @
huffman@35494
   230
        pat_rews @ dist_les @ dist_eqs)
wenzelm@23152
   231
end; (* let *)
wenzelm@23152
   232
wenzelm@23152
   233
fun comp_theorems (comp_dnam, eqs: eq list) thy =
wenzelm@23152
   234
let
huffman@35514
   235
val map_tab = Domain_Take_Proofs.get_map_tab thy;
wenzelm@27232
   236
wenzelm@23152
   237
val dnames = map (fst o fst) eqs;
wenzelm@23152
   238
val conss  = map  snd        eqs;
haftmann@28965
   239
val comp_dname = Sign.full_bname thy comp_dnam;
wenzelm@23152
   240
huffman@29402
   241
val _ = message ("Proving induction properties of domain "^comp_dname^" ...");
huffman@35497
   242
huffman@35497
   243
(* ----- define bisimulation predicate -------------------------------------- *)
huffman@35497
   244
huffman@35497
   245
local
huffman@35497
   246
  open HOLCF_Library
huffman@35497
   247
  val dtypes  = map (Type o fst) eqs;
huffman@35497
   248
  val relprod = mk_tupleT (map (fn tp => tp --> tp --> boolT) dtypes);
huffman@35497
   249
  val bisim_bind = Binding.name (comp_dnam ^ "_bisim");
huffman@35497
   250
  val bisim_type = relprod --> boolT;
huffman@35497
   251
in
huffman@35497
   252
  val (bisim_const, thy) =
huffman@35497
   253
      Sign.declare_const ((bisim_bind, bisim_type), NoSyn) thy;
huffman@35497
   254
end;
huffman@35497
   255
huffman@35497
   256
local
huffman@35497
   257
huffman@35497
   258
  fun legacy_infer_term thy t =
huffman@35497
   259
      singleton (Syntax.check_terms (ProofContext.init thy)) (Sign.intern_term thy t);
huffman@35497
   260
  fun legacy_infer_prop thy t = legacy_infer_term thy (TypeInfer.constrain propT t);
huffman@35497
   261
  fun infer_props thy = map (apsnd (legacy_infer_prop thy));
huffman@35497
   262
  fun add_defs_i x = PureThy.add_defs false (map Thm.no_attributes x);
huffman@35497
   263
  fun add_defs_infer defs thy = add_defs_i (infer_props thy defs) thy;
huffman@35497
   264
huffman@35497
   265
  val comp_dname = Sign.full_bname thy comp_dnam;
huffman@35497
   266
  val dnames = map (fst o fst) eqs;
huffman@35497
   267
  val x_name = idx_name dnames "x"; 
huffman@35497
   268
huffman@35521
   269
  fun one_con (con, args) =
huffman@35497
   270
    let
huffman@35497
   271
      val nonrec_args = filter_out is_rec args;
huffman@35497
   272
      val    rec_args = filter is_rec args;
huffman@35497
   273
      val    recs_cnt = length rec_args;
huffman@35497
   274
      val allargs     = nonrec_args @ rec_args
huffman@35497
   275
                        @ map (upd_vname (fn s=> s^"'")) rec_args;
huffman@35497
   276
      val allvns      = map vname allargs;
huffman@35497
   277
      fun vname_arg s arg = if is_rec arg then vname arg^s else vname arg;
huffman@35497
   278
      val vns1        = map (vname_arg "" ) args;
huffman@35497
   279
      val vns2        = map (vname_arg "'") args;
huffman@35497
   280
      val allargs_cnt = length nonrec_args + 2*recs_cnt;
huffman@35497
   281
      val rec_idxs    = (recs_cnt-1) downto 0;
huffman@35497
   282
      val nonlazy_idxs = map snd (filter_out (fn (arg,_) => is_lazy arg)
huffman@35497
   283
                                             (allargs~~((allargs_cnt-1) downto 0)));
huffman@35497
   284
      fun rel_app i ra = proj (Bound(allargs_cnt+2)) eqs (rec_of ra) $ 
huffman@35497
   285
                              Bound (2*recs_cnt-i) $ Bound (recs_cnt-i);
huffman@35497
   286
      val capps =
huffman@35497
   287
          List.foldr
huffman@35497
   288
            mk_conj
huffman@35497
   289
            (mk_conj(
huffman@35497
   290
             Bound(allargs_cnt+1)===list_ccomb(%%:con,map (bound_arg allvns) vns1),
huffman@35497
   291
             Bound(allargs_cnt+0)===list_ccomb(%%:con,map (bound_arg allvns) vns2)))
huffman@35497
   292
            (mapn rel_app 1 rec_args);
huffman@35497
   293
    in
huffman@35497
   294
      List.foldr
huffman@35497
   295
        mk_ex
huffman@35497
   296
        (Library.foldr mk_conj
huffman@35497
   297
                       (map (defined o Bound) nonlazy_idxs,capps)) allvns
huffman@35497
   298
    end;
huffman@35497
   299
  fun one_comp n (_,cons) =
huffman@35497
   300
      mk_all (x_name(n+1),
huffman@35497
   301
      mk_all (x_name(n+1)^"'",
huffman@35497
   302
      mk_imp (proj (Bound 2) eqs n $ Bound 1 $ Bound 0,
huffman@35497
   303
      foldr1 mk_disj (mk_conj(Bound 1 === UU,Bound 0 === UU)
huffman@35497
   304
                      ::map one_con cons))));
huffman@35497
   305
  val bisim_eqn =
huffman@35497
   306
      %%:(comp_dname^"_bisim") ==
huffman@35497
   307
         mk_lam("R", foldr1 mk_conj (mapn one_comp 0 eqs));
huffman@35497
   308
huffman@35497
   309
in
huffman@35497
   310
  val ([ax_bisim_def], thy) =
huffman@35497
   311
      thy
huffman@35497
   312
        |> Sign.add_path comp_dnam
huffman@35497
   313
        |> add_defs_infer [(Binding.name "bisim_def", bisim_eqn)]
huffman@35497
   314
        ||> Sign.parent_path;
huffman@35497
   315
end; (* local *)
huffman@35497
   316
wenzelm@23152
   317
val pg = pg' thy;
wenzelm@23152
   318
wenzelm@23152
   319
(* ----- getting the composite axiom and definitions ------------------------ *)
wenzelm@23152
   320
wenzelm@23152
   321
local
wenzelm@26343
   322
  fun ga s dn = PureThy.get_thm thy (dn ^ "." ^ s);
wenzelm@23152
   323
in
wenzelm@23152
   324
  val axs_take_def   = map (ga "take_def"  ) dnames;
huffman@35494
   325
  val axs_chain_take = map (ga "chain_take") dnames;
huffman@35494
   326
  val axs_lub_take   = map (ga "lub_take"  ) dnames;
wenzelm@23152
   327
  val axs_finite_def = map (ga "finite_def") dnames;
wenzelm@23152
   328
end;
wenzelm@23152
   329
wenzelm@23152
   330
local
wenzelm@26343
   331
  fun gt  s dn = PureThy.get_thm  thy (dn ^ "." ^ s);
wenzelm@26343
   332
  fun gts s dn = PureThy.get_thms thy (dn ^ "." ^ s);
wenzelm@23152
   333
in
wenzelm@23152
   334
  val cases = map (gt  "casedist" ) dnames;
wenzelm@26336
   335
  val con_rews  = maps (gts "con_rews" ) dnames;
wenzelm@23152
   336
end;
wenzelm@23152
   337
wenzelm@23152
   338
fun dc_take dn = %%:(dn^"_take");
wenzelm@23152
   339
val x_name = idx_name dnames "x"; 
wenzelm@23152
   340
val P_name = idx_name dnames "P";
wenzelm@23152
   341
val n_eqs = length eqs;
wenzelm@23152
   342
wenzelm@23152
   343
(* ----- theorems concerning finite approximation and finite induction ------ *)
wenzelm@23152
   344
huffman@35494
   345
val take_rews =
huffman@35494
   346
    maps (fn dn => PureThy.get_thms thy (dn ^ ".take_rews")) dnames;
wenzelm@23152
   347
wenzelm@23152
   348
local
huffman@35521
   349
  fun one_con p (con, args) =
wenzelm@23152
   350
    let
huffman@35443
   351
      val P_names = map P_name (1 upto (length dnames));
huffman@35443
   352
      val vns = Name.variant_list P_names (map vname args);
huffman@35443
   353
      val nonlazy_vns = map snd (filter_out (is_lazy o fst) (args ~~ vns));
wenzelm@23152
   354
      fun ind_hyp arg = %:(P_name (1 + rec_of arg)) $ bound_arg args arg;
wenzelm@23152
   355
      val t1 = mk_trp (%:p $ con_app2 con (bound_arg args) args);
wenzelm@33317
   356
      val t2 = lift ind_hyp (filter is_rec args, t1);
huffman@35443
   357
      val t3 = lift_defined (bound_arg vns) (nonlazy_vns, t2);
huffman@35443
   358
    in Library.foldr mk_All (vns, t3) end;
wenzelm@23152
   359
wenzelm@23152
   360
  fun one_eq ((p, cons), concl) =
wenzelm@23152
   361
    mk_trp (%:p $ UU) ===> Logic.list_implies (map (one_con p) cons, concl);
wenzelm@23152
   362
wenzelm@23152
   363
  fun ind_term concf = Library.foldr one_eq
wenzelm@23152
   364
    (mapn (fn n => fn x => (P_name n, x)) 1 conss,
wenzelm@23152
   365
     mk_trp (foldr1 mk_conj (mapn concf 1 dnames)));
huffman@35494
   366
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
wenzelm@27208
   367
  fun quant_tac ctxt i = EVERY
wenzelm@27239
   368
    (mapn (fn n => fn _ => res_inst_tac ctxt [(("x", 0), x_name n)] spec i) 1 dnames);
wenzelm@23152
   369
wenzelm@23152
   370
  fun ind_prems_tac prems = EVERY
wenzelm@26336
   371
    (maps (fn cons =>
wenzelm@23152
   372
      (resolve_tac prems 1 ::
huffman@35521
   373
        maps (fn (_,args) => 
wenzelm@23152
   374
          resolve_tac prems 1 ::
wenzelm@23152
   375
          map (K(atac 1)) (nonlazy args) @
wenzelm@33317
   376
          map (K(atac 1)) (filter is_rec args))
wenzelm@26336
   377
        cons))
wenzelm@26336
   378
      conss);
wenzelm@23152
   379
  local 
wenzelm@23152
   380
    (* check whether every/exists constructor of the n-th part of the equation:
wenzelm@23152
   381
       it has a possibly indirectly recursive argument that isn't/is possibly 
wenzelm@23152
   382
       indirectly lazy *)
wenzelm@23152
   383
    fun rec_to quant nfn rfn ns lazy_rec (n,cons) = quant (exists (fn arg => 
wenzelm@23152
   384
          is_rec arg andalso not(rec_of arg mem ns) andalso
wenzelm@23152
   385
          ((rec_of arg =  n andalso nfn(lazy_rec orelse is_lazy arg)) orelse 
wenzelm@23152
   386
            rec_of arg <> n andalso rec_to quant nfn rfn (rec_of arg::ns) 
wenzelm@23152
   387
              (lazy_rec orelse is_lazy arg) (n, (List.nth(conss,rec_of arg))))
huffman@35521
   388
          ) o snd) cons;
wenzelm@23152
   389
    fun all_rec_to ns  = rec_to forall not all_rec_to  ns;
wenzelm@23152
   390
    fun warn (n,cons) =
wenzelm@23152
   391
      if all_rec_to [] false (n,cons)
wenzelm@23152
   392
      then (warning ("domain "^List.nth(dnames,n)^" is empty!"); true)
wenzelm@23152
   393
      else false;
wenzelm@23152
   394
    fun lazy_rec_to ns = rec_to exists I  lazy_rec_to ns;
wenzelm@23152
   395
wenzelm@23152
   396
  in
wenzelm@23152
   397
    val n__eqs = mapn (fn n => fn (_,cons) => (n,cons)) 0 eqs;
wenzelm@23152
   398
    val is_emptys = map warn n__eqs;
wenzelm@23152
   399
    val is_finite = forall (not o lazy_rec_to [] false) n__eqs;
wenzelm@23152
   400
  end;
wenzelm@23152
   401
in (* local *)
huffman@29402
   402
  val _ = trace " Proving finite_ind...";
wenzelm@23152
   403
  val finite_ind =
wenzelm@23152
   404
    let
wenzelm@23152
   405
      fun concf n dn = %:(P_name n) $ (dc_take dn $ %:"n" `%(x_name n));
wenzelm@23152
   406
      val goal = ind_term concf;
wenzelm@23152
   407
wenzelm@27208
   408
      fun tacf {prems, context} =
wenzelm@23152
   409
        let
wenzelm@23152
   410
          val tacs1 = [
wenzelm@27208
   411
            quant_tac context 1,
wenzelm@23152
   412
            simp_tac HOL_ss 1,
wenzelm@27208
   413
            InductTacs.induct_tac context [[SOME "n"]] 1,
wenzelm@23152
   414
            simp_tac (take_ss addsimps prems) 1,
wenzelm@23152
   415
            TRY (safe_tac HOL_cs)];
wenzelm@23152
   416
          fun arg_tac arg =
huffman@35443
   417
                        (* FIXME! case_UU_tac *)
wenzelm@27208
   418
            case_UU_tac context (prems @ con_rews) 1
wenzelm@23152
   419
              (List.nth (dnames, rec_of arg) ^ "_take n$" ^ vname arg);
huffman@35521
   420
          fun con_tacs (con, args) = 
wenzelm@23152
   421
            asm_simp_tac take_ss 1 ::
wenzelm@33317
   422
            map arg_tac (filter is_nonlazy_rec args) @
wenzelm@23152
   423
            [resolve_tac prems 1] @
wenzelm@33317
   424
            map (K (atac 1)) (nonlazy args) @
wenzelm@33317
   425
            map (K (etac spec 1)) (filter is_rec args);
wenzelm@23152
   426
          fun cases_tacs (cons, cases) =
huffman@35528
   427
            res_inst_tac context [(("y", 0), "x")] cases 1 ::
wenzelm@23152
   428
            asm_simp_tac (take_ss addsimps prems) 1 ::
wenzelm@26336
   429
            maps con_tacs cons;
wenzelm@23152
   430
        in
wenzelm@26336
   431
          tacs1 @ maps cases_tacs (conss ~~ cases)
wenzelm@23152
   432
        end;
huffman@31232
   433
    in pg'' thy [] goal tacf
huffman@31232
   434
       handle ERROR _ => (warning "Proof of finite_ind failed."; TrueI)
huffman@31232
   435
    end;
wenzelm@23152
   436
huffman@29402
   437
  val _ = trace " Proving take_lemmas...";
wenzelm@23152
   438
  val take_lemmas =
wenzelm@23152
   439
    let
huffman@35494
   440
      fun take_lemma (ax_chain_take, ax_lub_take) =
huffman@35494
   441
        @{thm lub_ID_take_lemma} OF [ax_chain_take, ax_lub_take];
huffman@35494
   442
    in map take_lemma (axs_chain_take ~~ axs_lub_take) end;
huffman@35494
   443
huffman@35494
   444
  val axs_reach =
huffman@35494
   445
    let
huffman@35494
   446
      fun reach (ax_chain_take, ax_lub_take) =
huffman@35494
   447
        @{thm lub_ID_reach} OF [ax_chain_take, ax_lub_take];
huffman@35494
   448
    in map reach (axs_chain_take ~~ axs_lub_take) end;
wenzelm@23152
   449
wenzelm@23152
   450
(* ----- theorems concerning finiteness and induction ----------------------- *)
wenzelm@23152
   451
huffman@35497
   452
  val global_ctxt = ProofContext.init thy;
huffman@35497
   453
huffman@29402
   454
  val _ = trace " Proving finites, ind...";
wenzelm@23152
   455
  val (finites, ind) =
huffman@31232
   456
  (
wenzelm@23152
   457
    if is_finite
wenzelm@23152
   458
    then (* finite case *)
wenzelm@23152
   459
      let 
wenzelm@23152
   460
        fun take_enough dn = mk_ex ("n",dc_take dn $ Bound 0 ` %:"x" === %:"x");
wenzelm@23152
   461
        fun dname_lemma dn =
wenzelm@23152
   462
          let
wenzelm@23152
   463
            val prem1 = mk_trp (defined (%:"x"));
wenzelm@23152
   464
            val disj1 = mk_all ("n", dc_take dn $ Bound 0 ` %:"x" === UU);
wenzelm@23152
   465
            val prem2 = mk_trp (mk_disj (disj1, take_enough dn));
wenzelm@23152
   466
            val concl = mk_trp (take_enough dn);
wenzelm@23152
   467
            val goal = prem1 ===> prem2 ===> concl;
wenzelm@23152
   468
            val tacs = [
wenzelm@23152
   469
              etac disjE 1,
wenzelm@23152
   470
              etac notE 1,
wenzelm@23152
   471
              resolve_tac take_lemmas 1,
wenzelm@23152
   472
              asm_simp_tac take_ss 1,
wenzelm@23152
   473
              atac 1];
wenzelm@27208
   474
          in pg [] goal (K tacs) end;
huffman@31232
   475
        val _ = trace " Proving finite_lemmas1a";
wenzelm@23152
   476
        val finite_lemmas1a = map dname_lemma dnames;
wenzelm@23152
   477
 
huffman@31232
   478
        val _ = trace " Proving finite_lemma1b";
wenzelm@23152
   479
        val finite_lemma1b =
wenzelm@23152
   480
          let
wenzelm@23152
   481
            fun mk_eqn n ((dn, args), _) =
wenzelm@23152
   482
              let
wenzelm@23152
   483
                val disj1 = dc_take dn $ Bound 1 ` Bound 0 === UU;
wenzelm@23152
   484
                val disj2 = dc_take dn $ Bound 1 ` Bound 0 === Bound 0;
wenzelm@23152
   485
              in
wenzelm@23152
   486
                mk_constrainall
wenzelm@23152
   487
                  (x_name n, Type (dn,args), mk_disj (disj1, disj2))
wenzelm@23152
   488
              end;
wenzelm@23152
   489
            val goal =
wenzelm@23152
   490
              mk_trp (mk_all ("n", foldr1 mk_conj (mapn mk_eqn 1 eqs)));
wenzelm@27208
   491
            fun arg_tacs ctxt vn = [
wenzelm@27239
   492
              eres_inst_tac ctxt [(("x", 0), vn)] all_dupE 1,
wenzelm@23152
   493
              etac disjE 1,
wenzelm@23152
   494
              asm_simp_tac (HOL_ss addsimps con_rews) 1,
wenzelm@23152
   495
              asm_simp_tac take_ss 1];
huffman@35521
   496
            fun con_tacs ctxt (con, args) =
wenzelm@23152
   497
              asm_simp_tac take_ss 1 ::
wenzelm@27208
   498
              maps (arg_tacs ctxt) (nonlazy_rec args);
wenzelm@27208
   499
            fun foo_tacs ctxt n (cons, cases) =
wenzelm@23152
   500
              simp_tac take_ss 1 ::
wenzelm@23152
   501
              rtac allI 1 ::
huffman@35528
   502
              res_inst_tac ctxt [(("y", 0), x_name n)] cases 1 ::
wenzelm@23152
   503
              asm_simp_tac take_ss 1 ::
wenzelm@27208
   504
              maps (con_tacs ctxt) cons;
wenzelm@27208
   505
            fun tacs ctxt =
wenzelm@23152
   506
              rtac allI 1 ::
wenzelm@27208
   507
              InductTacs.induct_tac ctxt [[SOME "n"]] 1 ::
wenzelm@23152
   508
              simp_tac take_ss 1 ::
wenzelm@23152
   509
              TRY (safe_tac (empty_cs addSEs [conjE] addSIs [conjI])) ::
wenzelm@27208
   510
              flat (mapn (foo_tacs ctxt) 1 (conss ~~ cases));
wenzelm@23152
   511
          in pg [] goal tacs end;
wenzelm@23152
   512
wenzelm@23152
   513
        fun one_finite (dn, l1b) =
wenzelm@23152
   514
          let
wenzelm@23152
   515
            val goal = mk_trp (%%:(dn^"_finite") $ %:"x");
wenzelm@27208
   516
            fun tacs ctxt = [
huffman@35443
   517
                        (* FIXME! case_UU_tac *)
wenzelm@27208
   518
              case_UU_tac ctxt take_rews 1 "x",
wenzelm@23152
   519
              eresolve_tac finite_lemmas1a 1,
wenzelm@23152
   520
              step_tac HOL_cs 1,
wenzelm@23152
   521
              step_tac HOL_cs 1,
wenzelm@23152
   522
              cut_facts_tac [l1b] 1,
wenzelm@23152
   523
              fast_tac HOL_cs 1];
wenzelm@23152
   524
          in pg axs_finite_def goal tacs end;
wenzelm@23152
   525
huffman@31232
   526
        val _ = trace " Proving finites";
wenzelm@27232
   527
        val finites = map one_finite (dnames ~~ atomize global_ctxt finite_lemma1b);
huffman@31232
   528
        val _ = trace " Proving ind";
wenzelm@23152
   529
        val ind =
wenzelm@23152
   530
          let
wenzelm@23152
   531
            fun concf n dn = %:(P_name n) $ %:(x_name n);
wenzelm@27208
   532
            fun tacf {prems, context} =
wenzelm@23152
   533
              let
wenzelm@23152
   534
                fun finite_tacs (finite, fin_ind) = [
wenzelm@23152
   535
                  rtac(rewrite_rule axs_finite_def finite RS exE)1,
wenzelm@23152
   536
                  etac subst 1,
wenzelm@23152
   537
                  rtac fin_ind 1,
wenzelm@23152
   538
                  ind_prems_tac prems];
wenzelm@23152
   539
              in
wenzelm@23152
   540
                TRY (safe_tac HOL_cs) ::
wenzelm@27232
   541
                maps finite_tacs (finites ~~ atomize global_ctxt finite_ind)
wenzelm@23152
   542
              end;
wenzelm@23152
   543
          in pg'' thy [] (ind_term concf) tacf end;
wenzelm@23152
   544
      in (finites, ind) end (* let *)
wenzelm@23152
   545
wenzelm@23152
   546
    else (* infinite case *)
wenzelm@23152
   547
      let
wenzelm@23152
   548
        fun one_finite n dn =
wenzelm@27239
   549
          read_instantiate global_ctxt [(("P", 0), dn ^ "_finite " ^ x_name n)] excluded_middle;
wenzelm@23152
   550
        val finites = mapn one_finite 1 dnames;
wenzelm@23152
   551
wenzelm@23152
   552
        val goal =
wenzelm@23152
   553
          let
huffman@26012
   554
            fun one_adm n _ = mk_trp (mk_adm (%:(P_name n)));
wenzelm@23152
   555
            fun concf n dn = %:(P_name n) $ %:(x_name n);
wenzelm@23152
   556
          in Logic.list_implies (mapn one_adm 1 dnames, ind_term concf) end;
huffman@33396
   557
        val cont_rules =
huffman@33396
   558
            [cont_id, cont_const, cont2cont_Rep_CFun,
huffman@33396
   559
             cont2cont_fst, cont2cont_snd];
huffman@35494
   560
        val subgoal =
huffman@35494
   561
          let fun p n dn = %:(P_name n) $ (dc_take dn $ Bound 0 `%(x_name n));
huffman@35494
   562
          in mk_trp (mk_all ("n", foldr1 mk_conj (mapn p 1 dnames))) end;
huffman@35494
   563
        val subgoal' = legacy_infer_term thy subgoal;
wenzelm@27208
   564
        fun tacf {prems, context} =
huffman@35494
   565
          let
huffman@35494
   566
            val subtac =
huffman@35494
   567
                EVERY [rtac allI 1, rtac finite_ind 1, ind_prems_tac prems];
huffman@35494
   568
            val subthm = Goal.prove context [] [] subgoal' (K subtac);
huffman@35494
   569
          in
huffman@35494
   570
            map (fn ax_reach => rtac (ax_reach RS subst) 1) axs_reach @ [
huffman@35494
   571
            cut_facts_tac (subthm :: take (length dnames) prems) 1,
huffman@35494
   572
            REPEAT (rtac @{thm conjI} 1 ORELSE
huffman@35494
   573
                    EVERY [etac @{thm admD [OF _ ch2ch_Rep_CFunL]} 1,
huffman@35494
   574
                           resolve_tac axs_chain_take 1,
huffman@35494
   575
                           asm_simp_tac HOL_basic_ss 1])
huffman@35494
   576
            ]
huffman@35494
   577
          end;
wenzelm@23152
   578
        val ind = (pg'' thy [] goal tacf
wenzelm@23152
   579
          handle ERROR _ =>
huffman@35494
   580
            (warning "Cannot prove infinite induction rule"; TrueI)
huffman@35494
   581
                  );
huffman@31232
   582
      in (finites, ind) end
huffman@31232
   583
  )
huffman@31232
   584
      handle THM _ =>
huffman@31232
   585
             (warning "Induction proofs failed (THM raised)."; ([], TrueI))
huffman@31232
   586
           | ERROR _ =>
huffman@33810
   587
             (warning "Cannot prove induction rule"; ([], TrueI));
huffman@31232
   588
wenzelm@23152
   589
end; (* local *)
wenzelm@23152
   590
wenzelm@23152
   591
(* ----- theorem concerning coinduction ------------------------------------- *)
wenzelm@23152
   592
wenzelm@23152
   593
local
wenzelm@23152
   594
  val xs = mapn (fn n => K (x_name n)) 1 dnames;
wenzelm@23152
   595
  fun bnd_arg n i = Bound(2*(n_eqs - n)-i-1);
huffman@35497
   596
  val take_ss = HOL_ss addsimps (@{thm Rep_CFun_strict1} :: take_rews);
wenzelm@23152
   597
  val sproj = prj (fn s => K("fst("^s^")")) (fn s => K("snd("^s^")"));
huffman@29402
   598
  val _ = trace " Proving coind_lemma...";
wenzelm@23152
   599
  val coind_lemma =
wenzelm@23152
   600
    let
wenzelm@23152
   601
      fun mk_prj n _ = proj (%:"R") eqs n $ bnd_arg n 0 $ bnd_arg n 1;
wenzelm@23152
   602
      fun mk_eqn n dn =
wenzelm@23152
   603
        (dc_take dn $ %:"n" ` bnd_arg n 0) ===
wenzelm@23152
   604
        (dc_take dn $ %:"n" ` bnd_arg n 1);
wenzelm@23152
   605
      fun mk_all2 (x,t) = mk_all (x, mk_all (x^"'", t));
wenzelm@23152
   606
      val goal =
wenzelm@23152
   607
        mk_trp (mk_imp (%%:(comp_dname^"_bisim") $ %:"R",
wenzelm@23152
   608
          Library.foldr mk_all2 (xs,
wenzelm@23152
   609
            Library.foldr mk_imp (mapn mk_prj 0 dnames,
wenzelm@23152
   610
              foldr1 mk_conj (mapn mk_eqn 0 dnames)))));
wenzelm@27208
   611
      fun x_tacs ctxt n x = [
wenzelm@23152
   612
        rotate_tac (n+1) 1,
wenzelm@23152
   613
        etac all2E 1,
wenzelm@27239
   614
        eres_inst_tac ctxt [(("P", 1), sproj "R" eqs n^" "^x^" "^x^"'")] (mp RS disjE) 1,
wenzelm@23152
   615
        TRY (safe_tac HOL_cs),
wenzelm@23152
   616
        REPEAT (CHANGED (asm_simp_tac take_ss 1))];
wenzelm@27208
   617
      fun tacs ctxt = [
wenzelm@23152
   618
        rtac impI 1,
wenzelm@27208
   619
        InductTacs.induct_tac ctxt [[SOME "n"]] 1,
wenzelm@23152
   620
        simp_tac take_ss 1,
wenzelm@23152
   621
        safe_tac HOL_cs] @
wenzelm@27208
   622
        flat (mapn (x_tacs ctxt) 0 xs);
wenzelm@23152
   623
    in pg [ax_bisim_def] goal tacs end;
wenzelm@23152
   624
in
huffman@29402
   625
  val _ = trace " Proving coind...";
wenzelm@23152
   626
  val coind = 
wenzelm@23152
   627
    let
wenzelm@23152
   628
      fun mk_prj n x = mk_trp (proj (%:"R") eqs n $ %:x $ %:(x^"'"));
wenzelm@23152
   629
      fun mk_eqn x = %:x === %:(x^"'");
wenzelm@23152
   630
      val goal =
wenzelm@23152
   631
        mk_trp (%%:(comp_dname^"_bisim") $ %:"R") ===>
wenzelm@23152
   632
          Logic.list_implies (mapn mk_prj 0 xs,
wenzelm@23152
   633
            mk_trp (foldr1 mk_conj (map mk_eqn xs)));
wenzelm@23152
   634
      val tacs =
wenzelm@23152
   635
        TRY (safe_tac HOL_cs) ::
wenzelm@26336
   636
        maps (fn take_lemma => [
wenzelm@23152
   637
          rtac take_lemma 1,
wenzelm@23152
   638
          cut_facts_tac [coind_lemma] 1,
wenzelm@23152
   639
          fast_tac HOL_cs 1])
wenzelm@26336
   640
        take_lemmas;
wenzelm@27208
   641
    in pg [] goal (K tacs) end;
wenzelm@23152
   642
end; (* local *)
wenzelm@23152
   643
wenzelm@32172
   644
val inducts = Project_Rule.projections (ProofContext.init thy) ind;
huffman@30829
   645
fun ind_rule (dname, rule) = ((Binding.empty, [rule]), [Induct.induct_type dname]);
huffman@31232
   646
val induct_failed = (Thm.prop_of ind = Thm.prop_of TrueI);
huffman@30829
   647
wenzelm@24712
   648
in thy |> Sign.add_path comp_dnam
huffman@31004
   649
       |> snd o PureThy.add_thmss [
huffman@31004
   650
           ((Binding.name "take_lemmas", take_lemmas ), []),
huffman@35494
   651
           ((Binding.name "reach"      , axs_reach   ), []),
huffman@31004
   652
           ((Binding.name "finites"    , finites     ), []),
huffman@31004
   653
           ((Binding.name "finite_ind" , [finite_ind]), []),
huffman@35497
   654
           ((Binding.name "ind"        , [ind]       ), []),
huffman@35497
   655
           ((Binding.name "coind"      , [coind]     ), [])]
huffman@31232
   656
       |> (if induct_failed then I
huffman@31232
   657
           else snd o PureThy.add_thmss (map ind_rule (dnames ~~ inducts)))
haftmann@28536
   658
       |> Sign.parent_path |> pair take_rews
wenzelm@23152
   659
end; (* let *)
wenzelm@23152
   660
end; (* struct *)