src/HOL/Tools/Predicate_Compile/pred_compile_fun.ML
author bulwahn
Sat, 24 Oct 2009 16:55:42 +0200
changeset 33123 3c7c4372f9ad
parent 33122 7d01480cc8e3
child 33129 3085da75ed54
permissions -rw-r--r--
cleaned up debugging messages; added options to code_pred command
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     1
(* Author: Lukas Bulwahn, TU Muenchen
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     2
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     3
Preprocessing functions to predicates
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     4
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     5
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     6
signature PREDICATE_COMPILE_FUN =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     7
sig
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
     8
val define_predicates : (string * thm list) list -> theory -> (string * thm list) list * theory
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
     9
  val rewrite_intro : theory -> thm -> thm list
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    10
  val setup_oracle : theory -> theory
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
    11
  val pred_of_function : theory -> string -> string option
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    12
end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    13
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    14
structure Predicate_Compile_Fun : PREDICATE_COMPILE_FUN =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    15
struct
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    16
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    17
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    18
(* Oracle for preprocessing  *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    19
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32672
diff changeset
    20
val (oracle : (string * (cterm -> thm)) option Unsynchronized.ref) = Unsynchronized.ref NONE;
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    21
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    22
fun the_oracle () =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    23
  case !oracle of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    24
    NONE => error "Oracle is not setup"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    25
  | SOME (_, oracle) => oracle
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    26
             
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    27
val setup_oracle = Thm.add_oracle (Binding.name "pred_compile_preprocessing", I) #->
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    28
  (fn ora => fn thy => let val _ = (oracle := SOME ora) in thy end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    29
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    30
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    31
fun is_funtype (Type ("fun", [_, _])) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    32
  | is_funtype _ = false;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    33
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    34
fun is_Type (Type _) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    35
  | is_Type _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    36
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    37
(* returns true if t is an application of an datatype constructor *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    38
(* which then consequently would be splitted *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    39
(* else false *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    40
(*
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    41
fun is_constructor thy t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    42
  if (is_Type (fastype_of t)) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    43
    (case DatatypePackage.get_datatype thy ((fst o dest_Type o fastype_of) t) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    44
      NONE => false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    45
    | SOME info => (let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    46
      val constr_consts = flat (map (fn (_, (_, _, constrs)) => map fst constrs) (#descr info))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    47
      val (c, _) = strip_comb t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    48
      in (case c of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    49
        Const (name, _) => name mem_string constr_consts
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    50
        | _ => false) end))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    51
  else false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    52
*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    53
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    54
(* must be exported in code.ML *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    55
fun is_constr thy = is_some o Code.get_datatype_of_constr thy;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    56
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    57
(* Table from constant name (string) to term of inductive predicate *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    58
structure Pred_Compile_Preproc = TheoryDataFun
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    59
(
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    60
  type T = string Symtab.table;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    61
  val empty = Symtab.empty;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    62
  val copy = I;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    63
  val extend = I;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    64
  fun merge _ = Symtab.merge (op =);
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    65
)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    66
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
    67
fun pred_of_function thy name = Symtab.lookup (Pred_Compile_Preproc.get thy) name
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
    68
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    69
fun defined thy = Symtab.defined (Pred_Compile_Preproc.get thy) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    70
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    71
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    72
fun transform_ho_typ (T as Type ("fun", _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    73
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    74
    val (Ts, T') = strip_type T
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
    75
  in if T' = @{typ "bool"} then T else (Ts @ [T']) ---> HOLogic.boolT end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    76
| transform_ho_typ t = t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    77
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    78
fun transform_ho_arg arg = 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    79
  case (fastype_of arg) of
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
    80
    (T as Type ("fun", _)) =>
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    81
      (case arg of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    82
        Free (name, _) => Free (name, transform_ho_typ T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    83
      | _ => error "I am surprised")
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    84
| _ => arg
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    85
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    86
fun pred_type T =
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
    87
  let
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    88
    val (Ts, T') = strip_type T
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    89
    val Ts' = map transform_ho_typ Ts
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    90
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    91
    (Ts' @ [T']) ---> HOLogic.boolT
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    92
  end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    93
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    94
(* FIXME: create new predicate name -- does not avoid nameclashing *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    95
fun pred_of f =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    96
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    97
    val (name, T) = dest_Const f
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    98
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
    99
    if (body_type T = @{typ bool}) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   100
      (Free (Long_Name.base_name name ^ "P", T))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   101
    else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   102
      (Free (Long_Name.base_name name ^ "P", pred_type T))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   103
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   104
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   105
fun mk_param lookup_pred (t as Free (v, _)) = lookup_pred t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   106
  | mk_param lookup_pred t =
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   107
  if Predicate_Compile_Aux.is_predT (fastype_of t) then
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   108
    t
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   109
  else
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   110
    let
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   111
      val (vs, body) = strip_abs t
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   112
      val names = Term.add_free_names body []
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   113
      val vs_names = Name.variant_list names (map fst vs)
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   114
      val vs' = map2 (curry Free) vs_names (map snd vs)
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   115
      val body' = subst_bounds (rev vs', body)
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   116
      val (f, args) = strip_comb body'
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   117
      val resname = Name.variant (vs_names @ names) "res"
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   118
      val resvar = Free (resname, body_type (fastype_of body'))
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   119
      val P = lookup_pred f
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   120
      val pred_body = list_comb (P, args @ [resvar])
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   121
      val param = fold_rev lambda (vs' @ [resvar]) pred_body
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   122
    in param end;
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   123
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   124
(* creates the list of premises for every intro rule *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   125
(* theory -> term -> (string list, term list list) *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   126
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   127
fun dest_code_eqn eqn = let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   128
  val (lhs, rhs) = Logic.dest_equals (Logic.unvarify (Thm.prop_of eqn))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   129
  val (func, args) = strip_comb lhs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   130
in ((func, args), rhs) end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   131
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   132
fun string_of_typ T = Syntax.string_of_typ_global @{theory} T
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   133
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   134
fun string_of_term t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   135
  case t of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   136
    Const (c, T) => "Const (" ^ c ^ ", " ^ string_of_typ T ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   137
  | Free (c, T) => "Free (" ^ c ^ ", " ^ string_of_typ T ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   138
  | Var ((c, i), T) => "Var ((" ^ c ^ ", " ^ string_of_int i ^ "), " ^ string_of_typ T ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   139
  | Bound i => "Bound " ^ string_of_int i
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   140
  | Abs (x, T, t) => "Abs (" ^ x ^ ", " ^ string_of_typ T ^ ", " ^ string_of_term t ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   141
  | t1 $ t2 => "(" ^ string_of_term t1 ^ ") $ (" ^ string_of_term t2 ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   142
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   143
fun ind_package_get_nparams thy name =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   144
  case try (Inductive.the_inductive (ProofContext.init thy)) name of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   145
    SOME (_, result) => length (Inductive.params_of (#raw_induct result))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   146
  | NONE => error ("No such predicate: " ^ quote name) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   147
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   148
(* TODO: does not work with higher order functions yet *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   149
fun mk_rewr_eq (func, pred) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   150
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   151
    val (argTs, resT) = (strip_type (fastype_of func))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   152
    val nctxt =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   153
      Name.make_context (Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) (func $ pred) [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   154
    val (argnames, nctxt') = Name.variants (replicate (length argTs) "a") nctxt
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   155
    val ([resname], nctxt'') = Name.variants ["r"] nctxt'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   156
    val args = map Free (argnames ~~ argTs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   157
    val res = Free (resname, resT)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   158
  in Logic.mk_equals
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   159
      (HOLogic.mk_eq (res, list_comb (func, args)), list_comb (pred, args @ [res]))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   160
  end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   161
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   162
fun has_split_rule_cname @{const_name "nat_case"} = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   163
  | has_split_rule_cname @{const_name "list_case"} = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   164
  | has_split_rule_cname _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   165
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   166
fun has_split_rule_term thy (Const (@{const_name "nat_case"}, _)) = true 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   167
  | has_split_rule_term thy (Const (@{const_name "list_case"}, _)) = true 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   168
  | has_split_rule_term thy _ = false
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   169
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   170
fun has_split_rule_term' thy (Const (@{const_name "If"}, _)) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   171
  | has_split_rule_term' thy (Const (@{const_name "Let"}, _)) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   172
  | has_split_rule_term' thy c = has_split_rule_term thy c
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   173
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   174
fun prepare_split_thm ctxt split_thm =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   175
    (split_thm RS @{thm iffD2})
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   176
    |> LocalDefs.unfold ctxt [@{thm atomize_conjL[symmetric]},
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   177
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   178
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   179
fun find_split_thm thy (Const (name, typ)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   180
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   181
    fun split_name str =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   182
      case first_field "." str
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   183
        of (SOME (field, rest)) => field :: split_name rest
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   184
         | NONE => [str]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   185
    val splitted_name = split_name name
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   186
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   187
    if length splitted_name > 0 andalso
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   188
       String.isSuffix "_case" (List.last splitted_name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   189
    then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   190
      (List.take (splitted_name, length splitted_name - 1)) @ ["split"]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   191
      |> String.concatWith "."
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   192
      |> PureThy.get_thm thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   193
      |> SOME
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   194
      handle ERROR msg => NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   195
    else NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   196
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   197
  | find_split_thm _ _ = NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   198
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   199
fun find_split_thm' thy (Const (@{const_name "If"}, _)) = SOME @{thm split_if}
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   200
  | find_split_thm' thy (Const (@{const_name "Let"}, _)) = SOME @{thm refl} (* TODO *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   201
  | find_split_thm' thy c = find_split_thm thy c
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   202
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   203
fun strip_all t = (Term.strip_all_vars t, Term.strip_all_body t)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   204
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   205
fun folds_map f xs y =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   206
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   207
    fun folds_map' acc [] y = [(rev acc, y)]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   208
      | folds_map' acc (x :: xs) y =
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   209
        maps (fn (x, y) => folds_map' (x :: acc) xs y) (f x y)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   210
    in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   211
      folds_map' [] xs y
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   212
    end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   213
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   214
fun mk_prems thy (lookup_pred, get_nparams) t (names, prems) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   215
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   216
    fun mk_prems' (t as Const (name, T)) (names, prems) =
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   217
      if is_constr thy name orelse (is_none (try lookup_pred t)) then
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 32740
diff changeset
   218
        [(t, (names, prems))]
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   219
      else [(lookup_pred t, (names, prems))]
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   220
    | mk_prems' (t as Free (f, T)) (names, prems) = 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   221
      [(lookup_pred t, (names, prems))]
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   222
    | mk_prems' (t as Abs _) (names, prems) =
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   223
      if Predicate_Compile_Aux.is_predT (fastype_of t) then
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   224
      [(t, (names, prems))] else error "mk_prems': Abs "
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   225
      (* mk_param *)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   226
    | mk_prems' t (names, prems) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   227
      if Predicate_Compile_Aux.is_constrt thy t then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   228
        [(t, (names, prems))]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   229
      else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   230
        if has_split_rule_term' thy (fst (strip_comb t)) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   231
          let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   232
            val (f, args) = strip_comb t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   233
            val split_thm = prepare_split_thm (ProofContext.init thy) (the (find_split_thm' thy f))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   234
            (* TODO: contextify things - this line is to unvarify the split_thm *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   235
            (*val ((_, [isplit_thm]), _) = Variable.import true [split_thm] (ProofContext.init thy)*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   236
            val (assms, concl) = Logic.strip_horn (Thm.prop_of split_thm)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   237
            val (P, [split_t]) = strip_comb (HOLogic.dest_Trueprop concl) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   238
            val subst = Pattern.match thy (split_t, t) (Vartab.empty, Vartab.empty)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   239
            val (_, split_args) = strip_comb split_t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   240
            val match = split_args ~~ args
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   241
            fun mk_prems_of_assm assm =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   242
              let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   243
                val (vTs, assm') = strip_all (Envir.beta_norm (Envir.subst_term subst assm))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   244
                val var_names = Name.variant_list names (map fst vTs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   245
                val vars = map Free (var_names ~~ (map snd vTs))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   246
                val (prems', pre_res) = Logic.strip_horn (subst_bounds (rev vars, assm'))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   247
                val (_, [inner_t]) = strip_comb (HOLogic.dest_Trueprop pre_res)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   248
              in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   249
                mk_prems' inner_t (var_names @ names, prems' @ prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   250
              end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   251
          in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   252
            maps mk_prems_of_assm assms
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   253
          end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   254
        else
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 32740
diff changeset
   255
          let
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   256
            val (f, args) = strip_comb t
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
   257
            (* TODO: special procedure for higher-order functions: split arguments in
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
   258
              simple types and function types *)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   259
            val resname = Name.variant names "res"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   260
            val resvar = Free (resname, body_type (fastype_of t))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   261
            val names' = resname :: names
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   262
            fun mk_prems'' (t as Const (c, _)) =
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   263
              if is_constr thy c orelse (is_none (try lookup_pred t)) then
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   264
                folds_map mk_prems' args (names', prems) |>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   265
                map
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   266
                  (fn (argvs, (names'', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   267
                  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   268
                    val prem = HOLogic.mk_Trueprop (HOLogic.mk_eq (resvar, list_comb (f, argvs)))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   269
                  in (names'', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   270
              else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   271
                let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   272
                  val pred = lookup_pred t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   273
                  val nparams = get_nparams pred
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   274
                  val (params, args) = chop nparams args
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   275
                  val params' = map (mk_param lookup_pred) params
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   276
                in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   277
                  folds_map mk_prems' args (names', prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   278
                  |> map (fn (argvs, (names'', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   279
                    let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   280
                      val prem = HOLogic.mk_Trueprop (list_comb (pred, params' @ argvs @ [resvar]))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   281
                    in (names'', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   282
                end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   283
            | mk_prems'' (t as Free (_, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   284
                let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   285
                  (* higher order argument call *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   286
                  val pred = lookup_pred t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   287
                in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   288
                  folds_map mk_prems' args (resname :: names, prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   289
                  |> map (fn (argvs, (names', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   290
                     let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   291
                       val prem = HOLogic.mk_Trueprop (list_comb (pred, argvs @ [resvar]))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   292
                     in (names', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   293
                end
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 32740
diff changeset
   294
            | mk_prems'' t =
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   295
              error ("Invalid term: " ^ Syntax.string_of_term_global thy t)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   296
          in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   297
            map (pair resvar) (mk_prems'' f)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   298
          end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   299
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   300
    mk_prems' t (names, prems)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   301
  end;
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   302
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   303
(* assumption: mutual recursive predicates all have the same parameters. *)  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   304
fun define_predicates specs thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   305
  if forall (fn (const, _) => member (op =) (Symtab.keys (Pred_Compile_Preproc.get thy)) const) specs then
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   306
    ([], thy)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   307
  else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   308
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   309
    val consts = map fst specs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   310
    val eqns = maps snd specs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   311
    (*val eqns = maps (Predicate_Compile_Preproc_Data.get_specification thy) consts*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   312
      (* create prednames *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   313
    val ((funs, argss), rhss) = map_split dest_code_eqn eqns |>> split_list
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   314
    val argss' = map (map transform_ho_arg) argss
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   315
    val pnames = map dest_Free (distinct (op =) (maps (filter (is_funtype o fastype_of)) argss'))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   316
    val preds = map pred_of funs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   317
    val prednames = map (fst o dest_Free) preds
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   318
    val funnames = map (fst o dest_Const) funs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   319
    val fun_pred_names = (funnames ~~ prednames)  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   320
      (* mapping from term (Free or Const) to term *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   321
    fun lookup_pred (Const (@{const_name Cons}, T)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   322
      Const ("Preprocessing.ConsP", pred_type T) (* FIXME: temporary - Cons lookup *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   323
      | lookup_pred (Const (name, T)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   324
      (case (Symtab.lookup (Pred_Compile_Preproc.get thy) name) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   325
          SOME c => Const (c, pred_type T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   326
        | NONE =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   327
          (case AList.lookup op = fun_pred_names name of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   328
            SOME f => Free (f, pred_type T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   329
          | NONE => Const (name, T)))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   330
      | lookup_pred  (Free (name, T)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   331
        if member op = (map fst pnames) name then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   332
          Free (name, transform_ho_typ T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   333
        else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   334
          Free (name, T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   335
      | lookup_pred t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   336
         error ("lookup function is not defined for " ^ Syntax.string_of_term_global thy t)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   337
     
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   338
        (* mapping from term (predicate term, not function term!) to int *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   339
    fun get_nparams (Const (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   340
      the_default 0 (try (ind_package_get_nparams thy) name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   341
    | get_nparams (Free (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   342
        (if member op = prednames name then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   343
          length pnames
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   344
        else 0)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   345
    | get_nparams t = error ("No parameters for " ^ (Syntax.string_of_term_global thy t))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   346
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   347
    (* create intro rules *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   348
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   349
    fun mk_intros ((func, pred), (args, rhs)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   350
      if (body_type (fastype_of func) = @{typ bool}) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   351
       (*TODO: preprocess predicate definition of rhs *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   352
        [Logic.list_implies ([HOLogic.mk_Trueprop rhs], HOLogic.mk_Trueprop (list_comb (pred, args)))]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   353
      else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   354
        let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   355
          val names = Term.add_free_names rhs []
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   356
        in mk_prems thy (lookup_pred, get_nparams) rhs (names, [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   357
          |> map (fn (resultt, (names', prems)) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   358
            Logic.list_implies (prems, HOLogic.mk_Trueprop (list_comb (pred, args @ [resultt]))))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   359
        end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   360
    fun mk_rewr_thm (func, pred) = @{thm refl}
33114
4785ef554dcc added further examples; added mode to code_pred command; tuned; some temporary things in Predicate_Compile_ex
bulwahn
parents: 33108
diff changeset
   361
  in
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   362
    case try (maps mk_intros) ((funs ~~ preds) ~~ (argss' ~~ rhss)) of
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   363
      NONE => ([], thy) 
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33122
diff changeset
   364
    | SOME intr_ts =>
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   365
        if is_some (try (map (cterm_of thy)) intr_ts) then
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   366
          let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   367
            val (ind_result, thy') =
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   368
              Inductive.add_inductive_global (serial_string ())
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   369
                {quiet_mode = false, verbose = false, kind = Thm.internalK,
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   370
                  alt_name = Binding.empty, coind = false, no_elim = false,
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   371
                  no_ind = false, skip_mono = false, fork_mono = false}
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   372
                (map (fn (s, T) => ((Binding.name s, T), NoSyn)) (distinct (op =) (map dest_Free preds)))
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   373
                pnames
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   374
                (map (fn x => (Attrib.empty_binding, x)) intr_ts)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   375
                [] thy
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   376
            val prednames = map (fst o dest_Const) (#preds ind_result)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   377
            (* val rewr_thms = map mk_rewr_eq ((distinct (op =) funs) ~~ (#preds ind_result)) *)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   378
            (* add constants to my table *)
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   379
            val specs = map (fn predname => (predname, filter (Predicate_Compile_Aux.is_intro predname) (#intrs ind_result))) prednames
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   380
            val thy'' = Pred_Compile_Preproc.map (fold Symtab.update_new (consts ~~ prednames)) thy'
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   381
          in
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   382
            (specs, thy'')
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   383
          end
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   384
        else
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   385
          let
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   386
            val _ = tracing "Introduction rules of function_predicate are not welltyped"
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   387
          in ([], thy) end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   388
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   389
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   390
(* preprocessing intro rules - uses oracle *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   391
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   392
(* theory -> thm -> thm *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   393
fun rewrite_intro thy intro =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   394
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   395
    fun lookup_pred (Const (name, T)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   396
      (case (Symtab.lookup (Pred_Compile_Preproc.get thy) name) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   397
        SOME c => Const (c, pred_type T)
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   398
      | NONE => error ("Function " ^ name ^ " is not inductified"))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   399
    | lookup_pred (Free (name, T)) = Free (name, T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   400
    | lookup_pred _ = error "lookup function is not defined!"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   401
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   402
    fun get_nparams (Const (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   403
      the_default 0 (try (ind_package_get_nparams thy) name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   404
    | get_nparams (Free _) = 0
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   405
    | get_nparams t = error ("No parameters for " ^ (Syntax.string_of_term_global thy t))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   406
    
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   407
    val intro_t = (Logic.unvarify o prop_of) intro
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   408
    val (prems, concl) = Logic.strip_horn intro_t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   409
    val frees = map fst (Term.add_frees intro_t [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   410
    fun rewrite prem names =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   411
      let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   412
        val t = (HOLogic.dest_Trueprop prem)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   413
        val (lit, mk_lit) = case try HOLogic.dest_not t of
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   414
            SOME t => (t, HOLogic.mk_not)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   415
          | NONE => (t, I)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   416
        val (P, args) = (strip_comb lit) 
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   417
      in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   418
        folds_map (
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   419
          fn t => if (is_funtype (fastype_of t)) then (fn x => [(t, x)])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   420
            else mk_prems thy (lookup_pred, get_nparams) t) args (names, [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   421
        |> map (fn (resargs, (names', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   422
          let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   423
            val prem' = HOLogic.mk_Trueprop (mk_lit (list_comb (P, resargs)))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   424
          in (prem'::prems', names') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   425
      end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   426
    val intro_ts' = folds_map rewrite prems frees
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   427
      |> maps (fn (prems', frees') =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   428
        rewrite concl frees'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   429
        |> map (fn (concl'::conclprems, _) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   430
          Logic.list_implies ((flat prems') @ conclprems, concl')))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   431
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   432
    map (Drule.standard o the_oracle () o cterm_of thy) intro_ts'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   433
  end; 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   434
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   435
end;