| author | wenzelm | 
| Thu, 14 Feb 2008 21:33:44 +0100 | |
| changeset 26069 | 321c4ca82923 | 
| parent 25863 | 5b4a8b1d0f88 | 
| child 26246 | e212c22f35c2 | 
| permissions | -rw-r--r-- | 
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 1 | (* Title: Pure/axclass.ML | 
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 2 | ID: $Id$ | 
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 3 | Author: Markus Wenzel, TU Muenchen | 
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 4 | |
| 24964 | 5 | Type classes defined as predicates, associated with a record of | 
| 6 | parameters. | |
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 7 | *) | 
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 8 | |
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 9 | signature AX_CLASS = | 
| 3938 | 10 | sig | 
| 24589 | 11 | val define_class: bstring * class list -> string list -> | 
| 12 | ((bstring * attribute list) * term list) list -> theory -> class * theory | |
| 13 | val add_classrel: thm -> theory -> theory | |
| 14 | val add_arity: thm -> theory -> theory | |
| 15 | val prove_classrel: class * class -> tactic -> theory -> theory | |
| 16 | val prove_arity: string * sort list * sort -> tactic -> theory -> theory | |
| 24964 | 17 | val get_info: theory -> class -> | 
| 18 |     {def: thm, intro: thm, axioms: thm list, params: (string * typ) list}
 | |
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 19 | val class_intros: theory -> thm list | 
| 20107 | 20 | val class_of_param: theory -> string -> class option | 
| 19405 | 21 | val cert_classrel: theory -> class * class -> class * class | 
| 22 | val read_classrel: theory -> xstring * xstring -> class * class | |
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 23 | val axiomatize_class: bstring * class list -> theory -> theory | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 24 | val axiomatize_class_cmd: bstring * xstring list -> theory -> theory | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 25 | val axiomatize_classrel: (class * class) list -> theory -> theory | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 26 | val axiomatize_classrel_cmd: (xstring * xstring) list -> theory -> theory | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 27 | val axiomatize_arity: arity -> theory -> theory | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 28 | val axiomatize_arity_cmd: xstring * string list * string -> theory -> theory | 
| 25486 | 29 | val instance_name: string * class -> string | 
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 30 | val declare_overloaded: string * typ -> theory -> term * theory | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 31 | val define_overloaded: string -> string * term -> theory -> thm * theory | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 32 | val inst_tyco_of: theory -> string * typ -> string option | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 33 | val unoverload: theory -> thm -> thm | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 34 | val overload: theory -> thm -> thm | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 35 | val unoverload_conv: theory -> conv | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 36 | val overload_conv: theory -> conv | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 37 | val unoverload_const: theory -> string * typ -> string | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 38 | val param_of_inst: theory -> string * string -> string | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 39 | val inst_of_param: theory -> string -> (string * string) option | 
| 19574 | 40 | type cache | 
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 41 | val of_sort: theory -> typ * sort -> cache -> thm list * cache (*exception Sorts.CLASS_ERROR*) | 
| 19574 | 42 | val cache: cache | 
| 25617 | 43 | val axiomsN: string | 
| 3938 | 44 | end; | 
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 45 | |
| 15801 | 46 | structure AxClass: AX_CLASS = | 
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 47 | struct | 
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 48 | |
| 19405 | 49 | (** theory data **) | 
| 423 | 50 | |
| 19405 | 51 | (* class parameters (canonical order) *) | 
| 423 | 52 | |
| 19405 | 53 | type param = string * class; | 
| 423 | 54 | |
| 19405 | 55 | fun add_param pp ((x, c): param) params = | 
| 56 | (case AList.lookup (op =) params x of | |
| 57 | NONE => (x, c) :: params | |
| 58 |   | SOME c' => error ("Duplicate class parameter " ^ quote x ^
 | |
| 59 | " for " ^ Pretty.string_of_sort pp [c] ^ | |
| 60 | (if c = c' then "" else " and " ^ Pretty.string_of_sort pp [c']))); | |
| 423 | 61 | |
| 19405 | 62 | fun merge_params _ ([], qs) = qs | 
| 63 | | merge_params pp (ps, qs) = | |
| 64 | fold_rev (fn q => if member (op =) ps q then I else add_param pp q) qs ps; | |
| 423 | 65 | |
| 66 | ||
| 19511 | 67 | (* axclasses *) | 
| 6379 | 68 | |
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 69 | val introN = "intro"; | 
| 19511 | 70 | val superN = "super"; | 
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 71 | val axiomsN = "axioms"; | 
| 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 72 | |
| 19392 | 73 | datatype axclass = AxClass of | 
| 19460 | 74 |  {def: thm,
 | 
| 19392 | 75 | intro: thm, | 
| 21463 
42dd50268c8b
completed class parameter handling in axclass.ML
 haftmann parents: 
20628diff
changeset | 76 | axioms: thm list, | 
| 21925 | 77 | params: (string * typ) list}; | 
| 19392 | 78 | |
| 19460 | 79 | type axclasses = axclass Symtab.table * param list; | 
| 19392 | 80 | |
| 21925 | 81 | fun make_axclass ((def, intro, axioms), params) = | 
| 82 |   AxClass {def = def, intro = intro, axioms = axioms, params = params};
 | |
