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