author  wenzelm 
Wed, 17 Sep 2008 21:27:32 +0200  
changeset 28270  7ada24ebea94 
parent 27895  e4f8763b971b 
child 28279  7d56de7e2305 
permissions  rwrr 
24581  1 
(* Title: Pure/ML/ml_context.ML 
24574  2 
ID: $Id$ 
3 
Author: Makarius 

4 

5 
ML context and antiquotations. 

6 
*) 

7 

8 
signature BASIC_ML_CONTEXT = 

9 
sig 

26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

10 
val bind_thm: string * thm > unit 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

11 
val bind_thms: string * thm list > unit 
24574  12 
val thm: xstring > thm 
13 
val thms: xstring > thm list 

25204  14 
end 
24574  15 

16 
signature ML_CONTEXT = 

17 
sig 

18 
include BASIC_ML_CONTEXT 

19 
val the_generic_context: unit > Context.generic 

26432
095e448b95a0
renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents:
26416
diff
changeset

20 
val the_global_context: unit > theory 
24574  21 
val the_local_context: unit > Proof.context 
26455  22 
val exec: (unit > unit) > Context.generic > Context.generic 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

23 
val name_space: ML_NameSpace.nameSpace 
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

24 
val stored_thms: thm list ref 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

25 
val ml_store_thm: string * thm > unit 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

26 
val ml_store_thms: string * thm list > unit 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

27 
type antiq = 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

28 
{struct_name: string, background: Proof.context} > 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

29 
(Proof.context > string * string) * Proof.context 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

