src/HOL/Tools/LSC/lazysmallcheck.ML
author bulwahn
Fri Mar 11 10:37:37 2011 +0100 (2011-03-11)
changeset 41908 3bd9a21366d2
parent 41905 e2611bc96022
child 41909 383bbdad1650
permissions -rw-r--r--
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@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@41908
    29
    fun make_cmd executable files =
bulwahn@41908
    30
      getenv "EXEC_GHC" ^ " -fglasgow-exts " ^ (space_implode " " (map Path.implode files)) ^
bulwahn@41908
    31
        " -o " ^ executable ^ " && " ^ executable
bulwahn@41905
    32
    fun run in_path = 
bulwahn@41905
    33
      let
bulwahn@41905
    34
        val code_file = Path.append in_path (Path.basic "Code.hs")
bulwahn@41905
    35
        val lsc_file = Path.append in_path (Path.basic "LazySmallCheck.hs")
bulwahn@41905
    36
        val main_file = Path.append in_path (Path.basic "Main.hs")
bulwahn@41905
    37
        val main = "module Main where {\n\n" ^
bulwahn@41905
    38
          "import LazySmallCheck;\n" ^
bulwahn@41905
    39
          "import Code;\n\n" ^
bulwahn@41908
    40
          "main = LazySmallCheck.smallCheck 7 (Code.value ())\n\n" ^
bulwahn@41905
    41
          "}\n"
bulwahn@41905
    42
        val _ = File.write code_file code
bulwahn@41905
    43
        val _ = File.write lsc_file lsc_module
bulwahn@41905
    44
        val _ = File.write main_file main
bulwahn@41908
    45
        val executable = Path.implode (Path.append in_path (Path.basic "isa_lsc"))
bulwahn@41908
    46
        val cmd = make_cmd executable [code_file, lsc_file, main_file]
bulwahn@41905
    47
      in
bulwahn@41905
    48
        bash_output cmd
bulwahn@41905
    49
      end 
bulwahn@41905
    50
    val result = Isabelle_System.with_tmp_dir prefix run
bulwahn@41905
    51
    val output_value = the_default "NONE"
bulwahn@41905
    52
      (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
bulwahn@41905
    53
    val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
bulwahn@41905
    54
      ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
bulwahn@41905
    55
    val ctxt' = ctxt
bulwahn@41905
    56
      |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
bulwahn@41905
    57
      |> Context.proof_map (exec false ml_code);
bulwahn@41905
    58
  in get ctxt' () end;
bulwahn@41905
    59
bulwahn@41905
    60
fun evaluation cookie thy evaluator vs_t args =
bulwahn@41905
    61
  let
bulwahn@41905
    62
    val ctxt = ProofContext.init_global thy;
bulwahn@41905
    63
    val (program_code, value_name) = evaluator vs_t;
bulwahn@41905
    64
    val value_code = space_implode " "
bulwahn@41905
    65
      (value_name :: "()" :: map (enclose "(" ")") args);
bulwahn@41905
    66
  in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
bulwahn@41905
    67
bulwahn@41905
    68
fun dynamic_value_strict cookie thy postproc t args =
bulwahn@41905
    69
  let
bulwahn@41905
    70
    fun evaluator naming program ((_, vs_ty), t) deps =
bulwahn@41905
    71
      evaluation cookie thy (Code_Target.evaluator thy "Haskell" naming program deps) (vs_ty, t) args;
bulwahn@41905
    72
  in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
bulwahn@41905
    73
bulwahn@41905
    74
(* counterexample generator *)
bulwahn@41905
    75
  
bulwahn@41905
    76
structure Lazysmallcheck_Result = Proof_Data
bulwahn@41905
    77
(
bulwahn@41905
    78
  type T = unit -> term list option
bulwahn@41905
    79
  fun init _ () = error "Lazysmallcheck_Result"
bulwahn@41905
    80
)
bulwahn@41905
    81
bulwahn@41905
    82
val put_counterexample = Lazysmallcheck_Result.put
bulwahn@41905
    83
  
bulwahn@41905
    84
fun compile_generator_expr ctxt t size =
bulwahn@41905
    85
  let
bulwahn@41905
    86
    val thy = ProofContext.theory_of ctxt
bulwahn@41905
    87
    fun ensure_testable t =
bulwahn@41905
    88
      Const (@{const_name LSC.ensure_testable}, fastype_of t --> fastype_of t) $ t
bulwahn@41905
    89
    val t = dynamic_value_strict
bulwahn@41905
    90
      (Lazysmallcheck_Result.get, Lazysmallcheck_Result.put, "Lazysmallcheck.put_counterexample")
bulwahn@41905
    91
      thy (Option.map o map) (ensure_testable t) []
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;