src/HOL/Tools/SMT/smt_translate.ML
author blanchet
Tue Jun 20 14:41:35 2017 +0200 (2017-06-20)
changeset 66136 dd006934a719
parent 66134 a1fb6beb2731
child 66551 4df6b0ae900d
permissions -rw-r--r--
tuning
blanchet@58061
     1
(*  Title:      HOL/Tools/SMT/smt_translate.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
Translate theorems into an SMT intermediate format and serialize them.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@58061
     7
signature SMT_TRANSLATE =
blanchet@56078
     8
sig
blanchet@56078
     9
  (*intermediate term structure*)
blanchet@56078
    10
  datatype squant = SForall | SExists
blanchet@56078
    11
  datatype 'a spattern = SPat of 'a list | SNoPat of 'a list
blanchet@56078
    12
  datatype sterm =
blanchet@56078
    13
    SVar of int |
blanchet@56078
    14
    SApp of string * sterm list |
blanchet@57165
    15
    SQua of squant * string list * sterm spattern list * sterm
blanchet@56078
    16
blanchet@56078
    17
  (*translation configuration*)
blanchet@56078
    18
  type sign = {
blanchet@57238
    19
    logic: string,
blanchet@56078
    20
    sorts: string list,
blanchet@58429
    21
    dtyps: (BNF_Util.fp_kind * (string * (string * (string * string) list) list)) list,
blanchet@56078
    22
    funcs: (string * (string list * string)) list }
blanchet@56078
    23
  type config = {
blanchet@57238
    24
    logic: term list -> string,
blanchet@58360
    25
    fp_kinds: BNF_Util.fp_kind list,
blanchet@57239
    26
    serialize: (string * string) list -> string list -> sign -> sterm list -> string }
blanchet@56078
    27
  type replay_data = {
blanchet@56078
    28
    context: Proof.context,
blanchet@56078
    29
    typs: typ Symtab.table,
blanchet@56078
    30
    terms: term Symtab.table,
blanchet@57541
    31
    ll_defs: term list,
blanchet@56078
    32
    rewrite_rules: thm list,
blanchet@56078
    33
    assms: (int * thm) list }
blanchet@56078
    34
blanchet@56078
    35
  (*translation*)
blanchet@58061
    36
  val add_config: SMT_Util.class * (Proof.context -> config) -> Context.generic -> Context.generic
blanchet@57239
    37
  val translate: Proof.context -> (string * string) list -> string list -> (int * thm) list ->
blanchet@57239
    38
    string * replay_data
blanchet@57229
    39
end;
blanchet@56078
    40
blanchet@58061
    41
structure SMT_Translate: SMT_TRANSLATE =
blanchet@56078
    42
struct
blanchet@56078
    43
blanchet@56078
    44
blanchet@56078
    45
(* intermediate term structure *)
blanchet@56078
    46
blanchet@56078
    47
datatype squant = SForall | SExists
blanchet@56078
    48
blanchet@66134
    49
datatype 'a spattern =
blanchet@66134
    50
  SPat of 'a list | SNoPat of 'a list
blanchet@56078
    51
blanchet@56078
    52
datatype sterm =
blanchet@56078
    53
  SVar of int |
blanchet@56078
    54
  SApp of string * sterm list |
blanchet@57165
    55
  SQua of squant * string list * sterm spattern list * sterm
blanchet@56078
    56
blanchet@56078
    57
blanchet@56078
    58
(* translation configuration *)
blanchet@56078
    59
blanchet@56078
    60
type sign = {
blanchet@57238
    61
  logic: string,
blanchet@56078
    62
  sorts: string list,
blanchet@58429
    63
  dtyps: (BNF_Util.fp_kind * (string * (string * (string * string) list) list)) list,
blanchet@56078
    64
  funcs: (string * (string list * string)) list }
blanchet@56078
    65
blanchet@56078
    66
type config = {
blanchet@57238
    67
  logic: term list -> string,
blanchet@58360
    68
  fp_kinds: BNF_Util.fp_kind list,
blanchet@57239
    69
  serialize: (string * string) list -> string list -> sign -> sterm list -> string }
blanchet@56078
    70
blanchet@56078
    71
type replay_data = {
blanchet@56078
    72
  context: Proof.context,
blanchet@56078
    73
  typs: typ Symtab.table,
blanchet@56078
    74
  terms: term Symtab.table,
blanchet@57541
    75
  ll_defs: term list,
blanchet@56078
    76
  rewrite_rules: thm list,
blanchet@56078
    77
  assms: (int * thm) list }
blanchet@56078
    78
blanchet@56078
    79
blanchet@56078
    80
(* translation context *)
blanchet@56078
    81
blanchet@56078
    82
fun add_components_of_typ (Type (s, Ts)) =
blanchet@56078
    83
    cons (Long_Name.base_name s) #> fold_rev add_components_of_typ Ts
blanchet@56078
    84
  | add_components_of_typ (TFree (s, _)) = cons (perhaps (try (unprefix "'")) s)
blanchet@56078
    85
  | add_components_of_typ _ = I;
blanchet@56078
    86
blanchet@56078
    87
fun suggested_name_of_typ T = space_implode "_" (add_components_of_typ T []);
blanchet@56078
    88
blanchet@56078
    89
fun suggested_name_of_term (Const (s, _)) = Long_Name.base_name s
blanchet@56078
    90
  | suggested_name_of_term (Free (s, _)) = s
blanchet@56078
    91
  | suggested_name_of_term _ = Name.uu
blanchet@56078
    92
blanchet@56078
    93
val empty_tr_context = (Name.context, Typtab.empty, Termtab.empty)
blanchet@56096
    94
val safe_suffix = "$"
blanchet@56078
    95
blanchet@56078
    96
fun add_typ T proper (cx as (names, typs, terms)) =
blanchet@56078
    97
  (case Typtab.lookup typs T of
blanchet@56078
    98
    SOME (name, _) => (name, cx)
blanchet@56078
    99
  | NONE =>
blanchet@56078
   100
      let
haftmann@56811
   101
        val sugg = Name.desymbolize (SOME true) (suggested_name_of_typ T) ^ safe_suffix
blanchet@56078
   102
        val (name, names') = Name.variant sugg names
blanchet@56078
   103
        val typs' = Typtab.update (T, (name, proper)) typs
blanchet@56078
   104
      in (name, (names', typs', terms)) end)
blanchet@56078
   105
blanchet@56078
   106
fun add_fun t sort (cx as (names, typs, terms)) =
blanchet@56078
   107
  (case Termtab.lookup terms t of
blanchet@56078
   108
    SOME (name, _) => (name, cx)
blanchet@57230
   109
  | NONE =>
blanchet@56078
   110
      let
haftmann@56811
   111
        val sugg = Name.desymbolize (SOME false) (suggested_name_of_term t) ^ safe_suffix
blanchet@56078
   112
        val (name, names') = Name.variant sugg names
blanchet@56078
   113
        val terms' = Termtab.update (t, (name, sort)) terms
blanchet@56078
   114
      in (name, (names', typs, terms')) end)
blanchet@56078
   115
blanchet@58429
   116
fun sign_of logic dtyps (_, typs, terms) = {
blanchet@57238
   117
  logic = logic,
blanchet@56078
   118
  sorts = Typtab.fold (fn (_, (n, true)) => cons n | _ => I) typs [],
blanchet@58429
   119
  dtyps = dtyps,
blanchet@56078
   120
  funcs = Termtab.fold (fn (_, (n, SOME ss)) => cons (n,ss) | _ => I) terms []}
blanchet@56078
   121
blanchet@57541
   122
fun replay_data_of ctxt ll_defs rules assms (_, typs, terms) =
blanchet@56078
   123
  let
blanchet@56078
   124
    fun add_typ (T, (n, _)) = Symtab.update (n, T)
blanchet@56078
   125
    val typs' = Typtab.fold add_typ typs Symtab.empty
blanchet@56078
   126
blanchet@56078
   127
    fun add_fun (t, (n, _)) = Symtab.update (n, t)
blanchet@56078
   128
    val terms' = Termtab.fold add_fun terms Symtab.empty
blanchet@56078
   129
  in
blanchet@57541
   130
    {context = ctxt, typs = typs', terms = terms', ll_defs = ll_defs, rewrite_rules = rules,
blanchet@57541
   131
     assms = assms}
blanchet@56078
   132
  end
blanchet@56078
   133
blanchet@56078
   134
blanchet@56078
   135
(* preprocessing *)
blanchet@56078
   136
blanchet@58361
   137
(** (co)datatype declarations **)
blanchet@56078
   138
blanchet@58361
   139
fun collect_co_datatypes fp_kinds (tr_context, ctxt) ts =
blanchet@56078
   140
  let
blanchet@58429
   141
    val (fp_decls, ctxt') =
blanchet@58361
   142
      ([], ctxt)
blanchet@58429
   143
      |> fold (Term.fold_types (SMT_Datatypes.add_decls fp_kinds)) ts
blanchet@58429
   144
      |>> flat
blanchet@56078
   145
blanchet@58429
   146
    fun is_decl_typ T = exists (equal T o fst o snd) fp_decls
blanchet@56078
   147
blanchet@56078
   148
    fun add_typ' T proper =
blanchet@58429
   149
      (case SMT_Builtin.dest_builtin_typ ctxt' T of
blanchet@56078
   150
        SOME n => pair n
blanchet@56078
   151
      | NONE => add_typ T proper)
blanchet@56078
   152
blanchet@56078
   153
    fun tr_select sel =
blanchet@56078
   154
      let val T = Term.range_type (Term.fastype_of sel)
blanchet@56078
   155
      in add_fun sel NONE ##>> add_typ' T (not (is_decl_typ T)) end
blanchet@56078
   156
    fun tr_constr (constr, selects) =
blanchet@56078
   157
      add_fun constr NONE ##>> fold_map tr_select selects
blanchet@58429
   158
    fun tr_typ (fp, (T, cases)) =
blanchet@58429
   159
      add_typ' T false ##>> fold_map tr_constr cases #>> pair fp
blanchet@58361
   160
blanchet@58429
   161
    val (fp_decls', tr_context') = fold_map tr_typ fp_decls tr_context
blanchet@56078
   162
blanchet@56078
   163
    fun add (constr, selects) =
blanchet@56078
   164
      Termtab.update (constr, length selects) #>
blanchet@56078
   165
      fold (Termtab.update o rpair 1) selects
blanchet@58361
   166
blanchet@58429
   167
    val funcs = fold (fold add o snd o snd) fp_decls Termtab.empty
blanchet@58429
   168
blanchet@58429
   169
  in ((funcs, fp_decls', tr_context', ctxt'), ts) end
blanchet@58361
   170
    (* FIXME: also return necessary (co)datatype theorems *)
blanchet@56078
   171
blanchet@56078
   172
blanchet@56078
   173
(** eta-expand quantifiers, let expressions and built-ins *)
blanchet@56078
   174
blanchet@56078
   175
local
blanchet@56078
   176
  fun eta f T t = Abs (Name.uu, T, f (Term.incr_boundvars 1 t $ Bound 0))
blanchet@56078
   177
blanchet@56078
   178
  fun exp f T = eta f (Term.domain_type (Term.domain_type T))
blanchet@56078
   179
blanchet@56078
   180
  fun exp2 T q =
blanchet@56078
   181
    let val U = Term.domain_type T
blanchet@56078
   182
    in Abs (Name.uu, U, q $ eta I (Term.domain_type U) (Bound 0)) end
blanchet@56078
   183
blanchet@56078
   184
  fun expf k i T t =
blanchet@58061
   185
    let val Ts = drop i (fst (SMT_Util.dest_funT k T))
blanchet@56078
   186
    in
blanchet@56078
   187
      Term.incr_boundvars (length Ts) t
blanchet@56078
   188
      |> fold_rev (fn i => fn u => u $ Bound i) (0 upto length Ts - 1)
blanchet@56078
   189
      |> fold_rev (fn T => fn u => Abs (Name.uu, T, u)) Ts
blanchet@56078
   190
    end
blanchet@56078
   191
in
blanchet@56078
   192
blanchet@56078
   193
fun eta_expand ctxt funcs =
blanchet@56078
   194
  let
blanchet@56078
   195
    fun exp_func t T ts =
blanchet@56078
   196
      (case Termtab.lookup funcs t of
blanchet@56078
   197
        SOME k => Term.list_comb (t, ts) |> k <> length ts ? expf k (length ts) T
blanchet@56078
   198
      | NONE => Term.list_comb (t, ts))
blanchet@56078
   199
blanchet@56078
   200
    fun expand ((q as Const (@{const_name All}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   201
      | expand ((q as Const (@{const_name All}, T)) $ t) = q $ exp expand T t
blanchet@56078
   202
      | expand (q as Const (@{const_name All}, T)) = exp2 T q
blanchet@56078
   203
      | expand ((q as Const (@{const_name Ex}, _)) $ Abs a) = q $ abs_expand a
blanchet@56078
   204
      | expand ((q as Const (@{const_name Ex}, T)) $ t) = q $ exp expand T t
blanchet@56078
   205
      | expand (q as Const (@{const_name Ex}, T)) = exp2 T q
blanchet@57165
   206
      | expand (Const (@{const_name Let}, T) $ t) =
blanchet@56078
   207
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   208
          in Abs (Name.uu, U, Bound 0 $ Term.incr_boundvars 1 t) end
blanchet@56078
   209
      | expand (Const (@{const_name Let}, T)) =
blanchet@56078
   210
          let val U = Term.domain_type (Term.range_type T)
blanchet@56078
   211
          in Abs (Name.uu, Term.domain_type T, Abs (Name.uu, U, Bound 0 $ Bound 1)) end
blanchet@56078
   212
      | expand t =
blanchet@56078
   213
          (case Term.strip_comb t of
blanchet@66134
   214
            (Const (@{const_name Let}, _), t1 :: t2 :: ts) =>
blanchet@66136
   215
            Term.betapplys (Term.betapply (expand t2, expand t1), map expand ts)
blanchet@66134
   216
          | (u as Const (c as (_, T)), ts) =>
blanchet@58061
   217
              (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   218
                SOME (_, k, us, mk) =>
blanchet@56078
   219
                  if k = length us then mk (map expand us)
blanchet@56078
   220
                  else if k < length us then chop k (map expand us) |>> mk |> Term.list_comb
blanchet@56078
   221
                  else expf k (length ts) T (mk (map expand us))
blanchet@56078
   222
              | NONE => exp_func u T (map expand ts))
blanchet@56078
   223
          | (u as Free (_, T), ts) => exp_func u T (map expand ts)
blanchet@56078
   224
          | (Abs a, ts) => Term.list_comb (abs_expand a, map expand ts)
blanchet@56078
   225
          | (u, ts) => Term.list_comb (u, map expand ts))
blanchet@56078
   226
blanchet@56078
   227
    and abs_expand (n, T, t) = Abs (n, T, expand t)
blanchet@57230
   228
blanchet@56078
   229
  in map expand end
blanchet@56078
   230
blanchet@56078
   231
end
blanchet@56078
   232
blanchet@56078
   233
blanchet@56078
   234
(** introduce explicit applications **)
blanchet@56078
   235
blanchet@56078
   236
local
blanchet@56078
   237
  (*
blanchet@56078
   238
    Make application explicit for functions with varying number of arguments.
blanchet@56078
   239
  *)
blanchet@56078
   240
blanchet@56078
   241
  fun add t i = apfst (Termtab.map_default (t, i) (Integer.min i))
blanchet@56078
   242
  fun add_type T = apsnd (Typtab.update (T, ()))
blanchet@56078
   243
blanchet@56078
   244
  fun min_arities t =
blanchet@56078
   245
    (case Term.strip_comb t of
blanchet@56078
   246
      (u as Const _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   247
    | (u as Free _, ts) => add u (length ts) #> fold min_arities ts
blanchet@56078
   248
    | (Abs (_, T, u), ts) => (can dest_funT T ? add_type T) #> min_arities u #> fold min_arities ts
blanchet@56078
   249
    | (_, ts) => fold min_arities ts)
blanchet@56078
   250
blanchet@56078
   251
  fun minimize types t i =
blanchet@56078
   252
    let
blanchet@56078
   253
      fun find_min j [] _ = j
blanchet@56078
   254
        | find_min j (U :: Us) T =
blanchet@56078
   255
            if Typtab.defined types T then j else find_min (j + 1) Us (U --> T)
blanchet@56078
   256
blanchet@56078
   257
      val (Ts, T) = Term.strip_type (Term.type_of t)
blanchet@56078
   258
    in find_min 0 (take i (rev Ts)) T end
blanchet@56078
   259
blanchet@57230
   260
  fun app u (t, T) = (Const (@{const_name fun_app}, T --> T) $ t $ u, Term.range_type T)
blanchet@56078
   261
blanchet@56078
   262
  fun apply i t T ts =
blanchet@56078
   263
    let
blanchet@56078
   264
      val (ts1, ts2) = chop i ts
blanchet@58061
   265
      val (_, U) = SMT_Util.dest_funT i T
blanchet@56078
   266
    in fst (fold app ts2 (Term.list_comb (t, ts1), U)) end
blanchet@56078
   267
in
blanchet@56078
   268
blanchet@56078
   269
fun intro_explicit_application ctxt funcs ts =
blanchet@56078
   270
  let
blanchet@56078
   271
    val (arities, types) = fold min_arities ts (Termtab.empty, Typtab.empty)
blanchet@56078
   272
    val arities' = Termtab.map (minimize types) arities (* FIXME: highly suspicious *)
blanchet@56078
   273
blanchet@56078
   274
    fun app_func t T ts =
blanchet@56078
   275
      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
blanchet@56078
   276
      else apply (the (Termtab.lookup arities' t)) t T ts
blanchet@56078
   277
blanchet@58061
   278
    fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
blanchet@56078
   279
blanchet@56078
   280
    fun traverse Ts t =
blanchet@56078
   281
      (case Term.strip_comb t of
blanchet@56078
   282
        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
blanchet@56078
   283
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   284
      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
blanchet@56078
   285
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   286
      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
blanchet@56078
   287
          q $ traverse Ts u1 $ traverse Ts u2
blanchet@56078
   288
      | (u as Const (c as (_, T)), ts) =>
blanchet@58061
   289
          (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   290
            SOME (_, k, us, mk) =>
blanchet@56078
   291
              let
blanchet@56078
   292
                val (ts1, ts2) = chop k (map (traverse Ts) us)
blanchet@56078
   293
                val U = Term.strip_type T |>> snd o chop k |> (op --->)
blanchet@56078
   294
              in apply 0 (mk ts1) U ts2 end
blanchet@56078
   295
          | NONE => app_func u T (map (traverse Ts) ts))
blanchet@56078
   296
      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
blanchet@56078
   297
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
blanchet@56078
   298
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
blanchet@56078
   299
      | (u, ts) => traverses Ts u ts)
blanchet@57230
   300
    and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
blanchet@57165
   301
      | in_trigger Ts t = traverse Ts t
blanchet@56078
   302
    and in_pats Ts ps =
blanchet@57230
   303
      in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
blanchet@57230
   304
    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
blanchet@57230
   305
      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
blanchet@56078
   306
      | in_pat _ t = raise TERM ("bad pattern", [t])
blanchet@56078
   307
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
blanchet@56078
   308
  in map (traverse []) ts end
blanchet@56078
   309
blanchet@57230
   310
val fun_app_eq = mk_meta_eq @{thm fun_app_def}
blanchet@56078
   311
blanchet@56078
   312
end
blanchet@56078
   313
blanchet@56078
   314
blanchet@56078
   315
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
blanchet@56078
   316
blanchet@56078
   317
local
blanchet@56078
   318
  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
blanchet@56078
   319
blanchet@56078
   320
  val fol_rules = [
blanchet@56078
   321
    Let_def,
blanchet@61782
   322
    @{lemma "P = True == P" by (rule eq_reflection) simp}]
blanchet@56078
   323
blanchet@56078
   324
  exception BAD_PATTERN of unit
blanchet@56078
   325
blanchet@56078
   326
  fun is_builtin_conn_or_pred ctxt c ts =
blanchet@58061
   327
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
blanchet@58061
   328
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
blanchet@56078
   329
in
blanchet@56078
   330
blanchet@56078
   331
fun folify ctxt =
blanchet@56078
   332
  let
blanchet@58061
   333
    fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
blanchet@56078
   334
blanchet@56078
   335
    fun in_term pat t =
blanchet@56078
   336
      (case Term.strip_comb t of
blanchet@56078
   337
        (@{const True}, []) => t
blanchet@56078
   338
      | (@{const False}, []) => t
blanchet@56078
   339
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
blanchet@56104
   340
          if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
blanchet@56078
   341
      | (Const (c as (n, _)), ts) =>
blanchet@61782
   342
          if is_builtin_conn_or_pred ctxt c ts orelse is_quant n then
blanchet@61782
   343
            if pat then raise BAD_PATTERN () else in_form t
blanchet@61782
   344
          else
blanchet@61782
   345
            Term.list_comb (Const c, map (in_term pat) ts)
blanchet@56078
   346
      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
blanchet@56078
   347
      | _ => t)
blanchet@56078
   348
blanchet@57230
   349
    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
blanchet@56078
   350
          p $ in_term true t
blanchet@57230
   351
      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
blanchet@56078
   352
          p $ in_term true t
blanchet@56078
   353
      | in_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   354
blanchet@56078
   355
    and in_pats ps =
blanchet@57230
   356
      in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
blanchet@56078
   357
blanchet@57230
   358
    and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
blanchet@57165
   359
      | in_trigger t = in_form t
blanchet@56078
   360
blanchet@56078
   361
    and in_form t =
blanchet@56078
   362
      (case Term.strip_comb t of
blanchet@56078
   363
        (q as Const (qn, _), [Abs (n, T, u)]) =>
blanchet@56078
   364
          if is_quant qn then q $ Abs (n, T, in_trigger u)
blanchet@56078
   365
          else in_term false t
blanchet@56078
   366
      | (Const c, ts) =>
blanchet@58061
   367
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
blanchet@56078
   368
            SOME (_, _, us, mk) => mk (map in_form us)
blanchet@56078
   369
          | NONE =>
blanchet@58061
   370
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
blanchet@56078
   371
                SOME (_, _, us, mk) => mk (map (in_term false) us)
blanchet@56078
   372
              | NONE => in_term false t))
blanchet@56078
   373
      | _ => in_term false t)
blanchet@56078
   374
  in
blanchet@56078
   375
    map in_form #>
blanchet@56078
   376
    pair (fol_rules, I)
blanchet@56078
   377
  end
blanchet@56078
   378
blanchet@56078
   379
end
blanchet@56078
   380
blanchet@56078
   381
blanchet@56078
   382
(* translation into intermediate format *)
blanchet@56078
   383
blanchet@56078
   384
(** utility functions **)
blanchet@56078
   385
blanchet@56078
   386
val quantifier = (fn
blanchet@56078
   387
    @{const_name All} => SOME SForall
blanchet@56078
   388
  | @{const_name Ex} => SOME SExists
blanchet@56078
   389
  | _ => NONE)
blanchet@56078
   390
blanchet@56078
   391
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
blanchet@56078
   392
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
blanchet@56078
   393
  | group_quant _ Ts t = (Ts, t)
blanchet@56078
   394
blanchet@57230
   395
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
blanchet@57230
   396
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
blanchet@56078
   397
  | dest_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   398
blanchet@56078
   399
fun dest_pats [] = I
blanchet@56078
   400
  | dest_pats ts =
blanchet@56078
   401
      (case map dest_pat ts |> split_list ||> distinct (op =) of
blanchet@56078
   402
        (ps, [true]) => cons (SPat ps)
blanchet@56078
   403
      | (ps, [false]) => cons (SNoPat ps)
blanchet@56078
   404
      | _ => raise TERM ("bad multi-pattern", ts))
blanchet@56078
   405
blanchet@57230
   406
fun dest_trigger (@{const trigger} $ tl $ t) =
blanchet@58061
   407
      (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
blanchet@56078
   408
  | dest_trigger t = ([], t)
blanchet@56078
   409
blanchet@56078
   410
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
blanchet@56078
   411
  let
blanchet@56078
   412
    val (Ts, u) = group_quant qn [T] t
blanchet@56078
   413
    val (ps, p) = dest_trigger u
blanchet@57165
   414
  in (q, rev Ts, ps, p) end)
blanchet@56078
   415
blanchet@56078
   416
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
blanchet@56078
   417
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
blanchet@56078
   418
blanchet@56078
   419
blanchet@56078
   420
(** translation from Isabelle terms into SMT intermediate terms **)
blanchet@56078
   421
blanchet@58429
   422
fun intermediate logic dtyps builtin ctxt ts trx =
blanchet@56078
   423
  let
blanchet@56078
   424
    fun transT (T as TFree _) = add_typ T true
blanchet@56078
   425
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
blanchet@56078
   426
      | transT (T as Type _) =
blanchet@58061
   427
          (case SMT_Builtin.dest_builtin_typ ctxt T of
blanchet@56078
   428
            SOME n => pair n
blanchet@56078
   429
          | NONE => add_typ T true)
blanchet@56078
   430
blanchet@56078
   431
    fun app n ts = SApp (n, ts)
blanchet@56078
   432
blanchet@56078
   433
    fun trans t =
blanchet@56078
   434
      (case Term.strip_comb t of
blanchet@56078
   435
        (Const (qn, _), [Abs (_, T, t1)]) =>
blanchet@56078
   436
          (case dest_quant qn T t1 of
blanchet@57165
   437
            SOME (q, Ts, ps, b) =>
blanchet@56078
   438
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
blanchet@57165
   439
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
blanchet@56078
   440
          | NONE => raise TERM ("unsupported quantifier", [t]))
blanchet@56078
   441
      | (u as Const (c as (_, T)), ts) =>
blanchet@56078
   442
          (case builtin ctxt c ts of
blanchet@56078
   443
            SOME (n, _, us, _) => fold_map trans us #>> app n
blanchet@56078
   444
          | NONE => transs u T ts)
blanchet@56078
   445
      | (u as Free (_, T), ts) => transs u T ts
blanchet@56078
   446
      | (Bound i, []) => pair (SVar i)
blanchet@56078
   447
      | _ => raise TERM ("bad SMT term", [t]))
blanchet@57230
   448
blanchet@56078
   449
    and transs t T ts =
blanchet@58061
   450
      let val (Us, U) = SMT_Util.dest_funT (length ts) T
blanchet@56078
   451
      in
blanchet@56078
   452
        fold_map transT Us ##>> transT U #-> (fn Up =>
blanchet@56104
   453
          add_fun t (SOME Up) ##>> fold_map trans ts #>> SApp)
blanchet@56078
   454
      end
blanchet@56078
   455
blanchet@56078
   456
    val (us, trx') = fold_map trans ts trx
blanchet@58429
   457
  in ((sign_of (logic ts) dtyps trx', us), trx') end
blanchet@56078
   458
blanchet@56078
   459
blanchet@56078
   460
(* translation *)
blanchet@56078
   461
blanchet@56078
   462
structure Configs = Generic_Data
blanchet@56078
   463
(
blanchet@58061
   464
  type T = (Proof.context -> config) SMT_Util.dict
blanchet@56078
   465
  val empty = []
blanchet@56078
   466
  val extend = I
blanchet@58061
   467
  fun merge data = SMT_Util.dict_merge fst data
blanchet@56078
   468
)
blanchet@56078
   469
blanchet@58061
   470
fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
blanchet@56078
   471
blanchet@57230
   472
fun get_config ctxt =
blanchet@58061
   473
  let val cs = SMT_Config.solver_class_of ctxt
blanchet@56078
   474
  in
blanchet@58061
   475
    (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
blanchet@56078
   476
      SOME cfg => cfg ctxt
blanchet@56078
   477
    | NONE => error ("SMT: no translation configuration found " ^
blanchet@58061
   478
        "for solver class " ^ quote (SMT_Util.string_of_class cs)))
blanchet@56078
   479
  end
blanchet@56078
   480
blanchet@57239
   481
fun translate ctxt smt_options comments ithms =
blanchet@56078
   482
  let
blanchet@58360
   483
    val {logic, fp_kinds, serialize} = get_config ctxt
blanchet@56078
   484
blanchet@56078
   485
    fun no_dtyps (tr_context, ctxt) ts =
blanchet@58429
   486
      ((Termtab.empty, [], tr_context, ctxt), ts)
blanchet@56078
   487
blanchet@58061
   488
    val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
blanchet@56078
   489
blanchet@58429
   490
    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
blanchet@56078
   491
      ((empty_tr_context, ctxt), ts1)
blanchet@58361
   492
      |-> (if null fp_kinds then no_dtyps else collect_co_datatypes fp_kinds)
blanchet@56078
   493
blanchet@56078
   494
    fun is_binder (Const (@{const_name Let}, _) $ _) = true
blanchet@56078
   495
      | is_binder t = Lambda_Lifting.is_quantifier t
blanchet@56078
   496
blanchet@56078
   497
    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
blanchet@56078
   498
          q $ Abs (n, T, mk_trigger t)
blanchet@56078
   499
      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
blanchet@57230
   500
          Term.domain_type T --> @{typ pattern}
blanchet@57230
   501
          |> (fn T => Const (@{const_name pat}, T) $ lhs)
blanchet@58061
   502
          |> SMT_Util.mk_symb_list @{typ pattern} o single
blanchet@58061
   503
          |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
blanchet@57230
   504
          |> (fn t => @{const trigger} $ t $ eq)
blanchet@56078
   505
      | mk_trigger t = t
blanchet@56078
   506
blanchet@57541
   507
    val (ctxt2, (ts3, ll_defs)) =
blanchet@56078
   508
      ts2
blanchet@56078
   509
      |> eta_expand ctxt1 funcs
blanchet@56078
   510
      |> rpair ctxt1
blanchet@56078
   511
      |-> Lambda_Lifting.lift_lambdas NONE is_binder
blanchet@57541
   512
      |-> (fn (ts', ll_defs) => fn ctxt' =>
blanchet@57541
   513
          (ctxt', (intro_explicit_application ctxt' funcs (map mk_trigger ll_defs @ ts'), ll_defs)))
blanchet@56078
   514
blanchet@56078
   515
    val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
blanchet@56104
   516
      |>> apfst (cons fun_app_eq)
blanchet@56078
   517
  in
blanchet@56078
   518
    (ts4, tr_context)
blanchet@58429
   519
    |-> intermediate logic dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
blanchet@57239
   520
    |>> uncurry (serialize smt_options comments)
blanchet@57541
   521
    ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
blanchet@56078
   522
  end
blanchet@56078
   523
blanchet@57229
   524
end;