src/HOL/Library/Old_SMT/old_smt_utils.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59590 7ade9a33c653
child 59634 4b94cc030ba0
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
blanchet@58058
     1
(*  Title:      HOL/Library/Old_SMT/old_smt_utils.ML
boehmes@40662
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@40662
     3
boehmes@40662
     4
General utility functions.
boehmes@40662
     5
*)
boehmes@40662
     6
blanchet@58058
     7
signature OLD_SMT_UTILS =
boehmes@40662
     8
sig
boehmes@41123
     9
  (*basic combinators*)
boehmes@40662
    10
  val repeat: ('a -> 'a option) -> 'a -> 'a
boehmes@40662
    11
  val repeat_yield: ('a -> 'b -> ('a * 'b) option) -> 'a -> 'b -> 'a * 'b
boehmes@40662
    12
boehmes@41124
    13
  (*class dictionaries*)
boehmes@41124
    14
  type class = string list
boehmes@41124
    15
  val basicC: class
boehmes@41127
    16
  val string_of_class: class -> string
boehmes@41124
    17
  type 'a dict = (class * 'a) Ord_List.T
boehmes@41124
    18
  val dict_map_default: class * 'a -> ('a -> 'a) -> 'a dict -> 'a dict
boehmes@41124
    19
  val dict_update: class * 'a -> 'a dict -> 'a dict
