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