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