src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Wed May 12 23:54:04 2010 +0200 (2010-05-12)
changeset 36899 bcd6fce5bf06
parent 36898 8e55aa1306c5
child 37124 fe22fc54b876
permissions -rw-r--r--
layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/smt_translate.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
Translate theorems into an SMT intermediate format and serialize them.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature SMT_TRANSLATE =
boehmes@36898
     8
sig
boehmes@36898
     9
  (* intermediate term structure *)
boehmes@36898
    10
  datatype squant = SForall | SExists
boehmes@36898
    11
  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    12
  datatype sterm =
boehmes@36898
    13
    SVar of int |
boehmes@36898
    14
    SApp of string * sterm list |
boehmes@36898
    15
    SLet of string * sterm * sterm |
boehmes@36898
    16
    SQua of squant * string list * sterm spattern list * sterm
boehmes@36898
    17
boehmes@36898
    18
  (* configuration options *)
boehmes@36898
    19
  type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36899
    20
  type header = Proof.context -> term list -> string list
boehmes@36898
    21
  type strict = {
boehmes@36898
    22
    is_builtin_conn: string * typ -> bool,
boehmes@36899
    23
    is_builtin_pred: Proof.context -> string * typ -> bool,
boehmes@36898
    24
    is_builtin_distinct: bool}
boehmes@36898
    25
  type builtins = {
boehmes@36899
    26
    builtin_typ: Proof.context -> typ -> string option,
boehmes@36899
    27
    builtin_num: Proof.context -> typ -> int -> string option,
boehmes@36899
    28
    builtin_fun: Proof.context -> string * typ -> term list ->
boehmes@36899
    29
      (string * term list) option }
boehmes@36898
    30
  type sign = {
boehmes@36899
    31
    header: string list,
boehmes@36898
    32
    sorts: string list,
boehmes@36898
    33
    funcs: (string * (string list * string)) list }
