src/HOL/Tools/SMT/smt_normalize.ML
author boehmes
Wed, 12 May 2010 23:54:04 +0200
changeset 36899 bcd6fce5bf06
parent 36898 8e55aa1306c5
child 36936 c52d1c130898
permissions -rw-r--r--
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT/smt_normalize.ML
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     3
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     4
Normalization steps on theorems required by SMT solvers:
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     5
  * simplify trivial distincts (those with less than three elements),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     6
  * rewrite bool case expressions as if expressions,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     7
  * normalize numerals (e.g. replace negative numerals by negated positive
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     8
    numerals),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     9
  * embed natural numbers into integers,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    10
  * add extra rules specifying types and constants which occur frequently,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    11
  * fully translate into object logic, add universal closure,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    12
  * lift lambda terms,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    13
  * make applications explicit for functions with varying number of arguments.
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    14
*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    15
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    16
signature SMT_NORMALIZE =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    17
sig
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    18
  type extra_norm = thm list -> Proof.context -> thm list * Proof.context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    19
  val normalize: extra_norm -> thm list -> Proof.context ->
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    20
    thm list * Proof.context
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    21
  val atomize_conv: Proof.context -> conv
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    22
  val eta_expand_conv: (Proof.context -> conv) -> Proof.context -> conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    23
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    24
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    25
structure SMT_Normalize: SMT_NORMALIZE =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    26
struct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    27
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    28
infix 2 ??
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    29
fun (test ?? f) x = if test x then f x else x
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    30
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    31
fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    32
fun if_true_conv c cv = if_conv c cv Conv.all_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    33
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    34
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    35
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    36
(* simplification of trivial distincts (distinct should have at least
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    37
   three elements in the argument list) *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    38
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    39
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    40
  fun is_trivial_distinct (Const (@{const_name distinct}, _) $ t) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    41
        length (HOLogic.dest_list t) <= 2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    42
    | is_trivial_distinct _ = false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    43
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    44
  val thms = @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    45
    "distinct [] == True"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    46
    "distinct [x] == True"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    47
    "distinct [x, y] == (x ~= y)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    48
    by simp_all}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    49
  fun distinct_conv _ =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    50
    if_true_conv is_trivial_distinct (More_Conv.rewrs_conv thms)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    51
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    52
fun trivial_distinct ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    53
  map ((Term.exists_subterm is_trivial_distinct o Thm.prop_of) ??
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    54
    Conv.fconv_rule (More_Conv.top_conv distinct_conv ctxt))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    55
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    56
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    57
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    58
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    59
(* rewrite bool case expressions as if expressions *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    60
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    61
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    62
  val is_bool_case = (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    63
      Const (@{const_name "bool.bool_case"}, _) $ _ $ _ $ _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    64
    | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    65
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    66
  val thms = @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    67
    "(case P of True => x | False => y) == (if P then x else y)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    68
    "(case P of False => y | True => x) == (if P then x else y)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    69
    by (rule eq_reflection, simp)+}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    70
  val unfold_conv = if_true_conv is_bool_case (More_Conv.rewrs_conv thms)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    71
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    72
fun rewrite_bool_cases ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    73
  map ((Term.exists_subterm is_bool_case o Thm.prop_of) ??
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    74
    Conv.fconv_rule (More_Conv.top_conv (K unfold_conv) ctxt))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    75
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    76
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    77
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    78
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    79
(* normalization of numerals: rewriting of negative integer numerals into
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    80
   positive numerals, Numeral0 into 0, Numeral1 into 1 *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    81
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    82
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    83
  fun is_number_sort ctxt T =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    84
    Sign.of_sort (ProofContext.theory_of ctxt) (T, @{sort number_ring})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    85
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    86
  fun is_strange_number ctxt (t as Const (@{const_name number_of}, _) $ _) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    87
        (case try HOLogic.dest_number t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    88
          SOME (T, i) => is_number_sort ctxt T andalso i < 2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    89
        | NONE => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    90
    | is_strange_number _ _ = false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    91
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    92
  val pos_numeral_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    93
    addsimps [@{thm Int.number_of_minus}, @{thm Int.number_of_Min}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    94
    addsimps [@{thm Int.number_of_Pls}, @{thm Int.numeral_1_eq_1}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    95
    addsimps @{thms Int.pred_bin_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    96
    addsimps @{thms Int.normalize_bin_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    97
    addsimps @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    98
      "Int.Min = - Int.Bit1 Int.Pls"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    99
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   100
      "Int.Bit0 (- k) = - Int.Bit0 k"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   101
      "Int.Bit1 (- k) = - Int.Bit1 (Int.pred k)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   102
      by simp_all (simp add: pred_def)}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   103
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   104
  fun pos_conv ctxt = if_conv (is_strange_number ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   105
    (Simplifier.rewrite (Simplifier.context ctxt pos_numeral_ss))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   106
    Conv.no_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   107
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   108
fun normalize_numerals ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   109
  map ((Term.exists_subterm (is_strange_number ctxt) o Thm.prop_of) ??
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   110
    Conv.fconv_rule (More_Conv.top_sweep_conv pos_conv ctxt))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   111
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   112
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   113
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   114
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   115
(* embedding of standard natural number operations into integer operations *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   116
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   117
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   118
  val nat_embedding = @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   119
    "nat (int n) = n"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   120
    "i >= 0 --> int (nat i) = i"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   121
    "i < 0 --> int (nat i) = 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   122
    by simp_all}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   123
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   124
  val nat_rewriting = @{lemma
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   125
    "0 = nat 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   126
    "1 = nat 1"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   127
    "number_of i = nat (number_of i)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   128
    "int (nat 0) = 0"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   129
    "int (nat 1) = 1"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   130
    "a < b = (int a < int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   131
    "a <= b = (int a <= int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   132
    "Suc a = nat (int a + 1)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   133
    "a + b = nat (int a + int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   134
    "a - b = nat (int a - int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   135
    "a * b = nat (int a * int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   136
    "a div b = nat (int a div int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   137
    "a mod b = nat (int a mod int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   138
    "min a b = nat (min (int a) (int b))"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   139
    "max a b = nat (max (int a) (int b))"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   140
    "int (nat (int a + int b)) = int a + int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   141
    "int (nat (int a * int b)) = int a * int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   142
    "int (nat (int a div int b)) = int a div int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   143
    "int (nat (int a mod int b)) = int a mod int b"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   144
    "int (nat (min (int a) (int b))) = min (int a) (int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   145
    "int (nat (max (int a) (int b))) = max (int a) (int b)"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   146
    by (simp_all add: nat_mult_distrib nat_div_distrib nat_mod_distrib
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   147
      int_mult[symmetric] zdiv_int[symmetric] zmod_int[symmetric])}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   148
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   149
  fun on_positive num f x = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   150
    (case try HOLogic.dest_number (Thm.term_of num) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   151
      SOME (_, i) => if i >= 0 then SOME (f x) else NONE
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   152
    | NONE => NONE)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   153
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   154
  val cancel_int_nat_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   155
    addsimps [@{thm Nat_Numeral.nat_number_of}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   156
    addsimps [@{thm Nat_Numeral.int_nat_number_of}]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   157
    addsimps @{thms neg_simps}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   158
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   159
  fun cancel_int_nat_simproc _ ss ct = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   160
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   161
      val num = Thm.dest_arg (Thm.dest_arg ct)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   162
      val goal = Thm.mk_binop @{cterm "op == :: int => _"} ct num
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   163
      val simpset = Simplifier.inherit_context ss cancel_int_nat_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   164
      fun tac _ = Simplifier.simp_tac simpset 1
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   165
    in on_positive num (Goal.prove_internal [] goal) tac end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   166
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   167
  val nat_ss = HOL_ss
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   168
    addsimps nat_rewriting
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   169
    addsimprocs [Simplifier.make_simproc {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   170
      name = "cancel_int_nat_num", lhss = [@{cpat "int (nat _)"}],
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   171
      proc = cancel_int_nat_simproc, identifier = [] }]
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   172
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   173
  fun conv ctxt = Simplifier.rewrite (Simplifier.context ctxt nat_ss)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   174
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   175
  val uses_nat_type = Term.exists_type (Term.exists_subtype (equal @{typ nat}))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   176
  val uses_nat_int =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   177
    Term.exists_subterm (member (op aconv) [@{term int}, @{term nat}])
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   178
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   179
fun nat_as_int ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   180
  map ((uses_nat_type o Thm.prop_of) ?? Conv.fconv_rule (conv ctxt)) #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   181
  exists (uses_nat_int o Thm.prop_of) ?? append nat_embedding
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   182
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   183
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   184
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   185
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   186
(* further normalizations: beta/eta, universal closure, atomize *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   187
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   188
val eta_expand_eq = @{lemma "f == (%x. f x)" by (rule reflexive)}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   189
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   190
fun eta_expand_conv cv ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   191
  Conv.rewr_conv eta_expand_eq then_conv Conv.abs_conv (cv o snd) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   192
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   193
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   194
  val eta_conv = eta_expand_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   195
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   196
  fun keep_conv ctxt = More_Conv.binder_conv norm_conv ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   197
  and eta_binder_conv ctxt = Conv.arg_conv (eta_conv norm_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   198
  and keep_let_conv ctxt = Conv.combination_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   199
    (Conv.arg_conv (norm_conv ctxt)) (Conv.abs_conv (norm_conv o snd) ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   200
  and unfold_let_conv ctxt = Conv.combination_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   201
    (Conv.arg_conv (norm_conv ctxt)) (eta_conv norm_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   202
  and unfold_conv thm ctxt = Conv.rewr_conv thm then_conv keep_conv ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   203
  and unfold_ex1_conv ctxt = unfold_conv @{thm Ex1_def} ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   204
  and unfold_ball_conv ctxt = unfold_conv @{thm Ball_def} ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   205
  and unfold_bex_conv ctxt = unfold_conv @{thm Bex_def} ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   206
  and norm_conv ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   207
    (case Thm.term_of ct of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   208
      Const (@{const_name All}, _) $ Abs _ => keep_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   209
    | Const (@{const_name All}, _) $ _ => eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   210
    | Const (@{const_name All}, _) => eta_conv eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   211
    | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   212
    | Const (@{const_name Ex}, _) $ _ => eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   213
    | Const (@{const_name Ex}, _) => eta_conv eta_binder_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   214
    | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   215
    | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   216
    | Const (@{const_name Let}, _) $ _ => eta_conv unfold_let_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   217
    | Const (@{const_name Let}, _) => eta_conv (eta_conv unfold_let_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   218
    | Const (@{const_name Ex1}, _) $ _ => unfold_ex1_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   219
    | Const (@{const_name Ex1}, _) => eta_conv unfold_ex1_conv 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   220
    | Const (@{const_name Ball}, _) $ _ $ _ => unfold_ball_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   221
    | Const (@{const_name Ball}, _) $ _ => eta_conv unfold_ball_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   222
    | Const (@{const_name Ball}, _) => eta_conv (eta_conv unfold_ball_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   223
    | Const (@{const_name Bex}, _) $ _ $ _ => unfold_bex_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   224
    | Const (@{const_name Bex}, _) $ _ => eta_conv unfold_bex_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   225
    | Const (@{const_name Bex}, _) => eta_conv (eta_conv unfold_bex_conv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   226
    | Abs _ => Conv.abs_conv (norm_conv o snd)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   227
    | _ $ _ => Conv.comb_conv o norm_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   228
    | _ => K Conv.all_conv) ctxt ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   229
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   230
  fun is_normed t =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   231
    (case t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   232
      Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   233
    | Const (@{const_name All}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   234
    | Const (@{const_name All}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   235
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   236
    | Const (@{const_name Ex}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   237
    | Const (@{const_name Ex}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   238
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   239
        is_normed u1 andalso is_normed u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   240
    | Const (@{const_name Let}, _) $ _ $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   241
    | Const (@{const_name Let}, _) $ _ => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   242
    | Const (@{const_name Let}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   243
    | Const (@{const_name Ex1}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   244
    | Const (@{const_name Ball}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   245
    | Const (@{const_name Bex}, _) => false
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   246
    | Abs (_, _, u) => is_normed u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   247
    | u1 $ u2 => is_normed u1 andalso is_normed u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   248
    | _ => true)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   249
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   250
fun norm_binder_conv ctxt = if_conv is_normed Conv.all_conv (norm_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   251
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   252
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   253
fun norm_def ctxt thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   254
  (case Thm.prop_of thm of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   255
    @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   256
      norm_def ctxt (thm RS @{thm fun_cong})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   257
  | Const (@{const_name "=="}, _) $ _ $ Abs _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   258
      norm_def ctxt (thm RS @{thm meta_eq_to_obj_eq})
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   259
  | _ => thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   260
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   261
fun atomize_conv ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   262
  (case Thm.term_of ct of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   263
    @{term "op ==>"} $ _ $ _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   264
      Conv.binop_conv (atomize_conv ctxt) then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   265
      Conv.rewr_conv @{thm atomize_imp}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   266
  | Const (@{const_name "=="}, _) $ _ $ _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   267
      Conv.binop_conv (atomize_conv ctxt) then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   268
      Conv.rewr_conv @{thm atomize_eq}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   269
  | Const (@{const_name all}, _) $ Abs _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   270
      More_Conv.binder_conv atomize_conv ctxt then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   271
      Conv.rewr_conv @{thm atomize_all}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   272
  | _ => Conv.all_conv) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   273
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   274
fun normalize_rule ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   275
  Conv.fconv_rule (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   276
    (* reduce lambda abstractions, except at known binders: *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   277
    Thm.beta_conversion true then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   278
    Thm.eta_conversion then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   279
    norm_binder_conv ctxt) #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   280
  norm_def ctxt #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   281
  Drule.forall_intr_vars #>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   282
  Conv.fconv_rule (atomize_conv ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   283
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   284
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   285
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   286
(* lift lambda terms into additional rules *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   287
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   288
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   289
  val meta_eq = @{cpat "op =="}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   290
  val meta_eqT = hd (Thm.dest_ctyp (Thm.ctyp_of_term meta_eq))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   291
  fun inst_meta cT = Thm.instantiate_cterm ([(meta_eqT, cT)], []) meta_eq
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   292
  fun mk_meta_eq ct cu = Thm.mk_binop (inst_meta (Thm.ctyp_of_term ct)) ct cu
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   293
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   294
  fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   295
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   296
  fun used_vars cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   297
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   298
      val lookup = AList.lookup (op aconv) (map (` Thm.term_of) cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   299
      val add = (fn SOME ct => insert (op aconvc) ct | _ => I)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   300
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   301
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   302
  fun apply cv thm = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   303
    let val thm' = Thm.combination thm (Thm.reflexive cv)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   304
    in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   305
  fun apply_def cvs eq = Thm.symmetric (fold apply cvs eq)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   306
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   307
  fun replace_lambda cvs ct (cx as (ctxt, defs)) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   308
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   309
      val cvs' = used_vars cvs ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   310
      val ct' = fold_rev Thm.cabs cvs' ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   311
    in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   312
      (case Termtab.lookup defs (Thm.term_of ct') of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   313
        SOME eq => (apply_def cvs' eq, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   314
      | NONE =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   315
          let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   316
            val {T, ...} = Thm.rep_cterm ct' and n = Name.uu
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   317
            val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   318
            val cu = mk_meta_eq (cert ctxt (Free (n', T))) ct'
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   319
            val (eq, ctxt'') = yield_singleton Assumption.add_assumes cu ctxt'
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   320
            val defs' = Termtab.update (Thm.term_of ct', eq) defs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   321
          in (apply_def cvs' eq, (ctxt'', defs')) end)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   322
    end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   323
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   324
  fun none ct cx = (Thm.reflexive ct, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   325
  fun in_comb f g ct cx =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   326
    let val (cu1, cu2) = Thm.dest_comb ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   327
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   328
  fun in_arg f = in_comb none f
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   329
  fun in_abs f cvs ct (ctxt, defs) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   330
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   331
      val (n, ctxt') = yield_singleton Variable.variant_fixes Name.uu ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   332
      val (cv, cu) = Thm.dest_abs (SOME n) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   333
    in  (ctxt', defs) |> f (cv :: cvs) cu |>> Thm.abstract_rule n cv end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   334
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   335
  fun traverse cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   336
    (case Thm.term_of ct of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   337
      Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   338
    | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   339
    | Const (@{const_name Let}, _) $ _ $ Abs _ =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   340
        in_comb (in_arg (traverse cvs)) (in_abs traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   341
    | Abs _ => at_lambda cvs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   342
    | _ $ _ => in_comb (traverse cvs) (traverse cvs)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   343
    | _ => none) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   344
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   345
  and at_lambda cvs ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   346
    in_abs traverse cvs ct #-> (fn thm =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   347
    replace_lambda cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   348
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   349
  fun has_free_lambdas t =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   350
    (case t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   351
      Const (@{const_name All}, _) $ Abs (_, _, u) => has_free_lambdas u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   352
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => has_free_lambdas u
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   353
    | Const (@{const_name Let}, _) $ u1 $ Abs (_, _, u2) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   354
        has_free_lambdas u1 orelse has_free_lambdas u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   355
    | Abs _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   356
    | u1 $ u2 => has_free_lambdas u1 orelse has_free_lambdas u2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   357
    | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   358
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   359
  fun lift_lm f thm cx =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   360
    if not (has_free_lambdas (Thm.prop_of thm)) then (thm, cx)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   361
    else cx |> f (Thm.cprop_of thm) |>> (fn thm' => Thm.equal_elim thm' thm)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   362
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   363
fun lift_lambdas thms ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   364
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   365
    val cx = (ctxt, Termtab.empty)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   366
    val (thms', (ctxt', defs)) = fold_map (lift_lm (traverse [])) thms cx
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   367
    val eqs = Termtab.fold (cons o normalize_rule ctxt' o snd) defs []
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   368
  in (eqs @ thms', ctxt') end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   369
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   370
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   371
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   372
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   373
(* make application explicit for functions with varying number of arguments *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   374
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   375
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   376
  val const = prefix "c" and free = prefix "f"
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   377
  fun min i (e as (_, j)) = if i <> j then (true, Int.min (i, j)) else e
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   378
  fun add t i = Symtab.map_default (t, (false, i)) (min i)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   379
  fun traverse t =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   380
    (case Term.strip_comb t of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   381
      (Const (n, _), ts) => add (const n) (length ts) #> fold traverse ts 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   382
    | (Free (n, _), ts) => add (free n) (length ts) #> fold traverse ts
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   383
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   384
    | (_, ts) => fold traverse ts)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   385
  val prune = (fn (n, (true, i)) => Symtab.update (n, i) | _ => I)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   386
  fun prune_tab tab = Symtab.fold prune tab Symtab.empty
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   387
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   388
  fun binop_conv cv1 cv2 = Conv.combination_conv (Conv.arg_conv cv1) cv2
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   389
  fun nary_conv conv1 conv2 ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   390
    (Conv.combination_conv (nary_conv conv1 conv2) conv2 else_conv conv1) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   391
  fun abs_conv conv tb = Conv.abs_conv (fn (cv, cx) =>
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   392
    let val n = fst (Term.dest_Free (Thm.term_of cv))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   393
    in conv (Symtab.update (free n, 0) tb) cx end)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   394
  val apply_rule = @{lemma "f x == apply f x" by (simp add: apply_def)}
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   395
in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   396
fun explicit_application ctxt thms =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   397
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   398
    fun sub_conv tb ctxt ct =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   399
      (case Term.strip_comb (Thm.term_of ct) of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   400
        (Const (n, _), ts) => app_conv tb (const n) (length ts) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   401
      | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   402
      | (Abs _, _) => nary_conv (abs_conv sub_conv tb ctxt) (sub_conv tb ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   403
      | (_, _) => nary_conv Conv.all_conv (sub_conv tb ctxt)) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   404
    and app_conv tb n i ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   405
      (case Symtab.lookup tb n of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   406
        NONE => nary_conv Conv.all_conv (sub_conv tb ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   407
      | SOME j => apply_conv tb ctxt (i - j))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   408
    and apply_conv tb ctxt i ct = (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   409
      if i = 0 then nary_conv Conv.all_conv (sub_conv tb ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   410
      else
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   411
        Conv.rewr_conv apply_rule then_conv
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   412
        binop_conv (apply_conv tb ctxt (i-1)) (sub_conv tb ctxt)) ct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   413
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   414
    fun needs_exp_app tab = Term.exists_subterm (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   415
        Bound _ $ _ => true
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   416
      | Const (n, _) => Symtab.defined tab (const n)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   417
      | Free (n, _) => Symtab.defined tab (free n)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   418
      | _ => false)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   419
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   420
    fun rewrite tab ctxt thm =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   421
      if not (needs_exp_app tab (Thm.prop_of thm)) then thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   422
      else Conv.fconv_rule (sub_conv tab ctxt) thm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   423
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   424
    val tab = prune_tab (fold (traverse o Thm.prop_of) thms Symtab.empty)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   425
  in map (rewrite tab ctxt) thms end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   426
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   427
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   428
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   429
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   430
(* combined normalization *)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   431
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   432
type extra_norm = thm list -> Proof.context -> thm list * Proof.context
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   433
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   434
fun with_context f thms ctxt = (f ctxt thms, ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   435
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   436
fun normalize extra_norm thms ctxt =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   437
  thms
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   438
  |> trivial_distinct ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   439
  |> rewrite_bool_cases ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   440
  |> normalize_numerals ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   441
  |> nat_as_int ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   442
  |> rpair ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   443
  |-> extra_norm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   444
  |-> with_context (fn cx => map (normalize_rule cx))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   445
  |-> SMT_Monomorph.monomorph
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   446
  |-> lift_lambdas
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   447
  |-> with_context explicit_application
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   448
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   449
end