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