author | wenzelm |
Sun, 13 Mar 2011 19:16:19 +0100 | |
changeset 41952 | c7297638599b |
parent 41946 | 380f7f5ff126 |
child 41953 | 994d088fbfbc |
permissions | -rw-r--r-- |
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
1 |
(* Title: HOL/Tools/Quickcheck/narrowing_generators.ML |
41905 | 2 |
Author: Lukas Bulwahn, TU Muenchen |
3 |
||
41938 | 4 |
Narrowing-based counterexample generation. |
41905 | 5 |
*) |
6 |
||
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
7 |
signature NARROWING_GENERATORS = |
41905 | 8 |
sig |
9 |
val compile_generator_expr: |
|
10 |
Proof.context -> term -> int -> term list option * Quickcheck.report option |
|
11 |
val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context |
|
12 |
val setup: theory -> theory |
|
13 |
end; |
|
14 |
||
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
15 |
structure Narrowing_Generators : NARROWING_GENERATORS = |
41905 | 16 |
struct |
17 |
||
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
18 |
val target = "Haskell" |
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
19 |
|
41905 | 20 |
(* invocation of Haskell interpreter *) |
21 |
||
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
22 |
val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs") |
41905 | 23 |
|
24 |
fun exec verbose code = |
|
25 |
ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code) |
|
26 |
||
27 |
fun value ctxt (get, put, put_ml) (code, value) = |
|
28 |
let |
|
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
29 |
val tmp_prefix = "Quickcheck_Narrowing" |
41905 | 30 |
fun run in_path = |
31 |
let |
|
32 |
val code_file = Path.append in_path (Path.basic "Code.hs") |
|
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
33 |
val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs") |
41905 | 34 |
val main_file = Path.append in_path (Path.basic "Main.hs") |
35 |
val main = "module Main where {\n\n" ^ |
|
41933
10f254a4e5b9
adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents:
41932
diff
changeset
|
36 |
"import Narrowing_Engine;\n" ^ |
41905 | 37 |
"import Code;\n\n" ^ |
41933
10f254a4e5b9
adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents:
41932
diff
changeset
|
38 |
"main = Narrowing_Engine.smallCheck 7 (Code.value ())\n\n" ^ |
41905 | 39 |
"}\n" |
41909
383bbdad1650
replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents:
41908
diff
changeset
|
40 |
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
|
41 |
(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
|
42 |
val _ = File.write code_file code' |
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
43 |
val _ = File.write narrowing_engine_file narrowing_engine |
41905 | 44 |
val _ = File.write main_file main |
41946 | 45 |
val executable = File.shell_path (Path.append in_path (Path.basic "isa_lsc")) |
41952
c7297638599b
cleanup of former settings GHC_PATH, EXEC_GHC, EXEC_OCAML, EXEC_SWIPL, EXEC_YAP -- discontinued implicit detection;
wenzelm
parents:
41946
diff
changeset
|
46 |
val cmd = "\"$ISABELLE_GHC\" -fglasgow-exts " ^ |
41946 | 47 |
(space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^ |
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
48 |
" -o " ^ executable ^ " && " ^ executable |
41905 | 49 |
in |
50 |
bash_output cmd |
|
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
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 = |
|
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
73 |
evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args; |
41905 | 74 |
in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end; |
75 |
||
76 |
(* counterexample generator *) |
|
77 |
||
41932
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents:
41930
diff
changeset
|
78 |
structure Counterexample = Proof_Data |
41905 | 79 |
( |
80 |
type T = unit -> term list option |
|
41936
9792a882da9c
renaming tester from lazy_exhaustive to narrowing
bulwahn
parents:
41933
diff
changeset
|
81 |
fun init _ () = error "Counterexample" |
41905 | 82 |
) |
83 |
||
41932
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents:
41930
diff
changeset
|
84 |
val put_counterexample = Counterexample.put |
41905 | 85 |
|
86 |
fun compile_generator_expr ctxt t size = |
|
87 |
let |
|
88 |
val thy = ProofContext.theory_of ctxt |
|
89 |
fun ensure_testable t = |
|
41930
1e008cc4883a
renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents:
41925
diff
changeset
|
90 |
Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t |
41905 | 91 |
val t = dynamic_value_strict |
41932
e8f113ce8a94
adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents:
41930
diff
changeset
|
92 |
(Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample") |
41905 | 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 |
|
41936
9792a882da9c
renaming tester from lazy_exhaustive to narrowing
bulwahn
parents:
41933
diff
changeset
|
101 |
(Quickcheck.add_generator ("narrowing", compile_generator_expr)) |
41905 | 102 |
|
103 |
end; |