author | bulwahn |
Fri, 11 Mar 2011 15:21:13 +0100 | |
changeset 41925 | 4b9fdfd23752 |
parent 41909 | src/HOL/Tools/LSC/lazysmallcheck.ML@383bbdad1650 |
child 41930 | 1e008cc4883a |
permissions | -rw-r--r-- |
41905 | 1 |
(* Title: HOL/Tools/LSC/lazysmallcheck.ML |
2 |
Author: Lukas Bulwahn, TU Muenchen |
|
3 |
||
4 |
Prototypic implementation to invoke lazysmallcheck in Isabelle |
|
5 |
||
6 |
*) |
|
7 |
||
8 |
signature LAZYSMALLCHECK = |
|
9 |
sig |
|
10 |
val compile_generator_expr: |
|
11 |
Proof.context -> term -> int -> term list option * Quickcheck.report option |
|
12 |
val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context |
|
13 |
val setup: theory -> theory |
|
14 |
end; |
|
15 |
||
16 |
structure Lazysmallcheck : LAZYSMALLCHECK = |
|
17 |
struct |
|
18 |
||
19 |
(* invocation of Haskell interpreter *) |
|
20 |
||
21 |
val lsc_module = File.read (Path.explode "~~/src/HOL/Tools/LSC/LazySmallCheck.hs") |
|
22 |
||
23 |
fun exec verbose code = |
|
24 |
ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code) |
|
25 |
||
26 |
fun value ctxt (get, put, put_ml) (code, value) = |
|
27 |
let |
|
41909
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
28 |
val tmp_prefix = "LSC" |
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
29 |
fun make_cmd executable files = |
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
30 |
getenv "EXEC_GHC" ^ " -fglasgow-exts " ^ (space_implode " " (map Path.implode files)) ^ |
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
31 |
" -o " ^ executable ^ " && " ^ executable |
41905 | 32 |
fun run in_path = |
33 |
let |
|
34 |
val code_file = Path.append in_path (Path.basic "Code.hs") |
|
35 |
val lsc_file = Path.append in_path (Path.basic "LazySmallCheck.hs") |
|
36 |
val main_file = Path.append in_path (Path.basic "Main.hs") |
|
37 |
val main = "module Main where {\n\n" ^ |
|
38 |
"import LazySmallCheck;\n" ^ |
|
39 |
"import Code;\n\n" ^ |
|
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
40 |
"main = LazySmallCheck.smallCheck 7 (Code.value ())\n\n" ^ |
41905 | 41 |
"}\n" |
41909
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
42 |
val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n" |
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
43 |
(unprefix "module Code where {" code) |
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
44 |
val _ = File.write code_file code' |
41905 | 45 |
val _ = File.write lsc_file lsc_module |
46 |
val _ = File.write main_file main |
|
41908
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
47 |
val executable = Path.implode (Path.append in_path (Path.basic "isa_lsc")) |
3bd9a21366d2
changing invocation of ghc from interactive mode to compilation increases the performance of lazysmallcheck by a factor of twenty; changing Integer type to Int reduces by another 50 percent
bulwahn
parents:
41905
diff
changeset
|
48 |
val cmd = make_cmd executable [code_file, lsc_file, main_file] |
41905 | 49 |
in |
50 |
bash_output cmd |
|
51 |
end |
|
41909
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
52 |
val result = Isabelle_System.with_tmp_dir tmp_prefix run |
41905 | 53 |
val output_value = the_default "NONE" |
54 |
(try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result) |
|
55 |
val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml |
|
56 |
^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))"; |
|
57 |
val ctxt' = ctxt |
|
58 |
|> put (fn () => error ("Bad evaluation for " ^ quote put_ml)) |
|
59 |
|> Context.proof_map (exec false ml_code); |
|
60 |
in get ctxt' () end; |
|
61 |
||
62 |
fun evaluation cookie thy evaluator vs_t args = |
|
63 |
let |
|
64 |
val ctxt = ProofContext.init_global thy; |
|
65 |
val (program_code, value_name) = evaluator vs_t; |
|
66 |
val value_code = space_implode " " |
|
67 |
(value_name :: "()" :: map (enclose "(" ")") args); |
|
68 |
in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end; |
|
69 |
||
70 |
fun dynamic_value_strict cookie thy postproc t args = |
|
71 |
let |
|
72 |
fun evaluator naming program ((_, vs_ty), t) deps = |
|
73 |
evaluation cookie thy (Code_Target.evaluator thy "Haskell" naming program deps) (vs_ty, t) args; |
|
74 |
in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end; |
|
75 |
||
76 |
(* counterexample generator *) |
|
77 |
||
78 |
structure Lazysmallcheck_Result = Proof_Data |
|
79 |
( |
|
80 |
type T = unit -> term list option |
|
81 |
fun init _ () = error "Lazysmallcheck_Result" |
|
82 |
) |
|
83 |
||
84 |
val put_counterexample = Lazysmallcheck_Result.put |
|
85 |
||
86 |
fun compile_generator_expr ctxt t size = |
|
87 |
let |
|
88 |
val thy = ProofContext.theory_of ctxt |
|
89 |
fun ensure_testable t = |
|
90 |
Const (@{const_name LSC.ensure_testable}, fastype_of t --> fastype_of t) $ t |
|
91 |
val t = dynamic_value_strict |
|
92 |
(Lazysmallcheck_Result.get, Lazysmallcheck_Result.put, "Lazysmallcheck.put_counterexample") |
|
93 |
thy (Option.map o map) (ensure_testable t) [] |
|
94 |
in |
|
95 |
(t, NONE) |
|
96 |
end; |
|
97 |
||
98 |
||
99 |
val setup = |
|
100 |
Context.theory_map |
|
101 |
(Quickcheck.add_generator ("lazy_exhaustive", compile_generator_expr)) |
|
102 |
||
103 |
end; |