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