src/HOLCF/Tools/Domain/domain_take_proofs.ML
author huffman
Mon Mar 08 11:48:29 2010 -0800 (2010-03-08)
changeset 35656 b62731352812
parent 35655 e8e4af6da819
child 35659 a78bc1930a7a
permissions -rw-r--r--
add type take_induct_info
huffman@35514
     1
(*  Title:      HOLCF/Tools/domain/domain_take_proofs.ML
huffman@35514
     2
    Author:     Brian Huffman
huffman@35514
     3
huffman@35514
     4
Defines take functions for the given domain equation
huffman@35514
     5
and proves related theorems.
huffman@35514
     6
*)
huffman@35514
     7
huffman@35514
     8
signature DOMAIN_TAKE_PROOFS =
huffman@35514
     9
sig
huffman@35514
    10
  type iso_info =
huffman@35514
    11
    {
huffman@35514
    12
      absT : typ,
huffman@35514
    13
      repT : typ,
huffman@35514
    14
      abs_const : term,
huffman@35514
    15
      rep_const : term,
huffman@35514
    16
      abs_inverse : thm,
huffman@35514
    17
      rep_inverse : thm
huffman@35514
    18
    }
huffman@35651
    19
  type take_info =
huffman@35514
    20
    { take_consts : term list,
huffman@35514
    21
      take_defs : thm list,
huffman@35514
    22
      chain_take_thms : thm list,
huffman@35514
    23
      take_0_thms : thm list,
huffman@35514
    24
      take_Suc_thms : thm list,
huffman@35515
    25
      deflation_take_thms : thm list,
huffman@35515
    26
      finite_consts : term list,
huffman@35515
    27
      finite_defs : thm list
huffman@35651
    28
    }
huffman@35656
    29
  type take_induct_info =
huffman@35656
    30
    {
huffman@35656
    31
      reach_thms : thm list,
huffman@35656
    32
      take_lemma_thms : thm list,
huffman@35656
    33
      is_finite : bool,
huffman@35656
    34
      take_induct_thms : thm list
huffman@35656
    35
    }
huffman@35651
    36
  val define_take_functions :
huffman@35651
    37
    (binding * iso_info) list -> theory -> take_info * theory
huffman@35514
    38
huffman@35654
    39
  val add_lub_take_theorems :
huffman@35654
    40
    (binding * iso_info) list -> take_info -> thm list ->
huffman@35656
    41
    theory -> take_induct_info * theory
huffman@35654
    42
huffman@35514
    43
  val map_of_typ :
huffman@35514
    44
    theory -> (typ * term) list -> typ -> term
huffman@35514
    45
huffman@35514
    46
  val add_map_function :
huffman@35514
    47
    (string * string * thm) -> theory -> theory
huffman@35514
    48
huffman@35514
    49
  val get_map_tab : theory -> string Symtab.table
huffman@35514
    50
  val get_deflation_thms : theory -> thm list
huffman@35514
    51
end;
huffman@35514
    52
huffman@35514
    53
structure Domain_Take_Proofs : DOMAIN_TAKE_PROOFS =
huffman@35514
    54
struct
huffman@35514
    55
huffman@35514
    56
type iso_info =
huffman@35514
    57
  {
huffman@35514
    58
    absT : typ,
huffman@35514
    59
    repT : typ,
huffman@35514
    60
    abs_const : term,
huffman@35514
    61
    rep_const : term,
huffman@35514
    62
    abs_inverse : thm,
huffman@35514
    63
    rep_inverse : thm
huffman@35514
    64
  };
huffman@35514
    65
huffman@35651
    66
type take_info =
huffman@35651
    67
  { take_consts : term list,
huffman@35651
    68
    take_defs : thm list,
huffman@35651
    69
    chain_take_thms : thm list,
huffman@35651
    70
    take_0_thms : thm list,
huffman@35651
    71
    take_Suc_thms : thm list,
huffman@35651
    72
    deflation_take_thms : thm list,
huffman@35651
    73
    finite_consts : term list,
huffman@35651
    74
    finite_defs : thm list
huffman@35651
    75
  };
huffman@35651
    76
huffman@35656
    77
type take_induct_info =
huffman@35656
    78
  {
huffman@35656
    79
    reach_thms : thm list,
huffman@35656
    80
    take_lemma_thms : thm list,
huffman@35656
    81
    is_finite : bool,
huffman@35656
    82
    take_induct_thms : thm list
huffman@35656
    83
  };
huffman@35656
    84
huffman@35514
    85
val beta_ss =
huffman@35514
    86
  HOL_basic_ss
huffman@35514
    87
    addsimps simp_thms
huffman@35514
    88
    addsimps [@{thm beta_cfun}]
