src/HOL/Tools/SMT/smt_translate.ML
author blanchet
Wed Sep 17 17:32:27 2014 +0200 (2014-09-17)
changeset 58361 7f2b3b6f6ad1
parent 58360 dee1fd1cc631
child 58429 0b94858325a5
permissions -rw-r--r--
added codatatype support for CVC4
     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 * sterm
    17 
    18   (*translation configuration*)
    19   type sign = {
    20     logic: string,
    21     sorts: string list,
    22     lfp_dtyps: (string * (string * (string * string) list) list) list list,
    23     gfp_dtyps: (string * (string * (string * string) list) list) list list,
    24     funcs: (string * (string list * string)) list }
    25   type config = {
    26     logic: term list -> string,
    27     fp_kinds: BNF_Util.fp_kind list,
    28     serialize: (string * string) list -> string list -> sign -> sterm list -> string }
    29   type replay_data = {
    30     context: Proof.context,
    31     typs: typ Symtab.table,
    32     terms: term Symtab.table,
    33     ll_defs: term list,
    34     rewrite_rules: thm list,
    35     assms: (int * thm) list }
    36 
    37   (*translation*)
    38   val add_config: SMT_Util.class * (Proof.context -> config) -> Context.generic -> Context.generic
    39   val translate: Proof.context -> (string * string) list -> string list -> (int * thm) list ->
    40     string * replay_data
    41 end;
    42 
    43 structure SMT_Translate: SMT_TRANSLATE =
    44 struct
    45 
    46 
    47 (* intermediate term structure *)
    48 
    49 datatype squant = SForall | SExists
    50 
    51 datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
    52 
    53 datatype sterm =
    54   SVar of int |
    55   SApp of string * sterm list |
    56   SLet of string * sterm * sterm |
    57   SQua of squant * string list * sterm spattern list * sterm
    58 
    59 
    60 (* translation configuration *)
    61 
    62 type sign = {
    63   logic: string,
    64   sorts: string list,
    65   lfp_dtyps: (string * (string * (string * string) list) list) list list,
    66   gfp_dtyps: (string * (string * (string * string) list) list) list list,
    67   funcs: (string * (string list * string)) list }
    68 
    69 type config = {
    70   logic: term list -> string,
    71   fp_kinds: BNF_Util.fp_kind list,
    72   serialize: (string * string) list -> string list -> sign -> sterm list -> string }
    73 
    74 type replay_data = {
    75   context: Proof.context,
    76   typs: typ Symtab.table,
    77   terms: term Symtab.table,
    78   ll_defs: term list,
    79   rewrite_rules: thm list,
    80   assms: (int * thm) list }
    81 
    82 
    83 (* translation context *)
    84 
    85 fun add_components_of_typ (Type (s, Ts)) =
    86     cons (Long_Name.base_name s) #> fold_rev add_components_of_typ Ts
    87   | add_components_of_typ (TFree (s, _)) = cons (perhaps (try (unprefix "'")) s)
    88   | add_components_of_typ _ = I;
    89 
    90 fun suggested_name_of_typ T = space_implode "_" (add_components_of_typ T []);
    91 
    92 fun suggested_name_of_term (Const (s, _)) = Long_Name.base_name s
    93   | suggested_name_of_term (Free (s, _)) = s
    94   | suggested_name_of_term _ = Name.uu
    95 
    96 val empty_tr_context = (Name.context, Typtab.empty, Termtab.empty)
    97 val safe_suffix = "$"
    98 
    99 fun add_typ T proper (cx as (names, typs, terms)) =
   100   (case Typtab.lookup typs T of
   101     SOME (name, _) => (name, cx)
   102   | NONE =>
   103       let
   104         val sugg = Name.desymbolize (SOME true) (suggested_name_of_typ T) ^ safe_suffix
   105         val (name, names') = Name.variant sugg names
   106         val typs' = Typtab.update (T, (name, proper)) typs
   107       in (name, (names', typs', terms)) end)
   108 
   109 fun add_fun t sort (cx as (names, typs, terms)) =
   110   (case Termtab.lookup terms t of
   111     SOME (name, _) => (name, cx)
   112   | NONE =>
   113       let
   114         val sugg = Name.desymbolize (SOME false) (suggested_name_of_term t) ^ safe_suffix
   115         val (name, names') = Name.variant sugg names
   116         val terms' = Termtab.update (t, (name, sort)) terms
   117       in (name, (names', typs, terms')) end)
   118 
   119 fun sign_of logic lfp_dtyps gfp_dtyps (_, typs, terms) = {
   120   logic = logic,
   121   sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
   122   lfp_dtyps = lfp_dtyps,
   123   gfp_dtyps = gfp_dtyps,
   124   funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
   125 
   126 fun replay_data_of ctxt ll_defs rules assms (_, typs, terms) =
   127   let
   128     fun add_typ (T, (n, _)) = Symtab.update (n, T)
   129     val typs' = Typtab.fold add_typ typs Symtab.empty
   130 
   131     fun add_fun (t, (n, _)) = Symtab.update (n, t)
   132     val terms' = Termtab.fold add_fun terms Symtab.empty
   133   in
   134     {context = ctxt, typs = typs', terms = terms', ll_defs = ll_defs, rewrite_rules = rules,
   135      assms = assms}
   136   end
   137 
   138 
   139 (* preprocessing *)
   140 
   141 (** (co)datatype declarations **)
   142 
   143 fun collect_co_datatypes fp_kinds (tr_context, ctxt) ts =
   144   let
   145     val (lfp_declss, ctxt') =
   146       ([], ctxt)
   147       |> member (op =) fp_kinds BNF_Util.Least_FP
   148         ? fold (Term.fold_types (SMT_Datatypes.add_decls BNF_Util.Least_FP)) ts
   149     val (gfp_declss, ctxt'') =
   150       ([], ctxt')
   151       |> member (op =) fp_kinds BNF_Util.Greatest_FP
   152         ? fold (Term.fold_types (SMT_Datatypes.add_decls BNF_Util.Greatest_FP)) ts
   153 
   154     val fp_declsss = [lfp_declss, gfp_declss]
   155 
   156     fun is_decl_typ T = exists (exists (exists (equal T o fst))) fp_declsss
   157 
   158     fun add_typ' T proper =
   159       (case SMT_Builtin.dest_builtin_typ ctxt'' T of
   160         SOME n => pair n
   161       | NONE => add_typ T proper)
   162 
   163     fun tr_select sel =
   164       let val T = Term.range_type (Term.fastype_of sel)
   165       in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
   166     fun tr_constr (constr, selects) =
   167       add_fun constr NONE ##>> fold_map tr_select selects
   168     fun tr_typ (T, cases) = add_typ' T false ##>> fold_map tr_constr cases
   169 
   170     val (lfp_declss', tr_context') = fold_map (fold_map tr_typ) lfp_declss tr_context
   171     val (gfp_declss', tr_context'') = fold_map (fold_map tr_typ) gfp_declss tr_context'
   172 
   173     fun add (constr, selects) =
   174       Termtab.update (constr, length selects) #>
   175       fold (Termtab.update o rpair 1) selects
   176 
   177     val funcs = fold (fold (fold (fold add o snd))) fp_declsss Termtab.empty
   178   in ((funcs, lfp_declss', gfp_declss', tr_context'', ctxt''), ts) end
   179     (* FIXME: also return necessary (co)datatype theorems *)
   180 
   181 
   182 (** eta-expand quantifiers, let expressions and built-ins *)
   183 
   184 local
   185   fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
   186 
   187   fun exp f T = eta f (Term.domain_type (Term.domain_type T))
   188 
   189   fun exp2 T q =
   190     let val U = Term.domain_type T
   191     in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
   192 
   193   fun expf k i T t =
   194     let val Ts = drop i (fst (SMT_Util.dest_funT k T))
   195     in
   196       Term.incr_boundvars (length Ts) t
   197       |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
   198       |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
   199     end
   200 in
   201 
   202 fun eta_expand ctxt funcs =
   203   let
   204     fun exp_func t T ts =
   205       (case Termtab.lookup funcs t of
   206         SOME k => Term.list_comb (t, ts) |> k <> length ts ? expf k (length ts) T
   207       | NONE => Term.list_comb (t, ts))
   208 
   209     fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
   210       | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
   211       | expand (q as Const (@{const_name All}, T)) = exp2 T q
   212       | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
   213       | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
   214       | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
   215       | expand (Const (@{const_name Let}, _) $ t $ u) = expand (Term.betapply (u, t))
   216       | expand (Const (@{const_name Let}, T) $ t) =
   217           let val U = Term.domain_type (Term.range_type T)
   218           in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
   219       | expand (Const (@{const_name Let}, T)) =
   220           let val U = Term.domain_type (Term.range_type T)
   221           in Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U, Bound 0 $ Bound 1)) end
   222       | expand t =
   223           (case Term.strip_comb t of
   224             (u as Const (c as (_, T)), ts) =>
   225               (case SMT_Builtin.dest_builtin ctxt c ts of
   226                 SOME (_, k, us, mk) =>
   227                   if k = length us then mk (map expand us)
   228                   else if k < length us then chop k (map expand us) |>> mk |> Term.list_comb
   229                   else expf k (length ts) T (mk (map expand us))
   230               | NONE => exp_func u T (map expand ts))
   231           | (u as Free (_, T), ts) => exp_func u T (map expand ts)
   232           | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
   233           | (u, ts) => Term.list_comb (u, map expand ts))
   234 
   235     and abs_expand (n, T, t) = Abs (n, T, expand t)
   236 
   237   in map expand end
   238 
   239 end
   240 
   241 
   242 (** introduce explicit applications **)
   243 
   244 local
   245   (*
   246     Make application explicit for functions with varying number of arguments.
   247   *)
   248 
   249   fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
   250   fun add_type T = apsnd (Typtab.update (T, ()))
   251 
   252   fun min_arities t =
   253     (case Term.strip_comb t of
   254       (u as Const _, ts) => add u (length ts) #> fold min_arities ts
   255     | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
   256     | (Abs (_, T, u), ts) => (can dest_funT T ? add_type T) #> min_arities u #> fold min_arities ts
   257     | (_, ts) => fold min_arities ts)
   258 
   259   fun minimize types t i =
   260     let
   261       fun find_min j [] _ = j
   262         | find_min j (U :: Us) T =
   263             if Typtab.defined types T then j else find_min (j + 1) Us (U --> T)
   264 
   265       val (Ts, T) = Term.strip_type (Term.type_of t)
   266     in find_min 0 (take i (rev Ts)) T end
   267 
   268   fun app u (t, T) = (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
   269 
   270   fun apply i t T ts =
   271     let
   272       val (ts1, ts2) = chop i ts
   273       val (_, U) = SMT_Util.dest_funT i T
   274     in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
   275 in
   276 
   277 fun intro_explicit_application ctxt funcs ts =
   278   let
   279     val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
   280     val arities' = Termtab.map (minimize types) arities (* FIXME: highly suspicious *)
   281 
   282     fun app_func t T ts =
   283       if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
   284       else apply (the (Termtab.lookup arities' t)) t T ts
   285 
   286     fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
   287 
   288     fun traverse Ts t =
   289       (case Term.strip_comb t of
   290         (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
   291           q $ Abs (x, T, in_trigger (T :: Ts) u)
   292       | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
   293           q $ Abs (x, T, in_trigger (T :: Ts) u)
   294       | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
   295           q $ traverse Ts u1 $ traverse Ts u2
   296       | (u as Const (c as (_, T)), ts) =>
   297           (case SMT_Builtin.dest_builtin ctxt c ts of
   298             SOME (_, k, us, mk) =>
   299               let
   300                 val (ts1, ts2) = chop k (map (traverse Ts) us)
   301                 val U = Term.strip_type T |>> snd o chop k |> (op --->)
   302               in apply 0 (mk ts1) U ts2 end
   303           | NONE => app_func u T (map (traverse Ts) ts))
   304       | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
   305       | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   306       | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   307       | (u, ts) => traverses Ts u ts)
   308     and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
   309       | in_trigger Ts t = traverse Ts t
   310     and in_pats Ts ps =
   311       in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
   312     and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
   313       | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
   314       | in_pat _ t = raise TERM ("bad pattern", [t])
   315     and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   316   in map (traverse []) ts end
   317 
   318 val fun_app_eq = mk_meta_eq @{thm fun_app_def}
   319 
   320 end
   321 
   322 
   323 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   324 
   325 local
   326   val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
   327 
   328   val fol_rules = [
   329     Let_def,
   330     @{lemma "P = True == P" by (rule eq_reflection) simp},
   331     @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   332 
   333   exception BAD_PATTERN of unit
   334 
   335   fun wrap_in_if pat t =
   336     if pat then raise BAD_PATTERN () else @{const If (bool)} $ t $ @{const True} $ @{const False}
   337 
   338   fun is_builtin_conn_or_pred ctxt c ts =
   339     is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
   340     is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
   341 in
   342 
   343 fun folify ctxt =
   344   let
   345     fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
   346 
   347     fun in_term pat t =
   348       (case Term.strip_comb t of
   349         (@{const True}, []) => t
   350       | (@{const False}, []) => t
   351       | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   352           if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
   353       | (Const (c as (n, _)), ts) =>
   354           if is_builtin_conn_or_pred ctxt c ts then wrap_in_if pat (in_form t)
   355           else if is_quant n then wrap_in_if pat (in_form t)
   356           else Term.list_comb (Const c, map (in_term pat) ts)
   357       | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
   358       | _ => t)
   359 
   360     and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
   361           p $ in_term true t
   362       | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
   363           p $ in_term true t
   364       | in_pat t = raise TERM ("bad pattern", [t])
   365 
   366     and in_pats ps =
   367       in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
   368 
   369     and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
   370       | in_trigger t = in_form t
   371 
   372     and in_form t =
   373       (case Term.strip_comb t of
   374         (q as Const (qn, _), [Abs (n, T, u)]) =>
   375           if is_quant qn then q $ Abs (n, T, in_trigger u)
   376           else in_term false t
   377       | (Const c, ts) =>
   378           (case SMT_Builtin.dest_builtin_conn ctxt c ts of
   379             SOME (_, _, us, mk) => mk (map in_form us)
   380           | NONE =>
   381               (case SMT_Builtin.dest_builtin_pred ctxt c ts of
   382                 SOME (_, _, us, mk) => mk (map (in_term false) us)
   383               | NONE => in_term false t))
   384       | _ => in_term false t)
   385   in
   386     map in_form #>
   387     pair (fol_rules, I)
   388   end
   389 
   390 end
   391 
   392 
   393 (* translation into intermediate format *)
   394 
   395 (** utility functions **)
   396 
   397 val quantifier = (fn
   398     @{const_name All} => SOME SForall
   399   | @{const_name Ex} => SOME SExists
   400   | _ => NONE)
   401 
   402 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   403       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   404   | group_quant _ Ts t = (Ts, t)
   405 
   406 fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
   407   | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
   408   | dest_pat t = raise TERM ("bad pattern", [t])
   409 
   410 fun dest_pats [] = I
   411   | dest_pats ts =
   412       (case map dest_pat ts |> split_list ||> distinct (op =) of
   413         (ps, [true]) => cons (SPat ps)
   414       | (ps, [false]) => cons (SNoPat ps)
   415       | _ => raise TERM ("bad multi-pattern", ts))
   416 
   417 fun dest_trigger (@{const trigger} $ tl $ t) =
   418       (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
   419   | dest_trigger t = ([], t)
   420 
   421 fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   422   let
   423     val (Ts, u) = group_quant qn [T] t
   424     val (ps, p) = dest_trigger u
   425   in (q, rev Ts, ps, p) end)
   426 
   427 fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   428   | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   429 
   430 
   431 (** translation from Isabelle terms into SMT intermediate terms **)
   432 
   433 fun intermediate logic lfp_dtyps gfp_dtyps builtin ctxt ts trx =
   434   let
   435     fun transT (T as TFree _) = add_typ T true
   436       | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   437       | transT (T as Type _) =
   438           (case SMT_Builtin.dest_builtin_typ ctxt T of
   439             SOME n => pair n
   440           | NONE => add_typ T true)
   441 
   442     fun app n ts = SApp (n, ts)
   443 
   444     fun trans t =
   445       (case Term.strip_comb t of
   446         (Const (qn, _), [Abs (_, T, t1)]) =>
   447           (case dest_quant qn T t1 of
   448             SOME (q, Ts, ps, b) =>
   449               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   450               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
   451           | NONE => raise TERM ("unsupported quantifier", [t]))
   452       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   453           transT T ##>> trans t1 ##>> trans t2 #>> (fn ((U, u1), u2) => SLet (U, u1, u2))
   454       | (u as Const (c as (_, T)), ts) =>
   455           (case builtin ctxt c ts of
   456             SOME (n, _, us, _) => fold_map trans us #>> app n
   457           | NONE => transs u T ts)
   458       | (u as Free (_, T), ts) => transs u T ts
   459       | (Bound i, []) => pair (SVar i)
   460       | _ => raise TERM ("bad SMT term", [t]))
   461 
   462     and transs t T ts =
   463       let val (Us, U) = SMT_Util.dest_funT (length ts) T
   464       in
   465         fold_map transT Us ##>> transT U #-> (fn Up =>
   466           add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   467       end
   468 
   469     val (us, trx') = fold_map trans ts trx
   470   in ((sign_of (logic ts) lfp_dtyps gfp_dtyps trx', us), trx') end
   471 
   472 
   473 (* translation *)
   474 
   475 structure Configs = Generic_Data
   476 (
   477   type T = (Proof.context -> config) SMT_Util.dict
   478   val empty = []
   479   val extend = I
   480   fun merge data = SMT_Util.dict_merge fst data
   481 )
   482 
   483 fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
   484 
   485 fun get_config ctxt =
   486   let val cs = SMT_Config.solver_class_of ctxt
   487   in
   488     (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
   489       SOME cfg => cfg ctxt
   490     | NONE => error ("SMT: no translation configuration found " ^
   491         "for solver class " ^ quote (SMT_Util.string_of_class cs)))
   492   end
   493 
   494 fun translate ctxt smt_options comments ithms =
   495   let
   496     val {logic, fp_kinds, serialize} = get_config ctxt
   497 
   498     fun no_dtyps (tr_context, ctxt) ts =
   499       ((Termtab.empty, [], [], tr_context, ctxt), ts)
   500 
   501     val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
   502 
   503     val ((funcs, lfp_dtyps, gfp_dtyps, tr_context, ctxt1), ts2) =
   504       ((empty_tr_context, ctxt), ts1)
   505       |-> (if null fp_kinds then no_dtyps else collect_co_datatypes fp_kinds)
   506 
   507     fun is_binder (Const (@{const_name Let}, _) $ _) = true
   508       | is_binder t = Lambda_Lifting.is_quantifier t
   509 
   510     fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
   511           q $ Abs (n, T, mk_trigger t)
   512       | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
   513           Term.domain_type T --> @{typ pattern}
   514           |> (fn T => Const (@{const_name pat}, T) $ lhs)
   515           |> SMT_Util.mk_symb_list @{typ pattern} o single
   516           |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
   517           |> (fn t => @{const trigger} $ t $ eq)
   518       | mk_trigger t = t
   519 
   520     val (ctxt2, (ts3, ll_defs)) =
   521       ts2
   522       |> eta_expand ctxt1 funcs
   523       |> rpair ctxt1
   524       |-> Lambda_Lifting.lift_lambdas NONE is_binder
   525       |-> (fn (ts', ll_defs) => fn ctxt' =>
   526           (ctxt', (intro_explicit_application ctxt' funcs (map mk_trigger ll_defs @ ts'), ll_defs)))
   527 
   528     val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
   529       |>> apfst (cons fun_app_eq)
   530   in
   531     (ts4, tr_context)
   532     |-> intermediate logic lfp_dtyps gfp_dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
   533     |>> uncurry (serialize smt_options comments)
   534     ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
   535   end
   536 
   537 end;