src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Sun Jun 26 19:10:02 2011 +0200 (2011-06-26)
changeset 43554 9bece8cbb5be
parent 43507 d566714a9ce1
child 43829 fba9754b827e
permissions -rw-r--r--
generalized introduction of explicit application constant: consider more functions as possible witness/instance of quantifiers than before (a constant of type T1 -> T2 -> T3 should be considered to have a rank less or equal to 1 if variables of type T2 -> T3 occur bound in a problem);
maintain extra-logical information when introducing explicit application;
handle let-expressions properly
     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     let
   337       fun find_min j [] _ = j
   338         | find_min j (U :: Us) T =
   339             if Typtab.defined types T then j
   340             else find_min (j + 1) Us (U --> T)
   341 
   342       val (Ts, T) = Term.strip_type (Term.type_of t)
   343     in find_min 0 (take i (rev Ts)) T end
   344 
   345   fun app u (t, T) =
   346     (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
   347 
   348   fun apply i t T ts =
   349     let
   350       val (ts1, ts2) = chop i ts
   351       val (_, U) = SMT_Utils.dest_funT i T
   352     in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
   353 in
   354 
   355 fun intro_explicit_application ctxt funcs ts =
   356   let
   357     val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
   358     val arities' = Termtab.map (minimize types) arities
   359 
   360     fun app_func t T ts =
   361       if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
   362       else apply (the (Termtab.lookup arities' t)) t T ts
   363 
   364     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   365 
   366     fun traverse Ts t =
   367       (case Term.strip_comb t of
   368         (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
   369           q $ Abs (x, T, in_trigger (T :: Ts) u)
   370       | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
   371           q $ Abs (x, T, in_trigger (T :: Ts) u)
   372       | (q as Const (@{const_name Let}, _), [u1 as Abs _, u2]) =>
   373           q $ traverse Ts u1 $ traverse Ts u2
   374       | (u as Const (c as (_, T)), ts) =>
   375           (case SMT_Builtin.dest_builtin ctxt c ts of
   376             SOME (_, _, us, mk) => mk (map (traverse Ts) us)
   377           | NONE => app_func u T (map (traverse Ts) ts))
   378       | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
   379       | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   380       | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   381       | (u, ts) => traverses Ts u ts)
   382     and in_trigger Ts ((c as @{const SMT.trigger}) $ p $ t) =
   383           c $ in_pats Ts p $ in_weight Ts t
   384       | in_trigger Ts t = in_weight Ts t
   385     and in_pats Ts ps =
   386       in_list @{typ "SMT.pattern list"}
   387         (in_list @{typ SMT.pattern} (in_pat Ts)) ps
   388     and in_pat Ts ((p as Const (@{const_name SMT.pat}, _)) $ t) =
   389           p $ traverse Ts t
   390       | in_pat Ts ((p as Const (@{const_name SMT.nopat}, _)) $ t) =
   391           p $ traverse Ts t
   392       | in_pat _ t = raise TERM ("bad pattern", [t])
   393     and in_weight Ts ((c as @{const SMT.weight}) $ w $ t) =
   394           c $ w $ traverse Ts t
   395       | in_weight Ts t = traverse Ts t 
   396     and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   397   in map (traverse []) ts end
   398 
   399 val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
   400 
   401 end
   402 
   403 
   404 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   405 
   406 local
   407   val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
   408     by (simp add: SMT.term_true_def SMT.term_false_def)}
   409 
   410   val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
   411 
   412   val fol_rules = [
   413     Let_def,
   414     mk_meta_eq @{thm SMT.term_true_def},
   415     mk_meta_eq @{thm SMT.term_false_def},
   416     @{lemma "P = True == P" by (rule eq_reflection) simp},
   417     @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   418 
   419   fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
   420 
   421   fun wrap_in_if t =
   422     @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
   423 
   424   fun is_builtin_conn_or_pred ctxt c ts =
   425     is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
   426     is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
   427 
   428   fun builtin b ctxt c ts =
   429     (case (Const c, ts) of
   430       (@{const HOL.eq (bool)}, [t, u]) =>
   431         if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
   432           SMT_Builtin.dest_builtin_eq ctxt t u
   433         else b ctxt c ts
   434     | _ => b ctxt c ts)
   435 in
   436 
   437 fun folify ctxt =
   438   let
   439     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   440 
   441     fun in_term t =
   442       (case Term.strip_comb t of
   443         (@{const True}, []) => @{const SMT.term_true}
   444       | (@{const False}, []) => @{const SMT.term_false}
   445       | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   446           u $ in_form t1 $ in_term t2 $ in_term t3
   447       | (Const (c as (n, _)), ts) =>
   448           if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
   449           else  if is_quant n then wrap_in_if (in_form t)
   450           else Term.list_comb (Const c, map in_term ts)
   451       | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
   452       | _ => t)
   453 
   454     and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
   455       | in_weight t = in_form t 
   456 
   457     and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
   458       | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
   459       | in_pat t = raise TERM ("bad pattern", [t])
   460 
   461     and in_pats ps =
   462       in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
   463 
   464     and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
   465           c $ in_pats p $ in_weight t
   466       | in_trigger t = in_weight t
   467 
   468     and in_form t =
   469       (case Term.strip_comb t of
   470         (q as Const (qn, _), [Abs (n, T, u)]) =>
   471           if is_quant qn then q $ Abs (n, T, in_trigger u)
   472           else as_term (in_term t)
   473       | (Const c, ts) =>
   474           (case SMT_Builtin.dest_builtin_conn ctxt c ts of
   475             SOME (_, _, us, mk) => mk (map in_form us)
   476           | NONE =>
   477               (case SMT_Builtin.dest_builtin_pred ctxt c ts of
   478                 SOME (_, _, us, mk) => mk (map in_term us)
   479               | NONE => as_term (in_term t)))
   480       | _ => as_term (in_term t))
   481   in
   482     map in_form #>
   483     cons (SMT_Utils.prop_of term_bool) #>
   484     pair (fol_rules, [term_bool], builtin)
   485   end
   486 
   487 end
   488 
   489 
   490 (* translation into intermediate format *)
   491 
   492 (** utility functions **)
   493 
   494 val quantifier = (fn
   495     @{const_name All} => SOME SForall
   496   | @{const_name Ex} => SOME SExists
   497   | _ => NONE)
   498 
   499 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   500       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   501   | group_quant _ Ts t = (Ts, t)
   502 
   503 fun dest_weight (@{const SMT.weight} $ w $ t) =
   504       (SOME (snd (HOLogic.dest_number w)), t)
   505   | dest_weight t = (NONE, t)
   506 
   507 fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
   508   | dest_pat (Const (@{const_name SMT.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 SMT.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 builtin 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 SMT_Builtin.dest_builtin_typ ctxt T of
   541             SOME n => pair n
   542           | NONE => add_typ T true)
   543 
   544     fun app n ts = SApp (n, ts)
   545 
   546     fun trans t =
   547       (case Term.strip_comb t of
   548         (Const (qn, _), [Abs (_, T, t1)]) =>
   549           (case dest_quant qn T t1 of
   550             SOME (q, Ts, ps, w, b) =>
   551               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   552               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
   553           | NONE => raise TERM ("unsupported quantifier", [t]))
   554       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   555           transT T ##>> trans t1 ##>> trans t2 #>>
   556           (fn ((U, u1), u2) => SLet (U, u1, u2))
   557       | (u as Const (c as (_, T)), ts) =>
   558           (case builtin ctxt c ts of
   559             SOME (n, _, us, _) => fold_map trans us #>> app n
   560           | NONE => transs u T ts)
   561       | (u as Free (_, T), ts) => transs u T ts
   562       | (Bound i, []) => pair (SVar i)
   563       | _ => raise TERM ("bad SMT term", [t]))
   564  
   565     and transs t T ts =
   566       let val (Us, U) = SMT_Utils.dest_funT (length ts) T
   567       in
   568         fold_map transT Us ##>> transT U #-> (fn Up =>
   569         add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   570       end
   571 
   572     val (us, trx') = fold_map trans ts trx
   573   in ((sign_of (header ts) dtyps trx', us), trx') end
   574 
   575 
   576 
   577 (* translation *)
   578 
   579 structure Configs = Generic_Data
   580 (
   581   type T = (Proof.context -> config) SMT_Utils.dict
   582   val empty = []
   583   val extend = I
   584   fun merge data = SMT_Utils.dict_merge fst data
   585 )
   586 
   587 fun add_config (cs, cfg) = Configs.map (SMT_Utils.dict_update (cs, cfg))
   588 
   589 fun get_config ctxt = 
   590   let val cs = SMT_Config.solver_class_of ctxt
   591   in
   592     (case SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
   593       SOME cfg => cfg ctxt
   594     | NONE => error ("SMT: no translation configuration found " ^
   595         "for solver class " ^ quote (SMT_Utils.string_of_class cs)))
   596   end
   597 
   598 fun translate ctxt comments ithms =
   599   let
   600     val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
   601 
   602     val with_datatypes =
   603       has_datatypes andalso Config.get ctxt SMT_Config.datatypes
   604 
   605     fun no_dtyps (tr_context, ctxt) ts =
   606       ((Termtab.empty, [], tr_context, ctxt), ts)
   607 
   608     val ts1 = map (Envir.beta_eta_contract o SMT_Utils.prop_of o snd) ithms
   609 
   610     val ((funcs, dtyps, tr_context, ctxt1), ts2) =
   611       ((make_tr_context prefixes, ctxt), ts1)
   612       |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
   613 
   614     val (ctxt2, ts3) =
   615       ts2
   616       |> eta_expand ctxt1 is_fol funcs
   617       |> lift_lambdas ctxt1
   618       ||> (op @)
   619       |-> (fn ctxt1' => pair ctxt1' o intro_explicit_application ctxt1 funcs)
   620 
   621     val ((rewrite_rules, extra_thms, builtin), ts4) =
   622       (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
   623 
   624     val rewrite_rules' = fun_app_eq :: rewrite_rules
   625   in
   626     (ts4, tr_context)
   627     |-> intermediate header dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
   628     |>> uncurry (serialize comments)
   629     ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
   630   end
   631 
   632 end