src/HOL/SMT/Tools/smt_normalize.ML
author boehmes
Wed, 12 May 2010 23:53:53 +0200
changeset 36889 1355523fb07d
parent 36888 d9b9bec6ff8d
child 36890 0ab4217a07b1
permissions -rw-r--r--
added missing rewrite rules for natural min and max
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,
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
     6
  * simplify trivial distincts (those with less than three elements),
36888
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
     7
  * rewrite bool case expressions as if expressions,
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     8
  * replace negative numerals by negated positive numerals,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     9
  * embed natural numbers into integers,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    10
  * 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
    11
  * lift lambda terms,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    12
  * 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
    13
  * 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
    14
*)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    15
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    16
signature SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    17
sig
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    18
  val instantiate_free: cterm * cterm -> thm -> thm
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    19
  val discharge_definition: cterm -> thm -> thm
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    20
36085
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
    21
  val normalize: 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
    22
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    23
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    24
structure SMT_Normalize: SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    25
struct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    26
36083
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    27
infix 2 ??
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    28
fun (test ?? f) x = if test x then f x else x
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    29
36887
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
    30
fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
    31
fun if_true_conv c cv = if_conv c cv Conv.all_conv
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    32
36083
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    33
fun instantiate_free (cv, ct) =
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    34
  (Term.exists_subterm (equal (Thm.term_of cv)) o Thm.prop_of) ??
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    35
  (Thm.forall_elim ct o Thm.forall_intr cv)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    36
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    37
fun discharge_definition ct thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    38
  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
    39
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    40
    Thm.implies_intr ct thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    41
    |> instantiate_free (cv, cu)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    42
    |> (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
    43
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    44
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    45
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    46
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    47
(* 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
    48
   most once) *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    49
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    50
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    51
  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
    52
    | 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
    53
    | 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
    54
    | count _ _ = 0
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    55
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    56
  fun 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
    57
        (count 0 t <= 1)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    58
    | is_trivial_let _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    59
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    60
  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
    61
in
36083
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    62
fun trivial_let ctxt =
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    63
  map ((Term.exists_subterm is_trivial_let o Thm.prop_of) ??
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
    64
    Conv.fconv_rule (More_Conv.top_conv let_conv ctxt))
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    65
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    66
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    67
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    68
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    69
(* simplification of trivial distincts (distinct should have at least
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    70
   three elements in the argument list) *)
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    71
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    72
local
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    73
  fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) =
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    74
        length (HOLogic.dest_list t) <= 2
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    75
    | is_trivial_distinct _ = false
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    76
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    77
  val thms = @{lemma
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    78
    "distinct [] == True"
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    79
    "distinct [x] == True"
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    80
    "distinct [x, y] == (x ~= y)"
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    81
    by simp_all}
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    82
  fun distinct_conv _ =
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    83
    if_true_conv is_trivial_distinct (More_Conv.rewrs_conv thms)
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    84
in
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    85
fun trivial_distinct ctxt =
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    86
  map ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ??
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    87
    Conv.fconv_rule (More_Conv.top_conv distinct_conv ctxt))
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    88
end
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    89
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    90
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
    91
