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