src/HOL/Tools/SMT/smt_translate.ML
author blanchet
Thu Jul 14 17:29:30 2011 +0200 (2011-07-14)
changeset 43829 fba9754b827e
parent 43554 9bece8cbb5be
child 43928 24d6e759753f
permissions -rw-r--r--
allow lambda-lifting without triggers
     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 -> bool -> 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 triggers Ts T lhs rhs =
   250     let
   251       val eq = HOLogic.eq_const T $ lhs $ rhs
   252       fun 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
   258       fold mk_all Ts (if triggers then @{const SMT.trigger} $ trigger () $ eq
   259         else eq)
   260     end
   261 
   262   fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
   263 
   264   fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
   265     | dest_abs Ts t = (Ts, t)
   266 
   267   fun replace_lambda triggers Us Ts t (cx as (defs, ctxt)) =
   268     let
   269       val t1 = mk_abs Us t
   270       val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
   271       fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
   272       val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
   273       val t2 = Term.subst_bounds (rs, t1)
   274       val Ts' = map (nth Ts) bs 
   275       val (_, t3) = dest_abs [] t2
   276       val t4 = mk_abs Ts' t2
   277 
   278       val T = Term.fastype_of1 (Us @ Ts, t)
   279       fun app f = Term.list_comb (f, map Bound (rev bs))
   280     in
   281       (case Termtab.lookup defs t4 of
   282         SOME (f, _) => (app f, cx)
   283       | NONE =>
   284           let
   285             val (n, ctxt') =
   286               yield_singleton Variable.variant_fixes Name.uu ctxt
   287             val (is, UTs) = split_list (map_index I (Us @ Ts'))
   288             val f = Free (n, rev UTs ---> T)
   289             val lhs = Term.list_comb (f, map Bound (rev is))
   290             val def = mk_def triggers UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
   291           in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
   292     end
   293 
   294   fun traverse triggers Ts t =
   295     (case t of
   296       (q as Const (@{const_name All}, _)) $ Abs a =>
   297         abs_traverse triggers Ts a #>> (fn a' => q $ Abs a')
   298     | (q as Const (@{const_name Ex}, _)) $ Abs a =>
   299         abs_traverse triggers Ts a #>> (fn a' => q $ Abs a')
   300     | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
   301         traverse triggers Ts u ##>> abs_traverse triggers Ts a #>>
   302         (fn (u', a') => l $ u' $ Abs a')
   303     | Abs _ =>
   304         let val (Us, u) = dest_abs [] t
   305         in traverse triggers (Us @ Ts) u #-> replace_lambda triggers Us Ts end
   306     | u1 $ u2 => traverse triggers Ts u1 ##>> traverse triggers Ts u2 #>> (op $)
   307     | _ => pair t)
   308 
   309   and abs_traverse triggers Ts (n, T, t) =
   310     traverse triggers (T::Ts) t #>> (fn t' => (n, T, t'))
   311 in
   312 
   313 fun lift_lambdas ctxt triggers ts =
   314   (Termtab.empty, ctxt)
   315   |> fold_map (traverse triggers []) ts
   316   |> (fn (us, (defs, ctxt')) =>
   317        (ctxt', (Termtab.fold (cons o snd o snd) defs [], us)))
   318 
   319 end
   320 
   321 
   322 (** introduce explicit applications **)
   323 
   324 local
   325   (*
   326     Make application explicit for functions with varying number of arguments.
   327   *)
   328 
   329   fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
   330   fun add_type T = apsnd (Typtab.update (T, ()))
   331 
   332   fun min_arities t =
   333     (case Term.strip_comb t of
   334       (u as Const _, ts) => add u (length ts) #> fold min_arities ts
   335     | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
   336     | (Abs (_, T, u), ts) => add_type T #> min_arities u #> fold min_arities ts
   337     | (_, ts) => fold min_arities ts)
   338 
   339   fun minimize types t i =
   340     let
   341       fun find_min j [] _ = j
   342         | find_min j (U :: Us) T =
   343             if Typtab.defined types T then j
   344             else find_min (j + 1) Us (U --> T)
   345 
   346       val (Ts, T) = Term.strip_type (Term.type_of t)
   347     in find_min 0 (take i (rev Ts)) T end
   348 
   349   fun app u (t, T) =
   350     (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
   351 
   352   fun apply i t T ts =
   353     let
   354       val (ts1, ts2) = chop i ts
   355       val (_, U) = SMT_Utils.dest_funT i T
   356     in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
   357 in
   358 
   359 fun intro_explicit_application ctxt funcs ts =
   360   let
   361     val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
   362     val arities' = Termtab.map (minimize types) arities
   363 
   364     fun app_func t T ts =
   365       if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
   366       else apply (the (Termtab.lookup arities' t)) t T ts
   367 
   368     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   369 
   370     fun traverse Ts t =
   371       (case Term.strip_comb t of
   372         (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
   373           q $ Abs (x, T, in_trigger (T :: Ts) u)
   374       | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
   375           q $ Abs (x, T, in_trigger (T :: Ts) u)
   376       | (q as Const (@{const_name Let}, _), [u1 as Abs _, u2]) =>
   377           q $ traverse Ts u1 $ traverse Ts u2
   378       | (u as Const (c as (_, T)), ts) =>
   379           (case SMT_Builtin.dest_builtin ctxt c ts of
   380             SOME (_, _, us, mk) => mk (map (traverse Ts) us)
   381           | NONE => app_func u T (map (traverse Ts) ts))
   382       | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
   383       | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   384       | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   385       | (u, ts) => traverses Ts u ts)
   386     and in_trigger Ts ((c as @{const SMT.trigger}) $ p $ t) =
   387           c $ in_pats Ts p $ in_weight Ts t
   388       | in_trigger Ts t = in_weight Ts t
   389     and in_pats Ts ps =
   390       in_list @{typ "SMT.pattern list"}
   391         (in_list @{typ SMT.pattern} (in_pat Ts)) ps
   392     and in_pat Ts ((p as Const (@{const_name SMT.pat}, _)) $ t) =
   393           p $ traverse Ts t
   394       | in_pat Ts ((p as Const (@{const_name SMT.nopat}, _)) $ t) =
   395           p $ traverse Ts t
   396       | in_pat _ t = raise TERM ("bad pattern", [t])
   397     and in_weight Ts ((c as @{const SMT.weight}) $ w $ t) =
   398           c $ w $ traverse Ts t
   399       | in_weight Ts t = traverse Ts t 
   400     and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   401   in map (traverse []) ts end
   402 
   403 val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
   404 
   405 end
   406 
   407 
   408 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   409 
   410 local
   411   val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
   412     by (simp add: SMT.term_true_def SMT.term_false_def)}
   413 
   414   val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
   415 
   416   val fol_rules = [
   417     Let_def,
   418     mk_meta_eq @{thm SMT.term_true_def},
   419     mk_meta_eq @{thm SMT.term_false_def},
   420     @{lemma "P = True == P" by (rule eq_reflection) simp},
   421     @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   422 
   423   fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
   424 
   425   fun wrap_in_if t =
   426     @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
   427 
   428   fun is_builtin_conn_or_pred ctxt c ts =
   429     is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
   430     is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
   431 
   432   fun builtin b ctxt c ts =
   433     (case (Const c, ts) of
   434       (@{const HOL.eq (bool)}, [t, u]) =>
   435         if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
   436           SMT_Builtin.dest_builtin_eq ctxt t u
   437         else b ctxt c ts
   438     | _ => b ctxt c ts)
   439 in
   440 
   441 fun folify ctxt =
   442   let
   443     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   444 
   445     fun in_term t =
   446       (case Term.strip_comb t of
   447         (@{const True}, []) => @{const SMT.term_true}
   448       | (@{const False}, []) => @{const SMT.term_false}
   449       | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   450           u $ in_form t1 $ in_term t2 $ in_term t3
   451       | (Const (c as (n, _)), ts) =>
   452           if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
   453           else  if is_quant n then wrap_in_if (in_form t)
   454           else Term.list_comb (Const c, map in_term ts)
   455       | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
   456       | _ => t)
   457 
   458     and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
   459       | in_weight t = in_form t 
   460 
   461     and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
   462       | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
   463       | in_pat t = raise TERM ("bad pattern", [t])
   464 
   465     and in_pats ps =
   466       in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
   467 
   468     and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
   469           c $ in_pats p $ in_weight t
   470       | in_trigger t = in_weight t
   471 
   472     and in_form t =
   473       (case Term.strip_comb t of
   474         (q as Const (qn, _), [Abs (n, T, u)]) =>
   475           if is_quant qn then q $ Abs (n, T, in_trigger u)
   476           else as_term (in_term t)
   477       | (Const c, ts) =>
   478           (case SMT_Builtin.dest_builtin_conn ctxt c ts of
   479             SOME (_, _, us, mk) => mk (map in_form us)
   480           | NONE =>
   481               (case SMT_Builtin.dest_builtin_pred ctxt c ts of
   482                 SOME (_, _, us, mk) => mk (map in_term us)
   483               | NONE => as_term (in_term t)))
   484       | _ => as_term (in_term t))
   485   in
   486     map in_form #>
   487     cons (SMT_Utils.prop_of term_bool) #>
   488     pair (fol_rules, [term_bool], builtin)
   489   end
   490 
   491 end
   492 
   493 
   494 (* translation into intermediate format *)
   495 
   496 (** utility functions **)
   497 
   498 val quantifier = (fn
   499     @{const_name All} => SOME SForall
   500   | @{const_name Ex} => SOME SExists
   501   | _ => NONE)
   502 
   503 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   504       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   505   | group_quant _ Ts t = (Ts, t)
   506 
   507 fun dest_weight (@{const SMT.weight} $ w $ t) =
   508       (SOME (snd (HOLogic.dest_number w)), t)
   509   | dest_weight t = (NONE, t)
   510 
   511 fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
   512   | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
   513   | dest_pat t = raise TERM ("bad pattern", [t])
   514 
   515 fun dest_pats [] = I
   516   | dest_pats ts =
   517       (case map dest_pat ts |> split_list ||> distinct (op =) of
   518         (ps, [true]) => cons (SPat ps)
   519       | (ps, [false]) => cons (SNoPat ps)
   520       | _ => raise TERM ("bad multi-pattern", ts))
   521 
   522 fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
   523       (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
   524   | dest_trigger t = ([], t)
   525 
   526 fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   527   let
   528     val (Ts, u) = group_quant qn [T] t
   529     val (ps, p) = dest_trigger u
   530     val (w, b) = dest_weight p
   531   in (q, rev Ts, ps, w, b) end)
   532 
   533 fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   534   | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   535 
   536 
   537 (** translation from Isabelle terms into SMT intermediate terms **)
   538 
   539 fun intermediate header dtyps builtin ctxt ts trx =
   540   let
   541     fun transT (T as TFree _) = add_typ T true
   542       | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   543       | transT (T as Type _) =
   544           (case SMT_Builtin.dest_builtin_typ ctxt T of
   545             SOME n => pair n
   546           | NONE => add_typ T true)
   547 
   548     fun app n ts = SApp (n, ts)
   549 
   550     fun trans t =
   551       (case Term.strip_comb t of
   552         (Const (qn, _), [Abs (_, T, t1)]) =>
   553           (case dest_quant qn T t1 of
   554             SOME (q, Ts, ps, w, b) =>
   555               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   556               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
   557           | NONE => raise TERM ("unsupported quantifier", [t]))
   558       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   559           transT T ##>> trans t1 ##>> trans t2 #>>
   560           (fn ((U, u1), u2) => SLet (U, u1, u2))
   561       | (u as Const (c as (_, T)), ts) =>
   562           (case builtin ctxt c ts of
   563             SOME (n, _, us, _) => fold_map trans us #>> app n
   564           | NONE => transs u T ts)
   565       | (u as Free (_, T), ts) => transs u T ts
   566       | (Bound i, []) => pair (SVar i)
   567       | _ => raise TERM ("bad SMT term", [t]))
   568  
   569     and transs t T ts =
   570       let val (Us, U) = SMT_Utils.dest_funT (length ts) T
   571       in
   572         fold_map transT Us ##>> transT U #-> (fn Up =>
   573         add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   574       end
   575 
   576     val (us, trx') = fold_map trans ts trx
   577   in ((sign_of (header ts) dtyps trx', us), trx') end
   578 
   579 
   580 
   581 (* translation *)
   582 
   583 structure Configs = Generic_Data
   584 (
   585   type T = (Proof.context -> config) SMT_Utils.dict
   586   val empty = []
   587   val extend = I
   588   fun merge data = SMT_Utils.dict_merge fst data
   589 )
   590 
   591 fun add_config (cs, cfg) = Configs.map (SMT_Utils.dict_update (cs, cfg))
   592 
   593 fun get_config ctxt = 
   594   let val cs = SMT_Config.solver_class_of ctxt
   595   in
   596     (case SMT_Utils.dict_get (Configs.get (Context.Proof ctxt)) cs of
   597       SOME cfg => cfg ctxt
   598     | NONE => error ("SMT: no translation configuration found " ^
   599         "for solver class " ^ quote (SMT_Utils.string_of_class cs)))
   600   end
   601 
   602 fun translate ctxt comments ithms =
   603   let
   604     val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
   605 
   606     val with_datatypes =
   607       has_datatypes andalso Config.get ctxt SMT_Config.datatypes
   608 
   609     fun no_dtyps (tr_context, ctxt) ts =
   610       ((Termtab.empty, [], tr_context, ctxt), ts)
   611 
   612     val ts1 = map (Envir.beta_eta_contract o SMT_Utils.prop_of o snd) ithms
   613 
   614     val ((funcs, dtyps, tr_context, ctxt1), ts2) =
   615       ((make_tr_context prefixes, ctxt), ts1)
   616       |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
   617 
   618     val (ctxt2, ts3) =
   619       ts2
   620       |> eta_expand ctxt1 is_fol funcs
   621       |> lift_lambdas ctxt1 true
   622       ||> (op @)
   623       |-> (fn ctxt1' => pair ctxt1' o intro_explicit_application ctxt1 funcs)
   624 
   625     val ((rewrite_rules, extra_thms, builtin), ts4) =
   626       (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
   627 
   628     val rewrite_rules' = fun_app_eq :: rewrite_rules
   629   in
   630     (ts4, tr_context)
   631     |-> intermediate header dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
   632     |>> uncurry (serialize comments)
   633     ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
   634   end
   635 
   636 end