src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Tue Dec 07 14:53:12 2010 +0100 (2010-12-07)
changeset 41059 d2b1fc1b8e19
parent 41057 8dbc951a291c
child 41123 3bb9be510a9d
permissions -rw-r--r--
centralized handling of built-in types and constants;
also store types and constants which are rewritten during preprocessing;
interfaces are identified by classes (supporting inheritance, at least on the level of built-in symbols);
removed term_eq in favor of type replacements: term-level occurrences of type bool are replaced by type term_bool (only for the translation)
boehmes@36898
     1
(*  Title:      HOL/Tools/SMT/smt_translate.ML
boehmes@36898
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@36898
     3
boehmes@36898
     4
Translate theorems into an SMT intermediate format and serialize them.
boehmes@36898
     5
*)
boehmes@36898
     6
boehmes@36898
     7
signature SMT_TRANSLATE =
boehmes@36898
     8
sig
boehmes@36898
     9
  (* intermediate term structure *)
boehmes@36898
    10
  datatype squant = SForall | SExists
boehmes@36898
    11
  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    12
  datatype sterm =
boehmes@36898
    13
    SVar of int |
boehmes@36898
    14
    SApp of string * sterm list |
boehmes@36898
    15
    SLet of string * sterm * sterm |
boehmes@40664
    16
    SQua of squant * string list * sterm spattern list * int option * sterm
boehmes@36898
    17
boehmes@36898
    18
  (* configuration options *)
boehmes@36898
    19
  type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    20
  type sign = {
boehmes@36899
    21
    header: string list,
boehmes@36898
    22
    sorts: string list,
boehmes@39298
    23
    dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    24
    funcs: (string * (string list * string)) list }
boehmes@36898
    25
  type config = {
boehmes@36898
    26
    prefixes: prefixes,
boehmes@41059
    27
    header: Proof.context -> term list -> string list,
boehmes@41059
    28
    is_fol: bool,
boehmes@41059
    29
    has_datatypes: bool,
boehmes@36898
    30
    serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    31
  type recon = {
boehmes@36898
    32
    typs: typ Symtab.table,
boehmes@36898
    33
    terms: term Symtab.table,
boehmes@36898
    34
    unfolds: thm list,
boehmes@40161
    35
    assms: (int * thm) list }
boehmes@36898
    36
boehmes@40161
    37
  val translate: config -> Proof.context -> string list -> (int * thm) list ->
boehmes@36898
    38
    string * recon
boehmes@36898
    39
end
boehmes@36898
    40
boehmes@36898
    41
structure SMT_Translate: SMT_TRANSLATE =
boehmes@36898
    42
struct
boehmes@36898
    43
boehmes@40663
    44
structure U = SMT_Utils
boehmes@41059
    45
structure B = SMT_Builtin
boehmes@40663
    46
boehmes@40663
    47
boehmes@36898
    48
(* intermediate term structure *)
boehmes@36898
    49
boehmes@36898
    50
datatype squant = SForall | SExists
boehmes@36898
    51
boehmes@36898
    52
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    53
boehmes@36898
    54
datatype sterm =
boehmes@36898
    55
  SVar of int |
boehmes@36898
    56
  SApp of string * sterm list |
boehmes@36898
    57
  SLet of string * sterm * sterm |
boehmes@40664
    58
  SQua of squant * string list * sterm spattern list * int option * sterm
boehmes@36898
    59
boehmes@36898
    60
boehmes@36898
    61
boehmes@36898
    62
(* configuration options *)
boehmes@36898
    63
boehmes@36898
    64
type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    65
boehmes@36898
    66
type sign = {
boehmes@36899
    67
  header: string list,
boehmes@36898
    68
  sorts: string list,
boehmes@39298
    69
  dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    70
  funcs: (string * (string list * string)) list }
boehmes@36898
    71
boehmes@36898
    72
type config = {
boehmes@36898
    73
  prefixes: prefixes,
boehmes@41059
    74
  header: Proof.context -> term list -> string list,
boehmes@41059
    75
  is_fol: bool,
boehmes@41059
    76
  has_datatypes: bool,
boehmes@36898
    77
  serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    78
boehmes@36898
    79
type recon = {
boehmes@36898
    80
  typs: typ Symtab.table,
boehmes@36898
    81
  terms: term Symtab.table,
boehmes@36898
    82
  unfolds: thm list,
boehmes@40161
    83
  assms: (int * thm) list }
boehmes@36898
    84
boehmes@36898
    85
boehmes@36898
    86
boehmes@36898
    87
(* utility functions *)
boehmes@36898
    88
boehmes@36898
    89
val quantifier = (fn
boehmes@36898
    90
    @{const_name All} => SOME SForall
boehmes@36898
    91
  | @{const_name Ex} => SOME SExists
boehmes@36898
    92
  | _ => NONE)
boehmes@36898
    93
boehmes@36898
    94
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
    95
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
    96
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
    97
boehmes@40664
    98
fun dest_weight (@{const SMT.weight} $ w $ t) =
boehmes@40664
    99
      (SOME (snd (HOLogic.dest_number w)), t)
boehmes@40664
   100
  | dest_weight t = (NONE, t)
boehmes@40664
   101
boehmes@37124
   102
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
boehmes@37124
   103
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
boehmes@37124
   104
  | dest_pat t = raise TERM ("dest_pat", [t])
boehmes@37124
   105
boehmes@37124
   106
fun dest_pats [] = I
boehmes@37124
   107
  | dest_pats ts =
boehmes@37124
   108
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   109
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   110
      | (ps, [false]) => cons (SNoPat ps)
boehmes@37124
   111
      | _ => raise TERM ("dest_pats", ts))
boehmes@36898
   112
boehmes@40579
   113
fun dest_trigger (@{const trigger} $ tl $ t) =
boehmes@37124
   114
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   115
  | dest_trigger t = ([], t)
boehmes@36898
   116
boehmes@36898
   117
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   118
  let
boehmes@36898
   119
    val (Ts, u) = group_quant qn [T] t
boehmes@40664
   120
    val (ps, p) = dest_trigger u
boehmes@40664
   121
    val (w, b) = dest_weight p
boehmes@40664
   122
  in (q, rev Ts, ps, w, b) end)
