src/HOL/Tools/SMT/smt_translate.ML
author paulson
Thu Dec 16 12:05:00 2010 +0000 (2010-12-16)
changeset 41193 dc33b8ea4526
parent 41165 ceb81a08534e
child 41194 9796e5e01b61
permissions -rw-r--r--
made sml/nj happy
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@41123
     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@41127
    18
  (*translation configuration*)
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@41127
    27
    header: 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@41127
    32
    context: Proof.context,
boehmes@36898
    33
    typs: typ Symtab.table,
boehmes@36898
    34
    terms: term Symtab.table,
boehmes@41127
    35
    rewrite_rules: thm list,
boehmes@40161
    36
    assms: (int * thm) list }
boehmes@36898
    37
boehmes@41127
    38
  (*translation*)
boehmes@41127
    39
  val add_config: SMT_Utils.class * (Proof.context -> config) ->
boehmes@41127
    40
    Context.generic -> Context.generic 
boehmes@41127
    41
  val translate: Proof.context -> string list -> (int * thm) list ->
boehmes@36898
    42
    string * recon
boehmes@36898
    43
end
boehmes@36898
    44
boehmes@36898
    45
structure SMT_Translate: SMT_TRANSLATE =
boehmes@36898
    46
struct
boehmes@36898
    47
boehmes@40663
    48
structure U = SMT_Utils
boehmes@41059
    49
structure B = SMT_Builtin
boehmes@40663
    50
boehmes@40663
    51
boehmes@36898
    52
(* intermediate term structure *)
boehmes@36898
    53
boehmes@36898
    54
datatype squant = SForall | SExists
boehmes@36898
    55
boehmes@36898
    56
datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
boehmes@36898
    57
boehmes@36898
    58
datatype sterm =
boehmes@36898
    59
  SVar of int |
boehmes@36898
    60
  SApp of string * sterm list |
boehmes@36898
    61
  SLet of string * sterm * sterm |
boehmes@40664
    62
  SQua of squant * string list * sterm spattern list * int option * sterm
boehmes@36898
    63
boehmes@36898
    64
boehmes@36898
    65
boehmes@41127
    66
(* translation configuration *)
boehmes@36898
    67
boehmes@36898
    68
type prefixes = {sort_prefix: string, func_prefix: string}
boehmes@36898
    69
boehmes@36898
    70
type sign = {
boehmes@36899
    71
  header: string list,
boehmes@36898
    72
  sorts: string list,
boehmes@39298
    73
  dtyps: (string * (string * (string * string) list) list) list list,
boehmes@36898
    74
  funcs: (string * (string list * string)) list }
boehmes@36898
    75
boehmes@36898
    76
type config = {
boehmes@36898
    77
  prefixes: prefixes,
boehmes@41127
    78
  header: term list -> string list,
boehmes@41059
    79
  is_fol: bool,
boehmes@41059
    80
  has_datatypes: bool,
boehmes@36898
    81
  serialize: string list -> sign -> sterm list -> string }
boehmes@36898
    82
boehmes@36898
    83
type recon = {
boehmes@41127
    84
  context: Proof.context,
boehmes@36898
    85
  typs: typ Symtab.table,
boehmes@36898
    86
  terms: term Symtab.table,
boehmes@41127
    87
  rewrite_rules: thm list,
boehmes@40161
    88
  assms: (int * thm) list }
boehmes@36898
    89
boehmes@36898
    90
boehmes@36898
    91
boehmes@41127
    92
(* translation context *)
boehmes@41127
    93
boehmes@41127
    94
fun make_tr_context {sort_prefix, func_prefix} =
boehmes@41127
    95
  (sort_prefix, 1, Typtab.empty, func_prefix, 1, Termtab.empty)
boehmes@41127
    96
boehmes@41127
    97
fun string_of_index pre i = pre ^ string_of_int i
boehmes@41127
    98
boehmes@41127
    99
