src/HOL/Tools/Quickcheck/narrowing_generators.ML
author bulwahn
Mon, 14 Mar 2011 12:34:10 +0100
changeset 41963 d8c3b26b3da4
parent 41953 994d088fbfbc
child 42019 a9445d87bc3e
permissions -rw-r--r--
tuned exhaustive generator compilation; added narrowing generator compilation; removed exec as does not work properly here (reverting changeset 994d088fbfbc)
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:
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    10
    Proof.context -> term -> int -> term list option * Quickcheck.report option
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    11
  val put_counterexample: (unit -> term list option) -> Proof.context -> Proof.context
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    12
  val setup: theory -> theory
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    13
end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    14
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
    15
structure Narrowing_Generators : NARROWING_GENERATORS =
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    16
struct
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
    17
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
    18
fun mk_undefined T = Const(@{const_name undefined}, 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
    19
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
    20
(* 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
    21
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
    22
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
    23
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
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
    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
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
    27
  @{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
    28
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
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
    30
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
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
    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_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
    34
  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
    35
    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
    36
  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
    37
    (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
    38
      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
    39
  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
    40
  
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
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
    42
  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
    43
    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
    44
  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
    45
    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
    46
  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
    47
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
(* 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
    49
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
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
    51
  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
    52
    fun mk_call 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
    53
      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
    54
        val 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
    55
          Const (@{const_name "Quickcheck_Narrowing.narrowing_class.narrowing"}, 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
    56
      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
    57
        (T, 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
    58
      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
    59
    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
    60
      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
    61
        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
    62
        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
    63
      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
    64
        (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
    65
      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
    66
    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
    67
      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
    68
        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
    69
      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
    70
    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
    71
    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
    72
      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
    73
        { 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
    74
      |> (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
    75
      |> 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
    76
      |> 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
    77
    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
    78
    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
    79
  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
    80
    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
    81
  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
    82
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
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
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
    85
  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
    86
    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
    87
    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
    88
  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
    89
    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
    90
    |> Class.instantiation (tycos, vs, @{sort 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
    91
    |> (fold_map (fn (name, T) => Local_Theory.define
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
          ((Binding.conceal (Binding.name name), NoSyn),
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
            (apfst Binding.conceal Attrib.empty_binding, mk_undefined (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
    94
        #> apfst fst) (narrowingsN ~~ (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
    95
      #> (fn (narrowings, lthy) =>
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
        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
    97
          val eqs_t = 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
    98
          val eqs = map (fn eq => Goal.prove lthy ["f", "i"] [] eq
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
    99
            (fn _ => Skip_Proof.cheat_tac (ProofContext.theory_of lthy))) eqs_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
   100
        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
   101
          fold (fn (name, eq) => Local_Theory.note
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
   102
          ((Binding.conceal (Binding.qualify true prfx
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
   103
             (Binding.qualify true name (Binding.name "simps"))),
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
   104
             Code.add_default_eqn_attrib :: map (Attrib.internal o 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
   105
               [Simplifier.simp_add, Nitpick_Simps.add]), [eq]) #> snd) (narrowingsN ~~ eqs) lthy
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
   106
        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
   107
    |> 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
   108
  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
   109
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
   110
(* 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
   111
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   112
val target = "Haskell"
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   113
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   114
(* invocation of Haskell interpreter *)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   115
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   116
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
   117
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   118
fun exec verbose code =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   119
  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
   120
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   121
fun value ctxt (get, put, put_ml) (code, value) =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   122
  let
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   123
    val tmp_prefix = "Quickcheck_Narrowing"
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   124
    fun run in_path = 
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   125
      let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   126
        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
   127
        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
   128
        val main_file = Path.append in_path (Path.basic "Main.hs")
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   129
        val main = "module Main where {\n\n" ^
41933
10f254a4e5b9 adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents: 41932
diff changeset
   130
          "import Narrowing_Engine;\n" ^
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   131
          "import Code;\n\n" ^
41933
10f254a4e5b9 adapting Main file generation for Quickcheck_Narrowing
bulwahn
parents: 41932
diff changeset
   132
          "main = Narrowing_Engine.smallCheck 7 (Code.value ())\n\n" ^
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   133
          "}\n"
41909
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   134
        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
   135
          (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
   136
        val _ = File.write code_file code'
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   137
        val _ = File.write narrowing_engine_file narrowing_engine
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   138
        val _ = File.write main_file main
41946
380f7f5ff126 proper File.shell_path;
wenzelm
parents: 41940
diff changeset
   139
        val executable = File.shell_path (Path.append in_path (Path.basic "isa_lsc"))
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
   140
        val cmd = "\"$ISABELLE_GHC\" -fglasgow-exts " ^
41946
380f7f5ff126 proper File.shell_path;
wenzelm
parents: 41940
diff changeset
   141
          (space_implode " " (map File.shell_path [code_file, narrowing_engine_file, main_file])) ^
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   142
          " -o " ^ executable ^ " && " ^ executable
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
   143
          (* FIXME: should use bash command exec but does not work with && *) 
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   144
      in
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   145
        bash_output cmd
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   146
      end
41909
383bbdad1650 replacing strings in generated Code resolves the changing names of Typerep in lazysmallcheck prototype
bulwahn
parents: 41908
diff changeset
   147
    val result = Isabelle_System.with_tmp_dir tmp_prefix run
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   148
    val output_value = the_default "NONE"
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   149
      (try (snd o split_last o filter_out (fn s => s = "") o split_lines o fst) result)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   150
    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
   151
      ^ " (fn () => " ^ output_value ^ ")) (ML_Context.the_generic_context ())))";
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   152
    val ctxt' = ctxt
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   153
      |> put (fn () => error ("Bad evaluation for " ^ quote put_ml))
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   154
      |> Context.proof_map (exec false ml_code);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   155
  in get ctxt' () end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   156
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   157
fun evaluation cookie thy evaluator vs_t args =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   158
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   159
    val ctxt = ProofContext.init_global thy;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   160
    val (program_code, value_name) = evaluator vs_t;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   161
    val value_code = space_implode " "
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   162
      (value_name :: "()" :: map (enclose "(" ")") args);
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   163
  in Exn.interruptible_capture (value ctxt cookie) (program_code, value_code) end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   164
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   165
fun dynamic_value_strict cookie thy postproc t args =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   166
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   167
    fun evaluator naming program ((_, vs_ty), t) deps =
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   168
      evaluation cookie thy (Code_Target.evaluator thy target naming program deps) (vs_ty, t) args;
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   169
  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
   170
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   171
(* counterexample generator *)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   172
  
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41930
diff changeset
   173
structure Counterexample = Proof_Data
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   174
(
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   175
  type T = unit -> term list option
41936
9792a882da9c renaming tester from lazy_exhaustive to narrowing
bulwahn
parents: 41933
diff changeset
   176
  fun init _ () = error "Counterexample"
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   177
)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   178
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41930
diff changeset
   179
val put_counterexample =  Counterexample.put
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   180
  
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   181
fun compile_generator_expr ctxt t size =
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   182
  let
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   183
    val thy = ProofContext.theory_of ctxt
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   184
    fun ensure_testable t =
41930
1e008cc4883a renaming lazysmallcheck ML file to Quickcheck_Narrowing
bulwahn
parents: 41925
diff changeset
   185
      Const (@{const_name Quickcheck_Narrowing.ensure_testable}, fastype_of t --> fastype_of t) $ t
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   186
    val t = dynamic_value_strict
41932
e8f113ce8a94 adapting Quickcheck_Narrowing and example file to new names
bulwahn
parents: 41930
diff changeset
   187
      (Counterexample.get, Counterexample.put, "Narrowing_Generators.put_counterexample")
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   188
      thy (Option.map o map) (ensure_testable t) []
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   189
  in
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   190
    (t, NONE)
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   191
  end;
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   192
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   193
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   194
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
   195
  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
   196
    (Quickcheck_Common.ensure_sort_datatype (@{sort narrowing}, instantiate_narrowing_datatype))
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
   197
  #> Context.theory_map
41936
9792a882da9c renaming tester from lazy_exhaustive to narrowing
bulwahn
parents: 41933
diff changeset
   198
    (Quickcheck.add_generator ("narrowing", compile_generator_expr))
41905
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   199
    
e2611bc96022 adding files for prototype of lazysmallcheck
bulwahn
parents:
diff changeset
   200
end;