boehmes@36898
   123
boehmes@36898
   124
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   125
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   126
boehmes@36898
   127
fun prop_of thm = HOLogic.dest_Trueprop (Thm.prop_of thm)
boehmes@36898
   128
boehmes@36898
   129
boehmes@36898
   130
boehmes@41059
   131
(* map HOL formulas to FOL formulas (i.e., separate formulas froms terms) *)
boehmes@36898
   132
boehmes@41059
   133
val tboolT = @{typ SMT.term_bool}
boehmes@41059
   134
val term_true = Const (@{const_name True}, tboolT)
boehmes@41059
   135
val term_false = Const (@{const_name False}, tboolT)
boehmes@36898
   136
boehmes@41059
   137
val term_bool = @{lemma "True ~= False" by simp}
boehmes@41059
   138
val term_bool_prop =
boehmes@41059
   139
  let
boehmes@41059
   140
    fun replace @{const HOL.eq (bool)} = @{const HOL.eq (SMT.term_bool)}
boehmes@41059
   141
      | replace @{const True} = term_true
boehmes@41059
   142
      | replace @{const False} = term_false
boehmes@41059
   143
      | replace t = t
boehmes@41059
   144
  in Term.map_aterms replace (prop_of term_bool) end
boehmes@36898
   145
boehmes@36898
   146
val needs_rewrite = Thm.prop_of #> Term.exists_subterm (fn
boehmes@36898
   147
    Const (@{const_name Let}, _) => true
boehmes@40579
   148
  | @{const HOL.eq (bool)} $ _ $ @{const True} => true
boehmes@40579
   149
  | Const (@{const_name If}, _) $ _ $ @{const True} $ @{const False} => true
boehmes@36898
   150
  | _ => false)
boehmes@36898
   151
boehmes@36898
   152
