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