| 19405 | 83 | |
| 84 | fun merge_axclasses pp ((tab1, params1), (tab2, params2)) : axclasses = | |
| 85 | (Symtab.merge (K true) (tab1, tab2), merge_params pp (params1, params2)); | |
| 86 | ||
| 19392 | 87 | |
| 88 | (* instances *) | |
| 89 | ||
| 20628 | 90 | val classrel_prefix = "classrel_"; | 
| 91 | val arity_prefix = "arity_"; | |
| 19511 | 92 | |
| 19574 | 93 | type instances = | 
| 94 | ((class * class) * thm) list * | |
| 95 | ((class * sort list) * thm) list Symtab.table; | |
| 6379 | 96 | |
| 19574 | 97 | fun merge_instances ((classrel1, arities1): instances, (classrel2, arities2)) = | 
| 98 | (merge (eq_fst op =) (classrel1, classrel2), | |
| 99 | Symtab.join (K (merge (eq_fst op =))) (arities1, arities2)); | |
| 19392 | 100 | |
| 101 | ||
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 102 | (* instance parameters *) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 103 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 104 | type inst_params = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 105 | (string * thm) Symtab.table Symtab.table | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 106 | (*constant name ~> type constructor ~> (constant name, equation)*) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 107 | * (string * string) Symtab.table; (*constant name ~> (constant name, type constructor)*) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 108 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 109 | fun merge_inst_params ((const_param1, param_const1), (const_param2, param_const2)) = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 110 | (Symtab.join (K (Symtab.merge (K true))) (const_param1, const_param2), | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 111 | Symtab.merge (K true) (param_const1, param_const2)); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 112 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 113 | |
| 19511 | 114 | (* setup data *) | 
| 19392 | 115 | |
| 116 | structure AxClassData = TheoryDataFun | |
| 22846 | 117 | ( | 
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 118 | type T = axclasses * (instances * inst_params); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 119 | val empty = ((Symtab.empty, []), (([], Symtab.empty), (Symtab.empty, Symtab.empty))); | 
| 19574 | 120 | val copy = I; | 
| 121 | val extend = I; | |
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 122 | fun merge pp ((axclasses1, (instances1, inst_params1)), (axclasses2, (instances2, inst_params2))) = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 123 | (merge_axclasses pp (axclasses1, axclasses2), | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 124 | (merge_instances (instances1, instances2), merge_inst_params (inst_params1, inst_params2))); | 
| 22846 | 125 | ); | 
| 6379 | 126 | |
| 127 | ||
| 19574 | 128 | (* maintain axclasses *) | 
| 19392 | 129 | |
| 19574 | 130 | val get_axclasses = #1 o AxClassData.get; | 
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 131 | val map_axclasses = AxClassData.map o apfst; | 
| 19574 | 132 | |
| 133 | val lookup_def = Symtab.lookup o #1 o get_axclasses; | |
| 6379 | 134 | |
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 135 | fun get_info thy c = | 
| 19511 | 136 | (case lookup_def thy c of | 
| 19392 | 137 | SOME (AxClass info) => info | 
| 21919 | 138 |   | NONE => error ("No such axclass: " ^ quote c));
 | 
| 6379 | 139 | |
| 19123 | 140 | fun class_intros thy = | 
| 19392 | 141 | let | 
| 142 | fun add_intro c = | |
| 19511 | 143 |       (case lookup_def thy c of SOME (AxClass {intro, ...}) => cons intro | _ => I);
 | 
| 21931 
314f9e2a442c
replaced Sign.classes by Sign.all_classes (topologically sorted);
 wenzelm parents: 
