src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Mon, 04 Apr 2011 14:44:11 +0200
changeset 42214 9ca13615c619
parent 42184 1d4fae76ba5e
child 42258 79cb339d8989
permissions -rw-r--r--
refactoring generator definition in quickcheck and removing clone
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
     1
(*  Title:      HOL/Tools/Quickcheck/narrowing_generators.ML
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     3
41938
645cca858c69 tuned headers;
wenzelm
parents: 41936
diff changeset
     4
Narrowing-based counterexample generation.
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     5
*)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     6
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
     7
signature NARROWING_GENERATORS =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     8
sig
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
     9
  val compile_generator_expr:
42184
1d4fae76ba5e adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents: 42159
diff changeset
    10
    Proof.context -> (term * term list) list -> int list -> term list option * Quickcheck.report option
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    11
  val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    12
  val finite_functions : bool Config.T
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    13
  val setup: theory -> theory
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    14
end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    15
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
    16
structure Narrowing_Generators : NARROWING_GENERATORS =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    17
struct
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    18
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    19
(* configurations *)
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    20
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    21
val (finite_functions, setup_finite_functions) =
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    22
  Attrib.config_bool "quickcheck_finite_functions" (K true)
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
    23
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    24
(* narrowing specific names and types *)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    25
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    26
exception FUNCTION_TYPE;
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    27
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    28
val narrowingN = "narrowing";
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    29
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    30
fun narrowingT T =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    31
  @{typ Quickcheck_Narrowing.code_int} --> Type (@{type_name Quickcheck_Narrowing.cons}, [T])
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    32
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    33
fun mk_empty T = Const (@{const_name Quickcheck_Narrowing.empty}, narrowingT T)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    34
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    35
fun mk_cons c T = Const (@{const_name Quickcheck_Narrowing.cons}, T --> narrowingT T) $ Const (c, T)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    36
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    37
fun mk_apply (T, t) (U, u) =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    38
  let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    39
    val (_, U') = dest_funT U
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    40
  in
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    41
    (U', Const (@{const_name Quickcheck_Narrowing.apply},
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    42
      narrowingT U --> narrowingT T --> narrowingT U') $ u $ t)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    43
  end
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    44
  
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    45
fun mk_sum (t, u) =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    46
  let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    47
    val T = fastype_of t
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    48
  in
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    49
    Const (@{const_name Quickcheck_Narrowing.sum}, T --> T --> T) $ t $ u
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    50
  end
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    51
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    52
(* creating narrowing instances *)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    53
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    54
fun mk_equations descr vs tycos narrowings (Ts, Us) =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    55
  let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    56
    fun mk_call T =
42214
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42184
diff changeset
    57
      (T, Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, narrowingT T))
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    58
    fun mk_aux_call fTs (k, _) (tyco, Ts) =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    59
      let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    60
        val T = Type (tyco, Ts)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    61
        val _ = if not (null fTs) then raise FUNCTION_TYPE else ()
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    62
      in
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    63
        (T, nth narrowings k)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    64
      end
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    65
    fun mk_consexpr simpleT (c, xs) =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    66
      let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    67
        val Ts = map fst xs
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    68
      in snd (fold mk_apply xs (Ts ---> simpleT, mk_cons c (Ts ---> simpleT))) end
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    69
    fun mk_rhs exprs = foldr1 mk_sum exprs
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    70
    val rhss =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    71
      Datatype_Aux.interpret_construction descr vs
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    72
        { atyp = mk_call, dtyp = mk_aux_call }
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    73
      |> (map o apfst) Type
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    74
      |> map (fn (T, cs) => map (mk_consexpr T) cs)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    75
      |> map mk_rhs
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    76
    val lhss = narrowings
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    77
    val eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (lhss ~~ rhss)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    78
  in
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    79
    eqs
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    80
  end
42214
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42184
diff changeset
    81
  
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    82
fun instantiate_narrowing_datatype config descr vs tycos prfx (names, auxnames) (Ts, Us) thy =
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    83
  let
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    84
    val _ = Datatype_Aux.message config "Creating narrowing generators ...";
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    85
    val narrowingsN = map (prefix (narrowingN ^ "_")) (names @ auxnames);
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    86
  in
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    87
    thy
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    88
    |> Class.instantiation (tycos, vs, @{sort narrowing})
42214
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42184
diff changeset
    89
    |> Quickcheck_Common.define_functions
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42184
diff changeset
    90
      (fn narrowings => mk_equations descr vs tycos narrowings (Ts, Us), NONE)
9ca13615c619 refactoring generator definition in quickcheck and removing clone
bulwahn
parents: 42184
diff changeset
    91
      prfx [] narrowingsN (map narrowingT (Ts @ Us))
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    92
    |> Class.prove_instantiation_exit (K (Class.intro_classes_tac []))
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    93
  end;
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    94
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    95
(* testing framework *)
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
    96
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
    97
val target = "Haskell"
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
    98
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    99
(* invocation of Haskell interpreter *)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   100
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   101
val narrowing_engine = File.read (Path.explode "~~/src/HOL/Tools/Quickcheck/Narrowing_Engine.hs")
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   102
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   103
fun exec verbose code =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   104
  ML_Context.exec (fn () => Secure.use_text ML_Env.local_context (0, "generated code") verbose code)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   105
42019
a9445d87bc3e adding size as static argument in quickcheck_narrowing compilation
bulwahn
parents: 41963
diff changeset
   106
fun value ctxt (get, put, put_ml) (code, value) size =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   107
  let
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   108
    val tmp_prefix = "Quickcheck_Narrowing"
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   109
    fun run in_path = 
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   110
      let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   111
        val code_file = Path.append in_path (Path.basic "Code.hs")
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   112
        val narrowing_engine_file = Path.append in_path (Path.basic "Narrowing_Engine.hs")
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   113
        val main_file = Path.append in_path (Path.basic "Main.hs")
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   114
        val main = "module Main where {\n\n" ^
41933
10f254a4e5b9 adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents: 41932
diff changeset
   115
          "import Narrowing_Engine;\n" ^
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   116
          "import Code;\n\n" ^
42090
ef566ce50170 changing Quickcheck_Narrowing's main function to enumerate the depth instead upto the depth
bulwahn
parents: 42039
diff changeset
   117
          "main = Narrowing_Engine.depthCheck " ^ string_of_int size ^ " (Code.value ())\n\n" ^
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   118
          "}\n"
41909
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   119
        val code' = prefix "module Code where {\n\ndata Typerep = Typerep String [Typerep];\n"
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   120
          (unprefix "module Code where {" code)
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   121
        val _ = File.write code_file code'
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   122
        val _ = File.write narrowing_engine_file narrowing_engine
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   123
        val _ = File.write main_file main
41946
380f7f5ff126 proper File.shell_path;
wenzelm
parents: 41940
diff changeset
   124
        val executable = File.shell_path (Path.append in_path (Path.basic "isa_lsc"))
42039
cef738d55348 another attempt to exec ISABELLE_GHC robustly (cf. d8c3b26b3da4, 994d088fbfbc);
wenzelm
parents: 42028
diff changeset
   125
        val cmd = "( exec \"$ISABELLE_GHC\" -fglasgow-exts " ^
41946
380f7f5ff126 proper File.shell_path;
wenzelm
parents: 41940
diff changeset
   126
          (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
42039
cef738d55348 another attempt to exec ISABELLE_GHC robustly (cf. d8c3b26b3da4, 994d088fbfbc);
wenzelm
parents: 42028
diff changeset
   127
          " -o " ^ executable ^ "; ) && " ^ executable
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   128
      in
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   129
        bash_output cmd
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   130
      end
41909
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   131
    val result = Isabelle_System.with_tmp_dir tmp_prefix run
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   132
    val output_value = the_default "NONE"
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   133
      (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
42020
2da02764d523 translating bash output in quickcheck_narrowing to handle special characters; adding simple test cases
bulwahn
parents: 42019
diff changeset
   134
      |> translate_string (fn s => if s = "\\" then "\\\\" else s)
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   135
    val ml_code = "\nval _ = Context.set_thread_data (SOME (Context.map_proof (" ^ put_ml
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   136
      ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   137
    val ctxt' = ctxt
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   138
      |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   139
      |> Context.proof_map (exec false ml_code);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   140
  in get ctxt' () end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   141
42019
a9445d87bc3e adding size as static argument in quickcheck_narrowing compilation
bulwahn
parents: 41963
diff changeset
   142
fun evaluation cookie thy evaluator vs_t args size =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   143
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   144
    val ctxt = ProofContext.init_global thy;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   145
    val (program_code, value_name) = evaluator vs_t;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   146
    val value_code = space_implode " "
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   147
      (value_name :: "()" :: map (enclose "(" ")") args);
42019
a9445d87bc3e adding size as static argument in quickcheck_narrowing compilation
bulwahn
parents: 41963
diff changeset
   148
  in Exn.interruptible_capture (value ctxt cookie (program_code, value_code)) size end;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   149
42019
a9445d87bc3e adding size as static argument in quickcheck_narrowing compilation
bulwahn
parents: 41963
diff changeset
   150
fun dynamic_value_strict cookie thy postproc t args size =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   151
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   152
    fun evaluator naming program ((_, vs_ty), t) deps =
42019
a9445d87bc3e adding size as static argument in quickcheck_narrowing compilation
bulwahn
parents: 41963
diff changeset
   153
      evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args size;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   154
  in Exn.release (Code_Thingol.dynamic_value thy (Exn.map_result o postproc) evaluator t) end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   155
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   156
(* counterexample generator *)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   157
  
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41930
diff changeset
   158
structure Counterexample = Proof_Data
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   159
(
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   160
  type T = unit -> term list option
41936
9792a882da9c renaming tester from lazy_exhaustive to narrowing
bulwahn
parents: 41933
diff changeset
   161
  fun init _ () = error "Counterexample"
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   162
)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   163
42024
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   164
val put_counterexample = Counterexample.put
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
   165
42024
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   166
fun finitize_functions t =
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   167
  let
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   168
    val ((names, Ts), t') = apfst split_list (strip_abs t)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   169
    fun mk_eval_ffun dT rT =
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   170
      Const (@{const_name "Quickcheck_Narrowing.eval_ffun"}, 
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   171
        Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT]) --> dT --> rT)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   172
    fun mk_eval_cfun dT rT =
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   173
      Const (@{const_name "Quickcheck_Narrowing.eval_cfun"}, 
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   174
        Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT]) --> dT --> rT)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   175
    fun eval_function (T as Type (@{type_name fun}, [dT, rT])) =
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   176
      let
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   177
        val (rt', rT') = eval_function rT
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   178
      in
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   179
        case dT of
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   180
          Type (@{type_name fun}, _) =>
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   181
            (fn t => absdummy (dT, rt' (mk_eval_cfun dT rT' $ incr_boundvars 1 t $ Bound 0)),
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   182
            Type (@{type_name "Quickcheck_Narrowing.cfun"}, [rT']))
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   183
        | _ => (fn t => absdummy (dT, rt' (mk_eval_ffun dT rT' $ incr_boundvars 1 t $ Bound 0)),
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   184
            Type (@{type_name "Quickcheck_Narrowing.ffun"}, [dT, rT']))
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   185
      end
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   186
      | eval_function T = (I, T)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   187
    val (tt, Ts') = split_list (map eval_function Ts)
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   188
    val t'' = subst_bounds (map2 (fn f => fn x => f x) (rev tt) (map_index (Bound o fst) Ts), t')
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   189
  in
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   190
    list_abs (names ~~ Ts', t'')
51df23535105 handling a quite restricted set of functions in Quickcheck_Narrowing by an easy transformation
bulwahn
parents: 42023
diff changeset
   191
  end
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
   192
42184
1d4fae76ba5e adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents: 42159
diff changeset
   193
fun compile_generator_expr ctxt [(t, eval_terms)] [_, size] =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   194
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   195
    val thy = ProofContext.theory_of ctxt
42028
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   196
    val t' = list_abs_free (Term.add_frees t [], t)
bd6515e113b2 passing a term with free variables to the quickcheck tester functions instead of an lambda expression because this is more natural with passing further evaluation terms; added output of evaluation terms; added evaluation of terms in the exhaustive testing
bulwahn
parents: 42027
diff changeset
   197
    val t'' = if Config.get ctxt finite_functions then finitize_functions t' else t'
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   198
    fun ensure_testable t =
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   199
      Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
   200
    val result = dynamic_value_strict
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41930
diff changeset
   201
      (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
42184
1d4fae76ba5e adapting Quickcheck_Narrowing (overseen in 234ec7011e5d); commenting out some examples temporarily
bulwahn
parents: 42159
diff changeset
   202
      thy (Option.map o map) (ensure_testable t'') [] size
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   203
  in
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
   204
    (result, NONE)
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   205
  end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   206
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   207
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   208
val setup =
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
   209
  Datatype.interpretation
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
   210
    (Quickcheck_Common.ensure_sort_datatype (@{sort narrowing}, instantiate_narrowing_datatype))
42023
1bd4b6d1c482 adding minimalistic setup and transformation to handle functions as data to enable naive function generation for Quickcheck_Narrowing
bulwahn
parents: 42020
diff changeset
   211
  #> setup_finite_functions
41963
d8c3b26b3da4 tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
bulwahn
parents: 41953
diff changeset
   212
  #> Context.theory_map
41936
9792a882da9c renaming tester from lazy_exhaustive to narrowing
bulwahn
parents: 41933
diff changeset
   213
    (Quickcheck.add_generator ("narrowing", compile_generator_expr))
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   214
    
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   215
end;