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