boehmes@36898
    34
  type config = {
boehmes@36898
    35
    prefixes: prefixes,
boehmes@36899
    36
    header: header,
boehmes@36898
    37
    strict: strict option,
boehmes@36898
    38
    builtins: builtins,
boehmes@36898
    39
    serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    40
  type recon = {
boehmes@36898
    41
    typs: typ Symtab.table,
boehmes@36898
    42
    terms: term Symtab.table,
boehmes@36898
    43
    unfolds: thm list,
boehmes@36899
    44
    assms: thm list }
boehmes@36898
    45
boehmes@36898
    46
  val translate: config -> Proof.context -> string list -> thm list ->
boehmes@36898
    47
    string * recon
boehmes@36898
    48
end
boehmes@36898
    49
boehmes@36898
    50
structure SMT_Translate: SMT_TRANSLATE =
boehmes@36898
    51
struct
boehmes@36898
    52
boehmes@36898
    53
(* intermediate term structure *)
boehmes@36898
    54
boehmes@36898
    55
datatype squant = SForall | SExists
boehmes@36898
    56
boehmes@36898
    57
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    58
boehmes@36898
    59
datatype sterm =
boehmes@36898
    60
  SVar of int |
boehmes@36898
    61
  SApp of string * sterm list |
boehmes@36898
    62
  SLet of string * sterm * sterm |
boehmes@36898
    63
  SQua of squant * string list * sterm spattern list * sterm
boehmes@36898
    64
boehmes@36898
    65
boehmes@36898
    66
boehmes@36898
    67
(* configuration options *)
boehmes@36898
    68
boehmes@36898
    69
type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    70
boehmes@36899
    71
type header = Proof.context -> term list -> string list
boehmes@36899
    72
boehmes@36898
    73
type strict = {
boehmes@36898
    74
  is_builtin_conn: string * typ -> bool,
boehmes@36899
    75
  is_builtin_pred: Proof.context -> string * typ -> bool,
boehmes@36898
    76
  is_builtin_distinct: bool}
boehmes@36898
    77
boehmes@36898
    78
type builtins = {
boehmes@36899
    79
  builtin_typ: Proof.context -> typ -> string option,
boehmes@36899
    80
  builtin_num: Proof.context -> typ -> int -> string option,
boehmes@36899
    81
  builtin_fun: Proof.context -> string * typ -> term list ->
boehmes@36899
    82
    (string * term list) option }
boehmes@36898
    83
boehmes@36898
    84
type sign = {
boehmes@36899
    85
  header: string list,
boehmes@36898
    86
  sorts: string list,
boehmes@36898
    87
  funcs: (string * (string list * string)) list }
boehmes@36898
    88
boehmes@36898
    89
type config = {
boehmes@36898
    90
  prefixes: prefixes,
boehmes@36899
    91
  header: header,
boehmes@36898
    92
  strict: strict option,
boehmes@36898
    93
  builtins: builtins,
boehmes@36898
    94
  serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    95
boehmes@36898
    96
type recon = {
boehmes@36898
    97
  typs: typ Symtab.table,
boehmes@36898
    98
  terms: term Symtab.table,
boehmes@36898
    99
  unfolds: thm list,
boehmes@36899
   100
  assms: thm list }
boehmes@36898
   101
boehmes@36898
   102
boehmes@36898
   103
boehmes@36898
   104
(* utility functions *)
boehmes@36898
   105
boehmes@36898
   106
val dest_funT =
boehmes@36898
   107
  let
boehmes@36898
   108
    fun dest Ts 0 T = (rev Ts, T)
boehmes@36898
   109
      | dest Ts i (Type ("fun", [T, U])) = dest (T::Ts) (i-1) U
boehmes@36898
   110
      | dest _ _ T = raise TYPE ("dest_funT", [T], [])
boehmes@36898
   111
  in dest [] end
boehmes@36898
   112
boehmes@36898
   113
val quantifier = (fn
boehmes@36898
   114
    @{const_name All} => SOME SForall
boehmes@36898
   115
  | @{const_name Ex} => SOME SExists
boehmes@36898
   116
  | _ => NONE)
boehmes@36898
   117
boehmes@36898
   118
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   119
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   120
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   121
boehmes@36898
   122
fun dest_pat ts (Const (@{const_name pat}, _) $ t) = SPat (rev (t :: ts))
boehmes@36898
   123
  | dest_pat ts (Const (@{const_name nopat}, _) $ t) = SNoPat (rev (t :: ts))
boehmes@36898
   124
  | dest_pat ts (Const (@{const_name andpat}, _) $ p $ t) = dest_pat (t::ts) p
boehmes@36898
   125
  | dest_pat _ t = raise TERM ("dest_pat", [t])
boehmes@36898
   126
boehmes@36898
   127
fun dest_trigger (@{term trigger} $ tl $ t) =
boehmes@36898
   128
      (map (dest_pat []) (HOLogic.dest_list tl), t)
boehmes@36898
   129
  | dest_trigger t = ([], t)
boehmes@36898
   130
boehmes@36898
   131
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   132
  let
boehmes@36898
   133
    val (Ts, u) = group_quant qn [T] t
boehmes@36898
   134
    val (ps, b) = dest_trigger u
boehmes@36898
   135
  in (q, rev Ts, ps, b) end)
boehmes@36898
   136
boehmes@36898
   137
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   138
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   139
boehmes@36898
   140
fun prop_of thm = HOLogic.dest_Trueprop (Thm.prop_of thm)
boehmes@36898
   141
boehmes@36898
   142
boehmes@36898
   143
boehmes@36898
   144
(* enforce a strict separation between formulas and terms *)
boehmes@36898
   145
boehmes@36898
   146
val term_eq_rewr = @{lemma "x term_eq y == x = y" by (simp add: term_eq_def)}
boehmes@36898
   147
boehmes@36898
   148
val term_bool = @{lemma "~(True term_eq False)" by (simp add: term_eq_def)}
boehmes@36898
   149
val term_bool' = Simplifier.rewrite_rule [term_eq_rewr] term_bool
boehmes@36898
   150
boehmes@36898
   151
boehmes@36898
   152
val needs_rewrite = Thm.prop_of #> Term.exists_subterm (fn
boehmes@36898
   153
    Const (@{const_name Let}, _) => true
boehmes@36898
   154
  | @{term "op = :: bool => _"} $ _ $ @{term True} => true
boehmes@36898
   155
  | Const (@{const_name If}, _) $ _ $ @{term True} $ @{term False} => true
boehmes@36898
   156
  | _ => false)
