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