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