src/HOL/Tools/Quickcheck/narrowing_generators.ML
author haftmann
Fri Jul 27 22:26:38 2012 +0200 (2012-07-27)
changeset 48568 084cd758a8ab
parent 48565 7c497a239007
child 48901 5e0455e29339
permissions -rw-r--r--
evaluation: allow multiple code modules
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@43237
     9
  val allow_existentials : bool Config.T
bulwahn@42023
    10
  val finite_functions : bool Config.T
bulwahn@43079
    11
  val overlord : bool Config.T
wenzelm@48270
    12
  val ghc_options : string Config.T  (* FIXME prefer settings, i.e. getenv (!?) *)
bulwahn@43891
    13
  val active : bool Config.T
bulwahn@43237
    14
  datatype counterexample = Universal_Counterexample of (term * counterexample)
bulwahn@43237
    15
    | Existential_Counterexample of (term * counterexample) list
bulwahn@43237
    16
    | Empty_Assignment
bulwahn@45725
    17
  val put_counterexample: (unit -> (bool * term list) option) -> Proof.context -> Proof.context
bulwahn@43237
    18
  val put_existential_counterexample : (unit -> counterexample option) -> Proof.context -> Proof.context
bulwahn@41905
    19
  val setup: theory -> theory
bulwahn@41905
    20
end;
bulwahn@41905
    21
bulwahn@41930
    22
structure Narrowing_Generators : NARROWING_GENERATORS =
bulwahn@41905
    23
struct
bulwahn@41905
    24
bulwahn@42023
    25
(* configurations *)
bulwahn@42023
    26
bulwahn@43237
    27
val allow_existentials = Attrib.setup_config_bool @{binding quickcheck_allow_existentials} (K true)
wenzelm@42616
    28
val finite_functions = Attrib.setup_config_bool @{binding quickcheck_finite_functions} (K true)
bulwahn@43047
    29
val overlord = Attrib.setup_config_bool @{binding quickcheck_narrowing_overlord} (K false)
bulwahn@47843
    30
val ghc_options = Attrib.setup_config_string @{binding quickcheck_narrowing_ghc_options} (K "")
bulwahn@42023
    31
bulwahn@43047
    32
(* partial_term_of instances *)
bulwahn@43047
    33
bulwahn@43047
    34
