src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Fri Apr 08 19:04:08 2011 +0200 (2011-04-08)
changeset 42321 ce83c1654b86
parent 42319 9a8ba59aed06
child 43154 72e4753a6677
permissions -rw-r--r--
fixed eta-expansion: use correct order to apply new bound variables
     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 f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
   172 
   173   fun exp f T = eta f (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 I (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 I 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_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
   188       |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
   189     end
   190 in
   191 
   192 fun eta_expand ctxt is_fol 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 expand 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 expand 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           if is_fol then expand (Term.betapply (Abs a, t))
   209           else l $ expand t $ abs_expand a
   210       | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
   211           if is_fol then expand (u $ t)
   212           else l $ expand t $ exp expand (Term.range_type T) u
   213       | expand ((l as Const (@{const_name Let}, T)) $ t) =
   214           if is_fol then
   215             let val U = Term.domain_type (Term.range_type T)
   216             in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
   217           else exp2 T (l $ expand t)
   218       | expand (l as Const (@{const_name Let}, T)) =
   219           if is_fol then 
   220             let val U = Term.domain_type (Term.range_type T)
   221             in
   222               Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U,
   223                 Bound 0 $ Bound 1))
   224             end
   225           else exp2' T l
   226       | expand t =
   227           (case Term.strip_comb t of
   228             (u as Const (c as (_, T)), ts) =>
   229               (case SMT_Builtin.dest_builtin ctxt c ts of
   230                 SOME (_, k, us, mk) =>
   231                   if k = length us then mk (map expand us)
   232                   else expf k (length ts) T (mk (map expand us))
   233               | NONE => exp_func u T (map expand ts))
   234           | (u as Free (_, T), ts) => exp_func u T (map expand ts)
   235           | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
   236           | (u, ts) => Term.list_comb (u, map expand ts))
   237 
   238     and abs_expand (n, T, t) = Abs (n, T, expand t)
   239   
   240   in map expand end
   241 
   242 end
   243 
   244 
   245 (** lambda-lifting **)
   246 
   247 local
   248   fun mk_def Ts T lhs rhs =
   249     let
   250       val eq = HOLogic.eq_const T $ lhs $ rhs
   251       val trigger =
   252         [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
   253         |> map (HOLogic.mk_list @{typ SMT.pattern})
   254         |> HOLogic.mk_list @{typ "SMT.pattern list"}
   255       fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
   256     in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
   257 
   258   fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
   259 
   260   fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
   261     | dest_abs Ts t = (Ts, t)
   262 
   263   fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
   264     let
   265       val t1 = mk_abs Us t
   266       val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
   267       fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
   268       val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
   269       val t2 = Term.subst_bounds (rs, t1)
   270       val Ts' = map (nth Ts) bs 
   271       val (_, t3) = dest_abs [] t2
   272       val t4 = mk_abs Ts' t2
   273 
   274       val T = Term.fastype_of1 (Us @ Ts, t)
   275       fun app f = Term.list_comb (f, map Bound (rev bs))
   276     in
   277       (case Termtab.lookup defs t4 of
   278         SOME (f, _) => (app f, cx)
   279       | NONE =>
   280           let
   281             val (n, ctxt') =
   282               yield_singleton Variable.variant_fixes Name.uu ctxt
   283             val (is, UTs) = split_list (map_index I (Us @ Ts'))
   284             val f = Free (n, rev UTs ---> T)
   285             val lhs = Term.list_comb (f, map Bound (rev is))
   286             val def = mk_def UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
   287           in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
   288     end
   289 
   290   fun traverse Ts t =
   291     (case t of
   292       (q as Const (@{const_name All}, _)) $ Abs a =>
   293         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   294     | (q as Const (@{const_name Ex}, _)) $ Abs a =>
   295         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   296     | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
   297         traverse Ts u ##>> abs_traverse Ts a #>>
   298         (fn (u', a') => l $ u' $ Abs a')
   299     | Abs _ =>
   300         let val (Us, u) = dest_abs [] t
   301         in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
   302     | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
   303     | _ => pair t)
   304 
   305   and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
   306 in
   307 
   308 fun lift_lambdas ctxt ts =
   309   (Termtab.empty, ctxt)
   310   |> fold_map (traverse []) ts
   311   |> (fn (us, (defs, ctxt')) =>
   312        (ctxt', Termtab.fold (cons o snd o snd) defs us))
   313 
   314 end
   315 
   316 
   317 (** introduce explicit applications **)
   318 
   319 local
   320   (*
   321     Make application explicit for functions with varying number of arguments.
   322   *)
   323 
   324   fun add t i = Termtab.map_default (t, i) (Integer.min i)
   325 
   326   fun min_arities t =
   327     (case Term.strip_comb t of
   328       (u as Const _, ts) => add u (length ts) #> fold min_arities ts
   329     | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
   330     | (Abs (_, _, u), ts) => min_arities u #> fold min_arities ts
   331     | (_, ts) => fold min_arities ts)
   332 
   333   fun app u (t, T) =
   334     (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
   335 
   336   fun apply i t T ts =
   337     let
   338       val (ts1, ts2) = chop i ts
   339       val (_, U) = SMT_Utils.dest_funT i T
   340     in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
   341 in
   342 
   343 fun intro_explicit_application ts =
   344   let
   345     val arities = fold min_arities ts Termtab.empty
   346     fun apply' t = apply (the (Termtab.lookup arities t)) t
   347 
   348     fun traverse Ts t =
   349       (case Term.strip_comb t of
   350         (u as Const (_, T), ts) => apply' u T (map (traverse Ts) ts)
   351       | (u as Free (_, T), ts) => apply' u T (map (traverse Ts) ts)
   352       | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   353       | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   354       | (u, ts) => traverses Ts u ts)
   355     and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   356   in map (traverse []) ts end
   357 
   358 val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
   359 
   360 end
   361 
   362 
   363 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   364 
   365 local
   366   val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
   367     by (simp add: SMT.term_true_def SMT.term_false_def)}
   368 
   369   val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
   370 
   371   val fol_rules = [
   372     Let_def,
   373     mk_meta_eq @{thm SMT.term_true_def},
   374     mk_meta_eq @{thm SMT.term_false_def},
   375     @{lemma "P = True == P" by (rule eq_reflection) simp},
   376     @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   377 
   378   fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
   379 
   380   fun wrap_in_if t =
   381     @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
   382 
   383   fun is_builtin_conn_or_pred ctxt c ts =
   384     is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
   385     is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
   386 
   387   fun builtin b ctxt c ts =
   388     (case (Const c, ts) of
   389       (@{const HOL.eq (bool)}, [t, u]) =>
   390         if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
   391           SMT_Builtin.dest_builtin_eq ctxt t u
   392         else b ctxt c ts
   393     | _ => b ctxt c ts)
   394 in
   395 
   396 fun folify ctxt =
   397   let
   398     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   399 
   400     fun in_term t =
   401       (case Term.strip_comb t of
   402         (@{const True}, []) => @{const SMT.term_true}
   403       | (@{const False}, []) => @{const SMT.term_false}
   404       | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   405           u $ in_form t1 $ in_term t2 $ in_term t3
   406       | (Const (c as (n, _)), ts) =>
   407           if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
   408           else  if is_quant n then wrap_in_if (in_form t)
   409           else Term.list_comb (Const c, map in_term ts)
   410       | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
   411       | _ => t)
   412 
   413     and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
   414       | in_weight t = in_form t 
   415 
   416     and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
   417       | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
   418       | in_pat t = raise TERM ("bad pattern", [t])
   419 
   420     and in_pats ps =
   421       in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
   422 
   423     and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
   424           c $ in_pats p $ in_weight t
   425       | in_trigger t = in_weight t
   426 
   427     and in_form t =
   428       (case Term.strip_comb t of
   429         (q as Const (qn, _), [Abs (n, T, u)]) =>
   430           if is_quant qn then q $ Abs (n, T, in_trigger u)
   431           else as_term (in_term t)
   432       | (Const c, ts) =>
   433           (case SMT_Builtin.dest_builtin_conn ctxt c ts of
   434             SOME (_, _, us, mk) => mk (map in_form us)
   435           | NONE =>
   436               (case SMT_Builtin.dest_builtin_pred ctxt c ts of
   437                 SOME (_, _, us, mk) => mk (map in_term us)
   438               | NONE => as_term (in_term t)))
   439       | _ => as_term (in_term t))
   440   in
   441     map in_form #>
   442     cons (SMT_Utils.prop_of term_bool) #>
   443     pair (fol_rules, [term_bool], builtin)
   444   end
   445 
   446 end
   447 
   448 
   449 (* translation into intermediate format *)
   450 
   451 (** utility functions **)
   452 
   453 val quantifier = (fn
   454     @{const_name All} => SOME SForall
   455   | @{const_name Ex} => SOME SExists
   456   | _ => NONE)
   457 
   458 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   459       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   460   | group_quant _ Ts t = (Ts, t)
   461 
   462 fun dest_weight (@{const SMT.weight} $ w $ t) =
   463       (SOME (snd (HOLogic.dest_number w)), t)
   464   | dest_weight t = (NONE, t)
   465 
   466 fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
   467   | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
   468   | dest_pat t = raise TERM ("bad pattern", [t])
   469 
   470 fun dest_pats [] = I
   471   | dest_pats ts =
   472       (case map dest_pat ts |> split_list ||> distinct (op =) of
   473         (ps, [true]) => cons (SPat ps)
   474       | (ps, [false]) => cons (SNoPat ps)
   475       | _ => raise TERM ("bad multi-pattern", ts))
   476 
   477 fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
   478       (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
   479   | dest_trigger t = ([], t)
   480 
   481 fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   482   let
   483     val (Ts, u) = group_quant qn [T] t
   484     val (ps, p) = dest_trigger u
   485     val (w, b) = dest_weight p
   486   in (q, rev Ts, ps, w, b) end)
   487 
   488 fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   489   | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   490 
   491 
   492 (** translation from Isabelle terms into SMT intermediate terms **)
   493 
   494 fun intermediate header dtyps builtin ctxt ts trx =
   495   let
   496     fun transT (T as TFree _) = add_typ T true
   497       | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   498       | transT (T as Type _) =
   499           (case SMT_Builtin.dest_builtin_typ ctxt T of
   500             SOME n => pair n
   501           | NONE => add_typ T true)
   502 
   503     fun app n ts = SApp (n, ts)
   504 
   505     fun trans t =
   506       (case Term.strip_comb t of
   507         (Const (qn, _), [Abs (_, T, t1)]) =>
   508           (case dest_quant qn T t1 of
   509             SOME (q, Ts, ps, w, b) =>
   510               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   511               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
   512           | NONE => raise TERM ("unsupported quantifier", [t]))
   513       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   514           transT T ##>> trans t1 ##>> trans t2 #>>
   515           (fn ((U, u1), u2) => SLet (U, u1, u2))
   516       | (u as Const (c as (_, T)), ts) =>
   517           (case builtin ctxt c ts of
   518             SOME (n, _, us, _) => fold_map trans us #>> app n
   519           | NONE => transs u T ts)
   520       | (u as Free (_, T), ts) => transs u T ts
   521       | (Bound i, []) => pair (SVar i)
   522       | _ => raise TERM ("bad SMT term", [t]))
   523  
   524     and transs t T ts =
   525       let val (Us, U) = SMT_Utils.dest_funT (length ts) T
   526       in
   527         fold_map transT Us ##>> transT U #-> (fn Up =>
   528         add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   529       end
   530 
   531     val (us, trx') = fold_map trans ts trx
   532   in ((sign_of (header ts) dtyps trx', us), trx') end
   533 
   534 
   535 
   536 (* translation *)
   537 
   538 structure Configs = Generic_Data
   539 (
   540   type T = (Proof.context -> config) SMT_Utils.dict
   541   val empty = []
   542   val extend = I
   543   fun merge data = SMT_Utils.dict_merge fst data
   544 )
   545 
   546 fun add_config (cs, cfg) = Configs.map (SMT_Utils.dict_update (cs, cfg))
   547 
   548 fun get_config ctxt = 
   549   let val cs = SMT_Config.solver_class_of ctxt
   550   in
   551     (case SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
   552       SOME cfg => cfg ctxt
   553     | NONE => error ("SMT: no translation configuration found " ^
   554         "for solver class " ^ quote (SMT_Utils.string_of_class cs)))
   555   end
   556 
   557 fun translate ctxt comments ithms =
   558   let
   559     val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
   560 
   561     val with_datatypes =
   562       has_datatypes andalso Config.get ctxt SMT_Config.datatypes
   563 
   564     fun no_dtyps (tr_context, ctxt) ts =
   565       ((Termtab.empty, [], tr_context, ctxt), ts)
   566 
   567     val ts1 = map (Envir.beta_eta_contract o SMT_Utils.prop_of o snd) ithms
   568 
   569     val ((funcs, dtyps, tr_context, ctxt1), ts2) =
   570       ((make_tr_context prefixes, ctxt), ts1)
   571       |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
   572 
   573     val (ctxt2, ts3) =
   574       ts2
   575       |> eta_expand ctxt1 is_fol funcs
   576       |> lift_lambdas ctxt1
   577       ||> intro_explicit_application
   578 
   579     val ((rewrite_rules, extra_thms, builtin), ts4) =
   580       (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
   581 
   582     val rewrite_rules' = fun_app_eq :: rewrite_rules
   583   in
   584     (ts4, tr_context)
   585     |-> intermediate header dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
   586     |>> uncurry (serialize comments)
   587     ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
   588   end
   589 
   590 end