src/HOL/Tools/Quickcheck/narrowing_generators.ML
author wenzelm
Sat Apr 16 16:15:37 2011 +0200 (2011-04-16)
changeset 42361 23f352990944
parent 42258 79cb339d8989
child 42616 92715b528e78
permissions -rw-r--r--
modernized structure Proof_Context;
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@42184
    10
    Proof.context -> (term * term list) list -> int list -> term list option * Quickcheck.report option
bulwahn@41905
    11
  val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context
bulwahn@42023
    12
  val finite_functions : bool Config.T
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@42023
    19
(* configurations *)
bulwahn@42023
    20
bulwahn@42023
    21
val (finite_functions, setup_finite_functions) =
bulwahn@42023
    22
  Attrib.config_bool "quickcheck_finite_functions" (K true)
bulwahn@42023
    23
bulwahn@41963
    24
(* narrowing specific names and types *)
bulwahn@41963
    25
bulwahn@41963
    26
exception FUNCTION_TYPE;
bulwahn@41963
    27
bulwahn@41963
    28
val narrowingN = "narrowing";
bulwahn@41963
    29
bulwahn@41963
    30
fun narrowingT T =
bulwahn@41963
    31
  @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
bulwahn@41963
    32
bulwahn@41963
    33
fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
bulwahn@41963
    34
bulwahn@41963
    35
fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
bulwahn@41963
    36
bulwahn@41963
    37
fun mk_apply (T, t) (U, u) =
bulwahn@41963
    38
  let