boehmes@41124
    20
  val dict_merge: ('a * 'a -> 'a) -> 'a dict * 'a dict -> 'a dict
boehmes@41124
    21
  val dict_lookup: 'a dict -> class -> 'a list
boehmes@41127
    22
  val dict_get: 'a dict -> class -> 'a option
boehmes@41124
    23
boehmes@41123
    24
  (*types*)
boehmes@40663
    25
  val dest_funT: int -> typ -> typ list * typ
boehmes@40663
    26
boehmes@41123
    27
  (*terms*)
boehmes@40662
    28
  val dest_conj: term -> term * term
boehmes@40662
    29
  val dest_disj: term -> term * term
boehmes@41126
    30
  val under_quant: (term -> 'a) -> term -> 'a
boehmes@41280
    31
  val is_number: term -> bool
boehmes@40662
    32
boehmes@41123
    33
  (*patterns and instantiations*)
boehmes@40662
    34
  val mk_const_pat: theory -> string -> (ctyp -> 'a) -> 'a * cterm
boehmes@40662
    35
  val destT1: ctyp -> ctyp
boehmes@40662
    36
  val destT2: ctyp -> ctyp
boehmes@40662
    37
  val instTs: ctyp list -> ctyp list * cterm -> cterm
boehmes@40662
    38
  val instT: ctyp -> ctyp * cterm -> cterm
boehmes@40662
    39
  val instT': cterm -> ctyp * cterm -> cterm
boehmes@40662
    40
boehmes@41123
    41
  (*certified terms*)
boehmes@40662
    42
  val dest_cabs: cterm -> Proof.context -> cterm * Proof.context
boehmes@40662
    43
  val dest_all_cabs: cterm -> Proof.context -> cterm * Proof.context
boehmes@40662
    44
  val dest_cbinder: cterm -> Proof.context -> cterm * Proof.context
boehmes@40662
    45
  val dest_all_cbinders: cterm -> Proof.context -> cterm * Proof.context
boehmes@40662
    46
  val mk_cprop: cterm -> cterm
boehmes@40662
    47
  val dest_cprop: cterm -> cterm
boehmes@40662
    48
  val mk_cequals: cterm -> cterm -> cterm
boehmes@41172
    49
  val term_of: cterm -> term
boehmes@41172
    50
  val prop_of: thm -> term
boehmes@40662
    51
boehmes@41123
    52
  (*conversions*)
boehmes@40662
    53
  val if_conv: (term -> bool) -> conv -> conv -> conv
boehmes@40662
    54
  val if_true_conv: (term -> bool) -> conv -> conv
boehmes@41126
    55
  val if_exists_conv: (term -> bool) -> conv -> conv
boehmes@40662
    56
  val binders_conv: (Proof.context -> conv) -> Proof.context -> conv
boehmes@41126
    57
  val under_quant_conv: (Proof.context * cterm list -> conv) ->
boehmes@41126
    58
    Proof.context -> conv
boehmes@40662
    59
  val prop_conv: conv -> conv
boehmes@40662
    60
end
boehmes@40662
    61
blanchet@58058
    62
structure Old_SMT_Utils: OLD_SMT_UTILS =
boehmes@40662
    63
struct
boehmes@40662
    64
boehmes@41123
    65
(* basic combinators *)
boehmes@41123
    66
boehmes@40662
    67
fun repeat f =
boehmes@40662
    68
  let fun rep x = (case f x of SOME y => rep y | NONE => x)
boehmes@40662
    69
  in rep end
boehmes@40662
    70
boehmes@40662
    71
fun repeat_yield f =
boehmes@40662
    72
  let fun rep x y = (case f x y of SOME (x', y') => rep x' y' | NONE => (x, y))
boehmes@40662
    73
  in rep end
boehmes@40662
    74
boehmes@40662
    75
boehmes@41124
    76
(* class dictionaries *)
boehmes@41124
    77
boehmes@41124
    78
type class = string list
boehmes@41124
    79
boehmes@41124
    80
val basicC = []
boehmes@41124
    81
boehmes@41127
    82
fun string_of_class [] = "basic"
boehmes@41127
    83
  | string_of_class cs = "basic." ^ space_implode "." cs
boehmes@41127
    84
boehmes@41124
    85
type 'a dict = (class * 'a) Ord_List.T
boehmes@41124
    86
boehmes@41301
    87
fun class_ord ((cs1, _), (cs2, _)) =
boehmes@41301
    88
  rev_order (list_ord fast_string_ord (cs1, cs2))
boehmes@41124
    89
boehmes@41124
    90
fun dict_insert (cs, x) d =
boehmes@41124
    91
  if AList.defined (op =) d cs then d
boehmes@41124
    92
  else Ord_List.insert class_ord (cs, x) d
boehmes@41124
    93
boehmes@41124
    94
fun dict_map_default (cs, x) f =
boehmes@41124
    95
  dict_insert (cs, x) #> AList.map_entry (op =) cs f
boehmes@41124
    96
boehmes@41124
    97
fun dict_update (e as (_, x)) = dict_map_default e (K x)
boehmes@41124
    98
boehmes@41124
    99
fun dict_merge val_merge = sort class_ord o AList.join (op =) (K val_merge)
boehmes@41124
   100
boehmes@41124
   101
fun dict_lookup d cs =
boehmes@41124
   102
  let fun match (cs', x) = if is_prefix (op =) cs' cs then SOME x else NONE
boehmes@41124
   103
  in map_filter match d end
boehmes@41124
   104
boehmes@41127
   105
fun dict_get d cs =
boehmes@41127
   106
  (case AList.lookup (op =) d cs of
boehmes@41127
   107
    NONE => (case cs of [] => NONE | _ => dict_get d (take (length cs - 1) cs))
boehmes@41127
   108
  | SOME x => SOME x)
boehmes@41127
   109
boehmes@41124
   110
boehmes@40663
   111
(* types *)
boehmes@40663
   112
boehmes@40663
   113
val dest_funT =
boehmes@40663
   114
  let
boehmes@40663
   115
    fun dest Ts 0 T = (rev Ts, T)
boehmes@40663
   116
      | dest Ts i (Type ("fun", [T, U])) = dest (T::Ts) (i-1) U
boehmes@40663
   117
      | dest _ _ T = raise TYPE ("not a function type", [T], [])
boehmes@40663
   118
  in dest [] end
boehmes@40663
   119
boehmes@40663
   120
boehmes@40662
   121
(* terms *)
boehmes@40662
   122
boehmes@40662
   123
fun dest_conj (@{const HOL.conj} $ t $ u) = (t, u)
boehmes@40662
   124
  | dest_conj t = raise TERM ("not a conjunction", [t])
boehmes@40662
   125
boehmes@40662
   126
fun dest_disj (@{const HOL.disj} $ t $ u) = (t, u)
boehmes@40662
   127
  | dest_disj t = raise TERM ("not a disjunction", [t])
boehmes@40662
   128
boehmes@41126
   129
fun under_quant f t =
boehmes@41126
   130
  (case t of
boehmes@41126
   131
    Const (@{const_name All}, _) $ Abs (_, _, u) => under_quant f u
boehmes@41126
   132
  | Const (@{const_name Ex}, _) $ Abs (_, _, u) => under_quant f u
boehmes@41126
   133
  | _ => f t)
boehmes@41126
   134
boehmes@41280
   135
val is_number =
boehmes@41280
   136
  let
boehmes@41280
   137
    fun is_num env (Const (@{const_name If}, _) $ _ $ t $ u) =
boehmes@41280
   138
          is_num env t andalso is_num env u
boehmes@41280
   139
      | is_num env (Const (@{const_name Let}, _) $ t $ Abs (_, _, u)) =
boehmes@41280
   140
          is_num (t :: env) u
boehmes@41280
   141
      | is_num env (Bound i) = i < length env andalso is_num env (nth env i)
boehmes@41280
   142
      | is_num _ t = can HOLogic.dest_number t
boehmes@41280
   143
  in is_num [] end
boehmes@41280
   144
boehmes@40662
   145
boehmes@40662
   146
(* patterns and instantiations *)
boehmes@40662
   147
boehmes@40662
   148
fun mk_const_pat thy name destT =
wenzelm@59621
   149
  let val cpat = Thm.global_cterm_of thy (Const (name, Sign.the_const_type thy name))
wenzelm@59586
   150
  in (destT (Thm.ctyp_of_cterm cpat), cpat) end
boehmes@40662
   151
boehmes@40662
   152
val destT1 = hd o Thm.dest_ctyp
boehmes@40662
   153
val destT2 = hd o tl o Thm.dest_ctyp
boehmes@40662
   154
boehmes@40662
   155
fun instTs cUs (cTs, ct) = Thm.instantiate_cterm (cTs ~~ cUs, []) ct
boehmes@40662
   156
fun instT cU (cT, ct) = instTs [cU] ([cT], ct)
wenzelm@59586
   157
fun instT' ct = instT (Thm.ctyp_of_cterm ct)
boehmes@40662
   158
boehmes@40662
   159
boehmes@40662
   160
(* certified terms *)
boehmes@40662
   161
boehmes@40662
   162
fun dest_cabs ct ctxt =
boehmes@40662
   163
  (case Thm.term_of ct of
boehmes@40662
   164
    Abs _ =>
boehmes@40662
   165
      let val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
boehmes@40662
   166
      in (snd (Thm.dest_abs (SOME n) ct), ctxt') end
boehmes@40662
   167
  | _ => raise CTERM ("no abstraction", [ct]))
boehmes@40662
   168
boehmes@40662
   169
val dest_all_cabs = repeat_yield (try o dest_cabs) 
boehmes@40662
   170
boehmes@40662
   171
fun dest_cbinder ct ctxt =
boehmes@40662
   172
  (case Thm.term_of ct of
boehmes@40662
   173
    Const _ $ Abs _ => dest_cabs (Thm.dest_arg ct) ctxt
boehmes@40662
   174
  | _ => raise CTERM ("not a binder", [ct]))
boehmes@40662
   175
boehmes@40662
   176
val dest_all_cbinders = repeat_yield (try o dest_cbinder)
boehmes@40662
   177
wenzelm@59621
   178
val mk_cprop = Thm.apply (Thm.global_cterm_of @{theory} @{const Trueprop})
boehmes@40662
   179
boehmes@40662
   180
fun dest_cprop ct =
boehmes@40662
   181
  (case Thm.term_of ct of
boehmes@40662
   182
    @{const Trueprop} $ _ => Thm.dest_arg ct
boehmes@40662
   183
  | _ => raise CTERM ("not a property", [ct]))
boehmes@40662
   184
wenzelm@56245
   185
val equals = mk_const_pat @{theory} @{const_name Pure.eq} destT1
boehmes@40662
   186
fun mk_cequals ct cu = Thm.mk_binop (instT' ct equals) ct cu
boehmes@40662
   187
boehmes@41172
   188
val dest_prop = (fn @{const Trueprop} $ t => t | t => t)
boehmes@41172
   189
fun term_of ct = dest_prop (Thm.term_of ct)
boehmes@41172
   190
fun prop_of thm = dest_prop (Thm.prop_of thm)
boehmes@41172
   191
boehmes@40662
   192
boehmes@40662
   193
(* conversions *)
boehmes@40662
   194
boehmes@40663
   195
fun if_conv pred cv1 cv2 ct = if pred (Thm.term_of ct) then cv1 ct else cv2 ct
boehmes@40662
   196
boehmes@40663
   197
fun if_true_conv pred cv = if_conv pred cv Conv.all_conv
boehmes@40662
   198
boehmes@41126
   199
fun if_exists_conv pred = if_true_conv (Term.exists_subterm pred)
boehmes@41126
   200
boehmes@40662
   201
fun binders_conv cv ctxt =
boehmes@40662
   202
  Conv.binder_conv (binders_conv cv o snd) ctxt else_conv cv ctxt
boehmes@40662
   203
boehmes@41126
   204
fun under_quant_conv cv ctxt =
boehmes@41126
   205
  let
boehmes@41126
   206
    fun quant_conv inside ctxt cvs ct =
boehmes@41126
   207
      (case Thm.term_of ct of
boehmes@41126
   208
        Const (@{const_name All}, _) $ Abs _ =>
boehmes@41126
   209
          Conv.binder_conv (under_conv cvs) ctxt
boehmes@41126
   210
      | Const (@{const_name Ex}, _) $ Abs _ =>
boehmes@41126
   211
          Conv.binder_conv (under_conv cvs) ctxt
boehmes@41126
   212
      | _ => if inside then cv (ctxt, cvs) else Conv.all_conv) ct
boehmes@41126
   213
    and under_conv cvs (cv, ctxt) = quant_conv true ctxt (cv :: cvs)
boehmes@41126
   214
  in quant_conv false ctxt [] end
boehmes@41126
   215
boehmes@40662
   216
fun prop_conv cv ct =
boehmes@40662
   217
  (case Thm.term_of ct of
boehmes@40662
   218
    @{const Trueprop} $ _ => Conv.arg_conv cv ct
boehmes@40662
   219
  | _ => raise CTERM ("not a property", [ct]))
boehmes@40662
   220
boehmes@40662
   221
end