fun add_typ T proper (cx as (sp, Tidx, typs, fp, idx, terms)) =
boehmes@41127
   100
  (case Typtab.lookup typs T of
boehmes@41127
   101
    SOME (n, _) => (n, cx)
boehmes@41127
   102
  | NONE =>
boehmes@41127
   103
      let
boehmes@41127
   104
        val n = string_of_index sp Tidx
boehmes@41127
   105
        val typs' = Typtab.update (T, (n, proper)) typs
boehmes@41127
   106
      in (n, (sp, Tidx+1, typs', fp, idx, terms)) end)
boehmes@41127
   107
boehmes@41127
   108
fun add_fun t sort (cx as (sp, Tidx, typs, fp, idx, terms)) =
boehmes@41127
   109
  (case Termtab.lookup terms t of
boehmes@41127
   110
    SOME (n, _) => (n, cx)
boehmes@41127
   111
  | NONE => 
boehmes@41127
   112
      let
boehmes@41127
   113
        val n = string_of_index fp idx
boehmes@41127
   114
        val terms' = Termtab.update (t, (n, sort)) terms
boehmes@41127
   115
      in (n, (sp, Tidx, typs, fp, idx+1, terms')) end)
boehmes@41127
   116
boehmes@41127
   117
fun sign_of header dtyps (_, _, typs, _, _, terms) = {
boehmes@41127
   118
  header = header,
boehmes@41127
   119
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
boehmes@41127
   120
  dtyps = dtyps,
boehmes@41127
   121
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
boehmes@41127
   122
boehmes@41127
   123
fun recon_of ctxt rules thms ithms revertT revert (_, _, typs, _, _, terms) =
boehmes@41127
   124
  let
boehmes@41127
   125
    fun add_typ (T, (n, _)) = Symtab.update (n, revertT T)
boehmes@41127
   126
    val typs' = Typtab.fold add_typ typs Symtab.empty
boehmes@41127
   127
boehmes@41127
   128
    fun add_fun (t, (n, _)) = Symtab.update (n, revert t)
boehmes@41127
   129
    val terms' = Termtab.fold add_fun terms Symtab.empty
boehmes@41127
   130
boehmes@41127
   131
    val assms = map (pair ~1) thms @ ithms
boehmes@41127
   132
  in
boehmes@41127
   133
    {context=ctxt, typs=typs', terms=terms', rewrite_rules=rules, assms=assms}
boehmes@41127
   134
  end
boehmes@41127
   135
boehmes@41127
   136
boehmes@41127
   137
boehmes@41127
   138
(* preprocessing *)
boehmes@41127
   139
boehmes@41127
   140
(** mark built-in constants as Var **)
boehmes@41127
   141
boehmes@41127
   142
fun mark_builtins ctxt =
boehmes@41127
   143
  let
boehmes@41127
   144
    (*
boehmes@41127
   145
      Note: schematic terms cannot occur anymore in terms at this stage.
boehmes@41127
   146
    *)
boehmes@41127
   147
    fun mark t =
boehmes@41127
   148
      (case Term.strip_comb t of
boehmes@41127
   149
        (u as Const (@{const_name If}, _), ts) => marks u ts
boehmes@41127
   150
      | (u as Const c, ts) =>
boehmes@41127
   151
          (case B.builtin_num ctxt t of
boehmes@41127
   152
            SOME (n, T) =>
boehmes@41127
   153
              let val v = ((n, 0), T)
boehmes@41127
   154
              in Vartab.update v #> pair (Var v) end
boehmes@41127
   155
          | NONE =>
boehmes@41127
   156
              (case B.builtin_fun ctxt c ts of
boehmes@41127
   157
                SOME ((ni, T), us, U) =>
boehmes@41127
   158
                  Vartab.update (ni, U) #> marks (Var (ni, T)) us
boehmes@41127
   159
              | NONE => marks u ts))
boehmes@41127
   160
      | (Abs (n, T, u), ts) => mark u #-> (fn u' => marks (Abs (n, T, u')) ts)
boehmes@41127
   161
      | (u, ts) => marks u ts)
boehmes@41127
   162
 
boehmes@41127
   163
    and marks t ts = fold_map mark ts #>> Term.list_comb o pair t
boehmes@41127
   164
boehmes@41127
   165
  in (fn ts => swap (fold_map mark ts Vartab.empty)) end
boehmes@41127
   166
boehmes@41127
   167
fun mark_builtins' ctxt t = hd (snd (mark_builtins ctxt [t]))
boehmes@41127
   168
boehmes@41127
   169
boehmes@41127
   170
(** FIXME **)
boehmes@41127
   171
boehmes@41127
   172
local
boehmes@41127
   173
  (*
boehmes@41127
   174
    mark constructors and selectors as Vars (forcing eta-expansion),
boehmes@41127
   175
    add missing datatype selectors via hypothetical definitions,
boehmes@41127
   176
    also return necessary datatype and record theorems
boehmes@41127
   177
  *)
boehmes@41127
   178
in
boehmes@41127
   179
boehmes@41127
   180
fun collect_datatypes_and_records (tr_context, ctxt) ts =
boehmes@41127
   181
  (([], tr_context, ctxt), ts)
boehmes@41127
   182
boehmes@41127
   183
end
boehmes@41127
   184
boehmes@41127
   185
boehmes@41127
   186
(** eta-expand quantifiers, let expressions and built-ins *)
boehmes@41127
   187
boehmes@41127
   188
local
boehmes@41127
   189
  fun eta T t = Abs (Name.uu, T, Term.incr_boundvars 1 t $ Bound 0)
boehmes@41127
   190
boehmes@41127
   191
  fun exp T = eta (Term.domain_type (Term.domain_type T))
boehmes@41127
   192
boehmes@41127
   193
  fun exp2 T q =
boehmes@41127
   194
    let val U = Term.domain_type T
boehmes@41127
   195
    in Abs (Name.uu, U, q $ eta (Term.domain_type U) (Bound 0)) end
boehmes@41127
   196
boehmes@41127
   197
  fun exp2' T l =
boehmes@41127
   198
    let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
boehmes@41127
   199
    in Abs (Name.uu, U1, eta U2 (l $ Bound 0)) end
boehmes@41127
   200
boehmes@41127
   201
  fun expf t i T ts =
boehmes@41127
   202
    let val Ts = U.dest_funT i T |> fst |> drop (length ts)
boehmes@41127
   203
    in Term.list_comb (t, ts) |> fold_rev eta Ts end
boehmes@41127
   204
boehmes@41127
   205
  fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
boehmes@41127
   206
    | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp T t
boehmes@41127
   207
    | expand (q as Const (@{const_name All}, T)) = exp2 T q
boehmes@41127
   208
    | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
boehmes@41127
   209
    | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp T t
boehmes@41127
   210
    | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
boehmes@41127
   211
    | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
boehmes@41127
   212
        l $ expand t $ abs_expand a
boehmes@41127
   213
    | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
boehmes@41127
   214
        l $ expand t $ exp (Term.range_type T) u
boehmes@41127
   215
    | expand ((l as Const (@{const_name Let}, T)) $ t) = exp2 T (l $ expand t)
boehmes@41127
   216
    | expand (l as Const (@{const_name Let}, T)) = exp2' T l
boehmes@41127
   217
    | expand (Abs a) = abs_expand a
boehmes@41127
   218
    | expand t =
boehmes@41127
   219
        (case Term.strip_comb t of
boehmes@41127
   220
          (u as Const (@{const_name If}, T), ts) => expf u 3 T (map expand ts)
boehmes@41127
   221
        | (u as Var ((_, i), T), ts) => expf u i T (map expand ts)
boehmes@41127
   222
        | (u, ts) => Term.list_comb (u, map expand ts))
boehmes@41127
   223
boehmes@41127
   224
  and abs_expand (n, T, t) = Abs (n, T, expand t)
boehmes@41127
   225
in
boehmes@41127
   226
boehmes@41127
   227
val eta_expand = map expand
boehmes@41127
   228
boehmes@41127
   229
end
boehmes@41127
   230
boehmes@41127
   231
boehmes@41127
   232
(** lambda-lifting **)
boehmes@41127
   233
boehmes@41127
   234
local
boehmes@41127
   235
  fun mk_def Ts T lhs rhs =
boehmes@41127
   236
    let
boehmes@41127
   237
      val eq = HOLogic.eq_const T $ lhs $ rhs
boehmes@41127
   238
      val trigger =
boehmes@41127
   239
        [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
boehmes@41127
   240
        |> map (HOLogic.mk_list @{typ SMT.pattern})
boehmes@41127
   241
        |> HOLogic.mk_list @{typ "SMT.pattern list"}
boehmes@41127
   242
      fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
boehmes@41127
   243
    in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
boehmes@41127
   244
boehmes@41127
   245
  fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
boehmes@41127
   246
    let
boehmes@41127
   247
      val T = Term.fastype_of1 (Us @ Ts, t)
boehmes@41127
   248
      val lev = length Us
boehmes@41127
   249
      val bs = sort int_ord (Term.add_loose_bnos (t, lev, []))
boehmes@41127
   250
      val bss = map_index (fn (i, j) => (j, Integer.add lev i)) bs
boehmes@41127
   251
      val norm = perhaps (AList.lookup (op =) bss)
boehmes@41127
   252
      val t' = Term.map_aterms (fn Bound i => Bound (norm i) | t => t) t
boehmes@41127
   253
      val Ts' = map (nth Ts) bs
boehmes@41127
   254
boehmes@41127
   255
      fun mk_abs U u = Abs (Name.uu, U, u)
boehmes@41127
   256
      val abs_rhs = fold mk_abs Ts' (fold mk_abs Us t')
boehmes@41127
   257
    in
boehmes@41127
   258
      (case Termtab.lookup defs abs_rhs of
boehmes@41127
   259
        SOME (f, _) => (Term.list_comb (f, map Bound bs), cx)
boehmes@41127
   260
      | NONE =>
boehmes@41127
   261
          let
boehmes@41127
   262
            val (n, ctxt') =
boehmes@41127
   263
              yield_singleton Variable.variant_fixes Name.uu ctxt
boehmes@41127
   264
            val f = Free (n, rev Ts' ---> (rev Us ---> T))
boehmes@41127
   265
            fun mk_bapp i t = t $ Bound i
boehmes@41127
   266
            val lhs =
boehmes@41127
   267
              f
boehmes@41127
   268
              |> fold_rev (mk_bapp o snd) bss
boehmes@41127
   269
              |> fold_rev mk_bapp (0 upto (length Us - 1))
boehmes@41127
   270
            val def = mk_def (Us @ Ts') T lhs t'
boehmes@41127
   271
          in (f, (Termtab.update (abs_rhs, (f, def)) defs, ctxt')) end)
boehmes@41127
   272
    end
boehmes@41127
   273
boehmes@41127
   274
  fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
boehmes@41127
   275
    | dest_abs Ts t = (Ts, t)
boehmes@41127
   276
boehmes@41127
   277
  fun traverse Ts t =
boehmes@41127
   278
    (case t of
boehmes@41127
   279
      (q as Const (@{const_name All}, _)) $ Abs a =>
boehmes@41127
   280
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   281
    | (q as Const (@{const_name Ex}, _)) $ Abs a =>
boehmes@41127
   282
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   283
    | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
boehmes@41127
   284
        traverse Ts u ##>> abs_traverse Ts a #>>
boehmes@41127
   285
        (fn (u', a') => l $ u' $ Abs a')
boehmes@41127
   286
    | Abs _ =>
boehmes@41127
   287
        let val (Us, u) = dest_abs [] t
boehmes@41127
   288
        in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
boehmes@41127
   289
    | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
boehmes@41127
   290
    | _ => pair t)
boehmes@41127
   291
boehmes@41127
   292
  and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
boehmes@41127
   293
in
boehmes@41127
   294
boehmes@41127
   295
fun lift_lambdas ctxt ts =
boehmes@41127
   296
  (Termtab.empty, ctxt)
boehmes@41127
   297
  |> fold_map (traverse []) ts
boehmes@41127
   298
  |> (fn (us, (defs, ctxt')) =>
boehmes@41127
   299
       (ctxt', Termtab.fold (cons o snd o snd) defs us))
boehmes@41127
   300
boehmes@41127
   301
end
boehmes@41127
   302
boehmes@41127
   303
boehmes@41127
   304
(** introduce explicit applications **)
boehmes@41127
   305
boehmes@41127
   306
local
boehmes@41127
   307
  (*
boehmes@41127
   308
    Make application explicit for functions with varying number of arguments.
boehmes@41127
   309
  *)
boehmes@41127
   310
boehmes@41127
   311
  fun add t ts =
boehmes@41127
   312
    Termtab.map_default (t, []) (Ord_List.insert int_ord (length ts))
boehmes@41127
   313
boehmes@41127
   314
  fun collect t =
boehmes@41127
   315
    (case Term.strip_comb t of
boehmes@41127
   316
      (u as Const _, ts) => add u ts #> fold collect ts
boehmes@41127
   317
    | (u as Free _, ts) => add u ts #> fold collect ts
boehmes@41127
   318
    | (Abs (_, _, u), ts) => collect u #> fold collect ts
boehmes@41127
   319
    | (_, ts) => fold collect ts)
boehmes@41127
   320
boehmes@41127
   321
  fun app ts (t, T) =
boehmes@41127
   322
    let val f = Const (@{const_name SMT.fun_app}, T --> T)
boehmes@41127
   323
    in (Term.list_comb (f $ t, ts), snd (U.dest_funT (length ts) T)) end 
boehmes@41127
   324
boehmes@41127
   325
  fun appl _ _ [] = fst
boehmes@41127
   326
    | appl _ [] ts = fst o app ts
boehmes@41127
   327
    | appl i (k :: ks) ts =
boehmes@41127
   328
        let val (ts1, ts2) = chop (k - i) ts
boehmes@41127
   329
        in appl k ks ts2 o app ts1 end
boehmes@41127
   330
boehmes@41127
   331
  fun appl0 [_] ts (t, _) = Term.list_comb (t, ts)
boehmes@41127
   332
    | appl0 (0 :: ks) ts tT = appl 0 ks ts tT
boehmes@41127
   333
    | appl0 ks ts tT = appl 0 ks ts tT
boehmes@41127
   334
boehmes@41127
   335
  fun apply terms T t ts = appl0 (Termtab.lookup_list terms t) ts (t, T)
boehmes@41127
   336
boehmes@41127
   337
  fun get_arities i t =
boehmes@41127
   338
    (case Term.strip_comb t of
boehmes@41127
   339
      (Bound j, ts) =>
boehmes@41127
   340
        (if i = j then Ord_List.insert int_ord (length ts) else I) #>
boehmes@41127
   341
        fold (get_arities i) ts
boehmes@41127
   342
    | (Abs (_, _, u), ts) => get_arities (i+1) u #> fold (get_arities i) ts
boehmes@41127
   343
    | (_, ts) => fold (get_arities i) ts)
boehmes@41127
   344
in
boehmes@41127
   345
boehmes@41127
   346
fun intro_explicit_application ts =
boehmes@41127
   347
  let
boehmes@41127
   348
    val terms = fold collect ts Termtab.empty
boehmes@41127
   349
boehmes@41127
   350
    fun traverse (env as (arities, Ts)) t =
boehmes@41127
   351
      (case Term.strip_comb t of
boehmes@41127
   352
        (u as Const (_, T), ts) => apply terms T u (map (traverse env) ts)
boehmes@41127
   353
      | (u as Free (_, T), ts) => apply terms T u (map (traverse env) ts)
boehmes@41127
   354
      | (u as Bound i, ts) =>
boehmes@41127
   355
          appl0 (nth arities i) (map (traverse env) ts) (u, nth Ts i)
boehmes@41127
   356
      | (Abs (n, T, u), ts) =>
boehmes@41127
   357
          let val env' = (get_arities 0 u [] :: arities, T :: Ts)
boehmes@41127
   358
          in traverses env (Abs (n, T, traverse env' u)) ts end
boehmes@41127
   359
      | (u, ts) => traverses env u ts)
boehmes@41127
   360
    and traverses env t ts = Term.list_comb (t, map (traverse env) ts)
boehmes@41127
   361
  in map (traverse ([], [])) ts end
boehmes@41127
   362
boehmes@41127
   363
val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
boehmes@41127
   364
boehmes@41127
   365
end
boehmes@41127
   366
boehmes@41127
   367
boehmes@41127
   368
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
boehmes@41127
   369
boehmes@41127
   370
val tboolT = @{typ SMT.term_bool}
boehmes@41127
   371
val term_true = Const (@{const_name True}, tboolT)
boehmes@41127
   372
val term_false = Const (@{const_name False}, tboolT)
boehmes@41127
   373
boehmes@41127
   374
val term_bool = @{lemma "True ~= False" by simp}
boehmes@41127
   375
val term_bool_prop =
boehmes@41127
   376
  let
boehmes@41127
   377
    fun replace @{const HOL.eq (bool)} = @{const HOL.eq (SMT.term_bool)}
boehmes@41127
   378
      | replace @{const True} = term_true
boehmes@41127
   379
      | replace @{const False} = term_false
boehmes@41127
   380
      | replace t = t
boehmes@41127
   381
  in
boehmes@41127
   382
    Term.map_aterms replace (HOLogic.dest_Trueprop (Thm.prop_of term_bool))
boehmes@41127
   383
  end
boehmes@41127
   384
boehmes@41127
   385
val fol_rules = [
boehmes@41127
   386
  Let_def,
boehmes@41127
   387
  @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@41127
   388
  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@41127
   389
boehmes@41127
   390
fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
boehmes@41127
   391
      reduce_let (Term.betapply (u, t))
boehmes@41127
   392
  | reduce_let (t $ u) = reduce_let t $ reduce_let u
boehmes@41127
   393
  | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
boehmes@41127
   394
  | reduce_let t = t
boehmes@41127
   395
boehmes@41127
   396
fun is_pred_type NONE = false
boehmes@41127
   397
  | is_pred_type (SOME T) = (Term.body_type T = @{typ bool})
boehmes@41127
   398
boehmes@41127
   399
fun is_conn_type NONE = false
boehmes@41127
   400
  | is_conn_type (SOME T) =
boehmes@41127
   401
      forall (equal @{typ bool}) (Term.body_type T :: Term.binder_types T)
boehmes@41127
   402
boehmes@41127
   403
fun revert_typ @{typ SMT.term_bool} = @{typ bool}
boehmes@41127
   404
  | revert_typ (Type (n, Ts)) = Type (n, map revert_typ Ts)
boehmes@41127
   405
  | revert_typ T = T
boehmes@41127
   406
boehmes@41127
   407
val revert_types = Term.map_types revert_typ
boehmes@41127
   408
boehmes@41127
   409
fun folify ctxt builtins =
boehmes@41127
   410
  let
boehmes@41127
   411
    fun as_term t = @{const HOL.eq (SMT.term_bool)} $ t $ term_true
boehmes@41127
   412
boehmes@41127
   413
    fun as_tbool @{typ bool} = tboolT
boehmes@41127
   414
      | as_tbool (Type (n, Ts)) = Type (n, map as_tbool Ts)
boehmes@41127
   415
      | as_tbool T = T
boehmes@41127
   416
    fun mapTs f g i = U.dest_funT i #> (fn (Ts, T) => map f Ts ---> g T)
boehmes@41127
   417
    fun predT i = mapTs as_tbool I i
boehmes@41127
   418
    fun funcT i = mapTs as_tbool as_tbool i
boehmes@41127
   419
    fun func i (n, T) = (n, funcT i T)
boehmes@41127
   420
boehmes@41127
   421
    fun map_ifT T = T |> Term.dest_funT ||> funcT 2 |> (op -->)
boehmes@41127
   422
    val if_term = @{const If (bool)} |> Term.dest_Const ||> map_ifT |> Const
boehmes@41127
   423
    fun wrap_in_if t = if_term $ t $ term_true $ term_false
boehmes@41127
   424
boehmes@41127
   425
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@41127
   426
boehmes@41127
   427
    fun in_term t =
boehmes@41127
   428
      (case Term.strip_comb t of
boehmes@41127
   429
        (Const (n as @{const_name If}, T), [t1, t2, t3]) =>
boehmes@41127
   430
          Const (n, map_ifT T) $ in_form t1 $ in_term t2 $ in_term t3
boehmes@41127
   431
      | (Const (@{const_name HOL.eq}, _), _) => wrap_in_if (in_form t)
boehmes@41127
   432
      | (Var (ni as (_, i), T), ts) =>
boehmes@41127
   433
          let val U = Vartab.lookup builtins ni
boehmes@41127
   434
          in
boehmes@41127
   435
            if is_conn_type U orelse is_pred_type U then wrap_in_if (in_form t)
boehmes@41127
   436
            else Term.list_comb (Var (ni, funcT i T), map in_term ts)
boehmes@41127
   437
          end
boehmes@41127
   438
      | (Const c, ts) =>
boehmes@41127
   439
          Term.list_comb (Const (func (length ts) c), map in_term ts)
boehmes@41127
   440
      | (Free c, ts) =>
boehmes@41127
   441
          Term.list_comb (Free (func (length ts) c), map in_term ts)
boehmes@41127
   442
      | _ => t)
boehmes@41127
   443
boehmes@41127
   444
    and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
boehmes@41127
   445
      | in_weight t = in_form t 
boehmes@41127
   446
boehmes@41127
   447
    and in_pat (Const (c as (@{const_name pat}, _)) $ t) =
boehmes@41127
   448
          Const (func 1 c) $ in_term t
boehmes@41127
   449
      | in_pat (Const (c as (@{const_name nopat}, _)) $ t) =
boehmes@41127
   450
          Const (func 1 c) $ in_term t
boehmes@41127
   451
      | in_pat t = raise TERM ("bad pattern", [t])
boehmes@41127
   452
boehmes@41127
   453
    and in_pats ps =
boehmes@41127
   454
      in_list @{typ "pattern list"} (in_list @{typ pattern} in_pat) ps
boehmes@41127
   455
boehmes@41127
   456
    and in_trig ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_weight t
boehmes@41127
   457
      | in_trig t = in_weight t
boehmes@41127
   458
boehmes@41127
   459
    and in_form t =
boehmes@41127
   460
      (case Term.strip_comb t of
boehmes@41127
   461
        (q as Const (qn, _), [Abs (n, T, u)]) =>
boehmes@41127
   462
          if member (op =) [@{const_name All}, @{const_name Ex}] qn then
boehmes@41127
   463
            q $ Abs (n, as_tbool T, in_trig u)
boehmes@41127
   464
          else as_term (in_term t)
boehmes@41127
   465
      | (u as Const (@{const_name If}, _), ts) =>
boehmes@41127
   466
          Term.list_comb (u, map in_form ts)
boehmes@41127
   467
      | (b as @{const HOL.eq (bool)}, ts) => Term.list_comb (b, map in_form ts)
boehmes@41127
   468
      | (Const (n as @{const_name HOL.eq}, T), ts) =>
boehmes@41127
   469
          Term.list_comb (Const (n, predT 2 T), map in_term ts)
boehmes@41127
   470
      | (b as Var (ni as (_, i), T), ts) =>
boehmes@41127
   471
          if is_conn_type (Vartab.lookup builtins ni) then
boehmes@41127
   472
            Term.list_comb (b, map in_form ts)
boehmes@41127
   473
          else if is_pred_type (Vartab.lookup builtins ni) then
boehmes@41127
   474
            Term.list_comb (Var (ni, predT i T), map in_term ts)
boehmes@41127
   475
          else as_term (in_term t)
boehmes@41127
   476
      | _ => as_term (in_term t))
boehmes@41127
   477
  in
boehmes@41127
   478
    map (reduce_let #> in_form) #>
boehmes@41127
   479
    cons (mark_builtins' ctxt term_bool_prop) #>
boehmes@41127
   480
    pair (fol_rules, [term_bool])
boehmes@41127
   481
  end
boehmes@41127
   482
boehmes@41127
   483
boehmes@41127
   484
boehmes@41127
   485
(* translation into intermediate format *)
boehmes@41127
   486
boehmes@41127
   487
(** utility functions **)
boehmes@36898
   488
boehmes@36898
   489
val quantifier = (fn
boehmes@36898
   490
    @{const_name All} => SOME SForall
boehmes@36898
   491
  | @{const_name Ex} => SOME SExists
boehmes@36898
   492
  | _ => NONE)
boehmes@36898
   493
boehmes@36898
   494
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   495
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   496
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   497
boehmes@40664
   498
fun dest_weight (@{const SMT.weight} $ w $ t) =
blanchet@41165
   499
      ((SOME (snd (HOLogic.dest_number w)), t)
blanchet@41165
   500
       handle TERM _ =>
blanchet@41165
   501
                (case w of
blanchet@41165
   502
                  Var ((s, _), _) => (* FIXME: temporary workaround *)
blanchet@41165
   503
                    (case Int.fromString s of
blanchet@41165
   504
                      SOME n => (SOME n, t)
blanchet@41165
   505
                    | NONE => raise TERM ("bad weight", [w]))
blanchet@41165
   506
                 | _ => raise TERM ("bad weight", [w])))
boehmes@40664
   507
  | dest_weight t = (NONE, t)
boehmes@40664
   508
boehmes@37124
   509
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
boehmes@37124
   510
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
boehmes@41127
   511
  | dest_pat t = raise TERM ("bad pattern", [t])
boehmes@37124
   512
boehmes@37124
   513
fun dest_pats [] = I
boehmes@37124
   514
  | dest_pats ts =
boehmes@37124
   515
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   516
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   517
      | (ps, [false]) => cons (SNoPat ps)
boehmes@41127
   518
      | _ => raise TERM ("bad multi-pattern", ts))
boehmes@36898
   519
boehmes@40579
   520
fun dest_trigger (@{const trigger} $ tl $ t) =
boehmes@37124
   521
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   522
  | dest_trigger t = ([], t)
boehmes@36898
   523
boehmes@36898
   524
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   525
  let
boehmes@36898
   526
    val (Ts, u) = group_quant qn [T] t
boehmes@40664
   527
    val (ps, p) = dest_trigger u
boehmes@40664
   528
    val (w, b) = dest_weight p
boehmes@40664
   529
  in (q, rev Ts, ps, w, b) end)
boehmes@36898
   530
boehmes@36898
   531
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   532
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   533
boehmes@36898
   534
boehmes@41127
   535
(** translation from Isabelle terms into SMT intermediate terms **)
boehmes@36898
   536
boehmes@41127
   537
fun intermediate header dtyps ctxt ts trx =
boehmes@41059
   538
  let
boehmes@41127
   539
    fun transT (T as TFree _) = add_typ T true
boehmes@41127
   540
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
boehmes@41127
   541
      | transT (T as Type _) =
boehmes@41059
   542
          (case B.builtin_typ ctxt T of
boehmes@39298
   543
            SOME n => pair n
boehmes@41127
   544
          | NONE => add_typ T true)
boehmes@36898
   545
boehmes@41127
   546
    val unmarked_builtins = [@{const_name If}, @{const_name HOL.eq}]
boehmes@36898
   547
boehmes@36898
   548
    fun app n ts = SApp (n, ts)
boehmes@36898
   549
boehmes@36898
   550
    fun trans t =
boehmes@36898
   551
      (case Term.strip_comb t of
boehmes@36898
   552
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   553
          (case dest_quant qn T t1 of
boehmes@40664
   554
            SOME (q, Ts, ps, w, b) =>
boehmes@36898
   555
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@40664
   556
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
boehmes@41127
   557
          | NONE => raise TERM ("unsupported quantifier", [t]))
boehmes@36898
   558
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   559
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   560
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@41127
   561
      | (Var ((n, _), _), ts) => fold_map trans ts #>> app n
boehmes@41127
   562
      | (u as Const (c as (n, T)), ts) =>
boehmes@41127
   563
          if member (op =) unmarked_builtins n then
boehmes@41127
   564
            (case B.builtin_fun ctxt c ts of
boehmes@41127
   565
              SOME (((m, _), _), us, _) => fold_map trans us #>> app m
boehmes@41127
   566
            | NONE => raise TERM ("not a built-in symbol", [t]))
boehmes@41127
   567
          else transs u T ts
boehmes@41127
   568
      | (u as Free (_, T), ts) => transs u T ts
boehmes@36898
   569
      | (Bound i, []) => pair (SVar i)
boehmes@41127
   570
      | _ => raise TERM ("bad SMT term", [t]))
boehmes@41127
   571
 
boehmes@36898
   572
    and transs t T ts =
boehmes@40663
   573
      let val (Us, U) = U.dest_funT (length ts) T
boehmes@36898
   574
      in
boehmes@36898
   575
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@41127
   576
        add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   577
      end
boehmes@41127
   578
boehmes@41127
   579
    val (us, trx') = fold_map trans ts trx
boehmes@41127
   580
  in ((sign_of (header ts) dtyps trx', us), trx') end
boehmes@41127
   581
boehmes@41127
   582
boehmes@41127
   583
boehmes@41127
   584
(* translation *)
boehmes@41127
   585
boehmes@41127
   586
structure Configs = Generic_Data
boehmes@41127
   587
(
boehmes@41127
   588
  type T = (Proof.context -> config) U.dict
boehmes@41127
   589
  val empty = []
boehmes@41127
   590
  val extend = I
paulson@41193
   591
  fun merge xx = U.dict_merge fst xx
boehmes@41127
   592
)
boehmes@41127
   593
boehmes@41127
   594
fun add_config (cs, cfg) = Configs.map (U.dict_update (cs, cfg))
boehmes@41127
   595
boehmes@41127
   596
fun translate ctxt comments ithms =
boehmes@41127
   597
  let
boehmes@41127
   598
    val cs = SMT_Config.solver_class_of ctxt
boehmes@41127
   599
    val {prefixes, is_fol, header, has_datatypes, serialize} =
boehmes@41127
   600
      (case U.dict_get (Configs.get (Context.Proof ctxt)) cs of
boehmes@41127
   601
        SOME cfg => cfg ctxt
boehmes@41127
   602
      | NONE => error ("SMT: no translation configuration found " ^
boehmes@41127
   603
          "for solver class " ^ quote (U.string_of_class cs)))
boehmes@41127
   604
      
boehmes@41127
   605
    val with_datatypes =
boehmes@41127
   606
      has_datatypes andalso Config.get ctxt SMT_Config.datatypes
boehmes@41127
   607
boehmes@41127
   608
    fun no_dtyps (tr_context, ctxt) ts = (([], tr_context, ctxt), ts)
boehmes@41127
   609
boehmes@41127
   610
    val (builtins, ts1) =
boehmes@41127
   611
      ithms
boehmes@41127
   612
      |> map (HOLogic.dest_Trueprop o Thm.prop_of o snd)
boehmes@41127
   613
      |> map (Envir.eta_contract o Envir.beta_norm)
boehmes@41127
   614
      |> mark_builtins ctxt
boehmes@41127
   615
boehmes@41127
   616
    val ((dtyps, tr_context, ctxt1), ts2) =
boehmes@41127
   617
      ((make_tr_context prefixes, ctxt), ts1)
boehmes@41127
   618
      |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
boehmes@41127
   619
boehmes@41127
   620
    val (ctxt2, ts3) =
boehmes@41127
   621
      ts2
boehmes@41127
   622
      |> eta_expand
boehmes@41127
   623
      |> lift_lambdas ctxt1
boehmes@41127
   624
      ||> intro_explicit_application
boehmes@41127
   625
boehmes@41127
   626
    val ((rewrite_rules, extra_thms), ts4) =
boehmes@41127
   627
      (if is_fol then folify ctxt2 builtins else pair ([], [])) ts3
boehmes@41127
   628
boehmes@41127
   629
    val rewrite_rules' = fun_app_eq :: rewrite_rules
boehmes@36898
   630
  in
boehmes@41127
   631
    (ts4, tr_context)
boehmes@41127
   632
    |-> intermediate header dtyps ctxt2
boehmes@41127
   633
    |>> uncurry (serialize comments)
boehmes@41127
   634
    ||> recon_of ctxt2 rewrite_rules' extra_thms ithms revert_typ revert_types
boehmes@36898
   635
  end
boehmes@36898
   636
boehmes@36898
   637
end