src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Mon Dec 20 22:02:57 2010 +0100 (2010-12-20)
changeset 41328 6792a5c92a58
parent 41281 679118e35378
child 41426 09615ed31f04
permissions -rw-r--r--
avoid ML structure aliases (especially single-letter abbreviations)
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@41123
     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@40664
    16
    SQua of squant * string list * sterm spattern list * int option * sterm
boehmes@36898
    17
boehmes@41127
    18
  (*translation configuration*)
boehmes@36898
    19
  type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    20
  type sign = {
boehmes@36899
    21
    header: string list,
boehmes@36898
    22
    sorts: string list,
boehmes@39298
    23
    dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    24
    funcs: (string * (string list * string)) list }
boehmes@36898
    25
  type config = {
boehmes@36898
    26
    prefixes: prefixes,
boehmes@41127
    27
    header: term list -> string list,
boehmes@41059
    28
    is_fol: bool,
boehmes@41059
    29
    has_datatypes: bool,
boehmes@36898
    30
    serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    31
  type recon = {
boehmes@41127
    32
    context: Proof.context,
boehmes@36898
    33
    typs: typ Symtab.table,
boehmes@36898
    34
    terms: term Symtab.table,
boehmes@41127
    35
    rewrite_rules: thm list,
boehmes@40161
    36
    assms: (int * thm) list }
boehmes@36898
    37
boehmes@41127
    38
  (*translation*)
boehmes@41127
    39
  val add_config: SMT_Utils.class * (Proof.context -> config) ->
boehmes@41127
    40
    Context.generic -> Context.generic 
boehmes@41127
    41
  val translate: Proof.context -> string list -> (int * thm) list ->
boehmes@36898
    42
    string * recon
boehmes@36898
    43
end
boehmes@36898
    44
boehmes@36898
    45
structure SMT_Translate: SMT_TRANSLATE =
boehmes@36898
    46
struct
boehmes@36898
    47
boehmes@40663
    48
boehmes@36898
    49
(* intermediate term structure *)
boehmes@36898
    50
boehmes@36898
    51
datatype squant = SForall | SExists
boehmes@36898
    52
boehmes@36898
    53
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    54
boehmes@36898
    55
datatype sterm =
boehmes@36898
    56
  SVar of int |
boehmes@36898
    57
  SApp of string * sterm list |
boehmes@36898
    58
  SLet of string * sterm * sterm |
boehmes@40664
    59
  SQua of squant * string list * sterm spattern list * int option * sterm
boehmes@36898
    60
boehmes@36898
    61
boehmes@36898
    62
boehmes@41127
    63
(* translation configuration *)
boehmes@36898
    64
boehmes@36898
    65
type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    66
boehmes@36898
    67
type sign = {
boehmes@36899
    68
  header: string list,
boehmes@36898
    69
  sorts: string list,
boehmes@39298
    70
  dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    71
  funcs: (string * (string list * string)) list }
boehmes@36898
    72
boehmes@36898
    73
type config = {
boehmes@36898
    74
  prefixes: prefixes,
boehmes@41127
    75
  header: term list -> string list,
boehmes@41059
    76
  is_fol: bool,
boehmes@41059
    77
  has_datatypes: bool,
boehmes@36898
    78
  serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    79
boehmes@36898
    80
type recon = {
boehmes@41127
    81
  context: Proof.context,
boehmes@36898
    82
  typs: typ Symtab.table,
boehmes@36898
    83
  terms: term Symtab.table,
boehmes@41127
    84
  rewrite_rules: thm list,
boehmes@40161
    85
  assms: (int * thm) list }
boehmes@36898
    86
boehmes@36898
    87
boehmes@36898
    88
boehmes@41127
    89
(* translation context *)
boehmes@41127
    90
boehmes@41127
    91
fun make_tr_context {sort_prefix, func_prefix} =
boehmes@41127
    92
  (sort_prefix, 1, Typtab.empty, func_prefix, 1, Termtab.empty)
boehmes@41127
    93
boehmes@41127
    94
fun string_of_index pre i = pre ^ string_of_int i
boehmes@41127
    95
boehmes@41127
    96
fun add_typ T proper (cx as (sp, Tidx, typs, fp, idx, terms)) =
boehmes@41127
    97
  (case Typtab.lookup typs T of
boehmes@41127
    98
    SOME (n, _) => (n, cx)
boehmes@41127
    99
  | NONE =>
boehmes@41127
   100
      let
boehmes@41127
   101
        val n = string_of_index sp Tidx
boehmes@41127
   102
        val typs' = Typtab.update (T, (n, proper)) typs
boehmes@41127
   103
      in (n, (sp, Tidx+1, typs', fp, idx, terms)) end)
boehmes@41127
   104
boehmes@41127
   105
fun add_fun t sort (cx as (sp, Tidx, typs, fp, idx, terms)) =
boehmes@41127
   106
  (case Termtab.lookup terms t of
boehmes@41127
   107
    SOME (n, _) => (n, cx)
boehmes@41127
   108
  | NONE => 
boehmes@41127
   109
      let
boehmes@41127
   110
        val n = string_of_index fp idx
boehmes@41127
   111
        val terms' = Termtab.update (t, (n, sort)) terms
boehmes@41127
   112
      in (n, (sp, Tidx, typs, fp, idx+1, terms')) end)
boehmes@41127
   113
boehmes@41127
   114
fun sign_of header dtyps (_, _, typs, _, _, terms) = {
boehmes@41127
   115
  header = header,
boehmes@41127
   116
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
boehmes@41127
   117
  dtyps = dtyps,
boehmes@41127
   118
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
boehmes@41127
   119
boehmes@41281
   120
fun recon_of ctxt rules thms ithms (_, _, typs, _, _, terms) =
boehmes@41127
   121
  let
boehmes@41281
   122
    fun add_typ (T, (n, _)) = Symtab.update (n, T)
boehmes@41127
   123
    val typs' = Typtab.fold add_typ typs Symtab.empty
boehmes@41127
   124
boehmes@41281
   125
    fun add_fun (t, (n, _)) = Symtab.update (n, t)
boehmes@41127
   126
    val terms' = Termtab.fold add_fun terms Symtab.empty
boehmes@41127
   127
boehmes@41127
   128
    val assms = map (pair ~1) thms @ ithms
boehmes@41127
   129
  in
boehmes@41127
   130
    {context=ctxt, typs=typs', terms=terms', rewrite_rules=rules, assms=assms}
boehmes@41127
   131
  end
boehmes@41127
   132
boehmes@41127
   133
boehmes@41127
   134
boehmes@41127
   135
(* preprocessing *)
boehmes@41127
   136
boehmes@41127
   137
(** FIXME **)
boehmes@41127
   138
boehmes@41127
   139
local
boehmes@41127
   140
  (*
boehmes@41281
   141
    force eta-expansion for constructors and selectors,
boehmes@41127
   142
    add missing datatype selectors via hypothetical definitions,
boehmes@41127
   143
    also return necessary datatype and record theorems
boehmes@41127
   144
  *)
boehmes@41127
   145
in
boehmes@41127
   146
boehmes@41127
   147
fun collect_datatypes_and_records (tr_context, ctxt) ts =
boehmes@41127
   148
  (([], tr_context, ctxt), ts)
boehmes@41127
   149
boehmes@41127
   150
end
boehmes@41127
   151
boehmes@41127
   152
boehmes@41127
   153
(** eta-expand quantifiers, let expressions and built-ins *)
boehmes@41127
   154
boehmes@41127
   155
local
boehmes@41127
   156
  fun eta T t = Abs (Name.uu, T, Term.incr_boundvars 1 t $ Bound 0)
boehmes@41127
   157
boehmes@41127
   158
  fun exp T = eta (Term.domain_type (Term.domain_type T))
boehmes@41127
   159
boehmes@41127
   160
  fun exp2 T q =
boehmes@41127
   161
    let val U = Term.domain_type T
boehmes@41127
   162
    in Abs (Name.uu, U, q $ eta (Term.domain_type U) (Bound 0)) end
boehmes@41127
   163
boehmes@41127
   164
  fun exp2' T l =
boehmes@41127
   165
    let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
boehmes@41127
   166
    in Abs (Name.uu, U1, eta U2 (l $ Bound 0)) end
boehmes@41127
   167
boehmes@41281
   168
  fun expf k i T t =
boehmes@41328
   169
    let val Ts = drop i (fst (SMT_Utils.dest_funT k T))
boehmes@41195
   170
    in
boehmes@41281
   171
      Term.incr_boundvars (length Ts) t
boehmes@41195
   172
      |> fold_index (fn (i, _) => fn u => u $ Bound i) Ts
boehmes@41195
   173
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
boehmes@41195
   174
    end
boehmes@41127
   175
in
boehmes@41127
   176
boehmes@41281
   177
fun eta_expand ctxt =
boehmes@41281
   178
  let
boehmes@41281
   179
    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
boehmes@41281
   180
      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp T t
boehmes@41281
   181
      | expand (q as Const (@{const_name All}, T)) = exp2 T q
boehmes@41281
   182
      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
boehmes@41281
   183
      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp T t
boehmes@41281
   184
      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
boehmes@41281
   185
      | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
boehmes@41281
   186
          l $ expand t $ abs_expand a
boehmes@41281
   187
      | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
boehmes@41281
   188
          l $ expand t $ exp (Term.range_type T) u
boehmes@41281
   189
      | expand ((l as Const (@{const_name Let}, T)) $ t) =
boehmes@41281
   190
          exp2 T (l $ expand t)
boehmes@41281
   191
      | expand (l as Const (@{const_name Let}, T)) = exp2' T l
boehmes@41281
   192
      | expand t =
boehmes@41281
   193
          (case Term.strip_comb t of
boehmes@41281
   194
            (u as Const (c as (_, T)), ts) =>
boehmes@41328
   195
              (case SMT_Builtin.dest_builtin ctxt c ts of
boehmes@41281
   196
                SOME (_, k, us, mk) =>
boehmes@41281
   197
                  if k = length us then mk (map expand us)
boehmes@41281
   198
                  else expf k (length ts) T (mk (map expand us))
boehmes@41281
   199
              | NONE => Term.list_comb (u, map expand ts))
boehmes@41281
   200
          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
boehmes@41281
   201
          | (u, ts) => Term.list_comb (u, map expand ts))
boehmes@41281
   202
boehmes@41281
   203
    and abs_expand (n, T, t) = Abs (n, T, expand t)
boehmes@41281
   204
  
boehmes@41281
   205
  in map expand end
boehmes@41127
   206
boehmes@41127
   207
end
boehmes@41127
   208
boehmes@41127
   209
boehmes@41127
   210
(** lambda-lifting **)
boehmes@41127
   211
boehmes@41127
   212
local
boehmes@41127
   213
  fun mk_def Ts T lhs rhs =
boehmes@41127
   214
    let
boehmes@41127
   215
      val eq = HOLogic.eq_const T $ lhs $ rhs
boehmes@41127
   216
      val trigger =
boehmes@41127
   217
        [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
boehmes@41127
   218
        |> map (HOLogic.mk_list @{typ SMT.pattern})
boehmes@41127
   219
        |> HOLogic.mk_list @{typ "SMT.pattern list"}
boehmes@41127
   220
      fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
boehmes@41127
   221
    in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
boehmes@41127
   222
boehmes@41232
   223
  fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
boehmes@41232
   224
boehmes@41232
   225
  fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
boehmes@41232
   226
    | dest_abs Ts t = (Ts, t)
boehmes@41232
   227
boehmes@41127
   228
  fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
boehmes@41127
   229
    let
boehmes@41232
   230
      val t1 = mk_abs Us t
boehmes@41232
   231
      val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
boehmes@41232
   232
      fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
boehmes@41232
   233
      val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
boehmes@41232
   234
      val t2 = Term.subst_bounds (rs, t1)
boehmes@41232
   235
      val Ts' = map (nth Ts) bs 
boehmes@41232
   236
      val (_, t3) = dest_abs [] t2
boehmes@41232
   237
      val t4 = mk_abs Ts' t2
boehmes@41232
   238
boehmes@41127
   239
      val T = Term.fastype_of1 (Us @ Ts, t)
boehmes@41232
   240
      fun app f = Term.list_comb (f, map Bound (rev bs))
boehmes@41127
   241
    in
boehmes@41232
   242
      (case Termtab.lookup defs t4 of
boehmes@41197
   243
        SOME (f, _) => (app f, cx)
boehmes@41127
   244
      | NONE =>
boehmes@41127
   245
          let
boehmes@41127
   246
            val (n, ctxt') =
boehmes@41127
   247
              yield_singleton Variable.variant_fixes Name.uu ctxt
boehmes@41232
   248
            val (is, UTs) = split_list (map_index I (Us @ Ts'))
boehmes@41232
   249
            val f = Free (n, rev UTs ---> T)
boehmes@41232
   250
            val lhs = Term.list_comb (f, map Bound (rev is))
boehmes@41232
   251
            val def = mk_def UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
boehmes@41232
   252
          in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
boehmes@41127
   253
    end
boehmes@41127
   254
boehmes@41127
   255
  fun traverse Ts t =
boehmes@41127
   256
    (case t of
boehmes@41127
   257
      (q as Const (@{const_name All}, _)) $ Abs a =>
boehmes@41127
   258
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   259
    | (q as Const (@{const_name Ex}, _)) $ Abs a =>
boehmes@41127
   260
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   261
    | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
boehmes@41127
   262
        traverse Ts u ##>> abs_traverse Ts a #>>
boehmes@41127
   263
        (fn (u', a') => l $ u' $ Abs a')
boehmes@41127
   264
    | Abs _ =>
boehmes@41127
   265
        let val (Us, u) = dest_abs [] t
boehmes@41127
   266
        in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
boehmes@41127
   267
    | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
boehmes@41127
   268
    | _ => pair t)
boehmes@41127
   269
boehmes@41127
   270
  and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
boehmes@41127
   271
in
boehmes@41127
   272
boehmes@41127
   273
fun lift_lambdas ctxt ts =
boehmes@41127
   274
  (Termtab.empty, ctxt)
boehmes@41127
   275
  |> fold_map (traverse []) ts
boehmes@41127
   276
  |> (fn (us, (defs, ctxt')) =>
boehmes@41127
   277
       (ctxt', Termtab.fold (cons o snd o snd) defs us))
boehmes@41127
   278
boehmes@41127
   279
end
boehmes@41127
   280
boehmes@41127
   281
boehmes@41127
   282
(** introduce explicit applications **)
boehmes@41127
   283
boehmes@41127
   284
local
boehmes@41127
   285
  (*
boehmes@41127
   286
    Make application explicit for functions with varying number of arguments.
boehmes@41127
   287
  *)
boehmes@41127
   288
boehmes@41232
   289
  fun add t i = Termtab.map_default (t, i) (Integer.min i)
boehmes@41127
   290
boehmes@41232
   291
  fun min_arities t =
boehmes@41232
   292
    (case Term.strip_comb t of
boehmes@41232
   293
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   294
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   295
    | (Abs (_, _, u), ts) => min_arities u #> fold min_arities ts
boehmes@41232
   296
    | (_, ts) => fold min_arities ts)
boehmes@41127
   297
boehmes@41232
   298
  fun app u (t, T) =
boehmes@41232
   299
    (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
boehmes@41127
   300
boehmes@41232
   301
  fun apply i t T ts =
boehmes@41328
   302
    let
boehmes@41328
   303
      val (ts1, ts2) = chop i ts
boehmes@41328
   304
      val (_, U) = SMT_Utils.dest_funT i T
boehmes@41328
   305
    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
boehmes@41127
   306
in
boehmes@41127
   307
boehmes@41127
   308
fun intro_explicit_application ts =
boehmes@41127
   309
  let
boehmes@41232
   310
    val arities = fold min_arities ts Termtab.empty
boehmes@41232
   311
    fun apply' t = apply (the (Termtab.lookup arities t)) t
boehmes@41127
   312
boehmes@41232
   313
    fun traverse Ts t =
boehmes@41127
   314
      (case Term.strip_comb t of
boehmes@41232
   315
        (u as Const (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   316
      | (u as Free (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   317
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
boehmes@41232
   318
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
boehmes@41232
   319
      | (u, ts) => traverses Ts u ts)
boehmes@41232
   320
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
boehmes@41232
   321
  in map (traverse []) ts end
boehmes@41127
   322
boehmes@41127
   323
val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
boehmes@41127
   324
boehmes@41127
   325
end
boehmes@41127
   326
boehmes@41127
   327
boehmes@41127
   328
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
boehmes@41127
   329
boehmes@41281
   330
local
boehmes@41281
   331
  val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
boehmes@41281
   332
    by (simp add: SMT.term_true_def SMT.term_false_def)}
boehmes@41127
   333
boehmes@41281
   334
  val fol_rules = [
boehmes@41281
   335
    Let_def,
boehmes@41281
   336
    mk_meta_eq @{thm SMT.term_true_def},
boehmes@41281
   337
    mk_meta_eq @{thm SMT.term_false_def},
boehmes@41281
   338
    @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@41281
   339
    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@41127
   340
boehmes@41281
   341
  fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
boehmes@41281
   342
        reduce_let (Term.betapply (u, t))
boehmes@41281
   343
    | reduce_let (t $ u) = reduce_let t $ reduce_let u
boehmes@41281
   344
    | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
boehmes@41281
   345
    | reduce_let t = t
boehmes@41127
   346
boehmes@41281
   347
  fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
boehmes@41127
   348
boehmes@41281
   349
  fun wrap_in_if t =
boehmes@41281
   350
    @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
boehmes@41281
   351
boehmes@41281
   352
  fun is_builtin_conn_or_pred ctxt c ts =
boehmes@41328
   353
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
boehmes@41328
   354
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
boehmes@41127
   355
boehmes@41281
   356
  fun builtin b ctxt c ts =
boehmes@41281
   357
    (case (Const c, ts) of
boehmes@41281
   358
      (@{const HOL.eq (bool)}, [t, u]) =>
boehmes@41281
   359
        if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
boehmes@41328
   360
          SMT_Builtin.dest_builtin_eq ctxt t u
boehmes@41281
   361
        else b ctxt c ts
boehmes@41281
   362
    | _ => b ctxt c ts)
boehmes@41281
   363
in
boehmes@41127
   364
boehmes@41281
   365
fun folify ctxt =
boehmes@41127
   366
  let
boehmes@41127
   367
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@41127
   368
boehmes@41127
   369
    fun in_term t =
boehmes@41127
   370
      (case Term.strip_comb t of
boehmes@41281
   371
        (@{const True}, []) => @{const SMT.term_true}
boehmes@41281
   372
      | (@{const False}, []) => @{const SMT.term_false}
boehmes@41281
   373
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
boehmes@41281
   374
          u $ in_form t1 $ in_term t2 $ in_term t3
boehmes@41127
   375
      | (Const c, ts) =>
boehmes@41281
   376
          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
boehmes@41281
   377
          else Term.list_comb (Const c, map in_term ts)
boehmes@41281
   378
      | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
boehmes@41127
   379
      | _ => t)
boehmes@41127
   380
boehmes@41127
   381
    and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
boehmes@41127
   382
      | in_weight t = in_form t 
boehmes@41127
   383
boehmes@41281
   384
    and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
boehmes@41281
   385
      | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
boehmes@41127
   386
      | in_pat t = raise TERM ("bad pattern", [t])
boehmes@41127
   387
boehmes@41127
   388
    and in_pats ps =
boehmes@41232
   389
      in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
boehmes@41127
   390
boehmes@41281
   391
    and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
boehmes@41232
   392
          c $ in_pats p $ in_weight t
boehmes@41281
   393
      | in_trigger t = in_weight t
boehmes@41127
   394
boehmes@41127
   395
    and in_form t =
boehmes@41127
   396
      (case Term.strip_comb t of
boehmes@41127
   397
        (q as Const (qn, _), [Abs (n, T, u)]) =>
boehmes@41127
   398
          if member (op =) [@{const_name All}, @{const_name Ex}] qn then
boehmes@41281
   399
            q $ Abs (n, T, in_trigger u)
boehmes@41127
   400
          else as_term (in_term t)
boehmes@41281
   401
      | (Const c, ts) =>
boehmes@41328
   402
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
boehmes@41281
   403
            SOME (_, _, us, mk) => mk (map in_form us)
boehmes@41281
   404
          | NONE =>
boehmes@41328
   405
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
boehmes@41281
   406
                SOME (_, _, us, mk) => mk (map in_term us)
boehmes@41281
   407
              | NONE => as_term (in_term t)))
boehmes@41127
   408
      | _ => as_term (in_term t))
boehmes@41127
   409
  in
boehmes@41127
   410
    map (reduce_let #> in_form) #>
boehmes@41328
   411
    cons (SMT_Utils.prop_of term_bool) #>
boehmes@41281
   412
    pair (fol_rules, [term_bool], builtin)
boehmes@41127
   413
  end
boehmes@41127
   414
boehmes@41281
   415
end
boehmes@41127
   416
boehmes@41127
   417
boehmes@41127
   418
(* translation into intermediate format *)
boehmes@41127
   419
boehmes@41127
   420
(** utility functions **)
boehmes@36898
   421
boehmes@36898
   422
val quantifier = (fn
boehmes@36898
   423
    @{const_name All} => SOME SForall
boehmes@36898
   424
  | @{const_name Ex} => SOME SExists
boehmes@36898
   425
  | _ => NONE)
boehmes@36898
   426
boehmes@36898
   427
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   428
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   429
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   430
boehmes@40664
   431
fun dest_weight (@{const SMT.weight} $ w $ t) =
boehmes@41173
   432
      (SOME (snd (HOLogic.dest_number w)), t)
boehmes@40664
   433
  | dest_weight t = (NONE, t)
boehmes@40664
   434
boehmes@41232
   435
fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
boehmes@41232
   436
  | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
boehmes@41127
   437
  | dest_pat t = raise TERM ("bad pattern", [t])
boehmes@37124
   438
boehmes@37124
   439
fun dest_pats [] = I
boehmes@37124
   440
  | dest_pats ts =
boehmes@37124
   441
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   442
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   443
      | (ps, [false]) => cons (SNoPat ps)
boehmes@41127
   444
      | _ => raise TERM ("bad multi-pattern", ts))
boehmes@36898
   445
boehmes@41232
   446
fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
boehmes@37124
   447
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   448
  | dest_trigger t = ([], t)
boehmes@36898
   449
boehmes@36898
   450
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   451
  let
boehmes@36898
   452
    val (Ts, u) = group_quant qn [T] t
boehmes@40664
   453
    val (ps, p) = dest_trigger u
boehmes@40664
   454
    val (w, b) = dest_weight p
boehmes@40664
   455
  in (q, rev Ts, ps, w, b) end)
boehmes@36898
   456
boehmes@36898
   457
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   458
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   459
boehmes@36898
   460
boehmes@41127
   461
(** translation from Isabelle terms into SMT intermediate terms **)
boehmes@36898
   462
boehmes@41281
   463
fun intermediate header dtyps builtin ctxt ts trx =
boehmes@41059
   464
  let
boehmes@41127
   465
    fun transT (T as TFree _) = add_typ T true
boehmes@41127
   466
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
boehmes@41127
   467
      | transT (T as Type _) =
boehmes@41328
   468
          (case SMT_Builtin.dest_builtin_typ ctxt T of
boehmes@39298
   469
            SOME n => pair n
boehmes@41127
   470
          | NONE => add_typ T true)
boehmes@36898
   471
boehmes@36898
   472
    fun app n ts = SApp (n, ts)
boehmes@36898
   473
boehmes@36898
   474
    fun trans t =
boehmes@36898
   475
      (case Term.strip_comb t of
boehmes@36898
   476
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   477
          (case dest_quant qn T t1 of
boehmes@40664
   478
            SOME (q, Ts, ps, w, b) =>
boehmes@36898
   479
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@40664
   480
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
boehmes@41127
   481
          | NONE => raise TERM ("unsupported quantifier", [t]))
boehmes@36898
   482
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   483
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   484
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@41281
   485
      | (u as Const (c as (_, T)), ts) =>
boehmes@41281
   486
          (case builtin ctxt c ts of
boehmes@41281
   487
            SOME (n, _, us, _) => fold_map trans us #>> app n
boehmes@41281
   488
          | NONE => transs u T ts)
boehmes@41127
   489
      | (u as Free (_, T), ts) => transs u T ts
boehmes@36898
   490
      | (Bound i, []) => pair (SVar i)
boehmes@41127
   491
      | _ => raise TERM ("bad SMT term", [t]))
boehmes@41127
   492
 
boehmes@36898
   493
    and transs t T ts =
boehmes@41328
   494
      let val (Us, U) = SMT_Utils.dest_funT (length ts) T
boehmes@36898
   495
      in
boehmes@36898
   496
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@41127
   497
        add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   498
      end
boehmes@41127
   499
boehmes@41127
   500
    val (us, trx') = fold_map trans ts trx
boehmes@41127
   501
  in ((sign_of (header ts) dtyps trx', us), trx') end
boehmes@41127
   502
boehmes@41127
   503
boehmes@41127
   504
boehmes@41127
   505
(* translation *)
boehmes@41127
   506
boehmes@41127
   507
structure Configs = Generic_Data
boehmes@41127
   508
(
boehmes@41328
   509
  type T = (Proof.context -> config) SMT_Utils.dict
boehmes@41127
   510
  val empty = []
boehmes@41127
   511
  val extend = I
boehmes@41328
   512
  fun merge data = SMT_Utils.dict_merge fst data
boehmes@41127
   513
)
boehmes@41127
   514
boehmes@41328
   515
fun add_config (cs, cfg) = Configs.map (SMT_Utils.dict_update (cs, cfg))
boehmes@41127
   516
boehmes@41232
   517
fun get_config ctxt = 
boehmes@41232
   518
  let val cs = SMT_Config.solver_class_of ctxt
boehmes@41232
   519
  in
boehmes@41328
   520
    (case SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
boehmes@41232
   521
      SOME cfg => cfg ctxt
boehmes@41232
   522
    | NONE => error ("SMT: no translation configuration found " ^
boehmes@41328
   523
        "for solver class " ^ quote (SMT_Utils.string_of_class cs)))
boehmes@41232
   524
  end
boehmes@41232
   525
boehmes@41127
   526
fun translate ctxt comments ithms =
boehmes@41127
   527
  let
boehmes@41232
   528
    val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
boehmes@41232
   529
boehmes@41127
   530
    val with_datatypes =
boehmes@41127
   531
      has_datatypes andalso Config.get ctxt SMT_Config.datatypes
boehmes@41127
   532
boehmes@41127
   533
    fun no_dtyps (tr_context, ctxt) ts = (([], tr_context, ctxt), ts)
boehmes@41127
   534
boehmes@41328
   535
    val ts1 = map (Envir.beta_eta_contract o SMT_Utils.prop_of o snd) ithms
boehmes@41127
   536
boehmes@41127
   537
    val ((dtyps, tr_context, ctxt1), ts2) =
boehmes@41127
   538
      ((make_tr_context prefixes, ctxt), ts1)
boehmes@41127
   539
      |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
boehmes@41127
   540
boehmes@41127
   541
    val (ctxt2, ts3) =
boehmes@41127
   542
      ts2
boehmes@41281
   543
      |> eta_expand ctxt1
boehmes@41127
   544
      |> lift_lambdas ctxt1
boehmes@41127
   545
      ||> intro_explicit_application
boehmes@41127
   546
boehmes@41281
   547
    val ((rewrite_rules, extra_thms, builtin), ts4) =
boehmes@41281
   548
      (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
boehmes@41127
   549
boehmes@41127
   550
    val rewrite_rules' = fun_app_eq :: rewrite_rules
boehmes@36898
   551
  in
boehmes@41127
   552
    (ts4, tr_context)
boehmes@41328
   553
    |-> intermediate header dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
boehmes@41127
   554
    |>> uncurry (serialize comments)
boehmes@41281
   555
    ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
boehmes@36898
   556
  end
boehmes@36898
   557
boehmes@36898
   558
end