author  haftmann 
Tue, 11 Dec 2007 10:23:10 +0100  
changeset 25603  4b7a58fc168c 
parent 25597  34860182b250 
child 25618  01f20279fea1 
permissions  rwrr 
24218  1 
(* Title: Pure/Isar/class.ML 
2 
ID: $Id$ 

3 
Author: Florian Haftmann, TU Muenchen 

4 

5 
Type classes derived from primitive axclasses and locales. 

6 
*) 

7 

8 
signature CLASS = 

9 
sig 

25462  10 
(*classes*) 
25002  11 
val class: bstring > class list > Element.context_i Locale.element list 
24218  12 
> string list > theory > string * Proof.context 
25002  13 
val class_cmd: bstring > xstring list > Element.context Locale.element list 
24589  14 
> xstring list > theory > string * Proof.context 
25485  15 

25311  16 
val init: class > theory > Proof.context 
25603  17 
val declare: string > Markup.property list 
25104  18 
> (string * mixfix) * term > theory > theory 
25603  19 
val abbrev: string > Syntax.mode > Markup.property list 
25104  20 
> (string * mixfix) * term > theory > theory 
25083  21 
val refresh_syntax: class > Proof.context > Proof.context 
25485  22 

24589  23 
val intro_classes_tac: thm list > tactic 
24 
val default_intro_classes_tac: thm list > tactic 

25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

25 
val prove_subclass: class * class > thm list > Proof.context 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

26 
> theory > theory 
25485  27 

28 
val class_prefix: string > string 

29 
val is_class: theory > class > bool 

30 
val these_params: theory > sort > (string * (string * typ)) list 

24589  31 
val print_classes: theory > unit 
24423
ae9cd0e92423
overloaded definitions accompanied by explicit constants
haftmann
parents:
24304
diff
changeset

32 

25462  33 
(*instances*) 
25536  34 
val init_instantiation: string list * sort list * sort > theory > local_theory 
25485  35 
val instantiation_instance: (local_theory > local_theory) > local_theory > Proof.state 
36 
val prove_instantiation_instance: (Proof.context > tactic) > local_theory > local_theory 

37 
val conclude_instantiation: local_theory > local_theory 

25603  38 
val instantiation_param: local_theory > string > string option 
25485  39 
val confirm_declaration: string > local_theory > local_theory 
25603  40 
val pretty_instantiation: local_theory > Pretty.T 
25485  41 

25462  42 
(*old axclass layer*) 
43 
val axclass_cmd: bstring * xstring list 

44 
> ((bstring * Attrib.src list) * string list) list 

45 
> theory > class * theory 

46 
val classrel_cmd: xstring * xstring > theory > Proof.state 

47 

48 
(*old instance layer*) 

25536  49 
val instance_arity: (theory > theory) > arity > theory > Proof.state 
50 
val instance_arity_cmd: bstring * xstring list * xstring > theory > Proof.state 

24218  51 
end; 
52 

53 
structure Class : CLASS = 

54 
struct 

55 

56 
(** auxiliary **) 

57 

25062  58 
val classN = "class"; 
59 
val introN = "intro"; 

60 

25002  61 
fun prove_interpretation tac prfx_atts expr inst = 
62 
Locale.interpretation_i I prfx_atts expr inst 

24589  63 
#> Proof.global_terminal_proof 
64 
(Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) 

65 
#> ProofContext.theory_of; 

66 

25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

67 
fun prove_interpretation_in tac after_qed (name, expr) = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

68 
Locale.interpretation_in_locale 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

69 
(ProofContext.theory after_qed) (name, expr) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

70 
#> Proof.global_terminal_proof 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

71 
(Method.Basic (K (Method.SIMPLE_METHOD tac), Position.none), NONE) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

72 
#> ProofContext.theory_of; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

73 

25020  74 
fun OF_LAST thm1 thm2 = thm1 RSN (Thm.nprems_of thm2, thm2); 
24589  75 

76 
fun strip_all_ofclass thy sort = 

77 
let 

24847  78 
val typ = TVar ((Name.aT, 0), sort); 
24589  79 
fun prem_inclass t = 
80 
case Logic.strip_imp_prems t 

81 
of ofcls :: _ => try Logic.dest_inclass ofcls 

82 
 [] => NONE; 

83 
fun strip_ofclass class thm = 

84 
thm OF (fst o AxClass.of_sort thy (typ, [class])) AxClass.cache; 

85 
fun strip thm = case (prem_inclass o Thm.prop_of) thm 

86 
of SOME (_, class) => thm > strip_ofclass class > strip 

87 
 NONE => thm; 

88 
in strip end; 

89 

25038  90 
fun get_remove_global_constraint c thy = 
91 
let 

92 
val ty = Sign.the_const_constraint thy c; 

93 
in 

94 
thy 

95 
> Sign.add_const_constraint (c, NONE) 

96 
> pair (c, Logic.unvarifyT ty) 

97 
end; 

98 

24589  99 

25485  100 
(** primitive axclass and instance commands **) 
24589  101 

24218  102 
fun axclass_cmd (class, raw_superclasses) raw_specs thy = 
103 
let 

104 
val ctxt = ProofContext.init thy; 

105 
val superclasses = map (Sign.read_class thy) raw_superclasses; 

24589  106 
val name_atts = map ((apsnd o map) (Attrib.attribute thy) o fst) 
107 
raw_specs; 

108 
val axiomss = ProofContext.read_propp (ctxt, map (map (rpair []) o snd) 

109 
raw_specs) 

24218  110 
> snd 
111 
> (map o map) fst; 

24589  112 
in 
113 
AxClass.define_class (class, superclasses) [] 

114 
(name_atts ~~ axiomss) thy 

115 
end; 

24218  116 

117 
local 

118 

119 
fun gen_instance mk_prop add_thm after_qed insts thy = 

120 
let 

121 
fun after_qed' results = 

