author | wenzelm |
Mon, 25 Aug 2014 12:58:20 +0200 | |
changeset 58045 | ab2483fad861 |
parent 58033 | 154ae20ead35 |
child 58866 | f81e11391562 |
permissions | -rw-r--r-- |
27389 | 1 |
(* Title: Pure/ML/ml_thms.ML |
2 |
Author: Makarius |
|
3 |
||
45592 | 4 |
Attribute source and theorem values within ML. |
27389 | 5 |
*) |
6 |
||
7 |
signature ML_THMS = |
|
8 |
sig |
|
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
56304
diff
changeset
|
9 |
val the_attributes: Proof.context -> int -> Token.src list |
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
10 |
val the_thmss: Proof.context -> thm list list |
56199 | 11 |
val get_stored_thms: unit -> thm list |
12 |
val get_stored_thm: unit -> thm |
|
13 |
val store_thms: string * thm list -> unit |
|
14 |
val store_thm: string * thm -> unit |
|
15 |
val bind_thm: string * thm -> unit |
|
16 |
val bind_thms: string * thm list -> unit |
|
27389 | 17 |
end; |
18 |
||
19 |
structure ML_Thms: ML_THMS = |
|
20 |
struct |
|
21 |
||
45592 | 22 |
(* auxiliary data *) |
27389 | 23 |
|
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
24 |
type thms = (string * bool) * thm list; (*name, single, value*) |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
25 |
|
45592 | 26 |
structure Data = Proof_Data |
27389 | 27 |
( |
58011
bc6bced136e5
tuned signature -- moved type src to Token, without aliases;
wenzelm
parents:
56304
diff
changeset
|
28 |
type T = Token.src list Inttab.table * thms list; |
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
29 |
fun init _ = (Inttab.empty, []); |
27389 | 30 |
); |
31 |
||
45592 | 32 |
val put_attributes = Data.map o apfst o Inttab.update; |
33 |
fun the_attributes ctxt name = the (Inttab.lookup (fst (Data.get ctxt)) name); |
|
34 |
||
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
35 |
val get_thmss = snd o Data.get; |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
36 |
val the_thmss = map snd o get_thmss; |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
37 |
val cons_thms = Data.map o apsnd o cons; |
45592 | 38 |
|
39 |
||
40 |
(* attribute source *) |
|
27389 | 41 |
|
53171 | 42 |
val _ = Theory.setup |
58028
e4250d370657
tuned signature -- define some elementary operations earlier;
wenzelm
parents:
58011
diff
changeset
|
43 |
(ML_Antiquotation.declaration @{binding attributes} (Scan.lift Parse.attribs) |
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
44 |
(fn _ => fn raw_srcs => fn ctxt => |
53171 | 45 |
let |
46 |
val i = serial (); |
|
58045 | 47 |
|
53171 | 48 |
val (a, ctxt') = ctxt |
58045 | 49 |
|> ML_Antiquotation.variant "attributes" |
50 |
||> put_attributes (i, Attrib.check_attribs ctxt raw_srcs); |
|
53171 | 51 |
val ml = |
52 |
("val " ^ a ^ " = ML_Thms.the_attributes ML_context " ^ |
|
53 |
string_of_int i ^ ";\n", "Isabelle." ^ a); |
|
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
54 |
in (K ml, ctxt') end)); |
45592 | 55 |
|
56 |
||
57 |
(* fact references *) |
|
27389 | 58 |
|
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
59 |
fun thm_binding kind is_single thms ctxt = |
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
60 |
let |
53169
e2d31807cbf6
clarified type ML_Context.antiq: context parser maintains compilation context, declaration is applied to final context;
wenzelm
parents:
53168
diff
changeset
|
61 |
val initial = null (get_thmss ctxt); |
56072 | 62 |
val (name, ctxt') = ML_Antiquotation.variant kind ctxt; |
53169
e2d31807cbf6
clarified type ML_Context.antiq: context parser maintains compilation context, declaration is applied to final context;
wenzelm
parents:
53168
diff
changeset
|
63 |
val ctxt'' = cons_thms ((name, is_single), thms) ctxt'; |
27389 | 64 |
|
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
65 |
val ml_body = "Isabelle." ^ name; |
53169
e2d31807cbf6
clarified type ML_Context.antiq: context parser maintains compilation context, declaration is applied to final context;
wenzelm
parents:
53168
diff
changeset
|
66 |
fun decl final_ctxt = |
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
67 |
if initial then |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
68 |
let |
53169
e2d31807cbf6
clarified type ML_Context.antiq: context parser maintains compilation context, declaration is applied to final context;
wenzelm
parents:
53168
diff
changeset
|
69 |
val binds = get_thmss final_ctxt |> map (fn ((a, b), _) => (b ? enclose "[" "]") a); |
48782
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
70 |
val ml_env = "val [" ^ commas binds ^ "] = ML_Thms.the_thmss ML_context;\n"; |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
71 |
in (ml_env, ml_body) end |
e955964d89cb
more direct embedding of abstract thm values into the ML environment -- avoidance of repeated ML_Thms.the_thm(s) considerably reduces compilation time for Poly/ML 5.4.x;
wenzelm
parents:
48776
diff
changeset
|
72 |
else ("", ml_body); |
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
73 |
in (decl, ctxt'') end; |
27389 | 74 |
|
53171 | 75 |
val _ = Theory.setup |
56205 | 76 |
(ML_Antiquotation.declaration @{binding thm} (Attrib.thm >> single) (K (thm_binding "thm" true)) #> |
77 |
ML_Antiquotation.declaration @{binding thms} Attrib.thms (K (thm_binding "thms" false))); |
|
27389 | 78 |
|
79 |
||
80 |
(* ad-hoc goals *) |
|
81 |
||
30266
970bf4f594c9
ML antiquotation @{lemma}: allow 'and' list, proper simultaneous type-checking;
wenzelm
parents:
29606
diff
changeset
|
82 |
val and_ = Args.$$$ "and"; |
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27521
diff
changeset
|
83 |
val by = Args.$$$ "by"; |
55111 | 84 |
val goal = Scan.unless (by || and_) Args.name_inner_syntax; |
27389 | 85 |
|
53171 | 86 |
val _ = Theory.setup |
56205 | 87 |
(ML_Antiquotation.declaration @{binding lemma} |
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
88 |
(Scan.lift (Args.mode "open" -- Parse.enum1 "and" (Scan.repeat1 goal) -- |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
89 |
(by |-- Method.parse -- Scan.option Method.parse))) |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
90 |
(fn _ => fn ((is_open, raw_propss), (m1, m2)) => fn ctxt => |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
91 |
let |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
92 |
val _ = Context_Position.reports ctxt (maps Method.reports_of (m1 :: the_list m2)); |
55515 | 93 |
|
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
94 |
val propss = burrow (map (rpair []) o Syntax.read_props ctxt) raw_propss; |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
95 |
val prep_result = Goal.norm_result ctxt #> not is_open ? Thm.close_derivation; |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
96 |
fun after_qed res goal_ctxt = |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
97 |
Proof_Context.put_thms false (Auto_Bind.thisN, |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
98 |
SOME (map prep_result (Proof_Context.export goal_ctxt ctxt (flat res)))) goal_ctxt; |
53169
e2d31807cbf6
clarified type ML_Context.antiq: context parser maintains compilation context, declaration is applied to final context;
wenzelm
parents:
53168
diff
changeset
|
99 |
|
56069
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
100 |
val ctxt' = ctxt |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
101 |
|> Proof.theorem NONE after_qed propss |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
102 |
|> Proof.global_terminal_proof (m1, m2); |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
103 |
val thms = |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
104 |
Proof_Context.get_fact ctxt' |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
105 |
(Facts.named (Proof_Context.full_name ctxt' (Binding.name Auto_Bind.thisN))); |
451d5b73f8cf
simplified programming interface to define ML antiquotations -- NB: the transformed context ignores updates of the context parser;
wenzelm
parents:
55997
diff
changeset
|
106 |
in thm_binding "lemma" (length (flat propss) = 1) thms ctxt end)); |
27389 | 107 |
|
56199 | 108 |
|
109 |
(* old-style theorem bindings *) |
|
110 |
||
111 |
structure Stored_Thms = Theory_Data |
|
112 |
( |
|
113 |
type T = thm list; |
|
114 |
val empty = []; |
|
115 |
fun extend _ = []; |
|
116 |
fun merge _ = []; |
|
117 |
); |
|
118 |
||
119 |
fun get_stored_thms () = Stored_Thms.get (ML_Context.the_global_context ()); |
|
120 |
val get_stored_thm = hd o get_stored_thms; |
|
121 |
||
122 |
fun ml_store get (name, ths) = |
|
123 |
let |
|
124 |
val ths' = Context.>>> (Context.map_theory_result |
|
125 |
(Global_Theory.store_thms (Binding.name name, ths))); |
|
126 |
val _ = Theory.setup (Stored_Thms.put ths'); |
|
127 |
val _ = |
|
128 |
if name = "" then () |
|
129 |
else if not (ML_Syntax.is_identifier name) then |
|
130 |
error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value") |
|
131 |
else |
|
56304
40274e4f5ebf
redirect ML_Compiler reports more directly: only the (big) parse tree report is deferred via Execution.print (NB: this does not work for asynchronous "diag" commands);
wenzelm
parents:
56275
diff
changeset
|
132 |
ML_Compiler.eval (ML_Compiler.verbose true ML_Compiler.flags) Position.none |
56199 | 133 |
(ML_Lex.tokenize ("val " ^ name ^ " = " ^ get ^ " ();")); |
134 |
val _ = Theory.setup (Stored_Thms.put []); |
|
135 |
in () end; |
|
136 |
||
137 |
val store_thms = ml_store "ML_Thms.get_stored_thms"; |
|
138 |
fun store_thm (name, th) = ml_store "ML_Thms.get_stored_thm" (name, [th]); |
|
139 |
||
140 |
fun bind_thm (name, thm) = store_thm (name, Drule.export_without_context thm); |
|
141 |
fun bind_thms (name, thms) = store_thms (name, map Drule.export_without_context thms); |
|
142 |
||
27389 | 143 |
end; |
144 |