author | desharna |
Mon, 13 Jun 2022 20:02:00 +0200 | |
changeset 75560 | aeb797356de0 |
parent 74636 | c35001872139 |
child 75604 | 39df30349778 |
permissions | -rw-r--r-- |
59720 | 1 |
(* Title: HOL/Library/code_test.ML |
64577 | 2 |
Author: Andreas Lochbihler, ETH Zürich |
58039 | 3 |
|
64577 | 4 |
Test infrastructure for the code generator. |
58039 | 5 |
*) |
6 |
||
64577 | 7 |
signature CODE_TEST = |
8 |
sig |
|
9 |
val add_driver: |
|
10 |
string * ((Proof.context -> (string * string) list * string -> Path.T -> string) * string) -> |
|
11 |
theory -> theory |
|
72284 | 12 |
val debug: bool Config.T |
64577 | 13 |
val test_terms: Proof.context -> term list -> string -> unit |
64580 | 14 |
val test_code_cmd: string list -> string list -> Proof.context -> unit |
64577 | 15 |
val eval_term: string -> Proof.context -> term -> term |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
16 |
val check_settings: string -> string -> string -> unit |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
17 |
val compile: string -> string -> unit |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
18 |
val evaluate: string -> string -> string |
64577 | 19 |
val evaluate_in_polyml: Proof.context -> (string * string) list * string -> Path.T -> string |
20 |
val evaluate_in_mlton: Proof.context -> (string * string) list * string -> Path.T -> string |
|
21 |
val evaluate_in_smlnj: Proof.context -> (string * string) list * string -> Path.T -> string |
|
22 |
val evaluate_in_ocaml: Proof.context -> (string * string) list * string -> Path.T -> string |
|
23 |
val ghc_options: string Config.T |
|
24 |
val evaluate_in_ghc: Proof.context -> (string * string) list * string -> Path.T -> string |
|
25 |
val evaluate_in_scala: Proof.context -> (string * string) list * string -> Path.T -> string |
|
66284
378895354604
new derived targets for evaluating Haskell and Scala programs
Andreas Lochbihler
parents:
65905
diff
changeset
|
26 |
val target_Scala: string |
378895354604
new derived targets for evaluating Haskell and Scala programs
Andreas Lochbihler
parents:
65905
diff
changeset
|
27 |
val target_Haskell: string |
58039 | 28 |
end |
29 |
||
64577 | 30 |
structure Code_Test: CODE_TEST = |
31 |
struct |
|
58039 | 32 |
|
33 |
(* convert a list of terms into nested tuples and back *) |
|
64577 | 34 |
|
74636 | 35 |
fun mk_tuples [] = \<^Const>\<open>Unity\<close> |
58039 | 36 |
| mk_tuples [t] = t |
37 |
| mk_tuples (t :: ts) = HOLogic.mk_prod (t, mk_tuples ts) |
|
38 |
||
74636 | 39 |
fun dest_tuples \<^Const_>\<open>Pair _ _ for l r\<close> = l :: dest_tuples r |
58039 | 40 |
| dest_tuples t = [t] |
41 |
||
42 |
||
43 |
fun last_field sep str = |
|
44 |
let |
|
64577 | 45 |
val n = size sep |
46 |
val len = size str |
|
58039 | 47 |
fun find i = |
48 |
if i < 0 then NONE |
|
49 |
else if String.substring (str, i, n) = sep then SOME i |
|
64577 | 50 |
else find (i - 1) |
58039 | 51 |
in |
52 |
(case find (len - n) of |
|
53 |
NONE => NONE |
|
54 |
| SOME i => SOME (String.substring (str, 0, i), String.extract (str, i + n, NONE))) |
|
64577 | 55 |
end |
58039 | 56 |
|
57 |
fun split_first_last start stop s = |
|
64577 | 58 |
(case first_field start s of |
59 |
NONE => NONE |
|
60 |
| SOME (initial, rest) => |
|
61 |
(case last_field stop rest of |
|
62 |
NONE => NONE |
|
63 |
| SOME (middle, tail) => SOME (initial, middle, tail))) |
|
58039 | 64 |
|
64577 | 65 |
|
66 |
(* data slot for drivers *) |
|
58039 | 67 |
|
68 |
structure Drivers = Theory_Data |
|
69 |
( |
|
64577 | 70 |
type T = |
71 |
(string * ((Proof.context -> (string * string) list * string -> Path.T -> string) * string)) list |
|
72 |
val empty = [] |
|
73 |
fun merge data : T = AList.merge (op =) (K true) data |
|
58039 | 74 |
) |
75 |
||
64577 | 76 |
val add_driver = Drivers.map o AList.update (op =) |
77 |
val get_driver = AList.lookup (op =) o Drivers.get |
|
58039 | 78 |
|
79 |
(* |
|
80 |
Test drivers must produce output of the following format: |
|
64577 | 81 |
|
72307 | 82 |
The start of the relevant data is marked with start_marker, |
83 |
its end with end_marker. |
|
58039 | 84 |
|
85 |
Between these two markers, every line corresponds to one test. |
|
72307 | 86 |
Lines of successful tests start with success, failures start with failure. |
87 |
The failure failure may continue with the YXML encoding of the evaluated term. |
|
58039 | 88 |
There must not be any additional whitespace in between. |
89 |
*) |
|
90 |
||
64577 | 91 |
|
92 |
(* parsing of results *) |
|
58039 | 93 |
|
72307 | 94 |
val success = "True" |
95 |
val failure = "False" |
|
96 |
val start_marker = "*@*Isabelle/Code_Test-start*@*" |
|
97 |
val end_marker = "*@*Isabelle/Code_Test-end*@*" |
|
58039 | 98 |
|
99 |
fun parse_line line = |
|
72307 | 100 |
if String.isPrefix success line then (true, NONE) |
101 |
else if String.isPrefix failure line then (false, |
|
102 |
if size line > size failure then |
|
103 |
String.extract (line, size failure, NONE) |
|
58039 | 104 |
|> YXML.parse_body |
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
69950
diff
changeset
|
105 |
|> Term_XML.Decode.term_raw |
58039 | 106 |
|> dest_tuples |
107 |
|> SOME |
|
108 |
else NONE) |
|
109 |
else raise Fail ("Cannot parse result of evaluation:\n" ^ line) |
|
110 |
||
111 |
fun parse_result target out = |
|
72307 | 112 |
(case split_first_last start_marker end_marker out of |
72273 | 113 |
NONE => error ("Evaluation failed for " ^ target ^ "!\nCompiler output:\n" ^ out) |
65904
8411f1a2272c
permissive trim_line as in Scala, e.g. relevant for poly/TextIO.print output on Windows;
wenzelm
parents:
65901
diff
changeset
|
114 |
| SOME (_, middle, _) => middle |> trim_split_lines |> map parse_line) |
58039 | 115 |
|
64577 | 116 |
|
117 |
(* pretty printing of test results *) |
|
58039 | 118 |
|
119 |
fun pretty_eval _ NONE _ = [] |
|
64577 | 120 |
| pretty_eval ctxt (SOME evals) ts = |
121 |
[Pretty.fbrk, |
|
122 |
Pretty.big_list "Evaluated terms" |
|
123 |
(map (fn (t, eval) => Pretty.block |
|
124 |
[Syntax.pretty_term ctxt t, Pretty.brk 1, Pretty.str "=", Pretty.brk 1, |
|
125 |
Syntax.pretty_term ctxt eval]) |
|
126 |
(ts ~~ evals))] |
|
58039 | 127 |
|
128 |
fun pretty_failure ctxt target (((_, evals), query), eval_ts) = |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
129 |
Pretty.block (Pretty.text ("Test in " ^ target ^ " failed for") @ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
130 |
[Pretty.brk 1, Pretty.quote (Syntax.pretty_term ctxt query)] @ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
131 |
pretty_eval ctxt evals eval_ts) |
58039 | 132 |
|
133 |
fun pretty_failures ctxt target failures = |
|
134 |
Pretty.blk (0, Pretty.fbreaks (map (pretty_failure ctxt target) failures)) |
|
135 |
||
136 |
||
64577 | 137 |
(* driver invocation *) |
138 |
||
72293 | 139 |
val debug = Attrib.setup_config_bool \<^binding>\<open>test_code_debug\<close> (K false) |
58039 | 140 |
|
72284 | 141 |
fun with_debug_dir name f = |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
142 |
(Path.explode "$ISABELLE_HOME_USER" + Path.basic (name ^ serial_string ())) |
72376 | 143 |
|> Isabelle_System.make_directory |
144 |
|> Exn.capture f |
|
145 |
|> Exn.release; |
|
58039 | 146 |
|
147 |
fun dynamic_value_strict ctxt t compiler = |
|
148 |
let |
|
149 |
val thy = Proof_Context.theory_of ctxt |
|
64577 | 150 |
val (driver, target) = |
151 |
(case get_driver thy compiler of |
|
152 |
NONE => error ("No driver for target " ^ compiler) |
|
72272 | 153 |
| SOME drv => drv) |
72285 | 154 |
val with_dir = if Config.get ctxt debug then with_debug_dir else Isabelle_System.with_tmp_dir |
72274 | 155 |
fun eval result = |
69623
ef02c5e051e5
explicit model concerning files of generated code
haftmann
parents:
69597
diff
changeset
|
156 |
with_dir "Code_Test" (driver ctxt ((apfst o map o apfst) Long_Name.implode result)) |
ef02c5e051e5
explicit model concerning files of generated code
haftmann
parents:
69597
diff
changeset
|
157 |
|> parse_result compiler |
58039 | 158 |
fun evaluator program _ vs_ty deps = |
72274 | 159 |
Exn.interruptible_capture eval |
64957 | 160 |
(Code_Target.compilation_text ctxt target program deps true vs_ty) |
64578 | 161 |
fun postproc f = map (apsnd (Option.map (map f))) |
64577 | 162 |
in Exn.release (Code_Thingol.dynamic_value ctxt (Exn.map_res o postproc) evaluator t) end |
58039 | 163 |
|
64577 | 164 |
|
165 |
(* term preprocessing *) |
|
58039 | 166 |
|
74636 | 167 |
fun add_eval \<^Const_>\<open>Trueprop for t\<close> = add_eval t |
168 |
| add_eval \<^Const_>\<open>HOL.eq _ for lhs rhs\<close> = (fn acc => |
|
64577 | 169 |
acc |
170 |
|> add_eval rhs |
|
171 |
|> add_eval lhs |
|
172 |
|> cons rhs |
|
173 |
|> cons lhs) |
|
74636 | 174 |
| add_eval \<^Const_>\<open>Not for t\<close> = add_eval t |
175 |
| add_eval \<^Const_>\<open>less_eq _ for lhs rhs\<close> = (fn acc => lhs :: rhs :: acc) |
|
176 |
| add_eval \<^Const_>\<open>less _ for lhs rhs\<close> = (fn acc => lhs :: rhs :: acc) |
|
58039 | 177 |
| add_eval _ = I |
178 |
||
74383 | 179 |
fun mk_term_of [] = \<^Const>\<open>None \<^typ>\<open>unit \<Rightarrow> yxml_of_term\<close>\<close> |
58039 | 180 |
| mk_term_of ts = |
64577 | 181 |
let |
182 |
val tuple = mk_tuples ts |
|
183 |
val T = fastype_of tuple |
|
184 |
in |
|
74636 | 185 |
\<^Const>\<open>Some \<^typ>\<open>unit \<Rightarrow> yxml_of_term\<close> for |
186 |
\<open>absdummy \<^Type>\<open>unit\<close> |
|
187 |
\<^Const>\<open>yxml_string_of_term for \<^Const>\<open>Code_Evaluation.term_of T for tuple\<close>\<close>\<close>\<close> |
|
64577 | 188 |
end |
58039 | 189 |
|
190 |
fun test_terms ctxt ts target = |
|
191 |
let |
|
192 |
val thy = Proof_Context.theory_of ctxt |
|
193 |
||
69593 | 194 |
fun term_of t = Sign.of_sort thy (fastype_of t, \<^sort>\<open>term_of\<close>) |
58039 | 195 |
|
64577 | 196 |
fun ensure_bool t = |
197 |
(case fastype_of t of |
|
74636 | 198 |
\<^Type>\<open>bool\<close> => () |
64579 | 199 |
| _ => |
200 |
error (Pretty.string_of |
|
201 |
(Pretty.block [Pretty.str "Test case not of type bool:", Pretty.brk 1, |
|
202 |
Syntax.pretty_term ctxt t]))) |
|
58039 | 203 |
|
64578 | 204 |
val _ = List.app ensure_bool ts |
58039 | 205 |
|
206 |
val evals = map (fn t => filter term_of (add_eval t [])) ts |
|
207 |
val eval = map mk_term_of evals |
|
208 |
||
64578 | 209 |
val t = |
69593 | 210 |
HOLogic.mk_list \<^typ>\<open>bool \<times> (unit \<Rightarrow> yxml_of_term) option\<close> |
64578 | 211 |
(map HOLogic.mk_prod (ts ~~ eval)) |
58039 | 212 |
|
64577 | 213 |
val result = dynamic_value_strict ctxt t target |
58039 | 214 |
|
215 |
val failed = |
|
216 |
filter_out (fst o fst o fst) (result ~~ ts ~~ evals) |
|
64577 | 217 |
handle ListPair.UnequalLengths => |
72272 | 218 |
error ("Evaluation failed!\nWrong number of test results: " ^ string_of_int (length result)) |
58039 | 219 |
in |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
220 |
(case failed of |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
221 |
[] => () |
64578 | 222 |
| _ => error (Pretty.string_of (pretty_failures ctxt target failed))) |
58039 | 223 |
end |
224 |
||
64580 | 225 |
fun test_code_cmd raw_ts targets ctxt = |
58039 | 226 |
let |
64577 | 227 |
val ts = Syntax.read_terms ctxt raw_ts |
64579 | 228 |
val frees = fold Term.add_frees ts [] |
64578 | 229 |
val _ = |
230 |
if null frees then () |
|
64579 | 231 |
else error (Pretty.string_of |
232 |
(Pretty.block (Pretty.str "Terms contain free variables:" :: Pretty.brk 1 :: |
|
72283 | 233 |
Pretty.commas (map (Syntax.pretty_term ctxt o Free) frees)))) |
234 |
in List.app (test_terms ctxt ts) targets end |
|
58039 | 235 |
|
58348
2d47c7d10b62
add target language evaluators for the value command;
Andreas Lochbihler
parents:
58039
diff
changeset
|
236 |
fun eval_term target ctxt t = |
2d47c7d10b62
add target language evaluators for the value command;
Andreas Lochbihler
parents:
58039
diff
changeset
|
237 |
let |
64579 | 238 |
val frees = Term.add_frees t [] |
64578 | 239 |
val _ = |
240 |
if null frees then () |
|
64579 | 241 |
else |
242 |
error (Pretty.string_of |
|
243 |
(Pretty.block (Pretty.str "Term contains free variables:" :: Pretty.brk 1 :: |
|
72283 | 244 |
Pretty.commas (map (Syntax.pretty_term ctxt o Free) frees)))) |
58039 | 245 |
|
64579 | 246 |
val T = fastype_of t |
64578 | 247 |
val _ = |
69593 | 248 |
if Sign.of_sort (Proof_Context.theory_of ctxt) (T, \<^sort>\<open>term_of\<close>) then () |
64579 | 249 |
else error ("Type " ^ Syntax.string_of_typ ctxt T ^ |
69593 | 250 |
" of term not of sort " ^ Syntax.string_of_sort ctxt \<^sort>\<open>term_of\<close>) |
58039 | 251 |
|
64578 | 252 |
val t' = |
69593 | 253 |
HOLogic.mk_list \<^typ>\<open>bool \<times> (unit \<Rightarrow> yxml_of_term) option\<close> |
74636 | 254 |
[HOLogic.mk_prod (\<^Const>\<open>False\<close>, mk_term_of [t])] |
58039 | 255 |
|
64577 | 256 |
val result = dynamic_value_strict ctxt t' target |
257 |
in (case result of [(_, SOME [t])] => t | _ => error "Evaluation failed") end |
|
58039 | 258 |
|
64577 | 259 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
260 |
(* check and invoke compiler *) |
58039 | 261 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
262 |
fun check_settings compiler var descr = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
263 |
if getenv var = "" then |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
264 |
error (Pretty.string_of (Pretty.para |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
265 |
("Environment variable " ^ var ^ " is not set. To test code generation with " ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
266 |
compiler ^ ", set this variable to your " ^ descr ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
267 |
" in the $ISABELLE_HOME_USER/etc/settings file."))) |
72306 | 268 |
else () |
58039 | 269 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
270 |
fun compile compiler cmd = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
271 |
let val (out, ret) = Isabelle_System.bash_output cmd in |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
272 |
if ret = 0 then () |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
273 |
else error ("Compilation with " ^ compiler ^ " failed:\n" ^ cmd ^ "\n" ^ out) |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
274 |
end |
58039 | 275 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
276 |
fun evaluate compiler cmd = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
277 |
let val (out, res) = Isabelle_System.bash_output cmd in |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
278 |
if res = 0 then out |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
279 |
else error ("Evaluation for " ^ compiler ^ " terminated with error code " ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
280 |
string_of_int res ^ "\nCompiler output:\n" ^ out) |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
281 |
end |
58039 | 282 |
|
64577 | 283 |
|
284 |
(* driver for PolyML *) |
|
58039 | 285 |
|
64577 | 286 |
val polymlN = "PolyML" |
58039 | 287 |
|
72297 | 288 |
fun evaluate_in_polyml (_: Proof.context) (code_files, value_name) dir = |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
289 |
let |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
290 |
val code_path = dir + Path.basic "generated.sml" |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
291 |
val driver_path = dir + Path.basic "driver.sml" |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
292 |
val out_path = dir + Path.basic "out" |
72297 | 293 |
|
72306 | 294 |
val code = #2 (the_single code_files) |
72286 | 295 |
val driver = \<^verbatim>\<open> |
72290 | 296 |
fun main () = |
72286 | 297 |
let |
72307 | 298 |
fun format (true, _) = \<close> ^ ML_Syntax.print_string success ^ \<^verbatim>\<open> ^ "\n" |
299 |
| format (false, NONE) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ "\n" |
|
300 |
| format (false, SOME t) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ t () ^ "\n" |
|
72286 | 301 |
val result = \<close> ^ value_name ^ \<^verbatim>\<open> () |
302 |
val result_text = \<close> ^ |
|
72307 | 303 |
ML_Syntax.print_string start_marker ^ |
72286 | 304 |
\<^verbatim>\<open> ^ String.concat (map format result) ^ \<close> ^ |
72307 | 305 |
ML_Syntax.print_string end_marker ^ \<^verbatim>\<open> |
72288 | 306 |
val out = BinIO.openOut \<close> ^ ML_Syntax.print_platform_path out_path ^ \<^verbatim>\<open> |
72286 | 307 |
val _ = BinIO.output (out, Byte.stringToBytes result_text) |
308 |
val _ = BinIO.closeOut out |
|
309 |
in () end; |
|
310 |
\<close> |
|
72306 | 311 |
val _ = File.write code_path code |
312 |
val _ = File.write driver_path driver |
|
313 |
val _ = |
|
314 |
ML_Context.eval |
|
315 |
{environment = ML_Env.SML, redirect = false, verbose = false, debug = NONE, |
|
316 |
writeln = writeln, warning = warning} |
|
317 |
Position.none |
|
318 |
(ML_Lex.read_text (code, Path.position code_path) @ |
|
319 |
ML_Lex.read_text (driver, Path.position driver_path) @ |
|
320 |
ML_Lex.read "main ()") |
|
72308 | 321 |
handle ERROR msg => error ("Evaluation for " ^ polymlN ^ " failed:\n" ^ msg) |
72306 | 322 |
in File.read out_path end |
58039 | 323 |
|
64577 | 324 |
|
325 |
(* driver for mlton *) |
|
58039 | 326 |
|
327 |
val mltonN = "MLton" |
|
328 |
val ISABELLE_MLTON = "ISABELLE_MLTON" |
|
329 |
||
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
330 |
fun evaluate_in_mlton (_: Proof.context) (code_files, value_name) dir = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
331 |
let |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
332 |
val compiler = mltonN |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
333 |
val generatedN = "generated.sml" |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
334 |
val driverN = "driver.sml" |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
335 |
val projectN = "test" |
58039 | 336 |
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
337 |
val code_path = dir + Path.basic generatedN |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
338 |
val driver_path = dir + Path.basic driverN |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
339 |
val basis_path = dir + Path.basic (projectN ^ ".mlb") |
72291 | 340 |
val driver = \<^verbatim>\<open> |
72307 | 341 |
fun format (true, _) = \<close> ^ ML_Syntax.print_string success ^ \<^verbatim>\<open> ^ "\n" |
342 |
| format (false, NONE) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ "\n" |
|
343 |
| format (false, SOME t) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ t () ^ "\n" |
|
72291 | 344 |
val result = \<close> ^ value_name ^ \<^verbatim>\<open> () |
72307 | 345 |
val _ = print \<close> ^ ML_Syntax.print_string start_marker ^ \<^verbatim>\<open> |
72291 | 346 |
val _ = List.app (print o format) result |
72307 | 347 |
val _ = print \<close> ^ ML_Syntax.print_string end_marker ^ \<^verbatim>\<open> |
72291 | 348 |
\<close> |
72306 | 349 |
val _ = check_settings compiler ISABELLE_MLTON "MLton executable" |
350 |
val _ = List.app (File.write code_path o snd) code_files |
|
351 |
val _ = File.write driver_path driver |
|
352 |
val _ = File.write basis_path ("$(SML_LIB)/basis/basis.mlb\n" ^ generatedN ^ "\n" ^ driverN) |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
353 |
in |
72306 | 354 |
compile compiler ("\"$ISABELLE_MLTON\" -default-type intinf " ^ File.bash_path basis_path); |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
355 |
evaluate compiler (File.bash_path (dir + Path.basic projectN)) |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
356 |
end |
58039 | 357 |
|
64577 | 358 |
|
359 |
(* driver for SML/NJ *) |
|
58039 | 360 |
|
361 |
val smlnjN = "SMLNJ" |
|
362 |
val ISABELLE_SMLNJ = "ISABELLE_SMLNJ" |
|
363 |
||
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
364 |
fun evaluate_in_smlnj (_: Proof.context) (code_files, value_name) dir = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
365 |
let |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
366 |
val compiler = smlnjN |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
367 |
val generatedN = "generated.sml" |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
368 |
val driverN = "driver.sml" |
58039 | 369 |
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
370 |
val code_path = dir + Path.basic generatedN |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
371 |
val driver_path = dir + Path.basic driverN |
72291 | 372 |
val driver = \<^verbatim>\<open> |
373 |
structure Test = |
|
374 |
struct |
|
375 |
fun main () = |
|
376 |
let |
|
72307 | 377 |
fun format (true, _) = \<close> ^ ML_Syntax.print_string success ^ \<^verbatim>\<open> ^ "\n" |
378 |
| format (false, NONE) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ "\n" |
|
379 |
| format (false, SOME t) = \<close> ^ ML_Syntax.print_string failure ^ \<^verbatim>\<open> ^ t () ^ "\n" |
|
72291 | 380 |
val result = \<close> ^ value_name ^ \<^verbatim>\<open> () |
72307 | 381 |
val _ = print \<close> ^ ML_Syntax.print_string start_marker ^ \<^verbatim>\<open> |
72291 | 382 |
val _ = List.app (print o format) result |
72307 | 383 |
val _ = print \<close> ^ ML_Syntax.print_string end_marker ^ \<^verbatim>\<open> |
72291 | 384 |
in 0 end |
385 |
end |
|
386 |
\<close> |
|
72306 | 387 |
val _ = check_settings compiler ISABELLE_SMLNJ "SMLNJ executable" |
388 |
val _ = List.app (File.write code_path o snd) code_files |
|
389 |
val _ = File.write driver_path driver |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
390 |
val ml_source = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
391 |
"Control.MC.matchRedundantError := false; Control.MC.matchRedundantWarn := false;" ^ |
72277 | 392 |
"use " ^ ML_Syntax.print_string (File.platform_path code_path) ^ |
393 |
"; use " ^ ML_Syntax.print_string (File.platform_path driver_path) ^ |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
394 |
"; Test.main ();" |
72306 | 395 |
in evaluate compiler ("echo " ^ Bash.string ml_source ^ " | \"$ISABELLE_SMLNJ\"") end |
58039 | 396 |
|
64577 | 397 |
|
398 |
(* driver for OCaml *) |
|
58039 | 399 |
|
400 |
val ocamlN = "OCaml" |
|
69926
110fff287217
access OCaml tools and libraries via ISABELLE_OCAMLFIND;
wenzelm
parents:
69925
diff
changeset
|
401 |
val ISABELLE_OCAMLFIND = "ISABELLE_OCAMLFIND" |
58039 | 402 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
403 |
fun evaluate_in_ocaml (_: Proof.context) (code_files, value_name) dir = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
404 |
let |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
405 |
val compiler = ocamlN |
58039 | 406 |
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
407 |
val code_path = dir + Path.basic "generated.ml" |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
408 |
val driver_path = dir + Path.basic "driver.ml" |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
409 |
val driver = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
410 |
"let format_term = function\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
411 |
" | None -> \"\"\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
412 |
" | Some t -> t ();;\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
413 |
"let format = function\n" ^ |
72307 | 414 |
" | (true, _) -> \"" ^ success ^ "\\n\"\n" ^ |
415 |
" | (false, x) -> \"" ^ failure ^ "\" ^ format_term x ^ \"\\n\";;\n" ^ |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
416 |
"let result = " ^ ("Generated." ^ value_name) ^ " ();;\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
417 |
"let main x =\n" ^ |
72307 | 418 |
" let _ = print_string \"" ^ start_marker ^ "\" in\n" ^ |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
419 |
" let _ = List.map (fun x -> print_string (format x)) result in\n" ^ |
72307 | 420 |
" print_string \"" ^ end_marker ^ "\";;\n" ^ |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
421 |
"main ();;" |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
422 |
val compiled_path = dir + Path.basic "test" |
72306 | 423 |
|
424 |
val _ = check_settings compiler ISABELLE_OCAMLFIND "ocamlfind executable" |
|
425 |
val _ = List.app (File.write code_path o snd) code_files |
|
426 |
val _ = File.write driver_path driver |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
427 |
val cmd = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
428 |
"\"$ISABELLE_OCAMLFIND\" ocamlopt -w pu -package zarith -linkpkg" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
429 |
" -o " ^ File.bash_path compiled_path ^ " -I " ^ File.bash_path dir ^ " " ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
430 |
File.bash_path code_path ^ " " ^ File.bash_path driver_path ^ " </dev/null" |
72306 | 431 |
in compile compiler cmd; evaluate compiler (File.bash_path compiled_path) end |
58039 | 432 |
|
64577 | 433 |
|
434 |
(* driver for GHC *) |
|
58039 | 435 |
|
436 |
val ghcN = "GHC" |
|
437 |
val ISABELLE_GHC = "ISABELLE_GHC" |
|
438 |
||
69593 | 439 |
val ghc_options = Attrib.setup_config_string \<^binding>\<open>code_test_ghc\<close> (K "") |
58039 | 440 |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
441 |
fun evaluate_in_ghc ctxt (code_files, value_name) dir = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
442 |
let |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
443 |
val compiler = ghcN |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
444 |
val modules = map fst code_files |
58039 | 445 |
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
446 |
val driver_path = dir + Path.basic "Main.hs" |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
447 |
val driver = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
448 |
"module Main where {\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
449 |
implode (map (fn module => "import qualified " ^ unsuffix ".hs" module ^ ";\n") modules) ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
450 |
"main = do {\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
451 |
" let {\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
452 |
" format_term Nothing = \"\";\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
453 |
" format_term (Just t) = t ();\n" ^ |
72307 | 454 |
" format (True, _) = \"" ^ success ^ "\\n\";\n" ^ |
455 |
" format (False, to) = \"" ^ failure ^ "\" ++ format_term to ++ \"\\n\";\n" ^ |
|
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
456 |
" result = " ^ value_name ^ " ();\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
457 |
" };\n" ^ |
72307 | 458 |
" Prelude.putStr \"" ^ start_marker ^ "\";\n" ^ |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
459 |
" Prelude.mapM_ (putStr . format) result;\n" ^ |
72307 | 460 |
" Prelude.putStr \"" ^ end_marker ^ "\";\n" ^ |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
461 |
" }\n" ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
462 |
"}\n" |
58039 | 463 |
|
72306 | 464 |
val _ = check_settings compiler ISABELLE_GHC "GHC executable" |
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
465 |
val _ = List.app (fn (name, code) => File.write (dir + Path.basic name) code) code_files |
72306 | 466 |
val _ = File.write driver_path driver |
467 |
||
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
468 |
val compiled_path = dir + Path.basic "test" |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
469 |
val cmd = |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
470 |
"\"$ISABELLE_GHC\" " ^ Code_Haskell.language_params ^ " " ^ |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
471 |
Config.get ctxt ghc_options ^ " -o " ^ |
72278 | 472 |
File.bash_platform_path compiled_path ^ " " ^ |
473 |
File.bash_platform_path driver_path ^ " -i" ^ |
|
474 |
File.bash_platform_path dir |
|
72306 | 475 |
in compile compiler cmd; evaluate compiler (File.bash_path compiled_path) end |
58039 | 476 |
|
64577 | 477 |
|
478 |
(* driver for Scala *) |
|
58039 | 479 |
|
480 |
val scalaN = "Scala" |
|
481 |
||
72297 | 482 |
fun evaluate_in_scala (_: Proof.context) (code_files, value_name) dir = |
72276
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
483 |
let |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
484 |
val generatedN = "Generated_Code" |
dfe150a246e6
misc tuning and clarification: prefer functions over data;
wenzelm
parents:
72275
diff
changeset
|
485 |
val driverN = "Driver.scala" |
58039 | 486 |
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
487 |
val code_path = dir + Path.basic (generatedN ^ ".scala") |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
488 |
val driver_path = dir + Path.basic driverN |
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72376
diff
changeset
|
489 |
val out_path = dir + Path.basic "out" |
72297 | 490 |
|
72306 | 491 |
val code = #2 (the_single code_files) |
72296 | 492 |
val driver = \<^verbatim>\<open> |
493 |
{ |
|
494 |
val result = \<close> ^ value_name ^ \<^verbatim>\<open>(()) |
|
495 |
val result_text = |
|
496 |
result.map( |
|
497 |
{ |
|
498 |
case (true, _) => "True\n" |
|
499 |
case (false, None) => "False\n" |
|
500 |
case (false, Some(t)) => "False" + t(()) + "\n" |
|
501 |
}).mkString |
|
502 |
isabelle.File.write( |
|
503 |
isabelle.Path.explode(\<close> ^ quote (Path.implode (Path.expand out_path)) ^ \<^verbatim>\<open>), |
|
72307 | 504 |
\<close> ^ quote start_marker ^ \<^verbatim>\<open> + result_text + \<close> ^ quote end_marker ^ \<^verbatim>\<open>) |
72296 | 505 |
}\<close> |
72306 | 506 |
val _ = File.write code_path code |
507 |
val _ = File.write driver_path driver |
|
508 |
val _ = Scala_Compiler.toplevel true (code ^ driver) |
|
72308 | 509 |
handle ERROR msg => error ("Evaluation for " ^ scalaN ^ " failed:\n" ^ msg) |
72306 | 510 |
in File.read out_path end |
58039 | 511 |
|
64580 | 512 |
|
513 |
(* command setup *) |
|
58039 | 514 |
|
64577 | 515 |
val _ = |
69593 | 516 |
Outer_Syntax.command \<^command_keyword>\<open>test_code\<close> |
58039 | 517 |
"compile test cases to target languages, execute them and report results" |
69593 | 518 |
(Scan.repeat1 Parse.prop -- (\<^keyword>\<open>in\<close> |-- Scan.repeat1 Parse.name) |
64580 | 519 |
>> (fn (ts, targets) => Toplevel.keep (test_code_cmd ts targets o Toplevel.context_of))) |
58039 | 520 |
|
66284
378895354604
new derived targets for evaluating Haskell and Scala programs
Andreas Lochbihler
parents:
65905
diff
changeset
|
521 |
val target_Scala = "Scala_eval" |
378895354604
new derived targets for evaluating Haskell and Scala programs
Andreas Lochbihler
parents:
65905
diff
changeset
|
522 |
val target_Haskell = "Haskell_eval" |
378895354604
new derived targets for evaluating Haskell and Scala programs
Andreas Lochbihler
parents:
65905
diff
changeset
|
523 |
|
64580 | 524 |
val _ = |
72306 | 525 |
Theory.setup |
526 |
(Code_Target.add_derived_target (target_Scala, [(Code_Scala.target, I)]) #> |
|
527 |
Code_Target.add_derived_target (target_Haskell, [(Code_Haskell.target, I)]) #> |
|
528 |
fold add_driver |
|
529 |
[(polymlN, (evaluate_in_polyml, Code_ML.target_SML)), |
|
530 |
(mltonN, (evaluate_in_mlton, Code_ML.target_SML)), |
|
531 |
(smlnjN, (evaluate_in_smlnj, Code_ML.target_SML)), |
|
532 |
(ocamlN, (evaluate_in_ocaml, Code_ML.target_OCaml)), |
|
533 |
(ghcN, (evaluate_in_ghc, target_Haskell)), |
|
534 |
(scalaN, (evaluate_in_scala, target_Scala))] #> |
|
535 |
fold (fn target => Value_Command.add_evaluator (Binding.name target, eval_term target) #> #2) |
|
64580 | 536 |
[polymlN, mltonN, smlnjN, ocamlN, ghcN, scalaN]) |
59323 | 537 |
|
58039 | 538 |
end |