src/HOL/Tools/specification_package.ML
author wenzelm
Sun, 01 Mar 2009 23:36:12 +0100
changeset 30190 479806475f3c
parent 29579 cb520b766e00
child 30223 24d975352879
permissions -rw-r--r--
use long names for old-style fold combinators;
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
    Author:     Sebastian Skalberg, TU Muenchen
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     3
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     4
Package for defining constants by specification.
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     5
*)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     6
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     7
signature SPECIFICATION_PACKAGE =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     8
sig
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18418
diff changeset
     9
  val add_specification: string option -> (bstring * xstring * bool) list ->
216e31270509 simplified type attribute;
wenzelm
parents: 18418
diff changeset
    10
    theory * thm -> theory * thm
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    11
end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    12
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
    13
structure SpecificationPackage: SPECIFICATION_PACKAGE =
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    14
struct
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    15
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24867
diff changeset
    16
(* actual code *)
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    17
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    18
local
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    19
    fun mk_definitional [] arg = arg
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    20
      | mk_definitional ((thname,cname,covld)::cos) (thy,thm) =
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    21
        case HOLogic.dest_Trueprop (concl_of thm) of
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    22
            Const("Ex",_) $ P =>
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    23
            let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    24
                val ctype = domain_type (type_of P)
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22101
diff changeset
    25
                val cname_full = Sign.intern_const thy cname
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    26
                val cdefname = if thname = ""
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    27
                               then Thm.def_name (Sign.base_name cname)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    28
                               else thname
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    29
                val def_eq = Logic.mk_equals (Const(cname_full,ctype),
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    30
                                              HOLogic.choice_const ctype $  P)
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29265
diff changeset
    31
                val (thms, thy') = PureThy.add_defs covld [((Binding.name cdefname, def_eq),[])] thy
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 24867
diff changeset
    32
                val thm' = [thm,hd thms] MRS @{thm exE_some}
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    33
            in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    34
                mk_definitional cos (thy',thm')
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    35
            end
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    36
          | _ => raise THM ("Internal error: Bad specification theorem",0,[thm])
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    37
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    38
    fun mk_axiomatic axname cos arg =
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    39
        let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    40
            fun process [] (thy,tm) =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    41
                let
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29265
diff changeset
    42
                    val (thms, thy') = PureThy.add_axioms [((Binding.name axname, HOLogic.mk_Trueprop tm),[])] thy
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    43
                in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    44
                    (thy',hd thms)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    45
                end
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    46
              | process ((thname,cname,covld)::cos) (thy,tm) =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    47
                case tm of
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    48
                    Const("Ex",_) $ P =>
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    49
                    let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    50
                        val ctype = domain_type (type_of P)
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22101
diff changeset
    51
                        val cname_full = Sign.intern_const thy cname
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    52
                        val cdefname = if thname = ""
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    53
                                       then Thm.def_name (Sign.base_name cname)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    54
                                       else thname
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    55
                        val co = Const(cname_full,ctype)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    56
                        val thy' = Theory.add_finals_i covld [co] thy
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    57
                        val tm' = case P of
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    58
                                      Abs(_, _, bodt) => subst_bound (co, bodt)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    59
                                    | _ => P $ co
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    60
                    in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    61
                        process cos (thy',tm')
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    62
                    end
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    63
                  | _ => raise TERM ("Internal error: Bad specification theorem",[tm])
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    64
        in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    65
            process cos arg
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    66
        end
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    67
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    68
in
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    69
fun proc_exprop axiomatic cos arg =
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    70
    case axiomatic of
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    71
        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
    72
      | NONE => mk_definitional cos arg
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    73
end
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
    74
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18418
diff changeset
    75
fun add_specification axiomatic cos arg =
19876
wenzelm
parents: 19585
diff changeset
    76
    arg |> apsnd Thm.freezeT
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    77
        |> proc_exprop axiomatic cos
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    78
        |> apsnd standard
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    79
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18418
diff changeset
    80
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    81
(* Collect all intances of constants in term *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    82
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    83
fun collect_consts (        t $ u,tms) = collect_consts (u,collect_consts (t,tms))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    84
  | collect_consts (   Abs(_,_,t),tms) = collect_consts (t,tms)
18921
f47c46d7d654 canonical member/insert/merge;
wenzelm
parents: 18799
diff changeset
    85
  | collect_consts (tm as Const _,tms) = insert (op aconv) tm tms
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    86
  | collect_consts (            _,tms) = tms
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    87
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    88
(* Complementing Type.varify... *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    89
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    90
fun unvarify t fmap =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    91
    let
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    92
        val fmap' = map Library.swap fmap
17377
afaa031ed4da introduced AList.lookup
haftmann
parents: 17336
diff changeset
    93
        fun unthaw (f as (a, S)) =
afaa031ed4da introduced AList.lookup
haftmann
parents: 17336
diff changeset
    94
            (case AList.lookup (op =) fmap' a of
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
    95
                 NONE => TVar f
17377
afaa031ed4da introduced AList.lookup
haftmann
parents: 17336
diff changeset
    96
               | SOME (b, _) => TFree (b, S))
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    97
    in
20548
8ef25fe585a8 renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents: 20344
diff changeset
    98
        map_types (map_type_tvar unthaw) t
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    99
    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   100
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   101
(* The syntactic meddling needed to setup add_specification for work *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   102
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   103
fun process_spec axiomatic cos alt_props thy =
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   104
    let
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   105
        fun zip3 [] [] [] = []
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   106
          | zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   107
          | zip3 _ _ _ = error "SpecificationPackage.process_spec internal error"
14167
3c29bba24aa4 Improved the error messages (slightly).
skalberg
parents: 14166
diff changeset
   108
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   109
        fun myfoldr f [x] = x
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   110
          | myfoldr f (x::xs) = f (x,myfoldr f xs)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   111
          | myfoldr f [] = error "SpecificationPackage.process_spec internal error"
14164
8c3fab596219 Allowed for splitting the specification over several lemmas.
skalberg
parents: 14122
diff changeset
   112
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
   113
        val rew_imps = alt_props |>
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 23591
diff changeset
   114
          map (ObjectLogic.atomize o Thm.cterm_of thy o Syntax.read_prop_global thy o snd)
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
   115
        val props' = rew_imps |>
22902
ac833b4bb7ee moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents: 22709
diff changeset
   116
          map (HOLogic.dest_Trueprop o term_of o snd o Thm.dest_equals o cprop_of)
14166
1ed8f955727d Cleaned up the code.
skalberg
parents: 14164
diff changeset
   117
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   118
        fun proc_single prop =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   119
            let
29265
5b4247055bd7 moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents: 29006
diff changeset
   120
                val frees = OldTerm.term_frees prop
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21434
diff changeset
   121
                val _ = forall (fn v => Sign.of_sort thy (type_of v,HOLogic.typeS)) frees
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21434
diff changeset
   122
                  orelse error "Specificaton: Only free variables of sort 'type' allowed"
30190
479806475f3c use long names for old-style fold combinators;
wenzelm
parents: 29579
diff changeset
   123
                val prop_closed = List.foldr (fn ((vname,T),prop) => HOLogic.mk_all (vname,T,prop)) prop (map dest_Free frees)
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   124
            in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   125
                (prop_closed,frees)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   126
            end
14166
1ed8f955727d Cleaned up the code.
skalberg
parents: 14164
diff changeset
   127
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   128
        val props'' = map proc_single props'
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   129
        val frees = map snd props''
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   130
        val prop  = myfoldr HOLogic.mk_conj (map fst props'')
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
   131
        val cprop = cterm_of thy (HOLogic.mk_Trueprop prop)
14164
8c3fab596219 Allowed for splitting the specification over several lemmas.
skalberg
parents: 14122
diff changeset
   132
21116
be58cded79da fixed type signature of Type.varify
haftmann
parents: 20903
diff changeset
   133
        val (vmap, prop_thawed) = Type.varify [] prop
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   134
        val thawed_prop_consts = collect_consts (prop_thawed,[])
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   135
        val (altcos,overloaded) = Library.split_list cos
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   136
        val (names,sconsts) = Library.split_list altcos
24707
dfeb98f84e93 Syntax.parse/check/read;
wenzelm
parents: 23591
diff changeset
   137
        val consts = map (Syntax.read_term_global thy) sconsts
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21434
diff changeset
   138
        val _ = not (Library.exists (not o Term.is_Const) consts)
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21434
diff changeset
   139
          orelse error "Specification: Non-constant found as parameter"
14166
1ed8f955727d Cleaned up the code.
skalberg
parents: 14164
diff changeset
   140
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   141
        fun proc_const c =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   142
            let
21116
be58cded79da fixed type signature of Type.varify
haftmann
parents: 20903
diff changeset
   143
                val (_, c') = Type.varify [] c
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   144
                val (cname,ctyp) = dest_Const c'
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   145
            in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   146
                case List.filter (fn t => let val (name,typ) = dest_Const t
19414
a21431e996bf Sign.typ_equiv;
wenzelm
parents: 18921
diff changeset
   147
                                     in name = cname andalso Sign.typ_equiv thy (typ, ctyp)
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   148
                                     end) thawed_prop_consts of
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   149
                    [] => error ("Specification: No suitable instances of constant \"" ^ Syntax.string_of_term_global thy c ^ "\" found")
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   150
                  | [cf] => unvarify cf vmap
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   151
                  | _ => error ("Specification: Several variations of \"" ^ Syntax.string_of_term_global thy c ^ "\" found (try applying explicit type constraints)")
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   152
            end
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   153
        val proc_consts = map proc_const consts
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   154
        fun mk_exist (c,prop) =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   155
            let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   156
                val T = type_of c
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   157
                val cname = Sign.base_name (fst (dest_Const c))
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   158
                val vname = if Syntax.is_identifier cname
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   159
                            then cname
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   160
                            else "x"
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   161
            in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   162
                HOLogic.exists_const T $ Abs(vname,T,Term.abstract_over (c,prop))
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   163
            end
30190
479806475f3c use long names for old-style fold combinators;
wenzelm
parents: 29579
diff changeset
   164
        val ex_prop = List.foldr mk_exist prop proc_consts
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   165
        val cnames = map (fst o dest_Const) proc_consts
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   166
        fun post_process (arg as (thy,thm)) =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   167
            let
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
   168
                fun inst_all thy (thm,v) =
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   169
                    let
17895
6274b426594b moved helper lemma to HilbertChoice.thy;
wenzelm
parents: 17377
diff changeset
   170
                        val cv = cterm_of thy v
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   171
                        val cT = ctyp_of_term cv
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   172
                        val spec' = instantiate' [SOME cT] [NONE,SOME cv] spec
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   173
                    in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   174
                        thm RS spec'
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   175
                    end
22578
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22101
diff changeset
   176
                fun remove_alls frees thm =
b0eb5652f210 removed obsolete sign_of/sign_of_thm;
wenzelm
parents: 22101
diff changeset
   177
                    Library.foldl (inst_all (Thm.theory_of_thm thm)) (thm,frees)
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   178
                fun process_single ((name,atts),rew_imp,frees) args =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   179
                    let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   180
                        fun undo_imps thm =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   181
                            equal_elim (symmetric rew_imp) thm
14164
8c3fab596219 Allowed for splitting the specification over several lemmas.
skalberg
parents: 14122
diff changeset
   182
18358
0a733e11021a re-oriented some result tuples in PureThy
haftmann
parents: 17895
diff changeset
   183
                        fun add_final (arg as (thy, thm)) =
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   184
                            if name = ""
18358
0a733e11021a re-oriented some result tuples in PureThy
haftmann
parents: 17895
diff changeset
   185
                            then arg |> Library.swap
26928
ca87aff1ad2d structure Display: less pervasive operations;
wenzelm
parents: 26481
diff changeset
   186
                            else (writeln ("  " ^ name ^ ": " ^ (Display.string_of_thm thm));
29579
cb520b766e00 binding replaces bstring
haftmann
parents: 29265
diff changeset
   187
                                  PureThy.store_thm (Binding.name name, thm) thy)
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   188
                    in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   189
                        args |> apsnd (remove_alls frees)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   190
                             |> apsnd undo_imps
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   191
                             |> apsnd standard
18729
216e31270509 simplified type attribute;
wenzelm
parents: 18418
diff changeset
   192
                             |> Thm.theory_attributes (map (Attrib.attribute thy) atts)
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   193
                             |> add_final
18358
0a733e11021a re-oriented some result tuples in PureThy
haftmann
parents: 17895
diff changeset
   194
                             |> Library.swap
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   195
                    end
14164
8c3fab596219 Allowed for splitting the specification over several lemmas.
skalberg
parents: 14122
diff changeset
   196
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   197
                fun process_all [proc_arg] args =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   198
                    process_single proc_arg args
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   199
                  | process_all (proc_arg::rest) (thy,thm) =
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   200
                    let
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   201
                        val single_th = thm RS conjunct1
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   202
                        val rest_th   = thm RS conjunct2
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   203
                        val (thy',_)  = process_single proc_arg (thy,single_th)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   204
                    in
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   205
                        process_all rest (thy',rest_th)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   206
                    end
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   207
                  | process_all [] _ = error "SpecificationPackage.process_spec internal error"
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   208
                val alt_names = map fst alt_props
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   209
                val _ = if exists (fn(name,_) => not (name = "")) alt_names
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   210
                        then writeln "specification"
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   211
                        else ()
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   212
            in
19876
wenzelm
parents: 19585
diff changeset
   213
                arg |> apsnd Thm.freezeT
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   214
                    |> process_all (zip3 alt_names rew_imps frees)
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   215
            end
20903
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   216
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   217
      fun after_qed [[thm]] = ProofContext.theory (fn thy =>
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   218
        #1 (post_process (add_specification axiomatic (zip3 names cnames overloaded) (thy, thm))));
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   219
    in
20903
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   220
      thy
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   221
      |> ProofContext.init
21434
944f80576be0 simplified Proof.theorem(_i);
wenzelm
parents: 21359
diff changeset
   222
      |> Proof.theorem_i NONE after_qed [[(HOLogic.mk_Trueprop ex_prop, [])]]
20903
905effde63d9 attribute: Context.mapping;
wenzelm
parents: 20548
diff changeset
   223
    end;
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   224
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   225
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   226
(* outer syntax *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   227
17057
0934ac31985f OuterKeyword;
wenzelm
parents: 15945
diff changeset
   228
local structure P = OuterParse and K = OuterKeyword in
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   229
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   230
val opt_name = Scan.optional (P.name --| P.$$$ ":") ""
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   231
val opt_overloaded = P.opt_keyword "overloaded";
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   232
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   233
val specification_decl =
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   234
  P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28083
diff changeset
   235
          Scan.repeat1 ((SpecParse.opt_thm_name ":" >> apfst Binding.base_name) -- P.prop)
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   236
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   237
val _ =
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   238
  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
   239
    (specification_decl >> (fn (cos,alt_props) =>
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   240
                               Toplevel.print o (Toplevel.theory_to_proof
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   241
                                                     (process_spec NONE cos alt_props))))
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   242
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   243
val ax_specification_decl =
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   244
    P.name --
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   245
    (P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
29006
abe0f11cfa4e Name.name_of -> Binding.base_name
haftmann
parents: 28083
diff changeset
   246
           Scan.repeat1 ((SpecParse.opt_thm_name ":" >> apfst Binding.base_name) -- P.prop))
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   247
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24707
diff changeset
   248
val _ =
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   249
  OuterSyntax.command "ax_specification" "define constants by specification" K.thy_goal
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   250
    (ax_specification_decl >> (fn (axname,(cos,alt_props)) =>
17336
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   251
                               Toplevel.print o (Toplevel.theory_to_proof
c05f72cff368 tuned IsarThy.theorem_i;
wenzelm
parents: 17057
diff changeset
   252
                                                     (process_spec (SOME axname) cos alt_props))))
14222
1e61f23fd359 Added axiomatic specifications (ax_specification).
skalberg
parents: 14167
diff changeset
   253
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   254
end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   255
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   256
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   257
end