src/HOL/Tools/specification_package.ML
author skalberg
Mon, 21 Jul 2003 08:53:56 +0200
changeset 14118 05416ba8eef2
parent 14117 d3512dedbaea
child 14121 d2a0fd183f5f
permissions -rw-r--r--
Changed bstring argument to xstring.
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
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     5
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     6
Package for defining constants by specification.
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     7
*)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     8
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
     9
signature SPECIFICATION_PACKAGE =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    10
sig
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    11
    val quiet_mode: bool ref
14118
05416ba8eef2 Changed bstring argument to xstring.
skalberg
parents: 14117
diff changeset
    12
    val add_specification: (bstring * xstring * bool) list -> bstring * Args.src list
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    13
			   -> theory * thm -> theory * thm
14118
05416ba8eef2 Changed bstring argument to xstring.
skalberg
parents: 14117
diff changeset
    14
    val add_specification_i: (bstring * xstring * bool) list -> bstring * theory attribute list
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    15
			     -> theory * thm -> theory * thm
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    16
end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    17
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    18
structure SpecificationPackage : SPECIFICATION_PACKAGE =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    19
struct
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    20
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    21
(* messages *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    22
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    23
val quiet_mode = ref false
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    24
fun message s = if ! quiet_mode then () else writeln s
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    25
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    26
local
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    27
    val _ = Goal "[| Ex P ; c == Eps P |] ==> P c"
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    28
    val _ = by (Asm_simp_tac 1)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    29
    val _ = by (rtac (thm "someI_ex") 1)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    30
    val _ = ba 1
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    31
in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    32
val helper = Goals.result()
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    33
end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    34
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    35
(* Akin to HOLogic.exists_const *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    36
fun choice_const T = Const("Hilbert_Choice.Eps",(T-->HOLogic.boolT)-->T)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    37
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    38
(* Actual code *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    39
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    40
fun proc_exprop [] arg = arg
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
    41
  | proc_exprop ((thname,cname,covld)::cos) (thy,thm) =
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    42
    case concl_of thm of
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    43
	Const("Trueprop",_) $ (Const("Ex",_) $ P) =>
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    44
	let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    45
	    val ctype = domain_type (type_of P)
14117
d3512dedbaea *** empty log message ***
skalberg
parents: 14116
diff changeset
    46
	    val cname_full = Sign.intern_const (sign_of thy) cname
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
    47
	    val cdefname = if thname = ""
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
    48
			   then Thm.def_name (Sign.base_name cname)
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
    49
			   else thname
14117
d3512dedbaea *** empty log message ***
skalberg
parents: 14116
diff changeset
    50
	    val def_eq = Logic.mk_equals (Const(cname_full,ctype),choice_const ctype $  P)
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    51
	    val (thy',thms) = PureThy.add_defs_i covld [((cdefname,def_eq),[])] thy
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    52
	    val thm' = [thm,hd thms] MRS helper
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    53
	in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    54
	    proc_exprop cos (thy',thm')
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    55
	end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    56
      | _ => raise THM ("Internal error: Bad specification theorem",0,[thm])
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    57
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    58
fun add_specification_i cos (name,atts) arg =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    59
    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    60
	fun apply_attributes arg = Thm.apply_attributes(arg,atts)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    61
	fun add_final (arg as (thy,thm)) =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    62
	    if name = ""
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    63
	    then arg
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    64
	    else (writeln ("specification " ^ name ^ ": " ^ (string_of_thm thm));
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    65
		  PureThy.store_thm((name,thm),[]) thy)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    66
    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    67
	arg |> apsnd freezeT
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    68
	    |> proc_exprop cos
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    69
	    |> apsnd standard
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    70
	    |> apply_attributes
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    71
	    |> add_final
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    72
    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    73
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    74
fun add_specification cos (name,atts) arg =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    75
    add_specification_i cos (name,map (Attrib.global_attribute thy) atts) arg
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    76
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    77
(* Collect all intances of constants in term *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    78
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    79
fun collect_consts (        t $ u,tms) = collect_consts (u,collect_consts (t,tms))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    80
  | collect_consts (   Abs(_,_,t),tms) = collect_consts (t,tms)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    81
  | collect_consts (tm as Const _,tms) = gen_ins (op aconv) (tm,tms)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    82
  | collect_consts (            _,tms) = tms
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    83
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    84
(* Complementing Type.varify... *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    85
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    86
fun unvarify t fmap =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    87
    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    88
	val fmap' = map Library.swap fmap
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    89
	fun unthaw (f as (a,S)) =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    90
	    (case assoc (fmap',a) of
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    91
		 None => TVar f
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    92
	       | Some b => TFree (b,S))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    93
    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    94
	map_term_types (map_type_tvar unthaw) t
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    95
    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    96
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
    97
(* The syntactic meddling needed to setup add_specification for work *)
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 process_spec cos alt_name sprop int thy =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   100
    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   101
	val sg = sign_of thy
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   102
	fun typ_equiv t u =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   103
	    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   104
		val tsig = Sign.tsig_of sg
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   105
	    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   106
		Type.typ_instance(tsig,t,u) andalso
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   107
		Type.typ_instance(tsig,u,t)
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
	val prop = term_of (Thm.read_cterm sg (sprop,HOLogic.boolT))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   110
	val (prop_thawed,vmap) = Type.varify (prop,[])
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   111
	val thawed_prop_consts = collect_consts (prop_thawed,[])
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   112
	val (altcos,overloaded) = Library.split_list cos
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   113
	val (names,sconsts) = Library.split_list altcos
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   114
	val consts = map (term_of o Thm.read_cterm sg o rpair TypeInfer.logicT) sconsts
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   115
	val _ = assert (not (Library.exists (not o Term.is_Const) consts))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   116
		       "Non-constant found as parameter"
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   117
	fun proc_const c =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   118
	    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   119
		val c' = fst (Type.varify (c,[]))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   120
		val (cname,ctyp) = dest_Const c'
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   121
	    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   122
		case filter (fn t => let val (name,typ) = dest_Const t
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   123
				     in name = cname andalso typ_equiv typ ctyp
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   124
				     end) thawed_prop_consts of
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   125
		    [] => error ("Constant \"" ^ (Sign.string_of_term sg c) ^ "\" not found in specification")
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   126
		  | [cf] => unvarify cf vmap
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   127
		  | _ => error ("Several variations of \"" ^ (Sign.string_of_term sg c) ^ "\" found in specification")
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   128
	    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   129
	val proc_consts = map proc_const consts
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   130
	fun mk_exist (c,prop) =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   131
	    let
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   132
		val T = type_of c
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   133
	    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   134
		HOLogic.exists_const T $ Abs("x",T,Term.abstract_over (c,prop))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   135
	    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   136
	val ex_prop = foldr mk_exist (proc_consts,prop)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   137
	val cnames = map (fst o dest_Const) proc_consts
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   138
	fun zip3 [] [] [] = []
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   139
	  | zip3 (x::xs) (y::ys) (z::zs) = (x,y,z)::zip3 xs ys zs
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   140
	  | zip3 _ _ _ = error "Internal error: Bad specification syntax"
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   141
    in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   142
	IsarThy.theorem_i Drule.internalK
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   143
	    (("",[add_specification (zip3 (names:string list) (cnames:string list) (overloaded:bool list)) alt_name]),
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   144
	     (HOLogic.mk_Trueprop ex_prop,([],[]))) int thy
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   145
    end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   146
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   147
(* outer syntax *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   148
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   149
local structure P = OuterParse and K = OuterSyntax.Keyword in
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   150
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   151
(* taken from ~~/Pure/Isar/isar_syn.ML *)
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   152
val opt_overloaded =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   153
  Scan.optional (P.$$$ "(" |-- P.!!! ((P.$$$ "overloaded" >> K true) --| P.$$$ ")")) false
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   154
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   155
val opt_name = Scan.optional (P.name --| P.$$$ ":") ""
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   156
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   157
val specification_decl =
14116
63337d8e6e0f Added optional theorem names for the constant definitions added during
skalberg
parents: 14115
diff changeset
   158
    P.$$$ "(" |-- Scan.repeat1 (opt_name -- P.term -- opt_overloaded) --| P.$$$ ")" --
14115
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   159
	  P.opt_thm_name ":" -- P.prop
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   160
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   161
val specificationP =
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   162
  OuterSyntax.command "specification" "define constants by specification" K.thy_goal
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   163
    (specification_decl >> (fn ((cos,alt_name), prop) =>
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   164
			       Toplevel.print o (Toplevel.theory_to_proof (process_spec cos alt_name prop))))
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   165
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   166
val _ = OuterSyntax.add_parsers [specificationP]
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   167
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   168
end
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   169
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   170
65ec3f73d00b Added package for definition by specification.
skalberg
parents:
diff changeset
   171
end