src/HOL/Tools/SMT/smt_translate.ML
author boehmes
Fri Dec 17 14:36:33 2010 +0100 (2010-12-17)
changeset 41232 4ea9f2a8c093
parent 41198 aa627a799e8e
child 41250 41f86829e22f
permissions -rw-r--r--
fixed lambda-lifting: shift indices of bound variables correctly (after locking the required bound variables) and apply bound variables to the new function symbol in the right order;
fixed introduction of explicit application: use explicit application for every additional argument (grouping of arguments caused confusion when translating into the intermediate format)
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@41173
   150
      | (u as @{const SMT.weight}, [t1, t2]) =>
boehmes@41173
   151
          mark t2 #>> (fn t2' => u $ t1 $ t2')
boehmes@41127
   152
      | (u as Const c, ts) =>
boehmes@41127
   153
          (case B.builtin_num ctxt t of
boehmes@41127
   154
            SOME (n, T) =>
boehmes@41127
   155
              let val v = ((n, 0), T)
boehmes@41127
   156
              in Vartab.update v #> pair (Var v) end
boehmes@41127
   157
          | NONE =>
boehmes@41127
   158
              (case B.builtin_fun ctxt c ts of
boehmes@41127
   159
                SOME ((ni, T), us, U) =>
boehmes@41127
   160
                  Vartab.update (ni, U) #> marks (Var (ni, T)) us
boehmes@41127
   161
              | NONE => marks u ts))
boehmes@41127
   162
      | (Abs (n, T, u), ts) => mark u #-> (fn u' => marks (Abs (n, T, u')) ts)
boehmes@41127
   163
      | (u, ts) => marks u ts)
boehmes@41127
   164
 
boehmes@41127
   165
    and marks t ts = fold_map mark ts #>> Term.list_comb o pair t
boehmes@41127
   166
boehmes@41127
   167
  in (fn ts => swap (fold_map mark ts Vartab.empty)) end
boehmes@41127
   168
boehmes@41127
   169
fun mark_builtins' ctxt t = hd (snd (mark_builtins ctxt [t]))
boehmes@41127
   170
boehmes@41127
   171
boehmes@41127
   172
(** FIXME **)
boehmes@41127
   173
boehmes@41127
   174
local
boehmes@41127
   175
  (*
boehmes@41127
   176
    mark constructors and selectors as Vars (forcing eta-expansion),
boehmes@41127
   177
    add missing datatype selectors via hypothetical definitions,
boehmes@41127
   178
    also return necessary datatype and record theorems
boehmes@41127
   179
  *)
boehmes@41127
   180
in
boehmes@41127
   181
boehmes@41127
   182
fun collect_datatypes_and_records (tr_context, ctxt) ts =
boehmes@41127
   183
  (([], tr_context, ctxt), ts)
boehmes@41127
   184
boehmes@41127
   185
end
boehmes@41127
   186
boehmes@41127
   187
boehmes@41127
   188
(** eta-expand quantifiers, let expressions and built-ins *)
boehmes@41127
   189
boehmes@41127
   190
local
boehmes@41127
   191
  fun eta T t = Abs (Name.uu, T, Term.incr_boundvars 1 t $ Bound 0)
boehmes@41127
   192
boehmes@41127
   193
  fun exp T = eta (Term.domain_type (Term.domain_type T))
boehmes@41127
   194
boehmes@41127
   195
  fun exp2 T q =
boehmes@41127
   196
    let val U = Term.domain_type T
boehmes@41127
   197
    in Abs (Name.uu, U, q $ eta (Term.domain_type U) (Bound 0)) end
boehmes@41127
   198
boehmes@41127
   199
  fun exp2' T l =
boehmes@41127
   200
    let val (U1, U2) = Term.dest_funT T ||> Term.domain_type
boehmes@41127
   201
    in Abs (Name.uu, U1, eta U2 (l $ Bound 0)) end
boehmes@41127
   202
boehmes@41127
   203
  fun expf t i T ts =