122 
ProofContext.theory ((fold o fold) add_thm results #> after_qed); 

123 
in 

124 
thy 

125 
> ProofContext.init 

24589  126 
> Proof.theorem_i NONE after_qed' ((map (fn t => [(t, [])]) 
25536  127 
o mk_prop thy) insts) 
24218  128 
end; 
129 

130 
in 

131 

24589  132 
val instance_arity = 
24218  133 
gen_instance (Logic.mk_arities oo Sign.cert_arity) AxClass.add_arity; 
25502  134 
val instance_arity_cmd = 
135 
gen_instance (Logic.mk_arities oo Sign.read_arity) AxClass.add_arity I; 

24589  136 
val classrel = 
25536  137 
gen_instance (single oo (Logic.mk_classrel oo AxClass.cert_classrel)) AxClass.add_classrel I; 
24589  138 
val classrel_cmd = 
25536  139 
gen_instance (single oo (Logic.mk_classrel oo AxClass.read_classrel)) AxClass.add_classrel I; 
24218  140 

141 
end; (*local*) 

142 

143 

24589  144 
(** class data **) 
24218  145 

146 
datatype class_data = ClassData of { 

147 
consts: (string * string) list 

24836  148 
(*locale parameter ~> constant name*), 
25062  149 
base_sort: sort, 
25083  150 
inst: term option list 
151 
(*canonical interpretation*), 

25062  152 
morphism: morphism, 
153 
(*partial morphism of canonical interpretation*) 

24657  154 
intro: thm, 
155 
defs: thm list, 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

156 
operations: (string * (class * (typ * term))) list 
24657  157 
}; 
24218  158 

24657  159 
fun rep_class_data (ClassData d) = d; 
25062  160 
fun mk_class_data ((consts, base_sort, inst, morphism, intro), 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

161 
(defs, operations)) = 
25062  162 
ClassData { consts = consts, base_sort = base_sort, inst = inst, 
163 
morphism = morphism, intro = intro, defs = defs, 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

164 
operations = operations }; 
25062  165 
fun map_class_data f (ClassData { consts, base_sort, inst, morphism, intro, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

166 
defs, operations }) = 
25062  167 
mk_class_data (f ((consts, base_sort, inst, morphism, intro), 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

168 
(defs, operations))); 
25038  169 
fun merge_class_data _ (ClassData { consts = consts, 
25062  170 
base_sort = base_sort, inst = inst, morphism = morphism, intro = intro, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

171 
defs = defs1, operations = operations1 }, 
25062  172 
ClassData { consts = _, base_sort = _, inst = _, morphism = _, intro = _, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

173 
defs = defs2, operations = operations2 }) = 
25062  174 
mk_class_data ((consts, base_sort, inst, morphism, intro), 
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset

175 
(Thm.merge_thms (defs1, defs2), 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

176 
AList.merge (op =) (K true) (operations1, operations2))); 
24218  177 

178 
structure ClassData = TheoryDataFun 

179 
( 

25038  180 
type T = class_data Graph.T 
181 
val empty = Graph.empty; 

24218  182 
val copy = I; 
183 
val extend = I; 

25038  184 
fun merge _ = Graph.join merge_class_data; 
24218  185 
); 
186 

187 

188 
(* queries *) 

189 

25038  190 
val lookup_class_data = Option.map rep_class_data oo try o Graph.get_node o ClassData.get; 
24218  191 

24589  192 
fun the_class_data thy class = case lookup_class_data thy class 
25020  193 
of NONE => error ("Undeclared class " ^ quote class) 
24589  194 
 SOME data => data; 
24218  195 

25038  196 
val is_class = is_some oo lookup_class_data; 
197 

198 
val ancestry = Graph.all_succs o ClassData.get; 

24218  199 

25002  200 
fun these_params thy = 
24218  201 
let 
202 
fun params class = 

203 
let 

24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset

