src/HOL/Tools/SMT/z3_replay_util.ML
author hoelzl
Fri Oct 24 15:07:51 2014 +0200 (2014-10-24)
changeset 58776 95e58e04e534
parent 58061 3d060f43accb
child 60642 48dd1cefb4ae
permissions -rw-r--r--
use NO_MATCH-simproc for distribution rules in field_simps, otherwise field_simps on '(a / (c + d)) * (e + f)' can be non-terminating
     1 (*  Title:      HOL/Tools/SMT/z3_replay_util.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 Helper functions required for Z3 proof replay.
     5 *)
     6 
     7 signature Z3_REPLAY_UTIL =
     8 sig
     9   (*theorem nets*)
    10   val thm_net_of: ('a -> thm) -> 'a list -> 'a Net.net
    11   val net_instances: (int * thm) Net.net -> cterm -> (int * thm) list
    12 
    13   (*proof combinators*)
    14   val under_assumption: (thm -> thm) -> cterm -> thm
    15   val discharge: thm -> thm -> thm
    16 
    17   (*a faster COMP*)
    18   type compose_data
    19   val precompose: (cterm -> cterm list) -> thm -> compose_data
    20   val precompose2: (cterm -> cterm * cterm) -> thm -> compose_data
    21   val compose: compose_data -> thm -> thm
    22 
    23   (*simpset*)
    24   val add_simproc: Simplifier.simproc -> Context.generic -> Context.generic
    25   val make_simpset: Proof.context -> thm list -> simpset
    26 end;
    27 
    28 structure Z3_Replay_Util: Z3_REPLAY_UTIL =
    29 struct
    30 
    31 (* theorem nets *)
    32 
    33 fun thm_net_of f xthms =
    34   let fun insert xthm = Net.insert_term (K false) (Thm.prop_of (f xthm), xthm)
    35   in fold insert xthms Net.empty end
    36 
    37 fun maybe_instantiate ct thm =
    38   try Thm.first_order_match (Thm.cprop_of thm, ct)
    39   |> Option.map (fn inst => Thm.instantiate inst thm)
    40 
    41 local
    42   fun instances_from_net match f net ct =
    43     let
    44       val lookup = if match then Net.match_term else Net.unify_term
    45       val xthms = lookup net (Thm.term_of ct)
    46       fun select ct = map_filter (f (maybe_instantiate ct)) xthms
    47       fun select' ct =
    48         let val thm = Thm.trivial ct
    49         in map_filter (f (try (fn rule => rule COMP thm))) xthms end
    50     in (case select ct of [] => select' ct | xthms' => xthms') end
    51 in
    52 
    53 fun net_instances net =
    54   instances_from_net false (fn f => fn (i, thm) => Option.map (pair i) (f thm))
    55     net
    56 
    57 end
    58 
    59 
    60 (* proof combinators *)
    61 
    62 fun under_assumption f ct =
    63   let val ct' = SMT_Util.mk_cprop ct in Thm.implies_intr ct' (f (Thm.assume ct')) end
    64 
    65 fun discharge p pq = Thm.implies_elim pq p
    66 
    67 
    68 (* a faster COMP *)
    69 
    70 type compose_data = cterm list * (cterm -> cterm list) * thm
    71 
    72 fun list2 (x, y) = [x, y]
    73 
    74 fun precompose f rule = (f (Thm.cprem_of rule 1), f, rule)
    75 fun precompose2 f rule = precompose (list2 o f) rule
    76 
    77 fun compose (cvs, f, rule) thm =
    78   discharge thm (Thm.instantiate ([], cvs ~~ f (Thm.cprop_of thm)) rule)
    79 
    80 
    81 (* simpset *)
    82 
    83 local
    84   val antisym_le1 = mk_meta_eq @{thm order_class.antisym_conv}
    85   val antisym_le2 = mk_meta_eq @{thm linorder_class.antisym_conv2}
    86   val antisym_less1 = mk_meta_eq @{thm linorder_class.antisym_conv1}
    87   val antisym_less2 = mk_meta_eq @{thm linorder_class.antisym_conv3}
    88 
    89   fun eq_prop t thm = HOLogic.mk_Trueprop t aconv Thm.prop_of thm
    90   fun dest_binop ((c as Const _) $ t $ u) = (c, t, u)
    91     | dest_binop t = raise TERM ("dest_binop", [t])
    92 
    93   fun prove_antisym_le ctxt t =
    94     let
    95       val (le, r, s) = dest_binop t
    96       val less = Const (@{const_name less}, Term.fastype_of le)
    97       val prems = Simplifier.prems_of ctxt
    98     in
    99       (case find_first (eq_prop (le $ s $ r)) prems of
   100         NONE =>
   101           find_first (eq_prop (HOLogic.mk_not (less $ r $ s))) prems
   102           |> Option.map (fn thm => thm RS antisym_less1)
   103       | SOME thm => SOME (thm RS antisym_le1))
   104     end
   105     handle THM _ => NONE
   106 
   107   fun prove_antisym_less ctxt t =
   108     let
   109       val (less, r, s) = dest_binop (HOLogic.dest_not t)
   110       val le = Const (@{const_name less_eq}, Term.fastype_of less)
   111       val prems = Simplifier.prems_of ctxt
   112     in
   113       (case find_first (eq_prop (le $ r $ s)) prems of
   114         NONE =>
   115           find_first (eq_prop (HOLogic.mk_not (less $ s $ r))) prems
   116           |> Option.map (fn thm => thm RS antisym_less2)
   117       | SOME thm => SOME (thm RS antisym_le2))
   118   end
   119   handle THM _ => NONE
   120 
   121   val basic_simpset =
   122     simpset_of (put_simpset HOL_ss @{context}
   123       addsimps @{thms field_simps times_divide_eq_right times_divide_eq_left arith_special
   124         arith_simps rel_simps array_rules z3div_def z3mod_def NO_MATCH_def}
   125       addsimprocs [@{simproc binary_int_div}, @{simproc binary_int_mod},
   126         Simplifier.simproc_global @{theory} "fast_int_arith" [
   127           "(m::int) < n", "(m::int) <= n", "(m::int) = n"] Lin_Arith.simproc,
   128         Simplifier.simproc_global @{theory} "antisym_le" ["(x::'a::order) <= y"] prove_antisym_le,
   129         Simplifier.simproc_global @{theory} "antisym_less" ["~ (x::'a::linorder) < y"]
   130           prove_antisym_less])
   131 
   132   structure Simpset = Generic_Data
   133   (
   134     type T = simpset
   135     val empty = basic_simpset
   136     val extend = I
   137     val merge = Simplifier.merge_ss
   138   )
   139 in
   140 
   141 fun add_simproc simproc context =
   142   Simpset.map (simpset_map (Context.proof_of context)
   143     (fn ctxt => ctxt addsimprocs [simproc])) context
   144 
   145 fun make_simpset ctxt rules =
   146   simpset_of (put_simpset (Simpset.get (Context.Proof ctxt)) ctxt addsimps rules)
   147 
   148 end
   149 
   150 end;