src/HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
author bulwahn
Thu Oct 21 19:13:06 2010 +0200 (2010-10-21)
changeset 40048 f3a46d524101
parent 39802 7cadad6a18cc
child 40049 75d9f57123d6
permissions -rw-r--r--
adding option smart_depth_limiting to predicate compiler
     1 (*  Title:      HOL/Tools/Predicate_Compile/predicate_compile_aux.ML
     2     Author:     Lukas Bulwahn, TU Muenchen
     3 
     4 Auxilary functions for predicate compiler.
     5 *)
     6 
     7 signature PREDICATE_COMPILE_AUX =
     8 sig
     9   (* general functions *)
    10   val apfst3 : ('a -> 'd) -> 'a * 'b * 'c -> 'd * 'b * 'c
    11   val apsnd3 : ('b -> 'd) -> 'a * 'b * 'c -> 'a * 'd * 'c
    12   val aptrd3 : ('c -> 'd) -> 'a * 'b * 'c -> 'a * 'b * 'd
    13   val find_indices : ('a -> bool) -> 'a list -> int list
    14   val assert : bool -> unit
    15   (* mode *)
    16   datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
    17   val eq_mode : mode * mode -> bool
    18   val mode_ord: mode * mode -> order
    19   val list_fun_mode : mode list -> mode
    20   val strip_fun_mode : mode -> mode list
    21   val dest_fun_mode : mode -> mode list
    22   val dest_tuple_mode : mode -> mode list
    23   val all_modes_of_typ : typ -> mode list
    24   val all_smodes_of_typ : typ -> mode list
    25   val fold_map_aterms_prodT : ('a -> 'a -> 'a) -> (typ -> 'b -> 'a * 'b) -> typ -> 'b -> 'a * 'b
    26   val map_filter_prod : (term -> term option) -> term -> term option
    27   val replace_ho_args : mode -> term list -> term list -> term list
    28   val ho_arg_modes_of : mode -> mode list
    29   val ho_argsT_of : mode -> typ list -> typ list
    30   val ho_args_of : mode -> term list -> term list
    31   val ho_args_of_typ : typ -> term list -> term list
    32   val ho_argsT_of_typ : typ list -> typ list
    33   val split_map_mode : (mode -> term -> term option * term option)
    34     -> mode -> term list -> term list * term list
    35   val split_map_modeT : (mode -> typ -> typ option * typ option)
    36     -> mode -> typ list -> typ list * typ list
    37   val split_mode : mode -> term list -> term list * term list
    38   val split_modeT' : mode -> typ list -> typ list * typ list
    39   val string_of_mode : mode -> string
    40   val ascii_string_of_mode : mode -> string
    41   (* premises *)
    42   datatype indprem = Prem of term | Negprem of term | Sidecond of term
    43     | Generator of (string * typ)
    44   val dest_indprem : indprem -> term
    45   val map_indprem : (term -> term) -> indprem -> indprem
    46   (* general syntactic functions *)
    47   val conjuncts : term -> term list
    48   val is_equationlike : thm -> bool
    49   val is_pred_equation : thm -> bool
    50   val is_intro : string -> thm -> bool
    51   val is_predT : typ -> bool
    52   val is_constrt : theory -> term -> bool
    53   val is_constr : Proof.context -> string -> bool
    54   val focus_ex : term -> Name.context -> ((string * typ) list * term) * Name.context
    55   val strip_all : term -> (string * typ) list * term
    56   (* introduction rule combinators *)
    57   val map_atoms : (term -> term) -> term -> term
    58   val fold_atoms : (term -> 'a -> 'a) -> term -> 'a -> 'a
    59   val fold_map_atoms : (term -> 'a -> term * 'a) -> term -> 'a -> term * 'a
    60   val maps_premises : (term -> term list) -> term -> term
    61   val map_concl : (term -> term) -> term -> term
    62   val map_term : theory -> (term -> term) -> thm -> thm
    63   (* split theorems of case expressions *)
    64   val prepare_split_thm : Proof.context -> thm -> thm
    65   val find_split_thm : theory -> term -> thm option
    66   (* datastructures and setup for generic compilation *)
    67   datatype compilation_funs = CompilationFuns of {
    68     mk_predT : typ -> typ,
    69     dest_predT : typ -> typ,
    70     mk_bot : typ -> term,
    71     mk_single : term -> term,
    72     mk_bind : term * term -> term,
    73     mk_sup : term * term -> term,
    74     mk_if : term -> term,
    75     mk_iterate_upto : typ -> term * term * term -> term,
    76     mk_not : term -> term,
    77     mk_map : typ -> typ -> term -> term -> term
    78   };
    79   val mk_predT : compilation_funs -> typ -> typ
    80   val dest_predT : compilation_funs -> typ -> typ
    81   val mk_bot : compilation_funs -> typ -> term
    82   val mk_single : compilation_funs -> term -> term
    83   val mk_bind : compilation_funs -> term * term -> term
    84   val mk_sup : compilation_funs -> term * term -> term
    85   val mk_if : compilation_funs -> term -> term
    86   val mk_iterate_upto : compilation_funs -> typ -> term * term * term -> term
    87   val mk_not : compilation_funs -> term -> term
    88   val mk_map : compilation_funs -> typ -> typ -> term -> term -> term
    89   val funT_of : compilation_funs -> mode -> typ -> typ
    90   (* Different compilations *)
    91   datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
    92     | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
    93   val negative_compilation_of : compilation -> compilation
    94   val compilation_for_polarity : bool -> compilation -> compilation
    95   val string_of_compilation : compilation -> string
    96   val compilation_names : (string * compilation) list
    97   val non_random_compilations : compilation list
    98   val random_compilations : compilation list
    99   (* Different options for compiler *)
   100   datatype options = Options of {  
   101     expected_modes : (string * mode list) option,
   102     proposed_modes : (string * mode list) list,
   103     proposed_names : ((string * mode) * string) list,
   104     show_steps : bool,
   105     show_proof_trace : bool,
   106     show_intermediate_results : bool,
   107     show_mode_inference : bool,
   108     show_modes : bool,
   109     show_compilation : bool,
   110     show_caught_failures : bool,
   111     show_invalid_clauses : bool,
   112     skip_proof : bool,
   113     no_topmost_reordering : bool,
   114     function_flattening : bool,
   115     fail_safe_function_flattening : bool,
   116     specialise : bool,
   117     no_higher_order_predicate : string list,
   118     inductify : bool,
   119     detect_switches : bool,
   120     smart_depth_limiting : bool,
   121     compilation : compilation
   122   };
   123   val expected_modes : options -> (string * mode list) option
   124   val proposed_modes : options -> string -> mode list option
   125   val proposed_names : options -> string -> mode -> string option
   126   val show_steps : options -> bool
   127   val show_proof_trace : options -> bool
   128   val show_intermediate_results : options -> bool
   129   val show_mode_inference : options -> bool
   130   val show_modes : options -> bool
   131   val show_compilation : options -> bool
   132   val show_caught_failures : options -> bool
   133   val show_invalid_clauses : options -> bool
   134   val skip_proof : options -> bool
   135   val no_topmost_reordering : options -> bool
   136   val function_flattening : options -> bool
   137   val fail_safe_function_flattening : options -> bool
   138   val specialise : options -> bool
   139   val no_higher_order_predicate : options -> string list
   140   val is_inductify : options -> bool
   141   val detect_switches : options -> bool
   142   val smart_depth_limiting : options -> bool
   143   val compilation : options -> compilation
   144   val default_options : options
   145   val bool_options : string list
   146   val print_step : options -> string -> unit
   147   (* conversions *)
   148   val imp_prems_conv : conv -> conv
   149   (* simple transformations *)
   150   val split_conjuncts_in_assms : Proof.context -> thm -> thm
   151   val expand_tuples : theory -> thm -> thm
   152   val case_betapply : theory -> term -> term
   153   val eta_contract_ho_arguments : theory -> thm -> thm
   154   val remove_equalities : theory -> thm -> thm
   155   val remove_pointless_clauses : thm -> thm list
   156   val peephole_optimisation : theory -> thm -> thm option
   157   val define_quickcheck_predicate :
   158     term -> theory -> (((string * typ) * (string * typ) list) * thm) * theory 
   159 end;
   160 
   161 structure Predicate_Compile_Aux : PREDICATE_COMPILE_AUX =
   162 struct
   163 
   164 (* general functions *)
   165 
   166 fun apfst3 f (x, y, z) = (f x, y, z)
   167 fun apsnd3 f (x, y, z) = (x, f y, z)
   168 fun aptrd3 f (x, y, z) = (x, y, f z)
   169 
   170 fun comb_option f (SOME x1, SOME x2) = SOME (f (x1, x2))
   171   | comb_option f (NONE, SOME x2) = SOME x2
   172   | comb_option f (SOME x1, NONE) = SOME x1
   173   | comb_option f (NONE, NONE) = NONE
   174 
   175 fun map2_optional f (x :: xs) (y :: ys) = f x (SOME y) :: (map2_optional f xs ys)
   176   | map2_optional f (x :: xs) [] = (f x NONE) :: (map2_optional f xs [])
   177   | map2_optional f [] [] = []
   178 
   179 fun find_indices f xs =
   180   map_filter (fn (i, true) => SOME i | (i, false) => NONE) (map_index (apsnd f) xs)
   181 
   182 fun assert check = if check then () else raise Fail "Assertion failed!"
   183 
   184 (* mode *)
   185 
   186 datatype mode = Bool | Input | Output | Pair of mode * mode | Fun of mode * mode
   187 
   188 (* equality of instantiatedness with respect to equivalences:
   189   Pair Input Input == Input and Pair Output Output == Output *)
   190 fun eq_mode (Fun (m1, m2), Fun (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
   191   | eq_mode (Pair (m1, m2), Pair (m3, m4)) = eq_mode (m1, m3) andalso eq_mode (m2, m4)
   192   | eq_mode (Pair (m1, m2), Input) = eq_mode (m1, Input) andalso eq_mode (m2, Input)
   193   | eq_mode (Pair (m1, m2), Output) = eq_mode (m1, Output) andalso eq_mode (m2, Output)
   194   | eq_mode (Input, Pair (m1, m2)) = eq_mode (Input, m1) andalso eq_mode (Input, m2)
   195   | eq_mode (Output, Pair (m1, m2)) = eq_mode (Output, m1) andalso eq_mode (Output, m2)
   196   | eq_mode (Input, Input) = true
   197   | eq_mode (Output, Output) = true
   198   | eq_mode (Bool, Bool) = true
   199   | eq_mode _ = false
   200 
   201 fun mode_ord (Input, Output) = LESS
   202   | mode_ord (Output, Input) = GREATER
   203   | mode_ord (Input, Input) = EQUAL
   204   | mode_ord (Output, Output) = EQUAL
   205   | mode_ord (Bool, Bool) = EQUAL
   206   | mode_ord (Pair (m1, m2), Pair (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
   207   | mode_ord (Fun (m1, m2), Fun (m3, m4)) = prod_ord mode_ord mode_ord ((m1, m2), (m3, m4))
   208  
   209 fun list_fun_mode [] = Bool
   210   | list_fun_mode (m :: ms) = Fun (m, list_fun_mode ms)
   211 
   212 (* name: binder_modes? *)
   213 fun strip_fun_mode (Fun (mode, mode')) = mode :: strip_fun_mode mode'
   214   | strip_fun_mode Bool = []
   215   | strip_fun_mode _ = raise Fail "Bad mode for strip_fun_mode"
   216 
   217 (* name: strip_fun_mode? *)
   218 fun dest_fun_mode (Fun (mode, mode')) = mode :: dest_fun_mode mode'
   219   | dest_fun_mode mode = [mode]
   220 
   221 fun dest_tuple_mode (Pair (mode, mode')) = mode :: dest_tuple_mode mode'
   222   | dest_tuple_mode _ = []
   223 
   224 fun all_modes_of_typ' (T as Type ("fun", _)) = 
   225   let
   226     val (S, U) = strip_type T
   227   in
   228     if U = HOLogic.boolT then
   229       fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
   230         (map all_modes_of_typ' S) [Bool]
   231     else
   232       [Input, Output]
   233   end
   234   | all_modes_of_typ' (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
   235     map_product (curry Pair) (all_modes_of_typ' T1) (all_modes_of_typ' T2)
   236   | all_modes_of_typ' _ = [Input, Output]
   237 
   238 fun all_modes_of_typ (T as Type ("fun", _)) =
   239     let
   240       val (S, U) = strip_type T
   241     in
   242       if U = @{typ bool} then
   243         fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2)
   244           (map all_modes_of_typ' S) [Bool]
   245       else
   246         raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
   247     end
   248   | all_modes_of_typ @{typ bool} = [Bool]
   249   | all_modes_of_typ T =
   250     raise Fail "Invocation of all_modes_of_typ with a non-predicate type"
   251 
   252 fun all_smodes_of_typ (T as Type ("fun", _)) =
   253   let
   254     val (S, U) = strip_type T
   255     fun all_smodes (Type (@{type_name Product_Type.prod}, [T1, T2])) = 
   256       map_product (curry Pair) (all_smodes T1) (all_smodes T2)
   257       | all_smodes _ = [Input, Output]
   258   in
   259     if U = HOLogic.boolT then
   260       fold_rev (fn m1 => fn m2 => map_product (curry Fun) m1 m2) (map all_smodes S) [Bool]
   261     else
   262       raise Fail "invalid type for predicate"
   263   end
   264 
   265 fun ho_arg_modes_of mode =
   266   let
   267     fun ho_arg_mode (m as Fun _) =  [m]
   268       | ho_arg_mode (Pair (m1, m2)) = ho_arg_mode m1 @ ho_arg_mode m2
   269       | ho_arg_mode _ = []
   270   in
   271     maps ho_arg_mode (strip_fun_mode mode)
   272   end
   273 
   274 fun ho_args_of mode ts =
   275   let
   276     fun ho_arg (Fun _) (SOME t) = [t]
   277       | ho_arg (Fun _) NONE = raise Fail "mode and term do not match"
   278       | ho_arg (Pair (m1, m2)) (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
   279           ho_arg m1 (SOME t1) @ ho_arg m2 (SOME t2)
   280       | ho_arg (Pair (m1, m2)) NONE = ho_arg m1 NONE @ ho_arg m2 NONE
   281       | ho_arg _ _ = []
   282   in
   283     flat (map2_optional ho_arg (strip_fun_mode mode) ts)
   284   end
   285 
   286 fun ho_args_of_typ T ts =
   287   let
   288     fun ho_arg (T as Type("fun", [_,_])) (SOME t) = if body_type T = @{typ bool} then [t] else []
   289       | ho_arg (Type("fun", [_,_])) NONE = raise Fail "mode and term do not match"
   290       | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2]))
   291          (SOME (Const (@{const_name Pair}, _) $ t1 $ t2)) =
   292           ho_arg T1 (SOME t1) @ ho_arg T2 (SOME t2)
   293       | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) NONE =
   294           ho_arg T1 NONE @ ho_arg T2 NONE
   295       | ho_arg _ _ = []
   296   in
   297     flat (map2_optional ho_arg (binder_types T) ts)
   298   end
   299 
   300 fun ho_argsT_of_typ Ts =
   301   let
   302     fun ho_arg (T as Type("fun", [_,_])) = if body_type T = @{typ bool} then [T] else []
   303       | ho_arg (Type(@{type_name "Product_Type.prod"}, [T1, T2])) =
   304           ho_arg T1 @ ho_arg T2
   305       | ho_arg _ = []
   306   in
   307     maps ho_arg Ts
   308   end
   309   
   310 
   311 (* temporary function should be replaced by unsplit_input or so? *)
   312 fun replace_ho_args mode hoargs ts =
   313   let
   314     fun replace (Fun _, _) (arg' :: hoargs') = (arg', hoargs')
   315       | replace (Pair (m1, m2), Const (@{const_name Pair}, T) $ t1 $ t2) hoargs =
   316         let
   317           val (t1', hoargs') = replace (m1, t1) hoargs
   318           val (t2', hoargs'') = replace (m2, t2) hoargs'
   319         in
   320           (Const (@{const_name Pair}, T) $ t1' $ t2', hoargs'')
   321         end
   322       | replace (_, t) hoargs = (t, hoargs)
   323   in
   324     fst (fold_map replace (strip_fun_mode mode ~~ ts) hoargs)
   325   end
   326 
   327 fun ho_argsT_of mode Ts =
   328   let
   329     fun ho_arg (Fun _) T = [T]
   330       | ho_arg (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) = ho_arg m1 T1 @ ho_arg m2 T2
   331       | ho_arg _ _ = []
   332   in
   333     flat (map2 ho_arg (strip_fun_mode mode) Ts)
   334   end
   335 
   336 (* splits mode and maps function to higher-order argument types *)
   337 fun split_map_mode f mode ts =
   338   let
   339     fun split_arg_mode' (m as Fun _) t = f m t
   340       | split_arg_mode' (Pair (m1, m2)) (Const (@{const_name Pair}, _) $ t1 $ t2) =
   341         let
   342           val (i1, o1) = split_arg_mode' m1 t1
   343           val (i2, o2) = split_arg_mode' m2 t2
   344         in
   345           (comb_option HOLogic.mk_prod (i1, i2), comb_option HOLogic.mk_prod (o1, o2))
   346         end
   347       | split_arg_mode' m t =
   348         if eq_mode (m, Input) then (SOME t, NONE)
   349         else if eq_mode (m, Output) then (NONE,  SOME t)
   350         else raise Fail "split_map_mode: mode and term do not match"
   351   in
   352     (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) ts)
   353   end
   354 
   355 (* splits mode and maps function to higher-order argument types *)
   356 fun split_map_modeT f mode Ts =
   357   let
   358     fun split_arg_mode' (m as Fun _) T = f m T
   359       | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
   360         let
   361           val (i1, o1) = split_arg_mode' m1 T1
   362           val (i2, o2) = split_arg_mode' m2 T2
   363         in
   364           (comb_option HOLogic.mk_prodT (i1, i2), comb_option HOLogic.mk_prodT (o1, o2))
   365         end
   366       | split_arg_mode' Input T = (SOME T, NONE)
   367       | split_arg_mode' Output T = (NONE,  SOME T)
   368       | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
   369   in
   370     (pairself (map_filter I) o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
   371   end
   372 
   373 fun split_mode mode ts = split_map_mode (fn _ => fn _ => (NONE, NONE)) mode ts
   374 
   375 fun fold_map_aterms_prodT comb f (Type (@{type_name Product_Type.prod}, [T1, T2])) s =
   376   let
   377     val (x1, s') = fold_map_aterms_prodT comb f T1 s
   378     val (x2, s'') = fold_map_aterms_prodT comb f T2 s'
   379   in
   380     (comb x1 x2, s'')
   381   end
   382   | fold_map_aterms_prodT comb f T s = f T s
   383 
   384 fun map_filter_prod f (Const (@{const_name Pair}, _) $ t1 $ t2) =
   385   comb_option HOLogic.mk_prod (map_filter_prod f t1, map_filter_prod f t2)
   386   | map_filter_prod f t = f t
   387 
   388 (* obviously, split_mode' and split_modeT' do not match? where does that cause problems? *)
   389   
   390 fun split_modeT' mode Ts =
   391   let
   392     fun split_arg_mode' (Fun _) T = ([], [])
   393       | split_arg_mode' (Pair (m1, m2)) (Type (@{type_name Product_Type.prod}, [T1, T2])) =
   394         let
   395           val (i1, o1) = split_arg_mode' m1 T1
   396           val (i2, o2) = split_arg_mode' m2 T2
   397         in
   398           (i1 @ i2, o1 @ o2)
   399         end
   400       | split_arg_mode' Input T = ([T], [])
   401       | split_arg_mode' Output T = ([], [T])
   402       | split_arg_mode' _ _ = raise Fail "split_modeT': mode and type do not match"
   403   in
   404     (pairself flat o split_list) (map2 split_arg_mode' (strip_fun_mode mode) Ts)
   405   end
   406 
   407 fun string_of_mode mode =
   408   let
   409     fun string_of_mode1 Input = "i"
   410       | string_of_mode1 Output = "o"
   411       | string_of_mode1 Bool = "bool"
   412       | string_of_mode1 mode = "(" ^ (string_of_mode3 mode) ^ ")"
   413     and string_of_mode2 (Pair (m1, m2)) = string_of_mode3 m1 ^ " * " ^  string_of_mode2 m2
   414       | string_of_mode2 mode = string_of_mode1 mode
   415     and string_of_mode3 (Fun (m1, m2)) = string_of_mode2 m1 ^ " => " ^ string_of_mode3 m2
   416       | string_of_mode3 mode = string_of_mode2 mode
   417   in string_of_mode3 mode end
   418 
   419 fun ascii_string_of_mode mode' =
   420   let
   421     fun ascii_string_of_mode' Input = "i"
   422       | ascii_string_of_mode' Output = "o"
   423       | ascii_string_of_mode' Bool = "b"
   424       | ascii_string_of_mode' (Pair (m1, m2)) =
   425           "P" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
   426       | ascii_string_of_mode' (Fun (m1, m2)) = 
   427           "F" ^ ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Fun m2 ^ "B"
   428     and ascii_string_of_mode'_Fun (Fun (m1, m2)) =
   429           ascii_string_of_mode' m1 ^ (if m2 = Bool then "" else "_" ^ ascii_string_of_mode'_Fun m2)
   430       | ascii_string_of_mode'_Fun Bool = "B"
   431       | ascii_string_of_mode'_Fun m = ascii_string_of_mode' m
   432     and ascii_string_of_mode'_Pair (Pair (m1, m2)) =
   433           ascii_string_of_mode' m1 ^ ascii_string_of_mode'_Pair m2
   434       | ascii_string_of_mode'_Pair m = ascii_string_of_mode' m
   435   in ascii_string_of_mode'_Fun mode' end
   436 
   437 (* premises *)
   438 
   439 datatype indprem = Prem of term | Negprem of term | Sidecond of term
   440   | Generator of (string * typ);
   441 
   442 fun dest_indprem (Prem t) = t
   443   | dest_indprem (Negprem t) = t
   444   | dest_indprem (Sidecond t) = t
   445   | dest_indprem (Generator _) = raise Fail "cannot destruct generator"
   446 
   447 fun map_indprem f (Prem t) = Prem (f t)
   448   | map_indprem f (Negprem t) = Negprem (f t)
   449   | map_indprem f (Sidecond t) = Sidecond (f t)
   450   | map_indprem f (Generator (v, T)) = Generator (dest_Free (f (Free (v, T))))
   451 
   452 (* general syntactic functions *)
   453 
   454 (*Like dest_conj, but flattens conjunctions however nested*)
   455 fun conjuncts_aux (Const (@{const_name HOL.conj}, _) $ t $ t') conjs = conjuncts_aux t (conjuncts_aux t' conjs)
   456   | conjuncts_aux t conjs = t::conjs;
   457 
   458 fun conjuncts t = conjuncts_aux t [];
   459 
   460 fun is_equationlike_term (Const ("==", _) $ _ $ _) = true
   461   | is_equationlike_term (Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $ _ $ _)) = true
   462   | is_equationlike_term _ = false
   463   
   464 val is_equationlike = is_equationlike_term o prop_of 
   465 
   466 fun is_pred_equation_term (Const ("==", _) $ u $ v) =
   467   (fastype_of u = @{typ bool}) andalso (fastype_of v = @{typ bool})
   468   | is_pred_equation_term _ = false
   469   
   470 val is_pred_equation = is_pred_equation_term o prop_of 
   471 
   472 fun is_intro_term constname t =
   473   the_default false (try (fn t => case fst (strip_comb (HOLogic.dest_Trueprop (Logic.strip_imp_concl t))) of
   474     Const (c, _) => c = constname
   475   | _ => false) t)
   476   
   477 fun is_intro constname t = is_intro_term constname (prop_of t)
   478 
   479 fun is_pred thy constname = (body_type (Sign.the_const_type thy constname) = HOLogic.boolT);
   480 
   481 fun is_predT (T as Type("fun", [_, _])) = (snd (strip_type T) = @{typ bool})
   482   | is_predT _ = false
   483 
   484 (*** check if a term contains only constructor functions ***)
   485 (* TODO: another copy in the core! *)
   486 (* FIXME: constructor terms are supposed to be seen in the way the code generator
   487   sees constructors.*)
   488 fun is_constrt thy =
   489   let
   490     val cnstrs = flat (maps
   491       (map (fn (_, (Tname, _, cs)) => map (apsnd (rpair Tname o length)) cs) o #descr o snd)
   492       (Symtab.dest (Datatype.get_all thy)));
   493     fun check t = (case strip_comb t of
   494         (Var _, []) => true
   495       | (Free _, []) => true
   496       | (Const (s, T), ts) => (case (AList.lookup (op =) cnstrs s, body_type T) of
   497             (SOME (i, Tname), Type (Tname', _)) => length ts = i andalso Tname = Tname' andalso forall check ts
   498           | _ => false)
   499       | _ => false)
   500   in check end;
   501 
   502 fun is_funtype (Type ("fun", [_, _])) = true
   503   | is_funtype _ = false;
   504 
   505 fun is_Type (Type _) = true
   506   | is_Type _ = false
   507 
   508 (* returns true if t is an application of an datatype constructor *)
   509 (* which then consequently would be splitted *)
   510 (* else false *)
   511 (*
   512 fun is_constructor thy t =
   513   if (is_Type (fastype_of t)) then
   514     (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
   515       NONE => false
   516     | SOME info => (let
   517       val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
   518       val (c, _) = strip_comb t
   519       in (case c of
   520         Const (name, _) => name mem_string constr_consts
   521         | _ => false) end))
   522   else false
   523 *)
   524 
   525 val is_constr = Code.is_constr o ProofContext.theory_of;
   526 
   527 fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
   528 
   529 fun strip_ex (Const (@{const_name Ex}, _) $ Abs (x, T, t)) =
   530   let
   531     val (xTs, t') = strip_ex t
   532   in
   533     ((x, T) :: xTs, t')
   534   end
   535   | strip_ex t = ([], t)
   536 
   537 fun focus_ex t nctxt =
   538   let
   539     val ((xs, Ts), t') = apfst split_list (strip_ex t) 
   540     val (xs', nctxt') = Name.variants xs nctxt;
   541     val ps' = xs' ~~ Ts;
   542     val vs = map Free ps';
   543     val t'' = Term.subst_bounds (rev vs, t');
   544   in ((ps', t''), nctxt') end;
   545 
   546 (* introduction rule combinators *)
   547 
   548 fun map_atoms f intro = 
   549   let
   550     val (literals, head) = Logic.strip_horn intro
   551     fun appl t = (case t of
   552         (@{term Not} $ t') => HOLogic.mk_not (f t')
   553       | _ => f t)
   554   in
   555     Logic.list_implies
   556       (map (HOLogic.mk_Trueprop o appl o HOLogic.dest_Trueprop) literals, head)
   557   end
   558 
   559 fun fold_atoms f intro s =
   560   let
   561     val (literals, head) = Logic.strip_horn intro
   562     fun appl t s = (case t of
   563       (@{term Not} $ t') => f t' s
   564       | _ => f t s)
   565   in fold appl (map HOLogic.dest_Trueprop literals) s end
   566 
   567 fun fold_map_atoms f intro s =
   568   let
   569     val (literals, head) = Logic.strip_horn intro
   570     fun appl t s = (case t of
   571       (@{term Not} $ t') => apfst HOLogic.mk_not (f t' s)
   572       | _ => f t s)
   573     val (literals', s') = fold_map appl (map HOLogic.dest_Trueprop literals) s
   574   in
   575     (Logic.list_implies (map HOLogic.mk_Trueprop literals', head), s')
   576   end;
   577 
   578 fun map_premises f intro =
   579   let
   580     val (premises, head) = Logic.strip_horn intro
   581   in
   582     Logic.list_implies (map f premises, head)
   583   end
   584 
   585 fun map_filter_premises f intro =
   586   let
   587     val (premises, head) = Logic.strip_horn intro
   588   in
   589     Logic.list_implies (map_filter f premises, head)
   590   end
   591 
   592 fun maps_premises f intro =
   593   let
   594     val (premises, head) = Logic.strip_horn intro
   595   in
   596     Logic.list_implies (maps f premises, head)
   597   end
   598 
   599 fun map_concl f intro =
   600   let
   601     val (premises, head) = Logic.strip_horn intro
   602   in
   603     Logic.list_implies (premises, f head)
   604   end
   605 
   606 (* combinators to apply a function to all basic parts of nested products *)
   607 
   608 fun map_products f (Const (@{const_name Pair}, T) $ t1 $ t2) =
   609   Const (@{const_name Pair}, T) $ map_products f t1 $ map_products f t2
   610   | map_products f t = f t
   611 
   612 (* split theorems of case expressions *)
   613 
   614 fun prepare_split_thm ctxt split_thm =
   615     (split_thm RS @{thm iffD2})
   616     |> Local_Defs.unfold ctxt [@{thm atomize_conjL[symmetric]},
   617       @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
   618 
   619 fun find_split_thm thy (Const (name, T)) = Option.map #split (Datatype_Data.info_of_case thy name)
   620   | find_split_thm thy _ = NONE
   621 
   622 (* lifting term operations to theorems *)
   623 
   624 fun map_term thy f th =
   625   Skip_Proof.make_thm thy (f (prop_of th))
   626 
   627 (*
   628 fun equals_conv lhs_cv rhs_cv ct =
   629   case Thm.term_of ct of
   630     Const ("==", _) $ _ $ _ => Conv.arg_conv cv ct  
   631   | _ => error "equals_conv"  
   632 *)
   633 
   634 (* Different compilations *)
   635 
   636 datatype compilation = Pred | Depth_Limited | Random | Depth_Limited_Random | DSeq | Annotated
   637   | Pos_Random_DSeq | Neg_Random_DSeq | New_Pos_Random_DSeq | New_Neg_Random_DSeq
   638 
   639 fun negative_compilation_of Pos_Random_DSeq = Neg_Random_DSeq
   640   | negative_compilation_of Neg_Random_DSeq = Pos_Random_DSeq
   641   | negative_compilation_of New_Pos_Random_DSeq = New_Neg_Random_DSeq
   642   | negative_compilation_of New_Neg_Random_DSeq = New_Pos_Random_DSeq
   643   | negative_compilation_of c = c
   644   
   645 fun compilation_for_polarity false Pos_Random_DSeq = Neg_Random_DSeq
   646   | compilation_for_polarity false New_Pos_Random_DSeq = New_Neg_Random_DSeq
   647   | compilation_for_polarity _ c = c
   648 
   649 fun string_of_compilation c =
   650   case c of
   651     Pred => ""
   652   | Random => "random"
   653   | Depth_Limited => "depth limited"
   654   | Depth_Limited_Random => "depth limited random"
   655   | DSeq => "dseq"
   656   | Annotated => "annotated"
   657   | Pos_Random_DSeq => "pos_random dseq"
   658   | Neg_Random_DSeq => "neg_random_dseq"
   659   | New_Pos_Random_DSeq => "new_pos_random dseq"
   660   | New_Neg_Random_DSeq => "new_neg_random_dseq"
   661 
   662 val compilation_names = [("pred", Pred),
   663   ("random", Random),
   664   ("depth_limited", Depth_Limited),
   665   ("depth_limited_random", Depth_Limited_Random),
   666   (*("annotated", Annotated),*)
   667   ("dseq", DSeq), ("random_dseq", Pos_Random_DSeq),
   668   ("new_random_dseq", New_Pos_Random_DSeq)]
   669 
   670 val non_random_compilations = [Pred, Depth_Limited, DSeq, Annotated]
   671 
   672 
   673 val random_compilations = [Random, Depth_Limited_Random,
   674   Pos_Random_DSeq, Neg_Random_DSeq, New_Pos_Random_DSeq, New_Neg_Random_DSeq]
   675 
   676 (* datastructures and setup for generic compilation *)
   677 
   678 datatype compilation_funs = CompilationFuns of {
   679   mk_predT : typ -> typ,
   680   dest_predT : typ -> typ,
   681   mk_bot : typ -> term,
   682   mk_single : term -> term,
   683   mk_bind : term * term -> term,
   684   mk_sup : term * term -> term,
   685   mk_if : term -> term,
   686   mk_iterate_upto : typ -> term * term * term -> term,
   687   mk_not : term -> term,
   688   mk_map : typ -> typ -> term -> term -> term
   689 };
   690 
   691 fun mk_predT (CompilationFuns funs) = #mk_predT funs
   692 fun dest_predT (CompilationFuns funs) = #dest_predT funs
   693 fun mk_bot (CompilationFuns funs) = #mk_bot funs
   694 fun mk_single (CompilationFuns funs) = #mk_single funs
   695 fun mk_bind (CompilationFuns funs) = #mk_bind funs
   696 fun mk_sup (CompilationFuns funs) = #mk_sup funs
   697 fun mk_if (CompilationFuns funs) = #mk_if funs
   698 fun mk_iterate_upto (CompilationFuns funs) = #mk_iterate_upto funs
   699 fun mk_not (CompilationFuns funs) = #mk_not funs
   700 fun mk_map (CompilationFuns funs) = #mk_map funs
   701 
   702 (** function types and names of different compilations **)
   703 
   704 fun funT_of compfuns mode T =
   705   let
   706     val Ts = binder_types T
   707     val (inTs, outTs) = split_map_modeT (fn m => fn T => (SOME (funT_of compfuns m T), NONE)) mode Ts
   708   in
   709     inTs ---> (mk_predT compfuns (HOLogic.mk_tupleT outTs))
   710   end;
   711 
   712 (* Different options for compiler *)
   713 
   714 datatype options = Options of {  
   715   expected_modes : (string * mode list) option,
   716   proposed_modes : (string * mode list) list,
   717   proposed_names : ((string * mode) * string) list,
   718   show_steps : bool,
   719   show_proof_trace : bool,
   720   show_intermediate_results : bool,
   721   show_mode_inference : bool,
   722   show_modes : bool,
   723   show_compilation : bool,
   724   show_caught_failures : bool,
   725   show_invalid_clauses : bool,
   726   skip_proof : bool,
   727   no_topmost_reordering : bool,
   728   function_flattening : bool,
   729   specialise : bool,
   730   fail_safe_function_flattening : bool,
   731   no_higher_order_predicate : string list,
   732   inductify : bool,
   733   detect_switches : bool,
   734   smart_depth_limiting : bool,
   735   compilation : compilation
   736 };
   737 
   738 fun expected_modes (Options opt) = #expected_modes opt
   739 fun proposed_modes (Options opt) = AList.lookup (op =) (#proposed_modes opt)
   740 fun proposed_names (Options opt) name mode = AList.lookup (eq_pair (op =) eq_mode)
   741   (#proposed_names opt) (name, mode)
   742 
   743 fun show_steps (Options opt) = #show_steps opt
   744 fun show_intermediate_results (Options opt) = #show_intermediate_results opt
   745 fun show_proof_trace (Options opt) = #show_proof_trace opt
   746 fun show_modes (Options opt) = #show_modes opt
   747 fun show_mode_inference (Options opt) = #show_mode_inference opt
   748 fun show_compilation (Options opt) = #show_compilation opt
   749 fun show_caught_failures (Options opt) = #show_caught_failures opt
   750 fun show_invalid_clauses (Options opt) = #show_invalid_clauses opt
   751 fun skip_proof (Options opt) = #skip_proof opt
   752 
   753 fun function_flattening (Options opt) = #function_flattening opt
   754 fun fail_safe_function_flattening (Options opt) = #fail_safe_function_flattening opt
   755 fun specialise (Options opt) = #specialise opt
   756 fun no_topmost_reordering (Options opt) = #no_topmost_reordering opt
   757 fun no_higher_order_predicate (Options opt) = #no_higher_order_predicate opt
   758 
   759 fun is_inductify (Options opt) = #inductify opt
   760 
   761 fun compilation (Options opt) = #compilation opt
   762 
   763 fun detect_switches (Options opt) = #detect_switches opt
   764 
   765 fun smart_depth_limiting (Options opt) = #smart_depth_limiting opt
   766 
   767 val default_options = Options {
   768   expected_modes = NONE,
   769   proposed_modes = [],
   770   proposed_names = [],
   771   show_steps = false,
   772   show_intermediate_results = false,
   773   show_proof_trace = false,
   774   show_modes = false,
   775   show_mode_inference = false,
   776   show_compilation = false,
   777   show_caught_failures = false,
   778   show_invalid_clauses = false,
   779   skip_proof = true,
   780   no_topmost_reordering = false,
   781   function_flattening = false,
   782   specialise = false,
   783   fail_safe_function_flattening = false,
   784   no_higher_order_predicate = [],
   785   inductify = false,
   786   detect_switches = true,
   787   smart_depth_limiting = false,
   788   compilation = Pred
   789 }
   790 
   791 val bool_options = ["show_steps", "show_intermediate_results", "show_proof_trace", "show_modes",
   792   "show_mode_inference", "show_compilation", "show_invalid_clauses", "skip_proof", "inductify",
   793   "no_function_flattening", "detect_switches", "specialise", "no_topmost_reordering",
   794   "smart_depth_limiting"]
   795 
   796 fun print_step options s =
   797   if show_steps options then tracing s else ()
   798 
   799 (* simple transformations *)
   800 
   801 (** tuple processing **)
   802 
   803 fun rewrite_args [] (pats, intro_t, ctxt) = (pats, intro_t, ctxt)
   804   | rewrite_args (arg::args) (pats, intro_t, ctxt) = 
   805     (case HOLogic.strip_tupleT (fastype_of arg) of
   806       (Ts as _ :: _ :: _) =>
   807       let
   808         fun rewrite_arg' (Const (@{const_name Pair}, _) $ _ $ t2, Type (@{type_name Product_Type.prod}, [_, T2]))
   809           (args, (pats, intro_t, ctxt)) = rewrite_arg' (t2, T2) (args, (pats, intro_t, ctxt))
   810           | rewrite_arg' (t, Type (@{type_name Product_Type.prod}, [T1, T2])) (args, (pats, intro_t, ctxt)) =
   811             let
   812               val thy = ProofContext.theory_of ctxt
   813               val ([x, y], ctxt') = Variable.variant_fixes ["x", "y"] ctxt
   814               val pat = (t, HOLogic.mk_prod (Free (x, T1), Free (y, T2)))
   815               val intro_t' = Pattern.rewrite_term thy [pat] [] intro_t
   816               val args' = map (Pattern.rewrite_term thy [pat] []) args
   817             in
   818               rewrite_arg' (Free (y, T2), T2) (args', (pat::pats, intro_t', ctxt'))
   819             end
   820           | rewrite_arg' _ (args, (pats, intro_t, ctxt)) = (args, (pats, intro_t, ctxt))
   821         val (args', (pats, intro_t', ctxt')) = rewrite_arg' (arg, fastype_of arg)
   822           (args, (pats, intro_t, ctxt))
   823       in
   824         rewrite_args args' (pats, intro_t', ctxt')
   825       end
   826   | _ => rewrite_args args (pats, intro_t, ctxt))
   827 
   828 fun rewrite_prem atom =
   829   let
   830     val (_, args) = strip_comb atom
   831   in rewrite_args args end
   832 
   833 fun split_conjuncts_in_assms ctxt th =
   834   let
   835     val ((_, [fixed_th]), ctxt') = Variable.import false [th] ctxt 
   836     fun split_conjs i nprems th =
   837       if i > nprems then th
   838       else
   839         case try Drule.RSN (@{thm conjI}, (i, th)) of
   840           SOME th' => split_conjs i (nprems+1) th'
   841         | NONE => split_conjs (i+1) nprems th
   842   in
   843     singleton (Variable.export ctxt' ctxt) (split_conjs 1 (Thm.nprems_of fixed_th) fixed_th)
   844   end
   845   
   846 fun expand_tuples thy intro =
   847   let
   848     val ctxt = ProofContext.init_global thy
   849     val (((T_insts, t_insts), [intro']), ctxt1) = Variable.import false [intro] ctxt
   850     val intro_t = prop_of intro'
   851     val concl = Logic.strip_imp_concl intro_t
   852     val (p, args) = strip_comb (HOLogic.dest_Trueprop concl)
   853     val (pats', intro_t', ctxt2) = rewrite_args args ([], intro_t, ctxt1)
   854     val (pats', intro_t', ctxt3) = 
   855       fold_atoms rewrite_prem intro_t' (pats', intro_t', ctxt2)
   856     fun rewrite_pat (ct1, ct2) =
   857       (ct1, cterm_of thy (Pattern.rewrite_term thy pats' [] (term_of ct2)))
   858     val t_insts' = map rewrite_pat t_insts
   859     val intro'' = Thm.instantiate (T_insts, t_insts') intro
   860     val [intro'''] = Variable.export ctxt3 ctxt [intro'']
   861     val intro'''' = Simplifier.full_simplify
   862       (HOL_basic_ss addsimps [@{thm fst_conv}, @{thm snd_conv}, @{thm Pair_eq}])
   863       intro'''
   864     (* splitting conjunctions introduced by Pair_eq*)
   865     val intro''''' = split_conjuncts_in_assms ctxt intro''''
   866   in
   867     intro'''''
   868   end
   869 
   870 (** making case distributivity rules **)
   871 (*** this should be part of the datatype package ***)
   872 
   873 fun datatype_names_of_case_name thy case_name =
   874   map (#1 o #2) (#descr (the (Datatype_Data.info_of_case thy case_name)))
   875 
   876 fun make_case_distribs new_type_names descr sorts thy =
   877   let
   878     val case_combs = Datatype_Prop.make_case_combs new_type_names descr sorts thy "f";
   879     fun make comb =
   880       let
   881         val Type ("fun", [T, T']) = fastype_of comb;
   882         val (Const (case_name, _), fs) = strip_comb comb
   883         val used = Term.add_tfree_names comb []
   884         val U = TFree (Name.variant used "'t", HOLogic.typeS)
   885         val x = Free ("x", T)
   886         val f = Free ("f", T' --> U)
   887         fun apply_f f' =
   888           let
   889             val Ts = binder_types (fastype_of f')
   890             val bs = map Bound ((length Ts - 1) downto 0)
   891           in
   892             fold (curry absdummy) (rev Ts) (f $ (list_comb (f', bs)))
   893           end
   894         val fs' = map apply_f fs
   895         val case_c' = Const (case_name, (map fastype_of fs') @ [T] ---> U)
   896       in
   897         HOLogic.mk_eq (f $ (comb $ x), list_comb (case_c', fs') $ x)
   898       end
   899   in
   900     map make case_combs
   901   end
   902 
   903 fun case_rewrites thy Tcon =
   904   let
   905     val info = Datatype.the_info thy Tcon
   906     val descr = #descr info
   907     val sorts = #sorts info
   908     val typ_names = the_default [Tcon] (#alt_names info)
   909   in
   910     map (Drule.export_without_context o Skip_Proof.make_thm thy o HOLogic.mk_Trueprop)
   911       (make_case_distribs typ_names [descr] sorts thy)
   912   end
   913 
   914 fun instantiated_case_rewrites thy Tcon =
   915   let
   916     val rew_ths = case_rewrites thy Tcon
   917     val ctxt = ProofContext.init_global thy
   918     fun instantiate th =
   919     let
   920       val f = (fst (strip_comb (fst (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of th))))))
   921       val Type ("fun", [uninst_T, uninst_T']) = fastype_of f
   922       val ([tname, tname', uname, yname], ctxt') = Variable.add_fixes ["'t", "'t'", "'u", "y"] ctxt
   923       val T = TFree (tname, HOLogic.typeS)
   924       val T' = TFree (tname', HOLogic.typeS)
   925       val U = TFree (uname, HOLogic.typeS)
   926       val y = Free (yname, U)
   927       val f' = absdummy (U --> T', Bound 0 $ y)
   928       val th' = Thm.certify_instantiate
   929         ([(dest_TVar uninst_T, U --> T'), (dest_TVar uninst_T', T')],
   930          [((fst (dest_Var f), (U --> T') --> T'), f')]) th
   931       val [th'] = Variable.export ctxt' ctxt [th']
   932    in
   933      th'
   934    end
   935  in
   936    map instantiate rew_ths
   937  end
   938 
   939 fun case_betapply thy t =
   940   let
   941     val case_name = fst (dest_Const (fst (strip_comb t)))
   942     val Tcons = datatype_names_of_case_name thy case_name
   943     val ths = maps (instantiated_case_rewrites thy) Tcons
   944   in
   945     MetaSimplifier.rewrite_term thy
   946       (map (fn th => th RS @{thm eq_reflection}) ths) [] t
   947   end
   948 
   949 (*** conversions ***)
   950 
   951 fun imp_prems_conv cv ct =
   952   case Thm.term_of ct of
   953     Const ("==>", _) $ _ $ _ => Conv.combination_conv (Conv.arg_conv cv) (imp_prems_conv cv) ct
   954   | _ => Conv.all_conv ct
   955 
   956 fun all_params_conv cv ctxt ct =
   957   if Logic.is_all (Thm.term_of ct)
   958   then Conv.arg_conv (Conv.abs_conv (all_params_conv cv o #2) ctxt) ct
   959   else cv ctxt ct;
   960   
   961 (** eta contract higher-order arguments **)
   962 
   963 fun eta_contract_ho_arguments thy intro =
   964   let
   965     fun f atom = list_comb (apsnd ((map o map_products) Envir.eta_contract) (strip_comb atom))
   966   in
   967     map_term thy (map_concl f o map_atoms f) intro
   968   end
   969 
   970 (** remove equalities **)
   971 
   972 fun remove_equalities thy intro =
   973   let
   974     fun remove_eqs intro_t =
   975       let
   976         val (prems, concl) = Logic.strip_horn intro_t
   977         fun remove_eq (prems, concl) =
   978           let
   979             fun removable_eq prem =
   980               case try (HOLogic.dest_eq o HOLogic.dest_Trueprop) prem of
   981                 SOME (lhs, rhs) => (case lhs of
   982                   Var _ => true
   983                   | _ => (case rhs of Var _ => true | _ => false))
   984               | NONE => false
   985           in
   986             case find_first removable_eq prems of
   987               NONE => (prems, concl)
   988             | SOME eq =>
   989               let
   990                 val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
   991                 val prems' = remove (op =) eq prems
   992                 val subst = (case lhs of
   993                   (v as Var _) =>
   994                     (fn t => if t = v then rhs else t)
   995                 | _ => (case rhs of
   996                    (v as Var _) => (fn t => if t = v then lhs else t)))
   997               in
   998                 remove_eq (map (map_aterms subst) prems', map_aterms subst concl)
   999               end
  1000           end
  1001       in
  1002         Logic.list_implies (remove_eq (prems, concl))
  1003       end
  1004   in
  1005     map_term thy remove_eqs intro
  1006   end
  1007 
  1008 (* Some last processing *)
  1009 
  1010 fun remove_pointless_clauses intro =
  1011   if Logic.strip_imp_prems (prop_of intro) = [@{prop "False"}] then
  1012     []
  1013   else [intro]
  1014 
  1015 (* some peephole optimisations *)
  1016 
  1017 fun peephole_optimisation thy intro =
  1018   let
  1019     val process =
  1020       MetaSimplifier.rewrite_rule (Predicate_Compile_Simps.get (ProofContext.init_global thy))
  1021     fun process_False intro_t =
  1022       if member (op =) (Logic.strip_imp_prems intro_t) @{prop "False"} then NONE else SOME intro_t
  1023     fun process_True intro_t =
  1024       map_filter_premises (fn p => if p = @{prop True} then NONE else SOME p) intro_t
  1025   in
  1026     Option.map (Skip_Proof.make_thm thy)
  1027       (process_False (process_True (prop_of (process intro))))
  1028   end
  1029 
  1030 (* defining a quickcheck predicate *)
  1031 
  1032 fun strip_imp_prems (Const(@{const_name HOL.implies}, _) $ A $ B) = A :: strip_imp_prems B
  1033   | strip_imp_prems _ = [];
  1034 
  1035 fun strip_imp_concl (Const(@{const_name HOL.implies}, _) $ A $ B) = strip_imp_concl B
  1036   | strip_imp_concl A = A : term;
  1037 
  1038 fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
  1039 
  1040 fun define_quickcheck_predicate t thy =
  1041   let
  1042     val (vs, t') = strip_abs t
  1043     val vs' = Variable.variant_frees (ProofContext.init_global thy) [] vs
  1044     val t'' = subst_bounds (map Free (rev vs'), t')
  1045     val (prems, concl) = strip_horn t''
  1046     val constname = "quickcheck"
  1047     val full_constname = Sign.full_bname thy constname
  1048     val constT = map snd vs' ---> @{typ bool}
  1049     val thy1 = Sign.add_consts_i [(Binding.name constname, constT, NoSyn)] thy
  1050     val const = Const (full_constname, constT)
  1051     val t = Logic.list_implies
  1052       (map HOLogic.mk_Trueprop (prems @ [HOLogic.mk_not concl]),
  1053        HOLogic.mk_Trueprop (list_comb (const, map Free vs')))
  1054     val tac = fn _ => Skip_Proof.cheat_tac thy1
  1055     val intro = Goal.prove (ProofContext.init_global thy1) (map fst vs') [] t tac
  1056   in
  1057     ((((full_constname, constT), vs'), intro), thy1)
  1058   end
  1059 
  1060 end;