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