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