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