1 (* Title: HOL/Tools/Quotient/quotient_def.ML
2 Author: Cezary Kaliszyk and Christian Urban
4 Definitions for constants on quotient types.
7 signature QUOTIENT_DEF =
9 val quotient_def: (binding option * mixfix) * (Attrib.binding * (term * term)) ->
10 local_theory -> Quotient_Info.qconsts_info * local_theory
12 val quotdef_cmd: (binding option * mixfix) * (Attrib.binding * (string * string)) ->
13 local_theory -> Quotient_Info.qconsts_info * local_theory
15 val lift_raw_const: typ list -> (string * term * mixfix) -> local_theory ->
16 Quotient_Info.qconsts_info * local_theory
19 structure Quotient_Def: QUOTIENT_DEF =
25 (** Interface and Syntax Setup **)
27 (* The ML-interface for a quotient definition takes
30 - an optional binding and mixfix annotation
32 - the new constant as term
33 - the rhs of the definition as term
35 It stores the qconst_info in the qconsts data slot.
37 Restriction: At the moment the left- and right-hand
38 side of the definition must be a constant.
40 fun error_msg bind str =
42 val name = Binding.name_of bind
43 val pos = Position.str_of (Binding.pos_of bind)
45 error ("Head of quotient_definition " ^
46 quote str ^ " differs from declaration " ^ name ^ pos)
49 fun quotient_def ((optbind, mx), (attr, (lhs, rhs))) lthy =
51 val (lhs_str, lhs_ty) = dest_Free lhs handle TERM _ => error "Constant already defined."
52 val _ = if null (strip_abs_vars rhs) then () else error "The definiens cannot be an abstraction"
53 val _ = if is_Const rhs then () else warning "The definiens is not a constant"
55 fun sanity_test NONE _ = true
56 | sanity_test (SOME bind) str =
57 if Name.of_binding bind = str then true
58 else error_msg bind str
60 val _ = sanity_test optbind lhs_str
62 val qconst_bname = Binding.name lhs_str
63 val absrep_trm = absrep_fun AbsF lthy (fastype_of rhs, lhs_ty) $ rhs
64 val prop = Logic.mk_equals (lhs, Syntax.check_term lthy absrep_trm)
65 val (_, prop') = Local_Defs.cert_def lthy prop
66 val (_, newrhs) = Local_Defs.abs_def prop'
68 val ((trm, (_ , thm)), lthy') = Local_Theory.define ((qconst_bname, mx), (attr, newrhs)) lthy
71 val qconst_data = {qconst = trm, rconst = rhs, def = thm}
73 fun qcinfo phi = transform_qconsts phi qconst_data
74 fun trans_name phi = (fst o dest_Const o #qconst) (qcinfo phi)
75 val lthy'' = Local_Theory.declaration true
76 (fn phi => qconsts_update_gen (trans_name phi) (qcinfo phi)) lthy'
81 fun quotdef_cmd (decl, (attr, (lhs_str, rhs_str))) lthy =
83 val lhs = Syntax.read_term lthy lhs_str
84 val rhs = Syntax.read_term lthy rhs_str
85 val lthy' = Variable.declare_term lhs lthy
86 val lthy'' = Variable.declare_term rhs lthy'
88 quotient_def (decl, (attr, (lhs, rhs))) lthy''
91 (* a wrapper for automatically lifting a raw constant *)
92 fun lift_raw_const qtys (qconst_name, rconst, mx) ctxt =
94 val rty = fastype_of rconst
95 val qty = derive_qtyp qtys rty ctxt
96 val lhs = Free (qconst_name, qty)
98 quotient_def ((NONE, mx), (Attrib.empty_binding, (lhs, rconst))) ctxt
101 (* parser and command *)
102 val quotdef_decl = (Parse.binding >> SOME) -- Parse.opt_mixfix' --| Parse.$$$ "where"
105 Scan.optional quotdef_decl (NONE, NoSyn) --
106 Parse.!!! (Parse_Spec.opt_thm_name ":" -- (Parse.term --| Parse.$$$ "is" -- Parse.term))
109 Outer_Syntax.local_theory "quotient_definition"
110 "definition for constants over the quotient type"
111 Keyword.thy_decl (quotdef_parser >> (snd oo quotdef_cmd))