src/HOL/SMT/Tools/smt_normalize.ML
author haftmann
Thu, 29 Oct 2009 11:41:36 +0100
changeset 33318 ddd97d9dfbfb
parent 33243 17014b1b9353
child 33299 73af7831ba1e
permissions -rw-r--r--
moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
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
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    17
  val instantiate_free: cterm * cterm -> thm -> thm
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    18
  val discharge_definition: cterm -> thm -> thm
32618
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
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    36
  val normalize: config list -> Proof.context -> thm list -> cterm list * thm list
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    37
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    38
  val setup: theory -> theory
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    39
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    40
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    41
structure SMT_Normalize: SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    42
struct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    43
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    44
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
    45
  @{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
    46
  @{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
    47
  @{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
    48
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    49
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
    50
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    51
fun norm_meta_def cv thm = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    52
  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
    53
  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
    54
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    55
fun norm_def ctxt thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    56
  (case Thm.prop_of thm of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    57
    Const (@{const_name "=="}, _) $ _ $ Abs (_, T, _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    58
      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
    59
      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
    60
  | @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    61
      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
    62
  | _ => thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    63
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    64
fun normalize_rule ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    65
  Conv.fconv_rule (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    66
    Thm.beta_conversion true then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    67
    Thm.eta_conversion then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    68
    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
    69
  norm_def ctxt #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    70
  Drule.forall_intr_vars #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    71
  Conv.fconv_rule ObjectLogic.atomize
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    72
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    73
fun instantiate_free (cv, ct) thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    74
  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
    75
  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
    76
  else thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    77
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    78
fun discharge_definition ct thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    79
  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
    80
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    81
    Thm.implies_intr ct thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    82
    |> instantiate_free (cv, cu)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    83
    |> (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
    84
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    85
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    86
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
    87
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
    88
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
(* 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
    91
   most once) *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    92
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    93
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    94
  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
    95
    | 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
    96
    | 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
    97
    | count _ _ = 0
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    98
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    99
  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
   100
        (count 0 t <= 1)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   101
    | is_trivial_let _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   102
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   103
  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
   104
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   105
  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
   106
    (More_Conv.top_conv let_conv ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   107
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   108
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
   109
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   110
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
(* 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
   113
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   114
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   115
  fun neg_numeral @{term Int.Min} = true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   116
    | neg_numeral _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   117
  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
   118
  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
   119
        Term.exists_subterm neg_numeral t andalso
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   120
        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
   121
    | is_neg_number _ _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   122
  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
   123
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   124
  val pos_numeral_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   125
    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
   126
    addsimps [@{thm Int.numeral_1_eq_1}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   127
    addsimps @{thms Int.pred_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   128
    addsimps @{thms Int.normalize_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   129
    addsimps @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   130
      "Int.Min = - Int.Bit1 Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   131
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   132
      "Int.Bit0 (- k) = - Int.Bit0 k"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   133
      "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
   134
      by simp_all (simp add: pred_def)}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   135
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   136
  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
   137
    (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
   138
    Conv.no_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   139
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   140
  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
   141
    (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
   142
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   143
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
   144
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   145
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
(* 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
   148
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   149
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   150
  val nat_embedding = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   151
    "nat (int n) = n"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   152
    "i >= 0 --> int (nat i) = i"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   153
    "i < 0 --> int (nat i) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   154
    by simp_all}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   155
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   156
  val nat_rewriting = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   157
    "0 = nat 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   158
    "1 = nat 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   159
    "number_of i = nat (number_of i)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   160
    "int (nat 0) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   161
    "int (nat 1) = 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   162
    "a < b = (int a < int b)"
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
    "Suc a = nat (int a + 1)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   165
    "a + b = nat (int a + int b)"
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 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
   169
    "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
   170
    "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
   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 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
   173
    "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
   174
    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
   175
      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
   176
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   177
  fun on_positive num f x = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   178
    (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
   179
      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
   180
    | NONE => NONE)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   181
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   182
  val cancel_int_nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   183
    addsimps [@{thm Nat_Numeral.nat_number_of}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   184
    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
   185
    addsimps @{thms neg_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   186
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   187
  fun cancel_int_nat_simproc _ ss ct = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   188
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   189
      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
   190
      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
   191
      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
   192
      fun tac _ = Simplifier.simp_tac simpset 1
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   193
    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
   194
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   195
  val nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   196
    addsimps nat_rewriting
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   197
    addsimprocs [Simplifier.make_simproc {
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   198
      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
   199
      proc = cancel_int_nat_simproc, identifier = [] }]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   200
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   201
  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
   202
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   203
  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
   204
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   205
fun nat_as_int ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   206
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   207
    fun norm thm uses_nat =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   208
      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
   209
      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
   210
    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
   211
  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
   212
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   213
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
(* include additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   216
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   217
val (unfold_defs, unfold_defs_setup) =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   218
  Attrib.config_bool "smt_unfold_defs" true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   219
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   220
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   221
  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
   222
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   223
  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
   224
  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
   225
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   226
  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
   227
  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
   228
  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
   229
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   230
fun add_pair_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   231
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   232
  |> 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
   233
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   234
fun add_fun_upd_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   235
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   236
  |> 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
   237
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   238
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
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   241
  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
   242
  fun prepare_def thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   243
    (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
   244
      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
   245
    | t => raise TERM ("prepare_def", [t]))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   246
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   247
  val defs = map prepare_def [
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   248
    @{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
   249
    @{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
   250
    @{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
   251
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   252
  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
   253
  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
   254
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   255
  fun unfold_conv ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   256
    (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
   257
      SOME (_, eq) => Conv.rewr_conv eq
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   258
    | NONE => Conv.all_conv) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   259
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   260
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
   261
  if Config.get ctxt unfold_defs
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   262
  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
   263
  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
   264
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   265
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
(* lift lambda terms into additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   268
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   269
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   270
  val meta_eq = @{cpat "op =="}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   271
  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
   272
  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
   273
  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
   274
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   275
  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
   276
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   277
  fun used_vars cvs ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   278
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   279
      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
   280
      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
   281
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   282
  fun make_def cvs eq = Thm.symmetric (fold norm_meta_def cvs eq)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   283
  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
   284
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   285
  fun replace ctxt cvs ct (cx as (nctxt, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   286
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   287
      val cvs' = used_vars cvs ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   288
      val ct' = fold Thm.cabs cvs' ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   289
      val mk_repl = fold (fn ct => fn cu => Thm.capply cu ct) cvs'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   290
    in
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   291
      (case Termtab.lookup defs (Thm.term_of ct') of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   292
        SOME (_, eq) => (make_def cvs' eq, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   293
      | NONE =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   294
          let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   295
            val {t, T, ...} = Thm.rep_cterm ct'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   296
            val (n, nctxt') = fresh_name "" nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   297
            val eq = Thm.assume (mk_meta_eq (cert ctxt (Free (n, T))) ct')
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   298
          in (make_def cvs' eq, (nctxt', add_def ct' eq defs)) end)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   299
    end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   300
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   301
  fun none ct cx = (Thm.reflexive ct, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   302
  fun in_comb f g ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   303
    let val (cu1, cu2) = Thm.dest_comb ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   304
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   305
  fun in_arg f = in_comb none f
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   306
  fun in_abs f cvs ct (nctxt, defs) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   307
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   308
      val (n, nctxt') = fresh_name Name.uu nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   309
      val (cv, cu) = Thm.dest_abs (SOME n) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   310
    in f (cv :: cvs) cu (nctxt', defs) |>> Thm.abstract_rule n cv end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   311
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   312
  fun replace_lambdas ctxt =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   313
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   314
      fun repl cvs ct =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   315
        (case Thm.term_of ct of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   316
          Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   317
        | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   318
        | Const _ $ Abs _ => in_arg (at_lambda cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   319
        | Const (@{const_name Let}, _) $ _ $ Abs _ =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   320
            in_comb (in_arg (repl cvs)) (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   321
        | Abs _ => at_lambda cvs
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   322
        | _ $ _ => in_comb (repl cvs) (repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   323
        | _ => none) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   324
      and at_lambda cvs ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   325
        let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   326
          val (thm1, cx') = in_abs repl cvs ct cx
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   327
          val (thm2, cx'') = replace ctxt cvs (Thm.rhs_of thm1) cx'
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   328
        in (Thm.transitive thm1 thm2, cx'') end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   329
    in repl [] end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   330
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   331
fun lift_lambdas ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   332
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   333
    val declare_frees = fold (Thm.fold_terms Term.declare_term_frees)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   334
    fun rewrite f thm cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   335
      let val (thm', cx') = f (Thm.cprop_of thm) cx
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   336
      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
   337
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   338
    val rev_int_fst_ord = rev_order o int_ord o pairself fst
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   339
    fun ordered_values tab =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   340
      Termtab.fold (fn (_, x) => OrdList.insert rev_int_fst_ord x) tab []
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   341
      |> map snd
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   342
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   343
    val (thms', (_, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   344
      (declare_frees thms (Name.make_context []), Termtab.empty)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   345
      |> fold_map (rewrite (replace_lambdas ctxt)) thms
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   346
    val eqs = ordered_values defs
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   347
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   348
    (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
   349
  end
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
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
(* 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
   354
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   355
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   356
  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
   357
  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
   358
  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
   359
  fun traverse t =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   360
    (case Term.strip_comb t of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   361
      (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
   362
    | (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
   363
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   364
    | (_, ts) => fold traverse ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   365
  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
   366
  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
   367
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   368
  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
   369
  fun nary_conv conv1 conv2 ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   370
    (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
   371
  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
   372
    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
   373
    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
   374
  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
   375
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   376
fun explicit_application ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   377
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   378
    fun sub_conv tb ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   379
      (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
   380
        (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
   381
      | (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
   382
      | (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
   383
      | (_, 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
   384
    and app_conv tb n i ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   385
      (case Symtab.lookup tb n of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   386
        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
   387
      | 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
   388
    and apply_conv tb ctxt i ct = (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   389
      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
   390
      else
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   391
        Conv.rewr_conv apply_rule then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   392
        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
   393
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   394
    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
   395
  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
   396
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   397
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
(* combined normalization *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   400
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   401
datatype config =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   402
  RewriteTrivialLets |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   403
  RewriteNegativeNumerals |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   404
  RewriteNaturalNumbers |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   405
  AddPairRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   406
  AddFunUpdRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   407
  AddAbsMinMaxRules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   408
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   409
fun normalize config ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   410
  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
   411
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   412
    thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   413
    |> if_enabled RewriteTrivialLets trivial_let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   414
    |> if_enabled RewriteNegativeNumerals positive_numerals
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   415
    |> if_enabled RewriteNaturalNumbers nat_as_int
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   416
    |> if_enabled AddPairRules add_pair_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   417
    |> if_enabled AddFunUpdRules add_fun_upd_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   418
    |> 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
   419
    |> map (normalize_rule ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   420
    |> lift_lambdas ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   421
    |> apsnd (explicit_application ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   422
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   423
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   424
val setup = unfold_defs_setup
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   425
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   426
end