huffman@35514
    89
    addsimprocs [@{simproc cont_proc}];
huffman@35514
    90
huffman@35514
    91
val beta_tac = simp_tac beta_ss;
huffman@35514
    92
huffman@35514
    93
(******************************************************************************)
huffman@35514
    94
(******************************** theory data *********************************)
huffman@35514
    95
(******************************************************************************)
huffman@35514
    96
huffman@35514
    97
structure MapData = Theory_Data
huffman@35514
    98
(
huffman@35514
    99
  (* constant names like "foo_map" *)
huffman@35514
   100
  type T = string Symtab.table;
huffman@35514
   101
  val empty = Symtab.empty;
huffman@35514
   102
  val extend = I;
huffman@35514
   103
  fun merge data = Symtab.merge (K true) data;
huffman@35514
   104
);
huffman@35514
   105
huffman@35514
   106
structure DeflMapData = Theory_Data
huffman@35514
   107
(
huffman@35514
   108
  (* theorems like "deflation a ==> deflation (foo_map$a)" *)
huffman@35514
   109
  type T = thm list;
huffman@35514
   110
  val empty = [];
huffman@35514
   111
  val extend = I;
huffman@35514
   112
  val merge = Thm.merge_thms;
huffman@35514
   113
);
huffman@35514
   114
huffman@35514
   115
fun add_map_function (tname, map_name, deflation_map_thm) =
huffman@35514
   116
    MapData.map (Symtab.insert (K true) (tname, map_name))
huffman@35514
   117
    #> DeflMapData.map (Thm.add_thm deflation_map_thm);
huffman@35514
   118
huffman@35514
   119
val get_map_tab = MapData.get;
huffman@35514
   120
val get_deflation_thms = DeflMapData.get;
huffman@35514
   121
huffman@35514
   122
(******************************************************************************)
huffman@35514
   123
(************************** building types and terms **************************)
huffman@35514
   124
(******************************************************************************)
huffman@35514
   125
huffman@35514
   126
open HOLCF_Library;
huffman@35514
   127
huffman@35514
   128
infixr 6 ->>;
huffman@35514
   129
infix -->>;
huffman@35515
   130