bulwahn@41963
    39
    val (_, U') = dest_funT U
bulwahn@41963
    40
  in
bulwahn@41963
    41
    (U', Const (@{const_name Quickcheck_Narrowing.apply},
bulwahn@41963
    42
      narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
bulwahn@41963
    43
  end
bulwahn@41963
    44
  
bulwahn@41963
    45
fun mk_sum (t, u) =
bulwahn@41963
    46
  let
bulwahn@41963
    47
    val T = fastype_of t
bulwahn@41963
    48
  in
bulwahn@41963
    49
    Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
bulwahn@41963
    50
  end
bulwahn@41963
    51
bulwahn@41963
    52
(* creating narrowing instances *)
bulwahn@41963
    53
bulwahn@41963
    54
fun mk_equations descr vs tycos narrowings (Ts, Us) =
bulwahn@41963
    55
  let
bulwahn@41963
    56
    fun mk_call T =
bulwahn@42214
    57
      (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
bulwahn@41963
    58
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
bulwahn@41963
    59
      let
bulwahn@41963
    60
        val T = Type (tyco, Ts)
bulwahn@41963
    61
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@41963
    62
      in
bulwahn@41963
    63
        (T, nth narrowings k)
bulwahn@41963
    64
      end
bulwahn@41963
    65
    fun mk_consexpr simpleT (c, xs) =
bulwahn@41963
    66
      let
bulwahn@41963
    67
        val Ts = map fst xs
bulwahn@41963
    68
      in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
bulwahn@41963
    69
    fun mk_rhs exprs = foldr1 mk_sum exprs
bulwahn@41963
    70
    val rhss =
bulwahn@41963
    71
      Datatype_Aux.interpret_construction descr vs
bulwahn@41963
    72
        { atyp = mk_call, dtyp = mk_aux_call }
bulwahn@41963
    73
      |> (map o apfst) Type
bulwahn@41963
    74
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
bulwahn@41963
    75
      |> map mk_rhs
bulwahn@41963
    76
    val lhss = narrowings
bulwahn@41963
    77
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
bulwahn@41963
    78
  in
bulwahn@41963
    79
    eqs
bulwahn@41963
    80
  end
bulwahn@42214
    81
  
bulwahn@41963
    82
fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
bulwahn@41963
    83
  let
bulwahn@41963
    84
    val _ = Datatype_Aux.message config "Creating narrowing generators ...";
bulwahn@41963
    85
    val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
bulwahn@41963
    86
  in
bulwahn@41963
    87
    thy
bulwahn@41963
    88
    |> Class.instantiation (tycos, vs, @{sort narrowing})
bulwahn@42214
    89
    |> Quickcheck_Common.define_functions
bulwahn@42214
    90
      (fn narrowings => mk_equations descr vs tycos narrowings (Ts, Us), NONE)
bulwahn@42214
    91
      prfx [] narrowingsN (map narrowingT (Ts @ Us))
bulwahn@41963
    92
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@41963
    93
  end;
bulwahn@41963
    94
bulwahn@41963
    95
(* testing framework *)
bulwahn@41963
    96
bulwahn@41930
    97
val target = "Haskell"
bulwahn@41930
    98
bulwahn@41905
    99
(* invocation of Haskell interpreter *)
bulwahn@41905
   100
bulwahn@41930
   101
val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs")
bulwahn@41905
   102
bulwahn@41905
   103
fun exec verbose code =
bulwahn@41905
   104
  ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
bulwahn@41905
   105
bulwahn@42019
   106
fun value ctxt (get, put, put_ml) (code, value) size =
bulwahn@41905
   107
  let
bulwahn@41930
   108
    val tmp_prefix = "Quickcheck_Narrowing"
bulwahn@41905
   109
    fun run in_path = 
bulwahn@41905
   110
      let
bulwahn@41905
   111
        val code_file = Path.append in_path (Path.basic "Code.hs")
bulwahn@41930
   112
        val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
bulwahn@41905
   113
        val main_file = Path.append in_path (Path.basic "Main.hs")
bulwahn@41905
   114
        val main = "module Main where {\n\n" ^
bulwahn@41933
   115
          "import Narrowing_Engine;\n" ^
bulwahn@41905
   116
          "import Code;\n\n" ^
bulwahn@42090
   117
          "main = Narrowing_Engine.depthCheck " ^ string_of_int size ^ " (Code.value ())\n\n" ^
bulwahn@41905
   118
          "}\n"
bulwahn@41909
   119
        val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
bulwahn@41909
   120
          (unprefix "module Code where {" code)
bulwahn@41909
   121
        val _ = File.write code_file code'
bulwahn@41930
   122
        val _ = File.write narrowing_engine_file narrowing_engine
bulwahn@41905
   123
        val _ = File.write main_file main
wenzelm@41946
   124
        val executable = File.shell_path (Path.append in_path (Path.basic "isa_lsc"))
wenzelm@42039
   125
        val cmd = "( exec \"$ISABELLE_GHC\" -fglasgow-exts " ^
wenzelm@41946
   126
          (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
wenzelm@42039
   127
          " -o " ^ executable ^ "; ) && " ^ executable
bulwahn@41905
   128
      in
bulwahn@41905
   129
        bash_output cmd
bulwahn@41930
   130
      end
bulwahn@41909
   131
    val result = Isabelle_System.with_tmp_dir tmp_prefix run
bulwahn@41905
   132
    val output_value = the_default "NONE"
bulwahn@41905
   133
      (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
bulwahn@42020
   134
      |> translate_string (fn s => if s = "\\" then "\\\\" else s)
bulwahn@41905
   135
    val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
bulwahn@41905
   136
      ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
bulwahn@41905
   137
    val ctxt' = ctxt
bulwahn@41905
   138
      |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
bulwahn@41905
   139
      |> Context.proof_map (exec false ml_code);
bulwahn@41905
   140
  in get ctxt' () end;
bulwahn@41905
   141
bulwahn@42019
   142
fun evaluation cookie thy evaluator vs_t args size =
bulwahn@41905
   143
  let
wenzelm@42361
   144
    val ctxt = Proof_Context.init_global thy;
bulwahn@41905
   145
    val (program_code, value_name) = evaluator vs_t;
bulwahn@41905
   146
    val value_code = space_implode " "
bulwahn@41905
   147
      (value_name :: "()" :: map (enclose "(" ")") args);
bulwahn@42019
   148
  in Exn.interruptible_capture (value ctxt cookie (program_code, value_code)) size end;
bulwahn@41905
   149
bulwahn@42019
   150
fun dynamic_value_strict cookie thy postproc t args size =
bulwahn@41905
   151
  let
bulwahn@41905
   152
    fun evaluator naming program ((_, vs_ty), t) deps =
bulwahn@42019
   153
      evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args size;
bulwahn@41905
   154
  in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
bulwahn@41905
   155
bulwahn@41905
   156
(* counterexample generator *)
bulwahn@41905
   157
  
bulwahn@41932
   158
structure Counterexample = Proof_Data
bulwahn@41905
   159
(
bulwahn@41905
   160
  type T = unit -> term list option
bulwahn@41936
   161
  fun init _ () = error "Counterexample"
bulwahn@41905
   162
)
bulwahn@41905
   163
bulwahn@42024
   164
val put_counterexample = Counterexample.put
bulwahn@42023
   165
bulwahn@42024
   166
fun finitize_functions t =
bulwahn@42024
   167
  let
bulwahn@42024
   168
    val ((names, Ts), t') = apfst split_list (strip_abs t)
bulwahn@42024
   169
    fun mk_eval_ffun dT rT =
bulwahn@42024
   170
      Const (@{const_name "Quickcheck_Narrowing.eval_ffun"}, 
bulwahn@42024
   171
        Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
bulwahn@42024
   172
    fun mk_eval_cfun dT rT =
bulwahn@42024
   173
      Const (@{const_name "Quickcheck_Narrowing.eval_cfun"}, 
bulwahn@42024
   174
        Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
bulwahn@42024
   175
    fun eval_function (T as Type (@{type_name fun}, [dT, rT])) =
bulwahn@42024
   176
      let
bulwahn@42024
   177
        val (rt', rT') = eval_function rT
bulwahn@42024
   178
      in
bulwahn@42024
   179
        case dT of
bulwahn@42024
   180
          Type (@{type_name fun}, _) =>
bulwahn@42024
   181
            (fn t => absdummy (dT, rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
bulwahn@42024
   182
            Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
bulwahn@42024
   183
        | _ => (fn t => absdummy (dT, rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
bulwahn@42024
   184
            Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
bulwahn@42024
   185
      end
bulwahn@42024
   186
      | eval_function T = (I, T)
bulwahn@42024
   187
    val (tt, Ts') = split_list (map eval_function Ts)
bulwahn@42024
   188
    val t'' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) Ts), t')
bulwahn@42024
   189
  in
bulwahn@42024
   190
    list_abs (names ~~ Ts', t'')
bulwahn@42024
   191
  end
bulwahn@42023
   192
bulwahn@42184
   193
fun compile_generator_expr ctxt [(t, eval_terms)] [_, size] =
bulwahn@41905
   194
  let
wenzelm@42361
   195
    val thy = Proof_Context.theory_of ctxt
bulwahn@42028
   196
    val t' = list_abs_free (Term.add_frees t [], t)
bulwahn@42028
   197
    val t'' = if Config.get ctxt finite_functions then finitize_functions t' else t'
bulwahn@41905
   198
    fun ensure_testable t =
bulwahn@41930
   199
      Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
bulwahn@42023
   200
    val result = dynamic_value_strict
bulwahn@41932
   201
      (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
bulwahn@42184
   202
      thy (Option.map o map) (ensure_testable t'') [] size
bulwahn@41905
   203
  in
bulwahn@42023
   204
    (result, NONE)
bulwahn@41905
   205
  end;
bulwahn@41905
   206
bulwahn@41905
   207
bulwahn@41905
   208
val setup =
bulwahn@42258
   209
  Datatype.interpretation (Quickcheck_Common.ensure_sort_datatype
bulwahn@42258
   210
    (((@{sort typerep}, @{sort term_of}), @{sort narrowing}), instantiate_narrowing_datatype))
bulwahn@42023
   211
  #> setup_finite_functions
bulwahn@41963
   212
  #> Context.theory_map
bulwahn@41936
   213
    (Quickcheck.add_generator ("narrowing", compile_generator_expr))
bulwahn@41905
   214
    
bulwahn@41905
   215
end;