src/HOL/Tools/SMT/smt_translate.ML
changeset 58061 3d060f43accb
parent 57541 147e3f1e0459
child 58360 dee1fd1cc631
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/SMT/smt_translate.ML	Thu Aug 28 00:40:38 2014 +0200
     1.3 @@ -0,0 +1,522 @@
     1.4 +(*  Title:      HOL/Tools/SMT/smt_translate.ML
     1.5 +    Author:     Sascha Boehme, TU Muenchen
     1.6 +
     1.7 +Translate theorems into an SMT intermediate format and serialize them.
     1.8 +*)
     1.9 +
    1.10 +signature SMT_TRANSLATE =
    1.11 +sig
    1.12 +  (*intermediate term structure*)
    1.13 +  datatype squant = SForall | SExists
    1.14 +  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
    1.15 +  datatype sterm =
    1.16 +    SVar of int |
    1.17 +    SApp of string * sterm list |
    1.18 +    SLet of string * sterm * sterm |
    1.19 +    SQua of squant * string list * sterm spattern list * sterm
    1.20 +
    1.21 +  (*translation configuration*)
    1.22 +  type sign = {
    1.23 +    logic: string,
    1.24 +    sorts: string list,
    1.25 +    dtyps: (string * (string * (string * string) list) list) list list,
    1.26 +    funcs: (string * (string list * string)) list }
    1.27 +  type config = {
    1.28 +    logic: term list -> string,
    1.29 +    has_datatypes: bool,
    1.30 +    serialize: (string * string) list -> string list -> sign -> sterm list -> string }
    1.31 +  type replay_data = {
    1.32 +    context: Proof.context,
    1.33 +    typs: typ Symtab.table,
    1.34 +    terms: term Symtab.table,
    1.35 +    ll_defs: term list,
    1.36 +    rewrite_rules: thm list,
    1.37 +    assms: (int * thm) list }
    1.38 +
    1.39 +  (*translation*)
    1.40 +  val add_config: SMT_Util.class * (Proof.context -> config) -> Context.generic -> Context.generic
    1.41 +  val translate: Proof.context -> (string * string) list -> string list -> (int * thm) list ->
    1.42 +    string * replay_data
    1.43 +end;
    1.44 +
    1.45 +structure SMT_Translate: SMT_TRANSLATE =
    1.46 +struct
    1.47 +
    1.48 +
    1.49 +(* intermediate term structure *)
    1.50 +
    1.51 +datatype squant = SForall | SExists
    1.52 +
    1.53 +datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
    1.54 +
    1.55 +datatype sterm =
    1.56 +  SVar of int |
    1.57 +  SApp of string * sterm list |
    1.58 +  SLet of string * sterm * sterm |
    1.59 +  SQua of squant * string list * sterm spattern list * sterm
    1.60 +
    1.61 +
    1.62 +(* translation configuration *)
    1.63 +
    1.64 +type sign = {
    1.65 +  logic: string,
    1.66 +  sorts: string list,
    1.67 +  dtyps: (string * (string * (string * string) list) list) list list,
    1.68 +  funcs: (string * (string list * string)) list }
    1.69 +
    1.70 +type config = {
    1.71 +  logic: term list -> string,
    1.72 +  has_datatypes: bool,
    1.73 +  serialize: (string * string) list -> string list -> sign -> sterm list -> string }
    1.74 +
    1.75 +type replay_data = {
    1.76 +  context: Proof.context,
    1.77 +  typs: typ Symtab.table,
    1.78 +  terms: term Symtab.table,
    1.79 +  ll_defs: term list,
    1.80 +  rewrite_rules: thm list,
    1.81 +  assms: (int * thm) list }
    1.82 +
    1.83 +
    1.84 +(* translation context *)
    1.85 +
    1.86 +fun add_components_of_typ (Type (s, Ts)) =
    1.87 +    cons (Long_Name.base_name s) #> fold_rev add_components_of_typ Ts
    1.88 +  | add_components_of_typ (TFree (s, _)) = cons (perhaps (try (unprefix "'")) s)
    1.89 +  | add_components_of_typ _ = I;
    1.90 +
    1.91 +fun suggested_name_of_typ T = space_implode "_" (add_components_of_typ T []);
    1.92 +
    1.93 +fun suggested_name_of_term (Const (s, _)) = Long_Name.base_name s
    1.94 +  | suggested_name_of_term (Free (s, _)) = s
    1.95 +  | suggested_name_of_term _ = Name.uu
    1.96 +
    1.97 +val empty_tr_context = (Name.context, Typtab.empty, Termtab.empty)
    1.98 +val safe_suffix = "$"
    1.99 +
   1.100 +fun add_typ T proper (cx as (names, typs, terms)) =
   1.101 +  (case Typtab.lookup typs T of
   1.102 +    SOME (name, _) => (name, cx)
   1.103 +  | NONE =>
   1.104 +      let
   1.105 +        val sugg = Name.desymbolize (SOME true) (suggested_name_of_typ T) ^ safe_suffix
   1.106 +        val (name, names') = Name.variant sugg names
   1.107 +        val typs' = Typtab.update (T, (name, proper)) typs
   1.108 +      in (name, (names', typs', terms)) end)
   1.109 +
   1.110 +fun add_fun t sort (cx as (names, typs, terms)) =
   1.111 +  (case Termtab.lookup terms t of
   1.112 +    SOME (name, _) => (name, cx)
   1.113 +  | NONE =>
   1.114 +      let
   1.115 +        val sugg = Name.desymbolize (SOME false) (suggested_name_of_term t) ^ safe_suffix
   1.116 +        val (name, names') = Name.variant sugg names
   1.117 +        val terms' = Termtab.update (t, (name, sort)) terms
   1.118 +      in (name, (names', typs, terms')) end)
   1.119 +
   1.120 +fun sign_of logic dtyps (_, typs, terms) = {
   1.121 +  logic = logic,
   1.122 +  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
   1.123 +  dtyps = dtyps,
   1.124 +  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
   1.125 +
   1.126 +fun replay_data_of ctxt ll_defs rules assms (_, typs, terms) =
   1.127 +  let
   1.128 +    fun add_typ (T, (n, _)) = Symtab.update (n, T)
   1.129 +    val typs' = Typtab.fold add_typ typs Symtab.empty
   1.130 +
   1.131 +    fun add_fun (t, (n, _)) = Symtab.update (n, t)
   1.132 +    val terms' = Termtab.fold add_fun terms Symtab.empty
   1.133 +  in
   1.134 +    {context = ctxt, typs = typs', terms = terms', ll_defs = ll_defs, rewrite_rules = rules,
   1.135 +     assms = assms}
   1.136 +  end
   1.137 +
   1.138 +
   1.139 +(* preprocessing *)
   1.140 +
   1.141 +(** datatype declarations **)
   1.142 +
   1.143 +fun collect_datatypes_and_records (tr_context, ctxt) ts =
   1.144 +  let
   1.145 +    val (declss, ctxt') = fold (Term.fold_types SMT_Datatypes.add_decls) ts ([], ctxt)
   1.146 +
   1.147 +    fun is_decl_typ T = exists (exists (equal T o fst)) declss
   1.148 +
   1.149 +    fun add_typ' T proper =
   1.150 +      (case SMT_Builtin.dest_builtin_typ ctxt' T of
   1.151 +        SOME n => pair n
   1.152 +      | NONE => add_typ T proper)
   1.153 +
   1.154 +    fun tr_select sel =
   1.155 +      let val T = Term.range_type (Term.fastype_of sel)
   1.156 +      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
   1.157 +    fun tr_constr (constr, selects) =
   1.158 +      add_fun constr NONE ##>> fold_map tr_select selects
   1.159 +    fun tr_typ (T, cases) = add_typ' T false ##>> fold_map tr_constr cases
   1.160 +    val (declss', tr_context') = fold_map (fold_map tr_typ) declss tr_context
   1.161 +
   1.162 +    fun add (constr, selects) =
   1.163 +      Termtab.update (constr, length selects) #>
   1.164 +      fold (Termtab.update o rpair 1) selects
   1.165 +    val funcs = fold (fold (fold add o snd)) declss Termtab.empty
   1.166 +  in ((funcs, declss', tr_context', ctxt'), ts) end
   1.167 +    (* FIXME: also return necessary datatype and record theorems *)
   1.168 +
   1.169 +
   1.170 +(** eta-expand quantifiers, let expressions and built-ins *)
   1.171 +
   1.172 +local
   1.173 +  fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
   1.174 +
   1.175 +  fun exp f T = eta f (Term.domain_type (Term.domain_type T))
   1.176 +
   1.177 +  fun exp2 T q =
   1.178 +    let val U = Term.domain_type T
   1.179 +    in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
   1.180 +
   1.181 +  fun expf k i T t =
   1.182 +    let val Ts = drop i (fst (SMT_Util.dest_funT k T))
   1.183 +    in
   1.184 +      Term.incr_boundvars (length Ts) t
   1.185 +      |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
   1.186 +      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
   1.187 +    end
   1.188 +in
   1.189 +
   1.190 +fun eta_expand ctxt funcs =
   1.191 +  let
   1.192 +    fun exp_func t T ts =
   1.193 +      (case Termtab.lookup funcs t of
   1.194 +        SOME k => Term.list_comb (t, ts) |> k <> length ts ? expf k (length ts) T
   1.195 +      | NONE => Term.list_comb (t, ts))
   1.196 +
   1.197 +    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
   1.198 +      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
   1.199 +      | expand (q as Const (@{const_name All}, T)) = exp2 T q
   1.200 +      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
   1.201 +      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
   1.202 +      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
   1.203 +      | expand (Const (@{const_name Let}, _) $ t $ u) = expand (Term.betapply (u, t))
   1.204 +      | expand (Const (@{const_name Let}, T) $ t) =
   1.205 +          let val U = Term.domain_type (Term.range_type T)
   1.206 +          in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
   1.207 +      | expand (Const (@{const_name Let}, T)) =
   1.208 +          let val U = Term.domain_type (Term.range_type T)
   1.209 +          in Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U, Bound 0 $ Bound 1)) end
   1.210 +      | expand t =
   1.211 +          (case Term.strip_comb t of
   1.212 +            (u as Const (c as (_, T)), ts) =>
   1.213 +              (case SMT_Builtin.dest_builtin ctxt c ts of
   1.214 +                SOME (_, k, us, mk) =>
   1.215 +                  if k = length us then mk (map expand us)
   1.216 +                  else if k < length us then chop k (map expand us) |>> mk |> Term.list_comb
   1.217 +                  else expf k (length ts) T (mk (map expand us))
   1.218 +              | NONE => exp_func u T (map expand ts))
   1.219 +          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
   1.220 +          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
   1.221 +          | (u, ts) => Term.list_comb (u, map expand ts))
   1.222 +
   1.223 +    and abs_expand (n, T, t) = Abs (n, T, expand t)
   1.224 +
   1.225 +  in map expand end
   1.226 +
   1.227 +end
   1.228 +
   1.229 +
   1.230 +(** introduce explicit applications **)
   1.231 +
   1.232 +local
   1.233 +  (*
   1.234 +    Make application explicit for functions with varying number of arguments.
   1.235 +  *)
   1.236 +
   1.237 +  fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
   1.238 +  fun add_type T = apsnd (Typtab.update (T, ()))
   1.239 +
   1.240 +  fun min_arities t =
   1.241 +    (case Term.strip_comb t of
   1.242 +      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
   1.243 +    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
   1.244 +    | (Abs (_, T, u), ts) => (can dest_funT T ? add_type T) #> min_arities u #> fold min_arities ts
   1.245 +    | (_, ts) => fold min_arities ts)
   1.246 +
   1.247 +  fun minimize types t i =
   1.248 +    let
   1.249 +      fun find_min j [] _ = j
   1.250 +        | find_min j (U :: Us) T =
   1.251 +            if Typtab.defined types T then j else find_min (j + 1) Us (U --> T)
   1.252 +
   1.253 +      val (Ts, T) = Term.strip_type (Term.type_of t)
   1.254 +    in find_min 0 (take i (rev Ts)) T end
   1.255 +
   1.256 +  fun app u (t, T) = (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
   1.257 +
   1.258 +  fun apply i t T ts =
   1.259 +    let
   1.260 +      val (ts1, ts2) = chop i ts
   1.261 +      val (_, U) = SMT_Util.dest_funT i T
   1.262 +    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
   1.263 +in
   1.264 +
   1.265 +fun intro_explicit_application ctxt funcs ts =
   1.266 +  let
   1.267 +    val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
   1.268 +    val arities' = Termtab.map (minimize types) arities (* FIXME: highly suspicious *)
   1.269 +
   1.270 +    fun app_func t T ts =
   1.271 +      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
   1.272 +      else apply (the (Termtab.lookup arities' t)) t T ts
   1.273 +
   1.274 +    fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
   1.275 +
   1.276 +    fun traverse Ts t =
   1.277 +      (case Term.strip_comb t of
   1.278 +        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
   1.279 +          q $ Abs (x, T, in_trigger (T :: Ts) u)
   1.280 +      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
   1.281 +          q $ Abs (x, T, in_trigger (T :: Ts) u)
   1.282 +      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
   1.283 +          q $ traverse Ts u1 $ traverse Ts u2
   1.284 +      | (u as Const (c as (_, T)), ts) =>
   1.285 +          (case SMT_Builtin.dest_builtin ctxt c ts of
   1.286 +            SOME (_, k, us, mk) =>
   1.287 +              let
   1.288 +                val (ts1, ts2) = chop k (map (traverse Ts) us)
   1.289 +                val U = Term.strip_type T |>> snd o chop k |> (op --->)
   1.290 +              in apply 0 (mk ts1) U ts2 end
   1.291 +          | NONE => app_func u T (map (traverse Ts) ts))
   1.292 +      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
   1.293 +      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   1.294 +      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   1.295 +      | (u, ts) => traverses Ts u ts)
   1.296 +    and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
   1.297 +      | in_trigger Ts t = traverse Ts t
   1.298 +    and in_pats Ts ps =
   1.299 +      in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
   1.300 +    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
   1.301 +      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
   1.302 +      | in_pat _ t = raise TERM ("bad pattern", [t])
   1.303 +    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   1.304 +  in map (traverse []) ts end
   1.305 +
   1.306 +val fun_app_eq = mk_meta_eq @{thm fun_app_def}
   1.307 +
   1.308 +end
   1.309 +
   1.310 +
   1.311 +(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   1.312 +
   1.313 +local
   1.314 +  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
   1.315 +
   1.316 +  val fol_rules = [
   1.317 +    Let_def,
   1.318 +    @{lemma "P = True == P" by (rule eq_reflection) simp},
   1.319 +    @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   1.320 +
   1.321 +  exception BAD_PATTERN of unit
   1.322 +
   1.323 +  fun wrap_in_if pat t =
   1.324 +    if pat then raise BAD_PATTERN () else @{const If (bool)} $ t $ @{const True} $ @{const False}
   1.325 +
   1.326 +  fun is_builtin_conn_or_pred ctxt c ts =
   1.327 +    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
   1.328 +    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
   1.329 +in
   1.330 +
   1.331 +fun folify ctxt =
   1.332 +  let
   1.333 +    fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
   1.334 +
   1.335 +    fun in_term pat t =
   1.336 +      (case Term.strip_comb t of
   1.337 +        (@{const True}, []) => t
   1.338 +      | (@{const False}, []) => t
   1.339 +      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   1.340 +          if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
   1.341 +      | (Const (c as (n, _)), ts) =>
   1.342 +          if is_builtin_conn_or_pred ctxt c ts then wrap_in_if pat (in_form t)
   1.343 +          else if is_quant n then wrap_in_if pat (in_form t)
   1.344 +          else Term.list_comb (Const c, map (in_term pat) ts)
   1.345 +      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
   1.346 +      | _ => t)
   1.347 +
   1.348 +    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
   1.349 +          p $ in_term true t
   1.350 +      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
   1.351 +          p $ in_term true t
   1.352 +      | in_pat t = raise TERM ("bad pattern", [t])
   1.353 +
   1.354 +    and in_pats ps =
   1.355 +      in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
   1.356 +
   1.357 +    and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
   1.358 +      | in_trigger t = in_form t
   1.359 +
   1.360 +    and in_form t =
   1.361 +      (case Term.strip_comb t of
   1.362 +        (q as Const (qn, _), [Abs (n, T, u)]) =>
   1.363 +          if is_quant qn then q $ Abs (n, T, in_trigger u)
   1.364 +          else in_term false t
   1.365 +      | (Const c, ts) =>
   1.366 +          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
   1.367 +            SOME (_, _, us, mk) => mk (map in_form us)
   1.368 +          | NONE =>
   1.369 +              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
   1.370 +                SOME (_, _, us, mk) => mk (map (in_term false) us)
   1.371 +              | NONE => in_term false t))
   1.372 +      | _ => in_term false t)
   1.373 +  in
   1.374 +    map in_form #>
   1.375 +    pair (fol_rules, I)
   1.376 +  end
   1.377 +
   1.378 +end
   1.379 +
   1.380 +
   1.381 +(* translation into intermediate format *)
   1.382 +
   1.383 +(** utility functions **)
   1.384 +
   1.385 +val quantifier = (fn
   1.386 +    @{const_name All} => SOME SForall
   1.387 +  | @{const_name Ex} => SOME SExists
   1.388 +  | _ => NONE)
   1.389 +
   1.390 +fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   1.391 +      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   1.392 +  | group_quant _ Ts t = (Ts, t)
   1.393 +
   1.394 +fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
   1.395 +  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
   1.396 +  | dest_pat t = raise TERM ("bad pattern", [t])
   1.397 +
   1.398 +fun dest_pats [] = I
   1.399 +  | dest_pats ts =
   1.400 +      (case map dest_pat ts |> split_list ||> distinct (op =) of
   1.401 +        (ps, [true]) => cons (SPat ps)
   1.402 +      | (ps, [false]) => cons (SNoPat ps)
   1.403 +      | _ => raise TERM ("bad multi-pattern", ts))
   1.404 +
   1.405 +fun dest_trigger (@{const trigger} $ tl $ t) =
   1.406 +      (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
   1.407 +  | dest_trigger t = ([], t)
   1.408 +
   1.409 +fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   1.410 +  let
   1.411 +    val (Ts, u) = group_quant qn [T] t
   1.412 +    val (ps, p) = dest_trigger u
   1.413 +  in (q, rev Ts, ps, p) end)
   1.414 +
   1.415 +fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   1.416 +  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   1.417 +
   1.418 +
   1.419 +(** translation from Isabelle terms into SMT intermediate terms **)
   1.420 +
   1.421 +fun intermediate logic dtyps builtin ctxt ts trx =
   1.422 +  let
   1.423 +    fun transT (T as TFree _) = add_typ T true
   1.424 +      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   1.425 +      | transT (T as Type _) =
   1.426 +          (case SMT_Builtin.dest_builtin_typ ctxt T of
   1.427 +            SOME n => pair n
   1.428 +          | NONE => add_typ T true)
   1.429 +
   1.430 +    fun app n ts = SApp (n, ts)
   1.431 +
   1.432 +    fun trans t =
   1.433 +      (case Term.strip_comb t of
   1.434 +        (Const (qn, _), [Abs (_, T, t1)]) =>
   1.435 +          (case dest_quant qn T t1 of
   1.436 +            SOME (q, Ts, ps, b) =>
   1.437 +              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   1.438 +              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
   1.439 +          | NONE => raise TERM ("unsupported quantifier", [t]))
   1.440 +      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   1.441 +          transT T ##>> trans t1 ##>> trans t2 #>> (fn ((U, u1), u2) => SLet (U, u1, u2))
   1.442 +      | (u as Const (c as (_, T)), ts) =>
   1.443 +          (case builtin ctxt c ts of
   1.444 +            SOME (n, _, us, _) => fold_map trans us #>> app n
   1.445 +          | NONE => transs u T ts)
   1.446 +      | (u as Free (_, T), ts) => transs u T ts
   1.447 +      | (Bound i, []) => pair (SVar i)
   1.448 +      | _ => raise TERM ("bad SMT term", [t]))
   1.449 +
   1.450 +    and transs t T ts =
   1.451 +      let val (Us, U) = SMT_Util.dest_funT (length ts) T
   1.452 +      in
   1.453 +        fold_map transT Us ##>> transT U #-> (fn Up =>
   1.454 +          add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   1.455 +      end
   1.456 +
   1.457 +    val (us, trx') = fold_map trans ts trx
   1.458 +  in ((sign_of (logic ts) dtyps trx', us), trx') end
   1.459 +
   1.460 +
   1.461 +(* translation *)
   1.462 +
   1.463 +structure Configs = Generic_Data
   1.464 +(
   1.465 +  type T = (Proof.context -> config) SMT_Util.dict
   1.466 +  val empty = []
   1.467 +  val extend = I
   1.468 +  fun merge data = SMT_Util.dict_merge fst data
   1.469 +)
   1.470 +
   1.471 +fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
   1.472 +
   1.473 +fun get_config ctxt =
   1.474 +  let val cs = SMT_Config.solver_class_of ctxt
   1.475 +  in
   1.476 +    (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
   1.477 +      SOME cfg => cfg ctxt
   1.478 +    | NONE => error ("SMT: no translation configuration found " ^
   1.479 +        "for solver class " ^ quote (SMT_Util.string_of_class cs)))
   1.480 +  end
   1.481 +
   1.482 +fun translate ctxt smt_options comments ithms =
   1.483 +  let
   1.484 +    val {logic, has_datatypes, serialize} = get_config ctxt
   1.485 +
   1.486 +    fun no_dtyps (tr_context, ctxt) ts =
   1.487 +      ((Termtab.empty, [], tr_context, ctxt), ts)
   1.488 +
   1.489 +    val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
   1.490 +
   1.491 +    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
   1.492 +      ((empty_tr_context, ctxt), ts1)
   1.493 +      |-> (if has_datatypes then collect_datatypes_and_records else no_dtyps)
   1.494 +
   1.495 +    fun is_binder (Const (@{const_name Let}, _) $ _) = true
   1.496 +      | is_binder t = Lambda_Lifting.is_quantifier t
   1.497 +
   1.498 +    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
   1.499 +          q $ Abs (n, T, mk_trigger t)
   1.500 +      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
   1.501 +          Term.domain_type T --> @{typ pattern}
   1.502 +          |> (fn T => Const (@{const_name pat}, T) $ lhs)
   1.503 +          |> SMT_Util.mk_symb_list @{typ pattern} o single
   1.504 +          |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
   1.505 +          |> (fn t => @{const trigger} $ t $ eq)
   1.506 +      | mk_trigger t = t
   1.507 +
   1.508 +    val (ctxt2, (ts3, ll_defs)) =
   1.509 +      ts2
   1.510 +      |> eta_expand ctxt1 funcs
   1.511 +      |> rpair ctxt1
   1.512 +      |-> Lambda_Lifting.lift_lambdas NONE is_binder
   1.513 +      |-> (fn (ts', ll_defs) => fn ctxt' =>
   1.514 +          (ctxt', (intro_explicit_application ctxt' funcs (map mk_trigger ll_defs @ ts'), ll_defs)))
   1.515 +
   1.516 +    val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
   1.517 +      |>> apfst (cons fun_app_eq)
   1.518 +  in
   1.519 +    (ts4, tr_context)
   1.520 +    |-> intermediate logic dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
   1.521 +    |>> uncurry (serialize smt_options comments)
   1.522 +    ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
   1.523 +  end
   1.524 +
   1.525 +end;