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