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