src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Mon Feb 21 09:14:48 2011 +0100 (2011-02-21)
changeset 41785 77dcc197df9a
parent 41426 09615ed31f04
child 42319 9a8ba59aed06
permissions -rw-r--r--
wrap occurrences of quantifiers in first-order terms (i.e., outside first-order formulas) in If-expressions
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@41426
   137
(** datatype declarations **)
boehmes@41127
   138
boehmes@41127
   139
fun collect_datatypes_and_records (tr_context, ctxt) ts =
boehmes@41426
   140
  let
boehmes@41426
   141
    val (declss, ctxt') =
boehmes@41426
   142
      fold (Term.fold_types SMT_Datatypes.add_decls) ts ([], ctxt)
boehmes@41426
   143
boehmes@41426
   144
    fun is_decl_typ T = exists (exists (equal T o fst)) declss
boehmes@41426
   145
boehmes@41426
   146
    fun add_typ' T proper =
boehmes@41426
   147
      (case SMT_Builtin.dest_builtin_typ ctxt' T of
boehmes@41426
   148
        SOME n => pair n
boehmes@41426
   149
      | NONE => add_typ T proper)
boehmes@41127
   150
boehmes@41426
   151
    fun tr_select sel =
boehmes@41426
   152
      let val T = Term.range_type (Term.fastype_of sel)
boehmes@41426
   153
      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
boehmes@41426
   154
    fun tr_constr (constr, selects) =
boehmes@41426
   155
      add_fun constr NONE ##>> fold_map tr_select selects
boehmes@41426
   156
    fun tr_typ (T, cases) = add_typ' T false ##>> fold_map tr_constr cases
boehmes@41426
   157
    val (declss', tr_context') = fold_map (fold_map tr_typ) declss tr_context
boehmes@41426
   158
boehmes@41426
   159
    fun add (constr, selects) =
boehmes@41426
   160
      Termtab.update (constr, length selects) #>
boehmes@41426
   161
      fold (Termtab.update o rpair 1) selects
boehmes@41426
   162
    val funcs = fold (fold (fold add o snd)) declss Termtab.empty
boehmes@41426
   163
boehmes@41426
   164
  in ((funcs, declss', tr_context', ctxt'), ts) end
boehmes@41426
   165
    (* FIXME: also return necessary datatype and record theorems *)
boehmes@41127
   166
boehmes@41127
   167
boehmes@41127
   168
(** eta-expand quantifiers, let expressions and built-ins *)
boehmes@41127
   169
boehmes@41127
   170
local
boehmes@41127
   171
  fun eta T t = Abs (Name.uu, T, Term.incr_boundvars 1 t $ Bound 0)
boehmes@41127
   172
boehmes@41127
   173
  fun exp T = eta (Term.domain_type (Term.domain_type T))
boehmes@41127
   174
boehmes@41127
   175
  fun exp2 T q =
boehmes@41127
   176
    let val U = Term.domain_type T
boehmes@41127
   177
    in Abs (Name.uu, U, q $ eta (Term.domain_type U) (Bound 0)) end
boehmes@41127
   178
boehmes@41127
   179
  fun exp2' T l =
boehmes@41127
   180
    let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
boehmes@41127
   181
    in Abs (Name.uu, U1, eta U2 (l $ Bound 0)) end
boehmes@41127
   182
boehmes@41281
   183
  fun expf k i T t =
boehmes@41328
   184
    let val Ts = drop i (fst (SMT_Utils.dest_funT k T))
boehmes@41195
   185
    in
boehmes@41281
   186
      Term.incr_boundvars (length Ts) t
boehmes@41195
   187
      |> fold_index (fn (i, _) => fn u => u $ Bound i) Ts
boehmes@41195
   188
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
boehmes@41195
   189
    end
boehmes@41127
   190
in
boehmes@41127
   191
boehmes@41426
   192
fun eta_expand ctxt funcs =
boehmes@41281
   193
  let
boehmes@41426
   194
    fun exp_func t T ts =
boehmes@41426
   195
      (case Termtab.lookup funcs t of
boehmes@41426
   196
        SOME k =>
boehmes@41426
   197
          Term.list_comb (t, ts)
boehmes@41426
   198
          |> k <> length ts ? expf k (length ts) T
boehmes@41426
   199
      | NONE => Term.list_comb (t, ts))
boehmes@41426
   200
boehmes@41281
   201
    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
boehmes@41281
   202
      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp T t
boehmes@41281
   203
      | expand (q as Const (@{const_name All}, T)) = exp2 T q
boehmes@41281
   204
      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
boehmes@41281
   205
      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp T t
boehmes@41281
   206
      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
boehmes@41281
   207
      | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
boehmes@41281
   208
          l $ expand t $ abs_expand a
boehmes@41281
   209
      | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
boehmes@41281
   210
          l $ expand t $ exp (Term.range_type T) u
boehmes@41281
   211
      | expand ((l as Const (@{const_name Let}, T)) $ t) =
boehmes@41281
   212
          exp2 T (l $ expand t)
boehmes@41281
   213
      | expand (l as Const (@{const_name Let}, T)) = exp2' T l
boehmes@41281
   214
      | expand t =
boehmes@41281
   215
          (case Term.strip_comb t of
boehmes@41281
   216
            (u as Const (c as (_, T)), ts) =>
boehmes@41328
   217
              (case SMT_Builtin.dest_builtin ctxt c ts of
boehmes@41281
   218
                SOME (_, k, us, mk) =>
boehmes@41281
   219
                  if k = length us then mk (map expand us)
boehmes@41281
   220
                  else expf k (length ts) T (mk (map expand us))
boehmes@41426
   221
              | NONE => exp_func u T (map expand ts))
boehmes@41426
   222
          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
boehmes@41281
   223
          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
boehmes@41281
   224
          | (u, ts) => Term.list_comb (u, map expand ts))
boehmes@41281
   225
boehmes@41281
   226
    and abs_expand (n, T, t) = Abs (n, T, expand t)
boehmes@41281
   227
  
boehmes@41281
   228
  in map expand end
boehmes@41127
   229
boehmes@41127
   230
end
boehmes@41127
   231
boehmes@41127
   232
boehmes@41127
   233
(** lambda-lifting **)
boehmes@41127
   234
boehmes@41127
   235
local
boehmes@41127
   236
  fun mk_def Ts T lhs rhs =
boehmes@41127
   237
    let
boehmes@41127
   238
      val eq = HOLogic.eq_const T $ lhs $ rhs
boehmes@41127
   239
      val trigger =
boehmes@41127
   240
        [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
boehmes@41127
   241
        |> map (HOLogic.mk_list @{typ SMT.pattern})
boehmes@41127
   242
        |> HOLogic.mk_list @{typ "SMT.pattern list"}
boehmes@41127
   243
      fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
boehmes@41127
   244
    in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
boehmes@41127
   245
boehmes@41232
   246
  fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
boehmes@41232
   247
boehmes@41232
   248
  fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
boehmes@41232
   249
    | dest_abs Ts t = (Ts, t)
boehmes@41232
   250
boehmes@41127
   251
  fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
boehmes@41127
   252
    let
boehmes@41232
   253
      val t1 = mk_abs Us t
boehmes@41232
   254
      val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
boehmes@41232
   255
      fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
boehmes@41232
   256
      val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
boehmes@41232
   257
      val t2 = Term.subst_bounds (rs, t1)
boehmes@41232
   258
      val Ts' = map (nth Ts) bs 
boehmes@41232
   259
      val (_, t3) = dest_abs [] t2
boehmes@41232
   260
      val t4 = mk_abs Ts' t2
boehmes@41232
   261
boehmes@41127
   262
      val T = Term.fastype_of1 (Us @ Ts, t)
boehmes@41232
   263
      fun app f = Term.list_comb (f, map Bound (rev bs))
boehmes@41127
   264
    in
boehmes@41232
   265
      (case Termtab.lookup defs t4 of
boehmes@41197
   266
        SOME (f, _) => (app f, cx)
boehmes@41127
   267
      | NONE =>
boehmes@41127
   268
          let
boehmes@41127
   269
            val (n, ctxt') =
boehmes@41127
   270
              yield_singleton Variable.variant_fixes Name.uu ctxt
boehmes@41232
   271
            val (is, UTs) = split_list (map_index I (Us @ Ts'))
boehmes@41232
   272
            val f = Free (n, rev UTs ---> T)
boehmes@41232
   273
            val lhs = Term.list_comb (f, map Bound (rev is))
boehmes@41232
   274
            val def = mk_def UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
boehmes@41232
   275
          in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
boehmes@41127
   276
    end
boehmes@41127
   277
boehmes@41127
   278
  fun traverse Ts t =
boehmes@41127
   279
    (case t of
boehmes@41127
   280
      (q as Const (@{const_name All}, _)) $ Abs a =>
boehmes@41127
   281
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   282
    | (q as Const (@{const_name Ex}, _)) $ Abs a =>
boehmes@41127
   283
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   284
    | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
boehmes@41127
   285
        traverse Ts u ##>> abs_traverse Ts a #>>
boehmes@41127
   286
        (fn (u', a') => l $ u' $ Abs a')
boehmes@41127
   287
    | Abs _ =>
boehmes@41127
   288
        let val (Us, u) = dest_abs [] t
boehmes@41127
   289
        in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
boehmes@41127
   290
    | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
boehmes@41127
   291
    | _ => pair t)
boehmes@41127
   292
boehmes@41127
   293
  and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
boehmes@41127
   294
in
boehmes@41127
   295
boehmes@41127
   296
fun lift_lambdas ctxt ts =
boehmes@41127
   297
  (Termtab.empty, ctxt)
boehmes@41127
   298
  |> fold_map (traverse []) ts
boehmes@41127
   299
  |> (fn (us, (defs, ctxt')) =>
boehmes@41127
   300
       (ctxt', Termtab.fold (cons o snd o snd) defs us))
boehmes@41127
   301
boehmes@41127
   302
end
boehmes@41127
   303
boehmes@41127
   304
boehmes@41127
   305
(** introduce explicit applications **)
boehmes@41127
   306
boehmes@41127
   307
local
boehmes@41127
   308
  (*
boehmes@41127
   309
    Make application explicit for functions with varying number of arguments.
boehmes@41127
   310
  *)
boehmes@41127
   311
boehmes@41232
   312
  fun add t i = Termtab.map_default (t, i) (Integer.min i)
boehmes@41127
   313
boehmes@41232
   314
  fun min_arities t =
boehmes@41232
   315
    (case Term.strip_comb t of
boehmes@41232
   316
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   317
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   318
    | (Abs (_, _, u), ts) => min_arities u #> fold min_arities ts
boehmes@41232
   319
    | (_, ts) => fold min_arities ts)
boehmes@41127
   320
boehmes@41232
   321
  fun app u (t, T) =
boehmes@41232
   322
    (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
boehmes@41127
   323
boehmes@41232
   324
  fun apply i t T ts =
boehmes@41328
   325
    let
boehmes@41328
   326
      val (ts1, ts2) = chop i ts
boehmes@41328
   327
      val (_, U) = SMT_Utils.dest_funT i T
boehmes@41328
   328
    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
boehmes@41127
   329
in
boehmes@41127
   330
boehmes@41127
   331
fun intro_explicit_application ts =
boehmes@41127
   332
  let
boehmes@41232
   333
    val arities = fold min_arities ts Termtab.empty
boehmes@41232
   334
    fun apply' t = apply (the (Termtab.lookup arities t)) t
boehmes@41127
   335
boehmes@41232
   336
    fun traverse Ts t =
boehmes@41127
   337
      (case Term.strip_comb t of
boehmes@41232
   338
        (u as Const (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   339
      | (u as Free (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   340
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
boehmes@41232
   341
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
boehmes@41232
   342
      | (u, ts) => traverses Ts u ts)
boehmes@41232
   343
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
boehmes@41232
   344
  in map (traverse []) ts end
boehmes@41127
   345
boehmes@41127
   346
val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
boehmes@41127
   347
boehmes@41127
   348
end
boehmes@41127
   349
boehmes@41127
   350
boehmes@41127
   351
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
boehmes@41127
   352
boehmes@41281
   353
local
boehmes@41281
   354
  val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
boehmes@41281
   355
    by (simp add: SMT.term_true_def SMT.term_false_def)}
boehmes@41127
   356
boehmes@41785
   357
  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
boehmes@41785
   358
boehmes@41281
   359
  val fol_rules = [
boehmes@41281
   360
    Let_def,
boehmes@41281
   361
    mk_meta_eq @{thm SMT.term_true_def},
boehmes@41281
   362
    mk_meta_eq @{thm SMT.term_false_def},
boehmes@41281
   363
    @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@41281
   364
    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@41127
   365
boehmes@41281
   366
  fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
boehmes@41281
   367
        reduce_let (Term.betapply (u, t))
boehmes@41281
   368
    | reduce_let (t $ u) = reduce_let t $ reduce_let u
boehmes@41281
   369
    | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
boehmes@41281
   370
    | reduce_let t = t
boehmes@41127
   371
boehmes@41281
   372
  fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
boehmes@41127
   373
boehmes@41281
   374
  fun wrap_in_if t =
boehmes@41281
   375
    @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
boehmes@41281
   376
boehmes@41281
   377
  fun is_builtin_conn_or_pred ctxt c ts =
boehmes@41328
   378
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
boehmes@41328
   379
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
boehmes@41127
   380
boehmes@41281
   381
  fun builtin b ctxt c ts =
boehmes@41281
   382
    (case (Const c, ts) of
boehmes@41281
   383
      (@{const HOL.eq (bool)}, [t, u]) =>
boehmes@41281
   384
        if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
boehmes@41328
   385
          SMT_Builtin.dest_builtin_eq ctxt t u
boehmes@41281
   386
        else b ctxt c ts
boehmes@41281
   387
    | _ => b ctxt c ts)
boehmes@41281
   388
in
boehmes@41127
   389
boehmes@41281
   390
fun folify ctxt =
boehmes@41127
   391
  let
boehmes@41127
   392
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@41127
   393
boehmes@41127
   394
    fun in_term t =
boehmes@41127
   395
      (case Term.strip_comb t of
boehmes@41281
   396
        (@{const True}, []) => @{const SMT.term_true}
boehmes@41281
   397
      | (@{const False}, []) => @{const SMT.term_false}
boehmes@41281
   398
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
boehmes@41281
   399
          u $ in_form t1 $ in_term t2 $ in_term t3
boehmes@41785
   400
      | (Const (c as (n, _)), ts) =>
boehmes@41281
   401
          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
boehmes@41785
   402
          else  if is_quant n then wrap_in_if (in_form t)
boehmes@41281
   403
          else Term.list_comb (Const c, map in_term ts)
boehmes@41281
   404
      | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
boehmes@41127
   405
      | _ => t)
boehmes@41127
   406
boehmes@41127
   407
    and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
boehmes@41127
   408
      | in_weight t = in_form t 
boehmes@41127
   409
boehmes@41281
   410
    and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
boehmes@41281
   411
      | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
boehmes@41127
   412
      | in_pat t = raise TERM ("bad pattern", [t])
boehmes@41127
   413
boehmes@41127
   414
    and in_pats ps =
boehmes@41232
   415
      in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
boehmes@41127
   416
boehmes@41281
   417
    and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
boehmes@41232
   418
          c $ in_pats p $ in_weight t
boehmes@41281
   419
      | in_trigger t = in_weight t
boehmes@41127
   420
boehmes@41127
   421
    and in_form t =
boehmes@41127
   422
      (case Term.strip_comb t of
boehmes@41127
   423
        (q as Const (qn, _), [Abs (n, T, u)]) =>
boehmes@41785
   424
          if is_quant qn then q $ Abs (n, T, in_trigger u)
boehmes@41127
   425
          else as_term (in_term t)
boehmes@41281
   426
      | (Const c, ts) =>
boehmes@41328
   427
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
boehmes@41281
   428
            SOME (_, _, us, mk) => mk (map in_form us)
boehmes@41281
   429
          | NONE =>
boehmes@41328
   430
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
boehmes@41281
   431
                SOME (_, _, us, mk) => mk (map in_term us)
boehmes@41281
   432
              | NONE => as_term (in_term t)))
boehmes@41127
   433
      | _ => as_term (in_term t))
boehmes@41127
   434
  in
boehmes@41127
   435
    map (reduce_let #> in_form) #>
boehmes@41328
   436
    cons (SMT_Utils.prop_of term_bool) #>
boehmes@41281
   437
    pair (fol_rules, [term_bool], builtin)
boehmes@41127
   438
  end
boehmes@41127
   439
boehmes@41281
   440
end
boehmes@41127
   441
boehmes@41127
   442
boehmes@41127
   443
(* translation into intermediate format *)
boehmes@41127
   444
boehmes@41127
   445
(** utility functions **)
boehmes@36898
   446
boehmes@36898
   447
val quantifier = (fn
boehmes@36898
   448
    @{const_name All} => SOME SForall
boehmes@36898
   449
  | @{const_name Ex} => SOME SExists
boehmes@36898
   450
  | _ => NONE)
boehmes@36898
   451
boehmes@36898
   452
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   453
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   454
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   455
boehmes@40664
   456
fun dest_weight (@{const SMT.weight} $ w $ t) =
boehmes@41173
   457
      (SOME (snd (HOLogic.dest_number w)), t)
boehmes@40664
   458
  | dest_weight t = (NONE, t)
boehmes@40664
   459
boehmes@41232
   460
fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
boehmes@41232
   461
  | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
boehmes@41127
   462
  | dest_pat t = raise TERM ("bad pattern", [t])
boehmes@37124
   463
boehmes@37124
   464
fun dest_pats [] = I
boehmes@37124
   465
  | dest_pats ts =
boehmes@37124
   466
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   467
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   468
      | (ps, [false]) => cons (SNoPat ps)
boehmes@41127
   469
      | _ => raise TERM ("bad multi-pattern", ts))
boehmes@36898
   470
boehmes@41232
   471
fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
boehmes@37124
   472
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   473
  | dest_trigger t = ([], t)
boehmes@36898
   474
boehmes@36898
   475
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   476
  let
boehmes@36898
   477
    val (Ts, u) = group_quant qn [T] t
boehmes@40664
   478
    val (ps, p) = dest_trigger u
boehmes@40664
   479
    val (w, b) = dest_weight p
boehmes@40664
   480
  in (q, rev Ts, ps, w, b) end)
boehmes@36898
   481
boehmes@36898
   482
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   483
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   484
boehmes@36898
   485
boehmes@41127
   486
(** translation from Isabelle terms into SMT intermediate terms **)
boehmes@36898
   487
boehmes@41281
   488
fun intermediate header dtyps builtin ctxt ts trx =
boehmes@41059
   489
  let
boehmes@41127
   490
    fun transT (T as TFree _) = add_typ T true
boehmes@41127
   491
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
boehmes@41127
   492
      | transT (T as Type _) =
boehmes@41328
   493
          (case SMT_Builtin.dest_builtin_typ ctxt T of
boehmes@39298
   494
            SOME n => pair n
boehmes@41127
   495
          | NONE => add_typ T true)
boehmes@36898
   496
boehmes@36898
   497
    fun app n ts = SApp (n, ts)
boehmes@36898
   498
boehmes@36898
   499
    fun trans t =
boehmes@36898
   500
      (case Term.strip_comb t of
boehmes@36898
   501
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   502
          (case dest_quant qn T t1 of
boehmes@40664
   503
            SOME (q, Ts, ps, w, b) =>
boehmes@36898
   504
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@40664
   505
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
boehmes@41127
   506
          | NONE => raise TERM ("unsupported quantifier", [t]))
boehmes@36898
   507
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   508
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   509
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@41281
   510
      | (u as Const (c as (_, T)), ts) =>
boehmes@41281
   511
          (case builtin ctxt c ts of
boehmes@41281
   512
            SOME (n, _, us, _) => fold_map trans us #>> app n
boehmes@41281
   513
          | NONE => transs u T ts)
boehmes@41127
   514
      | (u as Free (_, T), ts) => transs u T ts
boehmes@36898
   515
      | (Bound i, []) => pair (SVar i)
boehmes@41127
   516
      | _ => raise TERM ("bad SMT term", [t]))
boehmes@41127
   517
 
boehmes@36898
   518
    and transs t T ts =
boehmes@41328
   519
      let val (Us, U) = SMT_Utils.dest_funT (length ts) T
boehmes@36898
   520
      in
boehmes@36898
   521
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@41127
   522
        add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   523
      end
boehmes@41127
   524
boehmes@41127
   525
    val (us, trx') = fold_map trans ts trx
boehmes@41127
   526
  in ((sign_of (header ts) dtyps trx', us), trx') end
boehmes@41127
   527
boehmes@41127
   528
boehmes@41127
   529
boehmes@41127
   530
(* translation *)
boehmes@41127
   531
boehmes@41127
   532
structure Configs = Generic_Data
boehmes@41127
   533
(
boehmes@41328
   534
  type T = (Proof.context -> config) SMT_Utils.dict
boehmes@41127
   535
  val empty = []
boehmes@41127
   536
  val extend = I
boehmes@41328
   537
  fun merge data = SMT_Utils.dict_merge fst data
boehmes@41127
   538
)
boehmes@41127
   539
boehmes@41328
   540
fun add_config (cs, cfg) = Configs.map (SMT_Utils.dict_update (cs, cfg))
boehmes@41127
   541
boehmes@41232
   542
fun get_config ctxt = 
boehmes@41232
   543
  let val cs = SMT_Config.solver_class_of ctxt
boehmes@41232
   544
  in
boehmes@41328
   545
    (case SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
boehmes@41232
   546
      SOME cfg => cfg ctxt
boehmes@41232
   547
    | NONE => error ("SMT: no translation configuration found " ^
boehmes@41328
   548
        "for solver class " ^ quote (SMT_Utils.string_of_class cs)))
boehmes@41232
   549
  end
boehmes@41232
   550
boehmes@41127
   551
fun translate ctxt comments ithms =
boehmes@41127
   552
  let
boehmes@41232
   553
    val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
boehmes@41232
   554
boehmes@41127
   555
    val with_datatypes =
boehmes@41127
   556
      has_datatypes andalso Config.get ctxt SMT_Config.datatypes
boehmes@41127
   557
boehmes@41426
   558
    fun no_dtyps (tr_context, ctxt) ts =
boehmes@41426
   559
      ((Termtab.empty, [], tr_context, ctxt), ts)
boehmes@41127
   560
boehmes@41328
   561
    val ts1 = map (Envir.beta_eta_contract o SMT_Utils.prop_of o snd) ithms
boehmes@41127
   562
boehmes@41426
   563
    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
boehmes@41127
   564
      ((make_tr_context prefixes, ctxt), ts1)
boehmes@41127
   565
      |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
boehmes@41127
   566
boehmes@41127
   567
    val (ctxt2, ts3) =
boehmes@41127
   568
      ts2
boehmes@41426
   569
      |> eta_expand ctxt1 funcs
boehmes@41127
   570
      |> lift_lambdas ctxt1
boehmes@41127
   571
      ||> intro_explicit_application
boehmes@41127
   572
boehmes@41281
   573
    val ((rewrite_rules, extra_thms, builtin), ts4) =
boehmes@41281
   574
      (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
boehmes@41127
   575
boehmes@41127
   576
    val rewrite_rules' = fun_app_eq :: rewrite_rules
boehmes@36898
   577
  in
boehmes@41127
   578
    (ts4, tr_context)
boehmes@41328
   579
    |-> intermediate header dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
boehmes@41127
   580
    |>> uncurry (serialize comments)
boehmes@41281
   581
    ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
boehmes@36898
   582
  end
boehmes@36898
   583
boehmes@36898
   584
end