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