src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Thu Sep 16 06:49:46 2010 +0200 (2010-09-16)
changeset 39435 5d18f4c00c07
parent 39298 5aefb5bc8a93
child 39483 9f0e5684f04b
permissions -rw-r--r--
reverse order of datatype declarations so that declarations only depend on already declared datatypes
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@36898
    16
    SQua of squant * string list * sterm spattern list * sterm
boehmes@36898
    17
boehmes@36898
    18
  (* configuration options *)
boehmes@36898
    19
  type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36899
    20
  type header = Proof.context -> term list -> string list
boehmes@36898
    21
  type strict = {
boehmes@36898
    22
    is_builtin_conn: string * typ -> bool,
boehmes@36899
    23
    is_builtin_pred: Proof.context -> string * typ -> bool,
boehmes@36898
    24
    is_builtin_distinct: bool}
boehmes@36898
    25
  type builtins = {
boehmes@36899
    26
    builtin_typ: Proof.context -> typ -> string option,
boehmes@36899
    27
    builtin_num: Proof.context -> typ -> int -> string option,
boehmes@36899
    28
    builtin_fun: Proof.context -> string * typ -> term list ->
boehmes@39298
    29
      (string * term list) option,
boehmes@39298
    30
    has_datatypes: bool }
boehmes@36898
    31
  type sign = {
boehmes@36899
    32
    header: string list,
boehmes@36898
    33
    sorts: string list,
boehmes@39298
    34
    dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    35
    funcs: (string * (string list * string)) list }
