src/HOL/Tools/specification_package.ML
author haftmann
Fri Dec 16 09:00:11 2005 +0100 (2005-12-16)
changeset 18418 bf448d999b7e
parent 18377 0e1d025d57b3
child 18729 216e31270509
permissions -rw-r--r--
re-arranged tuples (theory * 'a) to ('a * theory) in Pure
skalberg@14115
     1
(*  Title:      HOL/Tools/specification_package.ML
skalberg@14115
     2
    ID:         $Id$
skalberg@14115
     3
    Author:     Sebastian Skalberg, TU Muenchen
skalberg@14115
     4
skalberg@14115
     5
Package for defining constants by specification.
skalberg@14115
     6
*)
skalberg@14115
     7
skalberg@14115
     8
signature SPECIFICATION_PACKAGE =
skalberg@14115
     9
sig
wenzelm@17336
    10
  val quiet_mode: bool ref
wenzelm@17336
    11
  val add_specification_i: string option -> (bstring * xstring * bool) list ->
wenzelm@17336
    12
    theory attribute
skalberg@14115
    13
end
skalberg@14115
    14
wenzelm@17895
    15
structure SpecificationPackage: SPECIFICATION_PACKAGE =
skalberg@14115
    16
struct
skalberg@14115
    17
skalberg@14115
    18
(* messages *)
skalberg@14115
    19
skalberg@14115
    20
val quiet_mode = ref false
skalberg@14115
    21
fun message s = if ! quiet_mode then () else writeln s
skalberg@14115
    22
skalberg@14115
    23
skalberg@14115
    24
(* Actual code *)
skalberg@14115
    25
skalberg@14222
    26
local
wenzelm@17895
    27
    val exE_some = thm "exE_some";
wenzelm@17895
    28
skalberg@14222
    29
    fun mk_definitional [] arg = arg
skalberg@14222
    30
      | mk_definitional ((thname,cname,covld)::cos) (thy,thm) =
wenzelm@17336
    31
        case HOLogic.dest_Trueprop (concl_of thm) of
wenzelm@17336
    32
            Const("Ex",_) $ P =>
wenzelm@17336
    33
            let
wenzelm@17336
    34
                val ctype = domain_type (type_of P)
wenzelm@17336
    35
                val cname_full = Sign.intern_const (sign_of thy) cname
wenzelm@17336
    36
                val cdefname = if thname = ""
wenzelm@17336
    37
                               then Thm.def_name (Sign.base_name cname)
wenzelm@17336
    38
                               else thname
wenzelm@17336
    39
                val def_eq = Logic.mk_equals (Const(cname_full,ctype),
wenzelm@17336
    40
                                              HOLogic.choice_const ctype $  P)
