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