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