author | wenzelm |
Sat, 23 May 2015 17:19:37 +0200 | |
changeset 60299 | 5ae2a2e74c93 |
parent 60246 | 1f9cd721ece2 |
child 60338 | a808b57d5b0d |
permissions | -rw-r--r-- |
25519 | 1 |
(* Title: Pure/Isar/overloading.ML |
2 |
Author: Florian Haftmann, TU Muenchen |
|
3 |
||
4 |
Overloaded definitions without any discipline. |
|
5 |
*) |
|
6 |
||
7 |
signature OVERLOADING = |
|
8 |
sig |
|
26238 | 9 |
type improvable_syntax |
39378
df86b1b4ce10
more precise name for activation of improveable syntax
haftmann
parents:
38757
diff
changeset
|
10 |
val activate_improvable_syntax: Proof.context -> Proof.context |
26238 | 11 |
val map_improvable_syntax: (improvable_syntax -> improvable_syntax) |
12 |
-> Proof.context -> Proof.context |
|
26520 | 13 |
val set_primary_constraints: Proof.context -> Proof.context |
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
14 |
|
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
15 |
val overloading: (string * (string * typ) * bool) list -> theory -> local_theory |
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
16 |
val overloading_cmd: (string * string * bool) list -> theory -> local_theory |
25519 | 17 |
end; |
18 |
||
19 |
structure Overloading: OVERLOADING = |
|
20 |
struct |
|
21 |
||
42404 | 22 |
(* generic check/uncheck combinators for improvable constants *) |
26238 | 23 |
|
26249 | 24 |
type improvable_syntax = ((((string * typ) list * (string * typ) list) * |
26730 | 25 |
((((string * typ -> (typ * typ) option) * (string * typ -> (typ * term) option)) * bool) * |
26249 | 26 |
(term * term) list)) * bool); |
25519 | 27 |
|
42404 | 28 |
structure Improvable_Syntax = Proof_Data |
33519 | 29 |
( |
26249 | 30 |
type T = { |
26520 | 31 |
primary_constraints: (string * typ) list, |
32 |
secondary_constraints: (string * typ) list, |
|
26249 | 33 |
improve: string * typ -> (typ * typ) option, |
34 |
subst: string * typ -> (typ * term) option, |
|
26730 | 35 |
consider_abbrevs: bool, |
26249 | 36 |
unchecks: (term * term) list, |
37 |
passed: bool |
|
38 |
}; |
|
59150 | 39 |
val empty: T = { |
26520 | 40 |
primary_constraints = [], |
41 |
secondary_constraints = [], |
|
26238 | 42 |
improve = K NONE, |
43 |
subst = K NONE, |
|
26730 | 44 |
consider_abbrevs = false, |
26238 | 45 |
unchecks = [], |
46 |
passed = true |
|
47 |
}; |
|
59150 | 48 |
fun init _ = empty; |
26238 | 49 |
); |
25536 | 50 |
|
42404 | 51 |
fun map_improvable_syntax f = Improvable_Syntax.map (fn {primary_constraints, |
52 |
secondary_constraints, improve, subst, consider_abbrevs, unchecks, passed} => |
|
40782 | 53 |
let |
26730 | 54 |
val (((primary_constraints', secondary_constraints'), |
55 |
(((improve', subst'), consider_abbrevs'), unchecks')), passed') |
|
56 |
= f (((primary_constraints, secondary_constraints), |
|
57 |
(((improve, subst), consider_abbrevs), unchecks)), passed) |
|
42404 | 58 |
in |
59 |
{primary_constraints = primary_constraints', secondary_constraints = secondary_constraints', |
|
26730 | 60 |
improve = improve', subst = subst', consider_abbrevs = consider_abbrevs', |
42404 | 61 |
unchecks = unchecks', passed = passed'} |
40782 | 62 |
end); |
26238 | 63 |
|
26249 | 64 |
val mark_passed = (map_improvable_syntax o apsnd) (K true); |
26238 | 65 |
|
66 |
fun improve_term_check ts ctxt = |
|
25519 | 67 |
let |
42388 | 68 |
val thy = Proof_Context.theory_of ctxt; |
69 |
||
42404 | 70 |
val {secondary_constraints, improve, subst, consider_abbrevs, passed, ...} = |
71 |
Improvable_Syntax.get ctxt; |
|
42360 | 72 |
val is_abbrev = consider_abbrevs andalso Proof_Context.abbrev_mode ctxt; |
26730 | 73 |
val passed_or_abbrev = passed orelse is_abbrev; |
42388 | 74 |
fun accumulate_improvements (Const (c, ty)) = |
75 |
(case improve (c, ty) of |
|
76 |
SOME ty_ty' => Sign.typ_match thy ty_ty' |
|
26238 | 77 |
| _ => I) |
78 |
| accumulate_improvements _ = I; |
|
79 |
val improvements = (fold o fold_aterms) accumulate_improvements ts Vartab.empty; |
|
32035 | 80 |
val ts' = (map o map_types) (Envir.subst_type improvements) ts; |
42388 | 81 |
fun apply_subst t = |
82 |
Envir.expand_term |
|
83 |
(fn Const (c, ty) => |
|
84 |
(case subst (c, ty) of |
|
85 |
SOME (ty', t') => |
|
86 |
if Sign.typ_instance thy (ty, ty') |
|
26238 | 87 |
then SOME (ty', apply_subst t') else NONE |
88 |
| NONE => NONE) |
|
26259 | 89 |
| _ => NONE) t; |
26730 | 90 |
val ts'' = if is_abbrev then ts' else map apply_subst ts'; |
40782 | 91 |
in |
42404 | 92 |
if eq_list (op aconv) (ts, ts'') andalso passed_or_abbrev then NONE |
93 |
else if passed_or_abbrev then SOME (ts'', ctxt) |
|
94 |
else |
|
95 |
SOME (ts'', ctxt |
|
96 |
|> fold (Proof_Context.add_const_constraint o apsnd SOME) secondary_constraints |
|
97 |
|> mark_passed) |
|
26238 | 98 |
end; |
25519 | 99 |
|
31698 | 100 |
fun rewrite_liberal thy unchecks t = |
42404 | 101 |
(case try (Pattern.rewrite_term thy unchecks []) t of |
102 |
NONE => NONE |
|
103 |
| SOME t' => if t aconv t' then NONE else SOME t'); |
|
31698 | 104 |
|
26238 | 105 |
fun improve_term_uncheck ts ctxt = |
25519 | 106 |
let |
42360 | 107 |
val thy = Proof_Context.theory_of ctxt; |
42404 | 108 |
val {unchecks, ...} = Improvable_Syntax.get ctxt; |
31698 | 109 |
val ts' = map (rewrite_liberal thy unchecks) ts; |
110 |
in if exists is_some ts' then SOME (map2 the_default ts ts', ctxt) else NONE end; |
|
26238 | 111 |
|
26520 | 112 |
fun set_primary_constraints ctxt = |
42404 | 113 |
let val {primary_constraints, ...} = Improvable_Syntax.get ctxt; |
42360 | 114 |
in fold (Proof_Context.add_const_constraint o apsnd SOME) primary_constraints ctxt end; |
26259 | 115 |
|
39378
df86b1b4ce10
more precise name for activation of improveable syntax
haftmann
parents:
38757
diff
changeset
|
116 |
val activate_improvable_syntax = |
26259 | 117 |
Context.proof_map |
45444 | 118 |
(Syntax_Phases.term_check' 0 "improvement" improve_term_check |
119 |
#> Syntax_Phases.term_uncheck' 0 "improvement" improve_term_uncheck) |
|
26520 | 120 |
#> set_primary_constraints; |
26259 | 121 |
|
122 |
||
42404 | 123 |
(* overloading target *) |
26259 | 124 |
|
38382 | 125 |
structure Data = Proof_Data |
26259 | 126 |
( |
127 |
type T = ((string * typ) * (string * bool)) list; |
|
128 |
fun init _ = []; |
|
129 |
); |
|
130 |
||
38382 | 131 |
val get_overloading = Data.get o Local_Theory.target_of; |
132 |
val map_overloading = Local_Theory.target o Data.map; |
|
26259 | 133 |
|
42404 | 134 |
fun operation lthy b = |
135 |
get_overloading lthy |
|
30519
c05c0199826f
coherent binding policy with primitive target operations
haftmann
parents:
29606
diff
changeset
|
136 |
|> get_first (fn ((c, _), (v, checked)) => |
38382 | 137 |
if Binding.name_of b = v then SOME (c, (v, checked)) else NONE); |
26259 | 138 |
|
32343 | 139 |
fun synchronize_syntax ctxt = |
140 |
let |
|
38382 | 141 |
val overloading = Data.get ctxt; |
42404 | 142 |
fun subst (c, ty) = |
143 |
(case AList.lookup (op =) overloading (c, ty) of |
|
144 |
SOME (v, _) => SOME (ty, Free (v, ty)) |
|
145 |
| NONE => NONE); |
|
32343 | 146 |
val unchecks = |
147 |
map (fn (c_ty as (_, ty), (v, _)) => (Free (v, ty), Const c_ty)) overloading; |
|
148 |
in |
|
149 |
ctxt |
|
150 |
|> map_improvable_syntax (K ((([], []), (((K NONE, subst), false), unchecks)), false)) |
|
46916
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45444
diff
changeset
|
151 |
end; |
26259 | 152 |
|
38382 | 153 |
fun define_overloaded (c, U) (v, checked) (b_def, rhs) = |
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:
38382
diff
changeset
|
154 |
Local_Theory.background_theory_result |
42375
774df7c59508
report Name_Space.declare/define, relatively to context;
wenzelm
parents:
42360
diff
changeset
|
155 |
(Thm.add_def_global (not checked) true |
46916
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45444
diff
changeset
|
156 |
(Thm.def_binding_optional (Binding.name v) b_def, |
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45444
diff
changeset
|
157 |
Logic.mk_equals (Const (c, Term.fastype_of rhs), rhs))) |
38382 | 158 |
##> map_overloading (filter_out (fn (_, (v', _)) => v' = v)) |
47245
ff1770df59b8
Local_Theory.map_contexts with explicit level indication: 0 = main target at bottom;
wenzelm
parents:
47081
diff
changeset
|
159 |
##> Local_Theory.map_contexts (K synchronize_syntax) |
46916
e7ea35b41e2d
Local_Theory.define no longer hard-wires default theorem name -- targets/packages need to take care of it;
wenzelm
parents:
45444
diff
changeset
|
160 |
#-> (fn (_, def) => pair (Const (c, U), def)); |
26259 | 161 |
|
47289 | 162 |
fun foundation (((b, U), mx), (b_def, rhs)) params lthy = |
42404 | 163 |
(case operation lthy b of |
164 |
SOME (c, (v, checked)) => |
|
165 |
if mx <> NoSyn |
|
166 |
then error ("Illegal mixfix syntax for overloaded constant " ^ quote c) |
|
167 |
else lthy |> define_overloaded (c, U) (v, checked) (b_def, rhs) |
|
47289 | 168 |
| NONE => lthy |> Generic_Target.theory_foundation (((b, U), mx), (b_def, rhs)) params); |
25519 | 169 |
|
25606 | 170 |
fun pretty lthy = |
171 |
let |
|
172 |
val overloading = get_overloading lthy; |
|
173 |
fun pr_operation ((c, ty), (v, _)) = |
|
42359 | 174 |
Pretty.block (Pretty.breaks |
55304 | 175 |
[Pretty.str v, Pretty.str "==", Proof_Context.pretty_const lthy c, |
42359 | 176 |
Pretty.str "::", Syntax.pretty_typ lthy ty]); |
60246 | 177 |
in |
178 |
[Pretty.block |
|
179 |
(Pretty.fbreaks (Pretty.keyword1 "overloading" :: map pr_operation overloading))] |
|
180 |
end; |
|
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
181 |
|
38382 | 182 |
fun conclude lthy = |
183 |
let |
|
184 |
val overloading = get_overloading lthy; |
|
40782 | 185 |
val _ = |
186 |
if null overloading then () |
|
187 |
else |
|
42404 | 188 |
error ("Missing definition(s) for parameter(s) " ^ |
189 |
commas_quote (map (Syntax.string_of_term lthy o Const o fst) overloading)); |
|
38382 | 190 |
in lthy end; |
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
191 |
|
38382 | 192 |
fun gen_overloading prep_const raw_overloading thy = |
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
193 |
let |
42360 | 194 |
val ctxt = Proof_Context.init_global thy; |
38382 | 195 |
val _ = if null raw_overloading then error "At least one parameter must be given" else (); |
196 |
val overloading = raw_overloading |> map (fn (v, const, checked) => |
|
197 |
(Term.dest_Const (prep_const ctxt const), (v, checked))); |
|
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
198 |
in |
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
199 |
thy |
56056
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents:
55763
diff
changeset
|
200 |
|> Sign.change_begin |
42360 | 201 |
|> Proof_Context.init_global |
38382 | 202 |
|> Data.put overloading |
203 |
|> fold (fn ((_, ty), (v, _)) => Variable.declare_names (Free (v, ty))) overloading |
|
39378
df86b1b4ce10
more precise name for activation of improveable syntax
haftmann
parents:
38757
diff
changeset
|
204 |
|> activate_improvable_syntax |
38382 | 205 |
|> synchronize_syntax |
59876 | 206 |
|> Local_Theory.init (Sign.naming_of thy) |
38382 | 207 |
{define = Generic_Target.define foundation, |
47250
6523a21076a8
clarified Generic_Target.notes: always perform Attrib.partial_evaluation;
wenzelm
parents:
47245
diff
changeset
|
208 |
notes = Generic_Target.notes Generic_Target.theory_notes, |
47286
392c4cd97e5c
more uniform theory_abbrev with const_declaration;
wenzelm
parents:
47279
diff
changeset
|
209 |
abbrev = Generic_Target.abbrev Generic_Target.theory_abbrev, |
47279
4bab649dedf0
clarified standard_declaration vs. theory_declaration;
wenzelm
parents:
47250
diff
changeset
|
210 |
declaration = K Generic_Target.theory_declaration, |
56723
a8f71445c265
subscription as target-specific implementation device
haftmann
parents:
56056
diff
changeset
|
211 |
subscription = Generic_Target.theory_registration, |
38382 | 212 |
pretty = pretty, |
56056
4d46d53566e6
more efficient local theory operations, by imposing a linear change discipline on the main types/consts tables, in order to speed-up Proof_Context.transfer_syntax required for Local_Theory.raw_theory_result;
wenzelm
parents:
55763
diff
changeset
|
213 |
exit = conclude #> Local_Theory.target_of #> Sign.change_end_local} |
38342
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
214 |
end; |
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
215 |
|
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
216 |
val overloading = gen_overloading (fn ctxt => Syntax.check_term ctxt o Const); |
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
217 |
val overloading_cmd = gen_overloading Syntax.read_term; |
09d4a04d5c2e
moved overloading target formally to overloading.ML
haftmann
parents:
36610
diff
changeset
|
218 |
|
25519 | 219 |
end; |