src/HOL/Tools/SMT2/smt2_normalize.ML
author blanchet
Thu Jun 12 01:00:49 2014 +0200 (2014-06-12)
changeset 57229 489083abce44
parent 57165 7b1bf424ec5f
child 57230 ec5ff6bb2a92
permissions -rw-r--r--
tuning
     1 (*  Title:      HOL/Tools/SMT2/smt2_normalize.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 Normalization steps on theorems required by SMT solvers.
     5 *)
     6 
     7 signature SMT2_NORMALIZE =
     8 sig
     9   val drop_fact_warning: Proof.context -> thm -> unit
    10   val atomize_conv: Proof.context -> conv
    11 
    12   val special_quant_table: (string * thm) list
    13   val case_bool_entry: string * thm
    14   val abs_min_max_table: (string * thm) list
    15 
    16   type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
    17   val add_extra_norm: SMT2_Util.class * extra_norm -> Context.generic -> Context.generic
    18   val normalize: Proof.context -> thm list -> (int * thm) list
    19 end;
    20 
    21 structure SMT2_Normalize: SMT2_NORMALIZE =
    22 struct
    23 
    24 fun drop_fact_warning ctxt =
    25   SMT2_Config.verbose_msg ctxt (prefix "Warning: dropping assumption: " o
    26     Display.string_of_thm ctxt)
    27 
    28 
    29 (* general theorem normalizations *)
    30 
    31 (** instantiate elimination rules **)
    32  
    33 local
    34   val (cpfalse, cfalse) = `SMT2_Util.mk_cprop (Thm.cterm_of @{theory} @{const False})
    35 
    36   fun inst f ct thm =
    37     let val cv = f (Drule.strip_imp_concl (Thm.cprop_of thm))
    38     in Thm.instantiate ([], [(cv, ct)]) thm end
    39 in
    40 
    41 fun instantiate_elim thm =
    42   (case Thm.concl_of thm of
    43     @{const Trueprop} $ Var (_, @{typ bool}) => inst Thm.dest_arg cfalse thm
    44   | Var _ => inst I cpfalse thm
    45   | _ => thm)
    46 
    47 end
    48 
    49 
    50 (** normalize definitions **)
    51 
    52 fun norm_def thm =
    53   (case Thm.prop_of thm of
    54     @{const Trueprop} $ (Const (@{const_name HOL.eq}, _) $ _ $ Abs _) =>
    55       norm_def (thm RS @{thm fun_cong})
    56   | Const (@{const_name Pure.eq}, _) $ _ $ Abs _ => norm_def (thm RS @{thm meta_eq_to_obj_eq})
    57   | _ => thm)
    58 
    59 
    60 (** atomization **)
    61 
    62 fun atomize_conv ctxt ct =
    63   (case Thm.term_of ct of
    64     @{const Pure.imp} $ _ $ _ =>
    65       Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_imp}
    66   | Const (@{const_name Pure.eq}, _) $ _ $ _ =>
    67       Conv.binop_conv (atomize_conv ctxt) then_conv Conv.rewr_conv @{thm atomize_eq}
    68   | Const (@{const_name Pure.all}, _) $ Abs _ =>
    69       Conv.binder_conv (atomize_conv o snd) ctxt then_conv Conv.rewr_conv @{thm atomize_all}
    70   | _ => Conv.all_conv) ct
    71 
    72 val setup_atomize =
    73   fold SMT2_Builtin.add_builtin_fun_ext'' [@{const_name Pure.imp}, @{const_name Pure.eq},
    74     @{const_name Pure.all}, @{const_name Trueprop}]
    75 
    76 
    77 (** unfold special quantifiers **)
    78 
    79 val special_quant_table = [
    80   (@{const_name Ex1}, @{thm Ex1_def_raw}),
    81   (@{const_name Ball}, @{thm Ball_def_raw}),
    82   (@{const_name Bex}, @{thm Bex_def_raw})]
    83 
    84 local
    85   fun special_quant (Const (n, _)) = AList.lookup (op =) special_quant_table n
    86     | special_quant _ = NONE
    87 
    88   fun special_quant_conv _ ct =
    89     (case special_quant (Thm.term_of ct) of
    90       SOME thm => Conv.rewr_conv thm
    91     | NONE => Conv.all_conv) ct
    92 in
    93 
    94 fun unfold_special_quants_conv ctxt =
    95   SMT2_Util.if_exists_conv (is_some o special_quant) (Conv.top_conv special_quant_conv ctxt)
    96 
    97 val setup_unfolded_quants = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) special_quant_table
    98 
    99 end
   100 
   101 
   102 (** trigger inference **)
   103 
   104 local
   105   (*** check trigger syntax ***)
   106 
   107   fun dest_trigger (Const (@{const_name pat}, _) $ _) = SOME true
   108     | dest_trigger (Const (@{const_name nopat}, _) $ _) = SOME false
   109     | dest_trigger _ = NONE
   110 
   111   fun eq_list [] = false
   112     | eq_list (b :: bs) = forall (equal b) bs
   113 
   114   fun proper_trigger t =
   115     t
   116     |> these o try HOLogic.dest_list
   117     |> map (map_filter dest_trigger o these o try HOLogic.dest_list)
   118     |> (fn [] => false | bss => forall eq_list bss)
   119 
   120   fun proper_quant inside f t =
   121     (case t of
   122       Const (@{const_name All}, _) $ Abs (_, _, u) => proper_quant true f u
   123     | Const (@{const_name Ex}, _) $ Abs (_, _, u) => proper_quant true f u
   124     | @{const trigger} $ p $ u =>
   125         (if inside then f p else false) andalso proper_quant false f u
   126     | Abs (_, _, u) => proper_quant false f u
   127     | u1 $ u2 => proper_quant false f u1 andalso proper_quant false f u2
   128     | _ => true)
   129 
   130   fun check_trigger_error ctxt t =
   131     error ("SMT triggers must only occur under quantifier and multipatterns " ^
   132       "must have the same kind: " ^ Syntax.string_of_term ctxt t)
   133 
   134   fun check_trigger_conv ctxt ct =
   135     if proper_quant false proper_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   136     else check_trigger_error ctxt (Thm.term_of ct)
   137 
   138 
   139   (*** infer simple triggers ***)
   140 
   141   fun dest_cond_eq ct =
   142     (case Thm.term_of ct of
   143       Const (@{const_name HOL.eq}, _) $ _ $ _ => Thm.dest_binop ct
   144     | @{const HOL.implies} $ _ $ _ => dest_cond_eq (Thm.dest_arg ct)
   145     | _ => raise CTERM ("no equation", [ct]))
   146 
   147   fun get_constrs thy (Type (n, _)) = these (Datatype.get_constrs thy n)
   148     | get_constrs _ _ = []
   149 
   150   fun is_constr thy (n, T) =
   151     let fun match (m, U) = m = n andalso Sign.typ_instance thy (T, U)
   152     in can (the o find_first match o get_constrs thy o Term.body_type) T end
   153 
   154   fun is_constr_pat thy t =
   155     (case Term.strip_comb t of
   156       (Free _, []) => true
   157     | (Const c, ts) => is_constr thy c andalso forall (is_constr_pat thy) ts
   158     | _ => false)
   159 
   160   fun is_simp_lhs ctxt t =
   161     (case Term.strip_comb t of
   162       (Const c, ts as _ :: _) =>
   163         not (SMT2_Builtin.is_builtin_fun_ext ctxt c ts) andalso
   164         forall (is_constr_pat (Proof_Context.theory_of ctxt)) ts
   165     | _ => false)
   166 
   167   fun has_all_vars vs t =
   168     subset (op aconv) (vs, map Free (Term.add_frees t []))
   169 
   170   fun minimal_pats vs ct =
   171     if has_all_vars vs (Thm.term_of ct) then
   172       (case Thm.term_of ct of
   173         _ $ _ =>
   174           (case pairself (minimal_pats vs) (Thm.dest_comb ct) of
   175             ([], []) => [[ct]]
   176           | (ctss, ctss') => union (eq_set (op aconvc)) ctss ctss')
   177       | _ => [])
   178     else []
   179 
   180   fun proper_mpat _ _ _ [] = false
   181     | proper_mpat thy gen u cts =
   182         let
   183           val tps = (op ~~) (`gen (map Thm.term_of cts))
   184           fun some_match u = tps |> exists (fn (t', t) =>
   185             Pattern.matches thy (t', u) andalso not (t aconv u))
   186         in not (Term.exists_subterm some_match u) end
   187 
   188   val pat = SMT2_Util.mk_const_pat @{theory} @{const_name SMT2.pat} SMT2_Util.destT1
   189   fun mk_pat ct = Thm.apply (SMT2_Util.instT' ct pat) ct
   190 
   191   fun mk_clist T = pairself (Thm.cterm_of @{theory}) (HOLogic.cons_const T, HOLogic.nil_const T)
   192   fun mk_list (ccons, cnil) f cts = fold_rev (Thm.mk_binop ccons o f) cts cnil
   193   val mk_pat_list = mk_list (mk_clist @{typ SMT2.pattern})
   194   val mk_mpat_list = mk_list (mk_clist @{typ "SMT2.pattern list"})  
   195   fun mk_trigger ctss = mk_mpat_list (mk_pat_list mk_pat) ctss
   196 
   197   val trigger_eq = mk_meta_eq @{lemma "p = SMT2.trigger t p" by (simp add: trigger_def)}
   198 
   199   fun insert_trigger_conv [] ct = Conv.all_conv ct
   200     | insert_trigger_conv ctss ct =
   201         let val (ctr, cp) = Thm.dest_binop (Thm.rhs_of trigger_eq) ||> rpair ct
   202         in Thm.instantiate ([], [cp, (ctr, mk_trigger ctss)]) trigger_eq end
   203 
   204   fun infer_trigger_eq_conv outer_ctxt (ctxt, cvs) ct =
   205     let
   206       val (lhs, rhs) = dest_cond_eq ct
   207 
   208       val vs = map Thm.term_of cvs
   209       val thy = Proof_Context.theory_of ctxt
   210 
   211       fun get_mpats ct =
   212         if is_simp_lhs ctxt (Thm.term_of ct) then minimal_pats vs ct
   213         else []
   214       val gen = Variable.export_terms ctxt outer_ctxt
   215       val filter_mpats = filter (proper_mpat thy gen (Thm.term_of rhs))
   216 
   217     in insert_trigger_conv (filter_mpats (get_mpats lhs)) ct end
   218 
   219   fun has_trigger (@{const SMT2.trigger} $ _ $ _) = true
   220     | has_trigger _ = false
   221 
   222   fun try_trigger_conv cv ct =
   223     if SMT2_Util.under_quant has_trigger (SMT2_Util.term_of ct) then Conv.all_conv ct
   224     else Conv.try_conv cv ct
   225 
   226   fun infer_trigger_conv ctxt =
   227     if Config.get ctxt SMT2_Config.infer_triggers then
   228       try_trigger_conv (SMT2_Util.under_quant_conv (infer_trigger_eq_conv ctxt) ctxt)
   229     else Conv.all_conv
   230 in
   231 
   232 fun trigger_conv ctxt =
   233   SMT2_Util.prop_conv (check_trigger_conv ctxt then_conv infer_trigger_conv ctxt)
   234 
   235 val setup_trigger =
   236   fold SMT2_Builtin.add_builtin_fun_ext''
   237     [@{const_name SMT2.pat}, @{const_name SMT2.nopat}, @{const_name SMT2.trigger}]
   238 
   239 end
   240 
   241 
   242 (** combined general normalizations **)
   243 
   244 fun gen_normalize1_conv ctxt =
   245   atomize_conv ctxt then_conv
   246   unfold_special_quants_conv ctxt then_conv
   247   Thm.beta_conversion true then_conv
   248   trigger_conv ctxt
   249 
   250 fun gen_normalize1 ctxt =
   251   instantiate_elim #>
   252   norm_def #>
   253   Conv.fconv_rule (Thm.beta_conversion true then_conv Thm.eta_conversion) #>
   254   Drule.forall_intr_vars #>
   255   Conv.fconv_rule (gen_normalize1_conv ctxt) #>
   256   (* Z3 4.3.1 silently normalizes "P --> Q --> R" to "P & Q --> R" *)
   257   Raw_Simplifier.rewrite_rule ctxt @{thms HOL.imp_conjL[symmetric, THEN eq_reflection]}
   258 
   259 fun gen_norm1_safe ctxt (i, thm) =
   260   (case try (gen_normalize1 ctxt) thm of
   261     SOME thm' => SOME (i, thm')
   262   | NONE => (drop_fact_warning ctxt thm; NONE))
   263 
   264 fun gen_normalize ctxt iwthms = map_filter (gen_norm1_safe ctxt) iwthms
   265 
   266 
   267 (* unfolding of definitions and theory-specific rewritings *)
   268 
   269 fun expand_head_conv cv ct =
   270   (case Thm.term_of ct of
   271     _ $ _ =>
   272       Conv.fun_conv (expand_head_conv cv) then_conv
   273       Conv.try_conv (Thm.beta_conversion false)
   274   | _ => cv) ct
   275 
   276 
   277 (** rewrite bool case expressions as if expressions **)
   278 
   279 val case_bool_entry = (@{const_name "bool.case_bool"}, @{thm case_bool_if})
   280 
   281 local
   282   fun is_case_bool (Const (@{const_name "bool.case_bool"}, _)) = true
   283     | is_case_bool _ = false
   284 
   285   fun unfold_conv _ =
   286     SMT2_Util.if_true_conv (is_case_bool o Term.head_of)
   287       (expand_head_conv (Conv.rewr_conv @{thm case_bool_if}))
   288 in
   289 
   290 fun rewrite_case_bool_conv ctxt =
   291   SMT2_Util.if_exists_conv is_case_bool (Conv.top_conv unfold_conv ctxt)
   292 
   293 val setup_case_bool = SMT2_Builtin.add_builtin_fun_ext'' @{const_name "bool.case_bool"}
   294 
   295 end
   296 
   297 
   298 (** unfold abs, min and max **)
   299 
   300 val abs_min_max_table = [
   301   (@{const_name min}, @{thm min_def_raw}),
   302   (@{const_name max}, @{thm max_def_raw}),
   303   (@{const_name abs}, @{thm abs_if_raw})]
   304 
   305 local
   306   fun abs_min_max ctxt (Const (n, Type (@{type_name fun}, [T, _]))) =
   307         (case AList.lookup (op =) abs_min_max_table n of
   308           NONE => NONE
   309         | SOME thm => if SMT2_Builtin.is_builtin_typ_ext ctxt T then SOME thm else NONE)
   310     | abs_min_max _ _ = NONE
   311 
   312   fun unfold_amm_conv ctxt ct =
   313     (case abs_min_max ctxt (Term.head_of (Thm.term_of ct)) of
   314       SOME thm => expand_head_conv (Conv.rewr_conv thm)
   315     | NONE => Conv.all_conv) ct
   316 in
   317 
   318 fun unfold_abs_min_max_conv ctxt =
   319   SMT2_Util.if_exists_conv (is_some o abs_min_max ctxt) (Conv.top_conv unfold_amm_conv ctxt)
   320   
   321 val setup_abs_min_max = fold (SMT2_Builtin.add_builtin_fun_ext'' o fst) abs_min_max_table
   322 
   323 end
   324 
   325 
   326 (** embedding of standard natural number operations into integer operations **)
   327 
   328 local
   329   val nat_embedding = @{thms nat_int' int_nat_nneg int_nat_neg}
   330 
   331   val simple_nat_ops = [
   332     @{const less (nat)}, @{const less_eq (nat)},
   333     @{const Suc}, @{const plus (nat)}, @{const minus (nat)}]
   334 
   335   val mult_nat_ops =
   336     [@{const times (nat)}, @{const div (nat)}, @{const mod (nat)}]
   337 
   338   val nat_ops = simple_nat_ops @ mult_nat_ops
   339 
   340   val nat_consts = nat_ops @ [@{const numeral (nat)},
   341     @{const zero_class.zero (nat)}, @{const one_class.one (nat)}]
   342 
   343   val nat_int_coercions = [@{const of_nat (int)}, @{const nat}]
   344 
   345   val builtin_nat_ops = nat_int_coercions @ simple_nat_ops
   346 
   347   val is_nat_const = member (op aconv) nat_consts
   348 
   349   fun is_nat_const' @{const of_nat (int)} = true
   350     | is_nat_const' t = is_nat_const t
   351 
   352   val expands = map mk_meta_eq @{thms nat_zero_as_int nat_one_as_int nat_numeral_as_int
   353     nat_less_as_int nat_leq_as_int Suc_as_int nat_plus_as_int nat_minus_as_int nat_times_as_int
   354     nat_div_as_int nat_mod_as_int}
   355 
   356   val ints = map mk_meta_eq @{thms int_0 int_1 int_Suc int_plus int_minus int_mult zdiv_int
   357     zmod_int}
   358   val int_if = mk_meta_eq @{lemma "int (if P then n else m) = (if P then int n else int m)" by simp}
   359 
   360   fun mk_number_eq ctxt i lhs =
   361     let
   362       val eq = SMT2_Util.mk_cequals lhs (Numeral.mk_cnumber @{ctyp int} i)
   363       val ctxt' = put_simpset HOL_ss ctxt addsimps @{thms Int.int_numeral}
   364       val tac = HEADGOAL (Simplifier.simp_tac ctxt')
   365     in Goal.norm_result ctxt (Goal.prove_internal ctxt [] eq (K tac)) end
   366 
   367   fun ite_conv cv1 cv2 =
   368     Conv.combination_conv (Conv.combination_conv (Conv.arg_conv cv1) cv2) cv2
   369 
   370   fun int_conv ctxt ct =
   371     (case Thm.term_of ct of
   372       @{const of_nat (int)} $ (n as (@{const numeral (nat)} $ _)) =>
   373         Conv.rewr_conv (mk_number_eq ctxt (snd (HOLogic.dest_number n)) ct)
   374     | @{const of_nat (int)} $ _ =>
   375         (Conv.rewrs_conv ints then_conv Conv.sub_conv ints_conv ctxt) else_conv
   376         (Conv.rewr_conv int_if then_conv
   377           ite_conv (nat_conv ctxt) (int_conv ctxt)) else_conv
   378         Conv.sub_conv (Conv.top_sweep_conv nat_conv) ctxt
   379     | _ => Conv.no_conv) ct
   380 
   381   and ints_conv ctxt = Conv.top_sweep_conv int_conv ctxt
   382 
   383   and expand_conv ctxt =
   384     SMT2_Util.if_conv (is_nat_const o Term.head_of)
   385       (expand_head_conv (Conv.rewrs_conv expands) then_conv ints_conv ctxt) (int_conv ctxt)
   386 
   387   and nat_conv ctxt = SMT2_Util.if_exists_conv is_nat_const' (Conv.top_sweep_conv expand_conv ctxt)
   388 
   389   val uses_nat_int = Term.exists_subterm (member (op aconv) nat_int_coercions)
   390 in
   391 
   392 val nat_as_int_conv = nat_conv
   393 
   394 fun add_nat_embedding thms =
   395   if exists (uses_nat_int o Thm.prop_of) thms then (thms, nat_embedding) else (thms, [])
   396 
   397 val setup_nat_as_int =
   398   SMT2_Builtin.add_builtin_typ_ext (@{typ nat}, K true) #>
   399   fold (SMT2_Builtin.add_builtin_fun_ext' o Term.dest_Const) builtin_nat_ops
   400 
   401 end
   402 
   403 
   404 (** normalize numerals **)
   405 
   406 local
   407   (*
   408     rewrite Numeral1 into 1
   409     rewrite - 0 into 0
   410   *)
   411 
   412   fun is_irregular_number (Const (@{const_name numeral}, _) $ Const (@{const_name num.One}, _)) =
   413         true
   414     | is_irregular_number (Const (@{const_name uminus}, _) $ Const (@{const_name Groups.zero}, _)) =
   415         true
   416     | is_irregular_number _ =
   417         false;
   418 
   419   fun is_strange_number ctxt t = is_irregular_number t andalso SMT2_Builtin.is_builtin_num ctxt t;
   420 
   421   val proper_num_ss =
   422     simpset_of (put_simpset HOL_ss @{context}
   423       addsimps @{thms Num.numeral_One minus_zero})
   424 
   425   fun norm_num_conv ctxt =
   426     SMT2_Util.if_conv (is_strange_number ctxt) (Simplifier.rewrite (put_simpset proper_num_ss ctxt))
   427       Conv.no_conv
   428 in
   429 
   430 fun normalize_numerals_conv ctxt =
   431   SMT2_Util.if_exists_conv (is_strange_number ctxt) (Conv.top_sweep_conv norm_num_conv ctxt)
   432 
   433 end
   434 
   435 
   436 (** combined unfoldings and rewritings **)
   437 
   438 fun unfold_conv ctxt =
   439   rewrite_case_bool_conv ctxt then_conv
   440   unfold_abs_min_max_conv ctxt then_conv
   441   nat_as_int_conv ctxt then_conv
   442   Thm.beta_conversion true
   443 
   444 fun unfold1 ctxt = map (apsnd (Conv.fconv_rule (unfold_conv ctxt)))
   445 
   446 fun burrow_ids f ithms =
   447   let
   448     val (is, thms) = split_list ithms
   449     val (thms', extra_thms) = f thms
   450   in (is ~~ thms') @ map (pair ~1) extra_thms end
   451 
   452 fun unfold2 ctxt ithms =
   453   ithms
   454   |> map (apsnd (Conv.fconv_rule (normalize_numerals_conv ctxt)))
   455   |> burrow_ids add_nat_embedding
   456 
   457 
   458 (* overall normalization *)
   459 
   460 type extra_norm = Proof.context -> thm list * thm list -> thm list * thm list
   461 
   462 structure Extra_Norms = Generic_Data
   463 (
   464   type T = extra_norm SMT2_Util.dict
   465   val empty = []
   466   val extend = I
   467   fun merge data = SMT2_Util.dict_merge fst data
   468 )
   469 
   470 fun add_extra_norm (cs, norm) = Extra_Norms.map (SMT2_Util.dict_update (cs, norm))
   471 
   472 fun apply_extra_norms ctxt ithms =
   473   let
   474     val cs = SMT2_Config.solver_class_of ctxt
   475     val es = SMT2_Util.dict_lookup (Extra_Norms.get (Context.Proof ctxt)) cs
   476   in burrow_ids (fold (fn e => e ctxt) es o rpair []) ithms end
   477 
   478 local
   479   val ignored = member (op =) [@{const_name All}, @{const_name Ex},
   480     @{const_name Let}, @{const_name If}, @{const_name HOL.eq}]
   481 
   482   val schematic_consts_of =
   483     let
   484       fun collect (@{const SMT2.trigger} $ p $ t) =
   485             collect_trigger p #> collect t
   486         | collect (t $ u) = collect t #> collect u
   487         | collect (Abs (_, _, t)) = collect t
   488         | collect (t as Const (n, _)) = 
   489             if not (ignored n) then Monomorph.add_schematic_consts_of t else I
   490         | collect _ = I
   491       and collect_trigger t =
   492         let val dest = these o try HOLogic.dest_list 
   493         in fold (fold collect_pat o dest) (dest t) end
   494       and collect_pat (Const (@{const_name SMT2.pat}, _) $ t) = collect t
   495         | collect_pat (Const (@{const_name SMT2.nopat}, _) $ t) = collect t
   496         | collect_pat _ = I
   497     in (fn t => collect t Symtab.empty) end
   498 in
   499 
   500 fun monomorph ctxt xthms =
   501   let val (xs, thms) = split_list xthms
   502   in
   503     map (pair 1) thms
   504     |> Monomorph.monomorph schematic_consts_of ctxt
   505     |> maps (uncurry (map o pair)) o map2 pair xs o map (map snd)
   506   end
   507 
   508 end
   509 
   510 fun normalize ctxt wthms =
   511   wthms
   512   |> map_index I
   513   |> gen_normalize ctxt
   514   |> unfold1 ctxt
   515   |> monomorph ctxt
   516   |> unfold2 ctxt
   517   |> apply_extra_norms ctxt
   518 
   519 val _ = Theory.setup (Context.theory_map (
   520   setup_atomize #>
   521   setup_unfolded_quants #>
   522   setup_trigger #>
   523   setup_case_bool #>
   524   setup_abs_min_max #>
   525   setup_nat_as_int))
   526 
   527 end;