author | haftmann |
Thu, 22 May 2014 17:53:01 +0200 | |
changeset 57072 | dfac6ef0ca28 |
parent 57068 | 474403e50e05 |
child 57073 | 9c990475d44f |
permissions | -rw-r--r-- |
41959 | 1 |
(* Title: Pure/Isar/generic_target.ML |
38309 | 2 |
Author: Makarius |
3 |
Author: Florian Haftmann, TU Muenchen |
|
4 |
||
5 |
Common target infrastructure. |
|
6 |
*) |
|
7 |
||
8 |
signature GENERIC_TARGET = |
|
9 |
sig |
|
40782 | 10 |
val define: (((binding * typ) * mixfix) * (binding * term) -> |
11 |
term list * term list -> local_theory -> (term * thm) * local_theory) -> |
|
46992
eeea81b86b70
refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents:
46916
diff
changeset
|
12 |
bool -> (binding * mixfix) * (Attrib.binding * term) -> local_theory -> |
40782 | 13 |
(term * (string * thm)) * local_theory |
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
14 |
val notes: |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
15 |
(string -> (Attrib.binding * (thm list * Args.src list) list) list -> |
40782 | 16 |
(Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> local_theory) -> |
17 |
string -> (Attrib.binding * (thm list * Args.src list) list) list -> local_theory -> |
|
18 |
(string * thm list) list * local_theory |
|
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
19 |
val locale_notes: string -> string -> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
20 |
(Attrib.binding * (thm list * Args.src list) list) list -> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
21 |
(Attrib.binding * (thm list * Args.src list) list) list -> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
22 |
local_theory -> local_theory |
57068 | 23 |
val background_abbrev: binding * term -> local_theory -> (term * term) * local_theory |
40782 | 24 |
val abbrev: (string * bool -> binding * mixfix -> term * term -> |
25 |
term list -> local_theory -> local_theory) -> |
|
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
26 |
string * bool -> (binding * mixfix) * term -> local_theory -> (term * term) * local_theory |
47081
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
27 |
val background_declaration: declaration -> local_theory -> local_theory |
47280 | 28 |
val locale_declaration: string -> bool -> declaration -> local_theory -> local_theory |
57065 | 29 |
val standard_declaration: (int * int -> bool) -> declaration -> local_theory -> local_theory |
47282 | 30 |
val generic_const: bool -> Syntax.mode -> (binding * mixfix) * term -> |
31 |
Context.generic -> Context.generic |
|
57065 | 32 |
val const_declaration: (int * int -> bool) -> Syntax.mode -> (binding * mixfix) * term -> |
47282 | 33 |
local_theory -> local_theory |
57072
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
34 |
val locale_const_declaration: string -> Syntax.mode -> (binding * mixfix) * term -> |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
35 |
local_theory -> local_theory |
47284
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
36 |
val background_foundation: ((binding * typ) * mixfix) * (binding * term) -> |
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
37 |
term list * term list -> local_theory -> (term * thm) * local_theory |
40782 | 38 |
val theory_foundation: ((binding * typ) * mixfix) * (binding * term) -> |
39 |
term list * term list -> local_theory -> (term * thm) * local_theory |
|
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
40 |
val theory_notes: string -> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
41 |
(Attrib.binding * (thm list * Args.src list) list) list -> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
42 |
(Attrib.binding * (thm list * Args.src list) list) list -> |
40782 | 43 |
local_theory -> local_theory |
47286
392c4cd97e5c
more uniform theory_abbrev with const_declaration;
wenzelm
parents:
47285
diff
changeset
|
44 |
val theory_abbrev: Syntax.mode -> (binding * mixfix) -> term * term -> term list -> |
392c4cd97e5c
more uniform theory_abbrev with const_declaration;
wenzelm
parents:
47285
diff
changeset
|
45 |
local_theory -> local_theory |
47279
4bab649dedf0
clarified standard_declaration vs. theory_declaration;
wenzelm
parents:
47276
diff
changeset
|
46 |
val theory_declaration: declaration -> local_theory -> local_theory |
52153
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
47 |
val theory_registration: string * morphism -> (morphism * bool) option -> morphism -> |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
48 |
local_theory -> local_theory |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
49 |
val locale_dependency: string -> string * morphism -> (morphism * bool) option -> morphism -> |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
50 |
local_theory -> local_theory |
45353 | 51 |
end |
38309 | 52 |
|
53 |
structure Generic_Target: GENERIC_TARGET = |
|
54 |
struct |
|
55 |
||
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
56 |
(** lifting primitive to target operations **) |
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
57 |
|
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
58 |
(* mixfix syntax *) |
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
59 |
|
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
60 |
fun check_mixfix ctxt (b, extra_tfrees) mx = |
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
61 |
if null extra_tfrees then mx |
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
62 |
else |
56294
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
63 |
(if Context_Position.is_visible ctxt then |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
64 |
warning |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
65 |
("Additional type variable(s) in specification of " ^ Binding.print b ^ ": " ^ |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
66 |
commas (map (Syntax.string_of_typ ctxt o TFree) (sort_wrt #1 extra_tfrees)) ^ |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
67 |
(if mx = NoSyn then "" |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
68 |
else "\nDropping mixfix syntax " ^ Pretty.string_of (Mixfix.pretty_mixfix mx))) |
85911b8a6868
prefer Context_Position where a context is available;
wenzelm
parents:
56141
diff
changeset
|
69 |
else (); NoSyn); |
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
70 |
|
47291
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
71 |
fun check_mixfix_global (b, no_params) mx = |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
72 |
if no_params orelse mx = NoSyn then mx |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
73 |
else (warning ("Dropping global mixfix syntax: " ^ Binding.print b ^ " " ^ |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
74 |
Pretty.string_of (Mixfix.pretty_mixfix mx)); NoSyn); |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
75 |
|
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
76 |
|
38309 | 77 |
(* define *) |
78 |
||
46992
eeea81b86b70
refined Local_Theory.define vs. Local_Theory.define_internal, which allows to pass alternative name to the foundational axiom -- expecially important for 'instantiation' or 'overloading', which loose name information due to Long_Name.base_name cooking etc.;
wenzelm
parents:
46916
diff
changeset
|
79 |
fun define foundation internal ((b, mx), ((b_def, atts), rhs)) lthy = |
38309 | 80 |
let |
42360 | 81 |
val thy = Proof_Context.theory_of lthy; |
82 |
val thy_ctxt = Proof_Context.init_global thy; |
|
38309 | 83 |
|
84 |
(*term and type parameters*) |
|
47276 | 85 |
val ((defs, _), rhs') = Thm.cterm_of thy rhs |
86 |
|> Local_Defs.export_cterm lthy thy_ctxt ||> Thm.term_of; |
|
38309 | 87 |
|
47272
ca31cfa509b1
refined define/abbrev: allow extra fixes in aux. context vs. bottom target (NB: export_term expands defined variables, leaving fixed ones);
wenzelm
parents:
47250
diff
changeset
|
88 |
val xs = Variable.add_fixed lthy rhs' []; |
38309 | 89 |
val T = Term.fastype_of rhs; |
90 |
val tfreesT = Term.add_tfreesT T (fold (Term.add_tfreesT o #2) xs []); |
|
91 |
val extra_tfrees = rev (subtract (op =) tfreesT (Term.add_tfrees rhs [])); |
|
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
92 |
val mx' = check_mixfix lthy (b, extra_tfrees) mx; |
38309 | 93 |
|
94 |
val type_params = map (Logic.mk_type o TFree) extra_tfrees; |
|
47272
ca31cfa509b1
refined define/abbrev: allow extra fixes in aux. context vs. bottom target (NB: export_term expands defined variables, leaving fixed ones);
wenzelm
parents:
47250
diff
changeset
|
95 |
val term_params = map Free (sort (Variable.fixed_ord lthy o pairself #1) xs); |
38309 | 96 |
val params = type_params @ term_params; |
97 |
||
98 |
val U = map Term.fastype_of params ---> T; |
|
99 |
||
100 |
(*foundation*) |
|
45353 | 101 |
val ((lhs', global_def), lthy2) = lthy |
102 |
|> foundation (((b, U), mx'), (b_def, rhs')) (type_params, term_params); |
|
38309 | 103 |
|
104 |
(*local definition*) |
|
38315 | 105 |
val ((lhs, local_def), lthy3) = lthy2 |
38309 | 106 |
|> Local_Defs.add_def ((b, NoSyn), lhs'); |
47240 | 107 |
|
108 |
(*result*) |
|
47238 | 109 |
val def = |
47240 | 110 |
Thm.transitive local_def global_def |
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
52230
diff
changeset
|
111 |
|> Local_Defs.contract lthy3 defs |
47240 | 112 |
(Thm.cterm_of (Proof_Context.theory_of lthy3) (Logic.mk_equals (lhs, rhs))); |
38315 | 113 |
val ([(res_name, [res])], lthy4) = lthy3 |
47080 | 114 |
|> Local_Theory.notes [((if internal then Binding.empty else b_def, atts), [([def], [])])]; |
38315 | 115 |
in ((lhs, (res_name, res)), lthy4) end; |
38309 | 116 |
|
117 |
||
118 |
(* notes *) |
|
119 |
||
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
120 |
local |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
121 |
|
38309 | 122 |
fun import_export_proof ctxt (name, raw_th) = |
123 |
let |
|
42360 | 124 |
val thy = Proof_Context.theory_of ctxt; |
125 |
val thy_ctxt = Proof_Context.init_global thy; |
|
38309 | 126 |
val certT = Thm.ctyp_of thy; |
127 |
val cert = Thm.cterm_of thy; |
|
128 |
||
129 |
(*export assumes/defines*) |
|
54883
dd04a8b654fc
proper context for norm_hhf and derived operations;
wenzelm
parents:
54742
diff
changeset
|
130 |
val th = Goal.norm_result ctxt raw_th; |
45407
a83574606719
more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents:
45390
diff
changeset
|
131 |
val ((defs, asms), th') = Local_Defs.export ctxt thy_ctxt th; |
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
52230
diff
changeset
|
132 |
val asms' = map (rewrite_rule ctxt (Drule.norm_hhf_eqs @ defs)) asms; |
38309 | 133 |
|
134 |
(*export fixes*) |
|
135 |
val tfrees = map TFree (Thm.fold_terms Term.add_tfrees th' []); |
|
136 |
val frees = map Free (Thm.fold_terms Term.add_frees th' []); |
|
45352 | 137 |
val (th'' :: vs) = |
138 |
(th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees)) |
|
38309 | 139 |
|> Variable.export ctxt thy_ctxt |
140 |
|> Drule.zero_var_indexes_list; |
|
141 |
||
142 |
(*thm definition*) |
|
45413
117ff038f8f7
disabled Thm.compress (again) -- costs for building tables tend to be higher than potential benefit;
wenzelm
parents:
45407
diff
changeset
|
143 |
val result = Global_Theory.name_thm true true name th''; |
38309 | 144 |
|
145 |
(*import fixes*) |
|
146 |
val (tvars, vars) = |
|
147 |
chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs) |
|
148 |
|>> map Logic.dest_type; |
|
149 |
||
150 |
val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees); |
|
151 |
val inst = filter (is_Var o fst) (vars ~~ frees); |
|
152 |
val cinstT = map (pairself certT o apfst TVar) instT; |
|
153 |
val cinst = map (pairself (cert o Term.map_types (Term_Subst.instantiateT instT))) inst; |
|
154 |
val result' = Thm.instantiate (cinstT, cinst) result; |
|
155 |
||
156 |
(*import assumes/defines*) |
|
157 |
val result'' = |
|
45407
a83574606719
more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents:
45390
diff
changeset
|
158 |
(fold (curry op COMP) asms' result' |
a83574606719
more specific treatment of defines/assumes -- avoid normalizing defs by themselves (NB: locale specifications and Local_Theory.define may lead to arbitrary mixture);
wenzelm
parents:
45390
diff
changeset
|
159 |
handle THM _ => raise THM ("Failed to re-import result", 0, result' :: asms')) |
54742
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents:
52230
diff
changeset
|
160 |
|> Local_Defs.contract ctxt defs (Thm.cprop_of th) |
54883
dd04a8b654fc
proper context for norm_hhf and derived operations;
wenzelm
parents:
54742
diff
changeset
|
161 |
|> Goal.norm_result ctxt |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38831
diff
changeset
|
162 |
|> Global_Theory.name_thm false false name; |
38309 | 163 |
|
164 |
in (result'', result) end; |
|
165 |
||
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
166 |
fun standard_facts lthy ctxt = |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
167 |
Element.transform_facts (Local_Theory.standard_morphism lthy ctxt); |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
168 |
|
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
169 |
in |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
170 |
|
38309 | 171 |
fun notes target_notes kind facts lthy = |
172 |
let |
|
173 |
val facts' = facts |
|
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38831
diff
changeset
|
174 |
|> map (fn (a, bs) => (a, Global_Theory.burrow_fact (Global_Theory.name_multi |
38309 | 175 |
(Local_Theory.full_name lthy (fst a))) bs)) |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38831
diff
changeset
|
176 |
|> Global_Theory.map_facts (import_export_proof lthy); |
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38831
diff
changeset
|
177 |
val local_facts = Global_Theory.map_facts #1 facts'; |
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38831
diff
changeset
|
178 |
val global_facts = Global_Theory.map_facts #2 facts'; |
38309 | 179 |
in |
180 |
lthy |
|
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
181 |
|> target_notes kind global_facts (Attrib.partial_evaluation lthy local_facts) |
47249
c0481c3c2a6c
added Attrib.global_notes/local_notes/generic_notes convenience;
wenzelm
parents:
47246
diff
changeset
|
182 |
|> Attrib.local_notes kind local_facts |
38309 | 183 |
end; |
184 |
||
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
185 |
fun locale_notes locale kind global_facts local_facts = |
56141
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
wenzelm
parents:
56140
diff
changeset
|
186 |
Local_Theory.background_theory |
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
wenzelm
parents:
56140
diff
changeset
|
187 |
(Attrib.global_notes kind (Attrib.map_facts (K []) global_facts) #> snd) #> |
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
188 |
(fn lthy => lthy |> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
189 |
Local_Theory.target (fn ctxt => ctxt |> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
190 |
Locale.add_thmss locale kind (standard_facts lthy ctxt local_facts))) #> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
191 |
(fn lthy => lthy |> |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
192 |
Local_Theory.map_contexts (fn level => fn ctxt => |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
193 |
if level = 0 orelse level = Local_Theory.level lthy then ctxt |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
194 |
else ctxt |> Attrib.local_notes kind (standard_facts lthy ctxt local_facts) |> snd)); |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
195 |
|
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
196 |
end; |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
197 |
|
38309 | 198 |
|
199 |
(* abbrev *) |
|
200 |
||
57068 | 201 |
fun background_abbrev (b, t) = |
202 |
Local_Theory.background_theory_result (Sign.add_abbrev Print_Mode.internal (b, t)) |
|
203 |
||
38309 | 204 |
fun abbrev target_abbrev prmode ((b, mx), t) lthy = |
205 |
let |
|
42360 | 206 |
val thy_ctxt = Proof_Context.init_global (Proof_Context.theory_of lthy); |
38309 | 207 |
|
47292
1884d34e9aab
export into target context (again), to retain its 'defines' (e.g. abbreviation lcoeff in theory HOL/Algebra/UnivPoly);
wenzelm
parents:
47291
diff
changeset
|
208 |
val t' = Assumption.export_term lthy (Local_Theory.target_of lthy) t; |
47290 | 209 |
val xs = map Free (sort (Variable.fixed_ord lthy o pairself #1) (Variable.add_fixed lthy t' [])); |
38309 | 210 |
val u = fold_rev lambda xs t'; |
47290 | 211 |
val global_rhs = singleton (Variable.polymorphic thy_ctxt) u; |
38309 | 212 |
|
213 |
val extra_tfrees = |
|
214 |
subtract (op =) (Term.add_tfreesT (Term.fastype_of u) []) (Term.add_tfrees u []); |
|
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
215 |
val mx' = check_mixfix lthy (b, extra_tfrees) mx; |
38309 | 216 |
in |
217 |
lthy |
|
38312
9dd57db3c0f2
moved extra_tfrees check for mixfix syntax to Generic_Target
haftmann
parents:
38311
diff
changeset
|
218 |
|> target_abbrev prmode (b, mx') (global_rhs, t') xs |
42360 | 219 |
|> Proof_Context.add_abbrev Print_Mode.internal (b, t) |> snd |
38309 | 220 |
|> Local_Defs.fixed_abbrev ((b, NoSyn), t) |
221 |
end; |
|
222 |
||
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
223 |
|
47081
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
224 |
(* declaration *) |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
225 |
|
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
226 |
fun background_declaration decl lthy = |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
227 |
let |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
228 |
val theory_decl = |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
229 |
Local_Theory.standard_form lthy |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
230 |
(Proof_Context.init_global (Proof_Context.theory_of lthy)) decl; |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
231 |
in Local_Theory.background_theory (Context.theory_map theory_decl) lthy end; |
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
232 |
|
47246
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47245
diff
changeset
|
233 |
fun locale_declaration locale syntax decl lthy = lthy |
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47245
diff
changeset
|
234 |
|> Local_Theory.target (fn ctxt => ctxt |> |
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47245
diff
changeset
|
235 |
Locale.add_declaration locale syntax |
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47245
diff
changeset
|
236 |
(Morphism.transform (Local_Theory.standard_morphism lthy ctxt) decl)); |
2bbab021c0e6
clarified Named_Target.target_declaration: propagate through other levels as well;
wenzelm
parents:
47245
diff
changeset
|
237 |
|
47282 | 238 |
fun standard_declaration pred decl lthy = |
239 |
Local_Theory.map_contexts (fn level => fn ctxt => |
|
57065 | 240 |
if pred (Local_Theory.level lthy, level) |
57057 | 241 |
then Context.proof_map (Local_Theory.standard_form lthy ctxt decl) ctxt |
47282 | 242 |
else ctxt) lthy; |
243 |
||
244 |
||
245 |
(* const declaration *) |
|
246 |
||
247 |
fun generic_const same_shape prmode ((b, mx), t) context = |
|
248 |
let |
|
249 |
val const_alias = |
|
250 |
if same_shape then |
|
251 |
(case t of |
|
252 |
Const (c, T) => |
|
253 |
let |
|
254 |
val thy = Context.theory_of context; |
|
255 |
val ctxt = Context.proof_of context; |
|
256 |
in |
|
47291
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
257 |
(case Type_Infer_Context.const_type ctxt c of |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
258 |
SOME T' => if Sign.typ_equiv thy (T, T') then SOME c else NONE |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
259 |
| NONE => NONE) |
47282 | 260 |
end |
261 |
| _ => NONE) |
|
262 |
else NONE; |
|
263 |
in |
|
264 |
(case const_alias of |
|
265 |
SOME c => |
|
266 |
context |
|
267 |
|> Context.mapping (Sign.const_alias b c) (Proof_Context.const_alias b c) |
|
268 |
|> Morphism.form (Proof_Context.generic_notation true prmode [(t, mx)]) |
|
269 |
| NONE => |
|
270 |
context |
|
47288
b79bf8288b29
clarified generic_const vs. close_schematic_term;
wenzelm
parents:
47286
diff
changeset
|
271 |
|> Proof_Context.generic_add_abbrev Print_Mode.internal (b, Term.close_schematic_term t) |
47282 | 272 |
|-> (fn (const as Const (c, _), _) => same_shape ? |
273 |
(Proof_Context.generic_revert_abbrev (#1 prmode) c #> |
|
274 |
Morphism.form (Proof_Context.generic_notation true prmode [(const, mx)])))) |
|
275 |
end; |
|
276 |
||
277 |
fun const_declaration pred prmode ((b, mx), rhs) = |
|
278 |
standard_declaration pred (fn phi => |
|
279 |
let |
|
280 |
val b' = Morphism.binding phi b; |
|
281 |
val rhs' = Morphism.term phi rhs; |
|
282 |
val same_shape = Term.aconv_untyped (rhs, rhs'); |
|
47288
b79bf8288b29
clarified generic_const vs. close_schematic_term;
wenzelm
parents:
47286
diff
changeset
|
283 |
in generic_const same_shape prmode ((b', mx), rhs') end); |
47280 | 284 |
|
57072
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
285 |
fun locale_const_declaration locale prmode ((b, mx), rhs) = |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
286 |
locale_declaration locale true (fn phi => |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
287 |
let |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
288 |
val b' = Morphism.binding phi b; |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
289 |
val rhs' = Morphism.term phi rhs; |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
290 |
val same_shape = Term.aconv_untyped (rhs, rhs'); |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
291 |
in generic_const same_shape prmode ((b', mx), rhs') end) |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
292 |
#> const_declaration |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
293 |
(fn (this, other) => other <> 0 andalso this <> other) prmode ((b, mx), rhs); |
dfac6ef0ca28
moved const declaration further down in bootstrap hierarchy: keep Named_Target free of low-level stuff
haftmann
parents:
57068
diff
changeset
|
294 |
|
47081
5e70b457b704
uniform Generic_Target.standard_declaration, which uses the standard morphism for each context (NB: targets like "interpretation" appear like "theory" but declare local type parameters);
wenzelm
parents:
47080
diff
changeset
|
295 |
|
52153
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
296 |
(* registrations and dependencies *) |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
297 |
|
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
298 |
val theory_registration = |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
299 |
Local_Theory.raw_theory o Context.theory_map ooo Locale.add_registration; |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
300 |
|
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
301 |
fun locale_dependency locale dep_morph mixin export = |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
302 |
(Local_Theory.raw_theory ooo Locale.add_dependency locale) dep_morph mixin export |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
303 |
#> Local_Theory.activate_nonbrittle dep_morph mixin export; |
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
304 |
|
f5773a46cf05
more specific structure for registration into theory and dependency onto locale
haftmann
parents:
47313
diff
changeset
|
305 |
|
45352 | 306 |
|
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
307 |
(** primitive theory operations **) |
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
308 |
|
47284
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
309 |
fun background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) lthy = |
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
310 |
let |
47291
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
311 |
val params = type_params @ term_params; |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
312 |
val mx' = check_mixfix_global (b, null params) mx; |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
313 |
|
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
314 |
val (const, lthy2) = lthy |
47291
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
315 |
|> Local_Theory.background_theory_result (Sign.declare_const lthy ((b, U), mx')); |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
316 |
val lhs = Term.list_comb (const, params); |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
317 |
|
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
318 |
val ((_, def), lthy3) = lthy2 |
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
319 |
|> Local_Theory.background_theory_result |
46916
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45413
diff
changeset
|
320 |
(Thm.add_def lthy2 false false |
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45413
diff
changeset
|
321 |
(Thm.def_binding_optional b b_def, Logic.mk_equals (lhs, rhs))); |
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
322 |
in ((lhs, def), lthy3) end; |
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
323 |
|
47284
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
324 |
fun theory_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) = |
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
325 |
background_foundation (((b, U), mx), (b_def, rhs)) (type_params, term_params) |
57057 | 326 |
#-> (fn (lhs, def) => |
57065 | 327 |
const_declaration (op <>) Syntax.mode_default ((b, mx), lhs) |
57057 | 328 |
#> pair (lhs, def)); |
47284
0e65b6a016dc
clarified background_foundation vs. theory_foundation (with const_declaration);
wenzelm
parents:
47283
diff
changeset
|
329 |
|
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
330 |
fun theory_notes kind global_facts local_facts = |
56141
c06202417c4a
back to a form of hybrid facts, to reduce performance impact of ed92ce2ac88e;
wenzelm
parents:
56140
diff
changeset
|
331 |
Local_Theory.background_theory (Attrib.global_notes kind global_facts #> snd) #> |
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
332 |
(fn lthy => lthy |> Local_Theory.map_contexts (fn level => fn ctxt => |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
333 |
if level = Local_Theory.level lthy then ctxt |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
334 |
else |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
335 |
ctxt |> Attrib.local_notes kind |
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47249
diff
changeset
|
336 |
(Element.transform_facts (Local_Theory.standard_morphism lthy ctxt) local_facts) |> snd)); |
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
337 |
|
47286
392c4cd97e5c
more uniform theory_abbrev with const_declaration;
wenzelm
parents:
47285
diff
changeset
|
338 |
fun theory_abbrev prmode (b, mx) (t, _) xs = |
392c4cd97e5c
more uniform theory_abbrev with const_declaration;
wenzelm
parents:
47285
diff
changeset
|
339 |
Local_Theory.background_theory_result |
38757
2b3e054ae6fc
renamed Local_Theory.theory(_result) to Local_Theory.background_theory(_result) to emphasize that this belongs to the infrastructure and is rarely appropriate in user-space tools;
wenzelm
parents:
38341
diff
changeset
|
340 |
(Sign.add_abbrev (#1 prmode) (b, t) #-> |
47291
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
341 |
(fn (lhs, _) => (* FIXME type_params!? *) |
6a641856a0e9
better drop background syntax if entity depends on parameters;
wenzelm
parents:
47290
diff
changeset
|
342 |
Sign.notation true prmode [(lhs, check_mixfix_global (b, null xs) mx)] #> pair lhs)) |
57057 | 343 |
#-> (fn lhs => |
57065 | 344 |
const_declaration (op <>) prmode |
47293
052cd5f1a591
another attempt to avoid duplication of global vs. local syntax (reminiscent of old fork_mixfix);
wenzelm
parents:
47292
diff
changeset
|
345 |
((b, if null xs then NoSyn else mx), Term.list_comb (Logic.unvarify_global lhs, xs))); |
38341
72dba5bd5f63
moved theory-level target operation fragements to Generic_Target; adjusted bootstrap order
haftmann
parents:
38315
diff
changeset
|
346 |
|
47279
4bab649dedf0
clarified standard_declaration vs. theory_declaration;
wenzelm
parents:
47276
diff
changeset
|
347 |
fun theory_declaration decl = |
57065 | 348 |
background_declaration decl #> standard_declaration (K true) decl; |
47279
4bab649dedf0
clarified standard_declaration vs. theory_declaration;
wenzelm
parents:
47276
diff
changeset
|
349 |
|
38309 | 350 |
end; |