src/HOL/Tools/Quotient/quotient_def.ML
author haftmann
Thu, 08 Jul 2010 16:19:24 +0200
changeset 37743 3daaf23b9ab4
parent 37592 e16495cfdde0
child 38847 9bb0016f7e60
permissions -rw-r--r--
tuned titles
     1 (*  Title:      HOL/Tools/Quotient/quotient_def.ML
     2     Author:     Cezary Kaliszyk and Christian Urban
     3 
     4 Definitions for constants on quotient types.
     5 *)
     6 
     7 signature QUOTIENT_DEF =
     8 sig
     9   val quotient_def: (binding option * mixfix) * (Attrib.binding * (term * term)) ->
    10     local_theory -> Quotient_Info.qconsts_info * local_theory
    11 
    12   val quotdef_cmd: (binding option * mixfix) * (Attrib.binding * (string * string)) ->
    13     local_theory -> Quotient_Info.qconsts_info * local_theory
    14 
    15   val lift_raw_const: typ list -> (string * term * mixfix) -> local_theory ->
    16     Quotient_Info.qconsts_info * local_theory
    17 end;
    18 
    19 structure Quotient_Def: QUOTIENT_DEF =
    20 struct
    21 
    22 open Quotient_Info;
    23 open Quotient_Term;
    24 
    25 (** Interface and Syntax Setup **)
    26 
    27 (* The ML-interface for a quotient definition takes
    28    as argument:
    29 
    30     - an optional binding and mixfix annotation
    31     - attributes
    32     - the new constant as term
    33     - the rhs of the definition as term
    34 
    35    It stores the qconst_info in the qconsts data slot.
    36 
    37    Restriction: At the moment the left- and right-hand 
    38    side of the definition must be a constant. 
    39 *)
    40 fun error_msg bind str = 
    41 let 
    42   val name = Binding.name_of bind
    43   val pos = Position.str_of (Binding.pos_of bind)
    44 in
    45   error ("Head of quotient_definition " ^ 
    46     quote str ^ " differs from declaration " ^ name ^ pos)
    47 end
    48 
    49 fun quotient_def ((optbind, mx), (attr, (lhs, rhs))) lthy =
    50 let
    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"
    54   
    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
    59 
    60   val _ = sanity_test optbind lhs_str
    61 
    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'
    67 
    68   val ((trm, (_ , thm)), lthy') = Local_Theory.define ((qconst_bname, mx), (attr, newrhs)) lthy
    69 
    70   (* data storage *)
    71   val qconst_data = {qconst = trm, rconst = rhs, def = thm}
    72 
    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'
    77 in
    78   (qconst_data, lthy'')
    79 end
    80 
    81 fun quotdef_cmd (decl, (attr, (lhs_str, rhs_str))) lthy =
    82 let
    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'
    87 in
    88   quotient_def (decl, (attr, (lhs, rhs))) lthy''
    89 end
    90 
    91 (* a wrapper for automatically lifting a raw constant *)
    92 fun lift_raw_const qtys (qconst_name, rconst, mx) ctxt =
    93 let
    94   val rty = fastype_of rconst
    95   val qty = derive_qtyp qtys rty ctxt
    96   val lhs = Free (qconst_name, qty)
    97 in
    98   quotient_def ((NONE, mx), (Attrib.empty_binding, (lhs, rconst))) ctxt
    99 end
   100 
   101 (* parser and command *)
   102 val quotdef_decl = (Parse.binding >> SOME) -- Parse.opt_mixfix' --| Parse.$$$ "where"
   103 
   104 val quotdef_parser =
   105   Scan.optional quotdef_decl (NONE, NoSyn) -- 
   106     Parse.!!! (Parse_Spec.opt_thm_name ":" -- (Parse.term --| Parse.$$$ "is" -- Parse.term))
   107 
   108 val _ =
   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))
   112 
   113 end; (* structure *)