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