src/HOL/Tools/SMT2/z3_new_proof.ML
author wenzelm
Fri Mar 21 20:33:56 2014 +0100 (2014-03-21)
changeset 56245 84fc7dfa3cd4
parent 56122 40f7b45b2472
child 56811 b66639331db5
permissions -rw-r--r--
more qualified names;
blanchet@56078
     1
(*  Title:      HOL/Tools/SMT2/z3_new_proof.ML
blanchet@56078
     2
    Author:     Sascha Boehme, TU Muenchen
blanchet@56078
     3
blanchet@56078
     4
Z3 proofs: parsing and abstract syntax tree.
blanchet@56078
     5
*)
blanchet@56078
     6
blanchet@56078
     7
signature Z3_NEW_PROOF =
blanchet@56078
     8
sig
blanchet@56078
     9
  (*proof rules*)
blanchet@56078
    10
  datatype z3_rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
blanchet@56078
    11
    Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
blanchet@56078
    12
    Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
blanchet@56078
    13
    Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars |
blanchet@56078
    14
    Dest_Eq_Res | Quant_Inst | Hypothesis | Lemma | Unit_Resolution |
blanchet@56078
    15
    Iff_True | Iff_False | Commutativity | Def_Axiom | Intro_Def | Apply_Def |
blanchet@56078
    16
    Iff_Oeq | Nnf_Pos | Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize |
blanchet@56078
    17
    Modus_Ponens_Oeq | Th_Lemma of string
blanchet@56078
    18
  val string_of_rule: z3_rule -> string
blanchet@56078
    19
blanchet@56078
    20
  (*proofs*)
blanchet@56078
    21
  datatype z3_step = Z3_Step of {
blanchet@56078
    22
    id: int,
blanchet@56078
    23
    rule: z3_rule,
blanchet@56078
    24
    prems: int list,
blanchet@56078
    25
    concl: term,
blanchet@56078
    26
    fixes: string list,
blanchet@56078
    27
    is_fix_step: bool}
blanchet@56078
    28
blanchet@56078
    29
  (*type and term parsers*)
blanchet@56078
    30
  type type_parser = SMTLIB2.tree * typ list -> typ option
blanchet@56078
    31
  type term_parser = SMTLIB2.tree * term list -> term option
blanchet@56078
    32
  val add_type_parser: type_parser -> Context.generic -> Context.generic
blanchet@56078
    33
  val add_term_parser: term_parser -> Context.generic -> Context.generic
blanchet@56078
    34
blanchet@56078
    35
  (*proof parser*)
blanchet@56078
    36
  val parse: typ Symtab.table -> term Symtab.table -> string list ->
blanchet@56078
    37
    Proof.context -> z3_step list * Proof.context
blanchet@56078
    38
end
blanchet@56078
    39
blanchet@56078
    40
structure Z3_New_Proof: Z3_NEW_PROOF =
blanchet@56078
    41
struct
blanchet@56078
    42
blanchet@56078
    43
(* proof rules *)
blanchet@56078
    44
blanchet@56078
    45
datatype z3_rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
blanchet@56078
    46
  Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
blanchet@56078
    47
  Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
blanchet@56078
    48
  Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars | Dest_Eq_Res |
blanchet@56078
    49
  Quant_Inst | Hypothesis | Lemma | Unit_Resolution | Iff_True | Iff_False |
blanchet@56078
    50
  Commutativity | Def_Axiom | Intro_Def | Apply_Def | Iff_Oeq | Nnf_Pos |
blanchet@56078
    51
  Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize | Modus_Ponens_Oeq |
blanchet@56078
    52
  Th_Lemma of string
blanchet@56078
    53
  (* TODO: some proof rules come with further information
blanchet@56078
    54
     that is currently dropped by the parser *)
blanchet@56078
    55
blanchet@56078
    56
