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