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