204 
val const_typs = (#params o AxClass.get_info thy) class; 
24657  205 
val const_names = (#consts o the_class_data thy) class; 
24218  206 
in 
207 
(map o apsnd) (fn c => (c, (the o AList.lookup (op =) const_typs) c)) const_names 

208 
end; 

209 
in maps params o ancestry thy end; 

210 

24657  211 
fun these_defs thy = maps (these o Option.map #defs o lookup_class_data thy) o ancestry thy; 
24218  212 

25062  213 
fun morphism thy = #morphism o the_class_data thy; 
214 

24218  215 
fun these_intros thy = 
24657  216 
Graph.fold (fn (_, (data, _)) => insert Thm.eq_thm ((#intro o rep_class_data) data)) 
25038  217 
(ClassData.get thy) []; 
24218  218 

24836  219 
fun these_operations thy = 
220 
maps (#operations o the_class_data thy) o ancestry thy; 

24657  221 

24218  222 
fun print_classes thy = 
223 
let 

24920  224 
val ctxt = ProofContext.init thy; 
24218  225 
val algebra = Sign.classes_of thy; 
226 
val arities = 

227 
Symtab.empty 

228 
> Symtab.fold (fn (tyco, arities) => fold (fn (class, _) => 

229 
Symtab.map_default (class, []) (insert (op =) tyco)) arities) 

230 
((#arities o Sorts.rep_algebra) algebra); 

231 
val the_arities = these o Symtab.lookup arities; 

232 
fun mk_arity class tyco = 

233 
let 

234 
val Ss = Sorts.mg_domain algebra tyco [class]; 

24920  235 
in Syntax.pretty_arity ctxt (tyco, Ss, [class]) end; 
24218  236 
fun mk_param (c, ty) = Pretty.str (Sign.extern_const thy c ^ " :: " 
24920  237 
^ setmp show_sorts false (Syntax.string_of_typ ctxt o Type.strip_sorts) ty); 
24218  238 
fun mk_entry class = (Pretty.block o Pretty.fbreaks o map_filter I) [ 
25062  239 
(SOME o Pretty.str) ("class " ^ Sign.extern_class thy class ^ ":"), 
24218  240 
(SOME o Pretty.block) [Pretty.str "supersort: ", 
24920  241 
(Syntax.pretty_sort ctxt o Sign.minimize_sort thy o Sign.super_classes thy) class], 
25062  242 
if is_class thy class then (SOME o Pretty.str) 
243 
("locale: " ^ Locale.extern thy class) else NONE, 

244 
((fn [] => NONE  ps => (SOME o Pretty.block o Pretty.fbreaks) 

245 
(Pretty.str "parameters:" :: ps)) o map mk_param 

24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset

246 
o these o Option.map #params o try (AxClass.get_info thy)) class, 
24218  247 
(SOME o Pretty.block o Pretty.breaks) [ 
248 
Pretty.str "instances:", 

249 
Pretty.list "" "" (map (mk_arity class) (the_arities class)) 

250 
] 

251 
] 

252 
in 

24589  253 
(Pretty.writeln o Pretty.chunks o separate (Pretty.str "") 
254 
o map mk_entry o Sorts.all_classes) algebra 

24218  255 
end; 
256 

257 

258 
(* updaters *) 

259 

25163  260 
fun add_class_data ((class, superclasses), (cs, base_sort, inst, phi, intro)) thy = 
25002  261 
let 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

262 
val operations = map (fn (v_ty as (_, ty), (c, _)) => 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

263 
(c, (class, (ty, Free v_ty)))) cs; 
25002  264 
val cs = (map o pairself) fst cs; 
25038  265 
val add_class = Graph.new_node (class, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

266 
mk_class_data ((cs, base_sort, map (SOME o Const) inst, phi, intro), ([], operations))) 
25002  267 
#> fold (curry Graph.add_edge class) superclasses; 
268 
in 

25038  269 
ClassData.map add_class thy 
25002  270 
end; 
24218  271 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

272 
fun register_operation class (c, (t, some_def)) thy = 
25062  273 
let 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

274 
val base_sort = (#base_sort o the_class_data thy) class; 
25239  275 
val prep_typ = map_atyps 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

276 
(fn TVar (vi as (v, _), sort) => if Name.aT = v 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

277 
then TFree (v, base_sort) else TVar (vi, sort)); 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

278 
val t' = map_types prep_typ t; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

279 
val ty' = Term.fastype_of t'; 
25062  280 
in 
281 
thy 

282 
> (ClassData.map o Graph.map_node class o map_class_data o apsnd) 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

283 
(fn (defs, operations) => 
25096  284 
(fold cons (the_list some_def) defs, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

285 
(c, (class, (ty', t'))) :: operations)) 
25062  286 
end; 
24218  287 

24589  288 

289 
(** rule calculation, tactics and methods **) 

290 

25024  291 
val class_prefix = Logic.const_of_class o Sign.base_name; 
292 

25062  293 
fun calculate_morphism class cs = 
294 
let 

295 
val subst_typ = Term.map_type_tfree (fn var as (v, sort) => 

296 
if v = Name.aT then TVar ((v, 0), [class]) else TVar ((v, 0), sort)); 

297 
fun subst_aterm (t as Free (v, ty)) = (case AList.lookup (op =) cs v 

298 
of SOME (c, _) => Const (c, ty) 

299 
 NONE => t) 

300 
 subst_aterm t = t; 

301 
val subst_term = map_aterms subst_aterm #> map_types subst_typ; 

302 
in 

25209  303 
Morphism.term_morphism subst_term 
25062  304 
$> Morphism.typ_morphism subst_typ 
305 
end; 

306 

25038  307 
fun class_intro thy class sups = 
24589  308 
let 
309 
fun class_elim class = 

25020  310 
case (#axioms o AxClass.get_info thy) class 
311 
of [thm] => SOME (Drule.unconstrainTs thm) 

24589  312 
 [] => NONE; 
25038  313 
val pred_intro = case Locale.intros thy class 
24589  314 
of ([ax_intro], [intro]) => intro > OF_LAST ax_intro > SOME 
315 
 ([intro], []) => SOME intro 

316 
 ([], [intro]) => SOME intro 

317 
 _ => NONE; 

318 
val pred_intro' = pred_intro 

319 
> Option.map (fn intro => intro OF map_filter class_elim sups); 

24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset

320 
val class_intro = (#intro o AxClass.get_info thy) class; 
24589  321 
val raw_intro = case pred_intro' 
322 
of SOME pred_intro => class_intro > OF_LAST pred_intro 

323 
 NONE => class_intro; 

324 
val sort = Sign.super_classes thy class; 

24847  325 
val typ = TVar ((Name.aT, 0), sort); 
24589  326 
val defs = these_defs thy sups; 
327 
in 

328 
raw_intro 

329 
> Drule.instantiate' [SOME (Thm.ctyp_of thy typ)] [] 

330 
> strip_all_ofclass thy sort 

331 
> Thm.strip_shyps 

332 
> MetaSimplifier.rewrite_rule defs 

333 
> Drule.unconstrainTs 

334 
end; 

335 

336 
fun class_interpretation class facts defs thy = 

337 
let 

25038  338 
val params = these_params thy [class]; 
25083  339 
val inst = (#inst o the_class_data thy) class; 
25020  340 
val tac = ALLGOALS (ProofContext.fact_tac facts); 
25038  341 
val prfx = class_prefix class; 
24589  342 
in 
25038  343 
thy 
344 
> fold_map (get_remove_global_constraint o fst o snd) params 

25094
ba43514068fd
Interpretation equations may have name and/or attribute.
ballarin
parents:
25083
diff
changeset

345 
> prove_interpretation tac ((false, prfx), []) (Locale.Locale class) 
ba43514068fd
Interpretation equations may have name and/or attribute.
ballarin
parents:
25083
diff
changeset

346 
(inst, map (fn def => (("", []), def)) defs) 
25038  347 
> (fn cs => fold (Sign.add_const_constraint o apsnd SOME) cs) 
24589  348 
end; 
24218  349 

350 
fun intro_classes_tac facts st = 

351 
let 

352 
val thy = Thm.theory_of_thm st; 

353 
val classes = Sign.all_classes thy; 

354 
val class_trivs = map (Thm.class_triv thy) classes; 

355 
val class_intros = these_intros thy; 

24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset

356 
val axclass_intros = map_filter (try (#intro o AxClass.get_info thy)) classes; 
24218  357 
in 
25268  358 
Method.intros_tac (class_trivs @ class_intros @ axclass_intros) facts st 
24218  359 
end; 
360 

361 
fun default_intro_classes_tac [] = intro_classes_tac [] 

24930
cc2e0e8c81af
renamed AxClass.get_definition to AxClass.get_info (again);
wenzelm
parents:
24920
diff
changeset

362 
 default_intro_classes_tac _ = no_tac; 
24218  363 

364 
fun default_tac rules ctxt facts = 

365 
HEADGOAL (Method.some_rule_tac rules ctxt facts) ORELSE 

366 
default_intro_classes_tac facts; 

367 

368 
val _ = Context.add_setup (Method.add_methods 

369 
[("intro_classes", Method.no_args (Method.METHOD intro_classes_tac), 

370 
"backchain introduction rules of classes"), 

371 
("default", Method.thms_ctxt_args (Method.METHOD oo default_tac), 

372 
"apply some intro/elim rule")]); 

373 

25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

374 
fun subclass_rule thy (sub, sup) = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

375 
let 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

376 
val ctxt = Locale.init sub thy; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

377 
val ctxt_thy = ProofContext.init thy; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

378 
val props = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

379 
Locale.global_asms_of thy sup 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

380 
> maps snd 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

381 
> map (ObjectLogic.ensure_propT thy); 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

382 
fun tac { prems, context } = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

383 
Locale.intro_locales_tac true context prems 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

384 
ORELSE ALLGOALS assume_tac; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

385 
in 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

386 
Goal.prove_multi ctxt [] [] props tac 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

387 
> map (Assumption.export false ctxt ctxt_thy) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

388 
> Variable.export ctxt ctxt_thy 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

389 
end; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

390 

62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

391 
fun prove_single_subclass (sub, sup) thms ctxt thy = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

392 
let 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

393 
val ctxt_thy = ProofContext.init thy; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

394 
val subclass_rule = Conjunction.intr_balanced thms 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

395 
> Assumption.export false ctxt ctxt_thy 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

396 
> singleton (Variable.export ctxt ctxt_thy); 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

397 
val sub_inst = Thm.ctyp_of thy (TVar ((Name.aT, 0), [sub])); 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

398 
val sub_ax = #axioms (AxClass.get_info thy sub); 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

399 
val classrel = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

400 
#intro (AxClass.get_info thy sup) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

401 
> Drule.instantiate' [SOME sub_inst] [] 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

402 
> OF_LAST (subclass_rule OF sub_ax) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

403 
> strip_all_ofclass thy (Sign.super_classes thy sup) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

404 
> Thm.strip_shyps 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

405 
in 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

406 
thy 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

407 
> AxClass.add_classrel classrel 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

408 
> prove_interpretation_in (ALLGOALS (ProofContext.fact_tac thms)) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

409 
I (sub, Locale.Locale sup) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

410 
> ClassData.map (Graph.add_edge (sub, sup)) 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

411 
end; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

412 

62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

413 
fun prove_subclass (sub, sup) thms ctxt thy = 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

414 
let 
25268  415 
val classes = ClassData.get thy; 
416 
val is_sup = not o null o curry (Graph.irreducible_paths classes) sub; 

417 
val supclasses = Graph.all_succs classes [sup] > filter_out is_sup; 

25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

418 
fun transform sup' = subclass_rule thy (sup, sup') > map (fn thm => thm OF thms); 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

419 
in 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

420 
thy 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

421 
> fold_rev (fn sup' => prove_single_subclass (sub, sup') 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

422 
(transform sup') ctxt) supclasses 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

423 
end; 
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

424 

24218  425 

24589  426 
(** classes and class target **) 
24218  427 

25002  428 
(* class context syntax *) 
24748  429 

25083  430 
structure ClassSyntax = ProofDataFun( 
431 
type T = { 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

432 
local_constraints: (string * typ) list, 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

433 
global_constraints: (string * typ) list, 
25083  434 
base_sort: sort, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

435 
operations: (string * (typ * term)) list, 
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

436 
unchecks: (term * term) list, 
25083  437 
passed: bool 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

438 
}; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

439 
fun init _ = { 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

440 
local_constraints = [], 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

441 
global_constraints = [], 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

442 
base_sort = [], 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

443 
operations = [], 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

444 
unchecks = [], 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

445 
passed = true 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

446 
};; 
25083  447 
); 
448 

25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

449 
fun synchronize_syntax sups base_sort ctxt = 
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset

450 
let 
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

451 
val thy = ProofContext.theory_of ctxt; 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

452 
fun subst_class_typ sort = map_atyps 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

453 
(fn TFree _ => TVar ((Name.aT, 0), sort)  ty' => ty'); 
25083  454 
val operations = these_operations thy sups; 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

455 
val local_constraints = 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

456 
(map o apsnd) (subst_class_typ base_sort o fst o snd) operations; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

457 
val global_constraints = 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

458 
(map o apsnd) (fn (class, (ty, _)) => subst_class_typ [class] ty) operations; 
25318
c8352b38d47d
synchronize_syntax: declare operations within the local scope of fixes/consts
wenzelm
parents:
25311
diff
changeset

459 
fun declare_const (c, _) = 
c8352b38d47d
synchronize_syntax: declare operations within the local scope of fixes/consts
wenzelm
parents:
25311
diff
changeset

460 
let val b = Sign.base_name c 
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

461 
in Sign.intern_const thy b = c ? Variable.declare_const (b, c) end; 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

462 
val unchecks = map (fn (c, (_, (ty, t))) => (t, Const (c, ty))) operations; 
25083  463 
in 
464 
ctxt 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

465 
> fold declare_const local_constraints 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

466 
> fold (ProofContext.add_const_constraint o apsnd SOME) local_constraints 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

467 
> ClassSyntax.put { 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

468 
local_constraints = local_constraints, 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

469 
global_constraints = global_constraints, 
25083  470 
base_sort = base_sort, 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

471 
operations = (map o apsnd) snd operations, 
25195
62638dcafe38
fixed syntax; truned code structure; added primitive subclass interface with consideraton of syntax etc.
haftmann
parents:
25163
diff
changeset

472 
unchecks = unchecks, 
25083  473 
passed = false 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

474 
} 
25083  475 
end; 
476 

477 
fun refresh_syntax class ctxt = 

25002  478 
let 
479 
val thy = ProofContext.theory_of ctxt; 

25062  480 
val base_sort = (#base_sort o the_class_data thy) class; 
25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

481 
in synchronize_syntax [class] base_sort ctxt end; 
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset

482 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

483 
val mark_passed = ClassSyntax.map 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

484 
(fn { local_constraints, global_constraints, base_sort, operations, unchecks, passed } => 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

485 
{ local_constraints = local_constraints, global_constraints = global_constraints, 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

486 
base_sort = base_sort, operations = operations, unchecks = unchecks, passed = true }); 
25083  487 

488 
fun sort_term_check ts ctxt = 

24748  489 
let 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

490 
val { local_constraints, global_constraints, base_sort, operations, passed, ... } = 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

491 
ClassSyntax.get ctxt; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

492 
fun check_improve (Const (c, ty)) = (case AList.lookup (op =) local_constraints c 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

493 
of SOME ty0 => (case try (Type.raw_match (ty0, ty)) Vartab.empty 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

494 
of SOME tyenv => (case Vartab.lookup tyenv (Name.aT, 0) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

495 
of SOME (_, TVar (tvar as (vi, _))) => 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

496 
if TypeInfer.is_param vi then cons tvar else I 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

497 
 _ => I) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

498 
 NONE => I) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

499 
 NONE => I) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

500 
 check_improve _ = I; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

501 
val improvements = (fold o fold_aterms) check_improve ts []; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

502 
val ts' = (map o map_types o map_atyps) (fn ty as TVar tvar => 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

503 
if member (op =) improvements tvar 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

504 
then TFree (Name.aT, base_sort) else ty  ty => ty) ts; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

505 
fun check t0 = Envir.expand_term (fn Const (c, ty) => (case AList.lookup (op =) operations c 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

506 
of SOME (ty0, t) => 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

507 
if Type.typ_instance (ProofContext.tsig_of ctxt) (ty, ty0) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

508 
then SOME (ty0, check t) else NONE 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

509 
 NONE => NONE) 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

510 
 _ => NONE) t0; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

511 
val ts'' = map check ts'; 
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

512 
in if eq_list (op aconv) (ts, ts'') andalso passed then NONE 
25083  513 
else 
514 
ctxt 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

515 
> fold (ProofContext.add_const_constraint o apsnd SOME) global_constraints 
25083  516 
> mark_passed 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

517 
> pair ts'' 
25083  518 
> SOME 
519 
end; 

24748  520 

25083  521 
fun sort_term_uncheck ts ctxt = 
25002  522 
let 
523 
val thy = ProofContext.theory_of ctxt; 

25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

524 
val unchecks = (#unchecks o ClassSyntax.get) ctxt; 
25462  525 
val ts' = map (Pattern.rewrite_term thy unchecks []) ts; 
25060  526 
in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', ctxt) end; 
25002  527 

25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

528 
fun init_ctxt sups base_sort ctxt = 
25083  529 
ctxt 
530 
> Variable.declare_term 

531 
(Logic.mk_type (TFree (Name.aT, base_sort))) 

25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

532 
> synchronize_syntax sups base_sort 
25083  533 
> Context.proof_map ( 
534 
Syntax.add_term_check 0 "class" sort_term_check 

25103  535 
#> Syntax.add_term_uncheck 0 "class" sort_term_uncheck) 
24901
d3cbf79769b9
added first version of userspace type system for class target
haftmann
parents:
24847
diff
changeset

536 

25311  537 
fun init class thy = 
538 
thy 

539 
> Locale.init class 

25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

540 
> init_ctxt [class] ((#base_sort o the_class_data thy) class); 
24914
95cda5dd58d5
added proper subclass concept; improved class target
haftmann
parents:
24901
diff
changeset

541 

24748  542 

24589  543 
(* class definition *) 
24218  544 

545 
local 

546 

24748  547 
fun gen_class_spec prep_class prep_expr process_expr thy raw_supclasses raw_includes_elems = 
24218  548 
let 
24748  549 
val supclasses = map (prep_class thy) raw_supclasses; 
25209  550 
val sups = filter (is_class thy) supclasses; 
551 
fun the_base_sort class = lookup_class_data thy class 

552 
> Option.map #base_sort 

553 
> the_default [class]; 

554 
val base_sort = Sign.minimize_sort thy (maps the_base_sort supclasses); 

24748  555 
val supsort = Sign.minimize_sort thy supclasses; 
25038  556 
val suplocales = map Locale.Locale sups; 
24748  557 
val (raw_elems, includes) = fold_rev (fn Locale.Elem e => apfst (cons e) 
558 
 Locale.Expr i => apsnd (cons (prep_expr thy i))) raw_includes_elems ([], []); 

559 
val supexpr = Locale.Merge suplocales; 

560 
val supparams = (map fst o Locale.parameters_of_expr thy) supexpr; 

25002  561 
val supconsts = AList.make (the o AList.lookup (op =) (these_params thy sups)) 
24748  562 
(map fst supparams); 
563 
val mergeexpr = Locale.Merge (suplocales @ includes); 

564 
val constrain = Element.Constrains ((map o apsnd o map_atyps) 

24847  565 
(fn TFree (_, sort) => TFree (Name.aT, sort)) supparams); 
24748  566 
in 
567 
ProofContext.init thy 

568 
> Locale.cert_expr supexpr [constrain] 

569 
> snd 

25344
00c2179db769
synchronize_syntax: improved declare_const (still inactive);
wenzelm
parents:
25326
diff
changeset

570 
> init_ctxt sups base_sort 
24748  571 
> process_expr Locale.empty raw_elems 
572 
> fst 

25062  573 
> (fn elems => ((((sups, supconsts), (supsort, base_sort, mergeexpr)), 
24748  574 
(*FIXME*) if null includes then constrain :: elems else elems))) 
575 
end; 

576 

577 
val read_class_spec = gen_class_spec Sign.intern_class Locale.intern_expr Locale.read_expr; 

578 
val check_class_spec = gen_class_spec (K I) (K I) Locale.cert_expr; 

579 

24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

580 
fun define_class_params (name, raw_superclasses) raw_consts raw_dep_axioms other_consts thy = 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

581 
let 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

582 
val superclasses = map (Sign.certify_class thy) raw_superclasses; 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

583 
val consts = (map o apfst o apsnd) (Sign.certify_typ thy) raw_consts; 
25083  584 
fun add_const ((c, ty), syn) = 
585 
Sign.declare_const [] (c, Type.strip_sorts ty, syn) #>> Term.dest_Const; 

24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

586 
fun mk_axioms cs thy = 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

587 
raw_dep_axioms thy cs 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

588 
> (map o apsnd o map) (Sign.cert_prop thy) 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

589 
> rpair thy; 
25002  590 
fun constrain_typs class = (map o apsnd o Term.map_type_tfree) 
591 
(fn (v, _) => TFree (v, [class])) 

24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

592 
in 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

593 
thy 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

594 
> Sign.add_path (Logic.const_of_class name) 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

595 
> fold_map add_const consts 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

596 
> Sign.restore_naming thy 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

597 
> (fn cs => mk_axioms cs 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

598 
#> (fn axioms_prop => AxClass.define_class (name, superclasses) 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

599 
(map fst cs @ other_consts) axioms_prop 
25002  600 
#> (fn class => `(fn _ => constrain_typs class cs) 
601 
#> (fn cs' => `(fn thy => AxClass.get_info thy class) 

602 
#> (fn {axioms, ...} => fold (Sign.add_const_constraint o apsnd SOME) cs' 

603 
#> pair (class, (cs', axioms))))))) 

24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

604 
end; 
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

605 

25002  606 
fun gen_class prep_spec prep_param bname 
24748  607 
raw_supclasses raw_includes_elems raw_other_consts thy = 
608 
let 

25038  609 
val class = Sign.full_name thy bname; 
25062  610 
val (((sups, supconsts), (supsort, base_sort, mergeexpr)), elems_syn) = 
24748  611 
prep_spec thy raw_supclasses raw_includes_elems; 
24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

612 
val other_consts = map (tap (Sign.the_const_type thy) o prep_param thy) raw_other_consts; 
25163  613 
fun mk_inst class cs = 
614 
(map o apsnd o Term.map_type_tfree) (fn (v, _) => TFree (v, [class])) cs; 

25062  615 
fun fork_syntax (Element.Fixes xs) = 
616 
fold_map (fn (c, ty, syn) => cons (c, syn) #> pair (c, ty, NoSyn)) xs 

617 
#>> Element.Fixes 

618 
 fork_syntax x = pair x; 

619 
val (elems, global_syn) = fold_map fork_syntax elems_syn []; 

25326  620 
fun globalize (c, ty) = 
25062  621 
((c, Term.map_type_tfree (K (TFree (Name.aT, base_sort))) ty), 
622 
(the_default NoSyn o AList.lookup (op =) global_syn) c); 

25038  623 
fun extract_params thy = 
24218  624 
let 
25062  625 
val params = map fst (Locale.parameters_of thy class); 
24218  626 
in 
25062  627 
(params, (map globalize o snd o chop (length supconsts)) params) 
24218  628 
end; 
25038  629 
fun extract_assumes params thy cs = 
24218  630 
let 
631 
val consts = supconsts @ (map (fst o fst) params ~~ cs); 

632 
fun subst (Free (c, ty)) = 

633 
Const ((fst o the o AList.lookup (op =) consts) c, ty) 

634 
 subst t = t; 

635 
fun prep_asm ((name, atts), ts) = 

25024  636 
((Sign.base_name name, map (Attrib.attribute_i thy) atts), 
24589  637 
(map o map_aterms) subst ts); 
24218  638 
in 
25038  639 
Locale.global_asms_of thy class 
24218  640 
> map prep_asm 
641 
end; 

642 
in 

643 
thy 

24748  644 
> Locale.add_locale_i (SOME "") bname mergeexpr elems 
25038  645 
> snd 
25311  646 
> ProofContext.theory_of 
647 
> `extract_params 

648 
> (fn (all_params, params) => 

24968
f9bafc868847
replaced Sign.add_consts_authentic by Sign.declare_const;
wenzelm
parents:
24949
diff
changeset

649 
define_class_params (bname, supsort) params 
25038  650 
(extract_assumes params) other_consts 
651 
#> (fn (_, (consts, axioms)) => 

652 
`(fn thy => class_intro thy class sups) 

24218  653 
#> (fn class_intro => 
25062  654 
PureThy.note_thmss_qualified "" (NameSpace.append class classN) 
655 
[((introN, []), [([class_intro], [])])] 

656 
#> (fn [(_, [class_intro])] => 

25038  657 
add_class_data ((class, sups), 
25062  658 
(map fst params ~~ consts, base_sort, 
25163  659 
mk_inst class (map snd supconsts @ consts), 
25062  660 
calculate_morphism class (supconsts @ (map (fst o fst) params ~~ consts)), class_intro)) 
25038  661 
#> class_interpretation class axioms [] 
25311  662 
)))) 
25268  663 
> init class 
25038  664 
> pair class 
24218  665 
end; 
666 

25326  667 
fun read_const thy = #1 o Term.dest_Const o ProofContext.read_const (ProofContext.init thy); 
668 

24218  669 
in 
670 

25326  671 
val class_cmd = gen_class read_class_spec read_const; 
24748  672 
val class = gen_class check_class_spec (K I); 
24218  673 

674 
end; (*local*) 

675 

676 

25485  677 
(* class target *) 
24218  678 

25603  679 
fun declare class pos ((c, mx), dict) thy = 
24218  680 
let 
25024  681 
val prfx = class_prefix class; 
682 
val thy' = thy > Sign.add_path prfx; 

25062  683 
val phi = morphism thy' class; 
25024  684 

25062  685 
val c' = Sign.full_name thy' c; 
25239  686 
val dict' = Morphism.term phi dict; 
687 
val dict_def = map_types Logic.unvarifyT dict'; 

688 
val ty' = Term.fastype_of dict_def; 

25083  689 
val ty'' = Type.strip_sorts ty'; 
25239  690 
val def_eq = Logic.mk_equals (Const (c', ty'), dict_def); 
24218  691 
in 
25024  692 
thy' 
25096  693 
> Sign.declare_const pos (c, ty'', mx) > snd 
25518  694 
> Thm.add_def false false (c, def_eq) 
25062  695 
>> Thm.symmetric 
25083  696 
> (fn def => class_interpretation class [def] [Thm.prop_of def] 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

697 
#> register_operation class (c', (dict', SOME (Thm.varifyT def)))) 
24218  698 
> Sign.restore_naming thy 
25083  699 
> Sign.add_const_constraint (c', SOME ty') 
24218  700 
end; 
701 

25603  702 
fun abbrev class prmode pos ((c, mx), rhs) thy = 
24836  703 
let 
25024  704 
val prfx = class_prefix class; 
25096  705 
val thy' = thy > Sign.add_path prfx; 
25062  706 
val phi = morphism thy class; 
707 

25096  708 
val c' = Sign.full_name thy' c; 
25146  709 
val rews = map (Logic.dest_equals o Thm.prop_of) (these_defs thy' [class]) 
710 
val rhs' = (Pattern.rewrite_term thy rews [] o Morphism.term phi) rhs; 

25239  711 
val ty' = Logic.unvarifyT (Term.fastype_of rhs'); 
24836  712 
in 
25096  713 
thy' 
25146  714 
> Sign.add_abbrev (#1 prmode) pos (c, map_types Type.strip_sorts rhs') > snd 
25096  715 
> Sign.add_const_constraint (c', SOME ty') 
25024  716 
> Sign.notation true prmode [(Const (c', ty'), mx)] 
25368
f12613fda79d
proper implementation of check phase; nonqualified names for class operations
haftmann
parents:
25344
diff
changeset

717 
> register_operation class (c', (rhs', NONE)) 
25096  718 
> Sign.restore_naming thy 
24836  719 
end; 
720 

25462  721 

722 
(** instantiation target **) 

723 

724 
(* bookkeeping *) 

725 

726 
datatype instantiation = Instantiation of { 

25536  727 
arities: string list * sort list * sort, 
25462  728 
params: ((string * string) * (string * typ)) list 
25603  729 
(*(instantiation parameter, type constructor), (local instantiation parameter, typ)*) 
25462  730 
} 
731 

732 
structure Instantiation = ProofDataFun 

733 
( 

734 
type T = instantiation 

25536  735 
fun init _ = Instantiation { arities = ([], [], []), params = [] }; 
25462  736 
); 
737 

25485  738 
fun mk_instantiation (arities, params) = 
739 
Instantiation { arities = arities, params = params }; 

25514  740 
fun get_instantiation lthy = case Instantiation.get (LocalTheory.target_of lthy) 
25485  741 
of Instantiation data => data; 
25514  742 
fun map_instantiation f = (LocalTheory.target o Instantiation.map) 
743 
(fn Instantiation { arities, params } => mk_instantiation (f (arities, params))); 

25462  744 

25514  745 
fun the_instantiation lthy = case get_instantiation lthy 
25536  746 
of { arities = ([], [], []), ... } => error "No instantiation target" 
25485  747 
 data => data; 
25462  748 

25485  749 
val instantiation_params = #params o get_instantiation; 
25462  750 

25514  751 
fun instantiation_param lthy v = instantiation_params lthy 
25462  752 
> find_first (fn (_, (v', _)) => v = v') 
753 
> Option.map (fst o fst); 

754 

25514  755 
fun confirm_declaration c = (map_instantiation o apsnd) 
25485  756 
(filter_out (fn (_, (c', _)) => c' = c)); 
757 

25462  758 

759 
(* syntax *) 

760 

25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

761 
fun subst_param thy params = map_aterms (fn t as Const (c, ty) => 
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

762 
(case AxClass.inst_tyco_of thy (c, ty) 
25536  763 
of SOME tyco => (case AList.lookup (op =) params (c, tyco) 
764 
of SOME v_ty => Free v_ty 

765 
 NONE => t) 

766 
 NONE => t) 

767 
 t => t); 

768 

25514  769 
fun inst_term_check ts lthy = 
25462  770 
let 
25514  771 
val params = instantiation_params lthy; 
772 
val tsig = ProofContext.tsig_of lthy; 

773 
val thy = ProofContext.theory_of lthy; 

25462  774 

25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

775 
fun check_improve (Const (c, ty)) = (case AxClass.inst_tyco_of thy (c, ty) 
25462  776 
of SOME tyco => (case AList.lookup (op =) params (c, tyco) 
25502  777 
of SOME (_, ty') => perhaps (try (Type.typ_match tsig (ty, ty'))) 
25462  778 
 NONE => I) 
779 
 NONE => I) 

780 
 check_improve _ = I; 

781 
val improvement = (fold o fold_aterms) check_improve ts Vartab.empty; 

782 
val ts' = (map o map_types) (Envir.typ_subst_TVars improvement) ts; 

25536  783 
val ts'' = map (subst_param thy params) ts'; 
25514  784 
in if eq_list (op aconv) (ts, ts'') then NONE else SOME (ts'', lthy) end; 
25462  785 

25514  786 
fun inst_term_uncheck ts lthy = 
25462  787 
let 
25514  788 
val params = instantiation_params lthy; 
25462  789 
val ts' = (map o map_aterms) (fn t as Free (v, ty) => 
790 
(case get_first (fn ((c, _), (v', _)) => if v = v' then SOME c else NONE) params 

791 
of SOME c => Const (c, ty) 

792 
 NONE => t) 

793 
 t => t) ts; 

25514  794 
in if eq_list (op aconv) (ts, ts') then NONE else SOME (ts', lthy) end; 
25462  795 

796 

797 
(* target *) 

798 

25485  799 
val sanatize_name = (*FIXME*) 
800 
let 

25574  801 
fun is_valid s = Symbol.is_ascii_letter s orelse Symbol.is_ascii_digit s 
802 
orelse s = "'" orelse s = "_"; 

25485  803 
val is_junk = not o is_valid andf Symbol.is_regular; 
804 
val junk = Scan.many is_junk; 

805 
val scan_valids = Symbol.scanner "Malformed input" 

806 
((junk  

807 
(Scan.optional (Scan.one Symbol.is_ascii_letter) "x" ^^ (Scan.many is_valid >> implode) 

808 
 junk)) 

809 
 Scan.repeat ((Scan.many1 is_valid >> implode)  junk) >> op ::); 

810 
in 

811 
explode #> scan_valids #> implode 

812 
end; 

813 

25536  814 
fun init_instantiation (tycos, sorts, sort) thy = 
25462  815 
let 
25536  816 
val _ = if null tycos then error "At least one arity must be given" else (); 
817 
val _ = map (the_class_data thy) sort; 

818 
val vs = map TFree (Name.names Name.context Name.aT sorts); 

25485  819 
fun type_name "*" = "prod" 
820 
 type_name "+" = "sum" 

821 
 type_name s = sanatize_name (NameSpace.base s); (*FIXME*) 

25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

822 
fun get_param tyco (param, (c, ty)) = if can (AxClass.param_of_inst thy) (c, tyco) 
25603  823 
then NONE else SOME ((c, tyco), 
25536  824 
(param ^ "_" ^ type_name tyco, map_atyps (K (Type (tyco, vs))) ty)); 
825 
val params = map_product get_param tycos (these_params thy sort) > map_filter I; 

25485  826 
in 
827 
thy 

828 
> ProofContext.init 

25536  829 
> Instantiation.put (mk_instantiation ((tycos, sorts, sort), params)) 
830 
> fold (Variable.declare_term o Logic.mk_type) vs 

25574  831 
> fold (Variable.declare_names o Free o snd) params 
25536  832 
> fold (fn tyco => ProofContext.add_arity (tyco, sorts, sort)) tycos 
25485  833 
> Context.proof_map ( 
834 
Syntax.add_term_check 0 "instance" inst_term_check 

835 
#> Syntax.add_term_uncheck 0 "instance" inst_term_uncheck) 

836 
end; 

837 

838 
fun gen_instantiation_instance do_proof after_qed lthy = 

839 
let 

25536  840 
val (tycos, sorts, sort) = (#arities o the_instantiation) lthy; 
841 
val arities_proof = maps (fn tyco => Logic.mk_arities (tyco, sorts, sort)) tycos; 

25462  842 
fun after_qed' results = 
843 
LocalTheory.theory (fold (AxClass.add_arity o Thm.varifyT) results) 

844 
#> after_qed; 

845 
in 

846 
lthy 

847 
> do_proof after_qed' arities_proof 

848 
end; 

849 

25485  850 
val instantiation_instance = gen_instantiation_instance (fn after_qed => fn ts => 
25462  851 
Proof.theorem_i NONE (after_qed o map the_single) (map (fn t => [(t, [])]) ts)); 
852 

25485  853 
fun prove_instantiation_instance tac = gen_instantiation_instance (fn after_qed => 
25502  854 
fn ts => fn lthy => after_qed (map (fn t => Goal.prove lthy [] [] t 
855 
(fn {context, ...} => tac context)) ts) lthy) I; 

25462  856 

857 
fun conclude_instantiation lthy = 

858 
let 

25485  859 
val { arities, params } = the_instantiation lthy; 
25536  860 
val (tycos, sorts, sort) = arities; 
25462  861 
val thy = ProofContext.theory_of lthy; 
25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

862 
val _ = map (fn tyco => if Sign.of_sort thy 
25462  863 
(Type (tyco, map TFree (Name.names Name.context Name.aT sorts)), sort) 
864 
then () else error ("Missing instance proof for type " ^ quote (Sign.extern_type thy tyco))) 

25597
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

865 
tycos; 
34860182b250
moved instance parameter management from class.ML to axclass.ML
haftmann
parents:
25574
diff
changeset

866 
in lthy end; 
25462  867 

25603  868 
fun pretty_instantiation lthy = 
869 
let 

870 
val { arities, params } = the_instantiation lthy; 

871 
val (tycos, sorts, sort) = arities; 

872 
val thy = ProofContext.theory_of lthy; 

873 
fun pr_arity tyco = Syntax.pretty_arity lthy (tyco, sorts, sort); 

874 
fun pr_param ((c, _), (v, ty)) = 

875 
(Pretty.block o Pretty.breaks) [(Pretty.str o Sign.extern_const thy) c, Pretty.str "::", 

876 
Sign.pretty_typ thy ty, Pretty.str "as", Pretty.str v]; 

877 
in 

878 
(Pretty.block o Pretty.fbreaks) 

879 
(Pretty.str "instantiation" :: map pr_arity tycos @ map pr_param params) 

880 
end; 

881 

24218  882 
end; 