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