36888
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    92
(* rewrite bool case expressions as if expressions *)
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    93
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    94
local
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    95
  val is_bool_case = (fn
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    96
      Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    97
    | _ => false)
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    98
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
    99
  val thms = @{lemma
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   100
    "(case P of True => x | False => y) == (if P then x else y)"
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   101
    "(case P of False => y | True => x) == (if P then x else y)"
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   102
    by (rule eq_reflection, simp)+}
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   103
  val unfold_conv = if_true_conv is_bool_case (More_Conv.rewrs_conv thms)
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   104
in
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   105
fun rewrite_bool_cases ctxt =
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   106
  map ((Term.exists_subterm is_bool_case o Thm.prop_of) ??
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   107
    Conv.fconv_rule (More_Conv.top_conv (K unfold_conv) ctxt))
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   108
end
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   109
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   110
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   111
32618
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
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   123
  val pos_numeral_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   124
    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
   125
    addsimps [@{thm Int.numeral_1_eq_1}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   126
    addsimps @{thms Int.pred_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   127
    addsimps @{thms Int.normalize_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   128
    addsimps @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   129
      "Int.Min = - Int.Bit1 Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   130
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   131
      "Int.Bit0 (- k) = - Int.Bit0 k"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   132
      "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
   133
      by simp_all (simp add: pred_def)}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   134
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   135
  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
   136
    (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
   137
    Conv.no_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   138
in
36083
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   139
fun positive_numerals ctxt =
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   140
  map ((Term.exists_subterm (is_neg_number ctxt) o Thm.prop_of) ??
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   141
    Conv.fconv_rule (More_Conv.top_sweep_conv pos_conv ctxt))
32618
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
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   145
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   146
(* 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
   147
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   148
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   149
  val nat_embedding = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   150
    "nat (int n) = n"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   151
    "i >= 0 --> int (nat i) = i"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   152
    "i < 0 --> int (nat i) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   153
    by simp_all}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   154
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   155
  val nat_rewriting = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   156
    "0 = nat 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   157
    "1 = nat 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   158
    "number_of i = nat (number_of i)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   159
    "int (nat 0) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   160
    "int (nat 1) = 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   161
    "a < b = (int a < int b)"
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
    "Suc a = nat (int a + 1)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   164
    "a + b = nat (int a + int b)"
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 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
   168
    "a mod b = nat (int a mod int b)"
36889
1355523fb07d added missing rewrite rules for natural min and max
boehmes
parents: 36888
diff changeset
   169
    "min a b = nat (min (int a) (int b))"
1355523fb07d added missing rewrite rules for natural min and max
boehmes
parents: 36888
diff changeset
   170
    "max a b = nat (max (int a) (int b))"
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   171
    "int (nat (int a + int b)) = int a + int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   172
    "int (nat (int a * int b)) = int a * int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   173
    "int (nat (int a div int b)) = int a div int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   174
    "int (nat (int a mod int b)) = int a mod int b"
36889
1355523fb07d added missing rewrite rules for natural min and max
boehmes
parents: 36888
diff changeset
   175
    "int (nat (min (int a) (int b))) = min (int a) (int b)"
1355523fb07d added missing rewrite rules for natural min and max
boehmes
parents: 36888
diff changeset
   176
    "int (nat (max (int a) (int b))) = max (int a) (int b)"
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   177
    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
   178
      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
   179
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   180
  fun on_positive num f x = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   181
    (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
   182
      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
   183
    | NONE => NONE)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   184
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   185
  val cancel_int_nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   186
    addsimps [@{thm Nat_Numeral.nat_number_of}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   187
    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
   188
    addsimps @{thms neg_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   189
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   190
  fun cancel_int_nat_simproc _ ss ct = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   191
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   192
      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
   193
      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
   194
      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
   195
      fun tac _ = Simplifier.simp_tac simpset 1
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   196
    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
   197
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   198
  val nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   199
    addsimps nat_rewriting
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   200
    addsimprocs [Simplifier.make_simproc {
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   201
      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
   202
      proc = cancel_int_nat_simproc, identifier = [] }]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   203
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   204
  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
   205
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   206
  val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
33895
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   207
  val uses_nat_int =
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   208
    Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}])
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   209
in
36083
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   210
fun nat_as_int ctxt =
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   211
  map ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt)) #>
