src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Mon Mar 14 12:34:10 2011 +0100 (2011-03-14)
changeset 41963 d8c3b26b3da4
parent 41953 994d088fbfbc
child 42019 a9445d87bc3e
permissions -rw-r--r--
tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
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@41963
    18
fun mk_undefined T = Const(@{const_name undefined}, T)
bulwahn@41963
    19
bulwahn@41963
    20
(* narrowing specific names and types *)
bulwahn@41963
    21
bulwahn@41963
    22
exception FUNCTION_TYPE;
bulwahn@41963
    23
bulwahn@41963
    24
val narrowingN = "narrowing";
bulwahn@41963
    25
bulwahn@41963
    26
fun narrowingT T =
bulwahn@41963
    27
  @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
bulwahn@41963
    28
bulwahn@41963
    29
fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
bulwahn@41963
    30
bulwahn@41963
    31
fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
bulwahn@41963
    32
bulwahn@41963
    33
fun mk_apply (T, t) (U, u) =
bulwahn@41963
    34
  let
bulwahn@41963
    35
    val (_, U') = dest_funT U
bulwahn@41963
    36
  in
bulwahn@41963
    37
    (U', Const (@{const_name Quickcheck_Narrowing.apply},
bulwahn@41963
    38
      narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
bulwahn@41963
    39
  end
bulwahn@41963
    40
  
bulwahn@41963
    41
fun mk_sum (t, u) =
bulwahn@41963
    42
  let
bulwahn@41963
    43
    val T = fastype_of t
bulwahn@41963
    44
  in
bulwahn@41963
    45
    Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
bulwahn@41963
    46
  end
bulwahn@41963
    47
bulwahn@41963
    48
(* creating narrowing instances *)
bulwahn@41963
    49
bulwahn@41963
    50
fun mk_equations descr vs tycos narrowings (Ts, Us) =
bulwahn@41963
    51
  let
bulwahn@41963
    52
    fun mk_call T =
bulwahn@41963
    53
      let
bulwahn@41963
    54
        val narrowing =
bulwahn@41963
    55
          Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T)
bulwahn@41963
    56
      in
bulwahn@41963
    57
        (T, narrowing)
bulwahn@41963
    58
      end
bulwahn@41963
    59
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
bulwahn@41963
    60
      let
bulwahn@41963
    61
        val T = Type (tyco, Ts)
bulwahn@41963
    62
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@41963
    63
      in
bulwahn@41963
    64
        (T, nth narrowings k)
bulwahn@41963
    65
      end
bulwahn@41963
    66
    fun mk_consexpr simpleT (c, xs) =
bulwahn@41963
    67
      let
bulwahn@41963
    68
        val Ts = map fst xs
bulwahn@41963
    69
      in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
bulwahn@41963
    70
    fun mk_rhs exprs = foldr1 mk_sum exprs
bulwahn@41963
    71
    val rhss =
bulwahn@41963
    72
      Datatype_Aux.interpret_construction descr vs
bulwahn@41963
    73
        { atyp = mk_call, dtyp = mk_aux_call }
bulwahn@41963
    74
      |> (map o apfst) Type
bulwahn@41963
    75
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
bulwahn@41963
    76
      |> map mk_rhs
bulwahn@41963
    77
    val lhss = narrowings
bulwahn@41963
    78
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
bulwahn@41963
    79
  in
bulwahn@41963
    80
    eqs
bulwahn@41963
    81
  end
bulwahn@41963
    82
bulwahn@41963
    83
bulwahn@41963
    84
fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
bulwahn@41963
    85
  let
bulwahn@41963
    86
    val _ = Datatype_Aux.message config "Creating narrowing generators ...";
bulwahn@41963
    87
    val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
bulwahn@41963
    88
  in
bulwahn@41963
    89
    thy
bulwahn@41963
    90
    |> Class.instantiation (tycos, vs, @{sort narrowing})
bulwahn@41963
    91
    |> (fold_map (fn (name, T) => Local_Theory.define
bulwahn@41963
    92
          ((Binding.conceal (Binding.name name), NoSyn),
bulwahn@41963
    93
            (apfst Binding.conceal Attrib.empty_binding, mk_undefined (narrowingT T)))
bulwahn@41963
    94
        #> apfst fst) (narrowingsN ~~ (Ts @ Us))
bulwahn@41963
    95
      #> (fn (narrowings, lthy) =>
bulwahn@41963
    96
        let
bulwahn@41963
    97
          val eqs_t = mk_equations descr vs tycos narrowings (Ts, Us)
bulwahn@41963
    98
          val eqs = map (fn eq => Goal.prove lthy ["f", "i"] [] eq
bulwahn@41963
    99
            (fn _ => Skip_Proof.cheat_tac (ProofContext.theory_of lthy))) eqs_t
bulwahn@41963
   100
        in
bulwahn@41963
   101
          fold (fn (name, eq) => Local_Theory.note
bulwahn@41963
   102
          ((Binding.conceal (Binding.qualify true prfx
bulwahn@41963
   103
             (Binding.qualify true name (Binding.name "simps"))),
bulwahn@41963
   104
             Code.add_default_eqn_attrib :: map (Attrib.internal o K)
bulwahn@41963
   105
               [Simplifier.simp_add, Nitpick_Simps.add]), [eq]) #> snd) (narrowingsN ~~ eqs) lthy
bulwahn@41963
   106
        end))
bulwahn@41963
   107
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@41963
   108
  end;
bulwahn@41963
   109
bulwahn@41963
   110
(* testing framework *)
bulwahn@41963
   111
bulwahn@41930
   112
val target = "Haskell"
bulwahn@41930
   113
bulwahn@41905
   114
(* invocation of Haskell interpreter *)
bulwahn@41905
   115
bulwahn@41930
   116
val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs")
bulwahn@41905
   117
bulwahn@41905
   118
fun exec verbose code =
bulwahn@41905
   119
  ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
bulwahn@41905
   120
bulwahn@41905
   121
fun value ctxt (get, put, put_ml) (code, value) =
bulwahn@41905
   122
  let
bulwahn@41930
   123
    val tmp_prefix = "Quickcheck_Narrowing"
bulwahn@41905
   124
    fun run in_path = 
bulwahn@41905
   125
      let
bulwahn@41905
   126
        val code_file = Path.append in_path (Path.basic "Code.hs")
bulwahn@41930
   127
        val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
bulwahn@41905
   128
        val main_file = Path.append in_path (Path.basic "Main.hs")
bulwahn@41905
   129
        val main = "module Main where {\n\n" ^
bulwahn@41933
   130
          "import Narrowing_Engine;\n" ^
bulwahn@41905
   131
          "import Code;\n\n" ^
bulwahn@41933
   132
          "main = Narrowing_Engine.smallCheck 7 (Code.value ())\n\n" ^
bulwahn@41905
   133
          "}\n"
bulwahn@41909
   134
        val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
bulwahn@41909
   135
          (unprefix "module Code where {" code)
bulwahn@41909
   136
        val _ = File.write code_file code'
bulwahn@41930
   137
        val _ = File.write narrowing_engine_file narrowing_engine
bulwahn@41905
   138
        val _ = File.write main_file main
wenzelm@41946
   139
        val executable = File.shell_path (Path.append in_path (Path.basic "isa_lsc"))
bulwahn@41963
   140
        val cmd = "\"$ISABELLE_GHC\" -fglasgow-exts " ^
wenzelm@41946
   141
          (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
bulwahn@41930
   142
          " -o " ^ executable ^ " && " ^ executable
bulwahn@41963
   143
          (* FIXME: should use bash command exec but does not work with && *) 
bulwahn@41905
   144
      in
bulwahn@41905
   145
        bash_output cmd
bulwahn@41930
   146
      end
bulwahn@41909
   147
    val result = Isabelle_System.with_tmp_dir tmp_prefix run
bulwahn@41905
   148
    val output_value = the_default "NONE"
bulwahn@41905
   149
      (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
bulwahn@41905
   150
    val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
bulwahn@41905
   151
      ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
bulwahn@41905
   152
    val ctxt' = ctxt
bulwahn@41905
   153
      |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
bulwahn@41905
   154
      |> Context.proof_map (exec false ml_code);
bulwahn@41905
   155
  in get ctxt' () end;
bulwahn@41905
   156
bulwahn@41905
   157
fun evaluation cookie thy evaluator vs_t args =
bulwahn@41905
   158
  let
bulwahn@41905
   159
    val ctxt = ProofContext.init_global thy;
bulwahn@41905
   160
    val (program_code, value_name) = evaluator vs_t;
bulwahn@41905
   161
    val value_code = space_implode " "
bulwahn@41905
   162
      (value_name :: "()" :: map (enclose "(" ")") args);
bulwahn@41905
   163
  in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
bulwahn@41905
   164
bulwahn@41905
   165
fun dynamic_value_strict cookie thy postproc t args =
bulwahn@41905
   166
  let
bulwahn@41905
   167
    fun evaluator naming program ((_, vs_ty), t) deps =
bulwahn@41930
   168
      evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args;
bulwahn@41905
   169
  in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
bulwahn@41905
   170
bulwahn@41905
   171
(* counterexample generator *)
bulwahn@41905
   172
  
bulwahn@41932
   173
structure Counterexample = Proof_Data
bulwahn@41905
   174
(
bulwahn@41905
   175
  type T = unit -> term list option
bulwahn@41936
   176
  fun init _ () = error "Counterexample"
bulwahn@41905
   177
)
bulwahn@41905
   178
bulwahn@41932
   179
val put_counterexample =  Counterexample.put
bulwahn@41905
   180
  
bulwahn@41905
   181
fun compile_generator_expr ctxt t size =
bulwahn@41905
   182
  let
bulwahn@41905
   183
    val thy = ProofContext.theory_of ctxt
bulwahn@41905
   184
    fun ensure_testable t =
bulwahn@41930
   185
      Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
bulwahn@41905
   186
    val t = dynamic_value_strict
bulwahn@41932
   187
      (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
bulwahn@41905
   188
      thy (Option.map o map) (ensure_testable t) []
bulwahn@41905
   189
  in
bulwahn@41905
   190
    (t, NONE)
bulwahn@41905
   191
  end;
bulwahn@41905
   192
bulwahn@41905
   193
bulwahn@41905
   194
val setup =
bulwahn@41963
   195
  Datatype.interpretation
bulwahn@41963
   196
    (Quickcheck_Common.ensure_sort_datatype (@{sort narrowing}, instantiate_narrowing_datatype))
bulwahn@41963
   197
  #> Context.theory_map
bulwahn@41936
   198
    (Quickcheck.add_generator ("narrowing", compile_generator_expr))
bulwahn@41905
   199
    
bulwahn@41905
   200
end;