infix 9 `;
huffman@35514
   131
huffman@35514
   132
fun mapT (T as Type (_, Ts)) =
huffman@35514
   133
    (map (fn T => T ->> T) Ts) -->> (T ->> T)
huffman@35514
   134
  | mapT T = T ->> T;
huffman@35514
   135
huffman@35514
   136
fun mk_deflation t =
huffman@35514
   137
  Const (@{const_name deflation}, Term.fastype_of t --> boolT) $ t;
huffman@35514
   138
huffman@35514
   139
fun mk_eqs (t, u) = HOLogic.mk_Trueprop (HOLogic.mk_eq (t, u));
huffman@35514
   140
huffman@35514
   141
(******************************************************************************)
huffman@35514
   142
(****************************** isomorphism info ******************************)
huffman@35514
   143
(******************************************************************************)
huffman@35514
   144
huffman@35514
   145
fun deflation_abs_rep (info : iso_info) : thm =
huffman@35514
   146
  let
huffman@35514
   147
    val abs_iso = #abs_inverse info;
huffman@35514
   148
    val rep_iso = #rep_inverse info;
huffman@35514
   149
    val thm = @{thm deflation_abs_rep} OF [abs_iso, rep_iso];
huffman@35514
   150
  in
huffman@35514
   151
    Drule.export_without_context thm
huffman@35514
   152
  end
huffman@35514
   153
huffman@35514
   154
(******************************************************************************)
huffman@35514
   155
(********************* building map functions over types **********************)
huffman@35514
   156
(******************************************************************************)
huffman@35514
   157
huffman@35514
   158
fun map_of_typ (thy : theory) (sub : (typ * term) list) (T : typ) : term =
huffman@35514
   159
  let
huffman@35514
   160
    val map_tab = get_map_tab thy;
huffman@35514
   161
    fun auto T = T ->> T;
huffman@35514
   162
    fun map_of T =
huffman@35514
   163
        case AList.lookup (op =) sub T of
huffman@35514
   164
          SOME m => (m, true) | NONE => map_of' T
huffman@35514
   165
    and map_of' (T as (Type (c, Ts))) =
huffman@35514
   166
        (case Symtab.lookup map_tab c of
huffman@35514
   167
          SOME map_name =>
huffman@35514
   168
          let
huffman@35514
   169
            val map_type = map auto Ts -->> auto T;
huffman@35514
   170
            val (ms, bs) = map_split map_of Ts;
huffman@35514
   171
          in
huffman@35514
   172
            if exists I bs
huffman@35514
   173
            then (list_ccomb (Const (map_name, map_type), ms), true)
huffman@35514
   174
            else (mk_ID T, false)
huffman@35514
   175
          end
huffman@35514
   176
        | NONE => (mk_ID T, false))
huffman@35514
   177
      | map_of' T = (mk_ID T, false);
huffman@35514
   178
  in
huffman@35514
   179
    fst (map_of T)
huffman@35514
   180
  end;
huffman@35514
   181
huffman@35514
   182
huffman@35514
   183
(******************************************************************************)
huffman@35514
   184
(********************* declaring definitions and theorems *********************)
huffman@35514
   185
(******************************************************************************)
huffman@35514
   186
huffman@35514
   187
fun define_const
huffman@35514
   188
    (bind : binding, rhs : term)
huffman@35514
   189
    (thy : theory)
huffman@35514
   190
    : (term * thm) * theory =
huffman@35514
   191
  let
huffman@35514
   192
    val typ = Term.fastype_of rhs;
huffman@35514
   193
    val (const, thy) = Sign.declare_const ((bind, typ), NoSyn) thy;
huffman@35514
   194
    val eqn = Logic.mk_equals (const, rhs);
huffman@35514
   195
    val def = Thm.no_attributes (Binding.suffix_name "_def" bind, eqn);
huffman@35514
   196
    val (def_thm, thy) = yield_singleton (PureThy.add_defs false) def thy;
huffman@35514
   197
  in
huffman@35514
   198
    ((const, def_thm), thy)
huffman@35514
   199
  end;
huffman@35514
   200
huffman@35650
   201
fun add_qualified_def name (path, eqn) thy =
huffman@35650
   202
    thy
huffman@35650
   203
    |> Sign.add_path path
huffman@35650
   204
    |> yield_singleton (PureThy.add_defs false)
huffman@35650
   205
        (Thm.no_attributes (Binding.name name, eqn))
huffman@35650
   206
    ||> Sign.parent_path;
huffman@35650
   207
huffman@35514
   208
fun add_qualified_thm name (path, thm) thy =
huffman@35514
   209
    thy
huffman@35514
   210
    |> Sign.add_path path
huffman@35514
   211
    |> yield_singleton PureThy.add_thms
huffman@35514
   212
        (Thm.no_attributes (Binding.name name, thm))
huffman@35514
   213
    ||> Sign.parent_path;
huffman@35514
   214
huffman@35573
   215
fun add_qualified_simp_thm name (path, thm) thy =
huffman@35573
   216
    thy
huffman@35573
   217
    |> Sign.add_path path
huffman@35573
   218
    |> yield_singleton PureThy.add_thms
huffman@35573
   219
        ((Binding.name name, thm), [Simplifier.simp_add])
huffman@35573
   220
    ||> Sign.parent_path;
huffman@35573
   221
huffman@35514
   222
(******************************************************************************)
huffman@35514
   223
(************************** defining take functions ***************************)
huffman@35514
   224
(******************************************************************************)
huffman@35514
   225
huffman@35514
   226
fun define_take_functions
huffman@35514
   227
    (spec : (binding * iso_info) list)
huffman@35514
   228
    (thy : theory) =
huffman@35514
   229
  let
huffman@35514
   230
huffman@35514
   231
    (* retrieve components of spec *)
huffman@35514
   232
    val dom_binds = map fst spec;
huffman@35514
   233
    val iso_infos = map snd spec;
huffman@35514
   234
    val dom_eqns = map (fn x => (#absT x, #repT x)) iso_infos;
huffman@35514
   235
    val rep_abs_consts = map (fn x => (#rep_const x, #abs_const x)) iso_infos;
huffman@35514
   236
    val dnames = map Binding.name_of dom_binds;
huffman@35514
   237
huffman@35514
   238
    (* get table of map functions *)
huffman@35514
   239
    val map_tab = MapData.get thy;
huffman@35514
   240
huffman@35514
   241
    fun mk_projs []      t = []
huffman@35514
   242
      | mk_projs (x::[]) t = [(x, t)]
huffman@35514
   243
      | mk_projs (x::xs) t = (x, mk_fst t) :: mk_projs xs (mk_snd t);
huffman@35514
   244
huffman@35514
   245
    fun mk_cfcomp2 ((rep_const, abs_const), f) =
huffman@35514
   246
        mk_cfcomp (abs_const, mk_cfcomp (f, rep_const));
huffman@35514
   247
huffman@35514
   248
    (* define take functional *)
huffman@35514
   249
    val newTs : typ list = map fst dom_eqns;
huffman@35514
   250
    val copy_arg_type = mk_tupleT (map (fn T => T ->> T) newTs);
huffman@35514
   251
    val copy_arg = Free ("f", copy_arg_type);
huffman@35514
   252
    val copy_args = map snd (mk_projs dom_binds copy_arg);
huffman@35514
   253
    fun one_copy_rhs (rep_abs, (lhsT, rhsT)) =
huffman@35514
   254
      let
huffman@35514
   255
        val body = map_of_typ thy (newTs ~~ copy_args) rhsT;
huffman@35514
   256
      in
huffman@35514
   257
        mk_cfcomp2 (rep_abs, body)
huffman@35514
   258
      end;
huffman@35514
   259
    val take_functional =
huffman@35514
   260
        big_lambda copy_arg
huffman@35514
   261
          (mk_tuple (map one_copy_rhs (rep_abs_consts ~~ dom_eqns)));
huffman@35514
   262
    val take_rhss =
huffman@35514
   263
      let
huffman@35557
   264
        val n = Free ("n", HOLogic.natT);
huffman@35557
   265
        val rhs = mk_iterate (n, take_functional);
huffman@35514
   266
      in
huffman@35557
   267
        map (lambda n o snd) (mk_projs dom_binds rhs)
huffman@35514
   268
      end;
huffman@35514
   269
huffman@35514
   270
    (* define take constants *)
huffman@35514
   271
    fun define_take_const ((tbind, take_rhs), (lhsT, rhsT)) thy =
huffman@35514
   272
      let
huffman@35514
   273
        val take_type = HOLogic.natT --> lhsT ->> lhsT;
huffman@35514
   274
        val take_bind = Binding.suffix_name "_take" tbind;
huffman@35514
   275
        val (take_const, thy) =
huffman@35514
   276
          Sign.declare_const ((take_bind, take_type), NoSyn) thy;
huffman@35514
   277
        val take_eqn = Logic.mk_equals (take_const, take_rhs);
huffman@35514
   278
        val (take_def_thm, thy) =
huffman@35650
   279
            add_qualified_def "take_def"
huffman@35650
   280
             (Binding.name_of tbind, take_eqn) thy;
huffman@35514
   281
      in ((take_const, take_def_thm), thy) end;
huffman@35514
   282
    val ((take_consts, take_defs), thy) = thy
huffman@35514
   283
      |> fold_map define_take_const (dom_binds ~~ take_rhss ~~ dom_eqns)
huffman@35514
   284
      |>> ListPair.unzip;
huffman@35514
   285
huffman@35514
   286
    (* prove chain_take lemmas *)
huffman@35514
   287
    fun prove_chain_take (take_const, dname) thy =
huffman@35514
   288
      let
huffman@35514
   289
        val goal = mk_trp (mk_chain take_const);
huffman@35514
   290
        val rules = take_defs @ @{thms chain_iterate ch2ch_fst ch2ch_snd};
huffman@35514
   291
        val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
huffman@35572
   292
        val thm = Goal.prove_global thy [] [] goal (K tac);
huffman@35514
   293
      in
huffman@35573
   294
        add_qualified_simp_thm "chain_take" (dname, thm) thy
huffman@35514
   295
      end;
huffman@35514
   296
    val (chain_take_thms, thy) =
huffman@35514
   297
      fold_map prove_chain_take (take_consts ~~ dnames) thy;
huffman@35514
   298
huffman@35514
   299
    (* prove take_0 lemmas *)
huffman@35514
   300
    fun prove_take_0 ((take_const, dname), (lhsT, rhsT)) thy =
huffman@35514
   301
      let
huffman@35514
   302
        val lhs = take_const $ @{term "0::nat"};
huffman@35514
   303
        val goal = mk_eqs (lhs, mk_bottom (lhsT ->> lhsT));
huffman@35514
   304
        val rules = take_defs @ @{thms iterate_0 fst_strict snd_strict};
huffman@35514
   305
        val tac = simp_tac (HOL_basic_ss addsimps rules) 1;
huffman@35514
   306
        val take_0_thm = Goal.prove_global thy [] [] goal (K tac);
huffman@35514
   307
      in
huffman@35514
   308
        add_qualified_thm "take_0" (dname, take_0_thm) thy
huffman@35514
   309
      end;
huffman@35514
   310
    val (take_0_thms, thy) =
huffman@35514
   311
      fold_map prove_take_0 (take_consts ~~ dnames ~~ dom_eqns) thy;
huffman@35514
   312
huffman@35514
   313
    (* prove take_Suc lemmas *)
huffman@35557
   314
    val n = Free ("n", natT);
huffman@35557
   315
    val take_is = map (fn t => t $ n) take_consts;
huffman@35514
   316
    fun prove_take_Suc
huffman@35514
   317
          (((take_const, rep_abs), dname), (lhsT, rhsT)) thy =
huffman@35514
   318
      let
huffman@35557
   319
        val lhs = take_const $ (@{term Suc} $ n);
huffman@35514
   320
        val body = map_of_typ thy (newTs ~~ take_is) rhsT;
huffman@35514
   321
        val rhs = mk_cfcomp2 (rep_abs, body);
huffman@35514
   322
        val goal = mk_eqs (lhs, rhs);
huffman@35514
   323
        val simps = @{thms iterate_Suc fst_conv snd_conv}
huffman@35514
   324
        val rules = take_defs @ simps;
huffman@35514
   325
        val tac = simp_tac (beta_ss addsimps rules) 1;
huffman@35514
   326
        val take_Suc_thm = Goal.prove_global thy [] [] goal (K tac);
huffman@35514
   327
      in
huffman@35514
   328
        add_qualified_thm "take_Suc" (dname, take_Suc_thm) thy
huffman@35514
   329
      end;
huffman@35514
   330
    val (take_Suc_thms, thy) =
huffman@35514
   331
      fold_map prove_take_Suc
huffman@35514
   332
        (take_consts ~~ rep_abs_consts ~~ dnames ~~ dom_eqns) thy;
huffman@35514
   333
huffman@35514
   334
    (* prove deflation theorems for take functions *)
huffman@35514
   335
    val deflation_abs_rep_thms = map deflation_abs_rep iso_infos;
huffman@35514
   336
    val deflation_take_thm =
huffman@35514
   337
      let
huffman@35557
   338
        val n = Free ("n", natT);
huffman@35557
   339
        fun mk_goal take_const = mk_deflation (take_const $ n);
huffman@35514
   340
        val goal = mk_trp (foldr1 mk_conj (map mk_goal take_consts));
huffman@35514
   341
        val adm_rules =
huffman@35514
   342
          @{thms adm_conj adm_subst [OF _ adm_deflation]
huffman@35514
   343
                 cont2cont_fst cont2cont_snd cont_id};
huffman@35514
   344
        val bottom_rules =
huffman@35514
   345
          take_0_thms @ @{thms deflation_UU simp_thms};
huffman@35514
   346
        val deflation_rules =
huffman@35514
   347
          @{thms conjI deflation_ID}
huffman@35514
   348
          @ deflation_abs_rep_thms
huffman@35514
   349
          @ DeflMapData.get thy;
huffman@35514
   350
      in
huffman@35514
   351
        Goal.prove_global thy [] [] goal (fn _ =>
huffman@35514
   352
         EVERY
huffman@35514
   353
          [rtac @{thm nat.induct} 1,
huffman@35514
   354
           simp_tac (HOL_basic_ss addsimps bottom_rules) 1,
huffman@35514
   355
           asm_simp_tac (HOL_basic_ss addsimps take_Suc_thms) 1,
huffman@35514
   356
           REPEAT (etac @{thm conjE} 1
huffman@35514
   357
                   ORELSE resolve_tac deflation_rules 1
huffman@35514
   358
                   ORELSE atac 1)])
huffman@35514
   359
      end;
huffman@35514
   360
    fun conjuncts [] thm = []
huffman@35514
   361
      | conjuncts (n::[]) thm = [(n, thm)]
huffman@35514
   362
      | conjuncts (n::ns) thm = let
huffman@35514
   363
          val thmL = thm RS @{thm conjunct1};
huffman@35514
   364
          val thmR = thm RS @{thm conjunct2};
huffman@35514
   365
        in (n, thmL):: conjuncts ns thmR end;
huffman@35514
   366
    val (deflation_take_thms, thy) =
huffman@35514
   367
      fold_map (add_qualified_thm "deflation_take")
huffman@35514
   368
        (map (apsnd Drule.export_without_context)
huffman@35514
   369
          (conjuncts dnames deflation_take_thm)) thy;
huffman@35514
   370
huffman@35514
   371
    (* prove strictness of take functions *)
huffman@35572
   372
    fun prove_take_strict (deflation_take, dname) thy =
huffman@35514
   373
      let
huffman@35572
   374
        val take_strict_thm =
huffman@35572
   375
            Drule.export_without_context
huffman@35572
   376
            (@{thm deflation_strict} OF [deflation_take]);
huffman@35514
   377
      in
huffman@35514
   378
        add_qualified_thm "take_strict" (dname, take_strict_thm) thy
huffman@35514
   379
      end;
huffman@35514
   380
    val (take_strict_thms, thy) =
huffman@35572
   381
      fold_map prove_take_strict
huffman@35572
   382
        (deflation_take_thms ~~ dnames) thy;
huffman@35514
   383
huffman@35514
   384
    (* prove take/take rules *)
huffman@35514
   385
    fun prove_take_take ((chain_take, deflation_take), dname) thy =
huffman@35514
   386
      let
huffman@35514
   387
        val take_take_thm =
huffman@35557
   388
            Drule.export_without_context
huffman@35557
   389
            (@{thm deflation_chain_min} OF [chain_take, deflation_take]);
huffman@35514
   390
      in
huffman@35514
   391
        add_qualified_thm "take_take" (dname, take_take_thm) thy
huffman@35514
   392
      end;
huffman@35514
   393
    val (take_take_thms, thy) =
huffman@35514
   394
      fold_map prove_take_take
huffman@35514
   395
        (chain_take_thms ~~ deflation_take_thms ~~ dnames) thy;
huffman@35514
   396
huffman@35572
   397
    (* prove take_below rules *)
huffman@35572
   398
    fun prove_take_below (deflation_take, dname) thy =
huffman@35572
   399
      let
huffman@35572
   400
        val take_below_thm =
huffman@35572
   401
            Drule.export_without_context
huffman@35572
   402
            (@{thm deflation.below} OF [deflation_take]);
huffman@35572
   403
      in
huffman@35572
   404
        add_qualified_thm "take_below" (dname, take_below_thm) thy
huffman@35572
   405
      end;
huffman@35572
   406
    val (take_below_thms, thy) =
huffman@35572
   407
      fold_map prove_take_below
huffman@35572
   408
        (deflation_take_thms ~~ dnames) thy;
huffman@35572
   409
huffman@35515
   410
    (* define finiteness predicates *)
huffman@35515
   411
    fun define_finite_const ((tbind, take_const), (lhsT, rhsT)) thy =
huffman@35515
   412
      let
huffman@35515
   413
        val finite_type = lhsT --> boolT;
huffman@35515
   414
        val finite_bind = Binding.suffix_name "_finite" tbind;
huffman@35515
   415
        val (finite_const, thy) =
huffman@35515
   416
          Sign.declare_const ((finite_bind, finite_type), NoSyn) thy;
huffman@35515
   417
        val x = Free ("x", lhsT);
huffman@35557
   418
        val n = Free ("n", natT);
huffman@35515
   419
        val finite_rhs =
huffman@35515
   420
          lambda x (HOLogic.exists_const natT $
huffman@35557
   421
            (lambda n (mk_eq (mk_capply (take_const $ n, x), x))));
huffman@35515
   422
        val finite_eqn = Logic.mk_equals (finite_const, finite_rhs);
huffman@35515
   423
        val (finite_def_thm, thy) =
huffman@35650
   424
            add_qualified_def "finite_def"
huffman@35650
   425
             (Binding.name_of tbind, finite_eqn) thy;
huffman@35515
   426
      in ((finite_const, finite_def_thm), thy) end;
huffman@35515
   427
    val ((finite_consts, finite_defs), thy) = thy
huffman@35515
   428
      |> fold_map define_finite_const (dom_binds ~~ take_consts ~~ dom_eqns)
huffman@35515
   429
      |>> ListPair.unzip;
huffman@35515
   430
huffman@35514
   431
    val result =
huffman@35514
   432
      {
huffman@35514
   433
        take_consts = take_consts,
huffman@35514
   434
        take_defs = take_defs,
huffman@35514
   435
        chain_take_thms = chain_take_thms,
huffman@35514
   436
        take_0_thms = take_0_thms,
huffman@35514
   437
        take_Suc_thms = take_Suc_thms,
huffman@35515
   438
        deflation_take_thms = deflation_take_thms,
huffman@35515
   439
        finite_consts = finite_consts,
huffman@35515
   440
        finite_defs = finite_defs
huffman@35514
   441
      };
huffman@35514
   442
huffman@35514
   443
  in
huffman@35514
   444
    (result, thy)
huffman@35514
   445
  end;
huffman@35514
   446
huffman@35655
   447
fun prove_finite_take_induct
huffman@35655
   448
    (spec : (binding * iso_info) list)
huffman@35655
   449
    (take_info : take_info)
huffman@35655
   450
    (lub_take_thms : thm list)
huffman@35655
   451
    (thy : theory) =
huffman@35655
   452
  let
huffman@35655
   453
    val dom_binds = map fst spec;
huffman@35655
   454
    val iso_infos = map snd spec;
huffman@35655
   455
    val absTs = map #absT iso_infos;
huffman@35655
   456
    val dnames = map Binding.name_of dom_binds;
huffman@35655
   457
    val {take_consts, ...} = take_info;
huffman@35655
   458
    val {chain_take_thms, take_0_thms, take_Suc_thms, ...} = take_info;
huffman@35655
   459
    val {finite_consts, finite_defs, ...} = take_info;
huffman@35655
   460
huffman@35655
   461
    val decisive_lemma =
huffman@35655
   462
      let
huffman@35655
   463
        fun iso_locale info =
huffman@35655
   464
            @{thm iso.intro} OF [#abs_inverse info, #rep_inverse info];
huffman@35655
   465
        val iso_locale_thms = map iso_locale iso_infos;
huffman@35655
   466
        val decisive_abs_rep_thms =
huffman@35655
   467
            map (fn x => @{thm decisive_abs_rep} OF [x]) iso_locale_thms;
huffman@35655
   468
        val n = Free ("n", @{typ nat});
huffman@35655
   469
        fun mk_decisive t =
huffman@35655
   470
            Const (@{const_name decisive}, fastype_of t --> boolT) $ t;
huffman@35655
   471
        fun f take_const = mk_decisive (take_const $ n);
huffman@35655
   472
        val goal = mk_trp (foldr1 mk_conj (map f take_consts));
huffman@35655
   473
        val rules0 = @{thm decisive_bottom} :: take_0_thms;
huffman@35655
   474
        val rules1 =
huffman@35655
   475
            take_Suc_thms @ decisive_abs_rep_thms
huffman@35655
   476
            @ @{thms decisive_ID decisive_ssum_map decisive_sprod_map};
huffman@35655
   477
        val tac = EVERY [
huffman@35655
   478
            rtac @{thm nat.induct} 1,
huffman@35655
   479
            simp_tac (HOL_ss addsimps rules0) 1,
huffman@35655
   480
            asm_simp_tac (HOL_ss addsimps rules1) 1];
huffman@35655
   481
      in Goal.prove_global thy [] [] goal (K tac) end;
huffman@35655
   482
    fun conjuncts 1 thm = [thm]
huffman@35655
   483
      | conjuncts n thm = let
huffman@35655
   484
          val thmL = thm RS @{thm conjunct1};
huffman@35655
   485
          val thmR = thm RS @{thm conjunct2};
huffman@35655
   486
        in thmL :: conjuncts (n-1) thmR end;
huffman@35655
   487
    val decisive_thms = conjuncts (length spec) decisive_lemma;
huffman@35655
   488
huffman@35655
   489
    fun prove_finite_thm (absT, finite_const) =
huffman@35655
   490
      let
huffman@35655
   491
        val goal = mk_trp (finite_const $ Free ("x", absT));
huffman@35655
   492
        val tac =
huffman@35655
   493
            EVERY [
huffman@35655
   494
            rewrite_goals_tac finite_defs,
huffman@35655
   495
            rtac @{thm lub_ID_finite} 1,
huffman@35655
   496
            resolve_tac chain_take_thms 1,
huffman@35655
   497
            resolve_tac lub_take_thms 1,
huffman@35655
   498
            resolve_tac decisive_thms 1];
huffman@35655
   499
      in
huffman@35655
   500
        Goal.prove_global thy [] [] goal (K tac)
huffman@35655
   501
      end;
huffman@35655
   502
    val finite_thms =
huffman@35655
   503
        map prove_finite_thm (absTs ~~ finite_consts);
huffman@35655
   504
huffman@35655
   505
    fun prove_take_induct ((ch_take, lub_take), decisive) =
huffman@35655
   506
        Drule.export_without_context
huffman@35655
   507
          (@{thm lub_ID_finite_take_induct} OF [ch_take, lub_take, decisive]);
huffman@35655
   508
    val take_induct_thms =
huffman@35655
   509
        map prove_take_induct
huffman@35655
   510
          (chain_take_thms ~~ lub_take_thms ~~ decisive_thms);
huffman@35655
   511
huffman@35655
   512
    val thy = thy
huffman@35655
   513
        |> fold (snd oo add_qualified_thm "finite")
huffman@35655
   514
            (dnames ~~ finite_thms)
huffman@35655
   515
        |> fold (snd oo add_qualified_thm "take_induct")
huffman@35655
   516
            (dnames ~~ take_induct_thms);
huffman@35655
   517
  in
huffman@35655
   518
    ((finite_thms, take_induct_thms), thy)
huffman@35655
   519
  end;
huffman@35655
   520
huffman@35654
   521
fun add_lub_take_theorems
huffman@35654
   522
    (spec : (binding * iso_info) list)
huffman@35654
   523
    (take_info : take_info)
huffman@35654
   524
    (lub_take_thms : thm list)
huffman@35654
   525
    (thy : theory) =
huffman@35654
   526
  let
huffman@35654
   527
huffman@35654
   528
    (* retrieve components of spec *)
huffman@35654
   529
    val dom_binds = map fst spec;
huffman@35654
   530
    val iso_infos = map snd spec;
huffman@35655
   531
    val absTs = map #absT iso_infos;
huffman@35655
   532
    val repTs = map #repT iso_infos;
huffman@35654
   533
    val dnames = map Binding.name_of dom_binds;
huffman@35655
   534
    val {take_consts, take_0_thms, take_Suc_thms, ...} = take_info;
huffman@35654
   535
    val {chain_take_thms, deflation_take_thms, ...} = take_info;
huffman@35654
   536
huffman@35654
   537
    (* prove take lemmas *)
huffman@35654
   538
    fun prove_take_lemma ((chain_take, lub_take), dname) thy =
huffman@35654
   539
      let
huffman@35654
   540
        val take_lemma =
huffman@35654
   541
            Drule.export_without_context
huffman@35654
   542
              (@{thm lub_ID_take_lemma} OF [chain_take, lub_take]);
huffman@35654
   543
      in
huffman@35654
   544
        add_qualified_thm "take_lemma" (dname, take_lemma) thy
huffman@35654
   545
      end;
huffman@35654
   546
    val (take_lemma_thms, thy) =
huffman@35654
   547
      fold_map prove_take_lemma
huffman@35654
   548
        (chain_take_thms ~~ lub_take_thms ~~ dnames) thy;
huffman@35654
   549
huffman@35654
   550
    (* prove reach lemmas *)
huffman@35654
   551
    fun prove_reach_lemma ((chain_take, lub_take), dname) thy =
huffman@35654
   552
      let
huffman@35654
   553
        val thm =
huffman@35654
   554
            Drule.export_without_context
huffman@35654
   555
              (@{thm lub_ID_reach} OF [chain_take, lub_take]);
huffman@35654
   556
      in
huffman@35654
   557
        add_qualified_thm "reach" (dname, thm) thy
huffman@35654
   558
      end;
huffman@35654
   559
    val (reach_thms, thy) =
huffman@35654
   560
      fold_map prove_reach_lemma
huffman@35654
   561
        (chain_take_thms ~~ lub_take_thms ~~ dnames) thy;
huffman@35654
   562
huffman@35655
   563
    (* test for finiteness of domain definitions *)
huffman@35655
   564
    local
huffman@35655
   565
      val types = [@{type_name ssum}, @{type_name sprod}];
huffman@35655
   566
      fun finite d T = if T mem absTs then d else finite' d T
huffman@35655
   567
      and finite' d (Type (c, Ts)) =
huffman@35655
   568
          let val d' = d andalso c mem types;
huffman@35655
   569
          in forall (finite d') Ts end
huffman@35655
   570
        | finite' d _ = true;
huffman@35655
   571
    in
huffman@35655
   572
      val is_finite = forall (finite true) repTs;
huffman@35655
   573
    end;
huffman@35654
   574
huffman@35655
   575
    val ((finite_thms, take_induct_thms), thy) =
huffman@35655
   576
      if is_finite
huffman@35655
   577
      then
huffman@35655
   578
        let
huffman@35655
   579
          val ((finites, take_inducts), thy) =
huffman@35655
   580
              prove_finite_take_induct spec take_info lub_take_thms thy;
huffman@35655
   581
        in
huffman@35655
   582
          ((SOME finites, take_inducts), thy)
huffman@35655
   583
        end
huffman@35655
   584
      else
huffman@35655
   585
        let
huffman@35655
   586
          fun prove_take_induct (chain_take, lub_take) =
huffman@35655
   587
              Drule.export_without_context
huffman@35655
   588
                (@{thm lub_ID_take_induct} OF [chain_take, lub_take]);
huffman@35655
   589
          val take_inducts =
huffman@35655
   590
              map prove_take_induct (chain_take_thms ~~ lub_take_thms);
huffman@35655
   591
          val thy = fold (snd oo add_qualified_thm "take_induct")
huffman@35655
   592
                         (dnames ~~ take_inducts) thy;
huffman@35655
   593
        in
huffman@35655
   594
          ((NONE, take_inducts), thy)
huffman@35655
   595
        end;
huffman@35655
   596
huffman@35656
   597
    val result =
huffman@35656
   598
      {
huffman@35656
   599
        reach_thms = reach_thms,
huffman@35656
   600
        take_lemma_thms = take_lemma_thms,
huffman@35656
   601
        is_finite = is_finite,
huffman@35656
   602
        take_induct_thms = take_induct_thms
huffman@35656
   603
      };
huffman@35654
   604
  in
huffman@35654
   605
    (result, thy)
huffman@35654
   606
  end;
huffman@35654
   607
huffman@35514
   608
end;