val rule_names = Symtab.make [
blanchet@56078
    57
  ("true-axiom", True_Axiom),
blanchet@56078
    58
  ("asserted", Asserted),
blanchet@56078
    59
  ("goal", Goal),
blanchet@56078
    60
  ("mp", Modus_Ponens),
blanchet@56078
    61
  ("refl", Reflexivity),
blanchet@56078
    62
  ("symm", Symmetry),
blanchet@56078
    63
  ("trans", Transitivity),
blanchet@56078
    64
  ("trans*", Transitivity_Star),
blanchet@56078
    65
  ("monotonicity", Monotonicity),
blanchet@56078
    66
  ("quant-intro", Quant_Intro),
blanchet@56078
    67
  ("distributivity", Distributivity),
blanchet@56078
    68
  ("and-elim", And_Elim),
blanchet@56078
    69
  ("not-or-elim", Not_Or_Elim),
blanchet@56078
    70
  ("rewrite", Rewrite),
blanchet@56078
    71
  ("rewrite*", Rewrite_Star),
blanchet@56078
    72
  ("pull-quant", Pull_Quant),
blanchet@56078
    73
  ("pull-quant*", Pull_Quant_Star),
blanchet@56078
    74
  ("push-quant", Push_Quant),
blanchet@56078
    75
  ("elim-unused", Elim_Unused_Vars),
blanchet@56078
    76
  ("der", Dest_Eq_Res),
blanchet@56078
    77
  ("quant-inst", Quant_Inst),
blanchet@56078
    78
  ("hypothesis", Hypothesis),
blanchet@56078
    79
  ("lemma", Lemma),
blanchet@56078
    80
  ("unit-resolution", Unit_Resolution),
blanchet@56078
    81
  ("iff-true", Iff_True),
blanchet@56078
    82
  ("iff-false", Iff_False),
blanchet@56078
    83
  ("commutativity", Commutativity),
blanchet@56078
    84
  ("def-axiom", Def_Axiom),
blanchet@56078
    85
  ("intro-def", Intro_Def),
blanchet@56078
    86
  ("apply-def", Apply_Def),
blanchet@56078
    87
  ("iff~", Iff_Oeq),
blanchet@56078
    88
  ("nnf-pos", Nnf_Pos),
blanchet@56078
    89
  ("nnf-neg", Nnf_Neg),
blanchet@56078
    90
  ("nnf*", Nnf_Star),
blanchet@56078
    91
  ("cnf*", Cnf_Star),
blanchet@56078
    92
  ("sk", Skolemize),
blanchet@56078
    93
  ("mp~", Modus_Ponens_Oeq)]
blanchet@56078
    94
blanchet@56078
    95
fun rule_of_string name =
blanchet@56078
    96
  (case Symtab.lookup rule_names name of
blanchet@56078
    97
    SOME rule => rule
blanchet@56078
    98
  | NONE => error ("unknown Z3 proof rule " ^ quote name))
blanchet@56078
    99
blanchet@56078
   100
fun string_of_rule (Th_Lemma kind) = "th-lemma " ^ kind
blanchet@56078
   101
  | string_of_rule r =