boehmes@36898
    36
  type config = {
boehmes@36898
    37
    prefixes: prefixes,
boehmes@36899
    38
    header: header,
boehmes@36898
    39
    strict: strict option,
boehmes@36898
    40
    builtins: builtins,
boehmes@36898
    41
    serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    42
  type recon = {
boehmes@36898
    43
    typs: typ Symtab.table,
boehmes@36898
    44
    terms: term Symtab.table,
boehmes@36898
    45
    unfolds: thm list,
boehmes@36899
    46
    assms: thm list }
boehmes@36898
    47
boehmes@36898
    48
  val translate: config -> Proof.context -> string list -> thm list ->
boehmes@36898
    49
    string * recon
boehmes@36898
    50
end
boehmes@36898
    51
boehmes@36898
    52
structure SMT_Translate: SMT_TRANSLATE =
boehmes@36898
    53
struct
boehmes@36898
    54
boehmes@36898
    55
(* intermediate term structure *)
boehmes@36898
    56
boehmes@36898
    57
datatype squant = SForall | SExists
boehmes@36898
    58
boehmes@36898
    59
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    60
boehmes@36898
    61
datatype sterm =
boehmes@36898
    62
  SVar of int |
boehmes@36898
    63
  SApp of string * sterm list |
boehmes@36898
    64
  SLet of string * sterm * sterm |
boehmes@36898
    65
  SQua of squant * string list * sterm spattern list * sterm
boehmes@36898
    66
boehmes@36898
    67
boehmes@36898
    68
boehmes@36898
    69
(* configuration options *)
boehmes@36898
    70
boehmes@36898
    71
type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    72
boehmes@36899
    73
type header = Proof.context -> term list -> string list
boehmes@36899
    74
boehmes@36898
    75
type strict = {
boehmes@36898
    76
  is_builtin_conn: string * typ -> bool,
boehmes@36899
    77
  is_builtin_pred: Proof.context -> string * typ -> bool,
boehmes@36898
    78
  is_builtin_distinct: bool}
boehmes@36898
    79
boehmes@36898
    80
type builtins = {
boehmes@36899
    81
  builtin_typ: Proof.context -> typ -> string option,
boehmes@36899
    82
  builtin_num: Proof.context -> typ -> int -> string option,
boehmes@36899
    83
  builtin_fun: Proof.context -> string * typ -> term list ->
boehmes@39298
    84
    (string * term list) option,
boehmes@39298
    85
  has_datatypes: bool }
boehmes@36898
    86
boehmes@36898
    87
type sign = {
boehmes@36899
    88
  header: string list,
boehmes@36898
    89
  sorts: string list,
boehmes@39298
    90
  dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    91
  funcs: (string * (string list * string)) list }
boehmes@36898
    92
boehmes@36898
    93
type config = {
boehmes@36898
    94
  prefixes: prefixes,
boehmes@36899
    95
  header: header,
boehmes@36898
    96
  strict: strict option,
boehmes@36898
    97
  builtins: builtins,
boehmes@36898
    98
  serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    99
boehmes@36898
   100
type recon = {
boehmes@36898
   101
  typs: typ Symtab.table,
boehmes@36898
   102
  terms: term Symtab.table,
boehmes@36898
   103
  unfolds: thm list,
boehmes@36899
   104
  assms: thm list }
boehmes@36898
   105
boehmes@36898
   106
boehmes@36898
   107
boehmes@36898
   108
(* utility functions *)
boehmes@36898
   109
boehmes@36898
   110
val dest_funT =
boehmes@36898
   111
  let
boehmes@36898
   112
    fun dest Ts 0 T = (rev Ts, T)
boehmes@36898
   113
      | dest Ts i (Type ("fun", [T, U])) = dest (T::Ts) (i-1) U
boehmes@36898
   114
      | dest _ _ T = raise TYPE ("dest_funT", [T], [])
boehmes@36898
   115
  in dest [] end
boehmes@36898
   116
boehmes@36898
   117
val quantifier = (fn
boehmes@36898
   118
    @{const_name All} => SOME SForall
boehmes@36898
   119
  | @{const_name Ex} => SOME SExists
boehmes@36898
   120
  | _ => NONE)
boehmes@36898
   121
boehmes@36898
   122
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   123
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   124
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   125
boehmes@37124
   126
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
boehmes@37124
   127
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
boehmes@37124
   128
  | dest_pat t = raise TERM ("dest_pat", [t])
boehmes@37124
   129
boehmes@37124
   130
fun dest_pats [] = I
boehmes@37124
   131
  | dest_pats ts =
boehmes@37124
   132
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   133
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   134
      | (ps, [false]) => cons (SNoPat ps)
boehmes@37124
   135
      | _ => raise TERM ("dest_pats", ts))
boehmes@36898
   136
boehmes@36898
   137
fun dest_trigger (@{term trigger} $ tl $ t) =
boehmes@37124
   138
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   139
  | dest_trigger t = ([], t)
boehmes@36898
   140
boehmes@36898
   141
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   142
  let
boehmes@36898
   143
    val (Ts, u) = group_quant qn [T] t
boehmes@36898
   144
    val (ps, b) = dest_trigger u
boehmes@36898
   145
  in (q, rev Ts, ps, b) end)
boehmes@36898
   146
boehmes@36898
   147
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   148
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   149
boehmes@36898
   150
fun prop_of thm = HOLogic.dest_Trueprop (Thm.prop_of thm)
boehmes@36898
   151
boehmes@36898
   152
boehmes@36898
   153
boehmes@36898
   154
(* enforce a strict separation between formulas and terms *)
boehmes@36898
   155
boehmes@37124
   156
val term_eq_rewr = @{lemma "term_eq x y == x = y" by (simp add: term_eq_def)}
boehmes@36898
   157
boehmes@37124
   158
val term_bool = @{lemma "~(term_eq True False)" by (simp add: term_eq_def)}
boehmes@36898
   159
val term_bool' = Simplifier.rewrite_rule [term_eq_rewr] term_bool
boehmes@36898
   160
boehmes@36898
   161
boehmes@36898
   162
val needs_rewrite = Thm.prop_of #> Term.exists_subterm (fn
boehmes@36898
   163
    Const (@{const_name Let}, _) => true
boehmes@36898
   164
  | @{term "op = :: bool => _"} $ _ $ @{term True} => true
boehmes@36898
   165
  | Const (@{const_name If}, _) $ _ $ @{term True} $ @{term False} => true
boehmes@36898
   166
  | _ => false)
