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