src/HOL/SMT/Tools/smt_normalize.ML
author boehmes
Thu, 03 Dec 2009 15:56:06 +0100
changeset 34010 ac78f5cdc430
parent 33895 3e7c51bbeb24
child 35116 133be405a6f1
permissions -rw-r--r--
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), custom-made (top-down) atomize_conv, store predicate and function symbols in a table instead of a list for faster lookup, updated certificates
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/SMT/Tools/smt_normalize.ML
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     3
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     4
Normalization steps on theorems required by SMT solvers:
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     5
  * unfold trivial let expressions,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     6
  * replace negative numerals by negated positive numerals,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     7
  * embed natural numbers into integers,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     8
  * add extra rules specifying types and constants which occur frequently,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
     9
  * lift lambda terms,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    10
  * make applications explicit for functions with varying number of arguments,
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    11
  * fully translate into object logic, add universal closure. 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    12
*)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    13
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    14
signature SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    15
sig
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    16
  val normalize_rule: Proof.context -> thm -> thm
33243
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    17
  val instantiate_free: cterm * cterm -> thm -> thm
17014b1b9353 normalized basic type abbreviations;
wenzelm
parents: 33010
diff changeset
    18
  val discharge_definition: cterm -> thm -> thm
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    19
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    20
  val trivial_let: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    21
  val positive_numerals: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    22
  val nat_as_int: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    23
  val unfold_defs: bool Config.T
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    24
  val add_pair_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    25
  val add_fun_upd_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    26
  val add_abs_min_max_rules: Proof.context -> thm list -> thm list
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    27
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    28
  datatype config =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    29
    RewriteTrivialLets |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    30
    RewriteNegativeNumerals |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    31
    RewriteNaturalNumbers |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    32
    AddPairRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    33
    AddFunUpdRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    34
    AddAbsMinMaxRules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    35
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    36
  val normalize: config list -> Proof.context -> thm list ->
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    37
    cterm list * thm list
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    38
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    39
  val setup: theory -> theory
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    40
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    41
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    42
structure SMT_Normalize: SMT_NORMALIZE =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    43
struct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    44
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    45
local
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    46
  val all1 = @{lemma "All P == ALL x. P x" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    47
  val all2 = @{lemma "All == (%P. ALL x. P x)" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    48
  val ex1 = @{lemma "Ex P == EX x. P x" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    49
  val ex2 = @{lemma "Ex == (%P. EX x. P x)" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    50
  val let1 = @{lemma "Let c P == let x = c in P x" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    51
  val let2 = @{lemma "Let c == (%P. let x = c in P x)" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    52
  val let3 = @{lemma "Let == (%c P. let x = c in P x)" by (rule reflexive)}
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    53
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    54
  fun all_abs_conv cv ctxt =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    55
    Conv.abs_conv (all_abs_conv cv o snd) ctxt else_conv cv ctxt
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    56
  fun keep_conv ctxt = More_Conv.binder_conv norm_conv ctxt
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    57
  and unfold_conv rule ctxt =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    58
    Conv.rewr_conv rule then_conv all_abs_conv keep_conv ctxt
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    59
  and unfold_let_conv rule ctxt =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    60
    Conv.rewr_conv rule then_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    61
    all_abs_conv (fn cx => Conv.combination_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    62
      (Conv.arg_conv (norm_conv cx)) (Conv.abs_conv (norm_conv o snd) cx)) ctxt
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    63
  and norm_conv ctxt ct =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    64
    (case Thm.term_of ct of
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    65
      Const (@{const_name All}, _) $ Abs _ => keep_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    66
    | Const (@{const_name All}, _) $ _ => unfold_conv all1
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    67
    | Const (@{const_name All}, _) => unfold_conv all2
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    68
    | Const (@{const_name Ex}, _) $ Abs _ => keep_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    69
    | Const (@{const_name Ex}, _) $ _ => unfold_conv ex1
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    70
    | Const (@{const_name Ex}, _) => unfold_conv ex2
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    71
    | Const (@{const_name Let}, _) $ _ $ Abs _ => keep_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    72
    | Const (@{const_name Let}, _) $ _ $ _ => unfold_let_conv let1
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    73
    | Const (@{const_name Let}, _) $ _ => unfold_let_conv let2
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    74
    | Const (@{const_name Let}, _) => unfold_let_conv let3
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    75
    | Abs _ => Conv.abs_conv (norm_conv o snd)
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    76
    | _ $ _ => Conv.comb_conv o norm_conv
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    77
    | _ => K Conv.all_conv) ctxt ct
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
    78
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
    79
  fun is_normed 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
    80
    (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
    81
      Const (@{const_name All}, _) $ Abs (_, _, u) => is_normed 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
    82
    | Const (@{const_name All}, _) $ _ => 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
    83
    | Const (@{const_name All}, _) => 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
    84
    | Const (@{const_name Ex}, _) $ Abs (_, _, u) => is_normed 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
    85
    | Const (@{const_name Ex}, _) $ _ => 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
    86
    | Const (@{const_name Ex}, _) => 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
    87
    | 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
    88
        is_normed u1 andalso is_normed 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
    89
    | Const (@{const_name Let}, _) $ _ $ _ => 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
    90
    | Const (@{const_name Let}, _) $ _ => 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
    91
    | Const (@{const_name Let}, _) => 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
    92
    | Abs (_, _, u) => is_normed 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
    93
    | u1 $ u2 => is_normed u1 andalso is_normed 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
    94
    | _ => true)
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    95
in
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
    96
fun norm_binder_conv ctxt ct =
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
    97
  if is_normed (Thm.term_of ct) then Conv.all_conv ct else norm_conv ctxt ct
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
    98
end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
    99
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   100
fun cert ctxt = Thm.cterm_of (ProofContext.theory_of ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   101
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   102
fun norm_meta_def cv thm = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   103
  let val thm' = Thm.combination thm (Thm.reflexive cv)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   104
  in Thm.transitive thm' (Thm.beta_conversion false (Thm.rhs_of thm')) end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   105
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   106
fun norm_def ctxt thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   107
  (case Thm.prop_of thm of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   108
    Const (@{const_name "=="}, _) $ _ $ Abs (_, T, _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   109
      let val v = Var ((Name.uu, #maxidx (Thm.rep_thm thm) + 1), T)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   110
      in norm_def ctxt (norm_meta_def (cert ctxt v) thm) end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   111
  | @{term Trueprop} $ (Const (@{const_name "op ="}, _) $ _ $ Abs _) =>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   112
      norm_def ctxt (thm RS @{thm fun_cong})
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   113
  | _ => thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   114
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
   115
fun atomize_conv ctxt ct =
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
   116
  (case Thm.term_of ct 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
   117
    @{term "op ==>"} $ _ $ _ =>
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
   118
      Conv.binop_conv (atomize_conv ctxt) then_conv
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
   119
      Conv.rewr_conv @{thm atomize_imp}
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
   120
  | Const (@{const_name "=="}, _) $ _ $ _ =>
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
   121
      Conv.binop_conv (atomize_conv ctxt) then_conv
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
   122
      Conv.rewr_conv @{thm atomize_eq}
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
   123
  | Const (@{const_name all}, _) $ Abs _ =>
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
   124
      More_Conv.binder_conv atomize_conv ctxt then_conv
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
   125
      Conv.rewr_conv @{thm atomize_all}
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
   126
  | _ => Conv.all_conv) ct
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
   127
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   128
fun normalize_rule ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   129
  Conv.fconv_rule (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   130
    Thm.beta_conversion true then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   131
    Thm.eta_conversion then_conv
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   132
    norm_binder_conv ctxt) #>
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   133
  norm_def ctxt #>
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   134
  Drule.forall_intr_vars #>
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
   135
  Conv.fconv_rule (atomize_conv ctxt)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   136
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   137
fun instantiate_free (cv, ct) thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   138
  if Term.exists_subterm (equal (Thm.term_of cv)) (Thm.prop_of thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   139
  then Thm.forall_elim ct (Thm.forall_intr cv thm)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   140
  else thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   141
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   142
fun discharge_definition ct thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   143
  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
   144
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   145
    Thm.implies_intr ct thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   146
    |> instantiate_free (cv, cu)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   147
    |> (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
   148
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   149
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   150
fun if_conv c cv1 cv2 ct = (if c (Thm.term_of ct) then cv1 else cv2) ct
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   151
fun if_true_conv c cv = if_conv c cv Conv.all_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   152
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   153
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   154
(* 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
   155
   most once) *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   156
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   157
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   158
  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
   159
    | 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
   160
    | 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
   161
    | count _ _ = 0
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   162
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   163
  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
   164
        (count 0 t <= 1)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   165
    | is_trivial_let _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   166
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   167
  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
   168
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   169
  fun cond_let_conv ctxt = if_true_conv (Term.exists_subterm is_trivial_let)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   170
    (More_Conv.top_conv let_conv ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   171
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   172
fun trivial_let ctxt = map (Conv.fconv_rule (cond_let_conv ctxt))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   173
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   174
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   175
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   176
(* 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
   177
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   178
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   179
  fun neg_numeral @{term Int.Min} = true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   180
    | neg_numeral _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   181
  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
   182
  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
   183
        Term.exists_subterm neg_numeral t andalso
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   184
        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
   185
    | is_neg_number _ _ = false
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   186
  fun has_neg_number ctxt = Term.exists_subterm (is_neg_number ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   187
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   188
  val pos_numeral_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   189
    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
   190
    addsimps [@{thm Int.numeral_1_eq_1}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   191
    addsimps @{thms Int.pred_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   192
    addsimps @{thms Int.normalize_bin_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   193
    addsimps @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   194
      "Int.Min = - Int.Bit1 Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   195
      "Int.Bit0 (- Int.Pls) = - Int.Pls"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   196
      "Int.Bit0 (- k) = - Int.Bit0 k"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   197
      "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
   198
      by simp_all (simp add: pred_def)}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   199
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   200
  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
   201
    (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
   202
    Conv.no_conv
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 cond_pos_conv ctxt = if_true_conv (has_neg_number ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   205
    (More_Conv.top_sweep_conv pos_conv ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   206
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   207
fun positive_numerals ctxt = map (Conv.fconv_rule (cond_pos_conv ctxt))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   208
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   209
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   210
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   211
(* 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
   212
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   213
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   214
  val nat_embedding = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   215
    "nat (int n) = n"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   216
    "i >= 0 --> int (nat i) = i"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   217
    "i < 0 --> int (nat i) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   218
    by simp_all}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   219
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   220
  val nat_rewriting = @{lemma
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   221
    "0 = nat 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   222
    "1 = nat 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   223
    "number_of i = nat (number_of i)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   224
    "int (nat 0) = 0"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   225
    "int (nat 1) = 1"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   226
    "a < b = (int a < int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   227
    "a <= b = (int a <= int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   228
    "Suc a = nat (int a + 1)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   229
    "a + b = nat (int a + int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   230
    "a - b = nat (int a - int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   231
    "a * b = nat (int a * int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   232
    "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
   233
    "a mod b = nat (int a mod int b)"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   234
    "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
   235
    "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
   236
    "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
   237
    "int (nat (int a mod int b)) = int a mod int b"
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   238
    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
   239
      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
   240
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   241
  fun on_positive num f x = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   242
    (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
   243
      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
   244
    | NONE => NONE)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   245
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   246
  val cancel_int_nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   247
    addsimps [@{thm Nat_Numeral.nat_number_of}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   248
    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
   249
    addsimps @{thms neg_simps}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   250
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   251
  fun cancel_int_nat_simproc _ ss ct = 
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   252
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   253
      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
   254
      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
   255
      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
   256
      fun tac _ = Simplifier.simp_tac simpset 1
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   257
    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
   258
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   259
  val nat_ss = HOL_ss
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   260
    addsimps nat_rewriting
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   261
    addsimprocs [Simplifier.make_simproc {
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   262
      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
   263
      proc = cancel_int_nat_simproc, identifier = [] }]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   264
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   265
  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
   266
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   267
  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
   268
  val uses_nat_int =
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   269
    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
   270
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   271
fun nat_as_int ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   272
  let
33895
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   273
    fun norm thm = thm
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   274
      |> uses_nat_type (Thm.prop_of thm) ? Conv.fconv_rule (conv ctxt)
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   275
    val thms' = map norm thms
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   276
  in
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   277
    if exists (uses_nat_int o Thm.prop_of) thms' then nat_embedding @ thms'
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   278
    else thms'
3e7c51bbeb24 only add nat/int conversion rules if necessary
boehmes
parents: 33664
diff changeset
   279
  end
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   280
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   281
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   282
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   283
(* include additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   284
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   285
val (unfold_defs, unfold_defs_setup) =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   286
  Attrib.config_bool "smt_unfold_defs" true
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   287
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   288
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   289
  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
   290
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   291
  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
   292
  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
   293
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   294
  val fun_upd_rules = [@{thm fun_upd_same}, @{thm fun_upd_apply}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   295
  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
   296
  val exists_fun_upd = Term.exists_subterm is_fun_upd
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   297
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   298
fun add_pair_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   299
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   300
  |> exists (exists_pair_type o Thm.prop_of) thms ? append pair_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   301
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   302
fun add_fun_upd_rules _ thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   303
  thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   304
  |> exists (exists_fun_upd o Thm.prop_of) thms ? append fun_upd_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   305
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   306
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   307
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   308
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   309
  fun mk_entry t thm = (Term.head_of t, (thm, thm RS @{thm eq_reflection}))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   310
  fun prepare_def thm =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   311
    (case HOLogic.dest_Trueprop (Thm.prop_of thm) of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   312
      Const (@{const_name "op ="}, _) $ t $ _ => mk_entry t thm
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   313
    | t => raise TERM ("prepare_def", [t]))
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   314
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   315
  val defs = map prepare_def [
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   316
    @{thm abs_if[where 'a = int]}, @{thm abs_if[where 'a = real]},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   317
    @{thm min_def[where 'a = int]}, @{thm min_def[where 'a = real]},
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   318
    @{thm max_def[where 'a = int]}, @{thm max_def[where 'a = real]}]
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   319
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   320
  fun add_sym t = if AList.defined (op =) defs t then insert (op =) t else I
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   321
  fun add_syms thms = fold (Term.fold_aterms add_sym o Thm.prop_of) thms []
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   322
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
   323
  fun unfold_def_conv ds ct =
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
   324
    (case AList.lookup (op =) ds (Term.head_of (Thm.term_of ct)) of
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   325
      SOME (_, eq) => Conv.rewr_conv eq
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   326
    | NONE => Conv.all_conv) ct
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
   327
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
   328
  fun unfold_conv 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
   329
    (case filter (member (op =) (add_syms [thm]) o fst) defs 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
   330
      [] => 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
   331
    | ds => thm |> Conv.fconv_rule
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
   332
        (More_Conv.bottom_conv (K (unfold_def_conv ds)) ctxt))
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   333
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   334
fun add_abs_min_max_rules ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   335
  if Config.get ctxt unfold_defs
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
   336
  then map (unfold_conv ctxt) thms
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   337
  else map fst (map_filter (AList.lookup (op =) defs) (add_syms thms)) @ thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   338
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   339
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   340
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   341
(* lift lambda terms into additional rules *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   342
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   343
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   344
  val meta_eq = @{cpat "op =="}
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   345
  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
   346
  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
   347
  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
   348
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   349
  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
   350
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   351
  fun used_vars cvs ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   352
    let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   353
      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
   354
      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
   355
    in Term.fold_aterms (add o lookup) (Thm.term_of ct) [] end
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   356
  fun make_def cvs eq = Thm.symmetric (fold norm_meta_def cvs eq)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   357
  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
   358
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   359
  fun replace ctxt cvs ct (cx as (nctxt, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   360
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   361
      val cvs' = used_vars cvs ct
33355
ee5b5ef5e970 abstract over variables in reversed order (application uses given order)
boehmes
parents: 33299
diff changeset
   362
      val ct' = fold_rev Thm.cabs cvs' ct
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   363
    in
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   364
      (case Termtab.lookup defs (Thm.term_of ct') of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   365
        SOME (_, eq) => (make_def cvs' eq, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   366
      | NONE =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   367
          let
33664
d62805a237ef removed unused code and unused arguments,
boehmes
parents: 33355
diff changeset
   368
            val {T, ...} = Thm.rep_cterm ct'
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   369
            val (n, nctxt') = fresh_name "" nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   370
            val eq = Thm.assume (mk_meta_eq (cert ctxt (Free (n, T))) ct')
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   371
          in (make_def cvs' eq, (nctxt', add_def ct' eq defs)) end)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   372
    end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   373
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   374
  fun none ct cx = (Thm.reflexive ct, cx)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   375
  fun in_comb f g ct cx =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   376
    let val (cu1, cu2) = Thm.dest_comb ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   377
    in cx |> f cu1 ||>> g cu2 |>> uncurry Thm.combination end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   378
  fun in_arg f = in_comb none f
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   379
  fun in_abs f cvs ct (nctxt, defs) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   380
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   381
      val (n, nctxt') = fresh_name Name.uu nctxt
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   382
      val (cv, cu) = Thm.dest_abs (SOME n) ct
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   383
    in f (cv :: cvs) cu (nctxt', defs) |>> Thm.abstract_rule n cv end
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   384
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   385
  fun replace_lambdas ctxt =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   386
    let
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   387
      fun repl cvs ct =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   388
        (case Thm.term_of ct of
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   389
          Const (@{const_name All}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   390
        | Const (@{const_name Ex}, _) $ Abs _ => in_arg (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   391
        | Const _ $ Abs _ => in_arg (at_lambda cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   392
        | Const (@{const_name Let}, _) $ _ $ Abs _ =>
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   393
            in_comb (in_arg (repl cvs)) (in_abs repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   394
        | Abs _ => at_lambda cvs
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   395
        | _ $ _ => in_comb (repl cvs) (repl cvs)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   396
        | _ => none) ct
33299
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   397
      and at_lambda cvs ct =
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   398
        in_abs repl cvs ct #-> (fn thm =>
73af7831ba1e simplified method syntax of "smt",
boehmes
parents: 33243
diff changeset
   399
        replace ctxt cvs (Thm.rhs_of thm) #>> Thm.transitive thm)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   400
    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
   401
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
   402
  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
   403
    (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
   404
      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
   405
    | 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
   406
    | 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
   407
        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
   408
    | 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
   409
    | 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
   410
    | _ => false)
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   411
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   412
fun lift_lambdas ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   413
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   414
    val declare_frees = fold (Thm.fold_terms Term.declare_term_frees)
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   415
    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
   416
      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
   417
      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
   418
33010
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   419
    val rev_int_fst_ord = rev_order o int_ord o pairself fst
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   420
    fun ordered_values tab =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   421
      Termtab.fold (fn (_, x) => OrdList.insert rev_int_fst_ord x) tab []
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   422
      |> map snd
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   423
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   424
    val (thms', (_, defs)) =
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   425
      (declare_frees thms (Name.make_context []), Termtab.empty)
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   426
      |> fold_map (rewrite (replace_lambdas ctxt)) thms
39f73a59e855 added proof reconstructon for Z3,
boehmes
parents: 32740
diff changeset
   427
    val eqs = ordered_values defs
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   428
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   429
    (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
   430
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   431
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   432
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   433
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   434
(* 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
   435
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   436
local
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   437
  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
   438
  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
   439
  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
   440
  fun traverse t =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   441
    (case Term.strip_comb t of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   442
      (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
   443
    | (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
   444
    | (Abs (_, _, u), ts) => fold traverse (u :: ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   445
    | (_, ts) => fold traverse ts)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   446
  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
   447
  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
   448
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   449
  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
   450
  fun nary_conv conv1 conv2 ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   451
    (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
   452
  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
   453
    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
   454
    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
   455
  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
   456
in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   457
fun explicit_application ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   458
  let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   459
    fun sub_conv tb ctxt ct =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   460
      (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
   461
        (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
   462
      | (Free (n, _), ts) => app_conv tb (free n) (length ts) ctxt
33664
d62805a237ef removed unused code and unused arguments,
boehmes
parents: 33355
diff changeset
   463
      | (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
   464
      | (_, _) => 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
   465
    and app_conv tb n i ctxt =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   466
      (case Symtab.lookup tb n of
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   467
        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
   468
      | 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
   469
    and apply_conv tb ctxt i ct = (
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   470
      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
   471
      else
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   472
        Conv.rewr_conv apply_rule then_conv
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   473
        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
   474
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
   475
    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
   476
        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
   477
      | 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
   478
      | 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
   479
      | _ => 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
   480
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
   481
    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
   482
      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
   483
      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
   484
32618
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   485
    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
   486
  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
   487
end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   488
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   489
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   490
(* combined normalization *)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   491
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   492
datatype config =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   493
  RewriteTrivialLets |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   494
  RewriteNegativeNumerals |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   495
  RewriteNaturalNumbers |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   496
  AddPairRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   497
  AddFunUpdRules |
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   498
  AddAbsMinMaxRules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   499
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   500
fun normalize config ctxt thms =
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   501
  let fun if_enabled c f = member (op =) config c ? f ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   502
  in
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   503
    thms
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   504
    |> if_enabled RewriteTrivialLets trivial_let
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   505
    |> if_enabled RewriteNegativeNumerals positive_numerals
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   506
    |> if_enabled RewriteNaturalNumbers nat_as_int
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   507
    |> if_enabled AddPairRules add_pair_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   508
    |> if_enabled AddFunUpdRules add_fun_upd_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   509
    |> if_enabled AddAbsMinMaxRules add_abs_min_max_rules
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   510
    |> map (normalize_rule ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   511
    |> lift_lambdas ctxt
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   512
    |> apsnd (explicit_application ctxt)
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   513
  end
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   514
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   515
val setup = unfold_defs_setup
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   516
42865636d006 added new method "smt": an oracle-based connection to external SMT solvers
boehmes
parents:
diff changeset
   517
end