src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Sun Dec 19 18:54:29 2010 +0100 (2010-12-19)
changeset 41281 679118e35378
parent 41250 41f86829e22f
child 41328 6792a5c92a58
permissions -rw-r--r--
removed odd decoration of built-in symbols as Vars (instead provide built-in desctructor functions along with their inverse functions);
removed odd retyping during folify (instead, keep all terms well-typed)
     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 (_, _, typs, _, _, terms) =
   124   let
   125     fun add_typ (T, (n, _)) = Symtab.update (n, T)
   126     val typs' = Typtab.fold add_typ typs Symtab.empty
   127 
   128     fun add_fun (t, (n, _)) = Symtab.update (n, 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 (** FIXME **)
   141 
   142 local
   143   (*
   144     force eta-expansion for constructors and selectors,
   145     add missing datatype selectors via hypothetical definitions,
   146     also return necessary datatype and record theorems
   147   *)
   148 in
   149 
   150 fun collect_datatypes_and_records (tr_context, ctxt) ts =
   151   (([], tr_context, ctxt), ts)
   152 
   153 end
   154 
   155 
   156 (** eta-expand quantifiers, let expressions and built-ins *)
   157 
   158 local
   159   fun eta T t = Abs (Name.uu, T, Term.incr_boundvars 1 t $ Bound 0)
   160 
   161   fun exp T = eta (Term.domain_type (Term.domain_type T))
   162 
   163   fun exp2 T q =
   164     let val U = Term.domain_type T
   165     in Abs (Name.uu, U, q $ eta (Term.domain_type U) (Bound 0)) end
   166 
   167   fun exp2' T l =
   168     let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
   169     in Abs (Name.uu, U1, eta U2 (l $ Bound 0)) end
   170 
   171   fun expf k i T t =
   172     let val Ts = drop i (fst (U.dest_funT k T))
   173     in
   174       Term.incr_boundvars (length Ts) t
   175       |> fold_index (fn (i, _) => fn u => u $ Bound i) Ts
   176       |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
   177     end
   178 in
   179 
   180 fun eta_expand ctxt =
   181   let
   182     fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
   183       | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp T t
   184       | expand (q as Const (@{const_name All}, T)) = exp2 T q
   185       | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
   186       | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp T t
   187       | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
   188       | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
   189           l $ expand t $ abs_expand a
   190       | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
   191           l $ expand t $ exp (Term.range_type T) u
   192       | expand ((l as Const (@{const_name Let}, T)) $ t) =
   193           exp2 T (l $ expand t)
   194       | expand (l as Const (@{const_name Let}, T)) = exp2' T l
   195       | expand t =
   196           (case Term.strip_comb t of
   197             (u as Const (c as (_, T)), ts) =>
   198               (case B.dest_builtin ctxt c ts of
   199                 SOME (_, k, us, mk) =>
   200                   if k = length us then mk (map expand us)
   201                   else expf k (length ts) T (mk (map expand us))
   202               | NONE => Term.list_comb (u, map expand ts))
   203           | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
   204           | (u, ts) => Term.list_comb (u, map expand ts))
   205 
   206     and abs_expand (n, T, t) = Abs (n, T, expand t)
   207   
   208   in map expand end
   209 
   210 end
   211 
   212 
   213 (** lambda-lifting **)
   214 
   215 local
   216   fun mk_def Ts T lhs rhs =
   217     let
   218       val eq = HOLogic.eq_const T $ lhs $ rhs
   219       val trigger =
   220         [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
   221         |> map (HOLogic.mk_list @{typ SMT.pattern})
   222         |> HOLogic.mk_list @{typ "SMT.pattern list"}
   223       fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
   224     in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
   225 
   226   fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
   227 
   228   fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
   229     | dest_abs Ts t = (Ts, t)
   230 
   231   fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
   232     let
   233       val t1 = mk_abs Us t
   234       val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
   235       fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
   236       val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
   237       val t2 = Term.subst_bounds (rs, t1)
   238       val Ts' = map (nth Ts) bs 
   239       val (_, t3) = dest_abs [] t2
   240       val t4 = mk_abs Ts' t2
   241 
   242       val T = Term.fastype_of1 (Us @ Ts, t)
   243       fun app f = Term.list_comb (f, map Bound (rev bs))
   244     in
   245       (case Termtab.lookup defs t4 of
   246         SOME (f, _) => (app f, cx)
   247       | NONE =>
   248           let
   249             val (n, ctxt') =
   250               yield_singleton Variable.variant_fixes Name.uu ctxt
   251             val (is, UTs) = split_list (map_index I (Us @ Ts'))
   252             val f = Free (n, rev UTs ---> T)
   253             val lhs = Term.list_comb (f, map Bound (rev is))
   254             val def = mk_def UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
   255           in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
   256     end
   257 
   258   fun traverse Ts t =
   259     (case t of
   260       (q as Const (@{const_name All}, _)) $ Abs a =>
   261         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   262     | (q as Const (@{const_name Ex}, _)) $ Abs a =>
   263         abs_traverse Ts a #>> (fn a' => q $ Abs a')
   264     | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
   265         traverse Ts u ##>> abs_traverse Ts a #>>
   266         (fn (u', a') => l $ u' $ Abs a')
   267     | Abs _ =>
   268         let val (Us, u) = dest_abs [] t
   269         in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
   270     | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
   271     | _ => pair t)
   272 
   273   and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
   274 in
   275 
   276 fun lift_lambdas ctxt ts =
   277   (Termtab.empty, ctxt)
   278   |> fold_map (traverse []) ts
   279   |> (fn (us, (defs, ctxt')) =>
   280        (ctxt', Termtab.fold (cons o snd o snd) defs us))
   281 
   282 end
   283 
   284 
   285 (** introduce explicit applications **)
   286 
   287 local
   288   (*
   289     Make application explicit for functions with varying number of arguments.
   290   *)
   291 
   292   fun add t i = Termtab.map_default (t, i) (Integer.min i)
   293 
   294   fun min_arities t =
   295     (case Term.strip_comb t of
   296       (u as Const _, ts) => add u (length ts) #> fold min_arities ts
   297     | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
   298     | (Abs (_, _, u), ts) => min_arities u #> fold min_arities ts
   299     | (_, ts) => fold min_arities ts)
   300 
   301   fun app u (t, T) =
   302     (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
   303 
   304   fun apply i t T ts =
   305     let val (ts1, ts2) = chop i ts
   306     in fst (fold app ts2 (Term.list_comb (t, ts1), snd (U.dest_funT i T))) end
   307 in
   308 
   309 fun intro_explicit_application ts =
   310   let
   311     val arities = fold min_arities ts Termtab.empty
   312     fun apply' t = apply (the (Termtab.lookup arities t)) t
   313 
   314     fun traverse Ts t =
   315       (case Term.strip_comb t of
   316         (u as Const (_, T), ts) => apply' u T (map (traverse Ts) ts)
   317       | (u as Free (_, T), ts) => apply' u T (map (traverse Ts) ts)
   318       | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
   319       | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
   320       | (u, ts) => traverses Ts u ts)
   321     and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
   322   in map (traverse []) ts end
   323 
   324 val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
   325 
   326 end
   327 
   328 
   329 (** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
   330 
   331 local
   332   val term_bool = @{lemma "SMT.term_true ~= SMT.term_false"
   333     by (simp add: SMT.term_true_def SMT.term_false_def)}
   334 
   335   val fol_rules = [
   336     Let_def,
   337     mk_meta_eq @{thm SMT.term_true_def},
   338     mk_meta_eq @{thm SMT.term_false_def},
   339     @{lemma "P = True == P" by (rule eq_reflection) simp},
   340     @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
   341 
   342   fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
   343         reduce_let (Term.betapply (u, t))
   344     | reduce_let (t $ u) = reduce_let t $ reduce_let u
   345     | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
   346     | reduce_let t = t
   347 
   348   fun as_term t = @{const HOL.eq (bool)} $ t $ @{const SMT.term_true}
   349 
   350   fun wrap_in_if t =
   351     @{const If (bool)} $ t $ @{const SMT.term_true} $ @{const SMT.term_false}
   352 
   353   fun is_builtin_conn_or_pred ctxt c ts =
   354     is_some (B.dest_builtin_conn ctxt c ts) orelse
   355     is_some (B.dest_builtin_pred ctxt c ts)
   356 
   357   fun builtin b ctxt c ts =
   358     (case (Const c, ts) of
   359       (@{const HOL.eq (bool)}, [t, u]) =>
   360         if t = @{const SMT.term_true} orelse u = @{const SMT.term_true} then
   361           B.dest_builtin_eq ctxt t u
   362         else b ctxt c ts
   363     | _ => b ctxt c ts)
   364 in
   365 
   366 fun folify ctxt =
   367   let
   368     fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
   369 
   370     fun in_term t =
   371       (case Term.strip_comb t of
   372         (@{const True}, []) => @{const SMT.term_true}
   373       | (@{const False}, []) => @{const SMT.term_false}
   374       | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
   375           u $ in_form t1 $ in_term t2 $ in_term t3
   376       | (Const c, ts) =>
   377           if is_builtin_conn_or_pred ctxt c ts then wrap_in_if (in_form t)
   378           else Term.list_comb (Const c, map in_term ts)
   379       | (Free c, ts) => Term.list_comb (Free c, map in_term ts)
   380       | _ => t)
   381 
   382     and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
   383       | in_weight t = in_form t 
   384 
   385     and in_pat ((p as Const (@{const_name SMT.pat}, _)) $ t) = p $ in_term t
   386       | in_pat ((p as Const (@{const_name SMT.nopat}, _)) $ t) = p $ in_term t
   387       | in_pat t = raise TERM ("bad pattern", [t])
   388 
   389     and in_pats ps =
   390       in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
   391 
   392     and in_trigger ((c as @{const SMT.trigger}) $ p $ t) =
   393           c $ in_pats p $ in_weight t
   394       | in_trigger t = in_weight t
   395 
   396     and in_form t =
   397       (case Term.strip_comb t of
   398         (q as Const (qn, _), [Abs (n, T, u)]) =>
   399           if member (op =) [@{const_name All}, @{const_name Ex}] qn then
   400             q $ Abs (n, T, in_trigger u)
   401           else as_term (in_term t)
   402       | (Const c, ts) =>
   403           (case B.dest_builtin_conn ctxt c ts of
   404             SOME (_, _, us, mk) => mk (map in_form us)
   405           | NONE =>
   406               (case B.dest_builtin_pred ctxt c ts of
   407                 SOME (_, _, us, mk) => mk (map in_term us)
   408               | NONE => as_term (in_term t)))
   409       | _ => as_term (in_term t))
   410   in
   411     map (reduce_let #> in_form) #>
   412     cons (U.prop_of term_bool) #>
   413     pair (fol_rules, [term_bool], builtin)
   414   end
   415 
   416 end
   417 
   418 
   419 (* translation into intermediate format *)
   420 
   421 (** utility functions **)
   422 
   423 val quantifier = (fn
   424     @{const_name All} => SOME SForall
   425   | @{const_name Ex} => SOME SExists
   426   | _ => NONE)
   427 
   428 fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
   429       if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
   430   | group_quant _ Ts t = (Ts, t)
   431 
   432 fun dest_weight (@{const SMT.weight} $ w $ t) =
   433       (SOME (snd (HOLogic.dest_number w)), t)
   434   | dest_weight t = (NONE, t)
   435 
   436 fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
   437   | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
   438   | dest_pat t = raise TERM ("bad pattern", [t])
   439 
   440 fun dest_pats [] = I
   441   | dest_pats ts =
   442       (case map dest_pat ts |> split_list ||> distinct (op =) of
   443         (ps, [true]) => cons (SPat ps)
   444       | (ps, [false]) => cons (SNoPat ps)
   445       | _ => raise TERM ("bad multi-pattern", ts))
   446 
   447 fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
   448       (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
   449   | dest_trigger t = ([], t)
   450 
   451 fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
   452   let
   453     val (Ts, u) = group_quant qn [T] t
   454     val (ps, p) = dest_trigger u
   455     val (w, b) = dest_weight p
   456   in (q, rev Ts, ps, w, b) end)
   457 
   458 fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
   459   | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
   460 
   461 
   462 (** translation from Isabelle terms into SMT intermediate terms **)
   463 
   464 fun intermediate header dtyps builtin ctxt ts trx =
   465   let
   466     fun transT (T as TFree _) = add_typ T true
   467       | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
   468       | transT (T as Type _) =
   469           (case B.dest_builtin_typ ctxt T of
   470             SOME n => pair n
   471           | NONE => add_typ T true)
   472 
   473     fun app n ts = SApp (n, ts)
   474 
   475     fun trans t =
   476       (case Term.strip_comb t of
   477         (Const (qn, _), [Abs (_, T, t1)]) =>
   478           (case dest_quant qn T t1 of
   479             SOME (q, Ts, ps, w, b) =>
   480               fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
   481               trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
   482           | NONE => raise TERM ("unsupported quantifier", [t]))
   483       | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
   484           transT T ##>> trans t1 ##>> trans t2 #>>
   485           (fn ((U, u1), u2) => SLet (U, u1, u2))
   486       | (u as Const (c as (_, T)), ts) =>
   487           (case builtin ctxt c ts of
   488             SOME (n, _, us, _) => fold_map trans us #>> app n
   489           | NONE => transs u T ts)
   490       | (u as Free (_, T), ts) => transs u T ts
   491       | (Bound i, []) => pair (SVar i)
   492       | _ => raise TERM ("bad SMT term", [t]))
   493  
   494     and transs t T ts =
   495       let val (Us, U) = U.dest_funT (length ts) T
   496       in
   497         fold_map transT Us ##>> transT U #-> (fn Up =>
   498         add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
   499       end
   500 
   501     val (us, trx') = fold_map trans ts trx
   502   in ((sign_of (header ts) dtyps trx', us), trx') end
   503 
   504 
   505 
   506 (* translation *)
   507 
   508 structure Configs = Generic_Data
   509 (
   510   type T = (Proof.context -> config) U.dict
   511   val empty = []
   512   val extend = I
   513   fun merge data = U.dict_merge fst data
   514 )
   515 
   516 fun add_config (cs, cfg) = Configs.map (U.dict_update (cs, cfg))
   517 
   518 fun get_config ctxt = 
   519   let val cs = SMT_Config.solver_class_of ctxt
   520   in
   521     (case U.dict_get (Configs.get (Context.Proof ctxt)) cs of
   522       SOME cfg => cfg ctxt
   523     | NONE => error ("SMT: no translation configuration found " ^
   524         "for solver class " ^ quote (U.string_of_class cs)))
   525   end
   526 
   527 fun translate ctxt comments ithms =
   528   let
   529     val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
   530 
   531     val with_datatypes =
   532       has_datatypes andalso Config.get ctxt SMT_Config.datatypes
   533 
   534     fun no_dtyps (tr_context, ctxt) ts = (([], tr_context, ctxt), ts)
   535 
   536     val ts1 = map (Envir.beta_eta_contract o U.prop_of o snd) ithms
   537 
   538     val ((dtyps, tr_context, ctxt1), ts2) =
   539       ((make_tr_context prefixes, ctxt), ts1)
   540       |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
   541 
   542     val (ctxt2, ts3) =
   543       ts2
   544       |> eta_expand ctxt1
   545       |> lift_lambdas ctxt1
   546       ||> intro_explicit_application
   547 
   548     val ((rewrite_rules, extra_thms, builtin), ts4) =
   549       (if is_fol then folify ctxt2 else pair ([], [], I)) ts3
   550 
   551     val rewrite_rules' = fun_app_eq :: rewrite_rules
   552   in
   553     (ts4, tr_context)
   554     |-> intermediate header dtyps (builtin B.dest_builtin) ctxt2
   555     |>> uncurry (serialize comments)
   556     ||> recon_of ctxt2 rewrite_rules' extra_thms ithms
   557   end
   558 
   559 end