src/HOL/Tools/Quickcheck/abstract_generators.ML
author wenzelm
Sun, 09 Mar 2014 16:37:56 +0100
changeset 56002 2028467b4df4
parent 55952 2f85cc6c27d4
child 58112 8081087096ad
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Quickcheck/abstract_generators.ML
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     2
    Author:     Lukas Bulwahn, TU Muenchen
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     3
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     4
Quickcheck generators for abstract types.
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     5
*)
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     6
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     7
signature ABSTRACT_GENERATORS =
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
     8
sig
46564
daa915508f63 adding parsing of an optional predicate with quickcheck_generator command
bulwahn
parents: 45940
diff changeset
     9
  val quickcheck_generator : string -> term option -> term list -> theory -> theory
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    10
end;
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    11
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    12
structure Abstract_Generators : ABSTRACT_GENERATORS =
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    13
struct
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    14
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    15
fun check_constrs ctxt tyco constrs =
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    16
  let
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    17
    fun check_type c =
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    18
      case try (dest_Type o body_type o fastype_of) c of
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    19
        NONE => error (Syntax.string_of_term ctxt c ^ " has mismatching result type.")
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    20
      | SOME (tyco', vs) => if not (tyco' = tyco) then
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    21
            error (Syntax.string_of_term ctxt c ^ " has mismatching result type. " ^
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    22
              "Expected type constructor " ^ quote tyco ^ ", but found " ^ quote tyco' ^ ".")
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    23
          else
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    24
            case try (map dest_TFree) vs of
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    25
              NONE => error (Syntax.string_of_term ctxt c ^ " has mismatching result type.")
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    26
            | SOME _ => ()
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    27
  in
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    28
    map check_type constrs
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    29
  end
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    30
  
46564
daa915508f63 adding parsing of an optional predicate with quickcheck_generator command
bulwahn
parents: 45940
diff changeset
    31
fun gen_quickcheck_generator prep_tyco prep_term tyco_raw opt_pred_raw constrs_raw thy =
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    32
  let
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    33
    val ctxt = Proof_Context.init_global thy
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    34
    val tyco = prep_tyco ctxt tyco_raw
46565
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46564
diff changeset
    35
    val opt_pred = Option.map (prep_term ctxt) opt_pred_raw
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    36
    val constrs = map (prep_term ctxt) constrs_raw
46565
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46564
diff changeset
    37
    val _ = check_constrs ctxt tyco constrs
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    38
    val vs = map dest_TFree (snd (dest_Type (body_type (fastype_of (hd constrs)))))
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    39
    val name = Long_Name.base_name tyco 
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    40
    fun mk_dconstrs (Const (s, T)) =
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    41
        (s, map (Datatype_Aux.dtyp_of_typ [(tyco, vs)]) (binder_types T))
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    42
      | mk_dconstrs t = error (Syntax.string_of_term ctxt t ^
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    43
          " is not a valid constructor for quickcheck_generator, which expects a constant.")
46564
daa915508f63 adding parsing of an optional predicate with quickcheck_generator command
bulwahn
parents: 45940
diff changeset
    44
    fun the_descr _ _ =
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    45
      let
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    46
        val descr = [(0, (tyco, map Datatype_Aux.DtTFree vs, map mk_dconstrs constrs))]
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    47
      in
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    48
        (descr, vs, [tyco], name, ([name], []), ([Type (tyco, map TFree vs)], []))
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    49
      end
45940
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    50
    fun ensure_sort (sort, instantiate) =
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    51
      Quickcheck_Common.ensure_sort (((@{sort typerep}, @{sort term_of}), sort),
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    52
        (the_descr, instantiate))
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    53
      Datatype_Aux.default_config [tyco]
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    54
  in
45940
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    55
    thy
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    56
    |> ensure_sort (@{sort full_exhaustive}, Exhaustive_Generators.instantiate_full_exhaustive_datatype)
46565
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46564
diff changeset
    57
    |> ensure_sort (@{sort exhaustive}, Exhaustive_Generators.instantiate_exhaustive_datatype)
45940
71970a26a269 quickcheck_generator command also creates random generators
bulwahn
parents: 45925
diff changeset
    58
    |> ensure_sort (@{sort random}, Random_Generators.instantiate_random_datatype)
46565
ad21900e0ee9 subtype preprocessing in Quickcheck;
bulwahn
parents: 46564
diff changeset
    59
    |> (case opt_pred of NONE => I | SOME t => Context.theory_map (Quickcheck_Common.register_predicate (t, tyco)))
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    60
  end
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    61
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    62
val quickcheck_generator = gen_quickcheck_generator (K I) (K I)
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    63
  
55951
c07d184aebe9 tuned signature -- more uniform check_type_name/read_type_name;
wenzelm
parents: 46961
diff changeset
    64
val quickcheck_generator_cmd =
c07d184aebe9 tuned signature -- more uniform check_type_name/read_type_name;
wenzelm
parents: 46961
diff changeset
    65
  gen_quickcheck_generator
56002
2028467b4df4 tuned signature;
wenzelm
parents: 55952
diff changeset
    66
    ((#1 o dest_Type) oo Proof_Context.read_type_name {proper = true, strict = true})
55951
c07d184aebe9 tuned signature -- more uniform check_type_name/read_type_name;
wenzelm
parents: 46961
diff changeset
    67
    Syntax.read_term
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    68
  
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46949
diff changeset
    69
val _ =
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46949
diff changeset
    70
  Outer_Syntax.command @{command_spec "quickcheck_generator"}
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46949
diff changeset
    71
    "define quickcheck generators for abstract types"
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 46949
diff changeset
    72
    ((Parse.type_const --
46949
94aa7b81bcf6 prefer formally checked @{keyword} parser;
wenzelm
parents: 46565
diff changeset
    73
      Scan.option (Args.$$$ "predicate" |-- @{keyword ":"} |-- Parse.term)) --
94aa7b81bcf6 prefer formally checked @{keyword} parser;
wenzelm
parents: 46565
diff changeset
    74
      (Args.$$$ "constructors" |-- @{keyword ":"} |-- Parse.list1 Parse.term)
46564
daa915508f63 adding parsing of an optional predicate with quickcheck_generator command
bulwahn
parents: 45940
diff changeset
    75
      >> (fn ((tyco, opt_pred), constrs) =>
daa915508f63 adding parsing of an optional predicate with quickcheck_generator command
bulwahn
parents: 45940
diff changeset
    76
        Toplevel.theory (quickcheck_generator_cmd tyco opt_pred constrs)))
45925
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    77
cd4243c025bb quickcheck generators for abstract types; tuned
bulwahn
parents:
diff changeset
    78
end;