boehmes@36898
   167
boehmes@36898
   168
val rewrite_rules = [
boehmes@36898
   169
  Let_def,
boehmes@36898
   170
  @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@36898
   171
  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@36898
   172
boehmes@36898
   173
fun rewrite ctxt = Simplifier.full_rewrite
boehmes@36898
   174
  (Simplifier.context ctxt empty_ss addsimps rewrite_rules)
boehmes@36898
   175
boehmes@36898
   176
fun normalize ctxt thm =
boehmes@36898
   177
  if needs_rewrite thm then Conv.fconv_rule (rewrite ctxt) thm else thm
boehmes@36898
   178
boehmes@36898
   179
val unfold_rules = term_eq_rewr :: rewrite_rules
boehmes@36898
   180
boehmes@36898
   181
boehmes@36898
   182
val revert_types =
boehmes@36898
   183
  let
boehmes@36898
   184
    fun revert @{typ prop} = @{typ bool}
boehmes@36898
   185
      | revert (Type (n, Ts)) = Type (n, map revert Ts)
boehmes@36898
   186
      | revert T = T
boehmes@36898
   187
  in Term.map_types revert end
boehmes@36898
   188
boehmes@36898
   189
boehmes@36898
   190
fun strictify {is_builtin_conn, is_builtin_pred, is_builtin_distinct} ctxt =
boehmes@36898
   191
  let
boehmes@36898
   192
    fun is_builtin_conn' (@{const_name True}, _) = false
boehmes@36898
   193
      | is_builtin_conn' (@{const_name False}, _) = false
boehmes@36898
   194
      | is_builtin_conn' c = is_builtin_conn c
boehmes@36898
   195
boehmes@36898
   196
    val propT = @{typ prop} and boolT = @{typ bool}
boehmes@36898
   197
    val as_propT = (fn @{typ bool} => propT | T => T)
boehmes@36898
   198
    fun mapTs f g = Term.strip_type #> (fn (Ts, T) => map f Ts ---> g T)
boehmes@36898
   199
    fun conn (n, T) = (n, mapTs as_propT as_propT T)
boehmes@36898
   200
    fun pred (n, T) = (n, mapTs I as_propT T)
boehmes@36898
   201
boehmes@36898
   202
    val term_eq = @{term "op = :: bool => _"} |> Term.dest_Const |> pred
boehmes@36898
   203
    fun as_term t = Const term_eq $ t $ @{term True}
boehmes@36898
   204
boehmes@36898
   205
    val if_term = Const (@{const_name If}, [propT, boolT, boolT] ---> boolT)
boehmes@36898
   206
    fun wrap_in_if t = if_term $ t $ @{term True} $ @{term False}
boehmes@36898
   207
boehmes@36898
   208
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@36898
   209
boehmes@36898
   210
    fun in_term t =
boehmes@36898
   211
      (case Term.strip_comb t of
boehmes@36898
   212
        (c as Const (@{const_name If}, _), [t1, t2, t3]) =>
boehmes@36898
   213
          c $ in_form t1 $ in_term t2 $ in_term t3
boehmes@36898
   214
      | (h as Const c, ts) =>
boehmes@36899
   215
          if is_builtin_conn' (conn c) orelse is_builtin_pred ctxt (pred c)
boehmes@36898
   216
          then wrap_in_if (in_form t)
boehmes@36898
   217
          else Term.list_comb (h, map in_term ts)
boehmes@36898
   218
      | (h as Free _, ts) => Term.list_comb (h, map in_term ts)
boehmes@36898
   219
      | _ => t)
boehmes@36898
   220
boehmes@36898
   221
    and in_pat ((c as Const (@{const_name pat}, _)) $ t) = c $ in_term t
boehmes@36898
   222
      | in_pat ((c as Const (@{const_name nopat}, _)) $ t) = c $ in_term t
boehmes@36898
   223
      | in_pat t = raise TERM ("in_pat", [t])
boehmes@36898
   224
boehmes@37124
   225
    and in_pats ps =
boehmes@37124
   226
      in_list @{typ "pattern list"} (in_list @{typ pattern} in_pat) ps
boehmes@36898
   227
boehmes@36898
   228
    and in_trig ((c as @{term trigger}) $ p $ t) = c $ in_pats p $ in_form t
boehmes@36898
   229
      | in_trig t = in_form t
boehmes@36898
   230
boehmes@36898
   231
    and in_form t =
boehmes@36898
   232
      (case Term.strip_comb t of
boehmes@36898
   233
        (q as Const (qn, _), [Abs (n, T, t')]) =>
boehmes@36898
   234
          if is_some (quantifier qn) then q $ Abs (n, T, in_trig t')
boehmes@36898
   235
          else as_term (in_term t)
boehmes@36898
   236
      | (Const (c as (@{const_name distinct}, T)), [t']) =>
boehmes@36898
   237
          if is_builtin_distinct then Const (pred c) $ in_list T in_term t'
boehmes@36898
   238
          else as_term (in_term t)
boehmes@36898
   239
      | (Const c, ts) =>
boehmes@36898
   240
          if is_builtin_conn (conn c)
boehmes@36898
   241
          then Term.list_comb (Const (conn c), map in_form ts)
boehmes@36899
   242
          else if is_builtin_pred ctxt (pred c)
boehmes@36898
   243
          then Term.list_comb (Const (pred c), map in_term ts)
boehmes@36898
   244
          else as_term (in_term t)
boehmes@36898
   245
      | _ => as_term (in_term t))
boehmes@36898
   246
  in
boehmes@36898
   247
    map (normalize ctxt) #> (fn thms => ((unfold_rules, term_bool' :: thms),
boehmes@36898
   248
    map (in_form o prop_of) (term_bool :: thms)))
boehmes@36898
   249
  end
boehmes@36898
   250
boehmes@36898
   251
boehmes@36898
   252
boehmes@36898
   253
(* translation from Isabelle terms into SMT intermediate terms *)
boehmes@36898
   254
boehmes@39298
   255
val empty_context = (1, Typtab.empty, [], 1, Termtab.empty)
boehmes@36898
   256
boehmes@39298
   257
fun make_sign header (_, typs, dtyps, _, terms) = {
boehmes@36899
   258
  header = header,
boehmes@39298
   259
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
boehmes@39298
   260
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms [],
boehmes@39435
   261
  dtyps = rev dtyps }
boehmes@36898
   262
boehmes@39298
   263
fun make_recon (unfolds, assms) (_, typs, _, _, terms) = {
boehmes@39298
   264
  typs = Symtab.make (map (apfst fst o swap) (Typtab.dest typs)),
boehmes@39298
   265
    (*FIXME: don't drop the datatype information! *)
boehmes@36898
   266
  terms = Symtab.make (map (fn (t, (n, _)) => (n, t)) (Termtab.dest terms)),
boehmes@36898
   267
  unfolds = unfolds,
boehmes@36899
   268
  assms = assms }
boehmes@36898
   269
boehmes@36898
   270
fun string_of_index pre i = pre ^ string_of_int i
boehmes@36898
   271
boehmes@39298
   272
fun new_typ sort_prefix proper T (Tidx, typs, dtyps, idx, terms) =
boehmes@39298
   273
  let val s = string_of_index sort_prefix Tidx
boehmes@39298
   274
  in (s, (Tidx+1, Typtab.update (T, (s, proper)) typs, dtyps, idx, terms)) end
boehmes@39298
   275
boehmes@39298
   276
fun lookup_typ (_, typs, _, _, _) = Typtab.lookup typs
boehmes@36898
   277
boehmes@39298
   278
fun fresh_typ T f cx =
boehmes@39298
   279
  (case lookup_typ cx T of
boehmes@39298
   280
    SOME (s, _) => (s, cx)
boehmes@39298
   281
  | NONE => f T cx)
boehmes@39298
   282
boehmes@39298
   283
fun new_fun func_prefix t ss (Tidx, typs, dtyps, idx, terms) =
boehmes@39298
   284
  let
boehmes@39298
   285
    val f = string_of_index func_prefix idx
boehmes@39298
   286
    val terms' = Termtab.update (revert_types t, (f, ss)) terms
boehmes@39298
   287
  in (f, (Tidx, typs, dtyps, idx+1, terms')) end
boehmes@39298
   288
boehmes@39298
   289
fun fresh_fun func_prefix t ss (cx as (_, _, _, _, terms)) =
boehmes@36898
   290
  (case Termtab.lookup terms t of
boehmes@36898
   291
    SOME (f, _) => (f, cx)
boehmes@39298
   292
  | NONE => new_fun func_prefix t ss cx)
boehmes@39298
   293
boehmes@39298
   294
fun inst_const f Ts t =
boehmes@39298
   295
  let
boehmes@39298
   296
    val (n, T) = Term.dest_Const (snd (Type.varify_global [] t))
boehmes@39298
   297
    val inst = map Term.dest_TVar (snd (Term.dest_Type (f T))) ~~ Ts
boehmes@39298
   298
  in Const (n, Term_Subst.instantiateT inst T) end
boehmes@39298
   299
boehmes@39298
   300
fun inst_constructor Ts = inst_const Term.body_type Ts
boehmes@39298
   301
fun inst_selector Ts = inst_const Term.domain_type Ts
boehmes@39298
   302
boehmes@39298
   303
fun lookup_datatype ctxt n Ts = (* FIXME: use Datatype/Record.get_info *)
boehmes@39298
   304
  if n = @{type_name prod}
boehmes@39298
   305
  then SOME [
boehmes@39298
   306
    (Type (n, Ts), [
boehmes@39298
   307
      (inst_constructor Ts @{term Pair},
boehmes@39298
   308
        map (inst_selector Ts) [@{term fst}, @{term snd}])])]
boehmes@39298
   309
  else if n = @{type_name list}
boehmes@39298
   310
  then SOME [
boehmes@39298
   311
    (Type (n, Ts), [
boehmes@39298
   312
      (inst_constructor Ts @{term Nil}, []),
boehmes@39298
   313
      (inst_constructor Ts @{term Cons},
boehmes@39298
   314
        map (inst_selector Ts) [@{term hd}, @{term tl}])])]
boehmes@39298
   315
  else NONE
boehmes@36898
   316
boehmes@36898
   317
fun relaxed thms = (([], thms), map prop_of thms)
boehmes@36898
   318
boehmes@36899
   319
fun with_context header f (ths, ts) =
boehmes@36898
   320
  let val (us, context) = fold_map f ts empty_context
boehmes@36899
   321
  in ((make_sign (header ts) context, us), make_recon ths context) end
boehmes@36898
   322
boehmes@36898
   323
boehmes@36899
   324
fun translate {prefixes, strict, header, builtins, serialize} ctxt comments =
boehmes@36898
   325
  let
boehmes@36898
   326
    val {sort_prefix, func_prefix} = prefixes
boehmes@39298
   327
    val {builtin_typ, builtin_num, builtin_fun, has_datatypes} = builtins
boehmes@39298
   328
boehmes@39298
   329
    fun transT (T as TFree _) = fresh_typ T (new_typ sort_prefix true)
boehmes@39298
   330
      | transT (T as TVar _) = (fn _ => raise TYPE ("smt_translate", [T], []))
boehmes@39298
   331
      | transT (T as Type (n, Ts)) =
boehmes@39298
   332
          (case builtin_typ ctxt T of
boehmes@39298
   333
            SOME n => pair n
boehmes@39298
   334
          | NONE => fresh_typ T (fn _ => fn cx =>
boehmes@39298
   335
              if not has_datatypes then new_typ sort_prefix true T cx
boehmes@39298
   336
              else
boehmes@39298
   337
                (case lookup_datatype ctxt n Ts of
boehmes@39298
   338
                  NONE => new_typ sort_prefix true T cx
boehmes@39298
   339
                | SOME dts =>
boehmes@39298
   340
                    let val cx' = new_dtyps dts cx 
boehmes@39298
   341
                    in (fst (the (lookup_typ cx' T)), cx') end)))
boehmes@36898
   342
boehmes@39298
   343
    and new_dtyps dts cx =
boehmes@39298
   344
      let
boehmes@39298
   345
        fun new_decl i t =
boehmes@39298
   346
          let val (Ts, T) = dest_funT i (Term.fastype_of t)
boehmes@39298
   347
          in
boehmes@39298
   348
            fold_map transT Ts ##>> transT T ##>>
boehmes@39298
   349
            new_fun func_prefix t NONE #>> swap
boehmes@39298
   350
          end
boehmes@39298
   351
        fun new_dtyp_decl (con, sels) =
boehmes@39298
   352
          new_decl (length sels) con ##>> fold_map (new_decl 1) sels #>>
boehmes@39298
   353
          (fn ((con', _), sels') => (con', map (apsnd snd) sels'))
boehmes@39298
   354
      in
boehmes@39298
   355
        cx
boehmes@39298
   356
        |> fold_map (new_typ sort_prefix false o fst) dts
boehmes@39298
   357
        ||>> fold_map (fold_map new_dtyp_decl o snd) dts
boehmes@39298
   358
        |-> (fn (ss, decls) => fn (Tidx, typs, dtyps, idx, terms) =>
boehmes@39298
   359
              (Tidx, typs, (ss ~~ decls) :: dtyps, idx, terms))
boehmes@39298
   360
      end
boehmes@36898
   361
boehmes@36898
   362
    fun app n ts = SApp (n, ts)
boehmes@36898
   363
boehmes@36898
   364
    fun trans t =
boehmes@36898
   365
      (case Term.strip_comb t of
boehmes@36898
   366
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   367
          (case dest_quant qn T t1 of
boehmes@36898
   368
            SOME (q, Ts, ps, b) =>
boehmes@36898
   369
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@36898
   370
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
boehmes@36898
   371
          | NONE => raise TERM ("intermediate", [t]))
boehmes@36898
   372
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   373
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   374
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@36898
   375
      | (h as Const (c as (@{const_name distinct}, T)), [t1]) =>
boehmes@36899
   376
          (case builtin_fun ctxt c (HOLogic.dest_list t1) of
boehmes@36899
   377
            SOME (n, ts) => fold_map trans ts #>> app n
boehmes@36898
   378
          | NONE => transs h T [t1])
boehmes@36898
   379
      | (h as Const (c as (_, T)), ts) =>
boehmes@36898
   380
          (case try HOLogic.dest_number t of
boehmes@36898
   381
            SOME (T, i) =>
boehmes@36899
   382
              (case builtin_num ctxt T i of
boehmes@36899
   383
                SOME n => pair (SApp (n, []))
boehmes@36898
   384
              | NONE => transs t T [])
boehmes@36898
   385
          | NONE =>
boehmes@36899
   386
              (case builtin_fun ctxt c ts of
boehmes@36899
   387
                SOME (n, ts') => fold_map trans ts' #>> app n
boehmes@36898
   388
              | NONE => transs h T ts))
boehmes@36898
   389
      | (h as Free (_, T), ts) => transs h T ts
boehmes@36898
   390
      | (Bound i, []) => pair (SVar i)
boehmes@39298
   391
      | _ => raise TERM ("smt_translate", [t]))
boehmes@36898
   392
boehmes@36898
   393
    and transs t T ts =
boehmes@36898
   394
      let val (Us, U) = dest_funT (length ts) T
boehmes@36898
   395
      in
boehmes@36898
   396
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@39298
   397
        fresh_fun func_prefix t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   398
      end
boehmes@36898
   399
  in
boehmes@36899
   400
    (case strict of SOME strct => strictify strct ctxt | NONE => relaxed) #>
boehmes@36899
   401
    with_context (header ctxt) trans #>> uncurry (serialize comments)
boehmes@36898
   402
  end
boehmes@36898
   403
boehmes@36898
   404
end