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