30 
val add_antiq: string > 
27868  31 
(Position.T > Context.generic * Args.T list > antiq * (Context.generic * Args.T list)) > unit 
24574  32 
val trace: bool ref 
27723  33 
val eval_wrapper: (string > unit) * (string > 'a) > bool > Position.T > string > unit 
26455  34 
val eval: bool > Position.T > string > unit 
35 
val eval_file: Path.T > unit 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

36 
val eval_in: Proof.context option > bool > Position.T > string > unit 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

37 
val evaluate: Proof.context > (string > unit) * (string > 'b) > bool > 
25204  38 
string * (unit > 'a) option ref > string > 'a 
26455  39 
val expression: Position.T > string > string > string > Context.generic > Context.generic 
25204  40 
end 
24574  41 

42 
structure ML_Context: ML_CONTEXT = 

43 
struct 

44 

26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

45 
(** implicit ML context **) 
24574  46 

26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

47 
val the_generic_context = Context.the_thread_data; 
26432
095e448b95a0
renamed ML_Context.the_context to ML_Context.the_global_context;
wenzelm
parents:
26416
diff
changeset

48 
val the_global_context = Context.theory_of o the_generic_context; 
24574  49 
val the_local_context = Context.proof_of o the_generic_context; 
50 

26455  51 
fun exec (e: unit > unit) context = 
52 
(case Context.setmp_thread_data (SOME context) (fn () => (e (); Context.thread_data ())) () of 

53 
SOME context' => context' 

54 
 NONE => error "Missing context after execution"); 

26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

55 

a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

56 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

57 
(* ML name space *) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

58 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

59 
structure ML_Env = GenericDataFun 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

60 
( 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

61 
type T = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

62 
ML_NameSpace.valueVal Symtab.table * 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

63 
ML_NameSpace.typeVal Symtab.table * 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

64 
ML_NameSpace.fixityVal Symtab.table * 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

65 
ML_NameSpace.structureVal Symtab.table * 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

66 
ML_NameSpace.signatureVal Symtab.table * 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

67 
ML_NameSpace.functorVal Symtab.table; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

68 
val empty = (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

69 
val extend = I; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

70 
fun merge _ 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

71 
((val1, type1, fixity1, structure1, signature1, functor1), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

72 
(val2, type2, fixity2, structure2, signature2, functor2)) : T = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

73 
(Symtab.merge (K true) (val1, val2), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

74 
Symtab.merge (K true) (type1, type2), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

75 
Symtab.merge (K true) (fixity1, fixity2), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

76 
Symtab.merge (K true) (structure1, structure2), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

77 
Symtab.merge (K true) (signature1, signature2), 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

78 
Symtab.merge (K true) (functor1, functor2)); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

79 
); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

80 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

81 
val name_space: ML_NameSpace.nameSpace = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

82 
let 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

83 
fun lookup sel1 sel2 name = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

84 
Context.thread_data () 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

85 
> (fn NONE => NONE  SOME context => Symtab.lookup (sel1 (ML_Env.get context)) name) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

86 
> (fn NONE => sel2 ML_NameSpace.global name  some => some); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

87 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

88 
fun all sel1 sel2 () = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

89 
Context.thread_data () 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

90 
> (fn NONE => []  SOME context => Symtab.dest (sel1 (ML_Env.get context))) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

91 
> append (sel2 ML_NameSpace.global ()) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

92 
> sort_distinct (string_ord o pairself #1); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

93 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

94 
fun enter ap1 sel2 entry = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

95 
if is_some (Context.thread_data ()) then 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

96 
Context.>> (ML_Env.map (ap1 (Symtab.update entry))) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

97 
else sel2 ML_NameSpace.global entry; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

98 
in 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

99 
{lookupVal = lookup #1 #lookupVal, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

100 
lookupType = lookup #2 #lookupType, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

101 
lookupFix = lookup #3 #lookupFix, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

102 
lookupStruct = lookup #4 #lookupStruct, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

103 
lookupSig = lookup #5 #lookupSig, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

104 
lookupFunct = lookup #6 #lookupFunct, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

105 
enterVal = enter (fn h => fn (a, b, c, d, e, f) => (h a, b, c, d, e, f)) #enterVal, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

106 
enterType = enter (fn h => fn (a, b, c, d, e, f) => (a, h b, c, d, e, f)) #enterType, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

107 
enterFix = enter (fn h => fn (a, b, c, d, e, f) => (a, b, h c, d, e, f)) #enterFix, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

108 
enterStruct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, h d, e, f)) #enterStruct, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

109 
enterSig = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, h e, f)) #enterSig, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

110 
enterFunct = enter (fn h => fn (a, b, c, d, e, f) => (a, b, c, d, e, h f)) #enterFunct, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

111 
allVal = all #1 #allVal, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

112 
allType = all #2 #allType, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

113 
allFix = all #3 #allFix, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

114 
allStruct = all #4 #allStruct, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

115 
allSig = all #5 #allSig, 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

116 
allFunct = all #6 #allFunct} 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

117 
end; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

118 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

119 

26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

120 
(* theorem bindings *) 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

121 

a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

122 
val stored_thms: thm list ref = ref []; 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

123 

26492
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

124 
fun ml_store sel (name, ths) = 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

125 
let 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

126 
val ths' = Context.>>> (Context.map_theory_result (PureThy.store_thms (name, ths))); 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

127 
val _ = 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

128 
if name = "" then () 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

129 
else if not (ML_Syntax.is_identifier name) then 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

130 
error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value") 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

131 
else setmp stored_thms ths' (fn () => 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

132 
use_text name_space (0, "") Output.ml_output true 
26492
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

133 
("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) (); 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

134 
in () end; 
24574  135 

26492
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

136 
val ml_store_thms = ml_store ""; 
6e87cc839632
removed obsolete store_thm(s), cf. functional versions in pure_thy.ML;
wenzelm
parents:
26473
diff
changeset

137 
fun ml_store_thm (name, th) = ml_store "hd" (name, [th]); 
26416
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

138 

a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

139 
fun bind_thm (name, thm) = ml_store_thm (name, Drule.standard thm); 
a66f86ef7bb9
added store_thms etc. (formerly in Thy/thm_database.ML);
wenzelm
parents:
26405
diff
changeset

140 
fun bind_thms (name, thms) = ml_store_thms (name, map Drule.standard thms); 
24574  141 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

142 
fun thm name = ProofContext.get_thm (the_local_context ()) name; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

143 
fun thms name = ProofContext.get_thms (the_local_context ()) name; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

144 

24574  145 

146 

147 
(** ML antiquotations **) 

148 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

149 
(* antiquotation commands *) 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

150 

4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

151 
type antiq = 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

152 
{struct_name: string, background: Proof.context} > 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

153 
(Proof.context > string * string) * Proof.context; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

154 

4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

155 
local 
24574  156 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

157 
val global_parsers = ref (Symtab.empty: 
27868  158 
(Position.T > Context.generic * Args.T list > antiq * (Context.generic * Args.T list)) 
159 
Symtab.table); 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

160 

4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

161 
in 
24574  162 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

163 
fun add_antiq name scan = CRITICAL (fn () => 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

164 
change global_parsers (fn tab => 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

165 
(if not (Symtab.defined tab name) then () 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

166 
else warning ("Redefined ML antiquotation: " ^ quote name); 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

167 
Symtab.update (name, scan) tab))); 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

168 

4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

169 
fun antiquotation src ctxt = 
24574  170 
let val ((name, _), pos) = Args.dest_src src in 
171 
(case Symtab.lookup (! global_parsers) name of 

172 
NONE => error ("Unknown ML antiquotation command: " ^ quote name ^ Position.str_of pos) 

27895  173 
 SOME scan => (Position.report (Markup.ML_antiq name) pos; 
174 
Args.context_syntax "ML antiquotation" (scan pos) src ctxt)) 

24574  175 
end; 
176 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

177 
end; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

178 

24574  179 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

180 
(* parsing and evaluation *) 
24574  181 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

182 
local 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

183 

24574  184 
structure P = OuterParse; 
185 

186 
val antiq = 

27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
27781
diff
changeset

187 
P.!!! (P.position P.xname  Args.parse  Scan.ahead P.eof) 
24574  188 
>> (fn ((x, pos), y) => Args.src ((x, y), pos)); 
189 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

190 
val struct_name = "Isabelle"; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

191 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

192 
fun eval_antiquotes (txt, pos) opt_context = 
24574  193 
let 
27874
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
wenzelm
parents:
27868
diff
changeset

194 
val syms = SymbolPos.explode (txt, pos); 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
wenzelm
parents:
27868
diff
changeset

195 
val ants = Antiquote.read (syms, pos); 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

196 
val opt_ctxt = Option.map (Context.Proof o Context.proof_of) opt_context; 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

197 
val ((ml_env, ml_body), opt_ctxt') = 
27874
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
wenzelm
parents:
27868
diff
changeset

198 
if not (exists Antiquote.is_antiq ants) 
f0364f1c0ecf
antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
wenzelm
parents:
27868
diff
changeset

199 
then (("", Symbol.escape (SymbolPos.content syms)), opt_ctxt) 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

200 
else 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

201 
let 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

202 
val ctxt = 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

203 
(case opt_ctxt of 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

204 
NONE => error ("Unknown context  cannot expand ML antiquotations" ^ 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

205 
Position.str_of pos) 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

206 
 SOME ctxt => Context.proof_of ctxt); 
27378  207 

27359  208 
val lex = #1 (OuterKeyword.get_lexicons ()); 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

209 
fun no_decl _ = ("", ""); 
27378  210 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

211 
fun expand (Antiquote.Text s) state = (K ("", Symbol.escape s), state) 
27781  212 
 expand (Antiquote.Antiq x) (scope, background) = 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

213 
let 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

214 
val context = Stack.top scope; 
27781  215 
val (f, context') = antiquotation (Antiquote.read_antiq lex antiq x) context; 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

216 
val (decl, background') = f {background = background, struct_name = struct_name}; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

217 
in (decl, (Stack.map_top (K context') scope, background')) end 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

218 
 expand (Antiquote.Open _) (scope, background) = 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

219 
(no_decl, (Stack.push scope, background)) 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

220 
 expand (Antiquote.Close _) (scope, background) = 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

221 
(no_decl, (Stack.pop scope, background)); 
24574  222 

27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

223 
val (decls, (_, ctxt')) = fold_map expand ants (Stack.init ctxt, ctxt); 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

224 
val ml = decls > map (fn decl => decl ctxt') > split_list > pairself implode; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

225 
in (ml, SOME (Context.Proof ctxt')) end; 
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

226 
in (("structure " ^ struct_name ^ " =\nstruct\n" ^ ml_env ^ "end;", ml_body), opt_ctxt') end; 
24574  227 

228 
in 

229 

230 
val trace = ref false; 

231 

26385
ae7564661e76
ML runtime compilation: pass position, tuned signature;
wenzelm
parents:
26374
diff
changeset

232 
fun eval_wrapper pr verbose pos txt = 
24574  233 
let 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

234 
fun eval_raw p = use_text name_space 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

235 
(the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

236 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

237 
(*prepare source text*) 
27878  238 
val _ = Position.report Markup.ML_source pos; 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

239 
val ((env, body), env_ctxt) = eval_antiquotes (txt, pos) (Context.thread_data ()); 
27343
4b28b80dd1f8
add_antiq: more general notion of ML antiquotation;
wenzelm
parents:
26881
diff
changeset

240 
val _ = if ! trace then tracing (cat_lines [env, body]) else (); 
28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

241 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

242 
(*prepare static ML environment*) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

243 
val _ = Context.setmp_thread_data env_ctxt 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

244 
(fn () => (eval_raw Position.none false env; Context.thread_data ())) () 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

245 
> (fn NONE => ()  SOME context' => Context.>> (ML_Env.put (ML_Env.get context'))); 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

246 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

247 
(*eval ML*) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

248 
val _ = eval_raw pos verbose body; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

249 

7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

250 
(*reset static ML environment*) 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

251 
val _ = eval_raw Position.none false "structure Isabelle = struct end"; 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

252 
in () end; 
24574  253 

254 
end; 

255 

256 

257 
(* ML evaluation *) 

258 

26455  259 
val eval = eval_wrapper Output.ml_output; 
26881  260 
fun eval_file path = eval true (Path.position path) (File.read path); 
24574  261 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

262 
fun eval_in ctxt verbose pos txt = 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

263 
Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) (); 
25204  264 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

265 
fun evaluate ctxt pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () => 
25204  266 
let 
267 
val _ = r := NONE; 

28270
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

268 
val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () => 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

269 
eval_wrapper pr verbose Position.none 
7ada24ebea94
explicit handling of ML environment within generic context;
wenzelm
parents:
27895
diff
changeset

270 
("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) (); 
25204  271 
in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name)  SOME e => e) end) (); 
24574  272 

26455  273 
fun expression pos bind body txt = 
274 
exec (fn () => eval false pos 

275 
("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^ 

276 
" end (ML_Context.the_generic_context ())));")); 

277 

24574  278 
end; 
279 

280 
structure Basic_ML_Context: BASIC_ML_CONTEXT = ML_Context; 

281 
open Basic_ML_Context; 