src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Fri Oct 29 18:17:04 2010 +0200 (2010-10-29)
changeset 40274 6486c610a549
parent 40161 539d07b00e5f
child 40579 98ebd2300823
permissions -rw-r--r--
introduced SMT.distinct as a representation of the solvers' built-in predicate; check that SMT.distinct is always applied to an explicit list
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@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@40161
   104
  assms: (int * 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@40274
   236
      | (Const (c as (@{const_name SMT.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@40161
   247
    map (apsnd (normalize ctxt)) #> (fn irules =>
boehmes@40161
   248
    ((unfold_rules, (~1, term_bool') :: irules),
boehmes@40161
   249
     map (in_form o prop_of o snd) ((~1, term_bool) :: irules)))
boehmes@36898
   250
  end
boehmes@36898
   251
boehmes@36898
   252
boehmes@36898
   253
boehmes@36898
   254
(* translation from Isabelle terms into SMT intermediate terms *)
boehmes@36898
   255
boehmes@39298
   256
val empty_context = (1, Typtab.empty, [], 1, Termtab.empty)
boehmes@36898
   257
boehmes@39298
   258
fun make_sign header (_, typs, dtyps, _, terms) = {
boehmes@36899
   259
  header = header,
boehmes@39298
   260
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
boehmes@39298
   261
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms [],
boehmes@39435
   262
  dtyps = rev dtyps }
boehmes@36898
   263
boehmes@39298
   264
fun make_recon (unfolds, assms) (_, typs, _, _, terms) = {
boehmes@39298
   265
  typs = Symtab.make (map (apfst fst o swap) (Typtab.dest typs)),
boehmes@39298
   266
    (*FIXME: don't drop the datatype information! *)
boehmes@36898
   267
  terms = Symtab.make (map (fn (t, (n, _)) => (n, t)) (Termtab.dest terms)),
boehmes@36898
   268
  unfolds = unfolds,
boehmes@36899
   269
  assms = assms }
boehmes@36898
   270
boehmes@36898
   271
fun string_of_index pre i = pre ^ string_of_int i
boehmes@36898
   272
boehmes@39298
   273
fun new_typ sort_prefix proper T (Tidx, typs, dtyps, idx, terms) =
boehmes@39298
   274
  let val s = string_of_index sort_prefix Tidx
boehmes@39298
   275
  in (s, (Tidx+1, Typtab.update (T, (s, proper)) typs, dtyps, idx, terms)) end
boehmes@39298
   276
boehmes@39298
   277
fun lookup_typ (_, typs, _, _, _) = Typtab.lookup typs
boehmes@36898
   278
boehmes@39298
   279
fun fresh_typ T f cx =
boehmes@39298
   280
  (case lookup_typ cx T of
boehmes@39298
   281
    SOME (s, _) => (s, cx)
boehmes@39298
   282
  | NONE => f T cx)
boehmes@39298
   283
boehmes@39298
   284
fun new_fun func_prefix t ss (Tidx, typs, dtyps, idx, terms) =
boehmes@39298
   285
  let
boehmes@39298
   286
    val f = string_of_index func_prefix idx
boehmes@39298
   287
    val terms' = Termtab.update (revert_types t, (f, ss)) terms
boehmes@39298
   288
  in (f, (Tidx, typs, dtyps, idx+1, terms')) end
boehmes@39298
   289
boehmes@39298
   290
fun fresh_fun func_prefix t ss (cx as (_, _, _, _, terms)) =
boehmes@36898
   291
  (case Termtab.lookup terms t of
boehmes@36898
   292
    SOME (f, _) => (f, cx)
boehmes@39298
   293
  | NONE => new_fun func_prefix t ss cx)
boehmes@39298
   294
boehmes@39483
   295
fun mk_type (_, Tfs) (d as Datatype.DtTFree _) = the (AList.lookup (op =) Tfs d)
boehmes@39483
   296
  | mk_type Ts (Datatype.DtType (n, ds)) = Type (n, map (mk_type Ts) ds)
boehmes@39483
   297
  | mk_type (Tds, _) (Datatype.DtRec i) = nth Tds i
boehmes@39298
   298
boehmes@39483
   299
fun mk_selector ctxt Ts T n (i, d) =
boehmes@39483
   300
  (case Datatype_Selectors.lookup_selector ctxt (n, i+1) of
boehmes@39483
   301
    NONE => raise Fail ("missing selector for datatype constructor " ^ quote n)
boehmes@39483
   302
  | SOME m => mk_type Ts d |> (fn U => (Const (m, T --> U), U)))
boehmes@39483
   303
boehmes@39483
   304
fun mk_constructor ctxt Ts T (n, args) =
boehmes@39483
   305
  let val (sels, Us) = split_list (map_index (mk_selector ctxt Ts T n) args)
boehmes@39483
   306
  in (Const (n, Us ---> T), sels) end
boehmes@39298
   307
boehmes@39535
   308
fun lookup_datatype ctxt n Ts =
boehmes@39535
   309
  if member (op =) [@{type_name bool}, @{type_name nat}] n then NONE
boehmes@39535
   310
  else
boehmes@39535
   311
    Datatype.get_info (ProofContext.theory_of ctxt) n
boehmes@39535
   312
    |> Option.map (fn {descr, ...} =>
boehmes@39535
   313
         let
boehmes@39535
   314
           val Tds = map (fn (_, (tn, _, _)) => Type (tn, Ts))
boehmes@39535
   315
             (sort (int_ord o pairself fst) descr)
boehmes@39535
   316
           val Tfs = (case hd descr of (_, (_, tfs, _)) => tfs ~~ Ts)
boehmes@39535
   317
         in
boehmes@39535
   318
           descr |> map (fn (i, (_, _, cs)) =>
boehmes@39535
   319
             (nth Tds i, map (mk_constructor ctxt (Tds, Tfs) (nth Tds i)) cs))
boehmes@39535
   320
         end)
boehmes@36898
   321
boehmes@40161
   322
fun relaxed irules = (([], irules), map (prop_of o snd) irules)
boehmes@36898
   323
boehmes@36899
   324
fun with_context header f (ths, ts) =
boehmes@36898
   325
  let val (us, context) = fold_map f ts empty_context
boehmes@36899
   326
  in ((make_sign (header ts) context, us), make_recon ths context) end
boehmes@36898
   327
boehmes@36898
   328
boehmes@36899
   329
fun translate {prefixes, strict, header, builtins, serialize} ctxt comments =
boehmes@36898
   330
  let
boehmes@36898
   331
    val {sort_prefix, func_prefix} = prefixes
boehmes@39298
   332
    val {builtin_typ, builtin_num, builtin_fun, has_datatypes} = builtins
boehmes@39298
   333
boehmes@39298
   334
    fun transT (T as TFree _) = fresh_typ T (new_typ sort_prefix true)
boehmes@39298
   335
      | transT (T as TVar _) = (fn _ => raise TYPE ("smt_translate", [T], []))
boehmes@39298
   336
      | transT (T as Type (n, Ts)) =
boehmes@39298
   337
          (case builtin_typ ctxt T of
boehmes@39298
   338
            SOME n => pair n
boehmes@39298
   339
          | NONE => fresh_typ T (fn _ => fn cx =>
boehmes@39298
   340
              if not has_datatypes then new_typ sort_prefix true T cx
boehmes@39298
   341
              else
boehmes@39298
   342
                (case lookup_datatype ctxt n Ts of
boehmes@39298
   343
                  NONE => new_typ sort_prefix true T cx
boehmes@39298
   344
                | SOME dts =>
boehmes@39298
   345
                    let val cx' = new_dtyps dts cx 
boehmes@39298
   346
                    in (fst (the (lookup_typ cx' T)), cx') end)))
boehmes@36898
   347
boehmes@39298
   348
    and new_dtyps dts cx =
boehmes@39298
   349
      let
boehmes@39298
   350
        fun new_decl i t =
boehmes@39298
   351
          let val (Ts, T) = dest_funT i (Term.fastype_of t)
boehmes@39298
   352
          in
boehmes@39298
   353
            fold_map transT Ts ##>> transT T ##>>
boehmes@39298
   354
            new_fun func_prefix t NONE #>> swap
boehmes@39298
   355
          end
boehmes@39298
   356
        fun new_dtyp_decl (con, sels) =
boehmes@39298
   357
          new_decl (length sels) con ##>> fold_map (new_decl 1) sels #>>
boehmes@39298
   358
          (fn ((con', _), sels') => (con', map (apsnd snd) sels'))
boehmes@39298
   359
      in
boehmes@39298
   360
        cx
boehmes@39298
   361
        |> fold_map (new_typ sort_prefix false o fst) dts
boehmes@39298
   362
        ||>> fold_map (fold_map new_dtyp_decl o snd) dts
boehmes@39298
   363
        |-> (fn (ss, decls) => fn (Tidx, typs, dtyps, idx, terms) =>
boehmes@39298
   364
              (Tidx, typs, (ss ~~ decls) :: dtyps, idx, terms))
boehmes@39298
   365
      end
boehmes@36898
   366
boehmes@36898
   367
    fun app n ts = SApp (n, ts)
boehmes@36898
   368
boehmes@36898
   369
    fun trans t =
boehmes@36898
   370
      (case Term.strip_comb t of
boehmes@36898
   371
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   372
          (case dest_quant qn T t1 of
boehmes@36898
   373
            SOME (q, Ts, ps, b) =>
boehmes@36898
   374
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@36898
   375
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
boehmes@36898
   376
          | NONE => raise TERM ("intermediate", [t]))
boehmes@36898
   377
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   378
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   379
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@40274
   380
      | (h as Const (c as (@{const_name SMT.distinct}, T)), [t1]) =>
boehmes@36899
   381
          (case builtin_fun ctxt c (HOLogic.dest_list t1) of
boehmes@36899
   382
            SOME (n, ts) => fold_map trans ts #>> app n
boehmes@36898
   383
          | NONE => transs h T [t1])
boehmes@36898
   384
      | (h as Const (c as (_, T)), ts) =>
boehmes@36898
   385
          (case try HOLogic.dest_number t of
boehmes@36898
   386
            SOME (T, i) =>
boehmes@36899
   387
              (case builtin_num ctxt T i of
boehmes@36899
   388
                SOME n => pair (SApp (n, []))
boehmes@36898
   389
              | NONE => transs t T [])
boehmes@36898
   390
          | NONE =>
boehmes@36899
   391
              (case builtin_fun ctxt c ts of
boehmes@36899
   392
                SOME (n, ts') => fold_map trans ts' #>> app n
boehmes@36898
   393
              | NONE => transs h T ts))
boehmes@36898
   394
      | (h as Free (_, T), ts) => transs h T ts
boehmes@36898
   395
      | (Bound i, []) => pair (SVar i)
boehmes@39298
   396
      | _ => raise TERM ("smt_translate", [t]))
boehmes@36898
   397
boehmes@36898
   398
    and transs t T ts =
boehmes@36898
   399
      let val (Us, U) = dest_funT (length ts) T
boehmes@36898
   400
      in
boehmes@36898
   401
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@39298
   402
        fresh_fun func_prefix t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   403
      end
boehmes@36898
   404
  in
boehmes@36899
   405
    (case strict of SOME strct => strictify strct ctxt | NONE => relaxed) #>
boehmes@36899
   406
    with_context (header ctxt) trans #>> uncurry (serialize comments)
boehmes@36898
   407
  end
boehmes@36898
   408
boehmes@36898
   409
end