59f843c3f17f shorten the code by conditional function application
boehmes
parents: 36001
diff changeset
   212
  exists (uses_nat_int o Thm.prop_of) ?? append nat_embedding
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   213
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   214
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   215
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   216
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   217
(* include additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   218
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   219
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   220
  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
   221
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   222
  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
   223
  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
   224
36886
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   225
  val add_pair_rules =
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   226
    exists (exists_pair_type o Thm.prop_of) ?? append pair_rules
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   227
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   228
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   229
  val fun_upd_rules = [@{thm fun_upd_same}, @{thm fun_upd_apply}]
36886
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   230
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   231
  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
   232
  val exists_fun_upd = Term.exists_subterm is_fun_upd
36886
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   233
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   234
  val add_fun_upd_rules =
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   235
    exists (exists_fun_upd o Thm.prop_of) ?? append fun_upd_rules
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   236
in
36886
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   237
val add_rules = add_pair_rules #> add_fun_upd_rules
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   238
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   239
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   240
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   241
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   242
(* unfold definitions of specific constants *)
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   243
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   244
local
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   245
  fun mk_entry (t as Const (n, _)) thm = ((n, t), thm)
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   246
    | mk_entry t _ = raise TERM ("mk_entry", [t])
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   247
  fun prepare_def thm =
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   248
    (case Thm.prop_of thm of
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   249
      Const (@{const_name "=="}, _) $ t $ _ => mk_entry (Term.head_of t) thm
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   250
    | t => raise TERM ("prepare_def", [t]))
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
  val defs = map prepare_def [
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   253
    @{thm abs_if[where 'a = int, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   254
    @{thm abs_if[where 'a = real, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   255
    @{thm min_def[where 'a = int, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   256
    @{thm min_def[where 'a = real, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   257
    @{thm max_def[where 'a = int, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   258
    @{thm max_def[where 'a = real, THEN eq_reflection]},
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   259
    @{thm Ex1_def}, @{thm Ball_def}, @{thm Bex_def}]
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   260
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   261
  fun matches thy ((t as Const (n, _)), (m, p)) =
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   262
        n = m andalso Pattern.matches thy (p, t)
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   263
    | matches _ _ = false
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   264
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   265
  fun lookup_def thy = AList.lookup (matches thy) defs
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   266
  fun lookup_def_head thy = lookup_def thy o Term.head_of
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   267
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   268
  fun occurs_def thy = Term.exists_subterm (is_some o lookup_def thy)
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   269
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   270
  fun unfold_def_conv ctxt ct =
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   271
    (case lookup_def_head (ProofContext.theory_of ctxt) (Thm.term_of ct) of
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   272
      SOME thm => Conv.rewr_conv thm
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   273
    | NONE => Conv.all_conv) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   274
in
36084
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   275
fun unfold_defs ctxt =
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   276
  (occurs_def (ProofContext.theory_of ctxt) o Thm.prop_of) ??
3176ec2244ad always unfold definitions of specific constants (including special binders)
boehmes
parents: 36083
diff changeset
   277
  Conv.fconv_rule (More_Conv.top_conv unfold_def_conv ctxt)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   278
end
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
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   281
36887
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   282
(* further normalizations: beta/eta, universal closure, atomize *)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   283
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   284
local
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   285
  val all1 = @{lemma "All P == ALL x. P x" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   286
  val all2 = @{lemma "All == (%P. ALL x. P x)" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   287
  val ex1 = @{lemma "Ex P == EX x. P x" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   288
  val ex2 = @{lemma "Ex == (%P. EX x. P x)" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   289
  val let1 = @{lemma "Let c P == let x = c in P x" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   290
  val let2 = @{lemma "Let c == (%P. let x = c in P x)" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   291
  val let3 = @{lemma "Let == (%c P. let x = c in P x)" by (rule reflexive)}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   292
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   293
  fun all_abs_conv cv ctxt =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   294
    Conv.abs_conv (all_abs_conv cv o snd) ctxt else_conv cv ctxt
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   295
  fun keep_conv ctxt = More_Conv.binder_conv norm_conv ctxt
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   296
  and unfold_conv rule ctxt =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   297
    Conv.rewr_conv rule then_conv all_abs_conv keep_conv ctxt
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   298
  and unfold_let_conv rule ctxt =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   299
    Conv.rewr_conv rule then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   300
    all_abs_conv (fn cx => Conv.combination_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   301
      (Conv.arg_conv (norm_conv cx)) (Conv.abs_conv (norm_conv o snd) cx)) ctxt
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   302
  and norm_conv ctxt ct =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   303
    (case Thm.term_of ct of
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   304
      Const (@{const_name All}, _) $ Abs _ => keep_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   305
    | Const (@{const_name All}, _) $ _ => unfold_conv all1
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   306
    | Const (@{const_name All}, _) => unfold_conv all2
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   307
    | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   308
    | Const (@{const_name Ex}, _) $ _ => unfold_conv ex1
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   309
    | Const (@{const_name Ex}, _) => unfold_conv ex2
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   310
    | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   311
    | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv let1
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   312
    | Const (@{const_name Let}, _) $ _ => unfold_let_conv let2
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   313
    | Const (@{const_name Let}, _) => unfold_let_conv let3
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   314
    | Abs _ => Conv.abs_conv (norm_conv o snd)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   315
    | _ $ _ => Conv.comb_conv o norm_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   316
    | _ => K Conv.all_conv) ctxt ct
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   317
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   318
  fun is_normed t =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   319
    (case t of
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   320
      Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed u
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   321
    | Const (@{const_name All}, _) $ _ => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   322
    | Const (@{const_name All}, _) => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   323
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed u
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   324
    | Const (@{const_name Ex}, _) $ _ => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   325
    | Const (@{const_name Ex}, _) => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   326
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   327
        is_normed u1 andalso is_normed u2
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   328
    | Const (@{const_name Let}, _) $ _ $ _ => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   329
    | Const (@{const_name Let}, _) $ _ => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   330
    | Const (@{const_name Let}, _) => false
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   331
    | Abs (_, _, u) => is_normed u
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   332
    | u1 $ u2 => is_normed u1 andalso is_normed u2
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   333
    | _ => true)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   334
in
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   335
fun norm_binder_conv ctxt = if_conv is_normed Conv.all_conv (norm_conv ctxt)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   336
end
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   337
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   338
fun norm_def ctxt thm =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   339
  (case Thm.prop_of thm of
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   340
    @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   341
      norm_def ctxt (thm RS @{thm fun_cong})
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   342
  | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   343
      norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq})
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   344
  | _ => thm)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   345
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   346
fun atomize_conv ctxt ct =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   347
  (case Thm.term_of ct of
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   348
    @{term "op ==>"} $ _ $ _ =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   349
      Conv.binop_conv (atomize_conv ctxt) then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   350
      Conv.rewr_conv @{thm atomize_imp}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   351
  | Const (@{const_name "=="}, _) $ _ $ _ =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   352
      Conv.binop_conv (atomize_conv ctxt) then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   353
      Conv.rewr_conv @{thm atomize_eq}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   354
  | Const (@{const_name all}, _) $ Abs _ =>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   355
      More_Conv.binder_conv atomize_conv ctxt then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   356
      Conv.rewr_conv @{thm atomize_all}
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   357
  | _ => Conv.all_conv) ct
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   358
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   359
fun normalize_rule ctxt =
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   360
  Conv.fconv_rule (
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   361
    (* reduce lambda abstractions, except at known binders: *)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   362
    Thm.beta_conversion true then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   363
    Thm.eta_conversion then_conv
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   364
    norm_binder_conv ctxt) #>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   365
  norm_def ctxt #>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   366
  Drule.forall_intr_vars #>
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   367
  Conv.fconv_rule (atomize_conv ctxt)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   368
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   369
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   370
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   371
(* lift lambda terms into additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   372
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   373
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   374
  val meta_eq = @{cpat "op =="}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   375
  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
   376
  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
   377
  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
   378
36887
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   379
  fun norm_meta_def cv thm = 
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   380
    let val thm' = Thm.combination thm (Thm.reflexive cv)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   381
    in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   382
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   383
  fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
3b6a8ecd3c88 simplified normalize_rule and moved it further down in the code
boehmes
parents: 36886
diff changeset
   384
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   385
  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
   386
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   387
  fun used_vars cvs ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   388
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   389
      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
   390
      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
   391
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   392
  fun make_def cvs eq = Thm.symmetric (fold norm_meta_def cvs eq)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   393
  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
   394
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   395
  fun replace ctxt cvs ct (cx as (nctxt, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   396
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   397
      val cvs' = used_vars cvs ct
33355
ee5b5ef5e970 abstract over variables in reversed order (application uses given order)
boehmes
parents: 33299
diff changeset
   398
      val ct' = fold_rev Thm.cabs cvs' ct
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   399
    in
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   400
      (case Termtab.lookup defs (Thm.term_of ct') of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   401
        SOME (_, eq) => (make_def cvs' eq, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   402
      | NONE =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   403
          let
33664
d62805a237ef removed unused code and unused arguments,
boehmes
parents: 33355
diff changeset
   404
            val {T, ...} = Thm.rep_cterm ct'
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   405
            val (n, nctxt') = fresh_name "" nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   406
            val eq = Thm.assume (mk_meta_eq (cert ctxt (Free (n, T))) ct')
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   407
          in (make_def cvs' eq, (nctxt', add_def ct' eq defs)) end)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   408
    end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   409
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   410
  fun none ct cx = (Thm.reflexive ct, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   411
  fun in_comb f g ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   412
    let val (cu1, cu2) = Thm.dest_comb ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   413
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   414
  fun in_arg f = in_comb none f
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   415
  fun in_abs f cvs ct (nctxt, defs) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   416
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   417
      val (n, nctxt') = fresh_name Name.uu nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   418
      val (cv, cu) = Thm.dest_abs (SOME n) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   419
    in f (cv :: cvs) cu (nctxt', defs) |>> Thm.abstract_rule n cv end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   420
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   421
  fun replace_lambdas ctxt =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   422
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   423
      fun repl cvs ct =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   424
        (case Thm.term_of ct of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   425
          Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   426
        | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   427
        | Const _ $ Abs _ => in_arg (at_lambda cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   428
        | Const (@{const_name Let}, _) $ _ $ Abs _ =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   429
            in_comb (in_arg (repl cvs)) (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   430
        | Abs _ => at_lambda cvs
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   431
        | _ $ _ => in_comb (repl cvs) (repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   432
        | _ => none) ct
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   433
      and at_lambda cvs ct =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   434
        in_abs repl cvs ct #-> (fn thm =>
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   435
        replace ctxt cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   436
    in repl [] end
34010
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   437
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   438
  fun has_free_lambdas t =
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   439
    (case t of
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   440
      Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   441
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   442
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   443
        has_free_lambdas u1 orelse has_free_lambdas u2
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   444
    | Abs _ => true
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   445
    | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   446
    | _ => false)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   447
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   448
fun lift_lambdas ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   449
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   450
    val declare_frees = fold (Thm.fold_terms Term.declare_term_frees)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   451
    fun rewrite f thm cx =
34010
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   452
      if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx)
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   453
      else f (Thm.cprop_of thm) cx |>> (fn thm' => Thm.equal_elim thm' thm)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   454
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   455
    val rev_int_fst_ord = rev_order o int_ord o pairself fst
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   456
    fun ordered_values tab =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   457
      Termtab.fold (fn (_, x) => OrdList.insert rev_int_fst_ord x) tab []
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   458
      |> map snd
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   459
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   460
    val (thms', (_, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   461
      (declare_frees thms (Name.make_context []), Termtab.empty)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   462
      |> fold_map (rewrite (replace_lambdas ctxt)) thms
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   463
    val eqs = ordered_values defs
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   464
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   465
    (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
   466
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   467
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   468
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   469
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   470
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   471
(* 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
   472
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   473
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   474
  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
   475
  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
   476
  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
   477
  fun traverse t =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   478
    (case Term.strip_comb t of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   479
      (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
   480
    | (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
   481
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   482
    | (_, ts) => fold traverse ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   483
  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
   484
  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
   485
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   486
  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
   487
  fun nary_conv conv1 conv2 ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   488
    (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
   489
  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
   490
    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
   491
    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
   492
  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
   493
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   494
fun explicit_application ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   495
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   496
    fun sub_conv tb ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   497
      (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
   498
        (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
   499
      | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
33664
d62805a237ef removed unused code and unused arguments,
boehmes
parents: 33355
diff changeset
   500
      | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
d62805a237ef removed unused code and unused arguments,
boehmes
parents: 33355
diff changeset
   501
      | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   502
    and app_conv tb n i ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   503
      (case Symtab.lookup tb n of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   504
        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
   505
      | 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
   506
    and apply_conv tb ctxt i ct = (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   507
      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
   508
      else
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   509
        Conv.rewr_conv apply_rule then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   510
        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
   511
34010
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   512
    fun needs_exp_app tab = Term.exists_subterm (fn
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   513
        Bound _ $ _ => true
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   514
      | Const (n, _) => Symtab.defined tab (const n)
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   515
      | Free (n, _) => Symtab.defined tab (free n)
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   516
      | _ => false)
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   517
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   518
    fun rewrite tab ctxt thm =
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   519
      if not (needs_exp_app tab (Thm.prop_of thm)) then thm
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   520
      else Conv.fconv_rule (sub_conv tab ctxt) thm
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   521
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   522
    val tab = prune_tab (fold (traverse o Thm.prop_of) thms Symtab.empty)
34010
ac78f5cdc430 faster preprocessing: before applying a step, test if it is applicable (normalization of binders, unfolding of abs/min/max definitions, lambda lifting, explicit application, monomorphization),
boehmes
parents: 33895
diff changeset
   523
  in map (rewrite tab ctxt) thms end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   524
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   525
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   526
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   527
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   528
(* combined normalization *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   529
36085
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   530
fun normalize ctxt thms =
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   531
  thms
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   532
  |> trivial_let ctxt
36885
9407b8d0f6ad added simplification for distinctness of small lists
boehmes
parents: 36085
diff changeset
   533
  |> trivial_distinct ctxt
36888
d9b9bec6ff8d rewrite bool case expressions as if expression
boehmes
parents: 36887
diff changeset
   534
  |> rewrite_bool_cases ctxt
36085
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   535
  |> positive_numerals ctxt
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   536
  |> nat_as_int ctxt
36886
d8ea5bff3ca4 merged addition of rules into one function
boehmes
parents: 36885
diff changeset
   537
  |> add_rules
36085
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   538
  |> map (unfold_defs ctxt #> normalize_rule ctxt)
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   539
  |> lift_lambdas ctxt
0eaa6905590f shortened interface (do not export unused options and functions)
boehmes
parents: 36084
diff changeset
   540
  |> apsnd (explicit_application ctxt)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   541
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   542
end