val rewrite_rules = [
boehmes@36898
   153
  Let_def,
boehmes@36898
   154
  @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@36898
   155
  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@36898
   156
boehmes@41059
   157
fun rewrite ctxt ct =
boehmes@41059
   158
  Conv.top_sweep_conv (fn ctxt' =>
boehmes@41059
   159
    Conv.rewrs_conv rewrite_rules then_conv rewrite ctxt') ctxt ct
boehmes@36898
   160
boehmes@36898
   161
fun normalize ctxt thm =
boehmes@36898
   162
  if needs_rewrite thm then Conv.fconv_rule (rewrite ctxt) thm else thm
boehmes@36898
   163
boehmes@41059
   164
fun revert_typ @{typ SMT.term_bool} = @{typ bool}
boehmes@41059
   165
  | revert_typ (Type (n, Ts)) = Type (n, map revert_typ Ts)
boehmes@41059
   166
  | revert_typ T = T
boehmes@36898
   167
boehmes@41059
   168
val revert_types = Term.map_types revert_typ
boehmes@36898
   169
boehmes@41059
   170
fun folify ctxt =
boehmes@36898
   171
  let
boehmes@41059
   172
    fun is_builtin_conn (@{const_name True}, _) _ = false
boehmes@41059
   173
      | is_builtin_conn (@{const_name False}, _) _ = false
boehmes@41059
   174
      | is_builtin_conn c ts = B.is_builtin_conn ctxt c ts
boehmes@36898
   175
boehmes@41059
   176
    fun as_term t = @{const HOL.eq (SMT.term_bool)} $ t $ term_true
boehmes@40681
   177
boehmes@41059
   178
    fun as_tbool @{typ bool} = tboolT
boehmes@41059
   179
      | as_tbool (Type (n, Ts)) = Type (n, map as_tbool Ts)
boehmes@41059
   180
      | as_tbool T = T
boehmes@36898
   181
    fun mapTs f g = Term.strip_type #> (fn (Ts, T) => map f Ts ---> g T)
boehmes@41059
   182
    fun predT T = mapTs as_tbool I T
boehmes@41059
   183
    fun funcT T = mapTs as_tbool as_tbool T
boehmes@41059
   184
    fun func (n, T) = Const (n, funcT T)
boehmes@36898
   185
boehmes@41059
   186
    fun map_ifT T = T |> Term.dest_funT ||> funcT |> (op -->)
boehmes@41059
   187
    val if_term = @{const If (bool)} |> Term.dest_Const ||> map_ifT |> Const
boehmes@41059
   188
    fun wrap_in_if t = if_term $ t $ term_true $ term_false
boehmes@36898
   189
boehmes@36898
   190
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@36898
   191
boehmes@36898
   192
    fun in_term t =
boehmes@36898
   193
      (case Term.strip_comb t of
boehmes@41059
   194
        (Const (c as @{const_name If}, T), [t1, t2, t3]) =>
boehmes@41059
   195
          Const (c, map_ifT T) $ in_form t1 $ in_term t2 $ in_term t3
boehmes@41059
   196
      | (Const c, ts) =>
boehmes@41059
   197
          if is_builtin_conn c ts orelse B.is_builtin_pred ctxt c ts
boehmes@36898
   198
          then wrap_in_if (in_form t)
boehmes@41059
   199
          else Term.list_comb (func c, map in_term ts)
boehmes@41059
   200
      | (Free (n, T), ts) => Term.list_comb (Free (n, funcT T), map in_term ts)
boehmes@36898
   201
      | _ => t)
boehmes@36898
   202
boehmes@40664
   203
    and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
boehmes@40664
   204
      | in_weight t = in_form t 
boehmes@40664
   205
boehmes@41059
   206
    and in_pat (Const (c as (@{const_name pat}, _)) $ t) = func c $ in_term t
boehmes@41059
   207
      | in_pat (Const (c as (@{const_name nopat}, _)) $ t) = func c $ in_term t
boehmes@36898
   208
      | in_pat t = raise TERM ("in_pat", [t])
boehmes@36898
   209
boehmes@37124
   210
    and in_pats ps =
boehmes@37124
   211
      in_list @{typ "pattern list"} (in_list @{typ pattern} in_pat) ps
boehmes@36898
   212
boehmes@40664
   213
    and in_trig ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_weight t
boehmes@40664
   214
      | in_trig t = in_weight t
boehmes@36898
   215
boehmes@36898
   216
    and in_form t =
boehmes@36898
   217
      (case Term.strip_comb t of
boehmes@36898
   218
        (q as Const (qn, _), [Abs (n, T, t')]) =>
boehmes@41059
   219
          if is_some (quantifier qn) then q $ Abs (n, as_tbool T, in_trig t')
boehmes@36898
   220
          else as_term (in_term t)
boehmes@41059
   221
      | (Const (c as (n as @{const_name distinct}, T)), [t']) =>
boehmes@41059
   222
          if B.is_builtin_fun ctxt c [t'] then
boehmes@41059
   223
            Const (n, predT T) $ in_list T in_term t'
boehmes@36898
   224
          else as_term (in_term t)
boehmes@41059
   225
      | (Const (c as (n, T)), ts) =>
boehmes@41059
   226
          if B.is_builtin_conn ctxt c ts
boehmes@41059
   227
          then Term.list_comb (Const c, map in_form ts)
boehmes@41059
   228
          else if B.is_builtin_pred ctxt c ts
boehmes@41059
   229
          then Term.list_comb (Const (n, predT T), map in_term ts)
boehmes@36898
   230
          else as_term (in_term t)
boehmes@36898
   231
      | _ => as_term (in_term t))
boehmes@36898
   232
  in
boehmes@40161
   233
    map (apsnd (normalize ctxt)) #> (fn irules =>
boehmes@41059
   234
    ((rewrite_rules, (~1, term_bool) :: irules),
boehmes@41059
   235
     term_bool_prop :: map (in_form o prop_of o snd) irules))
boehmes@36898
   236
  end
boehmes@36898
   237
boehmes@36898
   238
boehmes@36898
   239
boehmes@36898
   240
(* translation from Isabelle terms into SMT intermediate terms *)
boehmes@36898
   241
boehmes@39298
   242
val empty_context = (1, Typtab.empty, [], 1, Termtab.empty)
boehmes@36898
   243
boehmes@39298
   244
fun make_sign header (_, typs, dtyps, _, terms) = {
boehmes@36899
   245
  header = header,
boehmes@39298
   246
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
boehmes@39298
   247
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms [],
boehmes@39435
   248
  dtyps = rev dtyps }
boehmes@36898
   249
boehmes@39298
   250
fun make_recon (unfolds, assms) (_, typs, _, _, terms) = {
boehmes@39298
   251
  typs = Symtab.make (map (apfst fst o swap) (Typtab.dest typs)),
boehmes@39298
   252
    (*FIXME: don't drop the datatype information! *)
boehmes@36898
   253
  terms = Symtab.make (map (fn (t, (n, _)) => (n, t)) (Termtab.dest terms)),
boehmes@36898
   254
  unfolds = unfolds,
boehmes@36899
   255
  assms = assms }
boehmes@36898
   256
boehmes@36898
   257
fun string_of_index pre i = pre ^ string_of_int i
boehmes@36898
   258
boehmes@39298
   259
fun new_typ sort_prefix proper T (Tidx, typs, dtyps, idx, terms) =
boehmes@41059
   260
  let
boehmes@41059
   261
    val s = string_of_index sort_prefix Tidx
boehmes@41059
   262
    val U = revert_typ T
boehmes@41059
   263
  in (s, (Tidx+1, Typtab.update (U, (s, proper)) typs, dtyps, idx, terms)) end
boehmes@39298
   264
boehmes@41059
   265
fun lookup_typ (_, typs, _, _, _) = Typtab.lookup typs o revert_typ
boehmes@36898
   266
boehmes@39298
   267
fun fresh_typ T f cx =
boehmes@39298
   268
  (case lookup_typ cx T of
boehmes@39298
   269
    SOME (s, _) => (s, cx)
boehmes@39298
   270
  | NONE => f T cx)
boehmes@39298
   271
boehmes@39298
   272
fun new_fun func_prefix t ss (Tidx, typs, dtyps, idx, terms) =
boehmes@39298
   273
  let
boehmes@39298
   274
    val f = string_of_index func_prefix idx
boehmes@39298
   275
    val terms' = Termtab.update (revert_types t, (f, ss)) terms
boehmes@39298
   276
  in (f, (Tidx, typs, dtyps, idx+1, terms')) end
boehmes@39298
   277
boehmes@39298
   278
fun fresh_fun func_prefix t ss (cx as (_, _, _, _, terms)) =
boehmes@41059
   279
  (case Termtab.lookup terms (revert_types t) of
boehmes@36898
   280
    SOME (f, _) => (f, cx)
boehmes@39298
   281
  | NONE => new_fun func_prefix t ss cx)
boehmes@39298
   282
boehmes@39483
   283
fun mk_type (_, Tfs) (d as Datatype.DtTFree _) = the (AList.lookup (op =) Tfs d)
boehmes@39483
   284
  | mk_type Ts (Datatype.DtType (n, ds)) = Type (n, map (mk_type Ts) ds)
boehmes@39483
   285
  | mk_type (Tds, _) (Datatype.DtRec i) = nth Tds i
boehmes@39298
   286
boehmes@39483
   287
fun mk_selector ctxt Ts T n (i, d) =
boehmes@39483
   288
  (case Datatype_Selectors.lookup_selector ctxt (n, i+1) of
boehmes@39483
   289
    NONE => raise Fail ("missing selector for datatype constructor " ^ quote n)
boehmes@39483
   290
  | SOME m => mk_type Ts d |> (fn U => (Const (m, T --> U), U)))
boehmes@39483
   291
boehmes@39483
   292
fun mk_constructor ctxt Ts T (n, args) =
boehmes@39483
   293
  let val (sels, Us) = split_list (map_index (mk_selector ctxt Ts T n) args)
boehmes@39483
   294
  in (Const (n, Us ---> T), sels) end
boehmes@39298
   295
boehmes@39535
   296
fun lookup_datatype ctxt n Ts =
boehmes@39535
   297
  if member (op =) [@{type_name bool}, @{type_name nat}] n then NONE
boehmes@39535
   298
  else
boehmes@39535
   299
    Datatype.get_info (ProofContext.theory_of ctxt) n
boehmes@39535
   300
    |> Option.map (fn {descr, ...} =>
boehmes@39535
   301
         let
boehmes@39535
   302
           val Tds = map (fn (_, (tn, _, _)) => Type (tn, Ts))
boehmes@39535
   303
             (sort (int_ord o pairself fst) descr)
boehmes@39535
   304
           val Tfs = (case hd descr of (_, (_, tfs, _)) => tfs ~~ Ts)
boehmes@39535
   305
         in
boehmes@39535
   306
           descr |> map (fn (i, (_, _, cs)) =>
boehmes@39535
   307
             (nth Tds i, map (mk_constructor ctxt (Tds, Tfs) (nth Tds i)) cs))
boehmes@39535
   308
         end)
boehmes@36898
   309
boehmes@40161
   310
fun relaxed irules = (([], irules), map (prop_of o snd) irules)
boehmes@36898
   311
boehmes@36899
   312
fun with_context header f (ths, ts) =
boehmes@36898
   313
  let val (us, context) = fold_map f ts empty_context
boehmes@36899
   314
  in ((make_sign (header ts) context, us), make_recon ths context) end
boehmes@36898
   315
boehmes@36898
   316
boehmes@41059
   317
fun translate config ctxt comments =
boehmes@36898
   318
  let
boehmes@41059
   319
    val {prefixes, is_fol, header, has_datatypes, serialize} = config
boehmes@36898
   320
    val {sort_prefix, func_prefix} = prefixes
boehmes@39298
   321
boehmes@39298
   322
    fun transT (T as TFree _) = fresh_typ T (new_typ sort_prefix true)
boehmes@39298
   323
      | transT (T as TVar _) = (fn _ => raise TYPE ("smt_translate", [T], []))
boehmes@39298
   324
      | transT (T as Type (n, Ts)) =
boehmes@41059
   325
          (case B.builtin_typ ctxt T of
boehmes@39298
   326
            SOME n => pair n
boehmes@39298
   327
          | NONE => fresh_typ T (fn _ => fn cx =>
boehmes@39298
   328
              if not has_datatypes then new_typ sort_prefix true T cx
boehmes@39298
   329
              else
boehmes@39298
   330
                (case lookup_datatype ctxt n Ts of
boehmes@39298
   331
                  NONE => new_typ sort_prefix true T cx
boehmes@39298
   332
                | SOME dts =>
boehmes@39298
   333
                    let val cx' = new_dtyps dts cx 
boehmes@39298
   334
                    in (fst (the (lookup_typ cx' T)), cx') end)))
boehmes@36898
   335
boehmes@39298
   336
    and new_dtyps dts cx =
boehmes@39298
   337
      let
boehmes@39298
   338
        fun new_decl i t =
boehmes@40663
   339
          let val (Ts, T) = U.dest_funT i (Term.fastype_of t)
boehmes@39298
   340
          in
boehmes@39298
   341
            fold_map transT Ts ##>> transT T ##>>
boehmes@39298
   342
            new_fun func_prefix t NONE #>> swap
boehmes@39298
   343
          end
boehmes@39298
   344
        fun new_dtyp_decl (con, sels) =
boehmes@39298
   345
          new_decl (length sels) con ##>> fold_map (new_decl 1) sels #>>
boehmes@39298
   346
          (fn ((con', _), sels') => (con', map (apsnd snd) sels'))
boehmes@39298
   347
      in
boehmes@39298
   348
        cx
boehmes@39298
   349
        |> fold_map (new_typ sort_prefix false o fst) dts
boehmes@39298
   350
        ||>> fold_map (fold_map new_dtyp_decl o snd) dts
boehmes@39298
   351
        |-> (fn (ss, decls) => fn (Tidx, typs, dtyps, idx, terms) =>
boehmes@39298
   352
              (Tidx, typs, (ss ~~ decls) :: dtyps, idx, terms))
boehmes@39298
   353
      end
boehmes@36898
   354
boehmes@36898
   355
    fun app n ts = SApp (n, ts)
boehmes@36898
   356
boehmes@36898
   357
    fun trans t =
boehmes@36898
   358
      (case Term.strip_comb t of
boehmes@36898
   359
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   360
          (case dest_quant qn T t1 of
boehmes@40664
   361
            SOME (q, Ts, ps, w, b) =>
boehmes@36898
   362
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@40664
   363
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
boehmes@36898
   364
          | NONE => raise TERM ("intermediate", [t]))
boehmes@36898
   365
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   366
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   367
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@40681
   368
      | (h as Const (c as (@{const_name distinct}, T)), ts) =>
boehmes@41059
   369
          (case B.builtin_fun ctxt c ts of
boehmes@36899
   370
            SOME (n, ts) => fold_map trans ts #>> app n
boehmes@40681
   371
          | NONE => transs h T ts)
boehmes@36898
   372
      | (h as Const (c as (_, T)), ts) =>
boehmes@41059
   373
          (case B.builtin_num ctxt t of
boehmes@41059
   374
            SOME n => pair (SApp (n, []))
boehmes@36898
   375
          | NONE =>
boehmes@41059
   376
              (case B.builtin_fun ctxt c ts of
boehmes@36899
   377
                SOME (n, ts') => fold_map trans ts' #>> app n
boehmes@36898
   378
              | NONE => transs h T ts))
boehmes@36898
   379
      | (h as Free (_, T), ts) => transs h T ts
boehmes@36898
   380
      | (Bound i, []) => pair (SVar i)
blanchet@40697
   381
      | (Abs (_, _, t1 $ Bound 0), []) =>
blanchet@40697
   382
        if not (loose_bvar1 (t1, 0)) then trans t1 (* eta-reduce on the fly *)
blanchet@40697
   383
        else raise TERM ("smt_translate", [t])
boehmes@39298
   384
      | _ => raise TERM ("smt_translate", [t]))
boehmes@36898
   385
boehmes@36898
   386
    and transs t T ts =
boehmes@40663
   387
      let val (Us, U) = U.dest_funT (length ts) T
boehmes@36898
   388
      in
boehmes@36898
   389
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@39298
   390
        fresh_fun func_prefix t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   391
      end
boehmes@36898
   392
  in
boehmes@41059
   393
    (if is_fol then folify ctxt else relaxed) #>
boehmes@36899
   394
    with_context (header ctxt) trans #>> uncurry (serialize comments)
boehmes@36898
   395
  end
boehmes@36898
   396
boehmes@36898
   397
end