author | wenzelm |
Tue, 01 Mar 2016 22:49:33 +0100 | |
changeset 62495 | 83db706d7771 |
parent 62494 | b90109b2487c |
child 62657 | cdd6db02eae8 |
permissions | -rw-r--r-- |
31325 | 1 |
(* Title: Pure/ML/ml_env.ML |
2 |
Author: Makarius |
|
3 |
||
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
4 |
Toplevel environment for Standard ML and Isabelle/ML within the |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
5 |
implicit context. |
31325 | 6 |
*) |
7 |
||
8 |
signature ML_ENV = |
|
9 |
sig |
|
10 |
val inherit: Context.generic -> Context.generic -> Context.generic |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
11 |
val forget_structure: string -> Context.generic -> Context.generic |
60746 | 12 |
val add_breakpoint: serial * (bool Unsynchronized.ref * Position.T) -> |
13 |
Context.generic -> Context.generic |
|
14 |
val get_breakpoint: Context.generic -> serial -> (bool Unsynchronized.ref * Position.T) option |
|
60858 | 15 |
val add_name_space: {SML: bool} -> ML_Name_Space.T -> Context.generic -> Context.generic |
62495 | 16 |
val make_name_space: {SML: bool, exchange: bool} -> ML_Name_Space.T |
17 |
val context: ML_Compiler0.context |
|
18 |
val name_space: ML_Name_Space.T |
|
36163
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
33519
diff
changeset
|
19 |
val check_functor: string -> unit |
31325 | 20 |
end |
21 |
||
22 |
structure ML_Env: ML_ENV = |
|
23 |
struct |
|
24 |
||
31328 | 25 |
(* context data *) |
26 |
||
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
27 |
type tables = |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
28 |
ML_Name_Space.valueVal Symtab.table * |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
29 |
ML_Name_Space.typeVal Symtab.table * |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
30 |
ML_Name_Space.fixityVal Symtab.table * |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
31 |
ML_Name_Space.structureVal Symtab.table * |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
32 |
ML_Name_Space.signatureVal Symtab.table * |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
33 |
ML_Name_Space.functorVal Symtab.table; |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
34 |
|
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
35 |
fun merge_tables |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
36 |
((val1, type1, fixity1, structure1, signature1, functor1), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
37 |
(val2, type2, fixity2, structure2, signature2, functor2)) : tables = |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
38 |
(Symtab.merge (K true) (val1, val2), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
39 |
Symtab.merge (K true) (type1, type2), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
40 |
Symtab.merge (K true) (fixity1, fixity2), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
41 |
Symtab.merge (K true) (structure1, structure2), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
42 |
Symtab.merge (K true) (signature1, signature2), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
43 |
Symtab.merge (K true) (functor1, functor2)); |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
44 |
|
60746 | 45 |
type data = |
46 |
{bootstrap: bool, |
|
47 |
tables: tables, |
|
48 |
sml_tables: tables, |
|
49 |
breakpoints: (bool Unsynchronized.ref * Position.T) Inttab.table}; |
|
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
50 |
|
60746 | 51 |
fun make_data (bootstrap, tables, sml_tables, breakpoints) : data = |
52 |
{bootstrap = bootstrap, tables = tables, sml_tables = sml_tables, breakpoints = breakpoints}; |
|
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
53 |
|
33519 | 54 |
structure Env = Generic_Data |
31325 | 55 |
( |
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
56 |
type T = data |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
57 |
val empty = |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
58 |
make_data (true, |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
59 |
(Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty), |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
60 |
(Symtab.make ML_Name_Space.initial_val, |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
61 |
Symtab.make ML_Name_Space.initial_type, |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
62 |
Symtab.make ML_Name_Space.initial_fixity, |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
63 |
Symtab.make ML_Name_Space.initial_structure, |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
64 |
Symtab.make ML_Name_Space.initial_signature, |
60746 | 65 |
Symtab.make ML_Name_Space.initial_functor), |
66 |
Inttab.empty); |
|
67 |
fun extend (data : T) = make_data (false, #tables data, #sml_tables data, #breakpoints data); |
|
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
68 |
fun merge (data : T * T) = |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
69 |
make_data (false, |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
56618
diff
changeset
|
70 |
merge_tables (apply2 #tables data), |
60746 | 71 |
merge_tables (apply2 #sml_tables data), |
72 |
Inttab.merge (K true) (apply2 #breakpoints data)); |
|
31325 | 73 |
); |
74 |
||
75 |
val inherit = Env.put o Env.get; |
|
76 |
||
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
77 |
fun forget_structure name = |
60746 | 78 |
Env.map (fn {bootstrap, tables, sml_tables, breakpoints} => |
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
79 |
let |
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
80 |
val _ = if bootstrap then ML_Name_Space.forget_global_structure name else (); |
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
81 |
val tables' = |
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
82 |
(#1 tables, #2 tables, #3 tables, Symtab.delete_safe name (#4 tables), #5 tables, #6 tables); |
60746 | 83 |
in make_data (bootstrap, tables', sml_tables, breakpoints) end); |
84 |
||
85 |
fun add_breakpoint breakpoint = |
|
86 |
Env.map (fn {bootstrap, tables, sml_tables, breakpoints} => |
|
87 |
let val breakpoints' = Inttab.update_new breakpoint breakpoints; |
|
88 |
in make_data (bootstrap, tables, sml_tables, breakpoints') end); |
|
89 |
||
90 |
val get_breakpoint = Inttab.lookup o #breakpoints o Env.get; |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59058
diff
changeset
|
91 |
|
31328 | 92 |
|
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
93 |
(* name space *) |
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
94 |
|
60858 | 95 |
fun add_name_space {SML} (space: ML_Name_Space.T) = |
96 |
Env.map (fn {bootstrap, tables, sml_tables, breakpoints} => |
|
97 |
let |
|
98 |
val (tables', sml_tables') = |
|
99 |
(tables, sml_tables) |> (if SML then apsnd else apfst) |
|
100 |
(fn (val1, type1, fixity1, structure1, signature1, functor1) => |
|
101 |
let |
|
102 |
val val2 = fold Symtab.update (#allVal space ()) val1; |
|
103 |
val type2 = fold Symtab.update (#allType space ()) type1; |
|
104 |
val fixity2 = fold Symtab.update (#allFix space ()) fixity1; |
|
105 |
val structure2 = fold Symtab.update (#allStruct space ()) structure1; |
|
106 |
val signature2 = fold Symtab.update (#allSig space ()) signature1; |
|
107 |
val functor2 = fold Symtab.update (#allFunct space ()) functor1; |
|
108 |
in (val2, type2, fixity2, structure2, signature2, functor2) end); |
|
109 |
in make_data (bootstrap, tables', sml_tables', breakpoints) end); |
|
110 |
||
62495 | 111 |
fun make_name_space {SML, exchange} : ML_Name_Space.T = |
31325 | 112 |
let |
113 |
fun lookup sel1 sel2 name = |
|
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
114 |
if SML then |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
115 |
Context.the_thread_data () |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
116 |
|> (fn context => Symtab.lookup (sel1 (#sml_tables (Env.get context))) name) |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
117 |
else |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
118 |
Context.thread_data () |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
119 |
|> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (#tables (Env.get context))) name) |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
120 |
|> (fn NONE => sel2 ML_Name_Space.global name | some => some); |
31325 | 121 |
|
122 |
fun all sel1 sel2 () = |
|
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
123 |
(if SML then |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
124 |
Context.the_thread_data () |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
125 |
|> (fn context => Symtab.dest (sel1 (#sml_tables (Env.get context)))) |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
126 |
else |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
127 |
Context.thread_data () |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
128 |
|> (fn NONE => [] | SOME context => Symtab.dest (sel1 (#tables (Env.get context)))) |
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
129 |
|> append (sel2 ML_Name_Space.global ())) |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
56618
diff
changeset
|
130 |
|> sort_distinct (string_ord o apply2 #1); |
31325 | 131 |
|
132 |
fun enter ap1 sel2 entry = |
|
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
133 |
if SML <> exchange then |
60746 | 134 |
Context.>> (Env.map (fn {bootstrap, tables, sml_tables, breakpoints} => |
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
135 |
let val sml_tables' = ap1 (Symtab.update entry) sml_tables |
60746 | 136 |
in make_data (bootstrap, tables, sml_tables', breakpoints) end)) |
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
137 |
else if is_some (Context.thread_data ()) then |
60746 | 138 |
Context.>> (Env.map (fn {bootstrap, tables, sml_tables, breakpoints} => |
56203
76c72f4d0667
clarified bootstrap process: switch to ML with context and antiquotations earlier;
wenzelm
parents:
48992
diff
changeset
|
139 |
let |
56275
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
140 |
val _ = if bootstrap then sel2 ML_Name_Space.global entry else (); |
600f432ab556
added command 'SML_file' for Standard ML without Isabelle/ML add-ons;
wenzelm
parents:
56203
diff
changeset
|
141 |
val tables' = ap1 (Symtab.update entry) tables; |
60746 | 142 |
in make_data (bootstrap, tables', sml_tables, breakpoints) end)) |
31325 | 143 |
else sel2 ML_Name_Space.global entry; |
144 |
in |
|
145 |
{lookupVal = lookup #1 #lookupVal, |
|
146 |
lookupType = lookup #2 #lookupType, |
|
147 |
lookupFix = lookup #3 #lookupFix, |
|
148 |
lookupStruct = lookup #4 #lookupStruct, |
|
149 |
lookupSig = lookup #5 #lookupSig, |
|
150 |
lookupFunct = lookup #6 #lookupFunct, |
|
151 |
enterVal = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal, |
|
152 |
enterType = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType, |
|
153 |
enterFix = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix, |
|
154 |
enterStruct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct, |
|
155 |
enterSig = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig, |
|
156 |
enterFunct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct, |
|
157 |
allVal = all #1 #allVal, |
|
158 |
allType = all #2 #allType, |
|
159 |
allFix = all #3 #allFix, |
|
160 |
allStruct = all #4 #allStruct, |
|
161 |
allSig = all #5 #allSig, |
|
162 |
allFunct = all #6 #allFunct} |
|
163 |
end; |
|
164 |
||
62495 | 165 |
val context: ML_Compiler0.context = |
166 |
{name_space = make_name_space {SML = false, exchange = false}, |
|
62493 | 167 |
here = Position.here oo Position.line_file, |
31325 | 168 |
print = writeln, |
169 |
error = error}; |
|
170 |
||
62495 | 171 |
val name_space = #name_space context; |
56618
874bdedb2313
added command 'SML_export' and 'SML_import' for exchange of toplevel bindings;
wenzelm
parents:
56275
diff
changeset
|
172 |
|
62495 | 173 |
val is_functor = is_some o #lookupFunct name_space; |
36163
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
33519
diff
changeset
|
174 |
|
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
33519
diff
changeset
|
175 |
fun check_functor name = |
36165 | 176 |
if not (is_functor "Table") (*mask dummy version of name_space*) orelse is_functor name then () |
36163
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
33519
diff
changeset
|
177 |
else error ("Unknown ML functor: " ^ quote name); |
823c9400eb62
proper checking of ML functors (in Poly/ML 5.2 or later);
wenzelm
parents:
33519
diff
changeset
|
178 |
|
31325 | 179 |
end; |