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