boehmes@36898
   157
boehmes@36898
   158
val rewrite_rules = [
boehmes@36898
   159
  Let_def,
boehmes@36898
   160
  @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@36898
   161
  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@36898
   162
boehmes@36898
   163
fun rewrite ctxt = Simplifier.full_rewrite
boehmes@36898
   164
  (Simplifier.context ctxt empty_ss addsimps rewrite_rules)
boehmes@36898
   165
boehmes@36898
   166
fun normalize ctxt thm =
boehmes@36898
   167
  if needs_rewrite thm then Conv.fconv_rule (rewrite ctxt) thm else thm
boehmes@36898
   168
boehmes@36898
   169
val unfold_rules = term_eq_rewr :: rewrite_rules
boehmes@36898
   170
boehmes@36898
   171
boehmes@36898
   172
val revert_types =
boehmes@36898
   173
  let
boehmes@36898
   174
    fun revert @{typ prop} = @{typ bool}
boehmes@36898
   175
      | revert (Type (n, Ts)) = Type (n, map revert Ts)
boehmes@36898
   176
      | revert T = T
boehmes@36898
   177
  in Term.map_types revert end
boehmes@36898
   178
boehmes@36898
   179
boehmes@36898
   180
fun strictify {is_builtin_conn, is_builtin_pred, is_builtin_distinct} ctxt =
boehmes@36898
   181
  let
boehmes@36898
   182
    fun is_builtin_conn' (@{const_name True}, _) = false
boehmes@36898
   183
      | is_builtin_conn' (@{const_name False}, _) = false
boehmes@36898
   184
      | is_builtin_conn' c = is_builtin_conn c
boehmes@36898
   185
boehmes@36898
   186
    val propT = @{typ prop} and boolT = @{typ bool}
boehmes@36898
   187
    val as_propT = (fn @{typ bool} => propT | T => T)
boehmes@36898
   188
    fun mapTs f g = Term.strip_type #> (fn (Ts, T) => map f Ts ---> g T)
boehmes@36898
   189
    fun conn (n, T) = (n, mapTs as_propT as_propT T)
boehmes@36898
   190
    fun pred (n, T) = (n, mapTs I as_propT T)
boehmes@36898
   191
boehmes@36898
   192
    val term_eq = @{term "op = :: bool => _"} |> Term.dest_Const |> pred
boehmes@36898
   193
    fun as_term t = Const term_eq $ t $ @{term True}
boehmes@36898
   194
boehmes@36898
   195
    val if_term = Const (@{const_name If}, [propT, boolT, boolT] ---> boolT)
boehmes@36898
   196
    fun wrap_in_if t = if_term $ t $ @{term True} $ @{term False}
boehmes@36898
   197
boehmes@36898
   198
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@36898
   199
boehmes@36898
   200
    fun in_term t =
boehmes@36898
   201
      (case Term.strip_comb t of
boehmes@36898
   202
        (c as Const (@{const_name If}, _), [t1, t2, t3]) =>
boehmes@36898
   203
          c $ in_form t1 $ in_term t2 $ in_term t3
boehmes@36898
   204
      | (h as Const c, ts) =>
boehmes@36899
   205
          if is_builtin_conn' (conn c) orelse is_builtin_pred ctxt (pred c)
boehmes@36898
   206
          then wrap_in_if (in_form t)
boehmes@36898
   207
          else Term.list_comb (h, map in_term ts)
boehmes@36898
   208
      | (h as Free _, ts) => Term.list_comb (h, map in_term ts)
boehmes@36898
   209
      | _ => t)
boehmes@36898
   210
boehmes@36898
   211
    and in_pat ((c as Const (@{const_name pat}, _)) $ t) = c $ in_term t
boehmes@36898
   212
      | in_pat ((c as Const (@{const_name nopat}, _)) $ t) = c $ in_term t
boehmes@36898
   213
      | in_pat ((c as Const (@{const_name andpat}, _)) $ p $ t) =
boehmes@36898
   214
          c $ in_pat p $ in_term t
boehmes@36898
   215
      | in_pat t = raise TERM ("in_pat", [t])
boehmes@36898
   216
boehmes@36898
   217
    and in_pats p = in_list @{typ pattern} in_pat p
boehmes@36898
   218
boehmes@36898
   219
    and in_trig ((c as @{term trigger}) $ p $ t) = c $ in_pats p $ in_form t
boehmes@36898
   220
      | in_trig t = in_form t
boehmes@36898
   221
boehmes@36898
   222
    and in_form t =
boehmes@36898
   223
      (case Term.strip_comb t of
boehmes@36898
   224
        (q as Const (qn, _), [Abs (n, T, t')]) =>
boehmes@36898
   225
          if is_some (quantifier qn) then q $ Abs (n, T, in_trig t')
boehmes@36898
   226
          else as_term (in_term t)
boehmes@36898
   227
      | (Const (c as (@{const_name distinct}, T)), [t']) =>
boehmes@36898
   228
          if is_builtin_distinct then Const (pred c) $ in_list T in_term t'
boehmes@36898
   229
          else as_term (in_term t)
boehmes@36898
   230
      | (Const c, ts) =>
boehmes@36898
   231
          if is_builtin_conn (conn c)
boehmes@36898
   232
          then Term.list_comb (Const (conn c), map in_form ts)
boehmes@36899
   233
          else if is_builtin_pred ctxt (pred c)
boehmes@36898
   234
          then Term.list_comb (Const (pred c), map in_term ts)
boehmes@36898
   235
          else as_term (in_term t)
boehmes@36898
   236
      | _ => as_term (in_term t))
boehmes@36898
   237
  in
boehmes@36898
   238
    map (normalize ctxt) #> (fn thms => ((unfold_rules, term_bool' :: thms),
boehmes@36898
   239
    map (in_form o prop_of) (term_bool :: thms)))
boehmes@36898
   240
  end
boehmes@36898
   241
boehmes@36898
   242
boehmes@36898
   243
boehmes@36898
   244
(* translation from Isabelle terms into SMT intermediate terms *)
boehmes@36898
   245
boehmes@36899
   246
val empty_context = (1, Typtab.empty, 1, Termtab.empty)
boehmes@36898
   247
boehmes@36899
   248
fun make_sign header (_, typs, _, terms) = {
boehmes@36899
   249
  header = header,
boehmes@36898
   250
  sorts = Typtab.fold (cons o snd) typs [],
boehmes@36898
   251
  funcs = Termtab.fold (cons o snd) terms [] }
boehmes@36898
   252
boehmes@36899
   253
fun make_recon (unfolds, assms) (_, typs, _, terms) = {
boehmes@36898
   254
  typs = Symtab.make (map swap (Typtab.dest typs)),
boehmes@36898
   255
  terms = Symtab.make (map (fn (t, (n, _)) => (n, t)) (Termtab.dest terms)),
boehmes@36898
   256
  unfolds = unfolds,
boehmes@36899
   257
  assms = assms }
boehmes@36898
   258
boehmes@36898
   259
fun string_of_index pre i = pre ^ string_of_int i
boehmes@36898
   260
boehmes@36899
   261
fun fresh_typ sort_prefix T (cx as (Tidx, typs, idx, terms)) =
boehmes@36898
   262
  (case Typtab.lookup typs T of
boehmes@36898
   263
    SOME s => (s, cx)
boehmes@36898
   264
  | NONE =>
boehmes@36898
   265
      let
boehmes@36898
   266
        val s = string_of_index sort_prefix Tidx
boehmes@36898
   267
        val typs' = Typtab.update (T, s) typs
boehmes@36899
   268
      in (s, (Tidx+1, typs', idx, terms)) end)
boehmes@36898
   269
boehmes@36899
   270
fun fresh_fun func_prefix t ss (cx as (Tidx, typs, idx, terms)) =
boehmes@36898
   271
  (case Termtab.lookup terms t of
boehmes@36898
   272
    SOME (f, _) => (f, cx)
boehmes@36898
   273
  | NONE =>
boehmes@36898
   274
      let
boehmes@36898
   275
        val f = string_of_index func_prefix idx
boehmes@36898
   276
        val terms' = Termtab.update (revert_types t, (f, ss)) terms
boehmes@36899
   277
      in (f, (Tidx, typs, idx+1, terms')) end)
boehmes@36898
   278
boehmes@36898
   279
fun relaxed thms = (([], thms), map prop_of thms)
boehmes@36898
   280
boehmes@36899
   281
fun with_context header f (ths, ts) =
boehmes@36898
   282
  let val (us, context) = fold_map f ts empty_context
boehmes@36899
   283
  in ((make_sign (header ts) context, us), make_recon ths context) end
boehmes@36898
   284
boehmes@36898
   285
boehmes@36899
   286
fun translate {prefixes, strict, header, builtins, serialize} ctxt comments =
boehmes@36898
   287
  let
boehmes@36898
   288
    val {sort_prefix, func_prefix} = prefixes
boehmes@36898
   289
    val {builtin_typ, builtin_num, builtin_fun} = builtins
boehmes@36898
   290
boehmes@36899
   291
    fun transT T =
boehmes@36899
   292
      (case builtin_typ ctxt T of
boehmes@36898
   293
        SOME n => pair n
boehmes@36898
   294
      | NONE => fresh_typ sort_prefix T)
boehmes@36898
   295
boehmes@36898
   296
    fun app n ts = SApp (n, ts)
boehmes@36898
   297
boehmes@36898
   298
    fun trans t =
boehmes@36898
   299
      (case Term.strip_comb t of
boehmes@36898
   300
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   301
          (case dest_quant qn T t1 of
boehmes@36898
   302
            SOME (q, Ts, ps, b) =>
boehmes@36898
   303
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@36898
   304
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
boehmes@36898
   305
          | NONE => raise TERM ("intermediate", [t]))
boehmes@36898
   306
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   307
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   308
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@36898
   309
      | (h as Const (c as (@{const_name distinct}, T)), [t1]) =>
boehmes@36899
   310
          (case builtin_fun ctxt c (HOLogic.dest_list t1) of
boehmes@36899
   311
            SOME (n, ts) => fold_map trans ts #>> app n
boehmes@36898
   312
          | NONE => transs h T [t1])
boehmes@36898
   313
      | (h as Const (c as (_, T)), ts) =>
boehmes@36898
   314
          (case try HOLogic.dest_number t of
boehmes@36898
   315
            SOME (T, i) =>
boehmes@36899
   316
              (case builtin_num ctxt T i of
boehmes@36899
   317
                SOME n => pair (SApp (n, []))
boehmes@36898
   318
              | NONE => transs t T [])
boehmes@36898
   319
          | NONE =>
boehmes@36899
   320
              (case builtin_fun ctxt c ts of
boehmes@36899
   321
                SOME (n, ts') => fold_map trans ts' #>> app n
boehmes@36898
   322
              | NONE => transs h T ts))
boehmes@36898
   323
      | (h as Free (_, T), ts) => transs h T ts
boehmes@36898
   324
      | (Bound i, []) => pair (SVar i)
boehmes@36898
   325
      | _ => raise TERM ("intermediate", [t]))
boehmes@36898
   326
boehmes@36898
   327
    and transs t T ts =
boehmes@36898
   328
      let val (Us, U) = dest_funT (length ts) T
boehmes@36898
   329
      in
boehmes@36898
   330
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@36898
   331
        fresh_fun func_prefix t Up ##>> fold_map trans ts #>> SApp)
boehmes@36898
   332
      end
boehmes@36898
   333
  in
boehmes@36899
   334
    (case strict of SOME strct => strictify strct ctxt | NONE => relaxed) #>
boehmes@36899
   335
    with_context (header ctxt) trans #>> uncurry (serialize comments)
boehmes@36898
   336
  end
boehmes@36898
   337
boehmes@36898
   338
end