src/HOL/Tools/Predicate_Compile/predicate_compile_core.ML
author bulwahn
Sat, 24 Oct 2009 16:55:42 +0200
changeset 33115 f765c3234059
parent 33114 4785ef554dcc
child 33116 b379ee2cddb1
permissions -rw-r--r--
changed proof method to handle widen predicate in JinjaThreads
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     2
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     3
(Prototype of) A compiler from predicates specified by intro/elim rules
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     4
to equations.
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     5
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     6
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     7
signature PREDICATE_COMPILE_CORE =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
     8
sig
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
     9
  val setup: theory -> theory
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
    10
  val code_pred: int list list option -> bool -> string -> Proof.context -> Proof.state
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
    11
  val code_pred_cmd: int list list option -> bool -> string -> Proof.context -> Proof.state
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    12
  type smode = (int * int list option) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    13
  type mode = smode option list * smode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    14
  datatype tmode = Mode of mode * smode * tmode option list;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    15
  (*val add_equations_of: bool -> string list -> theory -> theory *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    16
  val register_predicate : (thm list * thm * int) -> theory -> theory
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
    17
  val register_intros : thm list -> theory -> theory
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    18
  val is_registered : theory -> string -> bool
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    19
 (* val fetch_pred_data : theory -> string -> (thm list * thm * int)  *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    20
  val predfun_intro_of: theory -> string -> mode -> thm
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    21
  val predfun_elim_of: theory -> string -> mode -> thm
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    22
  val strip_intro_concl: int -> term -> term * (term list * term list)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    23
  val predfun_name_of: theory -> string -> mode -> string
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    24
  val all_preds_of : theory -> string list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    25
  val modes_of: theory -> string -> mode list
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
    26
  val sizelim_modes_of: theory -> string -> mode list
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
    27
  val sizelim_function_name_of : theory -> string -> mode -> string
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
    28
  val generator_modes_of: theory -> string -> mode list
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
    29
  val generator_name_of : theory -> string -> mode -> string
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    30
  val string_of_mode : mode -> string
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    31
  val intros_of: theory -> string -> thm list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    32
  val nparams_of: theory -> string -> int
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    33
  val add_intro: thm -> theory -> theory
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    34
  val set_elim: thm -> theory -> theory
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
    35
  val set_nparams : string -> int -> theory -> theory
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    36
  val print_stored_rules: theory -> unit
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    37
  val print_all_modes: theory -> unit
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32674
diff changeset
    38
  val do_proofs: bool Unsynchronized.ref
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    39
  val mk_casesrule : Proof.context -> int -> thm list -> term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    40
  val analyze_compr: theory -> term -> term
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32674
diff changeset
    41
  val eval_ref: (unit -> term Predicate.pred) option Unsynchronized.ref
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
    42
  val add_equations : int list list option -> string list -> theory -> theory
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    43
  val code_pred_intros_attrib : attribute
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    44
  (* used by Quickcheck_Generator *) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    45
  (*val funT_of : mode -> typ -> typ
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    46
  val mk_if_pred : term -> term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    47
  val mk_Eval : term * term -> term*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    48
  val mk_tupleT : typ list -> typ
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    49
(*  val mk_predT :  typ -> typ *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    50
  (* temporary for testing of the compilation *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    51
  datatype indprem = Prem of term list * term | Negprem of term list * term | Sidecond of term |
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    52
    GeneratorPrem of term list * term | Generator of (string * typ);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    53
 (* val prepare_intrs: theory -> string list ->
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    54
    (string * typ) list * int * string list * string list * (string * mode list) list *
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    55
    (string * (term list * indprem list) list) list * (string * (int option list * int)) list*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    56
  datatype compilation_funs = CompilationFuns of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    57
    mk_predT : typ -> typ,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    58
    dest_predT : typ -> typ,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    59
    mk_bot : typ -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    60
    mk_single : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    61
    mk_bind : term * term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    62
    mk_sup : term * term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    63
    mk_if : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    64
    mk_not : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    65
    mk_map : typ -> typ -> term -> term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    66
    lift_pred : term -> term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    67
  };  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    68
  type moded_clause = term list * (indprem * tmode) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    69
  type 'a pred_mode_table = (string * (mode * 'a) list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    70
  val infer_modes : theory -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    71
    -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    72
    -> string list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    73
    -> (string * (term list * indprem list) list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    74
    -> (moded_clause list) pred_mode_table
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    75
  val infer_modes_with_generator : theory -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    76
    -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    77
    -> string list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    78
    -> (string * (term list * indprem list) list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    79
    -> (moded_clause list) pred_mode_table  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    80
  (*val compile_preds : theory -> compilation_funs -> string list -> string list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    81
    -> (string * typ) list -> (moded_clause list) pred_mode_table -> term pred_mode_table
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    82
  val rpred_create_definitions :(string * typ) list -> string * mode list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    83
    -> theory -> theory 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    84
  val split_smode : int list -> term list -> (term list * term list) *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    85
  val print_moded_clauses :
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    86
    theory -> (moded_clause list) pred_mode_table -> unit
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    87
  val print_compiled_terms : theory -> term pred_mode_table -> unit
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    88
  (*val rpred_prove_preds : theory -> term pred_mode_table -> thm pred_mode_table*)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
    89
  val pred_compfuns : compilation_funs
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    90
  val rpred_compfuns : compilation_funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    91
  val dest_funT : typ -> typ * typ
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    92
 (* val depending_preds_of : theory -> thm list -> string list *)
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
    93
  val add_quickcheck_equations : int list list option -> string list -> theory -> theory
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
    94
  val add_sizelim_equations : int list list option -> string list -> theory -> theory
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    95
  val is_inductive_predicate : theory -> string -> bool
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    96
  val terms_vs : term list -> string list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    97
  val subsets : int -> int -> int list list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    98
  val check_mode_clause : bool -> theory -> string list ->
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
    99
    (string * mode list) list -> (string * mode list) list -> mode -> (term list * indprem list)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   100
      -> (term list * (indprem * tmode) list) option
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   101
  val string_of_moded_prem : theory -> (indprem * tmode) -> string
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   102
  val all_modes_of : theory -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   103
  val all_generator_modes_of : theory -> (string * mode list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   104
  val compile_clause : compilation_funs -> term option -> (term list -> term) ->
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   105
    theory -> string list -> string list -> mode -> term -> moded_clause -> term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   106
  val preprocess_intro : theory -> thm -> thm
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   107
  val is_constrt : theory -> term -> bool
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   108
  val is_predT : typ -> bool
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   109
  val guess_nparams : typ -> int
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   110
  val cprods_subset : 'a list list -> 'a list list
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
   111
  val dest_prem : theory -> term list ->  term -> indprem
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   112
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   113
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   114
structure Predicate_Compile_Core : PREDICATE_COMPILE_CORE =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   115
struct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   116
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   117
open Predicate_Compile_Aux;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   118
(** auxiliary **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   119
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   120
(* debug stuff *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   121
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   122
fun tracing s = (if ! Toplevel.debug then Output.tracing s else ());
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   123
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   124
fun print_tac s = Seq.single;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   125
fun debug_tac msg = Seq.single; (* (fn st => (Output.tracing msg; Seq.single st)); *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   126
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32674
diff changeset
   127
val do_proofs = Unsynchronized.ref true;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   128
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   129
datatype assertion = Max_number_of_subgoals of int
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   130
fun assert_tac (Max_number_of_subgoals i) st =
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   131
  if (nprems_of st <= i) then Seq.single st
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   132
  else error ("assert_tac: Numbers of subgoals mismatch at goal state :"
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   133
    ^ "\n" ^ Pretty.string_of (Pretty.chunks
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   134
      (Goal_Display.pretty_goals_without_context (! Goal_Display.goals_limit) st)));
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
   135
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   136
(* reference to preprocessing of InductiveSet package *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   137
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   138
val ind_set_codegen_preproc = (fn thy => I) (*Inductive_Set.codegen_preproc;*)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   139
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   140
(** fundamentals **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   141
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   142
(* syntactic operations *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   143
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   144
fun mk_eq (x, xs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   145
  let fun mk_eqs _ [] = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   146
        | mk_eqs a (b::cs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   147
            HOLogic.mk_eq (Free (a, fastype_of b), b) :: mk_eqs a cs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   148
  in mk_eqs x xs end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   149
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   150
fun mk_tupleT [] = HOLogic.unitT
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   151
  | mk_tupleT Ts = foldr1 HOLogic.mk_prodT Ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   152
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   153
fun dest_tupleT (Type (@{type_name Product_Type.unit}, [])) = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   154
  | dest_tupleT (Type (@{type_name "*"}, [T1, T2])) = T1 :: (dest_tupleT T2)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   155
  | dest_tupleT t = [t]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   156
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   157
fun mk_tuple [] = HOLogic.unit
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   158
  | mk_tuple ts = foldr1 HOLogic.mk_prod ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   159
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   160
fun dest_tuple (Const (@{const_name Product_Type.Unity}, _)) = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   161
  | dest_tuple (Const (@{const_name Pair}, _) $ t1 $ t2) = t1 :: (dest_tuple t2)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   162
  | dest_tuple t = [t]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   163
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   164
fun mk_scomp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   165
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   166
    val T = fastype_of t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   167
    val U = fastype_of u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   168
    val [A] = binder_types T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   169
    val D = body_type U 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   170
  in 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   171
    Const (@{const_name "scomp"}, T --> U --> A --> D) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   172
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   173
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   174
fun dest_funT (Type ("fun",[S, T])) = (S, T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   175
  | dest_funT T = raise TYPE ("dest_funT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   176
 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   177
fun mk_fun_comp (t, u) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   178
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   179
    val (_, B) = dest_funT (fastype_of t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   180
    val (C, A) = dest_funT (fastype_of u)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   181
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   182
    Const(@{const_name "Fun.comp"}, (A --> B) --> (C --> A) --> C --> B) $ t $ u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   183
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   184
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   185
fun dest_randomT (Type ("fun", [@{typ Random.seed},
32674
b629fbcc5313 merged; adopted to changes from Code_Evaluation in the predicate compiler
bulwahn
parents: 32673
diff changeset
   186
  Type ("*", [Type ("*", [T, @{typ "unit => Code_Evaluation.term"}]) ,@{typ Random.seed}])])) = T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   187
  | dest_randomT T = raise TYPE ("dest_randomT", [T], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   188
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   189
(* destruction of intro rules *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   190
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   191
(* FIXME: look for other place where this functionality was used before *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   192
fun strip_intro_concl nparams intro = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   193
  val _ $ u = Logic.strip_imp_concl intro
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   194
  val (pred, all_args) = strip_comb u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   195
  val (params, args) = chop nparams all_args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   196
in (pred, (params, args)) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   197
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   198
(** data structures **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   199
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   200
(* new datatype for modes: *)
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   201
(*
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   202
datatype instantiation = Input | Output
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   203
type arg_mode = Tuple of instantiation list | Atom of instantiation | HigherOrderMode of mode
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   204
type mode = arg_mode list
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   205
type tmode = Mode of mode * 
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
   206
*)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   207
type smode = (int * int list option) list
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   208
type mode = smode option list * smode;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   209
datatype tmode = Mode of mode * smode * tmode option list;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   210
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   211
fun gen_split_smode (mk_tuple, strip_tuple) smode ts =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   212
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   213
    fun split_tuple' _ _ [] = ([], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   214
    | split_tuple' is i (t::ts) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   215
      (if i mem is then apfst else apsnd) (cons t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   216
        (split_tuple' is (i+1) ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   217
    fun split_tuple is t = split_tuple' is 1 (strip_tuple t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   218
    fun split_smode' _ _ [] = ([], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   219
      | split_smode' smode i (t::ts) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   220
        (if i mem (map fst smode) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   221
          case (the (AList.lookup (op =) smode i)) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   222
            NONE => apfst (cons t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   223
            | SOME is =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   224
              let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   225
                val (ts1, ts2) = split_tuple is t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   226
                fun cons_tuple ts = if null ts then I else cons (mk_tuple ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   227
                in (apfst (cons_tuple ts1)) o (apsnd (cons_tuple ts2)) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   228
          else apsnd (cons t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   229
        (split_smode' smode (i+1) ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   230
  in split_smode' smode 1 ts end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   231
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   232
val split_smode = gen_split_smode (HOLogic.mk_tuple, HOLogic.strip_tuple)   
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   233
val split_smodeT = gen_split_smode (HOLogic.mk_tupleT, HOLogic.strip_tupleT)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   234
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   235
fun gen_split_mode split_smode (iss, is) ts =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   236
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   237
    val (t1, t2) = chop (length iss) ts 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   238
  in (t1, split_smode is t2) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   239
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   240
val split_mode = gen_split_mode split_smode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   241
val split_modeT = gen_split_mode split_smodeT
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   242
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   243
fun string_of_smode js =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   244
    commas (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   245
      (fn (i, is) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   246
        string_of_int i ^ (case is of NONE => ""
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   247
    | SOME is => "p" ^ enclose "[" "]" (commas (map string_of_int is)))) js)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   248
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   249
fun string_of_mode (iss, is) = space_implode " -> " (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   250
  (fn NONE => "X"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   251
    | SOME js => enclose "[" "]" (string_of_smode js))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   252
       (iss @ [SOME is]));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   253
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   254
fun string_of_tmode (Mode (predmode, termmode, param_modes)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   255
  "predmode: " ^ (string_of_mode predmode) ^ 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   256
  (if null param_modes then "" else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   257
    "; " ^ "params: " ^ commas (map (the_default "NONE" o Option.map string_of_tmode) param_modes))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   258
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   259
(* generation of case rules from user-given introduction rules *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   260
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   261
fun mk_casesrule ctxt nparams introrules =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   262
  let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   263
    val ((_, intros_th), ctxt1) = Variable.import false introrules ctxt
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   264
    val intros = map prop_of intros_th
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   265
    val (pred, (params, args)) = strip_intro_concl nparams (hd intros)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   266
    val ([propname], ctxt2) = Variable.variant_fixes ["thesis"] ctxt1
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   267
    val prop = HOLogic.mk_Trueprop (Free (propname, HOLogic.boolT))
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   268
    val (argnames, ctxt3) = Variable.variant_fixes
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   269
      (map (fn i => "a" ^ string_of_int i) (1 upto (length args))) ctxt2
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   270
    val argvs = map2 (curry Free) argnames (map fastype_of args)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   271
    fun mk_case intro =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   272
      let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   273
        val (_, (_, args)) = strip_intro_concl nparams intro
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   274
        val prems = Logic.strip_imp_prems intro
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   275
        val eqprems = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (argvs ~~ args)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   276
        val frees = (fold o fold_aterms)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   277
          (fn t as Free _ =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   278
              if member (op aconv) params t then I else insert (op aconv) t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   279
           | _ => I) (args @ prems) []
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   280
      in fold Logic.all frees (Logic.list_implies (eqprems @ prems, prop)) end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   281
    val assm = HOLogic.mk_Trueprop (list_comb (pred, params @ argvs))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   282
    val cases = map mk_case intros
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   283
  in Logic.list_implies (assm :: cases, prop) end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   284
    
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   285
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   286
datatype indprem = Prem of term list * term | Negprem of term list * term | Sidecond of term |
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   287
  GeneratorPrem of term list * term | Generator of (string * typ);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   288
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   289
type moded_clause = term list * (indprem * tmode) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   290
type 'a pred_mode_table = (string * (mode * 'a) list) list
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   291
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   292
datatype predfun_data = PredfunData of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   293
  name : string,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   294
  definition : thm,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   295
  intro : thm,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   296
  elim : thm
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   297
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   298
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   299
fun rep_predfun_data (PredfunData data) = data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   300
fun mk_predfun_data (name, definition, intro, elim) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   301
  PredfunData {name = name, definition = definition, intro = intro, elim = elim}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   302
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   303
datatype function_data = FunctionData of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   304
  name : string,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   305
  equation : thm option (* is not used at all? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   306
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   307
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   308
fun rep_function_data (FunctionData data) = data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   309
fun mk_function_data (name, equation) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   310
  FunctionData {name = name, equation = equation}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   311
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   312
datatype pred_data = PredData of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   313
  intros : thm list,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   314
  elim : thm option,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   315
  nparams : int,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   316
  functions : (mode * predfun_data) list,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   317
  generators : (mode * function_data) list,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   318
  sizelim_functions : (mode * function_data) list 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   319
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   320
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   321
fun rep_pred_data (PredData data) = data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   322
fun mk_pred_data ((intros, elim, nparams), (functions, generators, sizelim_functions)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   323
  PredData {intros = intros, elim = elim, nparams = nparams,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   324
    functions = functions, generators = generators, sizelim_functions = sizelim_functions}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   325
fun map_pred_data f (PredData {intros, elim, nparams, functions, generators, sizelim_functions}) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   326
  mk_pred_data (f ((intros, elim, nparams), (functions, generators, sizelim_functions)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   327
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   328
fun eq_option eq (NONE, NONE) = true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   329
  | eq_option eq (SOME x, SOME y) = eq (x, y)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   330
  | eq_option eq _ = false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   331
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   332
fun eq_pred_data (PredData d1, PredData d2) = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   333
  eq_list (Thm.eq_thm) (#intros d1, #intros d2) andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   334
  eq_option (Thm.eq_thm) (#elim d1, #elim d2) andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   335
  #nparams d1 = #nparams d2
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   336
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   337
structure PredData = TheoryDataFun
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   338
(
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   339
  type T = pred_data Graph.T;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   340
  val empty = Graph.empty;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   341
  val copy = I;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   342
  val extend = I;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   343
  fun merge _ = Graph.merge eq_pred_data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   344
);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   345
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   346
(* queries *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   347
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   348
fun lookup_pred_data thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   349
  Option.map rep_pred_data (try (Graph.get_node (PredData.get thy)) name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   350
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   351
fun the_pred_data thy name = case lookup_pred_data thy name
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   352
 of NONE => error ("No such predicate " ^ quote name)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   353
  | SOME data => data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   354
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   355
val is_registered = is_some oo lookup_pred_data 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   356
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   357
val all_preds_of = Graph.keys o PredData.get
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   358
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   359
fun intros_of thy = map (Thm.transfer thy) o #intros o the_pred_data thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   360
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   361
fun the_elim_of thy name = case #elim (the_pred_data thy name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   362
 of NONE => error ("No elimination rule for predicate " ^ quote name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   363
  | SOME thm => Thm.transfer thy thm 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   364
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   365
val has_elim = is_some o #elim oo the_pred_data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   366
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   367
val nparams_of = #nparams oo the_pred_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   368
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   369
val modes_of = (map fst) o #functions oo the_pred_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   370
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   371
val sizelim_modes_of = (map fst) o #sizelim_functions oo the_pred_data
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   372
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   373
val rpred_modes_of = (map fst) o #generators oo the_pred_data
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   374
  
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   375
fun all_modes_of thy = map (fn name => (name, modes_of thy name)) (all_preds_of thy) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   376
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   377
val is_compiled = not o null o #functions oo the_pred_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   378
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   379
fun lookup_predfun_data thy name mode =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   380
  Option.map rep_predfun_data (AList.lookup (op =)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   381
  (#functions (the_pred_data thy name)) mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   382
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   383
fun the_predfun_data thy name mode = case lookup_predfun_data thy name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   384
  of NONE => error ("No function defined for mode " ^ string_of_mode mode ^ " of predicate " ^ name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   385
   | SOME data => data;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   386
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   387
val predfun_name_of = #name ooo the_predfun_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   388
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   389
val predfun_definition_of = #definition ooo the_predfun_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   390
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   391
val predfun_intro_of = #intro ooo the_predfun_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   392
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   393
val predfun_elim_of = #elim ooo the_predfun_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   394
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   395
fun lookup_generator_data thy name mode = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   396
  Option.map rep_function_data (AList.lookup (op =)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   397
  (#generators (the_pred_data thy name)) mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   398
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   399
fun the_generator_data thy name mode = case lookup_generator_data thy name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   400
  of NONE => error ("No generator defined for mode " ^ string_of_mode mode ^ " of predicate " ^ name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   401
   | SOME data => data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   402
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   403
val generator_name_of = #name ooo the_generator_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   404
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   405
val generator_modes_of = (map fst) o #generators oo the_pred_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   406
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   407
fun all_generator_modes_of thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   408
  map (fn name => (name, generator_modes_of thy name)) (all_preds_of thy) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   409
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   410
fun lookup_sizelim_function_data thy name mode =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   411
  Option.map rep_function_data (AList.lookup (op =)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   412
  (#sizelim_functions (the_pred_data thy name)) mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   413
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   414
fun the_sizelim_function_data thy name mode = case lookup_sizelim_function_data thy name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   415
  of NONE => error ("No size-limited function defined for mode " ^ string_of_mode mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   416
    ^ " of predicate " ^ name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   417
   | SOME data => data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   418
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   419
val sizelim_function_name_of = #name ooo the_sizelim_function_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   420
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   421
(*val generator_modes_of = (map fst) o #generators oo the_pred_data*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   422
     
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   423
(* diagnostic display functions *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   424
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   425
fun print_modes modes = Output.tracing ("Inferred modes:\n" ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   426
  cat_lines (map (fn (s, ms) => s ^ ": " ^ commas (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   427
    string_of_mode ms)) modes));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   428
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   429
fun print_pred_mode_table string_of_entry thy pred_mode_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   430
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   431
    fun print_mode pred (mode, entry) =  "mode : " ^ (string_of_mode mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   432
      ^ (string_of_entry pred mode entry)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   433
    fun print_pred (pred, modes) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   434
      "predicate " ^ pred ^ ": " ^ cat_lines (map (print_mode pred) modes)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   435
    val _ = Output.tracing (cat_lines (map print_pred pred_mode_table))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   436
  in () end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   437
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   438
fun string_of_moded_prem thy (Prem (ts, p), tmode) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   439
    (Syntax.string_of_term_global thy (list_comb (p, ts))) ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   440
    "(" ^ (string_of_tmode tmode) ^ ")"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   441
  | string_of_moded_prem thy (GeneratorPrem (ts, p), Mode (predmode, is, _)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   442
    (Syntax.string_of_term_global thy (list_comb (p, ts))) ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   443
    "(generator_mode: " ^ (string_of_mode predmode) ^ ")"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   444
  | string_of_moded_prem thy (Generator (v, T), _) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   445
    "Generator for " ^ v ^ " of Type " ^ (Syntax.string_of_typ_global thy T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   446
  | string_of_moded_prem thy (Negprem (ts, p), Mode (_, is, _)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   447
    (Syntax.string_of_term_global thy (list_comb (p, ts))) ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   448
    "(negative mode: " ^ string_of_smode is ^ ")"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   449
  | string_of_moded_prem thy (Sidecond t, Mode (_, is, _)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   450
    (Syntax.string_of_term_global thy t) ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   451
    "(sidecond mode: " ^ string_of_smode is ^ ")"    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   452
  | string_of_moded_prem _ _ = error "string_of_moded_prem: unimplemented"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   453
     
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   454
fun print_moded_clauses thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   455
  let        
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   456
    fun string_of_clause pred mode clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   457
      cat_lines (map (fn (ts, prems) => (space_implode " --> "
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   458
        (map (string_of_moded_prem thy) prems)) ^ " --> " ^ pred ^ " "
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   459
        ^ (space_implode " " (map (Syntax.string_of_term_global thy) ts))) clauses)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   460
  in print_pred_mode_table string_of_clause thy end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   461
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   462
fun print_compiled_terms thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   463
  print_pred_mode_table (fn _ => fn _ => Syntax.string_of_term_global thy) thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   464
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   465
fun print_stored_rules thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   466
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   467
    val preds = (Graph.keys o PredData.get) thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   468
    fun print pred () = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   469
      val _ = writeln ("predicate: " ^ pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   470
      val _ = writeln ("number of parameters: " ^ string_of_int (nparams_of thy pred))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   471
      val _ = writeln ("introrules: ")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   472
      val _ = fold (fn thm => fn u => writeln (Display.string_of_thm_global thy thm))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   473
        (rev (intros_of thy pred)) ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   474
    in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   475
      if (has_elim thy pred) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   476
        writeln ("elimrule: " ^ Display.string_of_thm_global thy (the_elim_of thy pred))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   477
      else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   478
        writeln ("no elimrule defined")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   479
    end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   480
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   481
    fold print preds ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   482
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   483
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   484
fun print_all_modes thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   485
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   486
    val _ = writeln ("Inferred modes:")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   487
    fun print (pred, modes) u =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   488
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   489
        val _ = writeln ("predicate: " ^ pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   490
        val _ = writeln ("modes: " ^ (commas (map string_of_mode modes)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   491
      in u end  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   492
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   493
    fold print (all_modes_of thy) ()
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   494
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   495
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   496
(** preprocessing rules **)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   497
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   498
fun imp_prems_conv cv ct =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   499
  case Thm.term_of ct of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   500
    Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   501
  | _ => Conv.all_conv ct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   502
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   503
fun Trueprop_conv cv ct =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   504
  case Thm.term_of ct of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   505
    Const ("Trueprop", _) $ _ => Conv.arg_conv cv ct  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   506
  | _ => error "Trueprop_conv"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   507
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   508
fun preprocess_intro thy rule =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   509
  Conv.fconv_rule
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   510
    (imp_prems_conv
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   511
      (Trueprop_conv (Conv.try_conv (Conv.rewr_conv (Thm.symmetric @{thm Predicate.eq_is_eq})))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   512
    (Thm.transfer thy rule)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   513
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   514
fun preprocess_elim thy nparams elimrule =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   515
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   516
    val _ = Output.tracing ("Preprocessing elimination rule "
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   517
      ^ (Display.string_of_thm_global thy elimrule))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   518
    fun replace_eqs (Const ("Trueprop", _) $ (Const ("op =", T) $ lhs $ rhs)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   519
       HOLogic.mk_Trueprop (Const (@{const_name Predicate.eq}, T) $ lhs $ rhs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   520
     | replace_eqs t = t
33109
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   521
   val ctxt = ProofContext.init thy
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   522
   val ((_, [elimrule]), ctxt') = Variable.import false [elimrule] ctxt
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   523
   val prems = Thm.prems_of elimrule
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   524
    val nargs = length (snd (strip_comb (HOLogic.dest_Trueprop (hd prems)))) - nparams
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   525
    fun preprocess_case t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   526
     let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   527
       val params = Logic.strip_params t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   528
       val (assums1, assums2) = chop nargs (Logic.strip_assums_hyp t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   529
       val assums_hyp' = assums1 @ (map replace_eqs assums2)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   530
     in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   531
       list_all (params, Logic.list_implies (assums_hyp', Logic.strip_assums_concl t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   532
     end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   533
    val cases' = map preprocess_case (tl prems)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   534
    val elimrule' = Logic.list_implies ((hd prems) :: cases', Thm.concl_of elimrule)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   535
    val bigeq = (Thm.symmetric (Conv.implies_concl_conv
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   536
      (MetaSimplifier.rewrite true [@{thm Predicate.eq_is_eq}])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   537
        (cterm_of thy elimrule')))
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
   538
    val tac = (fn _ => setmp quick_and_dirty true (SkipProof.cheat_tac thy))    
33109
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   539
    val eq = Goal.prove ctxt' [] [] (Logic.mk_equals ((Thm.prop_of elimrule), elimrule')) tac
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   540
    val _ = Output.tracing "Preprocessed elimination rule"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   541
  in
33109
7025bc7a5054 changed elimination preprocessing due to an error with a JinjaThread predicate
bulwahn
parents: 33108
diff changeset
   542
    Thm.equal_elim eq elimrule |> singleton (Variable.export ctxt' ctxt)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   543
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   544
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   545
(* special case: predicate with no introduction rule *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   546
fun noclause thy predname elim = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   547
  val T = (Logic.unvarifyT o Sign.the_const_type thy) predname
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   548
  val Ts = binder_types T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   549
  val names = Name.variant_list []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   550
        (map (fn i => "x" ^ (string_of_int i)) (1 upto (length Ts)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   551
  val vs = map2 (curry Free) names Ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   552
  val clausehd = HOLogic.mk_Trueprop (list_comb (Const (predname, T), vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   553
  val intro_t = Logic.mk_implies (@{prop False}, clausehd)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   554
  val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   555
  val elim_t = Logic.list_implies ([clausehd, Logic.mk_implies (@{prop False}, P)], P)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   556
  val intro = Goal.prove (ProofContext.init thy) names [] intro_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   557
        (fn {...} => etac @{thm FalseE} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   558
  val elim = Goal.prove (ProofContext.init thy) ("P" :: names) [] elim_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   559
        (fn {...} => etac elim 1) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   560
in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   561
  ([intro], elim)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   562
end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   563
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   564
fun fetch_pred_data thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   565
  case try (Inductive.the_inductive (ProofContext.init thy)) name of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   566
    SOME (info as (_, result)) => 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   567
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   568
        fun is_intro_of intro =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   569
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   570
            val (const, _) = strip_comb (HOLogic.dest_Trueprop (concl_of intro))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   571
          in (fst (dest_Const const) = name) end;      
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   572
        val intros = ind_set_codegen_preproc thy ((map (preprocess_intro thy))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   573
          (filter is_intro_of (#intrs result)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   574
        val pre_elim = nth (#elims result) (find_index (fn s => s = name) (#names (fst info)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   575
        val nparams = length (Inductive.params_of (#raw_induct result))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   576
        val elim = singleton (ind_set_codegen_preproc thy) (preprocess_elim thy nparams pre_elim)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   577
        val (intros, elim) = if null intros then noclause thy name elim else (intros, elim)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   578
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   579
        mk_pred_data ((intros, SOME elim, nparams), ([], [], []))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   580
      end                                                                    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   581
  | NONE => error ("No such predicate: " ^ quote name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   582
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   583
(* updaters *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   584
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   585
fun apfst3 f (x, y, z) =  (f x, y, z)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   586
fun apsnd3 f (x, y, z) =  (x, f y, z)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   587
fun aptrd3 f (x, y, z) =  (x, y, f z)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   588
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   589
fun add_predfun name mode data =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   590
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   591
    val add = (apsnd o apfst3 o cons) (mode, mk_predfun_data data)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   592
  in PredData.map (Graph.map_node name (map_pred_data add)) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   593
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   594
fun is_inductive_predicate thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   595
  is_some (try (Inductive.the_inductive (ProofContext.init thy)) name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   596
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   597
fun depending_preds_of thy (key, value) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   598
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   599
    val intros = (#intros o rep_pred_data) value
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   600
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   601
    fold Term.add_const_names (map Thm.prop_of intros) []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   602
      |> filter (fn c => (not (c = key)) andalso (is_inductive_predicate thy c orelse is_registered thy c))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   603
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   604
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   605
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   606
(* code dependency graph *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   607
(*
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   608
fun dependencies_of thy name =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   609
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   610
    val (intros, elim, nparams) = fetch_pred_data thy name 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   611
    val data = mk_pred_data ((intros, SOME elim, nparams), ([], [], []))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   612
    val keys = depending_preds_of thy intros
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   613
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   614
    (data, keys)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   615
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   616
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   617
(* guessing number of parameters *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   618
fun find_indexes pred xs =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   619
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   620
    fun find is n [] = is
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   621
      | find is n (x :: xs) = find (if pred x then (n :: is) else is) (n + 1) xs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   622
  in rev (find [] 0 xs) end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   623
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   624
fun guess_nparams T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   625
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   626
    val argTs = binder_types T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   627
    val nparams = fold (curry Int.max)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   628
      (map (fn x => x + 1) (find_indexes is_predT argTs)) 0
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   629
  in nparams end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   630
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   631
fun add_intro thm thy = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   632
   val (name, T) = dest_Const (fst (strip_intro_concl 0 (prop_of thm)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   633
   fun cons_intro gr =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   634
     case try (Graph.get_node gr) name of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   635
       SOME pred_data => Graph.map_node name (map_pred_data
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   636
         (apfst (fn (intro, elim, nparams) => (thm::intro, elim, nparams)))) gr
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   637
     | NONE =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   638
       let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   639
         val nparams = the_default (guess_nparams T)  (try (#nparams o rep_pred_data o (fetch_pred_data thy)) name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   640
       in Graph.new_node (name, mk_pred_data (([thm], NONE, nparams), ([], [], []))) gr end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   641
  in PredData.map cons_intro thy end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   642
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   643
fun set_elim thm = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   644
    val (name, _) = dest_Const (fst 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   645
      (strip_comb (HOLogic.dest_Trueprop (hd (prems_of thm)))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   646
    fun set (intros, _, nparams) = (intros, SOME thm, nparams)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   647
  in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   648
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   649
fun set_nparams name nparams = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   650
    fun set (intros, elim, _ ) = (intros, elim, nparams) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   651
  in PredData.map (Graph.map_node name (map_pred_data (apfst set))) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   652
    
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   653
fun register_predicate (pre_intros, pre_elim, nparams) thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   654
  let
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   655
    val (name, _) = dest_Const (fst (strip_intro_concl nparams (prop_of (hd pre_intros))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   656
    (* preprocessing *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   657
    val intros = ind_set_codegen_preproc thy (map (preprocess_intro thy) pre_intros)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   658
    val elim = singleton (ind_set_codegen_preproc thy) (preprocess_elim thy nparams pre_elim)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   659
  in
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   660
    if not (member (op =) (Graph.keys (PredData.get thy)) name) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   661
      PredData.map
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   662
        (Graph.new_node (name, mk_pred_data ((intros, SOME elim, nparams), ([], [], [])))) thy
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   663
    else thy
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   664
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   665
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   666
fun register_intros pre_intros thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   667
  let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   668
    val (c, T) = dest_Const (fst (strip_intro_concl 0 (prop_of (hd pre_intros))))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   669
    val _ = Output.tracing ("Registering introduction rules of " ^ c)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   670
    val _ = Output.tracing (commas (map (Display.string_of_thm_global thy) pre_intros))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   671
    val nparams = guess_nparams T
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   672
    val pre_elim = 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   673
      (Drule.standard o (setmp quick_and_dirty true (SkipProof.make_thm thy)))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   674
      (mk_casesrule (ProofContext.init thy) nparams pre_intros)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   675
  in register_predicate (pre_intros, pre_elim, nparams) thy end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   676
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   677
fun set_generator_name pred mode name = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   678
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   679
    val set = (apsnd o apsnd3 o cons) (mode, mk_function_data (name, NONE))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   680
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   681
    PredData.map (Graph.map_node pred (map_pred_data set))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   682
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   683
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   684
fun set_sizelim_function_name pred mode name = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   685
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   686
    val set = (apsnd o aptrd3 o cons) (mode, mk_function_data (name, NONE))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   687
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   688
    PredData.map (Graph.map_node pred (map_pred_data set))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   689
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   690
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   691
(** data structures for generic compilation for different monads **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   692
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   693
(* maybe rename functions more generic:
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   694
  mk_predT -> mk_monadT; dest_predT -> dest_monadT
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   695
  mk_single -> mk_return (?)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   696
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   697
datatype compilation_funs = CompilationFuns of {
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   698
  mk_predT : typ -> typ,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   699
  dest_predT : typ -> typ,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   700
  mk_bot : typ -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   701
  mk_single : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   702
  mk_bind : term * term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   703
  mk_sup : term * term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   704
  mk_if : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   705
  mk_not : term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   706
(*  funT_of : mode -> typ -> typ, *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   707
(*  mk_fun_of : theory -> (string * typ) -> mode -> term, *) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   708
  mk_map : typ -> typ -> term -> term -> term,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   709
  lift_pred : term -> term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   710
};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   711
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   712
fun mk_predT (CompilationFuns funs) = #mk_predT funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   713
fun dest_predT (CompilationFuns funs) = #dest_predT funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   714
fun mk_bot (CompilationFuns funs) = #mk_bot funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   715
fun mk_single (CompilationFuns funs) = #mk_single funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   716
fun mk_bind (CompilationFuns funs) = #mk_bind funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   717
fun mk_sup (CompilationFuns funs) = #mk_sup funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   718
fun mk_if (CompilationFuns funs) = #mk_if funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   719
fun mk_not (CompilationFuns funs) = #mk_not funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   720
(*fun funT_of (CompilationFuns funs) = #funT_of funs*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   721
(*fun mk_fun_of (CompilationFuns funs) = #mk_fun_of funs*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   722
fun mk_map (CompilationFuns funs) = #mk_map funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   723
fun lift_pred (CompilationFuns funs) = #lift_pred funs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   724
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   725
fun funT_of compfuns (iss, is) T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   726
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   727
    val Ts = binder_types T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   728
    val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   729
    val paramTs' = map2 (fn NONE => I | SOME is => funT_of compfuns ([], is)) iss paramTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   730
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   731
    (paramTs' @ inargTs) ---> (mk_predT compfuns (mk_tupleT outargTs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   732
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   733
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   734
fun mk_fun_of compfuns thy (name, T) mode = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   735
  Const (predfun_name_of thy name mode, funT_of compfuns mode T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   736
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   737
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   738
structure PredicateCompFuns =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   739
struct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   740
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   741
fun mk_predT T = Type (@{type_name "Predicate.pred"}, [T])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   742
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   743
fun dest_predT (Type (@{type_name "Predicate.pred"}, [T])) = T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   744
  | dest_predT T = raise TYPE ("dest_predT", [T], []);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   745
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   746
fun mk_bot T = Const (@{const_name Orderings.bot}, mk_predT T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   747
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   748
fun mk_single t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   749
  let val T = fastype_of t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   750
  in Const(@{const_name Predicate.single}, T --> mk_predT T) $ t end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   751
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   752
fun mk_bind (x, f) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   753
  let val T as Type ("fun", [_, U]) = fastype_of f
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   754
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   755
    Const (@{const_name Predicate.bind}, fastype_of x --> T --> U) $ x $ f
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   756
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   757
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   758
val mk_sup = HOLogic.mk_binop @{const_name sup};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   759
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   760
fun mk_if cond = Const (@{const_name Predicate.if_pred},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   761
  HOLogic.boolT --> mk_predT HOLogic.unitT) $ cond;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   762
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   763
fun mk_not t = let val T = mk_predT HOLogic.unitT
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   764
  in Const (@{const_name Predicate.not_pred}, T --> T) $ t end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   765
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   766
fun mk_Enum f =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   767
  let val T as Type ("fun", [T', _]) = fastype_of f
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   768
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   769
    Const (@{const_name Predicate.Pred}, T --> mk_predT T') $ f    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   770
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   771
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   772
fun mk_Eval (f, x) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   773
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   774
    val T = fastype_of x
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   775
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   776
    Const (@{const_name Predicate.eval}, mk_predT T --> T --> HOLogic.boolT) $ f $ x
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   777
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   778
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   779
fun mk_map T1 T2 tf tp = Const (@{const_name Predicate.map},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   780
  (T1 --> T2) --> mk_predT T1 --> mk_predT T2) $ tf $ tp;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   781
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   782
val lift_pred = I
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   783
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   784
val compfuns = CompilationFuns {mk_predT = mk_predT, dest_predT = dest_predT, mk_bot = mk_bot,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   785
  mk_single = mk_single, mk_bind = mk_bind, mk_sup = mk_sup, mk_if = mk_if, mk_not = mk_not,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   786
  mk_map = mk_map, lift_pred = lift_pred};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   787
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   788
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   789
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   790
structure RPredCompFuns =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   791
struct
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   792
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   793
fun mk_rpredT T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   794
  @{typ "Random.seed"} --> HOLogic.mk_prodT (PredicateCompFuns.mk_predT T, @{typ "Random.seed"})
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   795
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   796
fun dest_rpredT (Type ("fun", [_,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   797
  Type (@{type_name "*"}, [Type (@{type_name "Predicate.pred"}, [T]), _])])) = T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   798
  | dest_rpredT T = raise TYPE ("dest_rpredT", [T], []); 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   799
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   800
fun mk_bot T = Const(@{const_name RPred.bot}, mk_rpredT T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   801
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   802
fun mk_single t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   803
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   804
    val T = fastype_of t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   805
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   806
    Const (@{const_name RPred.return}, T --> mk_rpredT T) $ t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   807
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   808
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   809
fun mk_bind (x, f) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   810
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   811
    val T as (Type ("fun", [_, U])) = fastype_of f
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   812
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   813
    Const (@{const_name RPred.bind}, fastype_of x --> T --> U) $ x $ f
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   814
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   815
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   816
val mk_sup = HOLogic.mk_binop @{const_name RPred.supp}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   817
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   818
fun mk_if cond = Const (@{const_name RPred.if_rpred},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   819
  HOLogic.boolT --> mk_rpredT HOLogic.unitT) $ cond;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   820
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   821
fun mk_not t = error "Negation is not defined for RPred"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   822
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   823
fun mk_map t = error "FIXME" (*FIXME*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   824
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   825
fun lift_pred t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   826
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   827
    val T = PredicateCompFuns.dest_predT (fastype_of t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   828
    val lift_predT = PredicateCompFuns.mk_predT T --> mk_rpredT T 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   829
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   830
    Const (@{const_name "RPred.lift_pred"}, lift_predT) $ t  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   831
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   832
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   833
val compfuns = CompilationFuns {mk_predT = mk_rpredT, dest_predT = dest_rpredT, mk_bot = mk_bot,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   834
    mk_single = mk_single, mk_bind = mk_bind, mk_sup = mk_sup, mk_if = mk_if, mk_not = mk_not,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   835
    mk_map = mk_map, lift_pred = lift_pred};
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   836
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   837
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   838
(* for external use with interactive mode *)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   839
val pred_compfuns = PredicateCompFuns.compfuns
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   840
val rpred_compfuns = RPredCompFuns.compfuns;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   841
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   842
fun lift_random random =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   843
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   844
    val T = dest_randomT (fastype_of random)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   845
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   846
    Const (@{const_name lift_random}, (@{typ Random.seed} -->
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   847
      HOLogic.mk_prodT (HOLogic.mk_prodT (T, @{typ "unit => term"}), @{typ Random.seed})) --> 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   848
      RPredCompFuns.mk_rpredT T) $ random
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   849
  end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   850
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   851
fun sizelim_funT_of compfuns (iss, is) T =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   852
  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   853
    val Ts = binder_types T
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   854
    val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   855
    val paramTs' = map2 (fn SOME is => sizelim_funT_of PredicateCompFuns.compfuns ([], is) | NONE => I) iss paramTs 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   856
  in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   857
    (paramTs' @ inargTs @ [@{typ "code_numeral"}]) ---> (mk_predT compfuns (mk_tupleT outargTs))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   858
  end;  
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   859
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   860
fun mk_sizelim_fun_of compfuns thy (name, T) mode =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   861
  Const (sizelim_function_name_of thy name mode, sizelim_funT_of compfuns mode T)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   862
  
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   863
fun mk_generator_of compfuns thy (name, T) mode = 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   864
  Const (generator_name_of thy name mode, sizelim_funT_of compfuns mode T)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
   865
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   866
(* Mode analysis *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   867
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   868
(*** check if a term contains only constructor functions ***)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   869
fun is_constrt thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   870
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   871
    val cnstrs = flat (maps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   872
      (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   873
      (Symtab.dest (Datatype.get_all thy)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   874
    fun check t = (case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   875
        (Free _, []) => true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   876
      | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   877
            (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   878
          | _ => false)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   879
      | _ => false)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   880
  in check end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   881
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   882
(*** check if a type is an equality type (i.e. doesn't contain fun)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   883
  FIXME this is only an approximation ***)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   884
fun is_eqT (Type (s, Ts)) = s <> "fun" andalso forall is_eqT Ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   885
  | is_eqT _ = true;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   886
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   887
fun term_vs tm = fold_aterms (fn Free (x, T) => cons x | _ => I) tm [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   888
val terms_vs = distinct (op =) o maps term_vs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   889
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   890
(** collect all Frees in a term (with duplicates!) **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   891
fun term_vTs tm =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   892
  fold_aterms (fn Free xT => cons xT | _ => I) tm [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   893
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   894
(*FIXME this function should not be named merge... make it local instead*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   895
fun merge xs [] = xs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   896
  | merge [] ys = ys
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   897
  | merge (x::xs) (y::ys) = if length x >= length y then x::merge xs (y::ys)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   898
      else y::merge (x::xs) ys;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   899
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   900
fun subsets i j = if i <= j then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   901
       let val is = subsets (i+1) j
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   902
       in merge (map (fn ks => i::ks) is) is end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   903
     else [[]];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   904
     
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
   905
(* FIXME: should be in library - cprod = map_prod I *)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   906
fun cprod ([], ys) = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   907
  | cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   908
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   909
fun cprods xss = foldr (map op :: o cprod) [[]] xss;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   910
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   911
fun cprods_subset [] = [[]]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   912
  | cprods_subset (xs :: xss) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   913
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   914
    val yss = (cprods_subset xss)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   915
  in maps (fn ys => map (fn x => cons x ys) xs) yss @ yss end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   916
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   917
(*TODO: cleanup function and put together with modes_of_term *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   918
(*
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   919
fun modes_of_param default modes t = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   920
    val (vs, t') = strip_abs t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   921
    val b = length vs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   922
    fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   923
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   924
          val (args1, args2) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   925
            if length args < length iss then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   926
              error ("Too few arguments for inductive predicate " ^ name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   927
            else chop (length iss) args;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   928
          val k = length args2;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   929
          val perm = map (fn i => (find_index_eq (Bound (b - i)) args2) + 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   930
            (1 upto b)  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   931
          val partial_mode = (1 upto k) \\ perm
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   932
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   933
          if not (partial_mode subset is) then [] else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   934
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   935
            val is' = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   936
            (fold_index (fn (i, j) => if j mem is then cons (i + 1) else I) perm [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   937
            |> fold (fn i => if i > k then cons (i - k + b) else I) is
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   938
              
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   939
           val res = map (fn x => Mode (m, is', x)) (cprods (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   940
            (fn (NONE, _) => [NONE]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   941
              | (SOME js, arg) => map SOME (filter
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   942
                  (fn Mode (_, js', _) => js=js') (modes_of_term modes arg)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   943
                    (iss ~~ args1)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   944
          in res end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   945
        end)) (AList.lookup op = modes name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   946
  in case strip_comb t' of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   947
    (Const (name, _), args) => the_default default (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   948
    | (Var ((name, _), _), args) => the (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   949
    | (Free (name, _), args) => the (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   950
    | _ => default end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   951
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   952
and
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   953
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   954
fun modes_of_term modes t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   955
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   956
    val ks = map_index (fn (i, T) => (i, NONE)) (binder_types (fastype_of t));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   957
    val default = [Mode (([], ks), ks, [])];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   958
    fun mk_modes name args = Option.map (maps (fn (m as (iss, is)) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   959
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   960
          val (args1, args2) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   961
            if length args < length iss then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   962
              error ("Too few arguments for inductive predicate " ^ name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   963
            else chop (length iss) args;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   964
          val k = length args2;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   965
          val prfx = map (rpair NONE) (1 upto k)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   966
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   967
          if not (is_prefix op = prfx is) then [] else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   968
          let val is' = List.drop (is, k)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   969
          in map (fn x => Mode (m, is', x)) (cprods (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   970
            (fn (NONE, _) => [NONE]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   971
              | (SOME js, arg) => map SOME (filter
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   972
                  (fn Mode (_, js', _) => js=js') (modes_of_term modes arg)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   973
                    (iss ~~ args1)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   974
          end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   975
        end)) (AList.lookup op = modes name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   976
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   977
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   978
    case strip_comb (Envir.eta_contract t) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   979
      (Const (name, _), args) => the_default default (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   980
    | (Var ((name, _), _), args) => the (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   981
    | (Free (name, _), args) => the (mk_modes name args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   982
    | (Abs _, []) => error "Abs at param position" (* modes_of_param default modes t *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   983
    | _ => default
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   984
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   985
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   986
fun select_mode_prem thy modes vs ps =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   987
  find_first (is_some o snd) (ps ~~ map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   988
    (fn Prem (us, t) => find_first (fn Mode (_, is, _) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   989
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   990
            val (in_ts, out_ts) = split_smode is us;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   991
            val (out_ts', in_ts') = List.partition (is_constrt thy) out_ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   992
            val vTs = maps term_vTs out_ts';
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   993
            val dupTs = map snd (duplicates (op =) vTs) @
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   994
              List.mapPartial (AList.lookup (op =) vTs) vs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   995
          in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   996
            terms_vs (in_ts @ in_ts') subset vs andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   997
            forall (is_eqT o fastype_of) in_ts' andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   998
            term_vs t subset vs andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
   999
            forall is_eqT dupTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1000
          end)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1001
            (modes_of_term modes t handle Option =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1002
               error ("Bad predicate: " ^ Syntax.string_of_term_global thy t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1003
      | Negprem (us, t) => find_first (fn Mode (_, is, _) =>
33112
6672184a736b added a few tricky examples with tuples; added alternative introduction rules for some constants; corrected mode analysis with negation; improved fetching of definitions
bulwahn
parents: 33109
diff changeset
  1004
        is = map (rpair NONE) (1 upto length us) andalso
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1005
            terms_vs us subset vs andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1006
            term_vs t subset vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1007
            (modes_of_term modes t handle Option =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1008
               error ("Bad predicate: " ^ Syntax.string_of_term_global thy t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1009
      | Sidecond t => if term_vs t subset vs then SOME (Mode (([], []), [], []))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1010
          else NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1011
      ) ps);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1012
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1013
fun fold_prem f (Prem (args, _)) = fold f args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1014
  | fold_prem f (Negprem (args, _)) = fold f args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1015
  | fold_prem f (Sidecond t) = f t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1016
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1017
fun all_subsets [] = [[]]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1018
  | all_subsets (x::xs) = let val xss' = all_subsets xs in xss' @ (map (cons x) xss') end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1019
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1020
fun generator vTs v = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1021
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1022
    val T = the (AList.lookup (op =) vTs v)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1023
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1024
    (Generator (v, T), Mode (([], []), [], []))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1025
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1026
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1027
fun gen_prem (Prem (us, t)) = GeneratorPrem (us, t)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1028
  | gen_prem (Negprem (us, t)) = error "it is a negated prem"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1029
  | gen_prem (Sidecond t) = error "it is a sidecond"
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1030
  | gen_prem _ = error "gen_prem : invalid input for gen_prem"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1031
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1032
fun param_gen_prem param_vs (p as Prem (us, t as Free (v, _))) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1033
  if member (op =) param_vs v then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1034
    GeneratorPrem (us, t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1035
  else p  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1036
  | param_gen_prem param_vs p = p
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1037
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1038
fun check_mode_clause with_generator thy param_vs modes gen_modes (iss, is) (ts, ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1039
  let
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1040
    (*
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1041
  val _ = Output.tracing ("param_vs:" ^ commas param_vs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1042
  val _ = Output.tracing ("iss:" ^
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1043
    commas (map (fn is => case is of SOME is => string_of_smode is | NONE => "NONE") iss))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1044
    *)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1045
    val modes' = modes @ List.mapPartial
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1046
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1047
        (param_vs ~~ iss);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1048
    val gen_modes' = gen_modes @ List.mapPartial
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1049
      (fn (_, NONE) => NONE | (v, SOME js) => SOME (v, [([], js)]))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1050
        (param_vs ~~ iss);  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1051
    val vTs = distinct (op =) ((fold o fold_prem) Term.add_frees ps (fold Term.add_frees ts []))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1052
    val prem_vs = distinct (op =) ((fold o fold_prem) Term.add_free_names ps [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1053
    fun check_mode_prems acc_ps vs [] = SOME (acc_ps, vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1054
      | check_mode_prems acc_ps vs ps = (case select_mode_prem thy modes' vs ps of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1055
          NONE =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1056
            (if with_generator then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1057
              (case select_mode_prem thy gen_modes' vs ps of
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1058
                SOME (p as Prem _, SOME mode) => check_mode_prems ((gen_prem p, mode) :: acc_ps) 
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1059
                  (case p of Prem (us, _) => vs union terms_vs us | _ => vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1060
                  (filter_out (equal p) ps)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1061
              | _ =>
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1062
                  let 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1063
                    val all_generator_vs = all_subsets (prem_vs \\ vs) |> sort (int_ord o (pairself length))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1064
                  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1065
                    case (find_first (fn generator_vs => is_some
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1066
                      (select_mode_prem thy modes' (vs union generator_vs) ps)) all_generator_vs) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1067
                      SOME generator_vs => check_mode_prems ((map (generator vTs) generator_vs) @ acc_ps)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1068
                        (vs union generator_vs) ps
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1069
                    | NONE => let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1070
                    val _ = Output.tracing ("ps:" ^ (commas
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  1071
                    (map (fn p => string_of_moded_prem thy (p, Mode (([], []), [], []))) ps)))
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1072
                  in (*error "mode analysis failed"*)NONE end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1073
                  end)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1074
            else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1075
              NONE)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1076
        | SOME (p, SOME mode) => check_mode_prems ((if with_generator then param_gen_prem param_vs p else p, mode) :: acc_ps) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1077
            (case p of Prem (us, _) => vs union terms_vs us | _ => vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1078
            (filter_out (equal p) ps))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1079
    val (in_ts, in_ts') = List.partition (is_constrt thy) (fst (split_smode is ts));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1080
    val in_vs = terms_vs in_ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1081
    val concl_vs = terms_vs ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1082
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1083
    if forall is_eqT (map snd (duplicates (op =) (maps term_vTs in_ts))) andalso
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1084
    forall (is_eqT o fastype_of) in_ts' then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1085
      case check_mode_prems [] (param_vs union in_vs) ps of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1086
         NONE => NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1087
       | SOME (acc_ps, vs) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1088
         if with_generator then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1089
           SOME (ts, (rev acc_ps) @ (map (generator vTs) (concl_vs \\ vs))) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1090
         else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1091
           if concl_vs subset vs then SOME (ts, rev acc_ps) else NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1092
    else NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1093
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1094
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1095
fun check_modes_pred with_generator thy param_vs clauses modes gen_modes (p, ms) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1096
  let val SOME rs = AList.lookup (op =) clauses p
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1097
  in (p, List.filter (fn m => case find_index
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1098
    (is_none o check_mode_clause with_generator thy param_vs modes gen_modes m) rs of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1099
      ~1 => true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1100
    | i => (Output.tracing ("Clause " ^ string_of_int (i + 1) ^ " of " ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1101
      p ^ " violates mode " ^ string_of_mode m);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1102
        Output.tracing (commas (map (Syntax.string_of_term_global thy) (fst (nth rs i)))); false)) ms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1103
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1104
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1105
fun get_modes_pred with_generator thy param_vs clauses modes gen_modes (p, ms) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1106
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1107
    val SOME rs = AList.lookup (op =) clauses p 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1108
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1109
    (p, map (fn m =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1110
      (m, map (the o check_mode_clause with_generator thy param_vs modes gen_modes m) rs)) ms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1111
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1112
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1113
fun fixp f (x : (string * mode list) list) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1114
  let val y = f x
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1115
  in if x = y then x else fixp f y end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1116
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1117
fun infer_modes thy extra_modes all_modes param_vs clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1118
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1119
    val modes =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1120
      fixp (fn modes =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1121
        map (check_modes_pred false thy param_vs clauses (modes @ extra_modes) []) modes)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1122
          all_modes
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1123
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1124
    map (get_modes_pred false thy param_vs clauses (modes @ extra_modes) []) modes
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1125
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1126
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1127
fun remove_from rem [] = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1128
  | remove_from rem ((k, vs) :: xs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1129
    (case AList.lookup (op =) rem k of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1130
      NONE => (k, vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1131
    | SOME vs' => (k, vs \\ vs'))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1132
    :: remove_from rem xs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1133
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1134
fun infer_modes_with_generator thy extra_modes all_modes param_vs clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1135
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1136
    val prednames = map fst clauses
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1137
    val extra_modes = all_modes_of thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1138
    val gen_modes = all_generator_modes_of thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1139
      |> filter_out (fn (name, _) => member (op =) prednames name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1140
    val starting_modes = remove_from extra_modes all_modes 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1141
    val modes =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1142
      fixp (fn modes =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1143
        map (check_modes_pred true thy param_vs clauses extra_modes (gen_modes @ modes)) modes)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1144
         starting_modes 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1145
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1146
    map (get_modes_pred true thy param_vs clauses extra_modes (gen_modes @ modes)) modes
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1147
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1148
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1149
(* term construction *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1150
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1151
fun mk_v (names, vs) s T = (case AList.lookup (op =) vs s of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1152
      NONE => (Free (s, T), (names, (s, [])::vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1153
    | SOME xs =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1154
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1155
          val s' = Name.variant names s;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1156
          val v = Free (s', T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1157
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1158
          (v, (s'::names, AList.update (op =) (s, v::xs) vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1159
        end);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1160
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1161
fun distinct_v (Free (s, T)) nvs = mk_v nvs s T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1162
  | distinct_v (t $ u) nvs =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1163
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1164
        val (t', nvs') = distinct_v t nvs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1165
        val (u', nvs'') = distinct_v u nvs';
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1166
      in (t' $ u', nvs'') end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1167
  | distinct_v x nvs = (x, nvs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1168
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1169
fun compile_match thy compfuns eqs eqs' out_ts success_t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1170
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1171
    val eqs'' = maps mk_eq eqs @ eqs'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1172
    val names = fold Term.add_free_names (success_t :: eqs'' @ out_ts) [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1173
    val name = Name.variant names "x";
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1174
    val name' = Name.variant (name :: names) "y";
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1175
    val T = mk_tupleT (map fastype_of out_ts);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1176
    val U = fastype_of success_t;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1177
    val U' = dest_predT compfuns U;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1178
    val v = Free (name, T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1179
    val v' = Free (name', T);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1180
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1181
    lambda v (fst (Datatype.make_case
32671
fbd224850767 adapted configuration for DatatypeCase.make_case
bulwahn
parents: 32669
diff changeset
  1182
      (ProofContext.init thy) DatatypeCase.Quiet [] v
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1183
      [(mk_tuple out_ts,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1184
        if null eqs'' then success_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1185
        else Const (@{const_name HOL.If}, HOLogic.boolT --> U --> U --> U) $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1186
          foldr1 HOLogic.mk_conj eqs'' $ success_t $
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1187
            mk_bot compfuns U'),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1188
       (v', mk_bot compfuns U')]))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1189
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1190
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1191
(*FIXME function can be removed*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1192
fun mk_funcomp f t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1193
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1194
    val names = Term.add_free_names t [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1195
    val Ts = binder_types (fastype_of t);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1196
    val vs = map Free
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1197
      (Name.variant_list names (replicate (length Ts) "x") ~~ Ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1198
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1199
    fold_rev lambda vs (f (list_comb (t, vs)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1200
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1201
(*
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1202
fun compile_param_ext thy compfuns modes (NONE, t) = t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1203
  | compile_param_ext thy compfuns modes (m as SOME (Mode ((iss, is'), is, ms)), t) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1204
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1205
        val (vs, u) = strip_abs t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1206
        val (ivs, ovs) = split_mode is vs    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1207
        val (f, args) = strip_comb u
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1208
        val (params, args') = chop (length ms) args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1209
        val (inargs, outargs) = split_mode is' args'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1210
        val b = length vs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1211
        val perm = map (fn i => (find_index_eq (Bound (b - i)) args') + 1) (1 upto b)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1212
        val outp_perm =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1213
          snd (split_mode is perm)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1214
          |> map (fn i => i - length (filter (fn x => x < i) is'))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1215
        val names = [] -- TODO
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1216
        val out_names = Name.variant_list names (replicate (length outargs) "x")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1217
        val f' = case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1218
            Const (name, T) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1219
              if AList.defined op = modes name then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1220
                mk_predfun_of thy compfuns (name, T) (iss, is')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1221
              else error "compile param: Not an inductive predicate with correct mode"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1222
          | Free (name, T) => Free (name, param_funT_of compfuns T (SOME is'))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1223
        val outTs = dest_tupleT (dest_predT compfuns (body_type (fastype_of f')))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1224
        val out_vs = map Free (out_names ~~ outTs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1225
        val params' = map (compile_param thy modes) (ms ~~ params)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1226
        val f_app = list_comb (f', params' @ inargs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1227
        val single_t = (mk_single compfuns (mk_tuple (map (fn i => nth out_vs (i - 1)) outp_perm)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1228
        val match_t = compile_match thy compfuns [] [] out_vs single_t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1229
      in list_abs (ivs,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1230
        mk_bind compfuns (f_app, match_t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1231
      end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1232
  | compile_param_ext _ _ _ _ = error "compile params"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1233
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1234
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1235
fun compile_param neg_in_sizelim size thy compfuns (NONE, t) = t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1236
  | compile_param neg_in_sizelim size thy compfuns (m as SOME (Mode ((iss, is'), is, ms)), t) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1237
   let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1238
     val (f, args) = strip_comb (Envir.eta_contract t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1239
     val (params, args') = chop (length ms) args
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1240
     val params' = map (compile_param neg_in_sizelim size thy compfuns) (ms ~~ params)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1241
     val mk_fun_of = case size of NONE => mk_fun_of | SOME _ => mk_sizelim_fun_of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1242
     val funT_of = case size of NONE => funT_of | SOME _ => sizelim_funT_of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1243
     val f' =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1244
       case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1245
         Const (name, T) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1246
           mk_fun_of compfuns thy (name, T) (iss, is')
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1247
       | Free (name, T) =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1248
         case neg_in_sizelim of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1249
           SOME _ =>  Free (name, sizelim_funT_of compfuns (iss, is') T)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1250
         | NONE => Free (name, funT_of compfuns (iss, is') T)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1251
           
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1252
       | _ => error ("PredicateCompiler: illegal parameter term")
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1253
   in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1254
     (case neg_in_sizelim of SOME size_t =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1255
       (fn t =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1256
       let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1257
         val Ts = fst (split_last (binder_types (fastype_of t)))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1258
         val names = map (fn i => "x" ^ string_of_int i) (1 upto length Ts)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1259
       in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1260
         list_abs (names ~~ Ts, list_comb (t, (map Bound ((length Ts) - 1 downto 0)) @ [size_t]))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1261
       end)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1262
     | NONE => I)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1263
     (list_comb (f', params' @ args'))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1264
   end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1265
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1266
fun compile_expr neg_in_sizelim size thy ((Mode (mode, is, ms)), t) =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1267
  case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1268
    (Const (name, T), params) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1269
       let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1270
         val params' = map (compile_param neg_in_sizelim size thy PredicateCompFuns.compfuns) (ms ~~ params)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1271
         val mk_fun_of = case size of NONE => mk_fun_of | SOME _ => mk_sizelim_fun_of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1272
       in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1273
         list_comb (mk_fun_of PredicateCompFuns.compfuns thy (name, T) mode, params')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1274
       end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1275
  | (Free (name, T), args) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1276
       let 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1277
         val funT_of = case size of NONE => funT_of | SOME _ => sizelim_funT_of 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1278
       in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1279
         list_comb (Free (name, funT_of PredicateCompFuns.compfuns ([], is) T), args)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1280
       end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1281
       
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1282
fun compile_gen_expr size thy compfuns ((Mode (mode, is, ms)), t) inargs =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1283
  case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1284
    (Const (name, T), params) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1285
      let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1286
        val params' = map (compile_param NONE size thy PredicateCompFuns.compfuns) (ms ~~ params)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1287
      in
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1288
        list_comb (mk_generator_of compfuns thy (name, T) mode, params' @ inargs)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1289
      end
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1290
    | (Free (name, T), params) =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1291
    lift_pred compfuns
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1292
    (list_comb (Free (name, sizelim_funT_of PredicateCompFuns.compfuns ([], is) T), params @ inargs))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1293
      
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1294
          
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1295
(** specific rpred functions -- move them to the correct place in this file *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1296
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1297
fun mk_Eval_of size ((x, T), NONE) names = (x, names)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1298
  | mk_Eval_of size ((x, T), SOME mode) names =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1299
	let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1300
    val Ts = binder_types T
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1301
    (*val argnames = Name.variant_list names
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1302
        (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts)));
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1303
    val args = map Free (argnames ~~ Ts)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1304
    val (inargs, outargs) = split_smode mode args*)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1305
		fun mk_split_lambda [] t = lambda (Free (Name.variant names "x", HOLogic.unitT)) t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1306
			| mk_split_lambda [x] t = lambda x t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1307
			| mk_split_lambda xs t =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1308
			let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1309
				fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1310
					| mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1311
			in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1312
				mk_split_lambda' xs t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1313
			end;
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1314
  	fun mk_arg (i, T) =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1315
		  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1316
	  	  val vname = Name.variant names ("x" ^ string_of_int i)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1317
		    val default = Free (vname, T)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1318
		  in 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1319
		    case AList.lookup (op =) mode i of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1320
		      NONE => (([], [default]), [default])
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1321
			  | SOME NONE => (([default], []), [default])
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1322
			  | SOME (SOME pis) =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1323
				  case HOLogic.strip_tupleT T of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1324
						[] => error "pair mode but unit tuple" (*(([default], []), [default])*)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1325
					| [_] => error "pair mode but not a tuple" (*(([default], []), [default])*)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1326
					| Ts =>
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1327
					  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1328
							val vnames = Name.variant_list names
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1329
								(map (fn j => "x" ^ string_of_int i ^ "p" ^ string_of_int j)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1330
									(1 upto length Ts))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1331
							val args = map Free (vnames ~~ Ts)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1332
							fun split_args (i, arg) (ins, outs) =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1333
							  if member (op =) pis i then
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1334
							    (arg::ins, outs)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1335
								else
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1336
								  (ins, arg::outs)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1337
							val (inargs, outargs) = fold_rev split_args ((1 upto length Ts) ~~ args) ([], [])
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1338
							fun tuple args = if null args then [] else [HOLogic.mk_tuple args]
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1339
						in ((tuple inargs, tuple outargs), args) end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1340
			end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1341
		val (inoutargs, args) = split_list (map mk_arg (1 upto (length Ts) ~~ Ts))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1342
    val (inargs, outargs) = pairself flat (split_list inoutargs)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1343
    val size_t = case size of NONE => [] | SOME size_t => [size_t]
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1344
		val r = PredicateCompFuns.mk_Eval (list_comb (x, inargs @ size_t), mk_tuple outargs)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1345
    val t = fold_rev mk_split_lambda args r
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1346
  in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1347
    (t, names)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1348
  end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1349
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1350
fun compile_arg size thy param_vs iss arg = 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1351
  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1352
    val funT_of = case size of NONE => funT_of | SOME _ => sizelim_funT_of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1353
    fun map_params (t as Free (f, T)) =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1354
      if member (op =) param_vs f then
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1355
        case (the (AList.lookup (op =) (param_vs ~~ iss) f)) of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1356
          SOME is => let val T' = funT_of PredicateCompFuns.compfuns ([], is) T
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1357
            in fst (mk_Eval_of size ((Free (f, T'), T), SOME is) []) end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1358
        | NONE => t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1359
      else t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1360
      | map_params t = t
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1361
    in map_aterms map_params arg end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1362
  
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1363
fun compile_clause compfuns size final_term thy all_vs param_vs (iss, is) inp (ts, moded_ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1364
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1365
    fun check_constrt t (names, eqs) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1366
      if is_constrt thy t then (t, (names, eqs)) else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1367
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1368
          val s = Name.variant names "x";
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1369
          val v = Free (s, fastype_of t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1370
        in (v, (s::names, HOLogic.mk_eq (v, t)::eqs)) end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1371
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1372
    val (in_ts, out_ts) = split_smode is ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1373
    val (in_ts', (all_vs', eqs)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1374
      fold_map check_constrt in_ts (all_vs, []);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1375
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1376
    fun compile_prems out_ts' vs names [] =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1377
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1378
            val (out_ts'', (names', eqs')) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1379
              fold_map check_constrt out_ts' (names, []);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1380
            val (out_ts''', (names'', constr_vs)) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1381
              out_ts'' (names', map (rpair []) vs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1382
          in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1383
          (* termify code:
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1384
            compile_match thy compfuns constr_vs (eqs @ eqs') out_ts'''
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1385
              (mk_single compfuns (mk_tuple (map mk_valtermify_term out_ts)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1386
           *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1387
            compile_match thy compfuns constr_vs (eqs @ eqs') out_ts'''
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1388
              (final_term out_ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1389
          end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1390
      | compile_prems out_ts vs names ((p, mode as Mode ((_, is), _, _)) :: ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1391
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1392
            val vs' = distinct (op =) (flat (vs :: map term_vs out_ts));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1393
            val (out_ts', (names', eqs)) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1394
              fold_map check_constrt out_ts (names, [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1395
            val (out_ts'', (names'', constr_vs')) = fold_map distinct_v
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1396
              out_ts' ((names', map (rpair []) vs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1397
            val (compiled_clause, rest) = case p of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1398
               Prem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1399
                 let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1400
                   val (in_ts, out_ts''') = split_smode is us;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1401
                   val in_ts = map (compile_arg size thy param_vs iss) in_ts
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1402
                   val args = case size of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1403
                     NONE => in_ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1404
                   | SOME size_t => in_ts @ [size_t]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1405
                   val u = lift_pred compfuns
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1406
                     (list_comb (compile_expr NONE size thy (mode, t), args))                     
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1407
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1408
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1409
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1410
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1411
             | Negprem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1412
                 let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1413
                   val (in_ts, out_ts''') = split_smode is us
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1414
                   val u = lift_pred compfuns
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1415
                     (mk_not PredicateCompFuns.compfuns (list_comb (compile_expr size NONE thy (mode, t), in_ts)))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1416
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1417
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1418
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1419
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1420
             | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1421
                 let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1422
                   val rest = compile_prems [] vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1423
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1424
                   (mk_if compfuns t, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1425
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1426
             | GeneratorPrem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1427
                 let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1428
                   val (in_ts, out_ts''') = split_smode is us;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1429
                   val args = case size of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1430
                     NONE => in_ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1431
                   | SOME size_t => in_ts @ [size_t]
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1432
                   val u = compile_gen_expr size thy compfuns (mode, t) args
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1433
                   val rest = compile_prems out_ts''' vs' names'' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1434
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1435
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1436
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1437
             | Generator (v, T) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1438
                 let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1439
                   val u = lift_random (HOLogic.mk_random T (the size))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1440
                   val rest = compile_prems [Free (v, T)]  vs' names'' ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1441
                 in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1442
                   (u, rest)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1443
                 end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1444
          in
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1445
            compile_match thy compfuns constr_vs' eqs out_ts''
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1446
              (mk_bind compfuns (compiled_clause, rest))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1447
          end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1448
    val prem_t = compile_prems in_ts' param_vs all_vs' moded_ps;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1449
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1450
    mk_bind compfuns (mk_single compfuns inp, prem_t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1451
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1452
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1453
fun compile_pred compfuns mk_fun_of use_size thy all_vs param_vs s T mode moded_cls =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1454
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1455
	  val (Ts1, Ts2) = chop (length (fst mode)) (binder_types T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1456
    val (Us1, Us2) = split_smodeT (snd mode) Ts2
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1457
    val funT_of = if use_size then sizelim_funT_of else funT_of
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1458
    val Ts1' = map2 (fn NONE => I | SOME is => funT_of PredicateCompFuns.compfuns ([], is)) (fst mode) Ts1
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1459
    val size_name = Name.variant (all_vs @ param_vs) "size"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1460
  	fun mk_input_term (i, NONE) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1461
		    [Free (Name.variant (all_vs @ param_vs) ("x" ^ string_of_int i), nth Ts2 (i - 1))]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1462
		  | mk_input_term (i, SOME pis) = case HOLogic.strip_tupleT (nth Ts2 (i - 1)) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1463
						   [] => error "strange unit input"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1464
					   | [T] => [Free (Name.variant (all_vs @ param_vs) ("x" ^ string_of_int i), nth Ts2 (i - 1))]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1465
						 | Ts => let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1466
							 val vnames = Name.variant_list (all_vs @ param_vs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1467
								(map (fn j => "x" ^ string_of_int i ^ "p" ^ string_of_int j)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1468
									pis)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1469
						 in if null pis then []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1470
						   else [HOLogic.mk_tuple (map Free (vnames ~~ map (fn j => nth Ts (j - 1)) pis))] end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1471
		val in_ts = maps mk_input_term (snd mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1472
    val params = map2 (fn s => fn T => Free (s, T)) param_vs Ts1'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1473
    val size = Free (size_name, @{typ "code_numeral"})
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1474
    val decr_size =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1475
      if use_size then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1476
        SOME (Const ("HOL.minus_class.minus", @{typ "code_numeral => code_numeral => code_numeral"})
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1477
          $ size $ Const ("HOL.one_class.one", @{typ "Code_Numeral.code_numeral"}))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1478
      else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1479
        NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1480
    val cl_ts =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1481
      map (compile_clause compfuns decr_size (fn out_ts => mk_single compfuns (mk_tuple out_ts))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1482
        thy all_vs param_vs mode (mk_tuple in_ts)) moded_cls;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1483
    val t = foldr1 (mk_sup compfuns) cl_ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1484
    val T' = mk_predT compfuns (mk_tupleT Us2)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1485
    val size_t = Const (@{const_name "If"}, @{typ bool} --> T' --> T' --> T')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1486
      $ HOLogic.mk_eq (size, @{term "0 :: code_numeral"})
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1487
      $ mk_bot compfuns (dest_predT compfuns T') $ t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1488
    val fun_const = mk_fun_of compfuns thy (s, T) mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1489
    val eq = if use_size then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1490
      (list_comb (fun_const, params @ in_ts @ [size]), size_t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1491
    else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1492
      (list_comb (fun_const, params @ in_ts), t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1493
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1494
    HOLogic.mk_Trueprop (HOLogic.mk_eq eq)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1495
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1496
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1497
(* special setup for simpset *)                  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1498
val HOL_basic_ss' = HOL_basic_ss addsimps (@{thms "HOL.simp_thms"} @ [@{thm Pair_eq}])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1499
  setSolver (mk_solver "all_tac_solver" (fn _ => fn _ => all_tac))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1500
	setSolver (mk_solver "True_solver" (fn _ => rtac @{thm TrueI}))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1501
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1502
(* Definition of executable functions and their intro and elim rules *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1503
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1504
fun print_arities arities = tracing ("Arities:\n" ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1505
  cat_lines (map (fn (s, (ks, k)) => s ^ ": " ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1506
    space_implode " -> " (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1507
      (fn NONE => "X" | SOME k' => string_of_int k')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1508
        (ks @ [SOME k]))) arities));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1509
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1510
fun create_intro_elim_rule (mode as (iss, is)) defthm mode_id funT pred thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1511
let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1512
  val Ts = binder_types (fastype_of pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1513
  val funtrm = Const (mode_id, funT)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1514
  val (Ts1, Ts2) = chop (length iss) Ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1515
  val Ts1' = map2 (fn NONE => I | SOME is => funT_of (PredicateCompFuns.compfuns) ([], is)) iss Ts1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1516
	val param_names = Name.variant_list []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1517
    (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts1)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1518
  val params = map Free (param_names ~~ Ts1')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1519
	fun mk_args (i, T) argnames =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1520
    let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1521
		  val vname = Name.variant (param_names @ argnames) ("x" ^ string_of_int (length Ts1' + i))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1522
		  val default = (Free (vname, T), vname :: argnames)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1523
	  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1524
  	  case AList.lookup (op =) is i of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1525
						 NONE => default
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1526
					 | SOME NONE => default
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1527
        	 | SOME (SOME pis) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1528
					   case HOLogic.strip_tupleT T of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1529
						   [] => default
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1530
					   | [_] => default
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1531
						 | Ts => 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1532
						let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1533
							val vnames = Name.variant_list (param_names @ argnames)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1534
								(map (fn j => "x" ^ string_of_int (length Ts1' + i) ^ "p" ^ string_of_int j)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1535
									(1 upto (length Ts)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1536
						 in (HOLogic.mk_tuple (map Free (vnames ~~ Ts)), vnames  @ argnames) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1537
		end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1538
	val (args, argnames) = fold_map mk_args (1 upto (length Ts2) ~~ Ts2) []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1539
  val (inargs, outargs) = split_smode is args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1540
  val param_names' = Name.variant_list (param_names @ argnames)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1541
    (map (fn i => "p" ^ string_of_int i) (1 upto (length iss)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1542
  val param_vs = map Free (param_names' ~~ Ts1)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1543
  val (params', names) = fold_map (mk_Eval_of NONE) ((params ~~ Ts1) ~~ iss) []
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1544
  val predpropI = HOLogic.mk_Trueprop (list_comb (pred, param_vs @ args))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1545
  val predpropE = HOLogic.mk_Trueprop (list_comb (pred, params' @ args))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1546
  val param_eqs = map (HOLogic.mk_Trueprop o HOLogic.mk_eq) (param_vs ~~ params')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1547
  val funargs = params @ inargs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1548
  val funpropE = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, funargs),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1549
                  if null outargs then Free("y", HOLogic.unitT) else mk_tuple outargs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1550
  val funpropI = HOLogic.mk_Trueprop (PredicateCompFuns.mk_Eval (list_comb (funtrm, funargs),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1551
                   mk_tuple outargs))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1552
  val introtrm = Logic.list_implies (predpropI :: param_eqs, funpropI)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1553
  val simprules = [defthm, @{thm eval_pred},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1554
	  @{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}, @{thm pair_collapse}]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1555
  val unfolddef_tac = Simplifier.asm_full_simp_tac (HOL_basic_ss addsimps simprules) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1556
  val introthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ param_names' @ ["y"]) [] introtrm (fn {...} => unfolddef_tac)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1557
  val P = HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1558
  val elimtrm = Logic.list_implies ([funpropE, Logic.mk_implies (predpropE, P)], P)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1559
  val elimthm = Goal.prove (ProofContext.init thy) (argnames @ param_names @ param_names' @ ["y", "P"]) [] elimtrm (fn {...} => unfolddef_tac)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1560
in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1561
  (introthm, elimthm)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1562
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1563
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1564
fun create_constname_of_mode thy prefix name mode = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1565
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1566
    fun string_of_mode mode = if null mode then "0"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1567
      else space_implode "_" (map (fn (i, NONE) => string_of_int i | (i, SOME pis) => string_of_int i ^ "p"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1568
        ^ space_implode "p" (map string_of_int pis)) mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1569
    val HOmode = space_implode "_and_"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1570
      (fold (fn NONE => I | SOME mode => cons (string_of_mode mode)) (fst mode) [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1571
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1572
    (Sign.full_bname thy (prefix ^ (Long_Name.base_name name))) ^
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1573
      (if HOmode = "" then "_" else "_for_" ^ HOmode ^ "_yields_") ^ (string_of_mode (snd mode))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1574
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1575
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1576
fun split_tupleT is T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1577
	let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1578
		fun split_tuple' _ _ [] = ([], [])
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1579
			| split_tuple' is i (T::Ts) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1580
			(if i mem is then apfst else apsnd) (cons T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1581
				(split_tuple' is (i+1) Ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1582
	in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1583
	  split_tuple' is 1 (HOLogic.strip_tupleT T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1584
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1585
	
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1586
fun mk_arg xin xout pis T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1587
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1588
	  val n = length (HOLogic.strip_tupleT T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1589
		val ni = length pis
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1590
	  fun mk_proj i j t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1591
		  (if i = j then I else HOLogic.mk_fst)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1592
			  (funpow (i - 1) HOLogic.mk_snd t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1593
	  fun mk_arg' i (si, so) = if i mem pis then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1594
		    (mk_proj si ni xin, (si+1, so))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1595
		  else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1596
			  (mk_proj so (n - ni) xout, (si, so+1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1597
	  val (args, _) = fold_map mk_arg' (1 upto n) (1, 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1598
	in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1599
	  HOLogic.mk_tuple args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1600
	end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1601
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1602
fun create_definitions preds (name, modes) thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1603
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1604
    val compfuns = PredicateCompFuns.compfuns
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1605
    val T = AList.lookup (op =) preds name |> the
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1606
    fun create_definition (mode as (iss, is)) thy = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1607
      val mode_cname = create_constname_of_mode thy "" name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1608
      val mode_cbasename = Long_Name.base_name mode_cname
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1609
      val Ts = binder_types T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1610
      val (Ts1, Ts2) = chop (length iss) Ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1611
      val (Us1, Us2) =  split_smodeT is Ts2
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1612
      val Ts1' = map2 (fn NONE => I | SOME is => funT_of compfuns ([], is)) iss Ts1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1613
      val funT = (Ts1' @ Us1) ---> (mk_predT compfuns (mk_tupleT Us2))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1614
      val names = Name.variant_list []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1615
        (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1616
			(* old *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1617
			(*
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1618
		  val xs = map Free (names ~~ (Ts1' @ Ts2))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1619
      val (xparams, xargs) = chop (length iss) xs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1620
      val (xins, xouts) = split_smode is xargs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1621
			*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1622
			(* new *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1623
			val param_names = Name.variant_list []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1624
			  (map (fn i => "x" ^ string_of_int i) (1 upto (length Ts1')))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1625
		  val xparams = map Free (param_names ~~ Ts1')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1626
      fun mk_vars (i, T) names =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1627
			  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1628
				  val vname = Name.variant names ("x" ^ string_of_int (length Ts1' + i))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1629
				in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1630
					case AList.lookup (op =) is i of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1631
						 NONE => ((([], [Free (vname, T)]), Free (vname, T)), vname :: names)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1632
					 | SOME NONE => ((([Free (vname, T)], []), Free (vname, T)), vname :: names)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1633
        	 | SOME (SOME pis) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1634
					   let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1635
						   val (Tins, Touts) = split_tupleT pis T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1636
							 val name_in = Name.variant names ("x" ^ string_of_int (length Ts1' + i) ^ "in")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1637
							 val name_out = Name.variant names ("x" ^ string_of_int (length Ts1' + i) ^ "out")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1638
						   val xin = Free (name_in, HOLogic.mk_tupleT Tins)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1639
							 val xout = Free (name_out, HOLogic.mk_tupleT Touts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1640
							 val xarg = mk_arg xin xout pis T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1641
						 in (((if null Tins then [] else [xin], if null Touts then [] else [xout]), xarg), name_in :: name_out :: names) end
32669
462b1dd67a58 added context free grammar example; removed dead code; adapted to work without quick and dirty mode; fixed typo
bulwahn
parents: 32668
diff changeset
  1642
						 end
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1643
   	  val (xinoutargs, names) = fold_map mk_vars ((1 upto (length Ts2)) ~~ Ts2) param_names
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1644
      val (xinout, xargs) = split_list xinoutargs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1645
			val (xins, xouts) = pairself flat (split_list xinout)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1646
			val (xparams', names') = fold_map (mk_Eval_of NONE) ((xparams ~~ Ts1) ~~ iss) names
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1647
      fun mk_split_lambda [] t = lambda (Free (Name.variant names' "x", HOLogic.unitT)) t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1648
        | mk_split_lambda [x] t = lambda x t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1649
        | mk_split_lambda xs t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1650
        let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1651
          fun mk_split_lambda' (x::y::[]) t = HOLogic.mk_split (lambda x (lambda y t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1652
            | mk_split_lambda' (x::xs) t = HOLogic.mk_split (lambda x (mk_split_lambda' xs t))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1653
        in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1654
          mk_split_lambda' xs t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1655
        end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1656
      val predterm = PredicateCompFuns.mk_Enum (mk_split_lambda xouts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1657
        (list_comb (Const (name, T), xparams' @ xargs)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1658
      val lhs = list_comb (Const (mode_cname, funT), xparams @ xins)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1659
      val def = Logic.mk_equals (lhs, predterm)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1660
      val ([definition], thy') = thy |>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1661
        Sign.add_consts_i [(Binding.name mode_cbasename, funT, NoSyn)] |>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1662
        PureThy.add_defs false [((Binding.name (mode_cbasename ^ "_def"), def), [])]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1663
      val (intro, elim) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1664
        create_intro_elim_rule mode definition mode_cname funT (Const (name, T)) thy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1665
      in thy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1666
			  |> add_predfun name mode (mode_cname, definition, intro, elim)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1667
        |> PureThy.store_thm (Binding.name (mode_cbasename ^ "I"), intro) |> snd
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1668
        |> PureThy.store_thm (Binding.name (mode_cbasename ^ "E"), elim)  |> snd
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1669
        |> Theory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1670
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1671
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1672
    fold create_definition modes thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1673
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1674
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1675
fun sizelim_create_definitions preds (name, modes) thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1676
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1677
    val T = AList.lookup (op =) preds name |> the
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1678
    fun create_definition mode thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1679
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1680
        val mode_cname = create_constname_of_mode thy "sizelim_" name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1681
        val funT = sizelim_funT_of PredicateCompFuns.compfuns mode T
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1682
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1683
        thy |> Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_cname), funT, NoSyn)]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1684
        |> set_sizelim_function_name name mode mode_cname 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1685
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1686
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1687
    fold create_definition modes thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1688
  end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1689
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1690
fun generator_funT_of (iss, is) T =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1691
  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1692
    val Ts = binder_types T
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1693
    val (paramTs, (inargTs, outargTs)) = split_modeT (iss, is) Ts
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1694
    val paramTs' = map2 (fn SOME is => sizelim_funT_of PredicateCompFuns.compfuns ([], is) | NONE => I) iss paramTs 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1695
  in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1696
    (paramTs' @ inargTs @ [@{typ "code_numeral"}]) ---> (mk_predT RPredCompFuns.compfuns (mk_tupleT outargTs))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1697
  end
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1698
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1699
fun rpred_create_definitions preds (name, modes) thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1700
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1701
    val T = AList.lookup (op =) preds name |> the
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1702
    fun create_definition mode thy =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1703
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1704
        val mode_cname = create_constname_of_mode thy "gen_" name mode
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  1705
        val funT = generator_funT_of mode T
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1706
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1707
        thy |> Sign.add_consts_i [(Binding.name (Long_Name.base_name mode_cname), funT, NoSyn)]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1708
        |> set_generator_name name mode mode_cname 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1709
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1710
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1711
    fold create_definition modes thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1712
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1713
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1714
(* Proving equivalence of term *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1715
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1716
fun is_Type (Type _) = true
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1717
  | is_Type _ = false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1718
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1719
(* returns true if t is an application of an datatype constructor *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1720
(* which then consequently would be splitted *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1721
(* else false *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1722
fun is_constructor thy t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1723
  if (is_Type (fastype_of t)) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1724
    (case Datatype.get_info thy ((fst o dest_Type o fastype_of) t) of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1725
      NONE => false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1726
    | SOME info => (let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1727
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1728
      val (c, _) = strip_comb t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1729
      in (case c of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1730
        Const (name, _) => name mem_string constr_consts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1731
        | _ => false) end))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1732
  else false
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1733
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1734
(* MAJOR FIXME:  prove_params should be simple
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1735
 - different form of introrule for parameters ? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1736
fun prove_param thy (NONE, t) = TRY (rtac @{thm refl} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1737
  | prove_param thy (m as SOME (Mode (mode, is, ms)), t) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1738
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1739
    val  (f, args) = strip_comb (Envir.eta_contract t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1740
    val (params, _) = chop (length ms) args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1741
    val f_tac = case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1742
      Const (name, T) => simp_tac (HOL_basic_ss addsimps 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1743
         ([@{thm eval_pred}, (predfun_definition_of thy name mode),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1744
         @{thm "split_eta"}, @{thm "split_beta"}, @{thm "fst_conv"},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1745
				 @{thm "snd_conv"}, @{thm pair_collapse}, @{thm "Product_Type.split_conv"}])) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1746
    | Free _ => TRY (rtac @{thm refl} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1747
    | Abs _ => error "prove_param: No valid parameter term"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1748
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1749
    REPEAT_DETERM (etac @{thm thin_rl} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1750
    THEN REPEAT_DETERM (rtac @{thm ext} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1751
    THEN print_tac "prove_param"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1752
    THEN f_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1753
    THEN print_tac "after simplification in prove_args"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1754
    THEN (EVERY (map (prove_param thy) (ms ~~ params)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1755
    THEN (REPEAT_DETERM (atac 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1756
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1757
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1758
fun prove_expr thy (Mode (mode, is, ms), t, us) (premposition : int) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1759
  case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1760
    (Const (name, T), args) =>  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1761
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1762
        val introrule = predfun_intro_of thy name mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1763
        val (args1, args2) = chop (length ms) args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1764
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1765
        rtac @{thm bindI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1766
        THEN print_tac "before intro rule:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1767
        (* for the right assumption in first position *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1768
        THEN rotate_tac premposition 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1769
        THEN debug_tac (Display.string_of_thm (ProofContext.init thy) introrule)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1770
        THEN rtac introrule 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1771
        THEN print_tac "after intro rule"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1772
        (* work with parameter arguments *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1773
        THEN (atac 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1774
        THEN (print_tac "parameter goal")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1775
        THEN (EVERY (map (prove_param thy) (ms ~~ args1)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1776
        THEN (REPEAT_DETERM (atac 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1777
      end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1778
  | _ => rtac @{thm bindI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1779
	  THEN asm_full_simp_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1780
		  (HOL_basic_ss' addsimps [@{thm "split_eta"}, @{thm "split_beta"}, @{thm "fst_conv"},
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1781
				 @{thm "snd_conv"}, @{thm pair_collapse}]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1782
	  THEN (atac 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1783
	  THEN print_tac "after prove parameter call"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1784
		
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1785
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1786
fun SOLVED tac st = FILTER (fn st' => nprems_of st' = nprems_of st - 1) tac st; 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1787
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1788
fun SOLVEDALL tac st = FILTER (fn st' => nprems_of st' = 0) tac st
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1789
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1790
fun prove_match thy (out_ts : term list) = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1791
  fun get_case_rewrite t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1792
    if (is_constructor thy t) then let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1793
      val case_rewrites = (#case_rewrites (Datatype.the_info thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1794
        ((fst o dest_Type o fastype_of) t)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1795
      in case_rewrites @ (flat (map get_case_rewrite (snd (strip_comb t)))) end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1796
    else []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1797
  val simprules = @{thm "unit.cases"} :: @{thm "prod.cases"} :: (flat (map get_case_rewrite out_ts))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1798
(* replace TRY by determining if it necessary - are there equations when calling compile match? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1799
in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1800
   (* make this simpset better! *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1801
  asm_full_simp_tac (HOL_basic_ss' addsimps simprules) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1802
  THEN print_tac "after prove_match:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1803
  THEN (DETERM (TRY (EqSubst.eqsubst_tac (ProofContext.init thy) [0] [@{thm "HOL.if_P"}] 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1804
         THEN (REPEAT_DETERM (rtac @{thm conjI} 1 THEN (SOLVED (asm_simp_tac HOL_basic_ss 1))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1805
         THEN (SOLVED (asm_simp_tac HOL_basic_ss 1)))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1806
  THEN print_tac "after if simplification"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1807
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1808
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1809
(* corresponds to compile_fun -- maybe call that also compile_sidecond? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1810
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1811
fun prove_sidecond thy modes t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1812
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1813
    fun preds_of t nameTs = case strip_comb t of 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1814
      (f as Const (name, T), args) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1815
        if AList.defined (op =) modes name then (name, T) :: nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1816
          else fold preds_of args nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1817
      | _ => nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1818
    val preds = preds_of t []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1819
    val defs = map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1820
      (fn (pred, T) => predfun_definition_of thy pred
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1821
        ([], map (rpair NONE) (1 upto (length (binder_types T)))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1822
        preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1823
  in 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1824
    (* remove not_False_eq_True when simpset in prove_match is better *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1825
    simp_tac (HOL_basic_ss addsimps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1826
      (@{thms "HOL.simp_thms"} @ (@{thm not_False_eq_True} :: @{thm eval_pred} :: defs))) 1 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1827
    (* need better control here! *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1828
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1829
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1830
fun prove_clause thy nargs modes (iss, is) (_, clauses) (ts, moded_ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1831
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1832
    val (in_ts, clause_out_ts) = split_smode is ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1833
    fun prove_prems out_ts [] =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1834
      (prove_match thy out_ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1835
			THEN print_tac "before simplifying assumptions"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1836
      THEN asm_full_simp_tac HOL_basic_ss' 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1837
			THEN print_tac "before single intro rule"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1838
      THEN (rtac (if null clause_out_ts then @{thm singleI_unit} else @{thm singleI}) 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1839
    | prove_prems out_ts ((p, mode as Mode ((iss, is), _, param_modes)) :: ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1840
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1841
        val premposition = (find_index (equal p) clauses) + nargs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1842
        val rest_tac = (case p of Prem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1843
            let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1844
              val (_, out_ts''') = split_smode is us
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1845
              val rec_tac = prove_prems out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1846
            in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1847
              print_tac "before clause:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1848
              THEN asm_simp_tac HOL_basic_ss 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1849
              THEN print_tac "before prove_expr:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1850
              THEN prove_expr thy (mode, t, us) premposition
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1851
              THEN print_tac "after prove_expr:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1852
              THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1853
            end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1854
          | Negprem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1855
            let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1856
              val (_, out_ts''') = split_smode is us
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1857
              val rec_tac = prove_prems out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1858
              val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1859
              val (_, params) = strip_comb t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1860
            in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1861
              rtac @{thm bindI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1862
              THEN (if (is_some name) then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1863
                  simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, is)]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1864
                  THEN rtac @{thm not_predI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1865
                  THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1866
                  THEN (REPEAT_DETERM (atac 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1867
                  (* FIXME: work with parameter arguments *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1868
                  THEN (EVERY (map (prove_param thy) (param_modes ~~ params)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1869
                else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1870
                  rtac @{thm not_predI'} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1871
                  THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1872
              THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1873
            end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1874
          | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1875
           rtac @{thm bindI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1876
           THEN rtac @{thm if_predI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1877
           THEN print_tac "before sidecond:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1878
           THEN prove_sidecond thy modes t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1879
           THEN print_tac "after sidecond:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1880
           THEN prove_prems [] ps)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1881
      in (prove_match thy out_ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1882
          THEN rest_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1883
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1884
    val prems_tac = prove_prems in_ts moded_ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1885
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1886
    rtac @{thm bindI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1887
    THEN rtac @{thm singleI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1888
    THEN prems_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1889
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1890
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1891
fun select_sup 1 1 = []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1892
  | select_sup _ 1 = [rtac @{thm supI1}]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1893
  | select_sup n i = (rtac @{thm supI2})::(select_sup (n - 1) (i - 1));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1894
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1895
fun prove_one_direction thy clauses preds modes pred mode moded_clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1896
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1897
    val T = the (AList.lookup (op =) preds pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1898
    val nargs = length (binder_types T) - nparams_of thy pred
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1899
    val pred_case_rule = the_elim_of thy pred
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1900
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1901
    REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"}))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1902
		THEN print_tac "before applying elim rule"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1903
    THEN etac (predfun_elim_of thy pred mode) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1904
    THEN etac pred_case_rule 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1905
    THEN (EVERY (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1906
           (fn i => EVERY' (select_sup (length moded_clauses) i) i) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1907
             (1 upto (length moded_clauses))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1908
    THEN (EVERY (map2 (prove_clause thy nargs modes mode) clauses moded_clauses))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1909
    THEN print_tac "proved one direction"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1910
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1911
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1912
(** Proof in the other direction **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1913
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1914
fun prove_match2 thy out_ts = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1915
  fun split_term_tac (Free _) = all_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1916
    | split_term_tac t =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1917
      if (is_constructor thy t) then let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1918
        val info = Datatype.the_info thy ((fst o dest_Type o fastype_of) t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1919
        val num_of_constrs = length (#case_rewrites info)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1920
        (* special treatment of pairs -- because of fishing *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1921
        val split_rules = case (fst o dest_Type o fastype_of) t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1922
          "*" => [@{thm prod.split_asm}] 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1923
          | _ => PureThy.get_thms thy (((fst o dest_Type o fastype_of) t) ^ ".split_asm")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1924
        val (_, ts) = strip_comb t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1925
      in
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1926
        (print_tac ("Term " ^ (Syntax.string_of_term_global thy t) ^ 
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1927
          "splitting with rules \n" ^
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1928
        commas (map (Display.string_of_thm_global thy) split_rules)))
33115
f765c3234059 changed proof method to handle widen predicate in JinjaThreads
bulwahn
parents: 33114
diff changeset
  1929
        THEN TRY ((Splitter.split_asm_tac split_rules 1)
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1930
        THEN (print_tac "after splitting with split_asm rules")
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1931
        (* THEN (Simplifier.asm_full_simp_tac HOL_basic_ss 1)
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1932
          THEN (DETERM (TRY (etac @{thm Pair_inject} 1)))*)
33115
f765c3234059 changed proof method to handle widen predicate in JinjaThreads
bulwahn
parents: 33114
diff changeset
  1933
          THEN (REPEAT_DETERM_N (num_of_constrs - 1) (etac @{thm botE} 1 ORELSE etac @{thm botE} 2)))
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 33106
diff changeset
  1934
        THEN (assert_tac (Max_number_of_subgoals 2))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1935
        THEN (EVERY (map split_term_tac ts))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1936
      end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1937
    else all_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1938
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1939
    split_term_tac (mk_tuple out_ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1940
    THEN (DETERM (TRY ((Splitter.split_asm_tac [@{thm "split_if_asm"}] 1) THEN (etac @{thm botE} 2))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1941
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1942
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1943
(* VERY LARGE SIMILIRATIY to function prove_param 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1944
-- join both functions
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1945
*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1946
(* TODO: remove function *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1947
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1948
fun prove_param2 thy (NONE, t) = all_tac 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1949
  | prove_param2 thy (m as SOME (Mode (mode, is, ms)), t) = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1950
    val  (f, args) = strip_comb (Envir.eta_contract t)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1951
    val (params, _) = chop (length ms) args
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1952
    val f_tac = case f of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1953
        Const (name, T) => full_simp_tac (HOL_basic_ss addsimps 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1954
           (@{thm eval_pred}::(predfun_definition_of thy name mode)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1955
           :: @{thm "Product_Type.split_conv"}::[])) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1956
      | Free _ => all_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1957
      | _ => error "prove_param2: illegal parameter term"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1958
  in  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1959
    print_tac "before simplification in prove_args:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1960
    THEN f_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1961
    THEN print_tac "after simplification in prove_args"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1962
    THEN (EVERY (map (prove_param2 thy) (ms ~~ params)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1963
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1964
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1965
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1966
fun prove_expr2 thy (Mode (mode, is, ms), t) = 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1967
  (case strip_comb t of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1968
    (Const (name, T), args) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1969
      etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1970
      THEN (REPEAT_DETERM (CHANGED (rewtac @{thm "split_paired_all"})))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1971
      THEN print_tac "prove_expr2-before"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1972
      THEN (debug_tac (Syntax.string_of_term_global thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1973
        (prop_of (predfun_elim_of thy name mode))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1974
      THEN (etac (predfun_elim_of thy name mode) 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1975
      THEN print_tac "prove_expr2"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1976
      THEN (EVERY (map (prove_param2 thy) (ms ~~ args)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1977
      THEN print_tac "finished prove_expr2"      
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1978
    | _ => etac @{thm bindE} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1979
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1980
(* FIXME: what is this for? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1981
(* replace defined by has_mode thy pred *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1982
(* TODO: rewrite function *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1983
fun prove_sidecond2 thy modes t = let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1984
  fun preds_of t nameTs = case strip_comb t of 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1985
    (f as Const (name, T), args) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1986
      if AList.defined (op =) modes name then (name, T) :: nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1987
        else fold preds_of args nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1988
    | _ => nameTs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1989
  val preds = preds_of t []
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1990
  val defs = map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1991
    (fn (pred, T) => predfun_definition_of thy pred 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1992
      ([], map (rpair NONE) (1 upto (length (binder_types T)))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1993
      preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1994
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1995
   (* only simplify the one assumption *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1996
   full_simp_tac (HOL_basic_ss' addsimps @{thm eval_pred} :: defs) 1 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1997
   (* need better control here! *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1998
   THEN print_tac "after sidecond2 simplification"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  1999
   end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2000
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2001
fun prove_clause2 thy modes pred (iss, is) (ts, ps) i =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2002
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2003
    val pred_intro_rule = nth (intros_of thy pred) (i - 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2004
    val (in_ts, clause_out_ts) = split_smode is ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2005
    fun prove_prems2 out_ts [] =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2006
      print_tac "before prove_match2 - last call:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2007
      THEN prove_match2 thy out_ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2008
      THEN print_tac "after prove_match2 - last call:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2009
      THEN (etac @{thm singleE} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2010
      THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2011
      THEN (asm_full_simp_tac HOL_basic_ss' 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2012
      THEN (REPEAT_DETERM (etac @{thm Pair_inject} 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2013
      THEN (asm_full_simp_tac HOL_basic_ss' 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2014
      THEN SOLVED (print_tac "state before applying intro rule:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2015
      THEN (rtac pred_intro_rule 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2016
      (* How to handle equality correctly? *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2017
      THEN (print_tac "state before assumption matching")
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2018
      THEN (REPEAT (atac 1 ORELSE 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2019
         (CHANGED (asm_full_simp_tac (HOL_basic_ss' addsimps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2020
					 [@{thm split_eta}, @{thm "split_beta"}, @{thm "fst_conv"}, @{thm "snd_conv"}, @{thm pair_collapse}]) 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2021
          THEN print_tac "state after simp_tac:"))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2022
    | prove_prems2 out_ts ((p, mode as Mode ((iss, is), _, param_modes)) :: ps) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2023
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2024
        val rest_tac = (case p of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2025
          Prem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2026
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2027
            val (_, out_ts''') = split_smode is us
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2028
            val rec_tac = prove_prems2 out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2029
          in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2030
            (prove_expr2 thy (mode, t)) THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2031
          end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2032
        | Negprem (us, t) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2033
          let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2034
            val (_, out_ts''') = split_smode is us
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2035
            val rec_tac = prove_prems2 out_ts''' ps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2036
            val name = (case strip_comb t of (Const (c, _), _) => SOME c | _ => NONE)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2037
            val (_, params) = strip_comb t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2038
          in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2039
            print_tac "before neg prem 2"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2040
            THEN etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2041
            THEN (if is_some name then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2042
                full_simp_tac (HOL_basic_ss addsimps [predfun_definition_of thy (the name) (iss, is)]) 1 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2043
                THEN etac @{thm not_predE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2044
                THEN simp_tac (HOL_basic_ss addsimps [@{thm not_False_eq_True}]) 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2045
                THEN (EVERY (map (prove_param2 thy) (param_modes ~~ params)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2046
              else
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2047
                etac @{thm not_predE'} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2048
            THEN rec_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2049
          end 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2050
        | Sidecond t =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2051
          etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2052
          THEN etac @{thm if_predE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2053
          THEN prove_sidecond2 thy modes t 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2054
          THEN prove_prems2 [] ps)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2055
      in print_tac "before prove_match2:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2056
         THEN prove_match2 thy out_ts
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2057
         THEN print_tac "after prove_match2:"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2058
         THEN rest_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2059
      end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2060
    val prems_tac = prove_prems2 in_ts ps 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2061
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2062
    print_tac "starting prove_clause2"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2063
    THEN etac @{thm bindE} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2064
    THEN (etac @{thm singleE'} 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2065
    THEN (TRY (etac @{thm Pair_inject} 1))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2066
    THEN print_tac "after singleE':"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2067
    THEN prems_tac
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2068
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2069
 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2070
fun prove_other_direction thy modes pred mode moded_clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2071
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2072
    fun prove_clause clause i =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2073
      (if i < length moded_clauses then etac @{thm supE} 1 else all_tac)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2074
      THEN (prove_clause2 thy modes pred mode clause i)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2075
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2076
    (DETERM (TRY (rtac @{thm unit.induct} 1)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2077
     THEN (REPEAT_DETERM (CHANGED (rewtac @{thm split_paired_all})))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2078
     THEN (rtac (predfun_intro_of thy pred mode) 1)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2079
     THEN (REPEAT_DETERM (rtac @{thm refl} 2))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2080
     THEN (EVERY (map2 prove_clause moded_clauses (1 upto (length moded_clauses))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2081
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2082
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2083
(** proof procedure **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2084
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2085
fun prove_pred thy clauses preds modes pred mode (moded_clauses, compiled_term) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2086
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2087
    val ctxt = ProofContext.init thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2088
    val clauses = the (AList.lookup (op =) clauses pred)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2089
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2090
    Goal.prove ctxt (Term.add_free_names compiled_term []) [] compiled_term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2091
      (if !do_proofs then
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2092
        (fn _ =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2093
        rtac @{thm pred_iffI} 1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2094
				THEN print_tac "after pred_iffI"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2095
        THEN prove_one_direction thy clauses preds modes pred mode moded_clauses
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2096
        THEN print_tac "proved one direction"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2097
        THEN prove_other_direction thy modes pred mode moded_clauses
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2098
        THEN print_tac "proved other direction")
32673
d5db9cf85401 replaced sorry by oops; removed old debug functions in predicate compiler
bulwahn
parents: 32672
diff changeset
  2099
      else (fn _ => setmp quick_and_dirty true SkipProof.cheat_tac thy))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2100
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2101
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2102
(* composition of mode inference, definition, compilation and proof *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2103
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2104
(** auxillary combinators for table of preds and modes **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2105
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2106
fun map_preds_modes f preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2107
  map (fn (pred, modes) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2108
    (pred, map (fn (mode, value) => (mode, f pred mode value)) modes)) preds_modes_table
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2109
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2110
fun join_preds_modes table1 table2 =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2111
  map_preds_modes (fn pred => fn mode => fn value =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2112
    (value, the (AList.lookup (op =) (the (AList.lookup (op =) table2 pred)) mode))) table1
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2113
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2114
fun maps_modes preds_modes_table =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2115
  map (fn (pred, modes) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2116
    (pred, map (fn (mode, value) => value) modes)) preds_modes_table  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2117
    
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2118
fun compile_preds compfuns mk_fun_of use_size thy all_vs param_vs preds moded_clauses =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2119
  map_preds_modes (fn pred => compile_pred compfuns mk_fun_of use_size thy all_vs param_vs pred
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2120
      (the (AList.lookup (op =) preds pred))) moded_clauses  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2121
  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2122
fun prove thy clauses preds modes moded_clauses compiled_terms =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2123
  map_preds_modes (prove_pred thy clauses preds modes)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2124
    (join_preds_modes moded_clauses compiled_terms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2125
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2126
fun prove_by_skip thy _ _ _ _ compiled_terms =
32669
462b1dd67a58 added context free grammar example; removed dead code; adapted to work without quick and dirty mode; fixed typo
bulwahn
parents: 32668
diff changeset
  2127
  map_preds_modes (fn pred => fn mode => fn t => Drule.standard (setmp quick_and_dirty true (SkipProof.make_thm thy) t))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2128
    compiled_terms
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2129
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2130
fun dest_prem thy params t =
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2131
  (case strip_comb t of
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2132
    (v as Free _, ts) => if v mem params then Prem (ts, v) else Sidecond t
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2133
  | (c as Const (@{const_name Not}, _), [t]) => (case dest_prem thy params t of          
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2134
      Prem (ts, t) => Negprem (ts, t)
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2135
    | Negprem _ => error ("Double negation not allowed in premise: " ^ (Syntax.string_of_term_global thy (c $ t))) 
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2136
    | Sidecond t => Sidecond (c $ t))
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2137
  | (c as Const (s, _), ts) =>
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2138
    if is_registered thy s then
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2139
      let val (ts1, ts2) = chop (nparams_of thy s) ts
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2140
      in Prem (ts2, list_comb (c, ts1)) end
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2141
    else Sidecond t
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2142
  | _ => Sidecond t)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2143
    
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2144
fun prepare_intrs thy prednames intros =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2145
  let
33112
6672184a736b added a few tricky examples with tuples; added alternative introduction rules for some constants; corrected mode analysis with negation; improved fetching of definitions
bulwahn
parents: 33109
diff changeset
  2146
    val ((_, intrs), _) = Variable.import false intros (ProofContext.init thy)
6672184a736b added a few tricky examples with tuples; added alternative introduction rules for some constants; corrected mode analysis with negation; improved fetching of definitions
bulwahn
parents: 33109
diff changeset
  2147
    val intrs = map prop_of intrs
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2148
    val nparams = nparams_of thy (hd prednames)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2149
    val extra_modes = all_modes_of thy |> filter_out (fn (name, _) => member (op =) prednames name)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2150
    val preds = distinct (op =) (map (dest_Const o fst o (strip_intro_concl nparams)) intrs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2151
    val _ $ u = Logic.strip_imp_concl (hd intrs);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2152
    val params = List.take (snd (strip_comb u), nparams);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2153
    val param_vs = maps term_vs params
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2154
    val all_vs = terms_vs intrs
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2155
    fun add_clause intr (clauses, arities) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2156
    let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2157
      val _ $ t = Logic.strip_imp_concl intr;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2158
      val (Const (name, T), ts) = strip_comb t;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2159
      val (ts1, ts2) = chop nparams ts;
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2160
      val prems = map (dest_prem thy params o HOLogic.dest_Trueprop) (Logic.strip_imp_prems intr);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2161
      val (Ts, Us) = chop nparams (binder_types T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2162
    in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2163
      (AList.update op = (name, these (AList.lookup op = clauses name) @
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2164
        [(ts2, prems)]) clauses,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2165
       AList.update op = (name, (map (fn U => (case strip_type U of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2166
                 (Rs as _ :: _, Type ("bool", [])) => SOME (length Rs)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2167
               | _ => NONE)) Ts,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2168
             length Us)) arities)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2169
    end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2170
    val (clauses, arities) = fold add_clause intrs ([], []);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2171
    fun modes_of_arities arities =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2172
      (map (fn (s, (ks, k)) => (s, cprod (cprods (map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2173
            (fn NONE => [NONE]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2174
              | SOME k' => map SOME (map (map (rpair NONE)) (subsets 1 k'))) ks),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2175
       map (map (rpair NONE)) (subsets 1 k)))) arities)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2176
    fun modes_of_typ T =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2177
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2178
        val (Ts, Us) = chop nparams (binder_types T)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2179
        fun all_smodes_of_typs Ts = cprods_subset (
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2180
          map_index (fn (i, U) =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2181
            case HOLogic.strip_tupleT U of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2182
              [] => [(i + 1, NONE)]
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2183
            | [U] => [(i + 1, NONE)]
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2184
            | Us =>  (i + 1, NONE) ::
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2185
              (map (pair (i + 1) o SOME) ((subsets 1 (length Us)) \\ [[], 1 upto (length Us)])))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2186
          Ts)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2187
      in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2188
        cprod (cprods (map (fn T => case strip_type T of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2189
          (Rs as _ :: _, Type ("bool", [])) => map SOME (all_smodes_of_typs Rs) | _ => [NONE]) Ts),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2190
           all_smodes_of_typs Us)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2191
      end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2192
    val all_modes = map (fn (s, T) => (s, modes_of_typ T)) preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2193
  in (preds, nparams, all_vs, param_vs, extra_modes, clauses, all_modes) end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2194
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2195
fun check_format_of_intro_rule thy intro =
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2196
  let
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2197
    val concl = Logic.strip_imp_concl (prop_of intro)
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2198
    val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2199
    val params = List.take (args, nparams_of thy (fst (dest_Const p)))
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2200
    fun check_arg arg = case HOLogic.strip_tupleT (fastype_of arg) of
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2201
      (Ts as _ :: _ :: _) =>
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2202
        if (length (HOLogic.strip_tuple arg) = length Ts) then true
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2203
        else
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2204
        error ("Format of introduction rule is invalid: tuples must be expanded:"
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2205
        ^ (Syntax.string_of_term_global thy arg) ^ " in " ^
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2206
        (Display.string_of_thm_global thy intro)) 
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2207
      | _ => true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2208
    val prems = Logic.strip_imp_prems (prop_of intro)
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2209
    fun check_prem (Prem (args, _)) = forall check_arg args
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2210
      | check_prem (Negprem (args, _)) = forall check_arg args
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2211
      | check_prem _ = true
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2212
  in
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2213
    forall check_arg args andalso
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2214
    forall (check_prem o dest_prem thy params o HOLogic.dest_Trueprop) prems
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2215
  end
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2216
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
  2217
33106
7a1636c3ffc9 extended core of predicate compiler to expand tuples in introduction rules
bulwahn
parents: 32740
diff changeset
  2218
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2219
(** main function of predicate compiler **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2220
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2221
fun add_equations_of steps expected_modes prednames thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2222
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2223
    val _ = Output.tracing ("Starting predicate compiler for predicates " ^ commas prednames ^ "...")
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2224
    val _ = Output.tracing (commas (map (Display.string_of_thm_global thy) (maps (intros_of thy) prednames)))
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2225
      (*val _ = map (check_format_of_intro_rule thy) (maps (intros_of thy) prednames)*)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2226
    val (preds, nparams, all_vs, param_vs, extra_modes, clauses, all_modes) =
33113
0f6e30b87cf1 processing of tuples in introduction rules
bulwahn
parents: 33112
diff changeset
  2227
      prepare_intrs thy prednames (maps (intros_of thy) prednames)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2228
    val _ = Output.tracing "Infering modes..."
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2229
    val moded_clauses = #infer_modes steps thy extra_modes all_modes param_vs clauses 
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2230
    val modes : (string * ((int * int list option) list option list * (int * int list option) list) list) list = map (fn (p, mps) => (p, map fst mps)) moded_clauses
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2231
    val all_smodes : (((int * int list option) list) list) list = map (map snd) (map snd modes)
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2232
    val _ = case expected_modes of
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2233
      SOME ms => if not (forall (fn smodes => eq_set (map (map (rpair NONE)) ms, smodes)) all_smodes) then
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2234
      error ("expected modes were not inferred - " ^ commas (map string_of_smode (flat all_smodes))) else ()
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2235
      | NONE => ()
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2236
    val _ = print_modes modes
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2237
    val _ = print_moded_clauses thy moded_clauses
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2238
    val _ = Output.tracing "Defining executable functions..."
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2239
    val thy' = fold (#create_definitions steps preds) modes thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2240
      |> Theory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2241
    val _ = Output.tracing "Compiling equations..."
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2242
    val compiled_terms =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2243
      (#compile_preds steps) thy' all_vs param_vs preds moded_clauses
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2244
    val _ = print_compiled_terms thy' compiled_terms
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2245
    val _ = Output.tracing "Proving equations..."
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2246
    val result_thms = #prove steps thy' clauses preds (extra_modes @ modes)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2247
      moded_clauses compiled_terms
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2248
    val qname = #qname steps
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2249
    (* val attrib = gn thy => Attrib.attribute_i thy Code.add_eqn_attrib *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2250
    val attrib = fn thy => Attrib.attribute_i thy (Attrib.internal (K (Thm.declaration_attribute
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2251
      (fn thm => Context.mapping (Code.add_eqn thm) I))))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2252
    val thy'' = fold (fn (name, result_thms) => fn thy => snd (PureThy.add_thmss
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2253
      [((Binding.qualify true (Long_Name.base_name name) (Binding.name qname), result_thms),
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2254
        [attrib thy ])] thy))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2255
      (maps_modes result_thms) thy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2256
      |> Theory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2257
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2258
    thy''
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2259
  end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2260
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2261
fun extend' value_of edges_of key (G, visited) =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2262
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2263
    val (G', v) = case try (Graph.get_node G) key of
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2264
        SOME v => (G, v)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2265
      | NONE => (Graph.new_node (key, value_of key) G, value_of key)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2266
    val (G'', visited') = fold (extend' value_of edges_of) (edges_of (key, v) \\ visited)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2267
      (G', key :: visited) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2268
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2269
    (fold (Graph.add_edge o (pair key)) (edges_of (key, v)) G'', visited')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2270
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2271
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2272
fun extend value_of edges_of key G = fst (extend' value_of edges_of key (G, [])) 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2273
  
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2274
fun gen_add_equations steps expected_modes names thy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2275
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2276
    val thy' = PredData.map (fold (extend (fetch_pred_data thy) (depending_preds_of thy)) names) thy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2277
      |> Theory.checkpoint;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2278
    fun strong_conn_of gr keys =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2279
      Graph.strong_conn (Graph.subgraph (member (op =) (Graph.all_succs gr keys)) gr)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2280
    val scc = strong_conn_of (PredData.get thy') names
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2281
    val thy'' = fold_rev
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2282
      (fn preds => fn thy =>
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2283
        if #are_not_defined steps thy preds then add_equations_of steps expected_modes preds thy else thy)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2284
      scc thy' |> Theory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2285
  in thy'' end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2286
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2287
(* different instantiantions of the predicate compiler *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2288
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2289
val add_equations = gen_add_equations
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2290
  {infer_modes = infer_modes,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2291
  create_definitions = create_definitions,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2292
  compile_preds = compile_preds PredicateCompFuns.compfuns mk_fun_of false,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2293
  prove = prove,
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2294
  are_not_defined = fn thy => forall (null o modes_of thy),
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2295
  qname = "equation"}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2296
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2297
val add_sizelim_equations = gen_add_equations
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2298
  {infer_modes = infer_modes,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2299
  create_definitions = sizelim_create_definitions,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2300
  compile_preds = compile_preds PredicateCompFuns.compfuns mk_sizelim_fun_of true,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2301
  prove = prove_by_skip,
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2302
  are_not_defined = fn thy => forall (null o sizelim_modes_of thy),
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2303
  qname = "sizelim_equation"
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2304
  }
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2305
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2306
val add_quickcheck_equations = gen_add_equations
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2307
  {infer_modes = infer_modes_with_generator,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2308
  create_definitions = rpred_create_definitions,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2309
  compile_preds = compile_preds RPredCompFuns.compfuns mk_generator_of true,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2310
  prove = prove_by_skip,
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2311
  are_not_defined = fn thy => forall (null o rpred_modes_of thy),
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2312
  qname = "rpred_equation"}
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2313
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2314
(** user interface **)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2315
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2316
(* code_pred_intro attribute *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2317
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2318
fun attrib f = Thm.declaration_attribute (fn thm => Context.mapping (f thm) I);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2319
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2320
val code_pred_intros_attrib = attrib add_intro;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2321
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2322
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2323
(*FIXME
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2324
- Naming of auxiliary rules necessary?
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2325
- add default code equations P x y z = P_i_i_i x y z
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2326
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2327
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2328
val setup = PredData.put (Graph.empty) #>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2329
  Attrib.setup @{binding code_pred_intros} (Scan.succeed (attrib add_intro))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2330
    "adding alternative introduction rules for code generation of inductive predicates"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2331
(*  Attrib.setup @{binding code_ind_cases} (Scan.succeed add_elim_attrib)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2332
    "adding alternative elimination rules for code generation of inductive predicates";
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2333
    *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2334
  (*FIXME name discrepancy in attribs and ML code*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2335
  (*FIXME intros should be better named intro*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2336
  (*FIXME why distinguished attribute for cases?*)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2337
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2338
(* TODO: make TheoryDataFun to GenericDataFun & remove duplication of local theory and theory *)
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2339
fun generic_code_pred prep_const modes rpred raw_const lthy =
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2340
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2341
    val thy = ProofContext.theory_of lthy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2342
    val const = prep_const thy raw_const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2343
    val lthy' = LocalTheory.theory (PredData.map
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2344
        (extend (fetch_pred_data thy) (depending_preds_of thy) const)) lthy
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2345
      |> LocalTheory.checkpoint
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2346
    val thy' = ProofContext.theory_of lthy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2347
    val preds = Graph.all_preds (PredData.get thy') [const] |> filter_out (has_elim thy')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2348
    fun mk_cases const =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2349
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2350
        val nparams = nparams_of thy' const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2351
        val intros = intros_of thy' const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2352
      in mk_casesrule lthy' nparams intros end  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2353
    val cases_rules = map mk_cases preds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2354
    val cases =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2355
      map (fn case_rule => RuleCases.Case {fixes = [],
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2356
        assumes = [("", Logic.strip_imp_prems case_rule)],
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2357
        binds = [], cases = []}) cases_rules
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2358
    val case_env = map2 (fn p => fn c => (Long_Name.base_name p, SOME c)) preds cases
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2359
    val lthy'' = lthy'
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2360
      |> fold Variable.auto_fixes cases_rules 
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2361
      |> ProofContext.add_cases true case_env
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2362
    fun after_qed thms goal_ctxt =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2363
      let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2364
        val global_thms = ProofContext.export goal_ctxt
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2365
          (ProofContext.init (ProofContext.theory_of goal_ctxt)) (map the_single thms)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2366
      in
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2367
        goal_ctxt |> LocalTheory.theory (fold set_elim global_thms #>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2368
          (if rpred then
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2369
            (add_equations NONE [const] #>
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2370
             add_sizelim_equations NONE [const] #> add_quickcheck_equations NONE [const])
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33113
diff changeset
  2371
           else add_equations modes [const]))
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2372
      end  
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2373
  in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2374
    Proof.theorem_i NONE after_qed (map (single o (rpair [])) cases_rules) lthy''
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2375
  end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2376
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2377
val code_pred = generic_code_pred (K I);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2378
val code_pred_cmd = generic_code_pred Code.read_const
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2379
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2380
(* transformation for code generation *)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2381
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32674
diff changeset
  2382
val eval_ref = Unsynchronized.ref (NONE : (unit -> term Predicate.pred) option);
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2383
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2384
(*FIXME turn this into an LCF-guarded preprocessor for comprehensions*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2385
fun analyze_compr thy t_compr =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2386
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2387
    val split = case t_compr of (Const (@{const_name Collect}, _) $ t) => t
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2388
      | _ => error ("Not a set comprehension: " ^ Syntax.string_of_term_global thy t_compr);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2389
    val (body, Ts, fp) = HOLogic.strip_psplits split;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2390
    val (pred as Const (name, T), all_args) = strip_comb body;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2391
    val (params, args) = chop (nparams_of thy name) all_args;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2392
    val user_mode = map_filter I (map_index
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2393
      (fn (i, t) => case t of Bound j => if j < length Ts then NONE
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2394
        else SOME (i+1) | _ => SOME (i+1)) args); (*FIXME dangling bounds should not occur*)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2395
    val user_mode' = map (rpair NONE) user_mode
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2396
    val modes = filter (fn Mode (_, is, _) => is = user_mode')
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2397
      (modes_of_term (all_modes_of thy) (list_comb (pred, params)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2398
    val m = case modes
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2399
     of [] => error ("No mode possible for comprehension "
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2400
                ^ Syntax.string_of_term_global thy t_compr)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2401
      | [m] => m
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2402
      | m :: _ :: _ => (warning ("Multiple modes possible for comprehension "
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2403
                ^ Syntax.string_of_term_global thy t_compr); m);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2404
    val (inargs, outargs) = split_smode user_mode' args;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2405
    val t_pred = list_comb (compile_expr NONE NONE thy (m, list_comb (pred, params)), inargs);
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2406
    val t_eval = if null outargs then t_pred else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2407
      let
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2408
        val outargs_bounds = map (fn Bound i => i) outargs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2409
        val outargsTs = map (nth Ts) outargs_bounds;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2410
        val T_pred = HOLogic.mk_tupleT outargsTs;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2411
        val T_compr = HOLogic.mk_ptupleT fp Ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2412
        val arrange_bounds = map_index I outargs_bounds
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2413
          |> sort (prod_ord (K EQUAL) int_ord)
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2414
          |> map fst;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2415
        val arrange = funpow (length outargs_bounds - 1) HOLogic.mk_split
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2416
          (Term.list_abs (map (pair "") outargsTs,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2417
            HOLogic.mk_ptuple fp T_compr (map Bound arrange_bounds)))
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2418
      in mk_map PredicateCompFuns.compfuns T_pred T_compr arrange t_pred end
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2419
  in t_eval end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2420
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2421
fun eval thy t_compr =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2422
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2423
    val t = analyze_compr thy t_compr;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2424
    val T = dest_predT PredicateCompFuns.compfuns (fastype_of t);
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2425
    val t' = mk_map PredicateCompFuns.compfuns T HOLogic.termT (HOLogic.term_of_const T) t;
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents: 32667
diff changeset
  2426
  in (T, Code_ML.eval NONE ("Predicate_Compile_Core.eval_ref", eval_ref) Predicate.map thy t' []) end;
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2427
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2428
fun values ctxt k t_compr =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2429
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2430
    val thy = ProofContext.theory_of ctxt;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2431
    val (T, t) = eval thy t_compr;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2432
    val setT = HOLogic.mk_setT T;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2433
    val (ts, _) = Predicate.yieldn k t;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2434
    val elemsT = HOLogic.mk_set T ts;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2435
  in if k = ~1 orelse length ts < k then elemsT
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2436
    else Const (@{const_name Set.union}, setT --> setT --> setT) $ elemsT $ t_compr
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2437
  end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2438
  (*
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2439
fun random_values ctxt k t = 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2440
  let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2441
    val thy = ProofContext.theory_of ctxt
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2442
    val _ = 
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2443
  in
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2444
  end;
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32671
diff changeset
  2445
  *)
32667
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2446
fun values_cmd modes k raw_t state =
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2447
  let
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2448
    val ctxt = Toplevel.context_of state;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2449
    val t = Syntax.read_term ctxt raw_t;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2450
    val t' = values ctxt k t;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2451
    val ty' = Term.type_of t';
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2452
    val ctxt' = Variable.auto_fixes t' ctxt;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2453
    val p = PrintMode.with_modes modes (fn () =>
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2454
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2455
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ();
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2456
  in Pretty.writeln p end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2457
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2458
local structure P = OuterParse in
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2459
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2460
val opt_modes = Scan.optional (P.$$$ "(" |-- P.!!! (Scan.repeat1 P.xname --| P.$$$ ")")) [];
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2461
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2462
val _ = OuterSyntax.improper_command "values" "enumerate and print comprehensions" OuterKeyword.diag
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2463
  (opt_modes -- Scan.optional P.nat ~1 -- P.term
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2464
    >> (fn ((modes, k), t) => Toplevel.no_timing o Toplevel.keep
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2465
        (values_cmd modes k t)));
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2466
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2467
end;
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2468
09546e654222 moved predicate compiler to Tools
bulwahn
parents:
diff changeset
  2469
end;