src/HOL/Tools/Predicate_Compile/pred_compile_fun.ML
author bulwahn
Sat, 24 Oct 2009 20:27:26 +0200
changeset 33150 75eddea4abef
parent 33149 2c8f1c450b47
child 33171 292970b42770
permissions -rw-r--r--
further changes due to the previous merge in the predicate compiler
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
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
    46
      val constr_consts = maps (fn (_, (_, _, constrs)) => map fst constrs) (#descr info)
32668
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
33150
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
    67
fun pred_of_function thy name = Symtab.lookup (Pred_Compile_Preproc.get thy) name
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
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
33150
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   105
fun mk_param thy lookup_pred (t as Free (v, _)) = lookup_pred t
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   106
  | mk_param thy lookup_pred t =
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33123
diff changeset
   107
  let
33150
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   108
  val _ = tracing ("called param with " ^ (Syntax.string_of_term_global thy t))
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   109
  in if Predicate_Compile_Aux.is_predT (fastype_of t) then
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   110
    t
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   111
  else
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   112
    let
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   113
      val (vs, body) = strip_abs t
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   114
      val names = Term.add_free_names body []
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   115
      val vs_names = Name.variant_list names (map fst vs)
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   116
      val vs' = map2 (curry Free) vs_names (map snd vs)
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   117
      val body' = subst_bounds (rev vs', body)
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   118
      val (f, args) = strip_comb body'
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   119
      val resname = Name.variant (vs_names @ names) "res"
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   120
      val resvar = Free (resname, body_type (fastype_of body'))
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   121
      (*val P = case try lookup_pred f of SOME P => P | NONE => error "mk_param"
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   122
      val pred_body = list_comb (P, args @ [resvar])
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   123
      *)
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   124
      val pred_body = HOLogic.mk_eq (body', resvar)
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   125
      val param = fold_rev lambda (vs' @ [resvar]) pred_body
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   126
    in param end
75eddea4abef further changes due to the previous merge in the predicate compiler
bulwahn
parents: 33149
diff changeset
   127
  end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   128
(* creates the list of premises for every intro rule *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   129
(* theory -> term -> (string list, term list list) *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   130
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   131
fun dest_code_eqn eqn = let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   132
  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
   133
  val (func, args) = strip_comb lhs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   134
in ((func, args), rhs) end;
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   135
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   136
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
   137
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   138
fun string_of_term t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   139
  case t of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   140
    Const (c, T) => "Const (" ^ c ^ ", " ^ string_of_typ T ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   141
  | Free (c, T) => "Free (" ^ c ^ ", " ^ string_of_typ T ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   142
  | 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
   143
  | Bound i => "Bound " ^ string_of_int i
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   144
  | 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
   145
  | t1 $ t2 => "(" ^ string_of_term t1 ^ ") $ (" ^ string_of_term t2 ^ ")"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   146
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   147
fun ind_package_get_nparams thy name =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   148
  case try (Inductive.the_inductive (ProofContext.init thy)) name of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   149
    SOME (_, result) => length (Inductive.params_of (#raw_induct result))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   150
  | NONE => error ("No such predicate: " ^ quote name) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   151
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   152
(* TODO: does not work with higher order functions yet *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   153
fun mk_rewr_eq (func, pred) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   154
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   155
    val (argTs, resT) = (strip_type (fastype_of func))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   156
    val nctxt =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   157
      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
   158
    val (argnames, nctxt') = Name.variants (replicate (length argTs) "a") nctxt
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   159
    val ([resname], nctxt'') = Name.variants ["r"] nctxt'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   160
    val args = map Free (argnames ~~ argTs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   161
    val res = Free (resname, resT)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   162
  in Logic.mk_equals
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   163
      (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
   164
  end;
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_cname @{const_name "nat_case"} = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   167
  | has_split_rule_cname @{const_name "list_case"} = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   168
  | has_split_rule_cname _ = 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 "nat_case"}, _)) = true 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   171
  | has_split_rule_term thy (Const (@{const_name "list_case"}, _)) = true 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   172
  | has_split_rule_term thy _ = false
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 has_split_rule_term' thy (Const (@{const_name "If"}, _)) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   175
  | has_split_rule_term' thy (Const (@{const_name "Let"}, _)) = true
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   176
  | 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
   177
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   178
fun prepare_split_thm ctxt split_thm =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   179
    (split_thm RS @{thm iffD2})
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   180
    |> LocalDefs.unfold ctxt [@{thm atomize_conjL[symmetric]},
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   181
      @{thm atomize_all[symmetric]}, @{thm atomize_imp[symmetric]}]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   182
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   183
fun find_split_thm thy (Const (name, typ)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   184
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   185
    fun split_name str =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   186
      case first_field "." str
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   187
        of (SOME (field, rest)) => field :: split_name rest
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   188
         | NONE => [str]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   189
    val splitted_name = split_name name
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   190
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   191
    if length splitted_name > 0 andalso
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   192
       String.isSuffix "_case" (List.last splitted_name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   193
    then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   194
      (List.take (splitted_name, length splitted_name - 1)) @ ["split"]
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32950
diff changeset
   195
      |> space_implode "."
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   196
      |> PureThy.get_thm thy
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   197
      |> SOME
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   198
      handle ERROR msg => NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   199
    else NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   200
  end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   201
  | find_split_thm _ _ = NONE
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   202
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   203
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
   204
  | 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
   205
  | 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
   206
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   207
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
   208
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   209
fun folds_map f xs y =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   210
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   211
    fun folds_map' acc [] y = [(rev acc, y)]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   212
      | 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
   213
        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
   214
    in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   215
      folds_map' [] xs y
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   216
    end;
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   217
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   218
fun mk_prems thy (lookup_pred, get_nparams) t (names, prems) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   219
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   220
    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
   221
      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
   222
        [(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
   223
      else [(lookup_pred t, (names, prems))]
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   224
    | mk_prems' (t as Free (f, T)) (names, prems) = 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   225
      [(lookup_pred t, (names, prems))]
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   226
    | mk_prems' (t as Abs _) (names, prems) =
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   227
      if Predicate_Compile_Aux.is_predT (fastype_of t) then
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   228
      [(t, (names, prems))] else error "mk_prems': Abs "
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   229
      (* mk_param *)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   230
    | mk_prems' t (names, prems) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   231
      if Predicate_Compile_Aux.is_constrt thy t then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   232
        [(t, (names, prems))]
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   233
      else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   234
        if has_split_rule_term' thy (fst (strip_comb t)) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   235
          let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   236
            val (f, args) = strip_comb t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   237
            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
   238
            (* TODO: contextify things - this line is to unvarify the split_thm *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   239
            (*val ((_, [isplit_thm]), _) = Variable.import true [split_thm] (ProofContext.init thy)*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   240
            val (assms, concl) = Logic.strip_horn (Thm.prop_of split_thm)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   241
            val (P, [split_t]) = strip_comb (HOLogic.dest_Trueprop concl) 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   242
            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
   243
            val (_, split_args) = strip_comb split_t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   244
            val match = split_args ~~ args
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   245
            fun mk_prems_of_assm assm =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   246
              let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   247
                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
   248
                val var_names = Name.variant_list names (map fst vTs)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   249
                val vars = map Free (var_names ~~ (map snd vTs))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   250
                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
   251
                val (_, [inner_t]) = strip_comb (HOLogic.dest_Trueprop pre_res)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   252
              in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   253
                mk_prems' inner_t (var_names @ names, prems' @ prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   254
              end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   255
          in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   256
            maps mk_prems_of_assm assms
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   257
          end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   258
        else
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 32740
diff changeset
   259
          let
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   260
            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
   261
            (* 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
   262
              simple types and function types *)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   263
            val resname = Name.variant names "res"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   264
            val resvar = Free (resname, body_type (fastype_of t))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   265
            val names' = resname :: names
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   266
            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
   267
              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
   268
                folds_map mk_prems' args (names', prems) |>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   269
                map
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   270
                  (fn (argvs, (names'', prems')) =>
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 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
   273
                  in (names'', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   274
              else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   275
                let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   276
                  val pred = lookup_pred t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   277
                  val nparams = get_nparams pred
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   278
                  val (params, args) = chop nparams args
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33123
diff changeset
   279
                  val params' = map (mk_param thy lookup_pred) params
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   280
                in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   281
                  folds_map mk_prems' args (names', prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   282
                  |> map (fn (argvs, (names'', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   283
                    let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   284
                      val prem = HOLogic.mk_Trueprop (list_comb (pred, params' @ argvs @ [resvar]))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   285
                    in (names'', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   286
                end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   287
            | mk_prems'' (t as Free (_, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   288
                let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   289
                  (* higher order argument call *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   290
                  val pred = lookup_pred t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   291
                in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   292
                  folds_map mk_prems' args (resname :: names, prems)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   293
                  |> map (fn (argvs, (names', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   294
                     let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   295
                       val prem = HOLogic.mk_Trueprop (list_comb (pred, argvs @ [resvar]))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   296
                     in (names', prem :: prems') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   297
                end
33108
9d9afd478016 added test for higher-order function inductification; added debug messages
bulwahn
parents: 32740
diff changeset
   298
            | mk_prems'' t =
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   299
              error ("Invalid term: " ^ Syntax.string_of_term_global thy t)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   300
          in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   301
            map (pair resvar) (mk_prems'' f)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   302
          end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   303
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   304
    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
   305
  end;
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   306
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   307
(* assumption: mutual recursive predicates all have the same parameters. *)  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   308
fun define_predicates specs thy =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   309
  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
   310
    ([], thy)
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   311
  else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   312
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   313
    val consts = map fst specs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   314
    val eqns = maps snd specs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   315
    (*val eqns = maps (Predicate_Compile_Preproc_Data.get_specification thy) consts*)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   316
      (* create prednames *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   317
    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
   318
    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
   319
    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
   320
    val preds = map pred_of funs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   321
    val prednames = map (fst o dest_Free) preds
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   322
    val funnames = map (fst o dest_Const) funs
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   323
    val fun_pred_names = (funnames ~~ prednames)  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   324
      (* mapping from term (Free or Const) to term *)
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33123
diff changeset
   325
    fun lookup_pred (Const (name, T)) =
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   326
      (case (Symtab.lookup (Pred_Compile_Preproc.get thy) name) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   327
          SOME c => Const (c, pred_type T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   328
        | NONE =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   329
          (case AList.lookup op = fun_pred_names name of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   330
            SOME f => Free (f, pred_type T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   331
          | NONE => Const (name, T)))
33129
3085da75ed54 changed import_intros to handle parameters differently; changed handling of higher-order function compilation; reverted MicroJava change; tuned
bulwahn
parents: 33123
diff changeset
   332
      | lookup_pred (Free (name, T)) =
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   333
        if member op = (map fst pnames) name then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   334
          Free (name, transform_ho_typ T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   335
        else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   336
          Free (name, T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   337
      | lookup_pred t =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   338
         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
   339
     
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   340
        (* mapping from term (predicate term, not function term!) to int *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   341
    fun get_nparams (Const (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   342
      the_default 0 (try (ind_package_get_nparams thy) name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   343
    | get_nparams (Free (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   344
        (if member op = prednames name then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   345
          length pnames
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   346
        else 0)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   347
    | 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
   348
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   349
    (* create intro rules *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   350
  
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   351
    fun mk_intros ((func, pred), (args, rhs)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   352
      if (body_type (fastype_of func) = @{typ bool}) then
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   353
       (*TODO: preprocess predicate definition of rhs *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   354
        [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
   355
      else
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   356
        let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   357
          val names = Term.add_free_names rhs []
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   358
        in mk_prems thy (lookup_pred, get_nparams) rhs (names, [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   359
          |> map (fn (resultt, (names', prems)) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   360
            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
   361
        end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   362
    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
   363
  in
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   364
    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
   365
      NONE => ([], thy) 
33123
3c7c4372f9ad cleaned up debugging messages; added options to code_pred command
bulwahn
parents: 33122
diff changeset
   366
    | 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
   367
        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
   368
          let
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   369
            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
   370
              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
   371
                {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
   372
                  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
   373
                  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
   374
                (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
   375
                pnames
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   376
                (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
   377
                [] thy
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   378
            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
   379
            (* 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
   380
            (* add constants to my table *)
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   381
            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
   382
            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
   383
          in
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   384
            (specs, thy'')
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   385
          end
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   386
        else
33122
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   387
          let
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   388
            val _ = tracing "Introduction rules of function_predicate are not welltyped"
7d01480cc8e3 added first support for higher-order function translation
bulwahn
parents: 33114
diff changeset
   389
          in ([], thy) end
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   390
  end
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
(* preprocessing intro rules - uses oracle *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   393
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   394
(* theory -> thm -> thm *)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   395
fun rewrite_intro thy intro =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   396
  let
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   397
    fun lookup_pred (Const (name, T)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   398
      (case (Symtab.lookup (Pred_Compile_Preproc.get thy) name) of
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   399
        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
   400
      | NONE => error ("Function " ^ name ^ " is not inductified"))
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   401
    | lookup_pred (Free (name, T)) = Free (name, T)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   402
    | lookup_pred _ = error "lookup function is not defined!"
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   403
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   404
    fun get_nparams (Const (name, _)) =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   405
      the_default 0 (try (ind_package_get_nparams thy) name)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   406
    | get_nparams (Free _) = 0
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   407
    | 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
   408
    
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   409
    val intro_t = (Logic.unvarify o prop_of) intro
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   410
    val (prems, concl) = Logic.strip_horn intro_t
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   411
    val frees = map fst (Term.add_frees intro_t [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   412
    fun rewrite prem names =
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   413
      let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   414
        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
   415
        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
   416
            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
   417
          | NONE => (t, I)
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   418
        val (P, args) = (strip_comb lit) 
32668
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   419
      in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   420
        folds_map (
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   421
          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
   422
            else mk_prems thy (lookup_pred, get_nparams) t) args (names, [])
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   423
        |> map (fn (resargs, (names', prems')) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   424
          let
32672
90f3ce5d27ae added first version of quickcheck based on the predicate compiler; added a few quickcheck examples
bulwahn
parents: 32668
diff changeset
   425
            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
   426
          in (prem'::prems', names') end)
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   427
      end
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   428
    val intro_ts' = folds_map rewrite prems frees
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   429
      |> maps (fn (prems', frees') =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   430
        rewrite concl frees'
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   431
        |> map (fn (concl'::conclprems, _) =>
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   432
          Logic.list_implies ((flat prems') @ conclprems, concl')))
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   433
  in
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   434
    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
   435
  end; 
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   436
b2de45007537 added first prototype of the extended predicate compiler
bulwahn
parents:
diff changeset
   437
end;