src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Fri Mar 11 15:21:13 2011 +0100 (2011-03-11)
changeset 41936 9792a882da9c
parent 41933 10f254a4e5b9
child 41938 645cca858c69
permissions -rw-r--r--
renaming tester from lazy_exhaustive to narrowing
     1 (*  Title:      HOL/Tools/Quickcheck/narrowing_generators.ML
     2     Author:     Lukas Bulwahn, TU Muenchen
     3 
     4 Narrowing-based counterexample generation 
     5 
     6 *)
     7 
     8 signature NARROWING_GENERATORS =
     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 Narrowing_Generators : NARROWING_GENERATORS =
    17 struct
    18 
    19 val target = "Haskell"
    20 
    21 (* invocation of Haskell interpreter *)
    22 
    23 val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs")
    24 
    25 fun exec verbose code =
    26   ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
    27 
    28 fun value ctxt (get, put, put_ml) (code, value) =
    29   let
    30     val tmp_prefix = "Quickcheck_Narrowing"
    31     fun run in_path = 
    32       let
    33         val code_file = Path.append in_path (Path.basic "Code.hs")
    34         val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
    35         val main_file = Path.append in_path (Path.basic "Main.hs")
    36         val main = "module Main where {\n\n" ^
    37           "import Narrowing_Engine;\n" ^
    38           "import Code;\n\n" ^
    39           "main = Narrowing_Engine.smallCheck 7 (Code.value ())\n\n" ^
    40           "}\n"
    41         val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
    42           (unprefix "module Code where {" code)
    43         val _ = File.write code_file code'
    44         val _ = File.write narrowing_engine_file narrowing_engine
    45         val _ = File.write main_file main
    46         val executable = Path.implode (Path.append in_path (Path.basic "isa_lsc"))
    47         val cmd = getenv "EXEC_GHC" ^ " -fglasgow-exts " ^ 
    48           (space_implode " " (map Path.implode [code_file, narrowing_engine_file, main_file])) ^
    49           " -o " ^ executable ^ " && " ^ executable
    50       in
    51         bash_output cmd
    52       end
    53     val result = Isabelle_System.with_tmp_dir tmp_prefix run
    54     val output_value = the_default "NONE"
    55       (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
    56     val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
    57       ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
    58     val ctxt' = ctxt
    59       |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
    60       |> Context.proof_map (exec false ml_code);
    61   in get ctxt' () end;
    62 
    63 fun evaluation cookie thy evaluator vs_t args =
    64   let
    65     val ctxt = ProofContext.init_global thy;
    66     val (program_code, value_name) = evaluator vs_t;
    67     val value_code = space_implode " "
    68       (value_name :: "()" :: map (enclose "(" ")") args);
    69   in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
    70 
    71 fun dynamic_value_strict cookie thy postproc t args =
    72   let
    73     fun evaluator naming program ((_, vs_ty), t) deps =
    74       evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args;
    75   in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
    76 
    77 (* counterexample generator *)
    78   
    79 structure Counterexample = Proof_Data
    80 (
    81   type T = unit -> term list option
    82   fun init _ () = error "Counterexample"
    83 )
    84 
    85 val put_counterexample =  Counterexample.put
    86   
    87 fun compile_generator_expr ctxt t size =
    88   let
    89     val thy = ProofContext.theory_of ctxt
    90     fun ensure_testable t =
    91       Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
    92     val t = dynamic_value_strict
    93       (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
    94       thy (Option.map o map) (ensure_testable t) []
    95   in
    96     (t, NONE)
    97   end;
    98 
    99 
   100 val setup =
   101   Context.theory_map
   102     (Quickcheck.add_generator ("narrowing", compile_generator_expr))
   103     
   104 end;