src/Pure/Tools/class_package.ML
changeset 22385 cc2be3315e72
parent 22353 c818c6b836f5
child 22423 c1836b14c63a
equal deleted inserted replaced
22384:33a46e6c7f04 22385:cc2be3315e72
     7 
     7 
     8 signature CLASS_PACKAGE =
     8 signature CLASS_PACKAGE =
     9 sig
     9 sig
    10   val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix
    10   val fork_mixfix: bool -> string option -> mixfix -> mixfix * mixfix
    11 
    11 
    12   val class: bstring -> class list -> Element.context_i Locale.element list -> theory ->
    12   val class: bstring -> class list -> Element.context_i Locale.element list
    13     string * Proof.context
    13     -> string list -> theory -> string * Proof.context
    14   val class_cmd: bstring -> string list -> Element.context Locale.element list -> theory ->
    14   val class_cmd: bstring -> string list -> Element.context Locale.element list
    15     string * Proof.context
    15     -> string list -> theory -> string * Proof.context
    16   val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list
    16   val instance_arity: arity list -> ((bstring * Attrib.src list) * term) list
    17     -> theory -> Proof.state
    17     -> theory -> Proof.state
    18   val instance_arity_cmd: (bstring * string list * string) list
    18   val instance_arity_cmd: (bstring * string list * string) list
    19     -> ((bstring * Attrib.src list) * string) list
    19     -> ((bstring * Attrib.src list) * string) list
    20     -> theory -> Proof.state
    20     -> theory -> Proof.state
    31 
    31 
    32   (* experimental class target *)
    32   (* experimental class target *)
    33   val class_of_locale: theory -> string -> class option
    33   val class_of_locale: theory -> string -> class option
    34   val add_def_in_class: local_theory -> string
    34   val add_def_in_class: local_theory -> string
    35     -> (string * Syntax.mixfix) * ((string * Attrib.src list) * (term * thm)) -> theory -> theory
    35     -> (string * Syntax.mixfix) * ((string * Attrib.src list) * (term * thm)) -> theory -> theory
       
    36   val fully_qualified: bool ref;
    36 
    37 
    37   val print_classes: theory -> unit
    38   val print_classes: theory -> unit
    38   val intro_classes_tac: thm list -> tactic
    39   val intro_classes_tac: thm list -> tactic
    39   val default_intro_classes_tac: thm list -> tactic
    40   val default_intro_classes_tac: thm list -> tactic
    40 end;
    41 end;
    79 end; (* local *)
    80 end; (* local *)
    80 
    81 
    81 
    82 
    82 (* introducing axclasses with implicit parameter handling *)
    83 (* introducing axclasses with implicit parameter handling *)
    83 
    84 
    84 fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms thy =
    85 fun axclass_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy =
    85   let
    86   let
    86     val superclasses = map (Sign.certify_class thy) raw_superclasses;
    87     val superclasses = map (Sign.certify_class thy) raw_superclasses;
    87     val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
    88     val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts;
    88     fun add_const ((c, ty), syn) =
    89     fun add_const ((c, ty), syn) =
    89       Sign.add_consts_authentic [(c, ty, syn)] #> pair (Sign.full_name thy c, ty);
    90       Sign.add_consts_authentic [(c, ty, syn)] #> pair (Sign.full_name thy c, ty);
    95       Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
    96       Sign.add_const_constraint_i (c, SOME (Term.map_type_tfree (fn (v, _) => TFree (v, [class])) ty));
    96   in
    97   in
    97     thy
    98     thy
    98     |> fold_map add_const consts
    99     |> fold_map add_const consts
    99     |-> (fn cs => mk_axioms cs
   100     |-> (fn cs => mk_axioms cs
   100     #-> (fn axioms_prop => AxClass.define_class_i (name, superclasses) (map fst cs) axioms_prop
   101     #-> (fn axioms_prop => AxClass.define_class_i (name, superclasses) (map fst cs @ other_consts) axioms_prop
   101     #-> (fn class => `(fn thy => AxClass.get_definition thy class)
   102     #-> (fn class => `(fn thy => AxClass.get_definition thy class)
   102     #-> (fn {intro, axioms, ...} => fold (add_constraint class) cs
   103     #-> (fn {intro, axioms, ...} => fold (add_constraint class) cs
   103     #> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs))))))
   104     #> pair (class, ((intro, (map Thm.prop_of axioms, axioms)), cs))))))
   104   end;
   105   end;
   105 
   106 
   424 val prove_instance_sort = instance_sort' o prove_interpretation_in;
   425 val prove_instance_sort = instance_sort' o prove_interpretation_in;
   425 
   426 
   426 
   427 
   427 (* classes and instances *)
   428 (* classes and instances *)
   428 
   429 
       
   430 val fully_qualified = ref false;
       
   431 
   429 local
   432 local
   430 
   433 
   431 fun add_axclass (name, supsort) params axs thy =
   434 fun add_axclass (name, supsort) params axs thy =
   432   let
   435   let
   433     val (c, thy') = thy
   436     val (c, thy') = thy
   439   tap (the_class_data thy) (Sign.certify_class thy class);
   442   tap (the_class_data thy) (Sign.certify_class thy class);
   440 
   443 
   441 fun read_class thy =
   444 fun read_class thy =
   442   certify_class thy o Sign.intern_class thy;
   445   certify_class thy o Sign.intern_class thy;
   443 
   446 
   444 fun gen_class add_locale prep_class bname raw_supclasses raw_elems thy =
   447 fun gen_class add_locale prep_class prep_param bname raw_supclasses raw_elems raw_other_consts thy =
   445   let
   448   let
   446     (*FIXME need proper concept for reading locale statements*)
   449     (*FIXME need proper concept for reading locale statements*)
   447     fun subst_classtyvar (_, _) =
   450     fun subst_classtyvar (_, _) =
   448           TFree (AxClass.param_tyvarname, [])
   451           TFree (AxClass.param_tyvarname, [])
   449       | subst_classtyvar (v, sort) =
   452       | subst_classtyvar (v, sort) =
   450           error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort);
   453           error ("Sort constraint illegal in type class, for type variable " ^ v ^ "::" ^ Sign.string_of_sort thy sort);
   451     (*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I,
   454     (*val subst_classtyvars = Element.map_ctxt {name = I, var = I, term = I,
   452       typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
   455       typ = Term.map_type_tfree subst_classtyvar, fact = I, attrib = I};*)
       
   456     val other_consts = map (prep_param thy) raw_other_consts;
   453     val elems = fold_rev (fn Locale.Elem e => cons e | _ => I)
   457     val elems = fold_rev (fn Locale.Elem e => cons e | _ => I)
   454       raw_elems []; (*FIXME make include possible here?*)
   458       raw_elems []; (*FIXME make include possible here?*)
   455     val supclasses = map (prep_class thy) raw_supclasses;
   459     val supclasses = map (prep_class thy) raw_supclasses;
   456     val supsort =
   460     val supsort =
   457       supclasses
   461       supclasses
   503     thy
   507     thy
   504     |> add_locale (SOME (bname ^ "_pred")) bname supexpr ((*elems_constrains @*) elems)
   508     |> add_locale (SOME (bname ^ "_pred")) bname supexpr ((*elems_constrains @*) elems)
   505     |-> (fn name_locale => ProofContext.theory_result (
   509     |-> (fn name_locale => ProofContext.theory_result (
   506       `(fn thy => extract_params thy name_locale)
   510       `(fn thy => extract_params thy name_locale)
   507       #-> (fn (param_names, params) =>
   511       #-> (fn (param_names, params) =>
   508         axclass_params (bname, supsort) params (extract_assumes name_locale params)
   512         axclass_params (bname, supsort) params (extract_assumes name_locale params) other_consts
   509       #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
   513       #-> (fn (name_axclass, ((_, (ax_terms, ax_axioms)), consts)) =>
   510         `(fn thy => extract_axiom_names thy name_locale)
   514         `(fn thy => extract_axiom_names thy name_locale)
   511       #-> (fn axiom_names =>
   515       #-> (fn axiom_names =>
   512         add_class_data ((name_axclass, supclasses),
   516         add_class_data ((name_axclass, supclasses),
   513           (name_locale, map (fst o fst) params ~~ map fst consts,
   517           (name_locale, map (fst o fst) params ~~ map fst consts,
   514             map2 make_witness ax_terms ax_axioms, axiom_names))
   518             map2 make_witness ax_terms ax_axioms, axiom_names))
   515       #> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
   519       #> prove_interpretation ((ALLGOALS o ProofContext.fact_tac) ax_axioms)
   516           ((true, Logic.const_of_class bname), []) (Locale.Locale name_locale)
   520           ((!fully_qualified, Logic.const_of_class bname), []) (Locale.Locale name_locale)
   517           (mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts))
   521           (mk_instT name_axclass, mk_inst name_axclass param_names (map snd supconsts @ consts))
   518       #> pair name_axclass
   522       #> pair name_axclass
   519       )))))
   523       )))))
   520   end;
   524   end;
   521 
   525 
   522 in
   526 in
   523 
   527 
   524 val class_cmd = gen_class Locale.add_locale read_class;
   528 val class_cmd = gen_class Locale.add_locale read_class AxClass.read_param;
   525 val class = gen_class Locale.add_locale_i certify_class;
   529 val class = gen_class Locale.add_locale_i certify_class (K I);
   526 
   530 
   527 end; (*local*)
   531 end; (*local*)
   528 
   532 
   529 local
   533 local
   530 
   534