haftmann@18358
    41
                val (thms, thy') = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy
wenzelm@17895
    42
                val thm' = [thm,hd thms] MRS exE_some
wenzelm@17336
    43
            in
wenzelm@17336
    44
                mk_definitional cos (thy',thm')
wenzelm@17336
    45
            end
wenzelm@17336
    46
          | _ => raise THM ("Internal error: Bad specification theorem",0,[thm])
skalberg@14222
    47
skalberg@14222
    48
    fun mk_axiomatic axname cos arg =
wenzelm@17336
    49
        let
wenzelm@17336
    50
            fun process [] (thy,tm) =
wenzelm@17336
    51
                let
haftmann@18377
    52
                    val (thms, thy') = PureThy.add_axioms_i [((axname,HOLogic.mk_Trueprop tm),[])] thy
wenzelm@17336
    53
                in
wenzelm@17336
    54
                    (thy',hd thms)
wenzelm@17336
    55
                end
wenzelm@17336
    56
              | process ((thname,cname,covld)::cos) (thy,tm) =
wenzelm@17336
    57
                case tm of
wenzelm@17336
    58
                    Const("Ex",_) $ P =>
wenzelm@17336
    59
                    let
wenzelm@17336
    60
                        val ctype = domain_type (type_of P)
wenzelm@17336
    61
                        val cname_full = Sign.intern_const (sign_of thy) cname
wenzelm@17336
    62
                        val cdefname = if thname = ""
wenzelm@17336
    63
                                       then Thm.def_name (Sign.base_name cname)
wenzelm@17336
    64
                                       else thname
wenzelm@17336
    65
                        val co = Const(cname_full,ctype)
wenzelm@17336
    66
                        val thy' = Theory.add_finals_i covld [co] thy
wenzelm@17336
    67
                        val tm' = case P of
wenzelm@17336
    68
                                      Abs(_, _, bodt) => subst_bound (co, bodt)
wenzelm@17336
    69
                                    | _ => P $ co
wenzelm@17336
    70
                    in
wenzelm@17336
    71
                        process cos (thy',tm')
wenzelm@17336
    72
                    end
wenzelm@17336
    73
                  | _ => raise TERM ("Internal error: Bad specification theorem",[tm])
wenzelm@17336
    74
        in
wenzelm@17336
    75
            process cos arg
wenzelm@17336
    76
        end
skalberg@14115
    77
skalberg@14222
    78
in
skalberg@14222
    79
fun proc_exprop axiomatic cos arg =
skalberg@14222
    80
    case axiomatic of
wenzelm@17336
    81
        SOME axname => mk_axiomatic axname cos (apsnd (HOLogic.dest_Trueprop o concl_of) arg)
skalberg@15531
    82
      | NONE => mk_definitional cos arg
skalberg@14222
    83
end
skalberg@14222
    84
skalberg@14222
    85
fun add_specification_i axiomatic cos arg =
skalberg@14121
    86
    arg |> apsnd freezeT
wenzelm@17336
    87
        |> proc_exprop axiomatic cos
wenzelm@17336
    88
        |> apsnd standard
skalberg@14115
    89
skalberg@14115
    90
(* Collect all intances of constants in term *)
skalberg@14115
    91
skalberg@14115
    92
fun collect_consts (        t $ u,tms) = collect_consts (u,collect_consts (t,tms))
skalberg@14115
    93
  | collect_consts (   Abs(_,_,t),tms) = collect_consts (t,tms)
skalberg@14115
    94
  | collect_consts (tm as Const _,tms) = gen_ins (op aconv) (tm,tms)
skalberg@14115
    95
  | collect_consts (            _,tms) = tms
skalberg@14115
    96
skalberg@14115
    97
(* Complementing Type.varify... *)
skalberg@14115
    98
skalberg@14115
    99
fun unvarify t fmap =
skalberg@14115
   100
    let
wenzelm@17336
   101
        val fmap' = map Library.swap fmap
haftmann@17377
   102
        fun unthaw (f as (a, S)) =
haftmann@17377
   103
            (case AList.lookup (op =) fmap' a of
wenzelm@17336
   104
                 NONE => TVar f
haftmann@17377
   105
               | SOME (b, _) => TFree (b, S))
skalberg@14115
   106
    in
wenzelm@17336
   107
        map_term_types (map_type_tvar unthaw) t
skalberg@14115
   108
    end
skalberg@14115
   109
skalberg@14115
   110
(* The syntactic meddling needed to setup add_specification for work *)
skalberg@14115
   111
wenzelm@17336
   112
fun process_spec axiomatic cos alt_props thy =
skalberg@14115
   113
    let
wenzelm@17336
   114
        fun zip3 [] [] [] = []
wenzelm@17336
   115
          | zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs
wenzelm@17336
   116
          | zip3 _ _ _ = error "SpecificationPackage.process_spec internal error"
skalberg@14167
   117
wenzelm@17336
   118
        fun myfoldr f [x] = x
wenzelm@17336
   119
          | myfoldr f (x::xs) = f (x,myfoldr f xs)
wenzelm@17336
   120
          | myfoldr f [] = error "SpecificationPackage.process_spec internal error"
skalberg@14164
   121
wenzelm@17895
   122
        fun typ_equiv t u = Sign.typ_instance thy (t,u) andalso Sign.typ_instance thy (u,t);
skalberg@14164
   123
wenzelm@17895
   124
        val rew_imps = alt_props |>
wenzelm@17895
   125
          map (ObjectLogic.atomize_cterm thy o Thm.read_cterm thy o rpair Term.propT o snd)
wenzelm@17895
   126
        val props' = rew_imps |>
wenzelm@17895
   127
          map (HOLogic.dest_Trueprop o term_of o snd o dest_equals o cprop_of)
skalberg@14166
   128
wenzelm@17336
   129
        fun proc_single prop =
wenzelm@17336
   130
            let
wenzelm@17336
   131
                val frees = Term.term_frees prop
wenzelm@17336
   132
                val tsig = Sign.tsig_of (sign_of thy)
wenzelm@17336
   133
                val _ = assert (forall (fn v => Type.of_sort tsig (type_of v,HOLogic.typeS)) frees)
wenzelm@17336
   134
                               "Specificaton: Only free variables of sort 'type' allowed"
wenzelm@17336
   135
                val prop_closed = foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) prop (map dest_Free frees)
wenzelm@17336
   136
            in
wenzelm@17336
   137
                (prop_closed,frees)
wenzelm@17336
   138
            end
skalberg@14166
   139
wenzelm@17336
   140
        val props'' = map proc_single props'
wenzelm@17336
   141
        val frees = map snd props''
wenzelm@17336
   142
        val prop  = myfoldr HOLogic.mk_conj (map fst props'')
wenzelm@17895
   143
        val cprop = cterm_of thy (HOLogic.mk_Trueprop prop)
skalberg@14164
   144
wenzelm@17336
   145
        val (prop_thawed,vmap) = Type.varify (prop,[])
wenzelm@17336
   146
        val thawed_prop_consts = collect_consts (prop_thawed,[])
wenzelm@17336
   147
        val (altcos,overloaded) = Library.split_list cos
wenzelm@17336
   148
        val (names,sconsts) = Library.split_list altcos
wenzelm@17895
   149
        val consts = map (term_of o Thm.read_cterm thy o rpair TypeInfer.logicT) sconsts
wenzelm@17336
   150
        val _ = assert (not (Library.exists (not o Term.is_Const) consts))
wenzelm@17336
   151
                       "Specification: Non-constant found as parameter"
skalberg@14166
   152
wenzelm@17336
   153
        fun proc_const c =
wenzelm@17336
   154
            let
wenzelm@17336
   155
                val c' = fst (Type.varify (c,[]))
wenzelm@17336
   156
                val (cname,ctyp) = dest_Const c'
wenzelm@17336
   157
            in
wenzelm@17336
   158
                case List.filter (fn t => let val (name,typ) = dest_Const t
wenzelm@17336
   159
                                     in name = cname andalso typ_equiv typ ctyp
wenzelm@17336
   160
                                     end) thawed_prop_consts of
wenzelm@17895
   161
                    [] => error ("Specification: No suitable instances of constant \"" ^ (Sign.string_of_term thy c) ^ "\" found")
wenzelm@17336
   162
                  | [cf] => unvarify cf vmap
wenzelm@17895
   163
                  | _ => error ("Specification: Several variations of \"" ^ (Sign.string_of_term thy c) ^ "\" found (try applying explicit type constraints)")
wenzelm@17336
   164
            end
wenzelm@17336
   165
        val proc_consts = map proc_const consts
wenzelm@17336
   166
        fun mk_exist (c,prop) =
wenzelm@17336
   167
            let
wenzelm@17336
   168
                val T = type_of c
wenzelm@17336
   169
                val cname = Sign.base_name (fst (dest_Const c))
wenzelm@17336
   170
                val vname = if Syntax.is_identifier cname
wenzelm@17336
   171
                            then cname
wenzelm@17336
   172
                            else "x"
wenzelm@17336
   173
            in
wenzelm@17336
   174
                HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop))
