src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Thu Dec 16 13:34:28 2010 +0100 (2010-12-16)
changeset 41197 edab1efe0a70
parent 41196 d23af676f9f8
child 41198 aa627a799e8e
permissions -rw-r--r--
fix lambda-lifting: take level of bound variables into account and also apply bound variables from outer scope
     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 replace_lambda Us Ts t (cx as (defs, ctxt)) =
   253     let
   254       val T = Term.fastype_of1 (Us @ Ts, t)
   255       val lev = length Us
   256       val bs = sort int_ord (Term.add_loose_bnos (t, lev, []))
   257       val bss = map_index (fn (i, j) => (j + lev, i + lev)) bs
   258       val norm = perhaps (AList.lookup (op =) bss)
   259       val t' = Term.map_aterms (fn Bound i => Bound (norm i) | t => t) t
   260       val Ts' = map (nth Ts) bs
   261 
   262       fun mk_abs U u = Abs (Name.uu, U, u)
   263       val abs_rhs = fold mk_abs Ts' (fold mk_abs Us t')
   264 
   265       fun app f = Term.list_comb (f, map Bound bs)
   266     in
   267       (case Termtab.lookup defs abs_rhs of
   268         SOME (f, _) => (app f, cx)
   269       | NONE =>
   270           let
   271             val (n, ctxt') =
   272               yield_singleton Variable.variant_fixes Name.uu ctxt
   273             val f = Free (n, rev Ts' ---> (rev Us ---> T))
   274             fun mk_bapp i t = t $ Bound i
   275             val lhs =
   276               f
   277               |> fold_rev (mk_bapp o snd) bss
   278               |> fold_rev mk_bapp (0 upto (length Us - 1))
   279             val def = mk_def (Us @ Ts') T lhs t'
   280           in (app f, (Termtab.update (abs_rhs, (f, def)) defs, ctxt')) end)
   281     end
   282 
   283   fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
   284     | dest_abs Ts t = (Ts, t)
   285 
   286   fun traverse Ts t =
   287     (case t of
   288       (q as Const (@{const_name All}, _)) $ Abs a =>
   289         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   290     | (q as Const (@{const_name Ex}, _)) $ Abs a =>
   291         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   292     | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
   293         traverse Ts u ##>> abs_traverse Ts a #>>
   294         (fn (u', a') => l $ u' $ Abs a')
   295     | Abs _ =>
   296         let val (Us, u) = dest_abs [] t
   297         in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
   298     | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
   299     | _ => pair t)
   300 
   301   and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
   302 in
   303 
   304 fun lift_lambdas ctxt ts =
   305   (Termtab.empty, ctxt)
   306   |> fold_map (traverse []) ts
   307   |> (fn (us, (defs, ctxt')) =>
   308        (ctxt', Termtab.fold (cons o snd o snd) defs us))
   309 
   310 end
   311 
   312 
   313 (** introduce explicit applications **)
   314 
   315 local
   316   (*
   317     Make application explicit for functions with varying number of arguments.
   318   *)
   319 
   320   fun add t ts =
   321     Termtab.map_default (t, []) (Ord_List.insert int_ord (length ts))
   322 
   323   fun collect t =
   324     (case Term.strip_comb t of
   325       (u as Const _, ts) => add u ts #> fold collect ts
   326     | (u as Free _, ts) => add u ts #> fold collect ts
   327     | (Abs (_, _, u), ts) => collect u #> fold collect ts
   328     | (_, ts) => fold collect ts)
   329 
   330   fun app ts (t, T) =
   331     let val f = Const (@{const_name SMT.fun_app}, T --> T)
   332     in (Term.list_comb (f $ t, ts), snd (U.dest_funT (length ts) T)) end 
   333 
   334   fun appl _ _ [] = fst
   335     | appl _ [] ts = fst o app ts
   336     | appl i (k :: ks) ts =
   337         let val (ts1, ts2) = chop (k - i) ts
   338         in appl k ks ts2 o app ts1 end
   339 
   340   fun appl0 [_] ts (t, _) = Term.list_comb (t, ts)
   341     | appl0 (0 :: ks) ts tT = appl 0 ks ts tT
   342     | appl0 ks ts tT = appl 0 ks ts tT
   343 
   344   fun apply terms T t ts = appl0 (Termtab.lookup_list terms t) ts (t, T)
   345 
   346   fun get_arities i t =
   347     (case Term.strip_comb t of
   348       (Bound j, ts) =>
   349         (if i = j then Ord_List.insert int_ord (length ts) else I) #>
   350         fold (get_arities i) ts
   351     | (Abs (_, _, u), ts) => get_arities (i+1) u #> fold (get_arities i) ts
   352     | (_, ts) => fold (get_arities i) ts)
   353 in
   354 
   355 fun intro_explicit_application ts =
   356   let
   357     val terms = fold collect ts Termtab.empty
   358 
   359     fun traverse (env as (arities, Ts)) t =
   360       (case Term.strip_comb t of
   361         (u as Const (_, T), ts) => apply terms T u (map (traverse env) ts)
   362       | (u as Free (_, T), ts) => apply terms T u (map (traverse env) ts)
   363       | (u as Bound i, ts) =>
   364           appl0 (nth arities i) (map (traverse env) ts) (u, nth Ts i)
   365       | (Abs (n, T, u), ts) =>
   366           let val env' = (get_arities 0 u [0] :: arities, T :: Ts)
   367           in traverses env (Abs (n, T, traverse env' u)) ts end
   368       | (u, ts) => traverses env u ts)
   369     and traverses env t ts = Term.list_comb (t, map (traverse env) ts)
   370   in map (traverse ([], [])) ts end
   371 
   372 val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
   373 
   374 end
   375 
   376 
   377 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   378 
   379 val tboolT = @{typ SMT.term_bool}
   380 val term_true = Const (@{const_name True}, tboolT)
   381 val term_false = Const (@{const_name False}, tboolT)
   382 
   383 val term_bool = @{lemma "True ~= False" by simp}
   384 val term_bool_prop =
   385   let
   386     fun replace @{const HOL.eq (bool)} = @{const HOL.eq (SMT.term_bool)}
   387       | replace @{const True} = term_true
   388       | replace @{const False} = term_false
   389       | replace t = t
   390   in Term.map_aterms replace (U.prop_of term_bool) end
   391 
   392 val fol_rules = [
   393   Let_def,
   394   @{lemma "P = True == P" by (rule eq_reflection) simp},
   395   @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   396 
   397 fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
   398       reduce_let (Term.betapply (u, t))
   399   | reduce_let (t $ u) = reduce_let t $ reduce_let u
   400   | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
   401   | reduce_let t = t
   402 
   403 fun is_pred_type NONE = false
   404   | is_pred_type (SOME T) = (Term.body_type T = @{typ bool})
   405 
   406 fun is_conn_type NONE = false
   407   | is_conn_type (SOME T) =
   408       forall (equal @{typ bool}) (Term.body_type T :: Term.binder_types T)
   409 
   410 fun revert_typ @{typ SMT.term_bool} = @{typ bool}
   411   | revert_typ (Type (n, Ts)) = Type (n, map revert_typ Ts)
   412   | revert_typ T = T
   413 
   414 val revert_types = Term.map_types revert_typ
   415 
   416 fun folify ctxt builtins =
   417   let
   418     fun as_term t = @{const HOL.eq (SMT.term_bool)} $ t $ term_true
   419 
   420     fun as_tbool @{typ bool} = tboolT
   421       | as_tbool (Type (n, Ts)) = Type (n, map as_tbool Ts)
   422       | as_tbool T = T
   423     fun mapTs f g i = U.dest_funT i #> (fn (Ts, T) => map f Ts ---> g T)
   424     fun predT i = mapTs as_tbool I i
   425     fun funcT i = mapTs as_tbool as_tbool i
   426     fun func i (n, T) = (n, funcT i T)
   427 
   428     fun map_ifT T = T |> Term.dest_funT ||> funcT 2 |> (op -->)
   429     val if_term = @{const If (bool)} |> Term.dest_Const ||> map_ifT |> Const
   430     fun wrap_in_if t = if_term $ t $ term_true $ term_false
   431 
   432     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   433 
   434     fun in_term t =
   435       (case Term.strip_comb t of
   436         (Const (n as @{const_name If}, T), [t1, t2, t3]) =>
   437           Const (n, map_ifT T) $ in_form t1 $ in_term t2 $ in_term t3
   438       | (Const (@{const_name HOL.eq}, _), _) => wrap_in_if (in_form t)
   439       | (Var (ni as (_, i), T), ts) =>
   440           let val U = Vartab.lookup builtins ni
   441           in
   442             if is_conn_type U orelse is_pred_type U then wrap_in_if (in_form t)
   443             else Term.list_comb (Var (ni, funcT i T), map in_term ts)
   444           end
   445       | (Const c, ts) =>
   446           Term.list_comb (Const (func (length ts) c), map in_term ts)
   447       | (Free c, ts) =>
   448           Term.list_comb (Free (func (length ts) c), map in_term ts)
   449       | _ => t)
   450 
   451     and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
   452       | in_weight t = in_form t 
   453 
   454     and in_pat (Const (c as (@{const_name pat}, _)) $ t) =
   455           Const (func 1 c) $ in_term t
   456       | in_pat (Const (c as (@{const_name nopat}, _)) $ t) =
   457           Const (func 1 c) $ in_term t
   458       | in_pat t = raise TERM ("bad pattern", [t])
   459 
   460     and in_pats ps =
   461       in_list @{typ "pattern list"} (in_list @{typ pattern} in_pat) ps
   462 
   463     and in_trig ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_weight t
   464       | in_trig t = in_weight t
   465 
   466     and in_form t =
   467       (case Term.strip_comb t of
   468         (q as Const (qn, _), [Abs (n, T, u)]) =>
   469           if member (op =) [@{const_name All}, @{const_name Ex}] qn then
   470             q $ Abs (n, as_tbool T, in_trig u)
   471           else as_term (in_term t)
   472       | (u as Const (@{const_name If}, _), ts) =>
   473           Term.list_comb (u, map in_form ts)
   474       | (b as @{const HOL.eq (bool)}, ts) => Term.list_comb (b, map in_form ts)
   475       | (Const (n as @{const_name HOL.eq}, T), ts) =>
   476           Term.list_comb (Const (n, predT 2 T), map in_term ts)
   477       | (b as Var (ni as (_, i), T), ts) =>
   478           if is_conn_type (Vartab.lookup builtins ni) then
   479             Term.list_comb (b, map in_form ts)
   480           else if is_pred_type (Vartab.lookup builtins ni) then
   481             Term.list_comb (Var (ni, predT i T), map in_term ts)
   482           else as_term (in_term t)
   483       | _ => as_term (in_term t))
   484   in
   485     map (reduce_let #> in_form) #>
   486     cons (mark_builtins' ctxt term_bool_prop) #>
   487     pair (fol_rules, [term_bool])
   488   end
   489 
   490 
   491 
   492 (* translation into intermediate format *)
   493 
   494 (** utility functions **)
   495 
   496 val quantifier = (fn
   497     @{const_name All} => SOME SForall
   498   | @{const_name Ex} => SOME SExists
   499   | _ => NONE)
   500 
   501 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   502       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   503   | group_quant _ Ts t = (Ts, t)
   504 
   505 fun dest_weight (@{const SMT.weight} $ w $ t) =
   506       (SOME (snd (HOLogic.dest_number w)), t)
   507   | dest_weight t = (NONE, t)
   508 
   509 fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
   510   | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
   511   | dest_pat t = raise TERM ("bad pattern", [t])
   512 
   513 fun dest_pats [] = I
   514   | dest_pats ts =
   515       (case map dest_pat ts |> split_list ||> distinct (op =) of
   516         (ps, [true]) => cons (SPat ps)
   517       | (ps, [false]) => cons (SNoPat ps)
   518       | _ => raise TERM ("bad multi-pattern", ts))
   519 
   520 fun dest_trigger (@{const trigger} $ tl $ t) =
   521       (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
   522   | dest_trigger t = ([], t)
   523 
   524 fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   525   let
   526     val (Ts, u) = group_quant qn [T] t
   527     val (ps, p) = dest_trigger u
   528     val (w, b) = dest_weight p
   529   in (q, rev Ts, ps, w, b) end)
   530 
   531 fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   532   | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   533 
   534 
   535 (** translation from Isabelle terms into SMT intermediate terms **)
   536 
   537 fun intermediate header dtyps ctxt ts trx =
   538   let
   539     fun transT (T as TFree _) = add_typ T true
   540       | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   541       | transT (T as Type _) =
   542           (case B.builtin_typ ctxt T of
   543             SOME n => pair n
   544           | NONE => add_typ T true)
   545 
   546     val unmarked_builtins = [@{const_name If}, @{const_name HOL.eq}]
   547 
   548     fun app n ts = SApp (n, ts)
   549 
   550     fun trans t =
   551       (case Term.strip_comb t of
   552         (Const (qn, _), [Abs (_, T, t1)]) =>
   553           (case dest_quant qn T t1 of
   554             SOME (q, Ts, ps, w, b) =>
   555               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   556               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
   557           | NONE => raise TERM ("unsupported quantifier", [t]))
   558       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   559           transT T ##>> trans t1 ##>> trans t2 #>>
   560           (fn ((U, u1), u2) => SLet (U, u1, u2))
   561       | (Var ((n, _), _), ts) => fold_map trans ts #>> app n
   562       | (u as Const (c as (n, T)), ts) =>
   563           if member (op =) unmarked_builtins n then
   564             (case B.builtin_fun ctxt c ts of
   565               SOME (((m, _), _), us, _) => fold_map trans us #>> app m
   566             | NONE => raise TERM ("not a built-in symbol", [t]))
   567           else transs u T ts
   568       | (u as Free (_, T), ts) => transs u T ts
   569       | (Bound i, []) => pair (SVar i)
   570       | _ => raise TERM ("bad SMT term", [t]))
   571  
   572     and transs t T ts =
   573       let val (Us, U) = U.dest_funT (length ts) T
   574       in
   575         fold_map transT Us ##>> transT U #-> (fn Up =>
   576         add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   577       end
   578 
   579     val (us, trx') = fold_map trans ts trx
   580   in ((sign_of (header ts) dtyps trx', us), trx') end
   581 
   582 
   583 
   584 (* translation *)
   585 
   586 structure Configs = Generic_Data
   587 (
   588   type T = (Proof.context -> config) U.dict
   589   val empty = []
   590   val extend = I
   591   val merge = U.dict_merge fst
   592 )
   593 
   594 fun add_config (cs, cfg) = Configs.map (U.dict_update (cs, cfg))
   595 
   596 fun translate ctxt comments ithms =
   597   let
   598     val cs = SMT_Config.solver_class_of ctxt
   599     val {prefixes, is_fol, header, has_datatypes, serialize} =
   600       (case U.dict_get (Configs.get (Context.Proof ctxt)) cs of
   601         SOME cfg => cfg ctxt
   602       | NONE => error ("SMT: no translation configuration found " ^
   603           "for solver class " ^ quote (U.string_of_class cs)))
   604       
   605     val with_datatypes =
   606       has_datatypes andalso Config.get ctxt SMT_Config.datatypes
   607 
   608     fun no_dtyps (tr_context, ctxt) ts = (([], tr_context, ctxt), ts)
   609 
   610     val (builtins, ts1) =
   611       ithms
   612       |> map (Envir.beta_eta_contract o U.prop_of o snd)
   613       |> mark_builtins ctxt
   614 
   615     val ((dtyps, tr_context, ctxt1), ts2) =
   616       ((make_tr_context prefixes, ctxt), ts1)
   617       |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
   618 
   619     val (ctxt2, ts3) =
   620       ts2
   621       |> eta_expand
   622       |> lift_lambdas ctxt1
   623       ||> intro_explicit_application
   624 
   625     val ((rewrite_rules, extra_thms), ts4) =
   626       (if is_fol then folify ctxt2 builtins else pair ([], [])) ts3
   627 
   628     val rewrite_rules' = fun_app_eq :: rewrite_rules
   629   in
   630     (ts4, tr_context)
   631     |-> intermediate header dtyps ctxt2
   632     |>> uncurry (serialize comments)
   633     ||> recon_of ctxt2 rewrite_rules' extra_thms ithms revert_typ revert_types
   634   end
   635 
   636 end