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