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