author | haftmann |
Mon, 05 Jan 2009 15:36:24 +0100 | |
changeset 29358 | efdfe5dfe008 |
parent 29252 | ea97aa6aeba2 |
child 29360 | a5be60c3674e |
permissions | -rw-r--r-- |
20894 | 1 |
(* Title: Pure/Isar/theory_target.ML |
2 |
Author: Makarius |
|
3 |
||
25542 | 4 |
Common theory/locale/class/instantiation/overloading targets. |
20894 | 5 |
*) |
6 |
||
7 |
signature THEORY_TARGET = |
|
8 |
sig |
|
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
9 |
val peek: local_theory -> {target: string, new_locale: bool, is_locale: bool, |
25864 | 10 |
is_class: bool, instantiation: string list * (string * sort) list * sort, |
11 |
overloading: (string * (string * typ) * bool) list} |
|
25269 | 12 |
val init: string option -> theory -> local_theory |
24935
a033971c63a0
removed LocalTheory.defs/target_morphism operations;
wenzelm
parents:
24914
diff
changeset
|
13 |
val begin: string -> Proof.context -> local_theory |
25291 | 14 |
val context: xstring -> theory -> local_theory |
25864 | 15 |
val instantiation: string list * (string * sort) list * sort -> theory -> local_theory |
29358 | 16 |
val instantiation_cmd: xstring list * sort * xstring -> theory -> local_theory |
25864 | 17 |
val overloading: (string * (string * typ) * bool) list -> theory -> local_theory |
26049
8186c03194ed
overloading: reduced code redundancy, no xstrings here;
wenzelm
parents:
25984
diff
changeset
|
18 |
val overloading_cmd: (string * string * bool) list -> theory -> local_theory |
20894 | 19 |
end; |
20 |
||
21 |
structure TheoryTarget: THEORY_TARGET = |
|
22 |
struct |
|
23 |
||
28834 | 24 |
(* new locales *) |
25 |
||
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
26 |
fun locale_extern new_locale x = |
29249 | 27 |
if new_locale then NewLocale.extern x else Locale.extern x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
28 |
fun locale_add_type_syntax new_locale x = |
29249 | 29 |
if new_locale then NewLocale.add_type_syntax x else Locale.add_type_syntax x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
30 |
fun locale_add_term_syntax new_locale x = |
29249 | 31 |
if new_locale then NewLocale.add_term_syntax x else Locale.add_term_syntax x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
32 |
fun locale_add_declaration new_locale x = |
29249 | 33 |
if new_locale then NewLocale.add_declaration x else Locale.add_declaration x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
34 |
fun locale_add_thmss new_locale x = |
29249 | 35 |
if new_locale then NewLocale.add_thmss x else Locale.add_thmss x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
36 |
fun locale_init new_locale x = |
29249 | 37 |
if new_locale then NewLocale.init x else Locale.init x; |
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
38 |
fun locale_intern new_locale x = |
29249 | 39 |
if new_locale then NewLocale.intern x else Locale.intern x; |
28834 | 40 |
|
21006 | 41 |
(* context data *) |
42 |
||
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
43 |
datatype target = Target of {target: string, new_locale: bool, is_locale: bool, |
25864 | 44 |
is_class: bool, instantiation: string list * (string * sort) list * sort, |
45 |
overloading: (string * (string * typ) * bool) list}; |
|
25012
448af76a1ba3
pass explicit target record -- more informative peek operation;
wenzelm
parents:
25002
diff
changeset
|
46 |
|
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
47 |
fun make_target target new_locale is_locale is_class instantiation overloading = |
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
48 |
Target {target = target, new_locale = new_locale, is_locale = is_locale, |
25519 | 49 |
is_class = is_class, instantiation = instantiation, overloading = overloading}; |
25291 | 50 |
|
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
51 |
val global_target = make_target "" false false false ([], [], []) []; |
25012
448af76a1ba3
pass explicit target record -- more informative peek operation;
wenzelm
parents:
25002
diff
changeset
|
52 |
|
21006 | 53 |
structure Data = ProofDataFun |
54 |
( |
|
25012
448af76a1ba3
pass explicit target record -- more informative peek operation;
wenzelm
parents:
25002
diff
changeset
|
55 |
type T = target; |
25291 | 56 |
fun init _ = global_target; |
21006 | 57 |
); |
58 |
||
25012
448af76a1ba3
pass explicit target record -- more informative peek operation;
wenzelm
parents:
25002
diff
changeset
|
59 |
val peek = (fn Target args => args) o Data.get; |
21006 | 60 |
|
61 |
||
20894 | 62 |
(* pretty *) |
63 |
||
25607 | 64 |
fun pretty_thy ctxt target is_locale is_class = |
20894 | 65 |
let |
66 |
val thy = ProofContext.theory_of ctxt; |
|
29223 | 67 |
val target_name = (if is_class then "class " else "locale ") ^ locale_extern is_class thy target; |
28965 | 68 |
val fixes = map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) |
28094 | 69 |
(#1 (ProofContext.inferred_fixes ctxt)); |
28965 | 70 |
val assumes = map (fn A => (Attrib.empty_binding, [(Thm.term_of A, [])])) |
28094 | 71 |
(Assumption.assms_of ctxt); |
20894 | 72 |
val elems = |
73 |
(if null fixes then [] else [Element.Fixes fixes]) @ |
|
74 |
(if null assumes then [] else [Element.Assumes assumes]); |
|
26049
8186c03194ed
overloading: reduced code redundancy, no xstrings here;
wenzelm
parents:
25984
diff
changeset
|
75 |
in |
8186c03194ed
overloading: reduced code redundancy, no xstrings here;
wenzelm
parents:
25984
diff
changeset
|
76 |
if target = "" then [] |
25607 | 77 |
else if null elems then [Pretty.str target_name] |
78 |
else [Pretty.big_list (target_name ^ " =") |
|
79 |
(map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)] |
|
20894 | 80 |
end; |
81 |
||
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
82 |
fun pretty (Target {target, is_locale, is_class, instantiation, overloading, ...}) ctxt = |
25607 | 83 |
Pretty.block [Pretty.str "theory", Pretty.brk 1, |
84 |
Pretty.str (Context.theory_name (ProofContext.theory_of ctxt))] :: |
|
85 |
(if not (null overloading) then [Overloading.pretty ctxt] |
|
29358 | 86 |
else if not (null (#1 instantiation)) then [Class_Target.pretty_instantiation ctxt] |
26049
8186c03194ed
overloading: reduced code redundancy, no xstrings here;
wenzelm
parents:
25984
diff
changeset
|
87 |
else pretty_thy ctxt target is_locale is_class); |
25607 | 88 |
|
20894 | 89 |
|
24838 | 90 |
(* target declarations *) |
91 |
||
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
92 |
fun target_decl add (Target {target, new_locale, ...}) d lthy = |
24838 | 93 |
let |
24935
a033971c63a0
removed LocalTheory.defs/target_morphism operations;
wenzelm
parents:
24914
diff
changeset
|
94 |
val d' = Morphism.transform (LocalTheory.target_morphism lthy) d; |
24838 | 95 |
val d0 = Morphism.form d'; |
96 |
in |
|
25012
448af76a1ba3
pass explicit target record -- more informative peek operation;
wenzelm
parents:
25002
diff
changeset
|
97 |
if target = "" then |
24838 | 98 |
lthy |
99 |
|> LocalTheory.theory (Context.theory_map d0) |
|
100 |
|> LocalTheory.target (Context.proof_map d0) |
|
101 |
else |
|
102 |
lthy |
|
29228
40b3630b0deb
Theory target distinguishes old and new locales.
ballarin
parents:
29223
diff
changeset
|
103 |
|> LocalTheory.target (add new_locale target d') |
24838 | 104 |
end; |
105 |
||
28834 | 106 |
val type_syntax = target_decl locale_add_type_syntax; |
107 |
val term_syntax = target_decl locale_add_term_syntax; |
|
108 |
val declaration = target_decl locale_add_declaration; |
|
24838 | 109 |
|
25105
c9f67836c4d8
internal tuning: class_target, fork_mixfix, declare_const (singleton), abbrev;
wenzelm
parents:
25096
diff
changeset
|
110 |
fun class_target (Target {target, ...}) f = |
c9f67836c4d8
internal tuning: class_target, fork_mixfix, declare_const (singleton), abbrev;
wenzelm
parents:
25096
diff
changeset
|
111 |
LocalTheory.raw_theory f #> |
29358 | 112 |
LocalTheory.target (Class_Target.refresh_syntax target); |
24838 | 113 |
|
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
114 |
|
20894 | 115 |
(* notes *) |
116 |
||
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
117 |
fun import_export_proof ctxt (name, raw_th) = |
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset
|
118 |
let |
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
119 |
val thy = ProofContext.theory_of ctxt; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
120 |
val thy_ctxt = ProofContext.init thy; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
121 |
val certT = Thm.ctyp_of thy; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
122 |
val cert = Thm.cterm_of thy; |
21594
2859c94d67d4
reworked notes: towards proper import/export of proof terms;
wenzelm
parents:
21585
diff
changeset
|
123 |
|
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
124 |
(*export assumes/defines*) |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
125 |
val th = Goal.norm_result raw_th; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
126 |
val (defs, th') = LocalDefs.export ctxt thy_ctxt th; |
21708 | 127 |
val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th); |
128 |
val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt); |
|
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
129 |
val nprems = Thm.nprems_of th' - Thm.nprems_of th; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
130 |
|
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
131 |
(*export fixes*) |
22692 | 132 |
val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []); |
133 |
val frees = map Free (Thm.fold_terms Term.add_frees th' []); |
|
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
134 |
val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees)) |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
135 |
|> Variable.export ctxt thy_ctxt |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
136 |
|> Drule.zero_var_indexes_list; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
137 |
|
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
138 |
(*thm definition*) |
28083
103d9282a946
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27690
diff
changeset
|
139 |
val result = PureThy.name_thm true true Position.none name th''; |
21611
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
140 |
|
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
141 |
(*import fixes*) |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
142 |
val (tvars, vars) = |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
143 |
chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs) |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
144 |
|>> map Logic.dest_type; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
145 |
|
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
146 |
val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees); |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
147 |
val inst = filter (is_Var o fst) (vars ~~ frees); |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
148 |
val cinstT = map (pairself certT o apfst TVar) instT; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
149 |
val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|
150 |
val result' = Thm.instantiate (cinstT, cinst) result; |
fc95ff1fe738
notes: proper import/export of proofs (still inactive);
wenzelm
parents:
21594
diff
changeset
|