boehmes@41127
   204
    let val Ts = U.dest_funT i T |> fst |> drop (length ts)
boehmes@41195
   205
    in
boehmes@41195
   206
      Term.list_comb (t, ts)
boehmes@41195
   207
      |> Term.incr_boundvars (length Ts)
boehmes@41195
   208
      |> fold_index (fn (i, _) => fn u => u $ Bound i) Ts
boehmes@41195
   209
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
boehmes@41195
   210
    end
boehmes@41127
   211
boehmes@41127
   212
  fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
boehmes@41127
   213
    | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp T t
boehmes@41127
   214
    | expand (q as Const (@{const_name All}, T)) = exp2 T q
boehmes@41127
   215
    | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
boehmes@41127
   216
    | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp T t
boehmes@41127
   217
    | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
boehmes@41127
   218
    | expand ((l as Const (@{const_name Let}, _)) $ t $ Abs a) =
boehmes@41127
   219
        l $ expand t $ abs_expand a
boehmes@41127
   220
    | expand ((l as Const (@{const_name Let}, T)) $ t $ u) =
boehmes@41127
   221
        l $ expand t $ exp (Term.range_type T) u
boehmes@41127
   222
    | expand ((l as Const (@{const_name Let}, T)) $ t) = exp2 T (l $ expand t)
boehmes@41127
   223
    | expand (l as Const (@{const_name Let}, T)) = exp2' T l
boehmes@41127
   224
    | expand (Abs a) = abs_expand a
boehmes@41127
   225
    | expand t =
boehmes@41127
   226
        (case Term.strip_comb t of
boehmes@41127
   227
          (u as Const (@{const_name If}, T), ts) => expf u 3 T (map expand ts)
boehmes@41127
   228
        | (u as Var ((_, i), T), ts) => expf u i T (map expand ts)
boehmes@41127
   229
        | (u, ts) => Term.list_comb (u, map expand ts))
boehmes@41127
   230
boehmes@41127
   231
  and abs_expand (n, T, t) = Abs (n, T, expand t)
boehmes@41127
   232
in
boehmes@41127
   233
boehmes@41127
   234
val eta_expand = map expand
boehmes@41127
   235
boehmes@41127
   236
end
boehmes@41127
   237
boehmes@41127
   238
boehmes@41127
   239
(** lambda-lifting **)
boehmes@41127
   240
boehmes@41127
   241
local
boehmes@41127
   242
  fun mk_def Ts T lhs rhs =
boehmes@41127
   243
    let
boehmes@41127
   244
      val eq = HOLogic.eq_const T $ lhs $ rhs
boehmes@41127
   245
      val trigger =
boehmes@41127
   246
        [[Const (@{const_name SMT.pat}, T --> @{typ SMT.pattern}) $ lhs]]
boehmes@41127
   247
        |> map (HOLogic.mk_list @{typ SMT.pattern})
boehmes@41127
   248
        |> HOLogic.mk_list @{typ "SMT.pattern list"}
boehmes@41127
   249
      fun mk_all T t = HOLogic.all_const T $ Abs (Name.uu, T, t)
boehmes@41127
   250
    in fold mk_all Ts (@{const SMT.trigger} $ trigger $ eq) end
boehmes@41127
   251
boehmes@41232
   252
  fun mk_abs Ts = fold (fn T => fn t => Abs (Name.uu, T, t)) Ts
boehmes@41232
   253
boehmes@41232
   254
  fun dest_abs Ts (Abs (_, T, t)) = dest_abs (T :: Ts) t
boehmes@41232
   255
    | dest_abs Ts t = (Ts, t)
boehmes@41232
   256
boehmes@41127
   257
  fun replace_lambda Us Ts t (cx as (defs, ctxt)) =
boehmes@41127
   258
    let
boehmes@41232
   259
      val t1 = mk_abs Us t
boehmes@41232
   260
      val bs = sort int_ord (Term.add_loose_bnos (t1, 0, []))
