src/HOL/Tools/SMT/smt_translate.ML
author wenzelm
Sun Nov 26 21:08:32 2017 +0100 (18 months ago)
changeset 67091 1393c2340eec
parent 66738 793e7a9c30c5
child 67149 e61557884799
permissions -rw-r--r--
more symbols;
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@66738
   253
  fun take_vars_into_account types t i =
blanchet@56078
   254
    let
blanchet@66738
   255
      fun find_min j (T as Type (@{type_name fun}, [_, T'])) =
blanchet@66738
   256
          if j = i orelse Typtab.defined types T then j else find_min (j + 1) T'
blanchet@66738
   257
        | find_min j _ = j
blanchet@66738
   258
    in find_min 0 (Term.type_of 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@66738
   271
    val explicit_application = Config.get ctxt SMT_Config.explicit_application
blanchet@66738
   272
    val get_arities =
blanchet@66738
   273
      (case explicit_application of
blanchet@66738
   274
        0 => min_arities
blanchet@66738
   275
      | 1 => min_arities
blanchet@66738
   276
      | 2 => K I
blanchet@66738
   277
      | n => error ("Illegal value for " ^ quote (Config.name_of SMT_Config.explicit_application) ^
blanchet@66738
   278
          ": " ^ string_of_int n))
blanchet@66738
   279
blanchet@66738
   280
    val (arities, types) = fold get_arities ts (Termtab.empty, Typtab.empty)
blanchet@66738
   281
    val arities' = arities |> explicit_application = 1 ? Termtab.map (take_vars_into_account types)
blanchet@56078
   282
blanchet@56078
   283
    fun app_func t T ts =
blanchet@56078
   284
      if is_some (Termtab.lookup funcs t) then Term.list_comb (t, ts)
blanchet@66738
   285
      else apply (the_default 0 (Termtab.lookup arities' t)) t T ts
blanchet@56078
   286
blanchet@58061
   287
    fun in_list T f t = SMT_Util.mk_symb_list T (map f (SMT_Util.dest_symb_list t))
blanchet@56078
   288
blanchet@56078
   289
    fun traverse Ts t =
blanchet@56078
   290
      (case Term.strip_comb t of
blanchet@56078
   291
        (q as Const (@{const_name All}, _), [Abs (x, T, u)]) =>
blanchet@56078
   292
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   293
      | (q as Const (@{const_name Ex}, _), [Abs (x, T, u)]) =>
blanchet@56078
   294
          q $ Abs (x, T, in_trigger (T :: Ts) u)
blanchet@56078
   295
      | (q as Const (@{const_name Let}, _), [u1, u2 as Abs _]) =>
blanchet@56078
   296
          q $ traverse Ts u1 $ traverse Ts u2
blanchet@56078
   297
      | (u as Const (c as (_, T)), ts) =>
blanchet@58061
   298
          (case SMT_Builtin.dest_builtin ctxt c ts of
blanchet@56078
   299
            SOME (_, k, us, mk) =>
blanchet@56078
   300
              let
blanchet@56078
   301
                val (ts1, ts2) = chop k (map (traverse Ts) us)
blanchet@56078
   302
                val U = Term.strip_type T |>> snd o chop k |> (op --->)
blanchet@56078
   303
              in apply 0 (mk ts1) U ts2 end
blanchet@56078
   304
          | NONE => app_func u T (map (traverse Ts) ts))
blanchet@56078
   305
      | (u as Free (_, T), ts) => app_func u T (map (traverse Ts) ts)
blanchet@56078
   306
      | (u as Bound i, ts) => apply 0 u (nth Ts i) (map (traverse Ts) ts)
blanchet@56078
   307
      | (Abs (n, T, u), ts) => traverses Ts (Abs (n, T, traverse (T::Ts) u)) ts
blanchet@56078
   308
      | (u, ts) => traverses Ts u ts)
blanchet@57230
   309
    and in_trigger Ts ((c as @{const trigger}) $ p $ t) = c $ in_pats Ts p $ traverse Ts t
blanchet@57165
   310
      | in_trigger Ts t = traverse Ts t
blanchet@56078
   311
    and in_pats Ts ps =
blanchet@57230
   312
      in_list @{typ "pattern symb_list"} (in_list @{typ pattern} (in_pat Ts)) ps
blanchet@57230
   313
    and in_pat Ts ((p as Const (@{const_name pat}, _)) $ t) = p $ traverse Ts t
blanchet@57230
   314
      | in_pat Ts ((p as Const (@{const_name nopat}, _)) $ t) = p $ traverse Ts t
blanchet@56078
   315
      | in_pat _ t = raise TERM ("bad pattern", [t])
blanchet@56078
   316
    and traverses Ts t ts = Term.list_comb (t, map (traverse Ts) ts)
blanchet@56078
   317
  in map (traverse []) ts end
blanchet@56078
   318
blanchet@57230
   319
val fun_app_eq = mk_meta_eq @{thm fun_app_def}
blanchet@56078
   320
blanchet@56078
   321
end
blanchet@56078
   322
blanchet@56078
   323
blanchet@56078
   324
(** map HOL formulas to FOL formulas (i.e., separate formulas froms terms) **)
blanchet@56078
   325
blanchet@56078
   326
local
blanchet@56078
   327
  val is_quant = member (op =) [@{const_name All}, @{const_name Ex}]
blanchet@56078
   328
blanchet@56078
   329
  val fol_rules = [
blanchet@56078
   330
    Let_def,
blanchet@61782
   331
    @{lemma "P = True == P" by (rule eq_reflection) simp}]
blanchet@56078
   332
blanchet@56078
   333
  exception BAD_PATTERN of unit
blanchet@56078
   334
blanchet@56078
   335
  fun is_builtin_conn_or_pred ctxt c ts =
blanchet@58061
   336
    is_some (SMT_Builtin.dest_builtin_conn ctxt c ts) orelse
blanchet@58061
   337
    is_some (SMT_Builtin.dest_builtin_pred ctxt c ts)
blanchet@56078
   338
in
blanchet@56078
   339
blanchet@56078
   340
fun folify ctxt =
blanchet@56078
   341
  let
blanchet@58061
   342
    fun in_list T f t = SMT_Util.mk_symb_list T (map_filter f (SMT_Util.dest_symb_list t))
blanchet@56078
   343
blanchet@56078
   344
    fun in_term pat t =
blanchet@56078
   345
      (case Term.strip_comb t of
blanchet@56078
   346
        (@{const True}, []) => t
blanchet@56078
   347
      | (@{const False}, []) => t
blanchet@56078
   348
      | (u as Const (@{const_name If}, _), [t1, t2, t3]) =>
blanchet@56104
   349
          if pat then raise BAD_PATTERN () else u $ in_form t1 $ in_term pat t2 $ in_term pat t3
blanchet@56078
   350
      | (Const (c as (n, _)), ts) =>
blanchet@61782
   351
          if is_builtin_conn_or_pred ctxt c ts orelse is_quant n then
blanchet@61782
   352
            if pat then raise BAD_PATTERN () else in_form t
blanchet@61782
   353
          else
blanchet@61782
   354
            Term.list_comb (Const c, map (in_term pat) ts)
blanchet@56078
   355
      | (Free c, ts) => Term.list_comb (Free c, map (in_term pat) ts)
blanchet@56078
   356
      | _ => t)
blanchet@56078
   357
blanchet@57230
   358
    and in_pat ((p as Const (@{const_name pat}, _)) $ t) =
blanchet@56078
   359
          p $ in_term true t
blanchet@57230
   360
      | in_pat ((p as Const (@{const_name nopat}, _)) $ t) =
blanchet@56078
   361
          p $ in_term true t
blanchet@56078
   362
      | in_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   363
blanchet@56078
   364
    and in_pats ps =
blanchet@57230
   365
      in_list @{typ "pattern symb_list"} (SOME o in_list @{typ pattern} (try in_pat)) ps
blanchet@56078
   366
blanchet@57230
   367
    and in_trigger ((c as @{const trigger}) $ p $ t) = c $ in_pats p $ in_form t
blanchet@57165
   368
      | in_trigger t = in_form t
blanchet@56078
   369
blanchet@56078
   370
    and in_form t =
blanchet@56078
   371
      (case Term.strip_comb t of
blanchet@56078
   372
        (q as Const (qn, _), [Abs (n, T, u)]) =>
blanchet@56078
   373
          if is_quant qn then q $ Abs (n, T, in_trigger u)
blanchet@56078
   374
          else in_term false t
blanchet@56078
   375
      | (Const c, ts) =>
blanchet@58061
   376
          (case SMT_Builtin.dest_builtin_conn ctxt c ts of
blanchet@56078
   377
            SOME (_, _, us, mk) => mk (map in_form us)
blanchet@56078
   378
          | NONE =>
blanchet@58061
   379
              (case SMT_Builtin.dest_builtin_pred ctxt c ts of
blanchet@56078
   380
                SOME (_, _, us, mk) => mk (map (in_term false) us)
blanchet@56078
   381
              | NONE => in_term false t))
blanchet@56078
   382
      | _ => in_term false t)
blanchet@56078
   383
  in
blanchet@56078
   384
    map in_form #>
blanchet@56078
   385
    pair (fol_rules, I)
blanchet@56078
   386
  end
blanchet@56078
   387
blanchet@56078
   388
end
blanchet@56078
   389
blanchet@56078
   390
blanchet@56078
   391
(* translation into intermediate format *)
blanchet@56078
   392
blanchet@56078
   393
(** utility functions **)
blanchet@56078
   394
blanchet@56078
   395
val quantifier = (fn
blanchet@56078
   396
    @{const_name All} => SOME SForall
blanchet@56078
   397
  | @{const_name Ex} => SOME SExists
blanchet@56078
   398
  | _ => NONE)
blanchet@56078
   399
blanchet@56078
   400
fun group_quant qname Ts (t as Const (q, _) $ Abs (_, T, u)) =
blanchet@56078
   401
      if q = qname then group_quant qname (T :: Ts) u else (Ts, t)
blanchet@56078
   402
  | group_quant _ Ts t = (Ts, t)
blanchet@56078
   403
blanchet@57230
   404
fun dest_pat (Const (@{const_name pat}, _) $ t) = (t, true)
blanchet@57230
   405
  | dest_pat (Const (@{const_name nopat}, _) $ t) = (t, false)
blanchet@56078
   406
  | dest_pat t = raise TERM ("bad pattern", [t])
blanchet@56078
   407
blanchet@56078
   408
fun dest_pats [] = I
blanchet@56078
   409
  | dest_pats ts =
blanchet@56078
   410
      (case map dest_pat ts |> split_list ||> distinct (op =) of
blanchet@56078
   411
        (ps, [true]) => cons (SPat ps)
blanchet@56078
   412
      | (ps, [false]) => cons (SNoPat ps)
blanchet@56078
   413
      | _ => raise TERM ("bad multi-pattern", ts))
blanchet@56078
   414
blanchet@57230
   415
fun dest_trigger (@{const trigger} $ tl $ t) =
blanchet@58061
   416
      (rev (fold (dest_pats o SMT_Util.dest_symb_list) (SMT_Util.dest_symb_list tl) []), t)
blanchet@56078
   417
  | dest_trigger t = ([], t)
blanchet@56078
   418
blanchet@56078
   419
fun dest_quant qn T t = quantifier qn |> Option.map (fn q =>
blanchet@56078
   420
  let
blanchet@56078
   421
    val (Ts, u) = group_quant qn [T] t
blanchet@56078
   422
    val (ps, p) = dest_trigger u
blanchet@57165
   423
  in (q, rev Ts, ps, p) end)
blanchet@56078
   424
blanchet@56078
   425
fun fold_map_pat f (SPat ts) = fold_map f ts #>> SPat
blanchet@56078
   426
  | fold_map_pat f (SNoPat ts) = fold_map f ts #>> SNoPat
blanchet@56078
   427
blanchet@56078
   428
blanchet@56078
   429
(** translation from Isabelle terms into SMT intermediate terms **)
blanchet@56078
   430
blanchet@58429
   431
fun intermediate logic dtyps builtin ctxt ts trx =
blanchet@56078
   432
  let
blanchet@56078
   433
    fun transT (T as TFree _) = add_typ T true
blanchet@56078
   434
      | transT (T as TVar _) = (fn _ => raise TYPE ("bad SMT type", [T], []))
blanchet@56078
   435
      | transT (T as Type _) =
blanchet@58061
   436
          (case SMT_Builtin.dest_builtin_typ ctxt T of
blanchet@66551
   437
            SOME (n, []) => pair n
blanchet@66551
   438
          | SOME (n, Ts) =>
blanchet@66551
   439
            fold_map transT Ts
blanchet@66551
   440
            #>> (fn ns => enclose "(" ")" (space_implode " " (n :: ns)))
blanchet@56078
   441
          | NONE => add_typ T true)
blanchet@56078
   442
blanchet@56078
   443
    fun trans t =
blanchet@56078
   444
      (case Term.strip_comb t of
blanchet@56078
   445
        (Const (qn, _), [Abs (_, T, t1)]) =>
blanchet@56078
   446
          (case dest_quant qn T t1 of
blanchet@57165
   447
            SOME (q, Ts, ps, b) =>
blanchet@56078
   448
              fold_map transT Ts ##>> fold_map (fold_map_pat trans) ps ##>>
blanchet@57165
   449
              trans b #>> (fn ((Ts', ps'), b') => SQua (q, Ts', ps', b'))
blanchet@56078
   450
          | NONE => raise TERM ("unsupported quantifier", [t]))
blanchet@56078
   451
      | (u as Const (c as (_, T)), ts) =>
blanchet@56078
   452
          (case builtin ctxt c ts of
blanchet@66551
   453
            SOME (n, _, us, _) => fold_map trans us #>> curry SConst n
blanchet@66551
   454
          | NONE => trans_applied_fun u T ts)
blanchet@66551
   455
      | (u as Free (_, T), ts) => trans_applied_fun u T ts
blanchet@66551
   456
      | (Bound i, ts) => pair i ##>> fold_map trans ts #>> SVar
blanchet@56078
   457
      | _ => raise TERM ("bad SMT term", [t]))
blanchet@57230
   458
blanchet@66551
   459
    and trans_applied_fun t T ts =
blanchet@58061
   460
      let val (Us, U) = SMT_Util.dest_funT (length ts) T
blanchet@56078
   461
      in
blanchet@56078
   462
        fold_map transT Us ##>> transT U #-> (fn Up =>
blanchet@66551
   463
          add_fun t (SOME Up) ##>> fold_map trans ts #>> SConst)
blanchet@56078
   464
      end
blanchet@56078
   465
blanchet@56078
   466
    val (us, trx') = fold_map trans ts trx
blanchet@58429
   467
  in ((sign_of (logic ts) dtyps trx', us), trx') end
blanchet@56078
   468
blanchet@56078
   469
blanchet@56078
   470
(* translation *)
blanchet@56078
   471
blanchet@56078
   472
structure Configs = Generic_Data
blanchet@56078
   473
(
blanchet@58061
   474
  type T = (Proof.context -> config) SMT_Util.dict
blanchet@56078
   475
  val empty = []
blanchet@56078
   476
  val extend = I
blanchet@58061
   477
  fun merge data = SMT_Util.dict_merge fst data
blanchet@56078
   478
)
blanchet@56078
   479
blanchet@58061
   480
fun add_config (cs, cfg) = Configs.map (SMT_Util.dict_update (cs, cfg))
blanchet@56078
   481
blanchet@57230
   482
fun get_config ctxt =
blanchet@58061
   483
  let val cs = SMT_Config.solver_class_of ctxt
blanchet@56078
   484
  in
blanchet@58061
   485
    (case SMT_Util.dict_get (Configs.get (Context.Proof ctxt)) cs of
blanchet@56078
   486
      SOME cfg => cfg ctxt
blanchet@56078
   487
    | NONE => error ("SMT: no translation configuration found " ^
blanchet@58061
   488
        "for solver class " ^ quote (SMT_Util.string_of_class cs)))
blanchet@56078
   489
  end
blanchet@56078
   490
blanchet@57239
   491
fun translate ctxt smt_options comments ithms =
blanchet@56078
   492
  let
blanchet@66551
   493
    val {order, logic, fp_kinds, serialize} = get_config ctxt
blanchet@56078
   494
blanchet@56078
   495
    fun no_dtyps (tr_context, ctxt) ts =
blanchet@58429
   496
      ((Termtab.empty, [], tr_context, ctxt), ts)
blanchet@56078
   497
blanchet@58061
   498
    val ts1 = map (Envir.beta_eta_contract o SMT_Util.prop_of o snd) ithms
blanchet@56078
   499
blanchet@58429
   500
    val ((funcs, dtyps, tr_context, ctxt1), ts2) =
blanchet@56078
   501
      ((empty_tr_context, ctxt), ts1)
blanchet@58361
   502
      |-> (if null fp_kinds then no_dtyps else collect_co_datatypes fp_kinds)
blanchet@56078
   503
blanchet@56078
   504
    fun is_binder (Const (@{const_name Let}, _) $ _) = true
blanchet@56078
   505
      | is_binder t = Lambda_Lifting.is_quantifier t
blanchet@56078
   506
blanchet@56078
   507
    fun mk_trigger ((q as Const (@{const_name All}, _)) $ Abs (n, T, t)) =
blanchet@56078
   508
          q $ Abs (n, T, mk_trigger t)
blanchet@56078
   509
      | mk_trigger (eq as (Const (@{const_name HOL.eq}, T) $ lhs $ _)) =
blanchet@57230
   510
          Term.domain_type T --> @{typ pattern}
blanchet@57230
   511
          |> (fn T => Const (@{const_name pat}, T) $ lhs)
blanchet@58061
   512
          |> SMT_Util.mk_symb_list @{typ pattern} o single
blanchet@58061
   513
          |> SMT_Util.mk_symb_list @{typ "pattern symb_list"} o single
blanchet@57230
   514
          |> (fn t => @{const trigger} $ t $ eq)
blanchet@56078
   515
      | mk_trigger t = t
blanchet@56078
   516
blanchet@57541
   517
    val (ctxt2, (ts3, ll_defs)) =
blanchet@56078
   518
      ts2
blanchet@56078
   519
      |> eta_expand ctxt1 funcs
blanchet@56078
   520
      |> rpair ctxt1
blanchet@56078
   521
      |-> Lambda_Lifting.lift_lambdas NONE is_binder
blanchet@57541
   522
      |-> (fn (ts', ll_defs) => fn ctxt' =>
blanchet@66551
   523
        let
blanchet@66551
   524
          val ts'' = map mk_trigger ll_defs @ ts'
blanchet@66551
   525
            |> order = SMT_Util.First_Order ? intro_explicit_application ctxt' funcs
blanchet@66551
   526
        in
blanchet@66551
   527
          (ctxt', (ts'', ll_defs))
blanchet@66551
   528
        end)
blanchet@56078
   529
    val ((rewrite_rules, builtin), ts4) = folify ctxt2 ts3
blanchet@66551
   530
      |>> order = SMT_Util.First_Order ? apfst (cons fun_app_eq)
blanchet@56078
   531
  in
blanchet@56078
   532
    (ts4, tr_context)
blanchet@58429
   533
    |-> intermediate logic dtyps (builtin SMT_Builtin.dest_builtin) ctxt2
blanchet@57239
   534
    |>> uncurry (serialize smt_options comments)
blanchet@57541
   535
    ||> replay_data_of ctxt2 ll_defs rewrite_rules ithms
blanchet@56078
   536
  end
blanchet@56078
   537
blanchet@57229
   538
end;