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