src/HOL/Tools/SMT2/smt2_normalize.ML
changeset 56078 624faeda77b5
child 56090 34bd10a9a2ad
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/SMT2/smt2_normalize.ML	Thu Mar 13 13:18:13 2014 +0100
     1.3 @@ -0,0 +1,652 @@
     1.4 +(*  Title:      HOL/Tools/SMT2/smt2_normalize.ML
     1.5 +    Author:     Sascha Boehme, TU Muenchen
     1.6 +
     1.7 +Normalization steps on theorems required by SMT solvers.
     1.8 +*)
     1.9 +
    1.10 +signature SMT2_NORMALIZE =
    1.11 +sig
    1.12 +  val drop_fact_warning: Proof.context -> thm -> unit
    1.13 +  val atomize_conv: Proof.context -> conv
    1.14 +  type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
    1.15 +  val add_extra_norm: SMT2_Utils.class * extra_norm -> Context.generic ->
    1.16 +    Context.generic
    1.17 +  val normalize: (int * (int option * thm)) list -> Proof.context ->
    1.18 +    (int * thm) list * Proof.context
    1.19 +end
    1.20 +
    1.21 +structure SMT2_Normalize: SMT2_NORMALIZE =
    1.22 +struct
    1.23 +
    1.24 +fun drop_fact_warning ctxt =
    1.25 +  SMT2_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
    1.26 +    Display.string_of_thm ctxt)
    1.27 +
    1.28 +
    1.29 +(* general theorem normalizations *)
    1.30 +
    1.31 +(** instantiate elimination rules **)
    1.32 + 
    1.33 +local
    1.34 +  val (cpfalse, cfalse) =
    1.35 +    `SMT2_Utils.mk_cprop (Thm.cterm_of @{theory} @{const False})
    1.36 +
    1.37 +  fun inst f ct thm =
    1.38 +    let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
    1.39 +    in Thm.instantiate ([], [(cv, ct)]) thm end
    1.40 +in
    1.41 +
    1.42 +fun instantiate_elim thm =
    1.43 +  (case Thm.concl_of thm of
    1.44 +    @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
    1.45 +  | Var _ => inst I cpfalse thm
    1.46 +  | _ => thm)
    1.47 +
    1.48 +end
    1.49 +
    1.50 +
    1.51 +(** normalize definitions **)
    1.52 +
    1.53 +fun norm_def thm =
    1.54 +  (case Thm.prop_of thm of
    1.55 +    @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
    1.56 +      norm_def (thm RS @{thm fun_cong})
    1.57 +  | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
    1.58 +      norm_def (thm RS @{thm meta_eq_to_obj_eq})
    1.59 +  | _ => thm)
    1.60 +
    1.61 +
    1.62 +(** atomization **)
    1.63 +
    1.64 +fun atomize_conv ctxt ct =
    1.65 +  (case Thm.term_of ct of
    1.66 +    @{const "==>"} $ _ $ _ =>
    1.67 +      Conv.binop_conv (atomize_conv ctxt) then_conv
    1.68 +      Conv.rewr_conv @{thm atomize_imp}
    1.69 +  | Const (@{const_name "=="}, _) $ _ $ _ =>
    1.70 +      Conv.binop_conv (atomize_conv ctxt) then_conv
    1.71 +      Conv.rewr_conv @{thm atomize_eq}
    1.72 +  | Const (@{const_name all}, _) $ Abs _ =>
    1.73 +      Conv.binder_conv (atomize_conv o snd) ctxt then_conv
    1.74 +      Conv.rewr_conv @{thm atomize_all}
    1.75 +  | _ => Conv.all_conv) ct
    1.76 +
    1.77 +val setup_atomize =
    1.78 +  fold SMT2_Builtin.add_builtin_fun_ext'' [@{const_name "==>"},
    1.79 +    @{const_name "=="}, @{const_name all}, @{const_name Trueprop}]
    1.80 +
    1.81 +
    1.82 +(** unfold special quantifiers **)
    1.83 +
    1.84 +local
    1.85 +  val ex1_def = mk_meta_eq @{lemma
    1.86 +    "Ex1 = (%P. EX x. P x & (ALL y. P y --> y = x))"
    1.87 +    by (rule ext) (simp only: Ex1_def)}
    1.88 +
    1.89 +  val ball_def = mk_meta_eq @{lemma "Ball = (%A P. ALL x. x : A --> P x)"
    1.90 +    by (rule ext)+ (rule Ball_def)}
    1.91 +
    1.92 +  val bex_def = mk_meta_eq @{lemma "Bex = (%A P. EX x. x : A & P x)"
    1.93 +    by (rule ext)+ (rule Bex_def)}
    1.94 +
    1.95 +  val special_quants = [(@{const_name Ex1}, ex1_def),
    1.96 +    (@{const_name Ball}, ball_def), (@{const_name Bex}, bex_def)]
    1.97 +  
    1.98 +  fun special_quant (Const (n, _)) = AList.lookup (op =) special_quants n
    1.99 +    | special_quant _ = NONE
   1.100 +
   1.101 +  fun special_quant_conv _ ct =
   1.102 +    (case special_quant (Thm.term_of ct) of
   1.103 +      SOME thm => Conv.rewr_conv thm
   1.104 +    | NONE => Conv.all_conv) ct
   1.105 +in
   1.106 +
   1.107 +fun unfold_special_quants_conv ctxt =
   1.108 +  SMT2_Utils.if_exists_conv (is_some o special_quant)
   1.109 +    (Conv.top_conv special_quant_conv ctxt)
   1.110 +
   1.111 +val setup_unfolded_quants =
   1.112 +  fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) special_quants
   1.113 +
   1.114 +end
   1.115 +
   1.116 +
   1.117 +(** trigger inference **)
   1.118 +
   1.119 +local
   1.120 +  (*** check trigger syntax ***)
   1.121 +
   1.122 +  fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
   1.123 +    | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
   1.124 +    | dest_trigger _ = NONE
   1.125 +
   1.126 +  fun eq_list [] = false
   1.127 +    | eq_list (b :: bs) = forall (equal b) bs
   1.128 +
   1.129 +  fun proper_trigger t =
   1.130 +    t
   1.131 +    |> these o try HOLogic.dest_list
   1.132 +    |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
   1.133 +    |> (fn [] => false | bss => forall eq_list bss)
   1.134 +
   1.135 +  fun proper_quant inside f t =
   1.136 +    (case t of
   1.137 +      Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
   1.138 +    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
   1.139 +    | @{const trigger} $ p $ u =>
   1.140 +        (if inside then f p else false) andalso proper_quant false f u
   1.141 +    | Abs (_, _, u) => proper_quant false f u
   1.142 +    | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
   1.143 +    | _ => true)
   1.144 +
   1.145 +  fun check_trigger_error ctxt t =
   1.146 +    error ("SMT triggers must only occur under quantifier and multipatterns " ^
   1.147 +      "must have the same kind: " ^ Syntax.string_of_term ctxt t)
   1.148 +
   1.149 +  fun check_trigger_conv ctxt ct =
   1.150 +    if proper_quant false proper_trigger (SMT2_Utils.term_of ct) then
   1.151 +      Conv.all_conv ct
   1.152 +    else check_trigger_error ctxt (Thm.term_of ct)
   1.153 +
   1.154 +
   1.155 +  (*** infer simple triggers ***)
   1.156 +
   1.157 +  fun dest_cond_eq ct =
   1.158 +    (case Thm.term_of ct of
   1.159 +      Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
   1.160 +    | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
   1.161 +    | _ => raise CTERM ("no equation", [ct]))
   1.162 +
   1.163 +  fun get_constrs thy (Type (n, _)) = these (Datatype.get_constrs thy n)
   1.164 +    | get_constrs _ _ = []
   1.165 +
   1.166 +  fun is_constr thy (n, T) =
   1.167 +    let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
   1.168 +    in can (the o find_first match o get_constrs thy o Term.body_type) T end
   1.169 +
   1.170 +  fun is_constr_pat thy t =
   1.171 +    (case Term.strip_comb t of
   1.172 +      (Free _, []) => true
   1.173 +    | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
   1.174 +    | _ => false)
   1.175 +
   1.176 +  fun is_simp_lhs ctxt t =
   1.177 +    (case Term.strip_comb t of
   1.178 +      (Const c, ts as _ :: _) =>
   1.179 +        not (SMT2_Builtin.is_builtin_fun_ext ctxt c ts) andalso
   1.180 +        forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
   1.181 +    | _ => false)
   1.182 +
   1.183 +  fun has_all_vars vs t =
   1.184 +    subset (op aconv) (vs, map Free (Term.add_frees t []))
   1.185 +
   1.186 +  fun minimal_pats vs ct =
   1.187 +    if has_all_vars vs (Thm.term_of ct) then
   1.188 +      (case Thm.term_of ct of
   1.189 +        _ $ _ =>
   1.190 +          (case pairself (minimal_pats vs) (Thm.dest_comb ct) of
   1.191 +            ([], []) => [[ct]]
   1.192 +          | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
   1.193 +      | _ => [])
   1.194 +    else []
   1.195 +
   1.196 +  fun proper_mpat _ _ _ [] = false
   1.197 +    | proper_mpat thy gen u cts =
   1.198 +        let
   1.199 +          val tps = (op ~~) (`gen (map Thm.term_of cts))
   1.200 +          fun some_match u = tps |> exists (fn (t', t) =>
   1.201 +            Pattern.matches thy (t', u) andalso not (t aconv u))
   1.202 +        in not (Term.exists_subterm some_match u) end
   1.203 +
   1.204 +  val pat =
   1.205 +    SMT2_Utils.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Utils.destT1
   1.206 +  fun mk_pat ct = Thm.apply (SMT2_Utils.instT' ct pat) ct
   1.207 +
   1.208 +  fun mk_clist T = pairself (Thm.cterm_of @{theory})
   1.209 +    (HOLogic.cons_const T, HOLogic.nil_const T)
   1.210 +  fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
   1.211 +  val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
   1.212 +  val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
   1.213 +  fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
   1.214 +
   1.215 +  val trigger_eq =
   1.216 +    mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
   1.217 +
   1.218 +  fun insert_trigger_conv [] ct = Conv.all_conv ct
   1.219 +    | insert_trigger_conv ctss ct =
   1.220 +        let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
   1.221 +        in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
   1.222 +
   1.223 +  fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
   1.224 +    let
   1.225 +      val (lhs, rhs) = dest_cond_eq ct
   1.226 +
   1.227 +      val vs = map Thm.term_of cvs
   1.228 +      val thy = Proof_Context.theory_of ctxt
   1.229 +
   1.230 +      fun get_mpats ct =
   1.231 +        if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
   1.232 +        else []
   1.233 +      val gen = Variable.export_terms ctxt outer_ctxt
   1.234 +      val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
   1.235 +
   1.236 +    in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
   1.237 +
   1.238 +  fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
   1.239 +    | has_trigger _ = false
   1.240 +
   1.241 +  fun try_trigger_conv cv ct =
   1.242 +    if SMT2_Utils.under_quant has_trigger (SMT2_Utils.term_of ct) then
   1.243 +      Conv.all_conv ct
   1.244 +    else Conv.try_conv cv ct
   1.245 +
   1.246 +  fun infer_trigger_conv ctxt =
   1.247 +    if Config.get ctxt SMT2_Config.infer_triggers then
   1.248 +      try_trigger_conv
   1.249 +        (SMT2_Utils.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
   1.250 +    else Conv.all_conv
   1.251 +in
   1.252 +
   1.253 +fun trigger_conv ctxt =
   1.254 +  SMT2_Utils.prop_conv
   1.255 +    (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
   1.256 +
   1.257 +val setup_trigger =
   1.258 +  fold SMT2_Builtin.add_builtin_fun_ext''
   1.259 +    [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.trigger}]
   1.260 +
   1.261 +end
   1.262 +
   1.263 +
   1.264 +(** adding quantifier weights **)
   1.265 +
   1.266 +local
   1.267 +  (*** check weight syntax ***)
   1.268 +
   1.269 +  val has_no_weight =
   1.270 +    not o Term.exists_subterm (fn @{const SMT2.weight} => true | _ => false)
   1.271 +
   1.272 +  fun is_weight (@{const SMT2.weight} $ w $ t) =
   1.273 +        (case try HOLogic.dest_number w of
   1.274 +          SOME (_, i) => i >= 0 andalso has_no_weight t
   1.275 +        | _ => false)
   1.276 +    | is_weight t = has_no_weight t
   1.277 +
   1.278 +  fun proper_trigger (@{const SMT2.trigger} $ _ $ t) = is_weight t
   1.279 +    | proper_trigger t = is_weight t 
   1.280 +
   1.281 +  fun check_weight_error ctxt t =
   1.282 +    error ("SMT weight must be a non-negative number and must only occur " ^
   1.283 +      "under the top-most quantifier and an optional trigger: " ^
   1.284 +      Syntax.string_of_term ctxt t)
   1.285 +
   1.286 +  fun check_weight_conv ctxt ct =
   1.287 +    if SMT2_Utils.under_quant proper_trigger (SMT2_Utils.term_of ct) then
   1.288 +      Conv.all_conv ct
   1.289 +    else check_weight_error ctxt (Thm.term_of ct)
   1.290 +
   1.291 +
   1.292 +  (*** insertion of weights ***)
   1.293 +
   1.294 +  fun under_trigger_conv cv ct =
   1.295 +    (case Thm.term_of ct of
   1.296 +      @{const SMT2.trigger} $ _ $ _ => Conv.arg_conv cv
   1.297 +    | _ => cv) ct
   1.298 +
   1.299 +  val weight_eq =
   1.300 +    mk_meta_eq @{lemma "p = SMT2.weight i p" by (simp add: weight_def)}
   1.301 +  fun mk_weight_eq w =
   1.302 +    let val cv = Thm.dest_arg1 (Thm.rhs_of weight_eq)
   1.303 +    in
   1.304 +      Thm.instantiate ([], [(cv, Numeral.mk_cnumber @{ctyp int} w)]) weight_eq
   1.305 +    end
   1.306 +
   1.307 +  fun add_weight_conv NONE _ = Conv.all_conv
   1.308 +    | add_weight_conv (SOME weight) ctxt =
   1.309 +        let val cv = Conv.rewr_conv (mk_weight_eq weight)
   1.310 +        in SMT2_Utils.under_quant_conv (K (under_trigger_conv cv)) ctxt end
   1.311 +in
   1.312 +
   1.313 +fun weight_conv weight ctxt = 
   1.314 +  SMT2_Utils.prop_conv
   1.315 +    (check_weight_conv ctxt then_conv add_weight_conv weight ctxt)
   1.316 +
   1.317 +val setup_weight = SMT2_Builtin.add_builtin_fun_ext'' @{const_name SMT2.weight}
   1.318 +
   1.319 +end
   1.320 +
   1.321 +
   1.322 +(** combined general normalizations **)
   1.323 +
   1.324 +fun gen_normalize1_conv ctxt weight =
   1.325 +  atomize_conv ctxt then_conv
   1.326 +  unfold_special_quants_conv ctxt then_conv
   1.327 +  Thm.beta_conversion true then_conv
   1.328 +  trigger_conv ctxt then_conv
   1.329 +  weight_conv weight ctxt
   1.330 +
   1.331 +fun gen_normalize1 ctxt weight thm =
   1.332 +  thm
   1.333 +  |> instantiate_elim
   1.334 +  |> norm_def
   1.335 +  |> Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion)
   1.336 +  |> Drule.forall_intr_vars
   1.337 +  |> Conv.fconv_rule (gen_normalize1_conv ctxt weight)
   1.338 +  (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
   1.339 +  |> Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
   1.340 +
   1.341 +fun gen_norm1_safe ctxt (i, (weight, thm)) =
   1.342 +  (case try (gen_normalize1 ctxt weight) thm of
   1.343 +    SOME thm' => SOME (i, thm')
   1.344 +  | NONE => (drop_fact_warning ctxt thm; NONE))
   1.345 +
   1.346 +fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
   1.347 +
   1.348 +
   1.349 +
   1.350 +(* unfolding of definitions and theory-specific rewritings *)
   1.351 +
   1.352 +fun expand_head_conv cv ct =
   1.353 +  (case Thm.term_of ct of
   1.354 +    _ $ _ =>
   1.355 +      Conv.fun_conv (expand_head_conv cv) then_conv
   1.356 +      Conv.try_conv (Thm.beta_conversion false)
   1.357 +  | _ => cv) ct
   1.358 +
   1.359 +
   1.360 +(** rewrite bool case expressions as if expressions **)
   1.361 +
   1.362 +local
   1.363 +  fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
   1.364 +    | is_case_bool _ = false
   1.365 +
   1.366 +  val thm = mk_meta_eq @{lemma
   1.367 +    "case_bool = (%x y P. if P then x else y)" by (rule ext)+ simp}
   1.368 +
   1.369 +  fun unfold_conv _ =
   1.370 +    SMT2_Utils.if_true_conv (is_case_bool o Term.head_of)
   1.371 +      (expand_head_conv (Conv.rewr_conv thm))
   1.372 +in
   1.373 +
   1.374 +fun rewrite_case_bool_conv ctxt =
   1.375 +  SMT2_Utils.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
   1.376 +
   1.377 +val setup_case_bool =
   1.378 +  SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
   1.379 +
   1.380 +end
   1.381 +
   1.382 +
   1.383 +(** unfold abs, min and max **)
   1.384 +
   1.385 +local
   1.386 +  val abs_def = mk_meta_eq @{lemma
   1.387 +    "abs = (%a::'a::abs_if. if a < 0 then - a else a)"
   1.388 +    by (rule ext) (rule abs_if)}
   1.389 +
   1.390 +  val min_def = mk_meta_eq @{lemma "min = (%a b. if a <= b then a else b)"
   1.391 +    by (rule ext)+ (rule min_def)}
   1.392 +
   1.393 +  val max_def = mk_meta_eq  @{lemma "max = (%a b. if a <= b then b else a)"
   1.394 +    by (rule ext)+ (rule max_def)}
   1.395 +
   1.396 +  val defs = [(@{const_name min}, min_def), (@{const_name max}, max_def),
   1.397 +    (@{const_name abs}, abs_def)]
   1.398 +
   1.399 +  fun is_builtinT ctxt T =
   1.400 +    SMT2_Builtin.is_builtin_typ_ext ctxt (Term.domain_type T)
   1.401 +
   1.402 +  fun abs_min_max ctxt (Const (n, T)) =
   1.403 +        (case AList.lookup (op =) defs n of
   1.404 +          NONE => NONE
   1.405 +        | SOME thm => if is_builtinT ctxt T then SOME thm else NONE)
   1.406 +    | abs_min_max _ _ = NONE
   1.407 +
   1.408 +  fun unfold_amm_conv ctxt ct =
   1.409 +    (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   1.410 +      SOME thm => expand_head_conv (Conv.rewr_conv thm)
   1.411 +    | NONE => Conv.all_conv) ct
   1.412 +in
   1.413 +
   1.414 +fun unfold_abs_min_max_conv ctxt =
   1.415 +  SMT2_Utils.if_exists_conv (is_some o abs_min_max ctxt)
   1.416 +    (Conv.top_conv unfold_amm_conv ctxt)
   1.417 +  
   1.418 +val setup_abs_min_max = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) defs
   1.419 +
   1.420 +end
   1.421 +
   1.422 +
   1.423 +(** embedding of standard natural number operations into integer operations **)
   1.424 +
   1.425 +local
   1.426 +  val nat_embedding = @{lemma
   1.427 +    "ALL n. nat (int n) = n"
   1.428 +    "ALL i. i >= 0 --> int (nat i) = i"
   1.429 +    "ALL i. i < 0 --> int (nat i) = 0"
   1.430 +    by simp_all}
   1.431 +
   1.432 +  val simple_nat_ops = [
   1.433 +    @{const less (nat)}, @{const less_eq (nat)},
   1.434 +    @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   1.435 +
   1.436 +  val mult_nat_ops =
   1.437 +    [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
   1.438 +
   1.439 +  val nat_ops = simple_nat_ops @ mult_nat_ops
   1.440 +
   1.441 +  val nat_consts = nat_ops @ [@{const numeral (nat)},
   1.442 +    @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   1.443 +
   1.444 +  val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   1.445 +
   1.446 +  val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   1.447 +
   1.448 +  val is_nat_const = member (op aconv) nat_consts
   1.449 +
   1.450 +  fun is_nat_const' @{const of_nat (int)} = true
   1.451 +    | is_nat_const' t = is_nat_const t
   1.452 +
   1.453 +  val expands = map mk_meta_eq @{lemma
   1.454 +    "0 = nat 0"
   1.455 +    "1 = nat 1"
   1.456 +    "(numeral :: num => nat) = (%i. nat (numeral i))"
   1.457 +    "op < = (%a b. int a < int b)"
   1.458 +    "op <= = (%a b. int a <= int b)"
   1.459 +    "Suc = (%a. nat (int a + 1))"
   1.460 +    "op + = (%a b. nat (int a + int b))"
   1.461 +    "op - = (%a b. nat (int a - int b))"
   1.462 +    "op * = (%a b. nat (int a * int b))"
   1.463 +    "op div = (%a b. nat (int a div int b))"
   1.464 +    "op mod = (%a b. nat (int a mod int b))"
   1.465 +    by (fastforce simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib)+}
   1.466 +
   1.467 +  val ints = map mk_meta_eq @{lemma
   1.468 +    "int 0 = 0"
   1.469 +    "int 1 = 1"
   1.470 +    "int (Suc n) = int n + 1"
   1.471 +    "int (n + m) = int n + int m"
   1.472 +    "int (n - m) = int (nat (int n - int m))"
   1.473 +    "int (n * m) = int n * int m"
   1.474 +    "int (n div m) = int n div int m"
   1.475 +    "int (n mod m) = int n mod int m"
   1.476 +    by (auto simp add: int_mult zdiv_int zmod_int)}
   1.477 +
   1.478 +  val int_if = mk_meta_eq @{lemma
   1.479 +    "int (if P then n else m) = (if P then int n else int m)"
   1.480 +    by simp}
   1.481 +
   1.482 +  fun mk_number_eq ctxt i lhs =
   1.483 +    let
   1.484 +      val eq = SMT2_Utils.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   1.485 +      val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
   1.486 +      val tac = HEADGOAL (Simplifier.simp_tac ctxt')
   1.487 +    in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
   1.488 +
   1.489 +  fun ite_conv cv1 cv2 =
   1.490 +    Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   1.491 +
   1.492 +  fun int_conv ctxt ct =
   1.493 +    (case Thm.term_of ct of
   1.494 +      @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   1.495 +        Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   1.496 +    | @{const of_nat (int)} $ _ =>
   1.497 +        (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   1.498 +        (Conv.rewr_conv int_if then_conv
   1.499 +          ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   1.500 +        Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   1.501 +    | _ => Conv.no_conv) ct
   1.502 +
   1.503 +  and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   1.504 +
   1.505 +  and expand_conv ctxt =
   1.506 +    SMT2_Utils.if_conv (is_nat_const o Term.head_of)
   1.507 +      (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt)
   1.508 +      (int_conv ctxt)
   1.509 +
   1.510 +  and nat_conv ctxt = SMT2_Utils.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
   1.511 +
   1.512 +  val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   1.513 +in
   1.514 +
   1.515 +val nat_as_int_conv = nat_conv
   1.516 +
   1.517 +fun add_nat_embedding thms =
   1.518 +  if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding)
   1.519 +  else (thms, [])
   1.520 +
   1.521 +val setup_nat_as_int =
   1.522 +  SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   1.523 +  fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   1.524 +
   1.525 +end
   1.526 +
   1.527 +
   1.528 +(** normalize numerals **)
   1.529 +
   1.530 +local
   1.531 +  (*
   1.532 +    rewrite Numeral1 into 1
   1.533 +    rewrite - 0 into 0
   1.534 +  *)
   1.535 +
   1.536 +  fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
   1.537 +        true
   1.538 +    | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
   1.539 +        true
   1.540 +    | is_irregular_number _ =
   1.541 +        false;
   1.542 +
   1.543 +  fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
   1.544 +
   1.545 +  val proper_num_ss =
   1.546 +    simpset_of (put_simpset HOL_ss @{context}
   1.547 +      addsimps @{thms Num.numeral_One minus_zero})
   1.548 +
   1.549 +  fun norm_num_conv ctxt =
   1.550 +    SMT2_Utils.if_conv (is_strange_number ctxt)
   1.551 +      (Simplifier.rewrite (put_simpset proper_num_ss ctxt)) Conv.no_conv
   1.552 +in
   1.553 +
   1.554 +fun normalize_numerals_conv ctxt =
   1.555 +  SMT2_Utils.if_exists_conv (is_strange_number ctxt)
   1.556 +    (Conv.top_sweep_conv norm_num_conv ctxt)
   1.557 +
   1.558 +end
   1.559 +
   1.560 +
   1.561 +(** combined unfoldings and rewritings **)
   1.562 +
   1.563 +fun unfold_conv ctxt =
   1.564 +  rewrite_case_bool_conv ctxt then_conv
   1.565 +  unfold_abs_min_max_conv ctxt then_conv
   1.566 +  nat_as_int_conv ctxt then_conv
   1.567 +  Thm.beta_conversion true
   1.568 +
   1.569 +fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   1.570 +
   1.571 +fun burrow_ids f ithms =
   1.572 +  let
   1.573 +    val (is, thms) = split_list ithms
   1.574 +    val (thms', extra_thms) = f thms
   1.575 +  in (is ~~ thms') @ map (pair ~1) extra_thms end
   1.576 +
   1.577 +fun unfold2 ctxt ithms =
   1.578 +  ithms
   1.579 +  |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   1.580 +  |> burrow_ids add_nat_embedding
   1.581 +
   1.582 +
   1.583 +
   1.584 +(* overall normalization *)
   1.585 +
   1.586 +type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   1.587 +
   1.588 +structure Extra_Norms = Generic_Data
   1.589 +(
   1.590 +  type T = extra_norm SMT2_Utils.dict
   1.591 +  val empty = []
   1.592 +  val extend = I
   1.593 +  fun merge data = SMT2_Utils.dict_merge fst data
   1.594 +)
   1.595 +
   1.596 +fun add_extra_norm (cs, norm) =
   1.597 +  Extra_Norms.map (SMT2_Utils.dict_update (cs, norm))
   1.598 +
   1.599 +fun apply_extra_norms ctxt ithms =
   1.600 +  let
   1.601 +    val cs = SMT2_Config.solver_class_of ctxt
   1.602 +    val es = SMT2_Utils.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   1.603 +  in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   1.604 +
   1.605 +local
   1.606 +  val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   1.607 +    @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   1.608 +
   1.609 +  val schematic_consts_of =
   1.610 +    let
   1.611 +      fun collect (@{const SMT2.trigger} $ p $ t) =
   1.612 +            collect_trigger p #> collect t
   1.613 +        | collect (t $ u) = collect t #> collect u
   1.614 +        | collect (Abs (_, _, t)) = collect t
   1.615 +        | collect (t as Const (n, _)) = 
   1.616 +            if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   1.617 +        | collect _ = I
   1.618 +      and collect_trigger t =
   1.619 +        let val dest = these o try HOLogic.dest_list 
   1.620 +        in fold (fold collect_pat o dest) (dest t) end
   1.621 +      and collect_pat (Const (@{const_name SMT2.pat}, _) $ t) = collect t
   1.622 +        | collect_pat (Const (@{const_name SMT2.nopat}, _) $ t) = collect t
   1.623 +        | collect_pat _ = I
   1.624 +    in (fn t => collect t Symtab.empty) end
   1.625 +in
   1.626 +
   1.627 +fun monomorph ctxt xthms =
   1.628 +  let val (xs, thms) = split_list xthms
   1.629 +  in
   1.630 +    map (pair 1) thms
   1.631 +    |> Monomorph.monomorph schematic_consts_of ctxt
   1.632 +    |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   1.633 +  end
   1.634 +
   1.635 +end
   1.636 +
   1.637 +fun normalize iwthms ctxt =
   1.638 +  iwthms
   1.639 +  |> gen_normalize ctxt
   1.640 +  |> unfold1 ctxt
   1.641 +  |> monomorph ctxt
   1.642 +  |> unfold2 ctxt
   1.643 +  |> apply_extra_norms ctxt
   1.644 +  |> rpair ctxt
   1.645 +
   1.646 +val _ = Theory.setup (Context.theory_map (
   1.647 +  setup_atomize #>
   1.648 +  setup_unfolded_quants #>
   1.649 +  setup_trigger #>
   1.650 +  setup_weight #>
   1.651 +  setup_case_bool #>
   1.652 +  setup_abs_min_max #>
   1.653 +  setup_nat_as_int))
   1.654 +
   1.655 +end