wenzelm@17336
   175
            end
wenzelm@17336
   176
        val ex_prop = foldr mk_exist prop proc_consts
wenzelm@17336
   177
        val cnames = map (fst o dest_Const) proc_consts
wenzelm@17336
   178
        fun post_process (arg as (thy,thm)) =
wenzelm@17336
   179
            let
wenzelm@17895
   180
                fun inst_all thy (thm,v) =
wenzelm@17336
   181
                    let
wenzelm@17895
   182
                        val cv = cterm_of thy v
wenzelm@17336
   183
                        val cT = ctyp_of_term cv
wenzelm@17336
   184
                        val spec' = instantiate' [SOME cT] [NONE,SOME cv] spec
wenzelm@17336
   185
                    in
wenzelm@17336
   186
                        thm RS spec'
wenzelm@17336
   187
                    end
wenzelm@17336
   188
                fun remove_alls frees thm = Library.foldl (inst_all (sign_of_thm thm)) (thm,frees)
wenzelm@17336
   189
                fun process_single ((name,atts),rew_imp,frees) args =
wenzelm@17336
   190
                    let
wenzelm@17336
   191
                        fun undo_imps thm =
wenzelm@17336
   192
                            equal_elim (symmetric rew_imp) thm
skalberg@14164
   193
haftmann@18358
   194
                        fun add_final (arg as (thy, thm)) =
wenzelm@17336
   195
                            if name = ""
haftmann@18358
   196
                            then arg |> Library.swap
