src/HOL/SMT/Tools/smt_normalize.ML
author haftmann
Wed, 21 Oct 2009 08:14:38 +0200
changeset 33038 8f9594c31de4
parent 33010 39f73a59e855
child 33243 17014b1b9353
permissions -rw-r--r--
dropped redundant gen_ prefix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/SMT/Tools/smt_normalize.ML
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     3
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     4
Normalization steps on theorems required by SMT solvers:
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     5
  * unfold trivial let expressions,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     6
  * replace negative numerals by negated positive numerals,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     7
  * embed natural numbers into integers,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     8
  * add extra rules specifying types and constants which occur frequently,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     9
  * lift lambda terms,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    10
  * make applications explicit for functions with varying number of arguments,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    11
  * fully translate into object logic, add universal closure. 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    12
*)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    13
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    14
signature SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    15
sig
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    16
  val normalize_rule: Proof.context -> thm -> thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    17
  val instantiate_free: Thm.cterm * Thm.cterm -> thm -> thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    18
  val discharge_definition: Thm.cterm -> thm -> thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    19
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    20
  val trivial_let: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    21
  val positive_numerals: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    22
  val nat_as_int: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    23
  val unfold_defs: bool Config.T
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    24
  val add_pair_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    25
  val add_fun_upd_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    26
  val add_abs_min_max_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    27
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    28
  datatype config =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    29
    RewriteTrivialLets |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    30
    RewriteNegativeNumerals |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    31
    RewriteNaturalNumbers |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    32
    AddPairRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    33
    AddFunUpdRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    34
    AddAbsMinMaxRules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    35
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    36
  val normalize: config list -> Proof.context -> thm list ->
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    37
    Thm.cterm list * thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    38
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    39
  val setup: theory -> theory
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    40
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    41
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    42
structure SMT_Normalize: SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    43
struct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    44
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    45
val norm_binder_conv = Conv.try_conv (More_Conv.rewrs_conv [
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    46
  @{lemma "All P == ALL x. P x" by (rule reflexive)},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    47
  @{lemma "Ex P == EX x. P x" by (rule reflexive)},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    48
  @{lemma "Let c P == let x = c in P x" by (rule reflexive)}])
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    49
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    50
fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    51
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    52
fun norm_meta_def cv thm = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    53
  let val thm' = Thm.combination thm (Thm.reflexive cv)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    54
  in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    55
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    56
fun norm_def ctxt thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    57
  (case Thm.prop_of thm of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    58
    Const (@{const_name "=="}, _) $ _ $ Abs (_, T, _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    59
      let val v = Var ((Name.uu, #maxidx (Thm.rep_thm thm) + 1), T)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    60
      in norm_def ctxt (norm_meta_def (cert ctxt v) thm) end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    61
  | @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    62
      norm_def ctxt (thm RS @{thm fun_cong})
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    63
  | _ => thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    64
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    65
fun normalize_rule ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    66
  Conv.fconv_rule (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    67
    Thm.beta_conversion true then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    68
    Thm.eta_conversion then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    69
    More_Conv.bottom_conv (K norm_binder_conv) ctxt) #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    70
  norm_def ctxt #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    71
  Drule.forall_intr_vars #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    72
  Conv.fconv_rule ObjectLogic.atomize
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    73
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    74
fun instantiate_free (cv, ct) thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    75
  if Term.exists_subterm (equal (Thm.term_of cv)) (Thm.prop_of thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    76
  then Thm.forall_elim ct (Thm.forall_intr cv thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    77
  else thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    78
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    79
fun discharge_definition ct thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    80
  let val (cv, cu) = Thm.dest_equals ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    81
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    82
    Thm.implies_intr ct thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    83
    |> instantiate_free (cv, cu)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    84
    |> (fn thm => Thm.implies_elim thm (Thm.reflexive cu))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    85
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    86
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    87
fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    88
fun if_true_conv c cv = if_conv c cv Conv.all_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    89
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    90
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    91
(* simplification of trivial let expressions (whose bound variables occur at
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    92
   most once) *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    93
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    94
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    95
  fun count i (Bound j) = if j = i then 1 else 0
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    96
    | count i (t $ u) = count i t + count i u
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    97
    | count i (Abs (_, _, t)) = count (i + 1) t
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    98
    | count _ _ = 0
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    99
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   100
  fun is_trivial_let (Const (@{const_name Let}, _) $ _ $ Abs (_, _, t)) =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   101
        (count 0 t <= 1)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   102
    | is_trivial_let _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   103
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   104
  fun let_conv _ = if_true_conv is_trivial_let (Conv.rewr_conv @{thm Let_def})
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   105
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   106
  fun cond_let_conv ctxt = if_true_conv (Term.exists_subterm is_trivial_let)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   107
    (More_Conv.top_conv let_conv ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   108
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   109
fun trivial_let ctxt = map (Conv.fconv_rule (cond_let_conv ctxt))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   110
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   111
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   112
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   113
(* rewriting of negative integer numerals into positive numerals *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   114
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   115
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   116
  fun neg_numeral @{term Int.Min} = true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   117
    | neg_numeral _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   118
  fun is_number_sort thy T = Sign.of_sort thy (T, @{sort number_ring})
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   119
  fun is_neg_number ctxt (Const (@{const_name number_of}, T) $ t) =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   120
        Term.exists_subterm neg_numeral t andalso
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   121
        is_number_sort (ProofContext.theory_of ctxt) (Term.body_type T)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   122
    | is_neg_number _ _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   123
  fun has_neg_number ctxt = Term.exists_subterm (is_neg_number ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   124
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   125
  val pos_numeral_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   126
    addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   127
    addsimps [@{thm Int.numeral_1_eq_1}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   128
    addsimps @{thms Int.pred_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   129
    addsimps @{thms Int.normalize_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   130
    addsimps @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   131
      "Int.Min = - Int.Bit1 Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   132
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   133
      "Int.Bit0 (- k) = - Int.Bit0 k"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   134
      "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   135
      by simp_all (simp add: pred_def)}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   136
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   137
  fun pos_conv ctxt = if_conv (is_neg_number ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   138
    (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   139
    Conv.no_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   140
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   141
  fun cond_pos_conv ctxt = if_true_conv (has_neg_number ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   142
    (More_Conv.top_sweep_conv pos_conv ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   143
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   144
fun positive_numerals ctxt = map (Conv.fconv_rule (cond_pos_conv ctxt))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   145
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   146
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   147
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   148
(* embedding of standard natural number operations into integer operations *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   149
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   150
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   151
  val nat_embedding = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   152
    "nat (int n) = n"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   153
    "i >= 0 --> int (nat i) = i"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   154
    "i < 0 --> int (nat i) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   155
    by simp_all}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   156
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   157
  val nat_rewriting = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   158
    "0 = nat 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   159
    "1 = nat 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   160
    "number_of i = nat (number_of i)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   161
    "int (nat 0) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   162
    "int (nat 1) = 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   163
    "a < b = (int a < int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   164
    "a <= b = (int a <= int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   165
    "Suc a = nat (int a + 1)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   166
    "a + b = nat (int a + int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   167
    "a - b = nat (int a - int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   168
    "a * b = nat (int a * int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   169
    "a div b = nat (int a div int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   170
    "a mod b = nat (int a mod int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   171
    "int (nat (int a + int b)) = int a + int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   172
    "int (nat (int a * int b)) = int a * int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   173
    "int (nat (int a div int b)) = int a div int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   174
    "int (nat (int a mod int b)) = int a mod int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   175
    by (simp add: nat_mult_distrib nat_div_distrib nat_mod_distrib
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   176
      int_mult[symmetric] zdiv_int[symmetric] zmod_int[symmetric])+}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   177
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   178
  fun on_positive num f x = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   179
    (case try HOLogic.dest_number (Thm.term_of num) of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   180
      SOME (_, i) => if i >= 0 then SOME (f x) else NONE
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   181
    | NONE => NONE)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   182
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   183
  val cancel_int_nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   184
    addsimps [@{thm Nat_Numeral.nat_number_of}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   185
    addsimps [@{thm Nat_Numeral.int_nat_number_of}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   186
    addsimps @{thms neg_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   187
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   188
  fun cancel_int_nat_simproc _ ss ct = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   189
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   190
      val num = Thm.dest_arg (Thm.dest_arg ct)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   191
      val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   192
      val simpset = Simplifier.inherit_context ss cancel_int_nat_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   193
      fun tac _ = Simplifier.simp_tac simpset 1
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   194
    in on_positive num (Goal.prove_internal [] goal) tac end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   195
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   196
  val nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   197
    addsimps nat_rewriting
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   198
    addsimprocs [Simplifier.make_simproc {
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   199
      name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}],
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   200
      proc = cancel_int_nat_simproc, identifier = [] }]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   201
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   202
  fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   203
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   204
  val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   205
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   206
fun nat_as_int ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   207
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   208
    fun norm thm uses_nat =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   209
      if not (uses_nat_type (Thm.prop_of thm)) then (thm, uses_nat)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   210
      else (Conv.fconv_rule (conv ctxt) thm, true)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   211
    val (thms', uses_nat) = fold_map norm thms false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   212
  in if uses_nat then nat_embedding @ thms' else thms' end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   213
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   214
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   215
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   216
(* include additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   217
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   218
val (unfold_defs, unfold_defs_setup) =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   219
  Attrib.config_bool "smt_unfold_defs" true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   220
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   221
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   222
  val pair_rules = [@{thm fst_conv}, @{thm snd_conv}, @{thm pair_collapse}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   223
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   224
  val pair_type = (fn Type (@{type_name "*"}, _) => true | _ => false)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   225
  val exists_pair_type = Term.exists_type (Term.exists_subtype pair_type)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   226
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   227
  val fun_upd_rules = [@{thm fun_upd_same}, @{thm fun_upd_apply}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   228
  val is_fun_upd = (fn Const (@{const_name fun_upd}, _) => true | _ => false)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   229
  val exists_fun_upd = Term.exists_subterm is_fun_upd
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   230
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   231
fun add_pair_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   232
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   233
  |> exists (exists_pair_type o Thm.prop_of) thms ? append pair_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   234
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   235
fun add_fun_upd_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   236
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   237
  |> exists (exists_fun_upd o Thm.prop_of) thms ? append fun_upd_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   238
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   239
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   240
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   241
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   242
  fun mk_entry t thm = (Term.head_of t, (thm, thm RS @{thm eq_reflection}))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   243
  fun prepare_def thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   244
    (case HOLogic.dest_Trueprop (Thm.prop_of thm) of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   245
      Const (@{const_name "op ="}, _) $ t $ _ => mk_entry t thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   246
    | t => raise TERM ("prepare_def", [t]))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   247
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   248
  val defs = map prepare_def [
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   249
    @{thm abs_if[where 'a = int]}, @{thm abs_if[where 'a = real]},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   250
    @{thm min_def[where 'a = int]}, @{thm min_def[where 'a = real]},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   251
    @{thm max_def[where 'a = int]}, @{thm max_def[where 'a = real]}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   252
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   253
  fun add_sym t = if AList.defined (op =) defs t then insert (op =) t else I
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   254
  fun add_syms thms = fold (Term.fold_aterms add_sym o Thm.prop_of) thms []
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   255
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   256
  fun unfold_conv ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   257
    (case AList.lookup (op =) defs (Term.head_of (Thm.term_of ct)) of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   258
      SOME (_, eq) => Conv.rewr_conv eq
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   259
    | NONE => Conv.all_conv) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   260
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   261
fun add_abs_min_max_rules ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   262
  if Config.get ctxt unfold_defs
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   263
  then map (Conv.fconv_rule (More_Conv.bottom_conv unfold_conv ctxt)) thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   264
  else map fst (map_filter (AList.lookup (op =) defs) (add_syms thms)) @ thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   265
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   266
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   267
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   268
(* lift lambda terms into additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   269
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   270
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   271
  val meta_eq = @{cpat "op =="}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   272
  val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   273
  fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   274
  fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   275
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   276
  val fresh_name = yield_singleton Name.variants
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   277
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   278
  fun used_vars cvs ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   279
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   280
      val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   281
      val add = (fn (SOME ct) => insert (op aconvc) ct | _ => I)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   282
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   283
  fun make_def cvs eq = Thm.symmetric (fold norm_meta_def cvs eq)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   284
  fun add_def ct thm = Termtab.update (Thm.term_of ct, (serial (), thm))
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   285
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   286
  fun replace ctxt cvs ct (cx as (nctxt, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   287
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   288
      val cvs' = used_vars cvs ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   289
      val ct' = fold Thm.cabs cvs' ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   290
      val mk_repl = fold (fn ct => fn cu => Thm.capply cu ct) cvs'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   291
    in
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   292
      (case Termtab.lookup defs (Thm.term_of ct') of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   293
        SOME (_, eq) => (make_def cvs' eq, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   294
      | NONE =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   295
          let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   296
            val {t, T, ...} = Thm.rep_cterm ct'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   297
            val (n, nctxt') = fresh_name "" nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   298
            val eq = Thm.assume (mk_meta_eq (cert ctxt (Free (n, T))) ct')
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   299
          in (make_def cvs' eq, (nctxt', add_def ct' eq defs)) end)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   300
    end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   301
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   302
  fun none ct cx = (Thm.reflexive ct, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   303
  fun in_comb f g ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   304
    let val (cu1, cu2) = Thm.dest_comb ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   305
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   306
  fun in_arg f = in_comb none f
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   307
  fun in_abs f cvs ct (nctxt, defs) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   308
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   309
      val (n, nctxt') = fresh_name Name.uu nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   310
      val (cv, cu) = Thm.dest_abs (SOME n) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   311
    in f (cv :: cvs) cu (nctxt', defs) |>> Thm.abstract_rule n cv end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   312
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   313
  fun replace_lambdas ctxt =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   314
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   315
      fun repl cvs ct =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   316
        (case Thm.term_of ct of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   317
          Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   318
        | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   319
        | Const _ $ Abs _ => in_arg (at_lambda cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   320
        | Const (@{const_name Let}, _) $ _ $ Abs _ =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   321
            in_comb (in_arg (repl cvs)) (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   322
        | Abs _ => at_lambda cvs
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   323
        | _ $ _ => in_comb (repl cvs) (repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   324
        | _ => none) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   325
      and at_lambda cvs ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   326
        let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   327
          val (thm1, cx') = in_abs repl cvs ct cx
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   328
          val (thm2, cx'') = replace ctxt cvs (Thm.rhs_of thm1) cx'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   329
        in (Thm.transitive thm1 thm2, cx'') end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   330
    in repl [] end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   331
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   332
fun lift_lambdas ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   333
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   334
    val declare_frees = fold (Thm.fold_terms Term.declare_term_frees)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   335
    fun rewrite f thm cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   336
      let val (thm', cx') = f (Thm.cprop_of thm) cx
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   337
      in (Thm.equal_elim thm' thm, cx') end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   338
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   339
    val rev_int_fst_ord = rev_order o int_ord o pairself fst
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   340
    fun ordered_values tab =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   341
      Termtab.fold (fn (_, x) => OrdList.insert rev_int_fst_ord x) tab []
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   342
      |> map snd
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   343
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   344
    val (thms', (_, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   345
      (declare_frees thms (Name.make_context []), Termtab.empty)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   346
      |> fold_map (rewrite (replace_lambdas ctxt)) thms
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   347
    val eqs = ordered_values defs
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   348
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   349
    (maps (#hyps o Thm.crep_thm) eqs, map (normalize_rule ctxt) eqs @ thms')
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   350
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   351
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   352
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   353
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   354
(* make application explicit for functions with varying number of arguments *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   355
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   356
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   357
  val const = prefix "c" and free = prefix "f"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   358
  fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   359
  fun add t i = Symtab.map_default (t, (false, i)) (min i)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   360
  fun traverse t =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   361
    (case Term.strip_comb t of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   362
      (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   363
    | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   364
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   365
    | (_, ts) => fold traverse ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   366
  val prune = (fn (n, (true, i)) => Symtab.update (n, i) | _ => I)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   367
  fun prune_tab tab = Symtab.fold prune tab Symtab.empty
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   368
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   369
  fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   370
  fun nary_conv conv1 conv2 ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   371
    (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   372
  fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   373
    let val n = fst (Term.dest_Free (Thm.term_of cv))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   374
    in conv (Symtab.update (free n, 0) tb) cx end)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   375
  val apply_rule = @{lemma "f x == apply f x" by (simp add: apply_def)}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   376
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   377
fun explicit_application ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   378
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   379
    fun sub_conv tb ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   380
      (case Term.strip_comb (Thm.term_of ct) of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   381
        (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   382
      | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   383
      | (Abs _, ts) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   384
      | (_, ts) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   385
    and app_conv tb n i ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   386
      (case Symtab.lookup tb n of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   387
        NONE => nary_conv Conv.all_conv (sub_conv tb ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   388
      | SOME j => apply_conv tb ctxt (i - j))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   389
    and apply_conv tb ctxt i ct = (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   390
      if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   391
      else
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   392
        Conv.rewr_conv apply_rule then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   393
        binop_conv (apply_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   394
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   395
    val tab = prune_tab (fold (traverse o Thm.prop_of) thms Symtab.empty)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   396
  in map (Conv.fconv_rule (sub_conv tab ctxt)) thms end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   397
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   398
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   399
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   400
(* combined normalization *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   401
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   402
datatype config =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   403
  RewriteTrivialLets |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   404
  RewriteNegativeNumerals |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   405
  RewriteNaturalNumbers |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   406
  AddPairRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   407
  AddFunUpdRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   408
  AddAbsMinMaxRules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   409
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   410
fun normalize config ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   411
  let fun if_enabled c f = member (op =) config c ? f ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   412
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   413
    thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   414
    |> if_enabled RewriteTrivialLets trivial_let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   415
    |> if_enabled RewriteNegativeNumerals positive_numerals
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   416
    |> if_enabled RewriteNaturalNumbers nat_as_int
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   417
    |> if_enabled AddPairRules add_pair_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   418
    |> if_enabled AddFunUpdRules add_fun_upd_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   419
    |> if_enabled AddAbsMinMaxRules add_abs_min_max_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   420
    |> map (normalize_rule ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   421
    |> lift_lambdas ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   422
    |> apsnd (explicit_application ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   423
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   424
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   425
val setup = unfold_defs_setup
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   426
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   427
end