fun mk_partial_term_of (x, T) =
bulwahn@43047
    35
  Const (@{const_name Quickcheck_Narrowing.partial_term_of_class.partial_term_of},
bulwahn@43047
    36
    Term.itselfT T --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
bulwahn@43047
    37
      $ Const ("TYPE", Term.itselfT T) $ x
bulwahn@43047
    38
bulwahn@43047
    39
(** formal definition **)
bulwahn@43047
    40
bulwahn@43047
    41
fun add_partial_term_of tyco raw_vs thy =
bulwahn@43047
    42
  let
bulwahn@43047
    43
    val vs = map (fn (v, _) => (v, @{sort typerep})) raw_vs;
bulwahn@43047
    44
    val ty = Type (tyco, map TFree vs);
bulwahn@43047
    45
    val lhs = Const (@{const_name partial_term_of},
bulwahn@43047
    46
        Term.itselfT ty --> @{typ narrowing_term} --> @{typ Code_Evaluation.term})
bulwahn@43047
    47
      $ Free ("x", Term.itselfT ty) $ Free ("t", @{typ narrowing_term});
bulwahn@43047
    48
    val rhs = @{term "undefined :: Code_Evaluation.term"};
bulwahn@43047
    49
    val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
bulwahn@43047
    50
    fun triv_name_of t = (fst o dest_Free o fst o strip_comb o fst
bulwahn@43047
    51
      o HOLogic.dest_eq o HOLogic.dest_Trueprop) t ^ "_triv";
bulwahn@43047
    52
  in
bulwahn@43047
    53
    thy
bulwahn@43047
    54
    |> Class.instantiation ([tyco], vs, @{sort partial_term_of})
bulwahn@43047
    55
    |> `(fn lthy => Syntax.check_term lthy eq)
bulwahn@43047
    56
    |-> (fn eq => Specification.definition (NONE, ((Binding.name (triv_name_of eq), []), eq)))
bulwahn@43047
    57
    |> snd
bulwahn@43047
    58
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@43047
    59
  end;
bulwahn@43047
    60
bulwahn@43047
    61
fun ensure_partial_term_of (tyco, (raw_vs, _)) thy =
bulwahn@43047
    62
  let
wenzelm@48272
    63
    val need_inst = not (Sorts.has_instance (Sign.classes_of thy) tyco @{sort partial_term_of})
wenzelm@48272
    64
      andalso Sorts.has_instance (Sign.classes_of thy) tyco @{sort typerep};
bulwahn@43047
    65
  in if need_inst then add_partial_term_of tyco raw_vs thy else thy end;
bulwahn@43047
    66
bulwahn@43047
    67
bulwahn@43047
    68
(** code equations for datatypes **)
bulwahn@43047
    69
bulwahn@43047
    70
fun mk_partial_term_of_eq thy ty (i, (c, (_, tys))) =
bulwahn@43047
    71
  let
wenzelm@43329
    72
    val frees = map Free (Name.invent_names Name.context "a" (map (K @{typ narrowing_term}) tys))
bulwahn@46758
    73
    val narrowing_term = @{term "Quickcheck_Narrowing.Narrowing_constructor"} $ HOLogic.mk_number @{typ code_int} i
bulwahn@43079
    74
      $ (HOLogic.mk_list @{typ narrowing_term} (rev frees))
bulwahn@43047
    75
    val rhs = fold (fn u => fn t => @{term "Code_Evaluation.App"} $ t $ u)
bulwahn@43047
    76
        (map mk_partial_term_of (frees ~~ tys))
bulwahn@43047
    77
        (@{term "Code_Evaluation.Const"} $ HOLogic.mk_literal c $ HOLogic.mk_typerep (tys ---> ty))
bulwahn@43047
    78
    val insts =
wenzelm@45344
    79
      map (SOME o Thm.cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
bulwahn@43047
    80
        [Free ("ty", Term.itselfT ty), narrowing_term, rhs]
bulwahn@43047
    81
    val cty = Thm.ctyp_of thy ty;
bulwahn@43047
    82
  in
bulwahn@43047
    83
    @{thm partial_term_of_anything}
bulwahn@43047
    84
    |> Drule.instantiate' [SOME cty] insts
bulwahn@43047
    85
    |> Thm.varifyT_global
bulwahn@43047
    86
  end
bulwahn@43047
    87
bulwahn@43047
    88
fun add_partial_term_of_code tyco raw_vs raw_cs thy =
bulwahn@43047
    89
  let
bulwahn@43047
    90
    val algebra = Sign.classes_of thy;
bulwahn@43047
    91
    val vs = map (fn (v, sort) =>
bulwahn@43047
    92
      (v, curry (Sorts.inter_sort algebra) @{sort typerep} sort)) raw_vs;
bulwahn@43047
    93
    val ty = Type (tyco, map TFree vs);
bulwahn@43047
    94
    val cs = (map o apsnd o apsnd o map o map_atyps)
bulwahn@43047
    95
      (fn TFree (v, _) => TFree (v, (the o AList.lookup (op =) vs) v)) raw_cs;
bulwahn@43047
    96
    val const = AxClass.param_of_inst thy (@{const_name partial_term_of}, tyco);
wenzelm@45344
    97
    val var_insts =
wenzelm@45344
    98
      map (SOME o Thm.cterm_of thy o Logic.unvarify_types_global o Logic.varify_global)
bulwahn@46758
    99
        [Free ("ty", Term.itselfT ty), @{term "Quickcheck_Narrowing.Narrowing_variable p tt"},
wenzelm@45344
   100
          @{term "Code_Evaluation.Free (STR ''_'')"} $ HOLogic.mk_typerep ty];
bulwahn@43079
   101
    val var_eq =
bulwahn@43079
   102
      @{thm partial_term_of_anything}
bulwahn@43079
   103
      |> Drule.instantiate' [SOME (Thm.ctyp_of thy ty)] var_insts
bulwahn@43079
   104
      |> Thm.varifyT_global
bulwahn@43079
   105
    val eqs = var_eq :: map_index (mk_partial_term_of_eq thy ty) cs;
bulwahn@43047
   106
 in
bulwahn@43047
   107
    thy
bulwahn@43047
   108
    |> Code.del_eqns const
bulwahn@43047
   109
    |> fold Code.add_eqn eqs
bulwahn@43047
   110
  end;
bulwahn@43047
   111
bulwahn@43047
   112
fun ensure_partial_term_of_code (tyco, (raw_vs, cs)) thy =
bulwahn@43047
   113
  let
wenzelm@48272
   114
    val has_inst = Sorts.has_instance (Sign.classes_of thy) tyco @{sort partial_term_of};
bulwahn@43047
   115
  in if has_inst then add_partial_term_of_code tyco raw_vs cs thy else thy end;
bulwahn@43047
   116
bulwahn@43047
   117
bulwahn@43047
   118
(* narrowing generators *)
bulwahn@43047
   119
bulwahn@43047
   120
(** narrowing specific names and types **)
bulwahn@41963
   121
bulwahn@41963
   122
exception FUNCTION_TYPE;
bulwahn@41963
   123
bulwahn@41963
   124
val narrowingN = "narrowing";
bulwahn@41963
   125
bulwahn@41963
   126
fun narrowingT T =
bulwahn@46758
   127
  @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.narrowing_cons}, [T])
bulwahn@41963
   128
bulwahn@41963
   129
fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
bulwahn@41963
   130
bulwahn@41963
   131
fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
bulwahn@41963
   132
bulwahn@41963
   133
fun mk_apply (T, t) (U, u) =
bulwahn@41963
   134
  let
bulwahn@41963
   135
    val (_, U') = dest_funT U
bulwahn@41963
   136
  in
bulwahn@41963
   137
    (U', Const (@{const_name Quickcheck_Narrowing.apply},
bulwahn@41963
   138
      narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
bulwahn@41963
   139
  end
bulwahn@41963
   140
  
bulwahn@41963
   141
fun mk_sum (t, u) =
bulwahn@41963
   142
  let
bulwahn@41963
   143
    val T = fastype_of t
bulwahn@41963
   144
  in
bulwahn@41963
   145
    Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
bulwahn@41963
   146
  end
bulwahn@41963
   147
bulwahn@43047
   148
(** deriving narrowing instances **)
bulwahn@41963
   149
bulwahn@41963
   150
fun mk_equations descr vs tycos narrowings (Ts, Us) =
bulwahn@41963
   151
  let
bulwahn@41963
   152
    fun mk_call T =
bulwahn@42214
   153
      (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
bulwahn@41963
   154
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
bulwahn@41963
   155
      let
bulwahn@41963
   156
        val T = Type (tyco, Ts)
bulwahn@41963
   157
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
bulwahn@41963
   158
      in
bulwahn@41963
   159
        (T, nth narrowings k)
bulwahn@41963
   160
      end
bulwahn@41963
   161
    fun mk_consexpr simpleT (c, xs) =
bulwahn@41963
   162
      let
bulwahn@41963
   163
        val Ts = map fst xs
bulwahn@41963
   164
      in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
bulwahn@41963
   165
    fun mk_rhs exprs = foldr1 mk_sum exprs
bulwahn@41963
   166
    val rhss =
bulwahn@41963
   167
      Datatype_Aux.interpret_construction descr vs
bulwahn@41963
   168
        { atyp = mk_call, dtyp = mk_aux_call }
bulwahn@41963
   169
      |> (map o apfst) Type
bulwahn@41963
   170
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
bulwahn@41963
   171
      |> map mk_rhs
bulwahn@41963
   172
    val lhss = narrowings
bulwahn@41963
   173
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
bulwahn@41963
   174
  in
bulwahn@41963
   175
    eqs
bulwahn@41963
   176
  end
bulwahn@43237
   177
    
bulwahn@43237
   178
fun contains_recursive_type_under_function_types xs =
bulwahn@43237
   179
  exists (fn (_, (_, _, cs)) => cs |> exists (snd #> exists (fn dT =>
bulwahn@43237
   180
    (case Datatype_Aux.strip_dtyp dT of (_ :: _, Datatype.DtRec _) => true | _ => false)))) xs
bulwahn@43237
   181
bulwahn@41963
   182
fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
bulwahn@41963
   183
  let
bulwahn@41963
   184
    val _ = Datatype_Aux.message config "Creating narrowing generators ...";
bulwahn@41963
   185
    val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
bulwahn@41963
   186
  in
bulwahn@43237
   187
    if not (contains_recursive_type_under_function_types descr) then
bulwahn@43237
   188
      thy
bulwahn@43237
   189
      |> Class.instantiation (tycos, vs, @{sort narrowing})
bulwahn@43237
   190
      |> Quickcheck_Common.define_functions
bulwahn@43237
   191
        (fn narrowings => mk_equations descr vs tycos narrowings (Ts, Us), NONE)
bulwahn@43237
   192
        prfx [] narrowingsN (map narrowingT (Ts @ Us))
bulwahn@43237
   193
      |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
bulwahn@43237
   194
    else
bulwahn@43237
   195
      thy
bulwahn@41963
   196
  end;
bulwahn@41963
   197
bulwahn@41963
   198
(* testing framework *)
bulwahn@41963
   199
bulwahn@43308
   200
val target = "Haskell_Quickcheck"
bulwahn@41930
   201
bulwahn@43047
   202
(** invocation of Haskell interpreter **)
bulwahn@41905
   203
wenzelm@43702
   204
val narrowing_engine =
wenzelm@43702
   205
  Context.>>> (Context.map_theory_result
wenzelm@43702
   206
    (Thy_Load.use_file (Path.explode "Tools/Quickcheck/Narrowing_Engine.hs")))
wenzelm@43702
   207
wenzelm@43702
   208
val pnf_narrowing_engine =
wenzelm@43702
   209
  Context.>>> (Context.map_theory_result
wenzelm@43702
   210
    (Thy_Load.use_file (Path.explode "Tools/Quickcheck/PNF_Narrowing_Engine.hs")))
bulwahn@41905
   211
bulwahn@41905
   212
fun exec verbose code =
bulwahn@41905
   213
  ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
bulwahn@41905
   214
bulwahn@43079
   215
fun with_overlord_dir name f =
bulwahn@43047
   216
  let
wenzelm@43602
   217
    val path =
wenzelm@43602
   218
      Path.append (Path.explode "$ISABELLE_HOME_USER") (Path.basic (name ^ serial_string ()))
bulwahn@43047
   219
    val _ = Isabelle_System.mkdirs path;
bulwahn@43047
   220
  in Exn.release (Exn.capture f path) end;
bulwahn@43379
   221
bulwahn@43379
   222
fun elapsed_time description e =
bulwahn@43379
   223
  let val ({elapsed, ...}, result) = Timing.timing e ()
bulwahn@43379
   224
  in (result, (description, Time.toMilliseconds elapsed)) end
bulwahn@43047
   225
  
haftmann@48568
   226
fun value (contains_existentials, ((genuine_only, (quiet, verbose)), size)) ctxt cookie (code_modules, value_name) =
bulwahn@41905
   227
  let
bulwahn@45756
   228
    val ((is_genuine, counterexample_of), (get, put, put_ml)) = cookie
bulwahn@43308
   229
    fun message s = if quiet then () else Output.urgent_message s
bulwahn@45765
   230
    fun verbose_message s = if not quiet andalso verbose then Output.urgent_message s else ()
bulwahn@43585
   231
    val current_size = Unsynchronized.ref 0
bulwahn@43585
   232
    val current_result = Unsynchronized.ref Quickcheck.empty_result 
bulwahn@43585
   233
    fun excipit () =
bulwahn@43585
   234
      "Quickcheck ran out of time while testing at size " ^ string_of_int (!current_size)
bulwahn@41930
   235
    val tmp_prefix = "Quickcheck_Narrowing"
bulwahn@47843
   236
    val ghc_options = Config.get ctxt ghc_options
bulwahn@43079
   237
    val with_tmp_dir =
bulwahn@43079
   238
      if Config.get ctxt overlord then with_overlord_dir else Isabelle_System.with_tmp_dir 
bulwahn@41905
   239
    fun run in_path = 
bulwahn@41905
   240
      let
haftmann@48568
   241
        fun mk_code_file name = Path.append in_path (Path.basic (name ^ ".hs"))
haftmann@48568
   242
        val generatedN = Code_Target.generatedN
haftmann@48568
   243
        val includes = AList.delete (op =) generatedN code_modules |> (map o apfst) mk_code_file;
haftmann@48568
   244
        val code = the (AList.lookup (op =) code_modules generatedN)
haftmann@48568
   245
        val code_file = mk_code_file generatedN
haftmann@48568
   246
        val narrowing_engine_file = mk_code_file "Narrowing_Engine"
haftmann@48568
   247
        val main_file = mk_code_file "Main"
bulwahn@41905
   248
        val main = "module Main where {\n\n" ^
bulwahn@46335
   249
          "import System.IO;\n" ^
bulwahn@46335
   250
          "import System.Environment;\n" ^
bulwahn@41933
   251
          "import Narrowing_Engine;\n" ^
haftmann@48568
   252
          "import " ^ generatedN ^ " ;\n\n" ^
bulwahn@45685
   253
          "main = getArgs >>= \\[potential, size] -> " ^
haftmann@48568
   254
          "Narrowing_Engine.depthCheck (read potential) (read size) (" ^ generatedN ^ ".value ())\n\n" ^
bulwahn@41905
   255
          "}\n"
haftmann@48565
   256
        val code' = code
haftmann@48565
   257
          |> unsuffix "}\n"
haftmann@48565
   258
          |> suffix "data Typerep = Typerep String [Typerep];\n\n}\n" (* FIXME *)
haftmann@48568
   259
        val _ = map (uncurry File.write) (includes @
haftmann@48568
   260
          [(narrowing_engine_file, if contains_existentials then pnf_narrowing_engine else narrowing_engine),
haftmann@48568
   261
           (code_file, code'), (main_file, main)])
bulwahn@43114
   262
        val executable = File.shell_path (Path.append in_path (Path.basic "isabelle_quickcheck_narrowing"))
noschinl@44926
   263
        val cmd = "exec \"$ISABELLE_GHC\" " ^ Code_Haskell.language_params ^ " " ^
bulwahn@47843
   264
          ghc_options ^ " " ^
haftmann@48568
   265
          (space_implode " " (map File.shell_path (map fst includes @ [code_file, narrowing_engine_file, main_file]))) ^
bulwahn@43114
   266
          " -o " ^ executable ^ ";"
wenzelm@43850
   267
        val (result, compilation_time) =
haftmann@48568
   268
          elapsed_time "Haskell compilation" (fn () => Isabelle_System.bash cmd)
bulwahn@43585
   269
        val _ = Quickcheck.add_timing compilation_time current_result
bulwahn@45685
   270
        fun haskell_string_of_bool v = if v then "True" else "False"
wenzelm@43850
   271
        val _ = if Isabelle_System.bash cmd <> 0 then error "Compilation with GHC failed" else ()
bulwahn@45756
   272
        fun with_size genuine_only k =
bulwahn@43308
   273
          if k > size then
bulwahn@43585
   274
            (NONE, !current_result)
bulwahn@43114
   275
          else
bulwahn@43114
   276
            let
bulwahn@45765
   277
              val _ = verbose_message ("[Quickcheck-narrowing] Test data size: " ^ string_of_int k)
bulwahn@43585
   278
              val _ = current_size := k
bulwahn@43585
   279
              val ((response, _), timing) = elapsed_time ("execution of size " ^ string_of_int k)
bulwahn@45685
   280
                (fn () => Isabelle_System.bash_output
bulwahn@45760
   281
                  (executable ^ " " ^ haskell_string_of_bool genuine_only ^ " " ^ string_of_int k))
bulwahn@43585
   282
              val _ = Quickcheck.add_timing timing current_result
bulwahn@43114
   283
            in
bulwahn@43585
   284
              if response = "NONE\n" then
bulwahn@45756
   285
                with_size genuine_only (k + 1)
bulwahn@43585
   286
              else
bulwahn@43585
   287
                let
bulwahn@43585
   288
                  val output_value = the_default "NONE"
bulwahn@43585
   289
                    (try (snd o split_last o filter_out (fn s => s = "") o split_lines) response)
bulwahn@43585
   290
                    |> translate_string (fn s => if s = "\\" then "\\\\" else s)
bulwahn@43585
   291
                  val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
bulwahn@43585
   292
                    ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
bulwahn@43585
   293
                  val ctxt' = ctxt
bulwahn@43585
   294
                    |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
bulwahn@45756
   295
                    |> Context.proof_map (exec false ml_code);                 
bulwahn@45757
   296
                  val counterexample = get ctxt' ()
bulwahn@45756
   297
                in
bulwahn@45756
   298
                  if is_genuine counterexample then
bulwahn@45756
   299
                    (counterexample, !current_result)
bulwahn@45756
   300
                  else
bulwahn@45756
   301
                    let
bulwahn@45756
   302
                      val cex = Option.map (rpair []) (counterexample_of counterexample)
bulwahn@45756
   303
                    in
bulwahn@45765
   304
                      (message (Pretty.string_of (Quickcheck.pretty_counterex ctxt false cex));
bulwahn@45765
   305
                      message "Quickcheck continues to find a genuine counterexample...";
bulwahn@45756
   306
                      with_size true (k + 1))
bulwahn@45756
   307
                    end
bulwahn@45756
   308
               end
bulwahn@43585
   309
            end 
bulwahn@45756
   310
      in with_size genuine_only 0 end
bulwahn@43585
   311
  in
bulwahn@45418
   312
    with_tmp_dir tmp_prefix run
bulwahn@43585
   313
  end;
bulwahn@41905
   314
bulwahn@43308
   315
fun dynamic_value_strict opts cookie thy postproc t =
bulwahn@41905
   316
  let
bulwahn@43114
   317
    val ctxt = Proof_Context.init_global thy
bulwahn@45756
   318
    fun evaluator naming program ((_, vs_ty), t) deps =
bulwahn@45756
   319
      Exn.interruptible_capture (value opts ctxt cookie)
haftmann@48568
   320
        (Code_Target.evaluator thy target naming program deps (vs_ty, t));
bulwahn@41905
   321
  in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
bulwahn@41905
   322
bulwahn@43047
   323
(** counterexample generator **)
wenzelm@46042
   324
wenzelm@46042
   325
(* FIXME just one data slot (record) per program unit *)
bulwahn@41932
   326
structure Counterexample = Proof_Data
bulwahn@41905
   327
(
bulwahn@45725
   328
  type T = unit -> (bool * term list) option
bulwahn@41936
   329
  fun init _ () = error "Counterexample"
bulwahn@41905
   330
)
bulwahn@41905
   331
bulwahn@43237
   332
datatype counterexample = Universal_Counterexample of (term * counterexample)
bulwahn@43237
   333
  | Existential_Counterexample of (term * counterexample) list
bulwahn@43237
   334
  | Empty_Assignment
bulwahn@43237
   335
  
bulwahn@43237
   336
fun map_counterexample f Empty_Assignment = Empty_Assignment
bulwahn@43237
   337
  | map_counterexample f (Universal_Counterexample (t, c)) =
bulwahn@43237
   338
      Universal_Counterexample (f t, map_counterexample f c)
bulwahn@43237
   339
  | map_counterexample f (Existential_Counterexample cs) =
bulwahn@43237
   340
      Existential_Counterexample (map (fn (t, c) => (f t, map_counterexample f c)) cs)
bulwahn@43237
   341
wenzelm@46042
   342
(* FIXME just one data slot (record) per program unit *)
bulwahn@43237
   343
structure Existential_Counterexample = Proof_Data
bulwahn@43237
   344
(
bulwahn@43237
   345
  type T = unit -> counterexample option
bulwahn@43237
   346
  fun init _ () = error "Counterexample"
bulwahn@43237
   347
)
bulwahn@43237
   348
bulwahn@43237
   349
val put_existential_counterexample = Existential_Counterexample.put
bulwahn@43237
   350
bulwahn@42024
   351
val put_counterexample = Counterexample.put
bulwahn@42023
   352
bulwahn@43240
   353
fun finitize_functions (xTs, t) =
bulwahn@42024
   354
  let
bulwahn@43240
   355
    val (names, boundTs) = split_list xTs
bulwahn@42024
   356
    fun mk_eval_ffun dT rT =
bulwahn@42024
   357
      Const (@{const_name "Quickcheck_Narrowing.eval_ffun"}, 
bulwahn@42024
   358
        Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
bulwahn@42024
   359
    fun mk_eval_cfun dT rT =
bulwahn@42024
   360
      Const (@{const_name "Quickcheck_Narrowing.eval_cfun"}, 
bulwahn@42024
   361
        Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
bulwahn@42024
   362
    fun eval_function (T as Type (@{type_name fun}, [dT, rT])) =
bulwahn@42024
   363
      let
bulwahn@42024
   364
        val (rt', rT') = eval_function rT
bulwahn@42024
   365
      in
bulwahn@42024
   366
        case dT of
bulwahn@42024
   367
          Type (@{type_name fun}, _) =>
wenzelm@44241
   368
            (fn t => absdummy dT (rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
wenzelm@44241
   369
              Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
wenzelm@44241
   370
        | _ =>
wenzelm@44241
   371
            (fn t => absdummy dT (rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
wenzelm@44241
   372
              Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
bulwahn@42024
   373
      end
bulwahn@47330
   374
      | eval_function (T as Type (@{type_name prod}, [fT, sT])) =
bulwahn@47330
   375
        let
bulwahn@47330
   376
          val (ft', fT') = eval_function fT
bulwahn@47330
   377
          val (st', sT') = eval_function sT
bulwahn@47330
   378
          val T' = Type (@{type_name prod}, [fT', sT'])
bulwahn@47330
   379
          val map_const = Const (@{const_name map_pair}, (fT' --> fT) --> (sT' --> sT) --> T' --> T)
bulwahn@47330
   380
          fun apply_dummy T t = absdummy T (t (Bound 0))
bulwahn@47330
   381
        in
bulwahn@47330
   382
          (fn t => list_comb (map_const, [apply_dummy fT' ft', apply_dummy sT' st', t]), T')
bulwahn@47330
   383
        end
bulwahn@42024
   384
      | eval_function T = (I, T)
bulwahn@43240
   385
    val (tt, boundTs') = split_list (map eval_function boundTs)
bulwahn@43240
   386
    val t' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) boundTs), t)
bulwahn@42024
   387
  in
bulwahn@43240
   388
    (names ~~ boundTs', t')
bulwahn@42024
   389
  end
bulwahn@42023
   390
bulwahn@45039
   391
fun dest_ffun (Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT])) = (dT, rT)
bulwahn@45039
   392
bulwahn@45039
   393
fun eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Constant"}, T) $ value) =
bulwahn@45039
   394
    absdummy (fst (dest_ffun (body_type T))) (eval_finite_functions value)
bulwahn@45039
   395
  | eval_finite_functions (Const (@{const_name "Quickcheck_Narrowing.ffun.Update"}, T) $ a $ b $ f) =
bulwahn@45039
   396
    let
bulwahn@45039
   397
      val (T1, T2) = dest_ffun (body_type T)
bulwahn@45039
   398
    in
bulwahn@45039
   399
      Quickcheck_Common.mk_fun_upd T1 T2
bulwahn@45039
   400
        (eval_finite_functions a, eval_finite_functions b) (eval_finite_functions f)
bulwahn@45039
   401
    end
bulwahn@45039
   402
  | eval_finite_functions t = t
bulwahn@45039
   403
bulwahn@43114
   404
(** tester **)
bulwahn@43237
   405
bulwahn@43237
   406
val rewrs =
bulwahn@43240
   407
    map (swap o HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
bulwahn@43240
   408
      (@{thms all_simps} @ @{thms ex_simps})
bulwahn@43240
   409
    @ map (HOLogic.dest_eq o HOLogic.dest_Trueprop o Thm.prop_of)
bulwahn@46316
   410
        [@{thm iff_conv_conj_imp}, @{thm not_ex}, @{thm not_all},
bulwahn@46316
   411
         @{thm meta_eq_to_obj_eq [OF Ex1_def]}]
bulwahn@43237
   412
bulwahn@43237
   413
fun make_pnf_term thy t = Pattern.rewrite_term thy rewrs [] t
bulwahn@43237
   414
bulwahn@43237
   415
fun strip_quantifiers (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
bulwahn@43237
   416
    apfst (cons (@{const_name Ex}, (x, T))) (strip_quantifiers t)
bulwahn@43237
   417
  | strip_quantifiers (Const (@{const_name All}, _) $ Abs (x, T, t)) =
bulwahn@43237
   418
    apfst (cons (@{const_name All}, (x, T))) (strip_quantifiers t)
bulwahn@43237
   419
  | strip_quantifiers t = ([], t)
bulwahn@43237
   420
bulwahn@43237
   421
fun contains_existentials t = exists (fn (Q, _) => Q = @{const_name Ex}) (fst (strip_quantifiers t))
bulwahn@43237
   422
bulwahn@43237
   423
fun mk_property qs t =
bulwahn@43237
   424
  let
bulwahn@43237
   425
    fun enclose (@{const_name Ex}, (x, T)) t =
bulwahn@43237
   426
        Const (@{const_name Quickcheck_Narrowing.exists}, (T --> @{typ property}) --> @{typ property})
bulwahn@43237
   427
          $ Abs (x, T, t)
bulwahn@43237
   428
      | enclose (@{const_name All}, (x, T)) t =
bulwahn@43237
   429
        Const (@{const_name Quickcheck_Narrowing.all}, (T --> @{typ property}) --> @{typ property})
bulwahn@43237
   430
          $ Abs (x, T, t)
bulwahn@43237
   431
  in
bulwahn@45002
   432
    fold_rev enclose qs (@{term Quickcheck_Narrowing.Property} $ t)
bulwahn@43237
   433
  end
bulwahn@43237
   434
bulwahn@43237
   435
fun mk_case_term ctxt p ((@{const_name Ex}, (x, T)) :: qs') (Existential_Counterexample cs) =
wenzelm@45891
   436
    Datatype_Case.make_case ctxt Datatype_Case.Quiet [] (Free (x, T)) (map (fn (t, c) =>
bulwahn@43317
   437
      (t, mk_case_term ctxt (p - 1) qs' c)) cs)
bulwahn@43237
   438
  | mk_case_term ctxt p ((@{const_name All}, (x, T)) :: qs') (Universal_Counterexample (t, c)) =
bulwahn@43237
   439
    if p = 0 then t else mk_case_term ctxt (p - 1) qs' c
bulwahn@43237
   440
bulwahn@45039
   441
val post_process = (perhaps (try Quickcheck_Common.post_process_term)) o eval_finite_functions
bulwahn@45039
   442
bulwahn@43237
   443
fun mk_terms ctxt qs result =
bulwahn@43237
   444
  let
bulwahn@43237
   445
    val
bulwahn@43237
   446
      ps = filter (fn (_, (@{const_name All}, _)) => true | _ => false) (map_index I qs)
bulwahn@43237
   447
    in
bulwahn@43237
   448
      map (fn (p, (_, (x, T))) => (x, mk_case_term ctxt p qs result)) ps
bulwahn@45039
   449
      |> map (apsnd post_process)
bulwahn@43237
   450
    end
bulwahn@43237
   451
  
bulwahn@46309
   452
fun test_term ctxt catch_code_errors (t, eval_terms) =
bulwahn@41905
   453
  let
bulwahn@43585
   454
    fun dest_result (Quickcheck.Result r) = r 
bulwahn@43585
   455
    val opts =
bulwahn@45765
   456
      ((Config.get ctxt Quickcheck.genuine_only,
bulwahn@45765
   457
       (Config.get ctxt Quickcheck.quiet, Config.get ctxt Quickcheck.verbose)),
bulwahn@45685
   458
        Config.get ctxt Quickcheck.size)
wenzelm@42361
   459
    val thy = Proof_Context.theory_of ctxt
bulwahn@43237
   460
    val t' = fold_rev (fn (x, T) => fn t => HOLogic.mk_all (x, T, t)) (Term.add_frees t []) t
bulwahn@43237
   461
    val pnf_t = make_pnf_term thy t'
bulwahn@41905
   462
  in
bulwahn@43237
   463
    if Config.get ctxt allow_existentials andalso contains_existentials pnf_t then
bulwahn@43237
   464
      let
bulwahn@43240
   465
        fun wrap f (qs, t) =
bulwahn@43240
   466
          let val (qs1, qs2) = split_list qs in
bulwahn@43240
   467
          apfst (map2 pair qs1) (f (qs2, t)) end
bulwahn@43240
   468
        val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
bulwahn@43240
   469
        val (qs, prop_t) = finitize (strip_quantifiers pnf_t)
bulwahn@46309
   470
        val act = if catch_code_errors then try else (fn f => SOME o f)
bulwahn@46309
   471
        val execute = dynamic_value_strict (true, opts)
bulwahn@45756
   472
          ((K true, fn _ => error ""), (Existential_Counterexample.get, Existential_Counterexample.put,
bulwahn@45756
   473
            "Narrowing_Generators.put_existential_counterexample"))
bulwahn@46309
   474
          thy (apfst o Option.map o map_counterexample)
bulwahn@46309
   475
      in  
bulwahn@46309
   476
        case act execute (mk_property qs prop_t) of 
bulwahn@46309
   477
          SOME (counterexample, result) => Quickcheck.Result
bulwahn@46309
   478
            {counterexample = Option.map (pair true o mk_terms ctxt qs) counterexample,
bulwahn@46309
   479
            evaluation_terms = Option.map (K []) counterexample,
bulwahn@46309
   480
            timings = #timings (dest_result result), reports = #reports (dest_result result)}
bulwahn@46309
   481
        | NONE =>
bulwahn@46309
   482
          (Quickcheck.message ctxt ("Conjecture is not executable with Quickcheck-narrowing");
bulwahn@46309
   483
           Quickcheck.empty_result)
bulwahn@43237
   484
      end
bulwahn@43240
   485
    else
bulwahn@43240
   486
      let
bulwahn@45756
   487
        val frees = Term.add_frees t []
bulwahn@45756
   488
        val t' = fold_rev absfree frees t
wenzelm@46219
   489
        fun wrap f t = uncurry (fold_rev Term.abs) (f (strip_abs t))
bulwahn@43240
   490
        val finitize = if Config.get ctxt finite_functions then wrap finitize_functions else I
bulwahn@43240
   491
        fun ensure_testable t =
bulwahn@43240
   492
          Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
bulwahn@45756
   493
        fun is_genuine (SOME (true, _)) = true 
bulwahn@45756
   494
          | is_genuine _ = false
bulwahn@45756
   495
        val counterexample_of = Option.map (apsnd (curry (op ~~) (map fst frees) o map post_process))
bulwahn@46309
   496
        val act = if catch_code_errors then try else (fn f => SOME o f)
bulwahn@46309
   497
        val execute = dynamic_value_strict (false, opts)
bulwahn@45756
   498
          ((is_genuine, counterexample_of),
bulwahn@45756
   499
            (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample"))
bulwahn@46309
   500
          thy (apfst o Option.map o apsnd o map)
bulwahn@43240
   501
      in
bulwahn@46309
   502
        case act execute (ensure_testable (finitize t')) of 
bulwahn@46309
   503
          SOME (counterexample, result) =>
bulwahn@46309
   504
            Quickcheck.Result
bulwahn@46309
   505
             {counterexample = counterexample_of counterexample,
bulwahn@46309
   506
              evaluation_terms = Option.map (K []) counterexample,
bulwahn@46309
   507
              timings = #timings (dest_result result),
bulwahn@46309
   508
              reports = #reports (dest_result result)}
bulwahn@46309
   509
        | NONE =>
bulwahn@46309
   510
          (Quickcheck.message ctxt ("Conjecture is not executable with Quickcheck-narrowing");
bulwahn@46309
   511
           Quickcheck.empty_result)
bulwahn@43240
   512
      end
bulwahn@41905
   513
  end;
bulwahn@41905
   514
bulwahn@46309
   515
fun test_goals ctxt catch_code_errors insts goals =
bulwahn@43314
   516
  if (not (getenv "ISABELLE_GHC" = "")) then
bulwahn@43314
   517
    let
bulwahn@45765
   518
      val _ = Quickcheck.message ctxt ("Testing conjecture with Quickcheck-narrowing...")
bulwahn@45159
   519
      val correct_inst_goals = Quickcheck_Common.instantiate_goals ctxt insts goals
bulwahn@43314
   520
    in
bulwahn@46309
   521
      Quickcheck_Common.collect_results (test_term ctxt catch_code_errors)
bulwahn@46309
   522
        (maps (map snd) correct_inst_goals) []
bulwahn@43314
   523
    end
bulwahn@43314
   524
  else
bulwahn@43314
   525
    (if Config.get ctxt Quickcheck.quiet then () else Output.urgent_message
bulwahn@43314
   526
      ("Environment variable ISABELLE_GHC is not set. To use narrowing-based quickcheck, please set "
bulwahn@43911
   527
        ^ "this variable to your GHC Haskell compiler in your settings file. "
bulwahn@43911
   528
        ^ "To deactivate narrowing-based quickcheck, set quickcheck_narrowing_active to false.");
bulwahn@43911
   529
      [Quickcheck.empty_result])
bulwahn@43114
   530
bulwahn@43047
   531
(* setup *)
bulwahn@41905
   532
bulwahn@46587
   533
val active = Attrib.setup_config_bool @{binding quickcheck_narrowing_active} (K false);
bulwahn@43878
   534
bulwahn@41905
   535
val setup =
bulwahn@43047
   536
  Code.datatype_interpretation ensure_partial_term_of
bulwahn@43047
   537
  #> Code.datatype_interpretation ensure_partial_term_of_code
bulwahn@45923
   538
  #> Quickcheck_Common.datatype_interpretation (@{sort narrowing}, instantiate_narrowing_datatype)
bulwahn@43878
   539
  #> Context.theory_map (Quickcheck.add_tester ("narrowing", (active, test_goals)))
bulwahn@41905
   540
    
bulwahn@44272
   541
end;