boehmes@41232
   261
      fun rep i k = if member (op =) bs i then (Bound k, k+1) else (Bound i, k)
boehmes@41232
   262
      val (rs, _) = fold_map rep (0 upto length Ts - 1) 0
boehmes@41232
   263
      val t2 = Term.subst_bounds (rs, t1)
boehmes@41232
   264
      val Ts' = map (nth Ts) bs 
boehmes@41232
   265
      val (_, t3) = dest_abs [] t2
boehmes@41232
   266
      val t4 = mk_abs Ts' t2
boehmes@41232
   267
boehmes@41127
   268
      val T = Term.fastype_of1 (Us @ Ts, t)
boehmes@41232
   269
      fun app f = Term.list_comb (f, map Bound (rev bs))
boehmes@41127
   270
    in
boehmes@41232
   271
      (case Termtab.lookup defs t4 of
boehmes@41197
   272
        SOME (f, _) => (app f, cx)
boehmes@41127
   273
      | NONE =>
boehmes@41127
   274
          let
boehmes@41127
   275
            val (n, ctxt') =
boehmes@41127
   276
              yield_singleton Variable.variant_fixes Name.uu ctxt
boehmes@41232
   277
            val (is, UTs) = split_list (map_index I (Us @ Ts'))
boehmes@41232
   278
            val f = Free (n, rev UTs ---> T)
boehmes@41232
   279
            val lhs = Term.list_comb (f, map Bound (rev is))
boehmes@41232
   280
            val def = mk_def UTs (Term.fastype_of1 (Us @ Ts, t)) lhs t3
boehmes@41232
   281
          in (app f, (Termtab.update (t4, (f, def)) defs, ctxt')) end)
boehmes@41127
   282
    end
boehmes@41127
   283
boehmes@41127
   284
  fun traverse Ts t =
boehmes@41127
   285
    (case t of
boehmes@41127
   286
      (q as Const (@{const_name All}, _)) $ Abs a =>
boehmes@41127
   287
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   288
    | (q as Const (@{const_name Ex}, _)) $ Abs a =>
boehmes@41127
   289
        abs_traverse Ts a #>> (fn a' => q $ Abs a')
boehmes@41127
   290
    | (l as Const (@{const_name Let}, _)) $ u $ Abs a =>
boehmes@41127
   291
        traverse Ts u ##>> abs_traverse Ts a #>>
boehmes@41127
   292
        (fn (u', a') => l $ u' $ Abs a')
boehmes@41127
   293
    | Abs _ =>
boehmes@41127
   294
        let val (Us, u) = dest_abs [] t
boehmes@41127
   295
        in traverse (Us @ Ts) u #-> replace_lambda Us Ts end
boehmes@41127
   296
    | u1 $ u2 => traverse Ts u1 ##>> traverse Ts u2 #>> (op $)
boehmes@41127
   297
    | _ => pair t)
boehmes@41127
   298
boehmes@41127
   299
  and abs_traverse Ts (n, T, t) = traverse (T::Ts) t #>> (fn t' => (n, T, t'))
boehmes@41127
   300
in
boehmes@41127
   301
boehmes@41127
   302
fun lift_lambdas ctxt ts =
boehmes@41127
   303
  (Termtab.empty, ctxt)
boehmes@41127
   304
  |> fold_map (traverse []) ts
boehmes@41127
   305
  |> (fn (us, (defs, ctxt')) =>
boehmes@41127
   306
       (ctxt', Termtab.fold (cons o snd o snd) defs us))
boehmes@41127
   307
boehmes@41127
   308
end
boehmes@41127
   309
boehmes@41127
   310
boehmes@41127
   311
(** introduce explicit applications **)
boehmes@41127
   312
boehmes@41127
   313
local
boehmes@41127
   314
  (*
boehmes@41127
   315
    Make application explicit for functions with varying number of arguments.
boehmes@41127
   316
  *)
boehmes@41127
   317
boehmes@41232
   318
  fun add t i = Termtab.map_default (t, i) (Integer.min i)
boehmes@41127
   319
boehmes@41232
   320
  fun min_arities t =
boehmes@41232
   321
    (case Term.strip_comb t of
boehmes@41232
   322
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   323
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
boehmes@41232
   324
    | (Abs (_, _, u), ts) => min_arities u #> fold min_arities ts
boehmes@41232
   325
    | (_, ts) => fold min_arities ts)
boehmes@41127
   326
boehmes@41232
   327
  fun app u (t, T) =
boehmes@41232
   328
    (Const (@{const_name SMT.fun_app}, T --> T) $ t $ u, Term.range_type T)
boehmes@41127
   329
boehmes@41232
   330
  fun apply i t T ts =
boehmes@41232
   331
    let val (ts1, ts2) = chop i ts
boehmes@41232
   332
    in fst (fold app ts2 (Term.list_comb (t, ts1), snd (U.dest_funT i T))) end
boehmes@41127
   333
in
boehmes@41127
   334
boehmes@41127
   335
fun intro_explicit_application ts =
boehmes@41127
   336
  let
boehmes@41232
   337
    val arities = fold min_arities ts Termtab.empty
boehmes@41232
   338
    fun apply' t = apply (the (Termtab.lookup arities t)) t
boehmes@41127
   339
boehmes@41232
   340
    fun traverse Ts t =
boehmes@41127
   341
      (case Term.strip_comb t of
boehmes@41232
   342
        (u as Const (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   343
      | (u as Free (_, T), ts) => apply' u T (map (traverse Ts) ts)
boehmes@41232
   344
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
boehmes@41232
   345
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
boehmes@41232
   346
      | (u, ts) => traverses Ts u ts)
boehmes@41232
   347
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
boehmes@41232
   348
  in map (traverse []) ts end
boehmes@41127
   349
boehmes@41127
   350
val fun_app_eq = mk_meta_eq @{thm SMT.fun_app_def}
boehmes@41127
   351
boehmes@41127
   352
end
boehmes@41127
   353
boehmes@41127
   354
boehmes@41127
   355
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
boehmes@41127
   356
boehmes@41127
   357
val tboolT = @{typ SMT.term_bool}
boehmes@41127
   358
val term_true = Const (@{const_name True}, tboolT)
boehmes@41127
   359
val term_false = Const (@{const_name False}, tboolT)
boehmes@41127
   360
boehmes@41127
   361
val term_bool = @{lemma "True ~= False" by simp}
boehmes@41127
   362
val term_bool_prop =
boehmes@41127
   363
  let
boehmes@41127
   364
    fun replace @{const HOL.eq (bool)} = @{const HOL.eq (SMT.term_bool)}
boehmes@41127
   365
      | replace @{const True} = term_true
boehmes@41127
   366
      | replace @{const False} = term_false
boehmes@41127
   367
      | replace t = t
boehmes@41172
   368
  in Term.map_aterms replace (U.prop_of term_bool) end
boehmes@41127
   369
boehmes@41127
   370
val fol_rules = [
boehmes@41127
   371
  Let_def,
boehmes@41127
   372
  @{lemma "P = True == P" by (rule eq_reflection) simp},
boehmes@41127
   373
  @{lemma "if P then True else False == P" by (rule eq_reflection) simp}]
boehmes@41127
   374
boehmes@41127
   375
fun reduce_let (Const (@{const_name Let}, _) $ t $ u) =
boehmes@41127
   376
      reduce_let (Term.betapply (u, t))
boehmes@41127
   377
  | reduce_let (t $ u) = reduce_let t $ reduce_let u
boehmes@41127
   378
  | reduce_let (Abs (n, T, t)) = Abs (n, T, reduce_let t)
boehmes@41127
   379
  | reduce_let t = t
boehmes@41127
   380
boehmes@41127
   381
fun is_pred_type NONE = false
boehmes@41127
   382
  | is_pred_type (SOME T) = (Term.body_type T = @{typ bool})
boehmes@41127
   383
boehmes@41127
   384
fun is_conn_type NONE = false
boehmes@41127
   385
  | is_conn_type (SOME T) =
boehmes@41127
   386
      forall (equal @{typ bool}) (Term.body_type T :: Term.binder_types T)
boehmes@41127
   387
boehmes@41127
   388
fun revert_typ @{typ SMT.term_bool} = @{typ bool}
boehmes@41127
   389
  | revert_typ (Type (n, Ts)) = Type (n, map revert_typ Ts)
boehmes@41127
   390
  | revert_typ T = T
boehmes@41127
   391
boehmes@41127
   392
val revert_types = Term.map_types revert_typ
boehmes@41127
   393
boehmes@41127
   394
fun folify ctxt builtins =
boehmes@41127
   395
  let
boehmes@41127
   396
    fun as_term t = @{const HOL.eq (SMT.term_bool)} $ t $ term_true
boehmes@41127
   397
boehmes@41127
   398
    fun as_tbool @{typ bool} = tboolT
boehmes@41127
   399
      | as_tbool (Type (n, Ts)) = Type (n, map as_tbool Ts)
boehmes@41127
   400
      | as_tbool T = T
boehmes@41127
   401
    fun mapTs f g i = U.dest_funT i #> (fn (Ts, T) => map f Ts ---> g T)
boehmes@41127
   402
    fun predT i = mapTs as_tbool I i
boehmes@41127
   403
    fun funcT i = mapTs as_tbool as_tbool i
boehmes@41127
   404
    fun func i (n, T) = (n, funcT i T)
boehmes@41127
   405
boehmes@41127
   406
    fun map_ifT T = T |> Term.dest_funT ||> funcT 2 |> (op -->)
boehmes@41127
   407
    val if_term = @{const If (bool)} |> Term.dest_Const ||> map_ifT |> Const
boehmes@41127
   408
    fun wrap_in_if t = if_term $ t $ term_true $ term_false
boehmes@41127
   409
boehmes@41127
   410
    fun in_list T f t = HOLogic.mk_list T (map f (HOLogic.dest_list t))
boehmes@41127
   411
boehmes@41127
   412
    fun in_term t =
boehmes@41127
   413
      (case Term.strip_comb t of
boehmes@41127
   414
        (Const (n as @{const_name If}, T), [t1, t2, t3]) =>
boehmes@41127
   415
          Const (n, map_ifT T) $ in_form t1 $ in_term t2 $ in_term t3
boehmes@41127
   416
      | (Const (@{const_name HOL.eq}, _), _) => wrap_in_if (in_form t)
boehmes@41127
   417
      | (Var (ni as (_, i), T), ts) =>
boehmes@41127
   418
          let val U = Vartab.lookup builtins ni
boehmes@41127
   419
          in
boehmes@41127
   420
            if is_conn_type U orelse is_pred_type U then wrap_in_if (in_form t)
boehmes@41127
   421
            else Term.list_comb (Var (ni, funcT i T), map in_term ts)
boehmes@41127
   422
          end
boehmes@41127
   423
      | (Const c, ts) =>
boehmes@41127
   424
          Term.list_comb (Const (func (length ts) c), map in_term ts)
boehmes@41127
   425
      | (Free c, ts) =>
boehmes@41127
   426
          Term.list_comb (Free (func (length ts) c), map in_term ts)
boehmes@41127
   427
      | _ => t)
boehmes@41127
   428
boehmes@41127
   429
    and in_weight ((c as @{const SMT.weight}) $ w $ t) = c $ w $ in_form t
boehmes@41127
   430
      | in_weight t = in_form t 
boehmes@41127
   431
boehmes@41232
   432
    and in_pat (Const (c as (@{const_name SMT.pat}, _)) $ t) =
boehmes@41127
   433
          Const (func 1 c) $ in_term t
boehmes@41232
   434
      | in_pat (Const (c as (@{const_name SMT.nopat}, _)) $ t) =
boehmes@41127
   435
          Const (func 1 c) $ in_term t
boehmes@41127
   436
      | in_pat t = raise TERM ("bad pattern", [t])
boehmes@41127
   437
boehmes@41127
   438
    and in_pats ps =
boehmes@41232
   439
      in_list @{typ "SMT.pattern list"} (in_list @{typ SMT.pattern} in_pat) ps
boehmes@41127
   440
boehmes@41232
   441
    and in_trig ((c as @{const SMT.trigger}) $ p $ t) =
boehmes@41232
   442
          c $ in_pats p $ in_weight t
boehmes@41127
   443
      | in_trig t = in_weight t
boehmes@41127
   444
boehmes@41127
   445
    and in_form t =
boehmes@41127
   446
      (case Term.strip_comb t of
boehmes@41127
   447
        (q as Const (qn, _), [Abs (n, T, u)]) =>
boehmes@41127
   448
          if member (op =) [@{const_name All}, @{const_name Ex}] qn then
boehmes@41127
   449
            q $ Abs (n, as_tbool T, in_trig u)
boehmes@41127
   450
          else as_term (in_term t)
boehmes@41127
   451
      | (u as Const (@{const_name If}, _), ts) =>
boehmes@41127
   452
          Term.list_comb (u, map in_form ts)
boehmes@41127
   453
      | (b as @{const HOL.eq (bool)}, ts) => Term.list_comb (b, map in_form ts)
boehmes@41127
   454
      | (Const (n as @{const_name HOL.eq}, T), ts) =>
boehmes@41127
   455
          Term.list_comb (Const (n, predT 2 T), map in_term ts)
boehmes@41127
   456
      | (b as Var (ni as (_, i), T), ts) =>
boehmes@41127
   457
          if is_conn_type (Vartab.lookup builtins ni) then
boehmes@41127
   458
            Term.list_comb (b, map in_form ts)
boehmes@41127
   459
          else if is_pred_type (Vartab.lookup builtins ni) then
boehmes@41127
   460
            Term.list_comb (Var (ni, predT i T), map in_term ts)
boehmes@41127
   461
          else as_term (in_term t)
boehmes@41127
   462
      | _ => as_term (in_term t))
boehmes@41127
   463
  in
boehmes@41127
   464
    map (reduce_let #> in_form) #>
boehmes@41127
   465
    cons (mark_builtins' ctxt term_bool_prop) #>
boehmes@41127
   466
    pair (fol_rules, [term_bool])
boehmes@41127
   467
  end
boehmes@41127
   468
boehmes@41127
   469
boehmes@41127
   470
boehmes@41127
   471
(* translation into intermediate format *)
boehmes@41127
   472
boehmes@41127
   473
(** utility functions **)
boehmes@36898
   474
boehmes@36898
   475
val quantifier = (fn
boehmes@36898
   476
    @{const_name All} => SOME SForall
boehmes@36898
   477
  | @{const_name Ex} => SOME SExists
boehmes@36898
   478
  | _ => NONE)
boehmes@36898
   479
boehmes@36898
   480
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
boehmes@36898
   481
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
boehmes@36898
   482
  | group_quant _ Ts t = (Ts, t)
boehmes@36898
   483
boehmes@40664
   484
fun dest_weight (@{const SMT.weight} $ w $ t) =
boehmes@41173
   485
      (SOME (snd (HOLogic.dest_number w)), t)
boehmes@40664
   486
  | dest_weight t = (NONE, t)
boehmes@40664
   487
boehmes@41232
   488
fun dest_pat (Const (@{const_name SMT.pat}, _) $ t) = (t, true)
boehmes@41232
   489
  | dest_pat (Const (@{const_name SMT.nopat}, _) $ t) = (t, false)
boehmes@41127
   490
  | dest_pat t = raise TERM ("bad pattern", [t])
boehmes@37124
   491
boehmes@37124
   492
fun dest_pats [] = I
boehmes@37124
   493
  | dest_pats ts =
boehmes@37124
   494
      (case map dest_pat ts |> split_list ||> distinct (op =) of
boehmes@37124
   495
        (ps, [true]) => cons (SPat ps)
boehmes@37124
   496
      | (ps, [false]) => cons (SNoPat ps)
boehmes@41127
   497
      | _ => raise TERM ("bad multi-pattern", ts))
boehmes@36898
   498
boehmes@41232
   499
fun dest_trigger (@{const SMT.trigger} $ tl $ t) =
boehmes@37124
   500
      (rev (fold (dest_pats o HOLogic.dest_list) (HOLogic.dest_list tl) []), t)
boehmes@36898
   501
  | dest_trigger t = ([], t)
boehmes@36898
   502
boehmes@36898
   503
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
boehmes@36898
   504
  let
boehmes@36898
   505
    val (Ts, u) = group_quant qn [T] t
boehmes@40664
   506
    val (ps, p) = dest_trigger u
boehmes@40664
   507
    val (w, b) = dest_weight p
boehmes@40664
   508
  in (q, rev Ts, ps, w, b) end)
boehmes@36898
   509
boehmes@36898
   510
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
boehmes@36898
   511
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
boehmes@36898
   512
boehmes@36898
   513
boehmes@41127
   514
(** translation from Isabelle terms into SMT intermediate terms **)
boehmes@36898
   515
boehmes@41127
   516
fun intermediate header dtyps ctxt ts trx =
boehmes@41059
   517
  let
boehmes@41127
   518
    fun transT (T as TFree _) = add_typ T true
boehmes@41127
   519
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
boehmes@41127
   520
      | transT (T as Type _) =
boehmes@41059
   521
          (case B.builtin_typ ctxt T of
boehmes@39298
   522
            SOME n => pair n
boehmes@41127
   523
          | NONE => add_typ T true)
boehmes@36898
   524
boehmes@41127
   525
    val unmarked_builtins = [@{const_name If}, @{const_name HOL.eq}]
boehmes@36898
   526
boehmes@36898
   527
    fun app n ts = SApp (n, ts)
boehmes@36898
   528
boehmes@36898
   529
    fun trans t =
boehmes@36898
   530
      (case Term.strip_comb t of
boehmes@36898
   531
        (Const (qn, _), [Abs (_, T, t1)]) =>
boehmes@36898
   532
          (case dest_quant qn T t1 of
boehmes@40664
   533
            SOME (q, Ts, ps, w, b) =>
boehmes@36898
   534
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
boehmes@40664
   535
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', w, b'))
boehmes@41127
   536
          | NONE => raise TERM ("unsupported quantifier", [t]))
boehmes@36898
   537
      | (Const (@{const_name Let}, _), [t1, Abs (_, T, t2)]) =>
boehmes@36898
   538
          transT T ##>> trans t1 ##>> trans t2 #>>
boehmes@36898
   539
          (fn ((U, u1), u2) => SLet (U, u1, u2))
boehmes@41127
   540
      | (Var ((n, _), _), ts) => fold_map trans ts #>> app n
boehmes@41127
   541
      | (u as Const (c as (n, T)), ts) =>
boehmes@41127
   542
          if member (op =) unmarked_builtins n then
boehmes@41127
   543
            (case B.builtin_fun ctxt c ts of
boehmes@41127
   544
              SOME (((m, _), _), us, _) => fold_map trans us #>> app m
boehmes@41127
   545
            | NONE => raise TERM ("not a built-in symbol", [t]))
boehmes@41127
   546
          else transs u T ts
boehmes@41127
   547
      | (u as Free (_, T), ts) => transs u T ts
boehmes@36898
   548
      | (Bound i, []) => pair (SVar i)
boehmes@41127
   549
      | _ => raise TERM ("bad SMT term", [t]))
boehmes@41127
   550
 
boehmes@36898
   551
    and transs t T ts =
boehmes@40663
   552
      let val (Us, U) = U.dest_funT (length ts) T
boehmes@36898
   553
      in
boehmes@36898
   554
        fold_map transT Us ##>> transT U #-> (fn Up =>
boehmes@41127
   555
        add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
boehmes@36898
   556
      end
boehmes@41127
   557
boehmes@41127
   558
    val (us, trx') = fold_map trans ts trx
boehmes@41127
   559
  in ((sign_of (header ts) dtyps trx', us), trx') end
boehmes@41127
   560
boehmes@41127
   561
boehmes@41127
   562
boehmes@41127
   563
(* translation *)
boehmes@41127
   564
boehmes@41127
   565
structure Configs = Generic_Data
boehmes@41127
   566
(
boehmes@41127
   567
  type T = (Proof.context -> config) U.dict
boehmes@41127
   568
  val empty = []
boehmes@41127
   569
  val extend = I
paulson@41193
   570
  fun merge xx = U.dict_merge fst xx
boehmes@41127
   571
)
boehmes@41127
   572
boehmes@41127
   573
fun add_config (cs, cfg) = Configs.map (U.dict_update (cs, cfg))
boehmes@41127
   574
boehmes@41232
   575
fun get_config ctxt = 
boehmes@41232
   576
  let val cs = SMT_Config.solver_class_of ctxt
boehmes@41232
   577
  in
boehmes@41232
   578
    (case U.dict_get (Configs.get (Context.Proof ctxt)) cs of
boehmes@41232
   579
      SOME cfg => cfg ctxt
boehmes@41232
   580
    | NONE => error ("SMT: no translation configuration found " ^
boehmes@41232
   581
        "for solver class " ^ quote (U.string_of_class cs)))
boehmes@41232
   582
  end
boehmes@41232
   583
boehmes@41127
   584
fun translate ctxt comments ithms =
boehmes@41127
   585
  let
boehmes@41232
   586
    val {prefixes, is_fol, header, has_datatypes, serialize} = get_config ctxt
boehmes@41232
   587
boehmes@41127
   588
    val with_datatypes =
boehmes@41127
   589
      has_datatypes andalso Config.get ctxt SMT_Config.datatypes
boehmes@41127
   590
boehmes@41127
   591
    fun no_dtyps (tr_context, ctxt) ts = (([], tr_context, ctxt), ts)
boehmes@41127
   592
boehmes@41127
   593
    val (builtins, ts1) =
boehmes@41127
   594
      ithms
boehmes@41172
   595
      |> map (Envir.beta_eta_contract o U.prop_of o snd)
boehmes@41127
   596
      |> mark_builtins ctxt
boehmes@41127
   597
boehmes@41127
   598
    val ((dtyps, tr_context, ctxt1), ts2) =
boehmes@41127
   599
      ((make_tr_context prefixes, ctxt), ts1)
boehmes@41127
   600
      |-> (if with_datatypes then collect_datatypes_and_records else no_dtyps)
boehmes@41127
   601
boehmes@41127
   602
    val (ctxt2, ts3) =
boehmes@41127
   603
      ts2
boehmes@41127
   604
      |> eta_expand
boehmes@41127
   605
      |> lift_lambdas ctxt1
boehmes@41127
   606
      ||> intro_explicit_application
boehmes@41127
   607
boehmes@41127
   608
    val ((rewrite_rules, extra_thms), ts4) =
boehmes@41127
   609
      (if is_fol then folify ctxt2 builtins else pair ([], [])) ts3
boehmes@41127
   610
boehmes@41127
   611
    val rewrite_rules' = fun_app_eq :: rewrite_rules
boehmes@36898
   612
  in
boehmes@41127
   613
    (ts4, tr_context)
boehmes@41127
   614
    |-> intermediate header dtyps ctxt2
boehmes@41127
   615
    |>> uncurry (serialize comments)
boehmes@41127
   616
    ||> recon_of ctxt2 rewrite_rules' extra_thms ithms revert_typ revert_types
boehmes@36898
   617
  end
boehmes@36898
   618
boehmes@36898
   619
end