blanchet@56078
   102
      let fun eq_rule (s, r') = if r = r' then SOME s else NONE 
blanchet@56078
   103
      in the (Symtab.get_first eq_rule rule_names) end
blanchet@56078
   104
blanchet@56078
   105
blanchet@56078
   106
blanchet@56078
   107
(* proofs *)
blanchet@56078
   108
blanchet@56078
   109
datatype z3_node = Z3_Node of {
blanchet@56078
   110
  id: int,
blanchet@56078
   111
  rule: z3_rule,
blanchet@56078
   112
  prems: z3_node list,
blanchet@56078
   113
  concl: term,
blanchet@56078
   114
  bounds: string list}
blanchet@56078
   115
blanchet@56078
   116
fun mk_node id rule prems concl bounds =
blanchet@56078
   117
  Z3_Node {id=id, rule=rule, prems=prems, concl=concl, bounds=bounds}
blanchet@56078
   118
blanchet@56078
   119
datatype z3_step = Z3_Step of {
blanchet@56078
   120
  id: int,
blanchet@56078
   121
  rule: z3_rule,
blanchet@56078
   122
  prems: int list,
blanchet@56078
   123
  concl: term,
blanchet@56078
   124
  fixes: string list,
blanchet@56078
   125
  is_fix_step: bool}
blanchet@56078
   126
blanchet@56078
   127
fun mk_step id rule prems concl fixes is_fix_step =
blanchet@56078
   128
  Z3_Step {id=id, rule=rule, prems=prems, concl=concl, fixes=fixes,
blanchet@56078
   129
    is_fix_step=is_fix_step}
blanchet@56078
   130
blanchet@56078
   131
blanchet@56078
   132
blanchet@56078
   133
(* core type and term parser *)
blanchet@56078
   134
blanchet@56078
   135
fun core_type_parser (SMTLIB2.Sym "Bool", []) = SOME @{typ HOL.bool}
blanchet@56078
   136
  | core_type_parser (SMTLIB2.Sym "Int", []) = SOME @{typ Int.int}
blanchet@56078
   137
  | core_type_parser _ = NONE
blanchet@56078
   138
blanchet@56078
   139
fun mk_unary n t =
blanchet@56078
   140
  let val T = fastype_of t
blanchet@56078
   141
  in Const (n, T --> T) $ t end
blanchet@56078
   142
blanchet@56078
   143
fun mk_binary' n T U t1 t2 = Const (n, [T, T] ---> U) $ t1 $ t2
blanchet@56078
   144
blanchet@56078
   145
fun mk_binary n t1 t2 =
blanchet@56078
   146
  let val T = fastype_of t1
blanchet@56078
   147
  in mk_binary' n T T t1 t2 end
blanchet@56078
   148
blanchet@56078
   149
fun mk_rassoc f t ts =
blanchet@56078
   150
  let val us = rev (t :: ts)
blanchet@56078
   151
  in fold f (tl us) (hd us) end
blanchet@56078
   152
blanchet@56078
   153
fun mk_lassoc f t ts = fold (fn u1 => fn u2 => f u2 u1) ts t
blanchet@56078
   154
blanchet@56078
   155
fun mk_lassoc' n = mk_lassoc (mk_binary n)
blanchet@56078
   156
blanchet@56078
   157
fun mk_binary_pred n S t1 t2 =
blanchet@56078
   158
  let
blanchet@56078
   159
    val T1 = fastype_of t1
blanchet@56078
   160
    val T2 = fastype_of t2
blanchet@56078
   161
    val T =
blanchet@56078
   162
      if T1 <> Term.dummyT then T1
blanchet@56078
   163
      else if T2 <> Term.dummyT then T2
blanchet@56078
   164
      else TVar (("?a", serial ()), S)
blanchet@56078
   165
  in mk_binary' n T @{typ HOL.bool} t1 t2 end
blanchet@56078
   166
blanchet@56078
   167
fun mk_less t1 t2 = mk_binary_pred @{const_name ord_class.less} @{sort linorder} t1 t2
blanchet@56078
   168
fun mk_less_eq t1 t2 = mk_binary_pred @{const_name ord_class.less_eq} @{sort linorder} t1 t2
blanchet@56078
   169
blanchet@56078
   170
fun core_term_parser (SMTLIB2.Sym "true", _) = SOME @{const HOL.True}
blanchet@56078
   171
  | core_term_parser (SMTLIB2.Sym "false", _) = SOME @{const HOL.False}
blanchet@56078
   172
  | core_term_parser (SMTLIB2.Sym "not", [t]) = SOME (HOLogic.mk_not t)
blanchet@56078
   173
  | core_term_parser (SMTLIB2.Sym "and", t :: ts) = SOME (mk_rassoc (curry HOLogic.mk_conj) t ts)
blanchet@56078
   174
  | core_term_parser (SMTLIB2.Sym "or", t :: ts) = SOME (mk_rassoc (curry HOLogic.mk_disj) t ts)
blanchet@56078
   175
  | core_term_parser (SMTLIB2.Sym "=>", [t1, t2]) = SOME (HOLogic.mk_imp (t1, t2))
blanchet@56078
   176
  | core_term_parser (SMTLIB2.Sym "implies", [t1, t2]) = SOME (HOLogic.mk_imp (t1, t2))
blanchet@56078
   177
  | core_term_parser (SMTLIB2.Sym "=", [t1, t2]) = SOME (HOLogic.mk_eq (t1, t2))
blanchet@56078
   178
  | core_term_parser (SMTLIB2.Sym "~", [t1, t2]) = SOME (HOLogic.mk_eq (t1, t2))
blanchet@56078
   179
  | core_term_parser (SMTLIB2.Sym "ite", [t1, t2, t3]) =
blanchet@56078
   180
      let
blanchet@56078
   181
        val T = fastype_of t2
blanchet@56078
   182
        val c = Const (@{const_name HOL.If}, [@{typ HOL.bool}, T, T] ---> T)
blanchet@56078
   183
      in SOME (c $ t1 $ t2 $ t3) end
blanchet@56078
   184
  | core_term_parser (SMTLIB2.Num i, []) = SOME (HOLogic.mk_number @{typ Int.int} i)
blanchet@56078
   185
  | core_term_parser (SMTLIB2.Sym "-", [t]) = SOME (mk_unary @{const_name uminus_class.uminus} t)
blanchet@56078
   186
  | core_term_parser (SMTLIB2.Sym "~", [t]) = SOME (mk_unary @{const_name uminus_class.uminus} t)
blanchet@56078
   187
  | core_term_parser (SMTLIB2.Sym "+", t :: ts) =
blanchet@56078
   188
      SOME (mk_lassoc' @{const_name plus_class.plus} t ts)
blanchet@56078
   189
  | core_term_parser (SMTLIB2.Sym "-", t :: ts) =
blanchet@56078
   190
      SOME (mk_lassoc' @{const_name minus_class.minus} t ts)
blanchet@56078
   191
  | core_term_parser (SMTLIB2.Sym "*", t :: ts) =
blanchet@56078
   192
      SOME (mk_lassoc' @{const_name times_class.times} t ts)
blanchet@56078
   193
  | core_term_parser (SMTLIB2.Sym "div", [t1, t2]) = SOME (mk_binary @{const_name SMT2.z3div} t1 t2)
blanchet@56078
   194
  | core_term_parser (SMTLIB2.Sym "mod", [t1, t2]) = SOME (mk_binary @{const_name SMT2.z3mod} t1 t2)
blanchet@56078
   195
  | core_term_parser (SMTLIB2.Sym "<", [t1, t2]) = SOME (mk_less t1 t2)
blanchet@56078
   196
  | core_term_parser (SMTLIB2.Sym ">", [t1, t2]) = SOME (mk_less t2 t1)
blanchet@56078
   197
  | core_term_parser (SMTLIB2.Sym "<=", [t1, t2]) = SOME (mk_less_eq t1 t2)
blanchet@56078
   198
  | core_term_parser (SMTLIB2.Sym ">=", [t1, t2]) = SOME (mk_less_eq t2 t1)
blanchet@56078
   199
  | core_term_parser _ = NONE
blanchet@56078
   200
blanchet@56078
   201
blanchet@56078
   202
blanchet@56078
   203
(* type and term parsers *)
blanchet@56078
   204
blanchet@56078
   205
type type_parser = SMTLIB2.tree * typ list -> typ option
blanchet@56078
   206
blanchet@56078
   207
type term_parser = SMTLIB2.tree * term list -> term option
blanchet@56078
   208
blanchet@56078
   209
fun id_ord ((id1, _), (id2, _)) = int_ord (id1, id2)
blanchet@56078
   210
blanchet@56078
   211
structure Parsers = Generic_Data
blanchet@56078
   212
(
blanchet@56078
   213
  type T = (int * type_parser) list * (int * term_parser) list
blanchet@56122
   214
  val empty : T = ([(serial (), core_type_parser)], [(serial (), core_term_parser)])
blanchet@56078
   215
  val extend = I
blanchet@56078
   216
  fun merge ((tys1, ts1), (tys2, ts2)) =
blanchet@56078
   217
    (Ord_List.merge id_ord (tys1, tys2), Ord_List.merge id_ord (ts1, ts2))
blanchet@56078
   218
)
blanchet@56078
   219
blanchet@56078
   220
fun add_type_parser type_parser =
blanchet@56078
   221
  Parsers.map (apfst (Ord_List.insert id_ord (serial (), type_parser)))
blanchet@56078
   222
blanchet@56078
   223
fun add_term_parser term_parser =
blanchet@56078
   224
  Parsers.map (apsnd (Ord_List.insert id_ord (serial (), term_parser)))
blanchet@56078
   225
blanchet@56078
   226
fun get_type_parsers ctxt = map snd (fst (Parsers.get (Context.Proof ctxt)))
blanchet@56078
   227
fun get_term_parsers ctxt = map snd (snd (Parsers.get (Context.Proof ctxt)))
blanchet@56078
   228
blanchet@56078
   229
fun apply_parsers parsers x =
blanchet@56078
   230
  let
blanchet@56078
   231
    fun apply [] = NONE
blanchet@56078
   232
      | apply (parser :: parsers) =
blanchet@56078
   233
          (case parser x of
blanchet@56078
   234
            SOME y => SOME y
blanchet@56078
   235
          | NONE => apply parsers)
blanchet@56078
   236
  in apply parsers end
blanchet@56078
   237
blanchet@56078
   238
blanchet@56078
   239
blanchet@56078
   240
(* proof parser context *)
blanchet@56078
   241
blanchet@56078
   242
datatype shared = Tree of SMTLIB2.tree | Term of term | Proof of z3_node | None
blanchet@56078
   243
blanchet@56078
   244
type 'a context = {
blanchet@56078
   245
  ctxt: Proof.context,
blanchet@56078
   246
  id: int,
blanchet@56078
   247
  syms: shared Symtab.table,
blanchet@56078
   248
  typs: typ Symtab.table,
blanchet@56078
   249
  funs: term Symtab.table,
blanchet@56078
   250
  extra: 'a}
blanchet@56078
   251
blanchet@56078
   252
fun mk_context ctxt id syms typs funs extra: 'a context =
blanchet@56078
   253
  {ctxt=ctxt, id=id, syms=syms, typs=typs, funs=funs, extra=extra}
blanchet@56078
   254
blanchet@56078
   255
fun empty_context ctxt typs funs = mk_context ctxt 1 Symtab.empty typs funs []
blanchet@56078
   256
blanchet@56078
   257
fun ctxt_of ({ctxt, ...}: 'a context) = ctxt
blanchet@56078
   258
blanchet@56078
   259
fun next_id ({ctxt, id, syms, typs, funs, extra}: 'a context) =
blanchet@56078
   260
  (id, mk_context ctxt (id + 1) syms typs funs extra)
blanchet@56078
   261
blanchet@56078
   262
fun lookup_binding ({syms, ...}: 'a context) =
blanchet@56078
   263
  the_default None o Symtab.lookup syms
blanchet@56078
   264
blanchet@56078
   265
fun map_syms f ({ctxt, id, syms, typs, funs, extra}: 'a context) =
blanchet@56078
   266
  mk_context ctxt id (f syms) typs funs extra
blanchet@56078
   267
blanchet@56078
   268
fun update_binding b = map_syms (Symtab.update b)
blanchet@56078
   269
blanchet@56078
   270
fun with_bindings bs f cx =
blanchet@56078
   271
  let val bs' = map (lookup_binding cx o fst) bs
blanchet@56078
   272
  in
blanchet@56078
   273
    cx
blanchet@56078
   274
    |> fold update_binding bs
blanchet@56078
   275
    |> f
blanchet@56078
   276
    ||> fold2 (fn (name, _) => update_binding o pair name) bs bs'
blanchet@56078
   277
  end
blanchet@56078
   278
blanchet@56078
   279
fun lookup_typ ({typs, ...}: 'a context) = Symtab.lookup typs
blanchet@56078
   280
fun lookup_fun ({funs, ...}: 'a context) = Symtab.lookup funs
blanchet@56078
   281
blanchet@56078
   282
fun fresh_fun add name n T ({ctxt, id, syms, typs, funs, extra}: 'a context) =
blanchet@56078
   283
  let
blanchet@56078
   284
    val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
blanchet@56078
   285
    val t = Free (n', T)
blanchet@56078
   286
    val funs' = Symtab.update (name, t) funs
blanchet@56078
   287
  in (t, mk_context ctxt' id syms typs funs' (add (n', T) extra)) end
blanchet@56078
   288
blanchet@56078
   289
fun declare_fun name n T = snd o fresh_fun cons name n T
blanchet@56078
   290
fun declare_free name n T = fresh_fun (cons o pair name) name n T
blanchet@56078
   291
blanchet@56078
   292
fun with_fresh_names f ({ctxt, id, syms, typs, funs, extra}: 'a context) =
blanchet@56078
   293
  let
blanchet@56078
   294
    fun bind (_, v as (_, T)) t = Logic.all_const T $ Term.absfree v t
blanchet@56078
   295
blanchet@56078
   296
    val needs_inferT = equal Term.dummyT orf Term.is_TVar
blanchet@56078
   297
    val needs_infer = Term.exists_type (Term.exists_subtype needs_inferT)
blanchet@56078
   298
    fun infer_types ctxt =
blanchet@56078
   299
      singleton (Type_Infer_Context.infer_types ctxt) #>
blanchet@56078
   300
      singleton (Proof_Context.standard_term_check_finish ctxt)
blanchet@56078
   301
    fun infer ctxt t = if needs_infer t then infer_types ctxt t else t
blanchet@56078
   302
blanchet@56078
   303
    type bindings = (string * (string * typ)) list
blanchet@56078
   304
    val (t, {ctxt=ctxt', extra=names, ...}: bindings context) =
blanchet@56078
   305
      f (mk_context ctxt id syms typs funs [])
blanchet@56078
   306
    val t' = infer ctxt' (fold_rev bind names (HOLogic.mk_Trueprop t))
blanchet@56078
   307
   
blanchet@56078
   308
  in ((t', map fst names), mk_context ctxt id syms typs funs extra) end
blanchet@56078
   309
blanchet@56078
   310
blanchet@56078
   311
blanchet@56078
   312
(* proof parser *)
blanchet@56078
   313
blanchet@56078
   314
exception Z3_PARSE of string * SMTLIB2.tree
blanchet@56078
   315
blanchet@56078
   316
val desymbolize = Name.desymbolize false o perhaps (try (unprefix "?"))
blanchet@56078
   317
blanchet@56078
   318
fun parse_type cx ty Ts =
blanchet@56078
   319
  (case apply_parsers (get_type_parsers (ctxt_of cx)) (ty, Ts) of
blanchet@56078
   320
    SOME T => T
blanchet@56078
   321
  | NONE =>
blanchet@56078
   322
      (case ty of
blanchet@56078
   323
        SMTLIB2.Sym name =>
blanchet@56078
   324
          (case lookup_typ cx name of
blanchet@56078
   325
            SOME T => T
blanchet@56078
   326
          | NONE => raise Z3_PARSE ("unknown Z3 type", ty))
blanchet@56078
   327
      | _ => raise Z3_PARSE ("bad Z3 type format", ty)))
blanchet@56078
   328
blanchet@56078
   329
fun parse_term t ts cx =
blanchet@56078
   330
  (case apply_parsers (get_term_parsers (ctxt_of cx)) (t, ts) of
blanchet@56078
   331
    SOME u => (u, cx)
blanchet@56078
   332
  | NONE =>
blanchet@56078
   333
      (case t of
blanchet@56078
   334
        SMTLIB2.Sym name =>
blanchet@56078
   335
          (case lookup_fun cx name of
blanchet@56078
   336
            SOME u => (Term.list_comb (u, ts), cx)
blanchet@56078
   337
          | NONE =>
blanchet@56078
   338
              if null ts then declare_free name (desymbolize name) Term.dummyT cx
blanchet@56078
   339
              else raise Z3_PARSE ("bad Z3 term", t))
blanchet@56078
   340
      | _ => raise Z3_PARSE ("bad Z3 term format", t)))
blanchet@56078
   341
blanchet@56078
   342
fun type_of cx ty =
blanchet@56078
   343
  (case try (parse_type cx ty) [] of
blanchet@56078
   344
    SOME T => T
blanchet@56078
   345
  | NONE =>
blanchet@56078
   346
      (case ty of
blanchet@56078
   347
        SMTLIB2.S (ty' :: tys) => parse_type cx ty' (map (type_of cx) tys)
blanchet@56078
   348
      | _ => raise Z3_PARSE ("bad Z3 type", ty)))
blanchet@56078
   349
blanchet@56078
   350
fun dest_var cx (SMTLIB2.S [SMTLIB2.Sym name, ty]) = (name, (desymbolize name, type_of cx ty))
blanchet@56078
   351
  | dest_var _ v = raise Z3_PARSE ("bad Z3 quantifier variable format", v)
blanchet@56078
   352
blanchet@56078
   353
fun dest_body (SMTLIB2.S (SMTLIB2.Sym "!" :: body :: _)) = dest_body body
blanchet@56078
   354
  | dest_body body = body
blanchet@56078
   355
blanchet@56078
   356
fun dest_binding (SMTLIB2.S [SMTLIB2.Sym name, t]) = (name, Tree t)
blanchet@56078
   357
  | dest_binding b = raise Z3_PARSE ("bad Z3 let binding format", b)
blanchet@56078
   358
blanchet@56078
   359
fun term_of t cx =
blanchet@56078
   360
  (case t of
blanchet@56078
   361
    SMTLIB2.S [SMTLIB2.Sym "forall", SMTLIB2.S vars, body] =>
blanchet@56078
   362
      quant HOLogic.mk_all vars body cx
blanchet@56078
   363
  | SMTLIB2.S [SMTLIB2.Sym "exists", SMTLIB2.S vars, body] =>
blanchet@56078
   364
      quant HOLogic.mk_exists vars body cx
blanchet@56078
   365
  | SMTLIB2.S [SMTLIB2.Sym "let", SMTLIB2.S bindings, body] =>
blanchet@56078
   366
      with_bindings (map dest_binding bindings) (term_of body) cx
blanchet@56078
   367
  | SMTLIB2.S (SMTLIB2.Sym "!" :: t :: _) => term_of t cx
blanchet@56078
   368
  | SMTLIB2.S (f :: args) =>
blanchet@56078
   369
      cx
blanchet@56078
   370
      |> fold_map term_of args
blanchet@56078
   371
      |-> parse_term f
blanchet@56078
   372
  | SMTLIB2.Sym name =>
blanchet@56078
   373
      (case lookup_binding cx name of
blanchet@56078
   374
        Tree u =>
blanchet@56078
   375
          cx
blanchet@56078
   376
          |> term_of u
blanchet@56078
   377
          |-> (fn u' => pair u' o update_binding (name, Term u'))
blanchet@56078
   378
      | Term u => (u, cx)
blanchet@56078
   379
      | None => parse_term t [] cx
blanchet@56078
   380
      | _ => raise Z3_PARSE ("bad Z3 term format", t))
blanchet@56078
   381
  | _ => parse_term t [] cx)
blanchet@56078
   382
blanchet@56078
   383
and quant q vars body cx =
blanchet@56078
   384
  let val vs = map (dest_var cx) vars
blanchet@56078
   385
  in
blanchet@56078
   386
    cx
blanchet@56078
   387
    |> with_bindings (map (apsnd (Term o Free)) vs) (term_of (dest_body body))
blanchet@56078
   388
    |>> fold_rev (fn (_, (n, T)) => fn t => q (n, T, t)) vs
blanchet@56078
   389
  end
blanchet@56078
   390
blanchet@56078
   391
fun rule_of (SMTLIB2.Sym name) = rule_of_string name
blanchet@56078
   392
  | rule_of (SMTLIB2.S (SMTLIB2.Sym "_" :: SMTLIB2.Sym name :: args)) =
blanchet@56078
   393
      (case (name, args) of
blanchet@56078
   394
        ("th-lemma", SMTLIB2.Sym kind :: _) => Th_Lemma kind
blanchet@56078
   395
      | _ => rule_of_string name)
blanchet@56078
   396
  | rule_of r = raise Z3_PARSE ("bad Z3 proof rule format", r)
blanchet@56078
   397
blanchet@56078
   398
fun node_of p cx =
blanchet@56078
   399
  (case p of
blanchet@56078
   400
    SMTLIB2.Sym name =>
blanchet@56078
   401
      (case lookup_binding cx name of
blanchet@56078
   402
        Proof node => (node, cx)
blanchet@56078
   403
      | Tree p' =>
blanchet@56078
   404
          cx
blanchet@56078
   405
          |> node_of p'
blanchet@56078
   406
          |-> (fn node => pair node o update_binding (name, Proof node))
blanchet@56078
   407
      | _ => raise Z3_PARSE ("bad Z3 proof format", p))
blanchet@56078
   408
  | SMTLIB2.S [SMTLIB2.Sym "let", SMTLIB2.S bindings, p] =>
blanchet@56078
   409
      with_bindings (map dest_binding bindings) (node_of p) cx
blanchet@56078
   410
  | SMTLIB2.S (name :: parts) =>
blanchet@56078
   411
      let
blanchet@56078
   412
        val (ps, p) = split_last parts
blanchet@56078
   413
        val r = rule_of name
blanchet@56078
   414
      in
blanchet@56078
   415
        cx
blanchet@56078
   416
        |> fold_map node_of ps
blanchet@56078
   417
        ||>> with_fresh_names (term_of p)
blanchet@56078
   418
        ||>> next_id
blanchet@56078
   419
        |>> (fn ((prems, (t, ns)), id) => mk_node id r prems t ns)
blanchet@56078
   420
      end
blanchet@56078
   421
  | _ => raise Z3_PARSE ("bad Z3 proof format", p))
blanchet@56078
   422
blanchet@56078
   423
fun dest_name (SMTLIB2.Sym name) = name
blanchet@56078
   424
  | dest_name t = raise Z3_PARSE ("bad name", t)
blanchet@56078
   425
blanchet@56078
   426
fun dest_seq (SMTLIB2.S ts) = ts
blanchet@56078
   427
  | dest_seq t = raise Z3_PARSE ("bad Z3 proof format", t)
blanchet@56078
   428
blanchet@56078
   429
fun parse' (SMTLIB2.S (SMTLIB2.Sym "set-logic" :: _) :: ts) cx = parse' ts cx
blanchet@56078
   430
  | parse' (SMTLIB2.S [SMTLIB2.Sym "declare-fun", n, tys, ty] :: ts) cx =
blanchet@56078
   431
      let
blanchet@56078
   432
        val name = dest_name n
blanchet@56078
   433
        val Ts = map (type_of cx) (dest_seq tys)
blanchet@56078
   434
        val T = type_of cx ty
blanchet@56078
   435
      in parse' ts (declare_fun name (desymbolize name) (Ts ---> T) cx) end
blanchet@56078
   436
  | parse' (SMTLIB2.S [SMTLIB2.Sym "proof", p] :: _) cx = node_of p cx
blanchet@56078
   437
  | parse' ts _ = raise Z3_PARSE ("bad Z3 proof declarations", SMTLIB2.S ts)
blanchet@56078
   438
blanchet@56078
   439
fun parse_proof typs funs lines ctxt =
blanchet@56078
   440
  let
blanchet@56078
   441
    val ts = dest_seq (SMTLIB2.parse lines)
blanchet@56078
   442
    val (node, cx) = parse' ts (empty_context ctxt typs funs)
blanchet@56078
   443
  in (node, ctxt_of cx) end
blanchet@56078
   444
  handle SMTLIB2.PARSE (l, msg) =>
blanchet@56078
   445
           error ("parsing error at line " ^ string_of_int l ^ ": " ^ msg)
blanchet@56078
   446
       | Z3_PARSE (msg, t) =>
blanchet@56078
   447
           error (msg ^ ": " ^ SMTLIB2.str_of t)
blanchet@56078
   448
blanchet@56078
   449
blanchet@56078
   450
blanchet@56078
   451
(* handling of bound variables *)
blanchet@56078
   452
blanchet@56078
   453
fun subst_of tyenv =
blanchet@56078
   454
  let fun add (ix, (S, T)) = cons (TVar (ix, S), T)
blanchet@56078
   455
  in Vartab.fold add tyenv [] end
blanchet@56078
   456
blanchet@56078
   457
fun substTs_same subst = 
blanchet@56078
   458
  let val applyT = Same.function (AList.lookup (op =) subst)
blanchet@56078
   459
  in Term_Subst.map_atypsT_same applyT end
blanchet@56078
   460
blanchet@56078
   461
fun subst_types ctxt env bounds t =
blanchet@56078
   462
  let
blanchet@56078
   463
    val match = Sign.typ_match (Proof_Context.theory_of ctxt)
blanchet@56078
   464
blanchet@56078
   465
    val t' = singleton (Variable.polymorphic ctxt) t
wenzelm@56245
   466
    val patTs = map snd (Term.strip_qnt_vars @{const_name Pure.all} t')
blanchet@56078
   467
    val objTs = map (the o Symtab.lookup env) bounds
blanchet@56078
   468
    val subst = subst_of (fold match (patTs ~~ objTs) Vartab.empty)
blanchet@56078
   469
  in Same.commit (Term_Subst.map_types_same (substTs_same subst)) t' end
blanchet@56078
   470
blanchet@56078
   471
fun eq_quant (@{const_name HOL.All}, _) (@{const_name HOL.All}, _) = true
blanchet@56078
   472
  | eq_quant (@{const_name HOL.Ex}, _) (@{const_name HOL.Ex}, _) = true
blanchet@56078
   473
  | eq_quant _ _ = false
blanchet@56078
   474
blanchet@56078
   475
fun opp_quant (@{const_name HOL.All}, _) (@{const_name HOL.Ex}, _) = true
blanchet@56078
   476
  | opp_quant (@{const_name HOL.Ex}, _) (@{const_name HOL.All}, _) = true
blanchet@56078
   477
  | opp_quant _ _ = false
blanchet@56078
   478
blanchet@56078
   479
fun with_quant pred i (Const q1 $ Abs (_, T1, t1), Const q2 $ Abs (_, T2, t2)) =
blanchet@56078
   480
      if pred q1 q2 andalso T1 = T2 then
blanchet@56078
   481
        let val t = Var (("", i), T1)
blanchet@56078
   482
        in SOME (pairself Term.subst_bound ((t, t1), (t, t2))) end
blanchet@56078
   483
      else NONE
blanchet@56078
   484
  | with_quant _ _ _ = NONE
blanchet@56078
   485
blanchet@56078
   486
fun dest_quant_pair i (@{term HOL.Not} $ t1, t2) =
blanchet@56078
   487
      Option.map (apfst HOLogic.mk_not) (with_quant opp_quant i (t1, t2))
blanchet@56078
   488
  | dest_quant_pair i (t1, t2) = with_quant eq_quant i (t1, t2)
blanchet@56078
   489
blanchet@56078
   490
fun dest_quant i t =
blanchet@56078
   491
  (case dest_quant_pair i (HOLogic.dest_eq (HOLogic.dest_Trueprop t)) of
blanchet@56078
   492
    SOME (t1, t2) => HOLogic.mk_Trueprop (HOLogic.mk_eq (t1, t2))
blanchet@56078
   493
  | NONE => raise TERM ("lift_quant", [t]))
blanchet@56078
   494
blanchet@56078
   495
fun match_types ctxt pat obj =
blanchet@56078
   496
  (Vartab.empty, Vartab.empty)
blanchet@56078
   497
  |> Pattern.first_order_match (Proof_Context.theory_of ctxt) (pat, obj)
blanchet@56078
   498
blanchet@56078
   499
fun strip_match ctxt pat i obj =
blanchet@56078
   500
  (case try (match_types ctxt pat) obj of
blanchet@56078
   501
    SOME (tyenv, _) => subst_of tyenv
blanchet@56078
   502
  | NONE => strip_match ctxt pat (i + 1) (dest_quant i obj))
blanchet@56078
   503
wenzelm@56245
   504
fun dest_all i (Const (@{const_name Pure.all}, _) $ (a as Abs (_, T, _))) =
blanchet@56078
   505
      dest_all (i + 1) (Term.betapply (a, Var (("", i), T)))
blanchet@56078
   506
  | dest_all i t = (i, t)
blanchet@56078
   507
blanchet@56078
   508
fun dest_alls t = dest_all (Term.maxidx_of_term t + 1) t
blanchet@56078
   509
blanchet@56078
   510
fun match_rule ctxt env (Z3_Node {bounds=bs', concl=t', ...}) bs t =
blanchet@56078
   511
  let
blanchet@56078
   512
    val t'' = singleton (Variable.polymorphic ctxt) t'
blanchet@56078
   513
    val (i, obj) = dest_alls (subst_types ctxt env bs t)
blanchet@56078
   514
  in
blanchet@56078
   515
    (case try (strip_match ctxt (snd (dest_alls t'')) i) obj of
blanchet@56078
   516
      NONE => NONE
blanchet@56078
   517
    | SOME subst =>
blanchet@56078
   518
        let
blanchet@56078
   519
          val applyT = Same.commit (substTs_same subst)
wenzelm@56245
   520
          val patTs = map snd (Term.strip_qnt_vars @{const_name Pure.all} t'')
blanchet@56078
   521
        in SOME (Symtab.make (bs' ~~ map applyT patTs)) end)
blanchet@56078
   522
  end
blanchet@56078
   523
blanchet@56078
   524
blanchet@56078
   525
blanchet@56078
   526
(* linearizing proofs and resolving types of bound variables *)
blanchet@56078
   527
blanchet@56078
   528
fun has_step (tab, _) = Inttab.defined tab
blanchet@56078
   529
blanchet@56078
   530
fun add_step id rule bounds concl is_fix_step ids (tab, sts) =
blanchet@56078
   531
  let val step = mk_step id rule ids concl bounds is_fix_step
blanchet@56078
   532
  in (id, (Inttab.update (id, ()) tab, step :: sts)) end
blanchet@56078
   533
blanchet@56078
   534
fun is_fix_rule rule prems =
blanchet@56078
   535
  member (op =) [Quant_Intro, Nnf_Pos, Nnf_Neg] rule andalso length prems = 1
blanchet@56078
   536
blanchet@56078
   537
fun lin_proof ctxt env (Z3_Node {id, rule, prems, concl, bounds}) steps =
blanchet@56078
   538
  if has_step steps id then (id, steps)
blanchet@56078
   539
  else
blanchet@56078
   540
    let
blanchet@56078
   541
      val t = subst_types ctxt env bounds concl
blanchet@56078
   542
      val add = add_step id rule bounds t
blanchet@56078
   543
      fun rec_apply e b = fold_map (lin_proof ctxt e) prems #-> add b
blanchet@56078
   544
    in
blanchet@56078
   545
      if is_fix_rule rule prems then
blanchet@56078
   546
        (case match_rule ctxt env (hd prems) bounds t of
blanchet@56078
   547
          NONE => rec_apply env false steps
blanchet@56078
   548
        | SOME env' => rec_apply env' true steps)
blanchet@56078
   549
      else rec_apply env false steps
blanchet@56078
   550
    end
blanchet@56078
   551
blanchet@56078
   552
fun linearize ctxt node =
blanchet@56078
   553
  rev (snd (snd (lin_proof ctxt Symtab.empty node (Inttab.empty, []))))
blanchet@56078
   554
blanchet@56078
   555
blanchet@56078
   556
blanchet@56078
   557
(* overall proof parser *)
blanchet@56078
   558
blanchet@56078
   559
fun parse typs funs lines ctxt =
blanchet@56078
   560
  let val (node, ctxt') = parse_proof typs funs lines ctxt
blanchet@56078
   561
  in (linearize ctxt' node, ctxt') end
blanchet@56078
   562
blanchet@56078
   563
end