wenzelm@17336
   197
                            else (writeln ("  " ^ name ^ ": " ^ (string_of_thm thm));
haftmann@18358
   198
                                  PureThy.store_thm ((name, thm), []) thy)
wenzelm@17336
   199
                    in
wenzelm@17336
   200
                        args |> apsnd (remove_alls frees)
wenzelm@17336
   201
                             |> apsnd undo_imps
wenzelm@17336
   202
                             |> apsnd standard
haftmann@18418
   203
                             |> Thm.apply_attributes (map (Attrib.global_attribute thy) atts)
wenzelm@17336
   204
                             |> add_final
haftmann@18358
   205
                             |> Library.swap
wenzelm@17336
   206
                    end
skalberg@14164
   207
wenzelm@17336
   208
                fun process_all [proc_arg] args =
wenzelm@17336
   209
                    process_single proc_arg args
wenzelm@17336
   210
                  | process_all (proc_arg::rest) (thy,thm) =
wenzelm@17336
   211
                    let
wenzelm@17336
   212
                        val single_th = thm RS conjunct1
wenzelm@17336
   213
                        val rest_th   = thm RS conjunct2
wenzelm@17336
   214
                        val (thy',_)  = process_single proc_arg (thy,single_th)
wenzelm@17336
   215
                    in
wenzelm@17336
   216
                        process_all rest (thy',rest_th)
wenzelm@17336
   217
                    end
wenzelm@17336
   218
                  | process_all [] _ = error "SpecificationPackage.process_spec internal error"
wenzelm@17336
   219
                val alt_names = map fst alt_props
wenzelm@17336
   220
                val _ = if exists (fn(name,_) => not (name = "")) alt_names
wenzelm@17336
   221
                        then writeln "specification"
wenzelm@17336
   222
                        else ()
wenzelm@17336
   223
            in
wenzelm@17336
   224
                arg |> apsnd freezeT
wenzelm@17336
   225
                    |> process_all (zip3 alt_names rew_imps frees)
wenzelm@17336
   226
            end
skalberg@14115
   227
    in
wenzelm@17336
   228
      IsarThy.theorem_i Drule.internalK
wenzelm@17336
   229
        ("", [add_specification_i axiomatic (zip3 names cnames overloaded), post_process])
wenzelm@17336
   230
        (HOLogic.mk_Trueprop ex_prop, ([], [])) thy
skalberg@14115
   231
    end
skalberg@14115
   232
wenzelm@17336
   233
skalberg@14115
   234
(* outer syntax *)
skalberg@14115
   235
wenzelm@17057
   236
local structure P = OuterParse and K = OuterKeyword in
skalberg@14115
   237
skalberg@14116
   238
val opt_name = Scan.optional (P.name --| P.$$$ ":") ""
wenzelm@17336
   239
val opt_overloaded = P.opt_keyword "overloaded";
skalberg@14116
   240
skalberg@14115
   241
val specification_decl =
wenzelm@17336
   242
  P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
wenzelm@17336
   243
          Scan.repeat1 (P.opt_thm_name ":" -- P.prop)
skalberg@14115
   244
skalberg@14115
   245
val specificationP =
skalberg@14115
   246
  OuterSyntax.command "specification" "define constants by specification" K.thy_goal
skalberg@14164
   247
    (specification_decl >> (fn (cos,alt_props) =>
wenzelm@17336
   248
                               Toplevel.print o (Toplevel.theory_to_proof
wenzelm@17336
   249
                                                     (process_spec NONE cos alt_props))))
skalberg@14222
   250
skalberg@14222
   251
val ax_specification_decl =
skalberg@14222
   252
    P.name --
skalberg@14222
   253
    (P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
wenzelm@17336
   254
           Scan.repeat1 (P.opt_thm_name ":" -- P.prop))
skalberg@14115
   255
skalberg@14222
   256
val ax_specificationP =
skalberg@14222
   257
  OuterSyntax.command "ax_specification" "define constants by specification" K.thy_goal
skalberg@14222
   258
    (ax_specification_decl >> (fn (axname,(cos,alt_props)) =>
wenzelm@17336
   259
                               Toplevel.print o (Toplevel.theory_to_proof
wenzelm@17336
   260
                                                     (process_spec (SOME axname) cos alt_props))))
skalberg@14222
   261
skalberg@14222
   262
val _ = OuterSyntax.add_parsers [specificationP,ax_specificationP]
skalberg@14115
   263
skalberg@14115
   264
end
skalberg@14115
   265
skalberg@14115
   266
skalberg@14115
   267
end