21925diff
changeset | 144 | val classes = Sign.all_classes thy; | 
| 19392 | 145 | in map (Thm.class_triv thy) classes @ fold add_intro classes [] end; | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 146 | |
| 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 147 | |
| 19460 | 148 | fun get_params thy pred = | 
| 19574 | 149 | let val params = #2 (get_axclasses thy); | 
| 19460 | 150 | in fold (fn (x, c) => if pred c then cons x else I) params [] end; | 
| 151 | ||
| 152 | fun params_of thy c = get_params thy (fn c' => c' = c); | |
| 153 | fun all_params_of thy S = get_params thy (fn c => Sign.subsort thy (S, [c])); | |
| 154 | ||
| 24964 | 155 | fun class_of_param thy = AList.lookup (op =) (#2 (get_axclasses thy)); | 
| 19460 | 156 | |
| 21919 | 157 | |
| 19511 | 158 | (* maintain instances *) | 
| 19503 | 159 | |
| 25486 | 160 | fun instance_name (a, c) = NameSpace.base c ^ "_" ^ NameSpace.base a; | 
| 161 | ||
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 162 | val get_instances = #1 o #2 o AxClassData.get; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 163 | val map_instances = AxClassData.map o apsnd o apfst; | 
| 19528 | 164 | |
| 165 | ||
| 19574 | 166 | fun the_classrel thy (c1, c2) = | 
| 167 | (case AList.lookup (op =) (#1 (get_instances thy)) (c1, c2) of | |
| 168 | SOME th => Thm.transfer thy th | |
| 24920 | 169 |   | NONE => error ("Unproven class relation " ^
 | 
| 170 | Syntax.string_of_classrel (ProofContext.init thy) [c1, c2])); | |
| 19574 | 171 | |
| 172 | fun put_classrel arg = map_instances (fn (classrel, arities) => | |
| 173 | (insert (eq_fst op =) arg classrel, arities)); | |
| 19503 | 174 | |
| 175 | ||
| 19574 | 176 | fun the_arity thy a (c, Ss) = | 
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 177 | (case AList.lookup (op =) (Symtab.lookup_list (#2 (get_instances thy)) a) (c, Ss) of | 
| 19574 | 178 | SOME th => Thm.transfer thy th | 
| 24920 | 179 |   | NONE => error ("Unproven type arity " ^
 | 
| 180 | Syntax.string_of_arity (ProofContext.init thy) (a, Ss, [c]))); | |
| 19503 | 181 | |
| 19574 | 182 | fun put_arity ((t, Ss, c), th) = map_instances (fn (classrel, arities) => | 
| 183 | (classrel, arities |> Symtab.insert_list (eq_fst op =) (t, ((c, Ss), th)))); | |
| 19503 | 184 | |
| 185 | ||
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 186 | (* maintain instance parameters *) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 187 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 188 | val get_inst_params = #2 o #2 o AxClassData.get; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 189 | val map_inst_params = AxClassData.map o apsnd o apsnd; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 190 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 191 | fun get_inst_param thy (c, tyco) = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 192 | (the o Symtab.lookup ((the o Symtab.lookup (fst (get_inst_params thy))) c)) tyco; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 193 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 194 | fun add_inst_param (c, tyco) inst = (map_inst_params o apfst | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 195 | o Symtab.map_default (c, Symtab.empty)) (Symtab.update_new (tyco, inst)) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 196 | #> (map_inst_params o apsnd) (Symtab.update_new (fst inst, (c, tyco))); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 197 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 198 | val inst_of_param = Symtab.lookup o snd o get_inst_params; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 199 | val param_of_inst = fst oo get_inst_param; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 200 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 201 | fun inst_thms thy = (Symtab.fold (Symtab.fold (cons o snd o snd) o snd) o fst) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 202 | (get_inst_params thy) []; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 203 | |
| 25863 | 204 | fun inst_tyco_of thy = try (fst o dest_Type o the_single o Sign.const_typargs thy); | 
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 205 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 206 | fun unoverload thy = MetaSimplifier.simplify true (inst_thms thy); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 207 | fun overload thy = MetaSimplifier.simplify true (map Thm.symmetric (inst_thms thy)); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 208 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 209 | fun unoverload_conv thy = MetaSimplifier.rewrite true (inst_thms thy); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 210 | fun overload_conv thy = MetaSimplifier.rewrite true (map Thm.symmetric (inst_thms thy)); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 211 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 212 | fun unoverload_const thy (c_ty as (c, _)) = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 213 | case class_of_param thy c | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 214 | of SOME class => (case inst_tyco_of thy c_ty | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 215 | of SOME tyco => try (param_of_inst thy) (c, tyco) |> the_default c | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 216 | | NONE => c) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 217 | | NONE => c; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 218 | |
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 219 | |
| 19511 | 220 | (** instances **) | 
| 19418 
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
 wenzelm parents: 
19405diff
changeset | 221 | |
| 
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
 wenzelm parents: 
19405diff
changeset | 222 | (* class relations *) | 
| 
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
 wenzelm parents: 
19405diff
changeset | 223 | |
| 19405 | 224 | fun cert_classrel thy raw_rel = | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 225 | let | 
| 19405 | 226 | val (c1, c2) = pairself (Sign.certify_class thy) raw_rel; | 
| 24271 | 227 | val _ = Sign.primitive_classrel (c1, c2) (Theory.copy thy); | 
| 19405 | 228 | val _ = | 
| 19460 | 229 | (case subtract (op =) (all_params_of thy [c1]) (all_params_of thy [c2]) of | 
| 19405 | 230 | [] => () | 
| 231 |       | xs => raise TYPE ("Class " ^ Sign.string_of_sort thy [c1] ^ " lacks parameter(s) " ^
 | |
| 232 | commas_quote xs ^ " of " ^ Sign.string_of_sort thy [c2], [], [])); | |
| 233 | in (c1, c2) end; | |
| 234 | ||
| 235 | fun read_classrel thy raw_rel = | |
| 236 | cert_classrel thy (pairself (Sign.read_class thy) raw_rel) | |
| 237 | handle TYPE (msg, _, _) => error msg; | |
| 238 | ||
| 239 | ||
| 240 | (* primitive rules *) | |
| 241 | ||
| 242 | fun add_classrel th thy = | |
| 243 | let | |
| 244 |     fun err () = raise THM ("add_classrel: malformed class relation", 0, [th]);
 | |
| 22691 | 245 | val prop = Thm.plain_prop_of (Thm.transfer thy th); | 
| 19405 | 246 | val rel = Logic.dest_classrel prop handle TERM _ => err (); | 
| 247 | val (c1, c2) = cert_classrel thy rel handle TYPE _ => err (); | |
| 19574 | 248 | in | 
| 249 | thy | |
| 250 | |> Sign.primitive_classrel (c1, c2) | |
| 251 | |> put_classrel ((c1, c2), Drule.unconstrainTs th) | |
| 252 | end; | |
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 253 | |
| 19405 | 254 | fun add_arity th thy = | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 255 | let | 
| 19528 | 256 |     fun err () = raise THM ("add_arity: malformed type arity", 0, [th]);
 | 
| 22691 | 257 | val prop = Thm.plain_prop_of (Thm.transfer thy th); | 
| 19528 | 258 | val (t, Ss, c) = Logic.dest_arity prop handle TERM _ => err (); | 
| 24731 | 259 | val _ = map (Sign.certify_sort thy) Ss = Ss orelse err (); | 
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 260 | (*FIXME turn this into a mere check as soon as "attach" has disappeared*) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 261 | val missing_params = Sign.complete_sort thy [c] | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 262 |       |> maps (these o Option.map (fn AxClass { params, ... } => params) o lookup_def thy)
 | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 263 | |> filter_out (fn (p, _) => can (get_inst_param thy) (p, t)); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 264 | fun declare_missing (p, T0) thy = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 265 | let | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 266 | val name_inst = instance_name (t, c) ^ "_inst"; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 267 | val p' = NameSpace.base p ^ "_" ^ NameSpace.base t; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 268 | val vs = Name.names Name.context Name.aT (replicate (Sign.arity_number thy t) []); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 269 | val T = map_atyps (fn _ => Type (t, map TFree vs)) T0; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 270 | in | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 271 | thy | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 272 | |> Sign.sticky_prefix name_inst | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 273 | |> Sign.no_base_names | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 274 | |> Sign.declare_const [] (p', T, NoSyn) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 275 | |-> (fn const' as Const (p'', _) => Thm.add_def false true | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 276 | (Thm.def_name p', Logic.mk_equals (const', Const (p, T))) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 277 | #>> Thm.varifyT | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 278 | #-> (fn thm => add_inst_param (p, t) (p'', Thm.symmetric thm) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 279 | #> PureThy.note Thm.internalK (p', thm) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 280 | #> snd | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 281 | #> Sign.restore_naming thy)) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 282 | end; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 283 | |
| 19574 | 284 | in | 
| 285 | thy | |
| 286 | |> Sign.primitive_arity (t, Ss, [c]) | |
| 287 | |> put_arity ((t, Ss, c), Drule.unconstrainTs th) | |
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 288 | |> fold declare_missing missing_params | 
| 19574 | 289 | end; | 
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 290 | |
| 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 291 | |
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 292 | (* tactical proofs *) | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 293 | |
| 19405 | 294 | fun prove_classrel raw_rel tac thy = | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 295 | let | 
| 24920 | 296 | val ctxt = ProofContext.init thy; | 
| 19405 | 297 | val (c1, c2) = cert_classrel thy raw_rel; | 
| 24920 | 298 | val th = Goal.prove ctxt [] [] | 
| 20049 | 299 | (Logic.mk_classrel (c1, c2)) (fn _ => tac) handle ERROR msg => | 
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 300 |       cat_error msg ("The error(s) above occurred while trying to prove class relation " ^
 | 
| 24920 | 301 | quote (Syntax.string_of_classrel ctxt [c1, c2])); | 
| 19511 | 302 | in | 
| 303 | thy | |
| 20628 | 304 | |> PureThy.add_thms [((prefix classrel_prefix (Logic.name_classrel (c1, c2)), th), [])] | 
| 19511 | 305 | |-> (fn [th'] => add_classrel th') | 
| 306 | end; | |
| 404 
dd3d3d6467db
axiomatic type class 'package' for Pure (alpha version);
 wenzelm parents: diff
changeset | 307 | |
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 308 | fun prove_arity raw_arity tac thy = | 
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 309 | let | 
| 24920 | 310 | val ctxt = ProofContext.init thy; | 
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 311 | val arity = Sign.cert_arity thy raw_arity; | 
| 20628 | 312 | val names = map (prefix arity_prefix) (Logic.name_arities arity); | 
| 19405 | 313 | val props = Logic.mk_arities arity; | 
| 24920 | 314 | val ths = Goal.prove_multi ctxt [] [] props | 
| 21687 | 315 | (fn _ => Goal.precise_conjunction_tac (length props) 1 THEN tac) handle ERROR msg => | 
| 19243 
5dcb899a8486
moved read_class, read/cert_classrel/arity to sign.ML;
 wenzelm parents: 
19134diff
changeset | 316 |         cat_error msg ("The error(s) above occurred while trying to prove type arity " ^
 | 
| 24920 | 317 | quote (Syntax.string_of_arity ctxt arity)); | 
| 19511 | 318 | in | 
| 319 | thy | |
| 20628 | 320 | |> PureThy.add_thms (map (rpair []) (names ~~ ths)) | 
| 19511 | 321 | |-> fold add_arity | 
| 322 | end; | |
| 323 | ||
| 324 | ||
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 325 | (* instance parameters and overloaded definitions *) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 326 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 327 | (* declaration and definition of instances of overloaded constants *) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 328 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 329 | fun declare_overloaded (c, T) thy = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 330 | let | 
| 25605 | 331 | val class = case class_of_param thy c | 
| 332 | of SOME class => class | |
| 333 |       | NONE => error ("Not a class parameter: " ^ quote c);
 | |
| 334 | val tyco = case inst_tyco_of thy (c, T) | |
| 335 | of SOME tyco => tyco | |
| 336 |       | NONE => error ("Illegal type for instantiation of class parameter: "
 | |
| 337 | ^ quote (c ^ " :: " ^ Sign.string_of_typ thy T)); | |
| 25597 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 338 | val name_inst = instance_name (tyco, class) ^ "_inst"; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 339 | val c' = NameSpace.base c ^ "_" ^ NameSpace.base tyco; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 340 | val T' = Type.strip_sorts T; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 341 | in | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 342 | thy | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 343 | |> Sign.sticky_prefix name_inst | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 344 | |> Sign.no_base_names | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 345 | |> Sign.declare_const [] (c', T', NoSyn) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 346 | |-> (fn const' as Const (c'', _) => Thm.add_def false true | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 347 | (Thm.def_name c', Logic.mk_equals (Const (c, T'), const')) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 348 | #>> Thm.varifyT | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 349 | #-> (fn thm => add_inst_param (c, tyco) (c'', thm) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 350 | #> PureThy.note Thm.internalK (c', thm) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 351 | #> snd | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 352 | #> Sign.restore_naming thy | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 353 | #> pair (Const (c, T)))) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 354 | end; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 355 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 356 | fun define_overloaded name (c, t) thy = | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 357 | let | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 358 | val T = Term.fastype_of t; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 359 | val SOME tyco = inst_tyco_of thy (c, T); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 360 | val (c', eq) = get_inst_param thy (c, tyco); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 361 | val prop = Logic.mk_equals (Const (c', T), t); | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 362 | val name' = Thm.def_name_optional | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 363 | (NameSpace.base c ^ "_" ^ NameSpace.base tyco) name; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 364 | in | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 365 | thy | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 366 | |> Thm.add_def false false (name', prop) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 367 | |>> (fn thm => Drule.transitive_thm OF [eq, thm]) | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 368 | end; | 
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 369 | |
| 
34860182b250
moved instance parameter management from class.ML to axclass.ML
 haftmann parents: 
25486diff
changeset | 370 | |
| 19511 | 371 | |
| 372 | (** class definitions **) | |
| 373 | ||
| 23421 | 374 | fun split_defined n eq = | 
| 375 | let | |
| 376 | val intro = | |
| 377 | (eq RS Drule.equal_elim_rule2) | |
| 378 | |> Conjunction.curry_balanced n | |
| 379 | |> n = 0 ? Thm.eq_assumption 1; | |
| 380 | val dests = | |
| 381 | if n = 0 then [] | |
| 382 | else | |
| 383 | (eq RS Drule.equal_elim_rule1) | |
| 384 | |> BalancedTree.dest (fn th => | |
| 385 | (th RS Conjunction.conjunctionD1, th RS Conjunction.conjunctionD2)) n; | |
| 386 | in (intro, dests) end; | |
| 387 | ||
| 24964 | 388 | fun define_class (bclass, raw_super) raw_params raw_specs thy = | 
| 19511 | 389 | let | 
| 390 | val ctxt = ProofContext.init thy; | |
| 24964 | 391 | val pp = Syntax.pp ctxt; | 
| 19511 | 392 | |
| 393 | ||
| 24964 | 394 | (* class *) | 
| 19511 | 395 | |
| 396 | val bconst = Logic.const_of_class bclass; | |
| 397 | val class = Sign.full_name thy bclass; | |
| 24731 | 398 | val super = Sign.minimize_sort thy (Sign.certify_sort thy raw_super); | 
| 19511 | 399 | |
| 24964 | 400 | fun check_constraint (a, S) = | 
| 401 | if Sign.subsort thy (super, S) then () | |
| 402 |       else error ("Sort constraint of type variable " ^
 | |
| 403 | setmp show_sorts true (Pretty.string_of_typ pp) (TFree (a, S)) ^ | |
| 404 | " needs to be weaker than " ^ Pretty.string_of_sort pp super); | |
| 405 | ||
| 406 | ||
| 407 | (* params *) | |
| 408 | ||
| 409 | val params = raw_params |> map (fn p => | |
| 410 | let | |
| 411 | val T = Sign.the_const_type thy p; | |
| 412 | val _ = | |
| 413 | (case Term.add_tvarsT T [] of | |
| 414 | [((a, _), S)] => check_constraint (a, S) | |
| 415 |           | _ => error ("Exactly one type variable expected in class parameter " ^ quote p));
 | |
| 416 | val T' = Term.map_type_tvar (fn _ => TFree (Name.aT, [class])) T; | |
| 417 | in (p, T') end); | |
| 418 | ||
| 419 | ||
| 420 | (* axioms *) | |
| 421 | ||
| 19511 | 422 | fun prep_axiom t = | 
| 423 | (case Term.add_tfrees t [] of | |
| 24964 | 424 | [(a, S)] => check_constraint (a, S) | 
| 425 | | [] => () | |
| 426 |       | _ => error ("Multiple type variables in class axiom:\n" ^ Pretty.string_of_term pp t);
 | |
| 427 | t | |
| 428 | |> Term.map_types (Term.map_atyps (fn TFree _ => Term.aT [] | U => U)) | |
| 429 | |> Logic.close_form); | |
| 19511 | 430 | |
| 24681 | 431 | val axiomss = map (map (prep_axiom o Sign.cert_prop thy) o snd) raw_specs; | 
| 22745 | 432 | val name_atts = map fst raw_specs; | 
| 19511 | 433 | |
| 434 | ||
| 435 | (* definition *) | |
| 436 | ||
| 437 | val conjs = map (curry Logic.mk_inclass (Term.aT [])) super @ flat axiomss; | |
| 438 | val class_eq = | |
| 23421 | 439 | Logic.mk_equals (Logic.mk_inclass (Term.aT [], class), Logic.mk_conjunction_balanced conjs); | 
| 19511 | 440 | |
| 441 | val ([def], def_thy) = | |
| 442 | thy | |
| 443 | |> Sign.primitive_class (bclass, super) | |
| 444 | |> PureThy.add_defs_i false [((Thm.def_name bconst, class_eq), [])]; | |
| 445 | val (raw_intro, (raw_classrel, raw_axioms)) = | |
| 23421 | 446 | split_defined (length conjs) def ||> chop (length super); | 
| 19392 | 447 | |
| 19418 
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
 wenzelm parents: 
19405diff
changeset | 448 | |
| 19511 | 449 | (* facts *) | 
| 450 | ||
| 451 | val class_triv = Thm.class_triv def_thy class; | |
| 452 | val ([(_, [intro]), (_, classrel), (_, axioms)], facts_thy) = | |
| 453 | def_thy | |
| 454 | |> PureThy.note_thmss_qualified "" bconst | |
| 455 | [((introN, []), [([Drule.standard raw_intro], [])]), | |
| 456 | ((superN, []), [(map Drule.standard raw_classrel, [])]), | |
| 457 | ((axiomsN, []), [(map (fn th => Drule.standard (class_triv RS th)) raw_axioms, [])])]; | |
| 458 | ||
| 24847 | 459 | |
| 19511 | 460 | (* result *) | 
| 461 | ||
| 24964 | 462 | val axclass = make_axclass ((def, intro, axioms), params); | 
| 19511 | 463 | val result_thy = | 
| 464 | facts_thy | |
| 19574 | 465 | |> fold put_classrel (map (pair class) super ~~ classrel) | 
| 19511 | 466 | |> Sign.add_path bconst | 
| 467 | |> PureThy.note_thmss_i "" (name_atts ~~ map Thm.simple_fact (unflat axiomss axioms)) |> snd | |
| 468 | |> Sign.restore_naming facts_thy | |
| 19574 | 469 | |> map_axclasses (fn (axclasses, parameters) => | 
| 21919 | 470 | (Symtab.update (class, axclass) axclasses, | 
| 24964 | 471 | fold (fn (x, _) => add_param pp (x, class)) params parameters)); | 
| 19511 | 472 | |
| 473 | in (class, result_thy) end; | |
| 474 | ||
| 475 | ||
| 19531 | 476 | |
| 19511 | 477 | (** axiomatizations **) | 
| 478 | ||
| 479 | local | |
| 480 | ||
| 20628 | 481 | fun axiomatize prep mk name add raw_args thy = | 
| 482 | let | |
| 483 | val args = prep thy raw_args; | |
| 484 | val specs = mk args; | |
| 485 | val names = name args; | |
| 486 | in thy |> PureThy.add_axioms_i (map (rpair []) (names ~~ specs)) |-> fold add end; | |
| 19511 | 487 | |
| 488 | fun ax_classrel prep = | |
| 20628 | 489 | axiomatize (map o prep) (map Logic.mk_classrel) | 
| 490 | (map (prefix classrel_prefix o Logic.name_classrel)) add_classrel; | |
| 19511 | 491 | |
| 492 | fun ax_arity prep = | |
| 20628 | 493 | axiomatize prep Logic.mk_arities | 
| 494 | (map (prefix arity_prefix) o Logic.name_arities) add_arity; | |
| 19511 | 495 | |
| 19706 | 496 | fun class_const c = | 
| 497 | (Logic.const_of_class c, Term.itselfT (Term.aT []) --> propT); | |
| 498 | ||
| 19511 | 499 | fun ax_class prep_class prep_classrel (bclass, raw_super) thy = | 
| 500 | let | |
| 501 | val class = Sign.full_name thy bclass; | |
| 24731 | 502 | val super = map (prep_class thy) raw_super |> Sign.minimize_sort thy; | 
| 19511 | 503 | in | 
| 504 | thy | |
| 505 | |> Sign.primitive_class (bclass, super) | |
| 506 | |> ax_classrel prep_classrel (map (fn c => (class, c)) super) | |
| 19706 | 507 | |> Theory.add_deps "" (class_const class) (map class_const super) | 
| 19511 | 508 | end; | 
| 509 | ||
| 510 | in | |
| 511 | ||
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 512 | val axiomatize_class = ax_class Sign.certify_class cert_classrel; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 513 | val axiomatize_class_cmd = ax_class Sign.read_class read_classrel; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 514 | val axiomatize_classrel = ax_classrel cert_classrel; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 515 | val axiomatize_classrel_cmd = ax_classrel read_classrel; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 516 | val axiomatize_arity = ax_arity Sign.cert_arity; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 517 | val axiomatize_arity_cmd = ax_arity Sign.read_arity; | 
| 19511 | 518 | |
| 519 | end; | |
| 520 | ||
| 521 | ||
| 522 | ||
| 523 | (** explicit derivations -- cached **) | |
| 524 | ||
| 19574 | 525 | datatype cache = Types of (class * thm) list Typtab.table; | 
| 526 | ||
| 19511 | 527 | local | 
| 19503 | 528 | |
| 19574 | 529 | fun lookup_type (Types cache) = AList.lookup (op =) o Typtab.lookup_list cache; | 
| 530 | fun insert_type T der (Types cache) = Types (Typtab.insert_list (eq_fst op =) (T, der) cache); | |
| 19503 | 531 | |
| 19522 | 532 | fun derive_type _ (_, []) = [] | 
| 533 | | derive_type thy (typ, sort) = | |
| 534 | let | |
| 19528 | 535 | val vars = Term.fold_atyps | 
| 19522 | 536 | (fn T as TFree (_, S) => insert (eq_fst op =) (T, S) | 
| 537 | | T as TVar (_, S) => insert (eq_fst op =) (T, S) | |
| 19528 | 538 | | _ => I) typ []; | 
| 19574 | 539 | val hyps = vars | 
| 540 | |> map (fn (T, S) => (T, Drule.sort_triv thy (T, S) ~~ S)); | |
| 19642 | 541 | val ths = (typ, sort) |> Sorts.of_sort_derivation (Sign.pp thy) (Sign.classes_of thy) | 
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22385diff
changeset | 542 |            {class_relation =
 | 
| 19574 | 543 | fn (th, c1) => fn c2 => th RS the_classrel thy (c1, c2), | 
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22385diff
changeset | 544 | type_constructor = | 
| 19574 | 545 | fn a => fn dom => fn c => | 
| 546 | let val Ss = map (map snd) dom and ths = maps (map fst) dom | |
| 547 | in ths MRS the_arity thy a (c, Ss) end, | |
| 22570 
f166a5416b3f
renamed of_sort_derivation record fields (avoid clash with Alice keywords);
 wenzelm parents: 
22385diff
changeset | 548 | type_variable = | 
| 19574 | 549 | the_default [] o AList.lookup (op =) hyps}; | 
| 550 | in ths end; | |
| 19503 | 551 | |
| 19511 | 552 | in | 
| 553 | ||
| 19574 | 554 | fun of_sort thy (typ, sort) cache = | 
| 555 | let | |
| 556 | val sort' = filter (is_none o lookup_type cache typ) sort; | |
| 557 | val ths' = derive_type thy (typ, sort') | |
| 558 |       handle ERROR msg => cat_error msg ("The error(s) above occurred for sort derivation: " ^
 | |
| 559 | Sign.string_of_typ thy typ ^ " :: " ^ Sign.string_of_sort thy sort'); | |
| 560 | val cache' = cache |> fold (insert_type typ) (sort' ~~ ths'); | |
| 561 | val ths = | |
| 562 | sort |> map (fn c => | |
| 563 | Goal.finish (the (lookup_type cache' typ c) RS | |
| 564 | Goal.init (Thm.cterm_of thy (Logic.mk_inclass (typ, c)))) | |
| 20260 | 565 | |> Thm.adjust_maxidx_thm ~1); | 
| 19574 | 566 | in (ths, cache') end; | 
| 19503 | 567 | |
| 19511 | 568 | end; | 
| 19418 
03b01c9314fc
reworded add_axclass(_i): canonical specification format,
 wenzelm parents: 
19405diff
changeset | 569 | |
| 24929 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 570 | val cache = Types Typtab.empty; | 
| 
408becab067e
renamed AxClass.get_definition to AxClass.get_info (again);
 wenzelm parents: 
24920diff
changeset | 571 | |
| 15876 
a67343c6ab2a
sane interfaces for tactical instance proofs (do not expand defs of theory, proper handling of sort instances);
 wenzelm parents: 
15853diff
changeset | 572 | end; |