| author | haftmann | 
| Sat, 28 Jun 2014 09:16:42 +0200 | |
| changeset 57418 | 6ab1c7cb0b8d | 
| parent 57230 | ec5ff6bb2a92 | 
| child 57747 | 816f96fff418 | 
| permissions | -rw-r--r-- | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 1 | (* Title: HOL/Tools/SMT2/smtlib2_proof.ML | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 2 | Author: Sascha Boehme, TU Muenchen | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 3 | Author: Mathias Fleury, ENS Rennes | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 4 | Author: Jasmin Blanchette, TU Muenchen | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 5 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 6 | SMT-LIB-2-style proofs: parsing and abstract syntax tree. | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 7 | *) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 8 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 9 | signature SMTLIB2_PROOF = | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 10 | sig | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 11 | datatype 'b shared = Tree of SMTLIB2.tree | Term of term | Proof of 'b | None | 
| 57221 | 12 |   type ('a, 'b) context
 | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 13 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 14 | val mk_context: Proof.context -> int -> 'b shared Symtab.table -> typ Symtab.table -> | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 15 |     term Symtab.table -> 'a -> ('a, 'b) context
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 16 |   val empty_context: Proof.context -> typ Symtab.table -> term Symtab.table -> ('a list, 'b) context
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 17 |   val ctxt_of: ('a, 'b) context -> Proof.context
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 18 |   val lookup_binding: ('a, 'b) context -> string -> 'b shared
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 19 |   val update_binding: string * 'b shared -> ('a, 'b) context -> ('a, 'b) context
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 20 |   val with_bindings: (string * 'b shared) list -> (('a, 'b) context -> 'c * ('d, 'b) context) ->
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 21 |     ('a, 'b) context -> 'c * ('d, 'b) context
 | 
| 57222 | 22 |   val next_id: ('a, 'b) context -> int * ('a, 'b) context
 | 
| 23 |   val with_fresh_names: (('a list, 'b) context ->
 | |
| 24 |     term * ((string * (string * typ)) list, 'b) context) -> ('c, 'b) context ->
 | |
| 25 |     (term * string list) * ('c, 'b) context
 | |
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 26 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 27 | (*type and term parsers*) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 28 | type type_parser = SMTLIB2.tree * typ list -> typ option | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 29 | type term_parser = SMTLIB2.tree * term list -> term option | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 30 | val add_type_parser: type_parser -> Context.generic -> Context.generic | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 31 | val add_term_parser: term_parser -> Context.generic -> Context.generic | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 32 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 33 | exception SMTLIB2_PARSE of string * SMTLIB2.tree | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 34 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 35 | val declare_fun: string -> typ -> ((string * typ) list, 'a) context -> | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 36 | ((string * typ) list, 'a) context | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 37 | val dest_binding: SMTLIB2.tree -> string * 'a shared | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 38 |   val type_of: ('a, 'b) context -> SMTLIB2.tree -> typ
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 39 | val term_of: SMTLIB2.tree -> ((string * (string * typ)) list, 'a) context -> | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 40 | term * ((string * (string * typ)) list, 'a) context | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 41 | end; | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 42 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 43 | structure SMTLIB2_Proof: SMTLIB2_PROOF = | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 44 | struct | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 45 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 46 | (* proof parser context *) | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 47 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 48 | datatype 'b shared = Tree of SMTLIB2.tree | Term of term | Proof of 'b | None | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 49 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 50 | type ('a, 'b) context = {
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 51 | ctxt: Proof.context, | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 52 | id: int, | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 53 | syms: 'b shared Symtab.table, | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 54 | typs: typ Symtab.table, | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 55 | funs: term Symtab.table, | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 56 | extra: 'a} | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 57 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 58 | fun mk_context ctxt id syms typs funs extra: ('a, 'b) context =
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 59 |   {ctxt=ctxt, id=id, syms=syms, typs=typs, funs=funs, extra=extra}
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 60 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 61 | fun empty_context ctxt typs funs = mk_context ctxt 1 Symtab.empty typs funs [] | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 62 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 63 | fun ctxt_of ({ctxt, ...}: ('a, 'b) context) = ctxt
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 64 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 65 | fun lookup_binding ({syms, ...}: ('a, 'b) context) =
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 66 | the_default None o Symtab.lookup syms | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 67 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 68 | fun map_syms f ({ctxt, id, syms, typs, funs, extra}: ('a, 'b) context) =
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 69 | mk_context ctxt id (f syms) typs funs extra | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 70 | |
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 71 | fun update_binding b = map_syms (Symtab.update b) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 72 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 73 | fun with_bindings bs f cx = | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 74 | let val bs' = map (lookup_binding cx o fst) bs | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 75 | in | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 76 | cx | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 77 | |> fold update_binding bs | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 78 | |> f | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 79 | ||> fold2 (fn (name, _) => update_binding o pair name) bs bs' | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 80 | end | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 81 | |
| 57221 | 82 | fun next_id ({ctxt, id, syms, typs, funs, extra}: ('a, 'b) context) =
 | 
| 83 | (id, mk_context ctxt (id + 1) syms typs funs extra) | |
| 84 | ||
| 85 | fun with_fresh_names f ({ctxt, id, syms, typs, funs, extra}: ('a, 'b) context) =
 | |
| 86 | let | |
| 87 | fun bind (_, v as (_, T)) t = Logic.all_const T $ Term.absfree v t | |
| 88 | ||
| 89 | val needs_inferT = equal Term.dummyT orf Term.is_TVar | |
| 90 | val needs_infer = Term.exists_type (Term.exists_subtype needs_inferT) | |
| 91 | fun infer_types ctxt = | |
| 92 | singleton (Type_Infer_Context.infer_types ctxt) #> | |
| 93 | singleton (Proof_Context.standard_term_check_finish ctxt) | |
| 94 | fun infer ctxt t = if needs_infer t then infer_types ctxt t else t | |
| 95 | ||
| 57222 | 96 |     val (t, {ctxt=ctxt', extra=names, ...}: ((string * (string * typ)) list, 'b) context) =
 | 
| 57221 | 97 | f (mk_context ctxt id syms typs funs []) | 
| 98 | val t' = infer ctxt' (fold_rev bind names (HOLogic.mk_Trueprop t)) | |
| 99 | in | |
| 100 | ((t', map fst names), mk_context ctxt id syms typs funs extra) | |
| 101 | end | |
| 102 | ||
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 103 | fun lookup_typ ({typs, ...}: ('a, 'b) context) = Symtab.lookup typs
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 104 | fun lookup_fun ({funs, ...}: ('a, 'b) context) = Symtab.lookup funs
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 105 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 106 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 107 | (* core type and term parser *) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 108 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 109 | fun core_type_parser (SMTLIB2.Sym "Bool", []) = SOME @{typ HOL.bool}
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 110 |   | core_type_parser (SMTLIB2.Sym "Int", []) = SOME @{typ Int.int}
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 111 | | core_type_parser _ = NONE | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 112 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 113 | fun mk_unary n t = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 114 | let val T = fastype_of t | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 115 | in Const (n, T --> T) $ t end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 116 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 117 | fun mk_binary' n T U t1 t2 = Const (n, [T, T] ---> U) $ t1 $ t2 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 118 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 119 | fun mk_binary n t1 t2 = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 120 | let val T = fastype_of t1 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 121 | in mk_binary' n T T t1 t2 end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 122 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 123 | fun mk_rassoc f t ts = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 124 | let val us = rev (t :: ts) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 125 | in fold f (tl us) (hd us) end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 126 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 127 | fun mk_lassoc f t ts = fold (fn u1 => fn u2 => f u2 u1) ts t | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 128 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 129 | fun mk_lassoc' n = mk_lassoc (mk_binary n) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 130 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 131 | fun mk_binary_pred n S t1 t2 = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 132 | let | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 133 | val T1 = fastype_of t1 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 134 | val T2 = fastype_of t2 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 135 | val T = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 136 | if T1 <> Term.dummyT then T1 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 137 | else if T2 <> Term.dummyT then T2 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 138 |       else TVar (("?a", serial ()), S)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 139 |   in mk_binary' n T @{typ HOL.bool} t1 t2 end
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 140 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 141 | fun mk_less t1 t2 = mk_binary_pred @{const_name ord_class.less} @{sort linorder} t1 t2
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 142 | fun mk_less_eq t1 t2 = mk_binary_pred @{const_name ord_class.less_eq} @{sort linorder} t1 t2
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 143 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 144 | fun core_term_parser (SMTLIB2.Sym "true", _) = SOME @{const HOL.True}
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 145 |   | core_term_parser (SMTLIB2.Sym "false", _) = SOME @{const HOL.False}
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 146 | | core_term_parser (SMTLIB2.Sym "not", [t]) = SOME (HOLogic.mk_not t) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 147 | | core_term_parser (SMTLIB2.Sym "and", t :: ts) = SOME (mk_rassoc (curry HOLogic.mk_conj) t ts) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 148 | | core_term_parser (SMTLIB2.Sym "or", t :: ts) = SOME (mk_rassoc (curry HOLogic.mk_disj) t ts) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 149 | | core_term_parser (SMTLIB2.Sym "=>", [t1, t2]) = SOME (HOLogic.mk_imp (t1, t2)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 150 | | core_term_parser (SMTLIB2.Sym "implies", [t1, t2]) = SOME (HOLogic.mk_imp (t1, t2)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 151 | | core_term_parser (SMTLIB2.Sym "=", [t1, t2]) = SOME (HOLogic.mk_eq (t1, t2)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 152 | | core_term_parser (SMTLIB2.Sym "~", [t1, t2]) = SOME (HOLogic.mk_eq (t1, t2)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 153 | | core_term_parser (SMTLIB2.Sym "ite", [t1, t2, t3]) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 154 | let | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 155 | val T = fastype_of t2 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 156 |         val c = Const (@{const_name HOL.If}, [@{typ HOL.bool}, T, T] ---> T)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 157 | in SOME (c $ t1 $ t2 $ t3) end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 158 |   | core_term_parser (SMTLIB2.Num i, []) = SOME (HOLogic.mk_number @{typ Int.int} i)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 159 |   | core_term_parser (SMTLIB2.Sym "-", [t]) = SOME (mk_unary @{const_name uminus_class.uminus} t)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 160 |   | core_term_parser (SMTLIB2.Sym "~", [t]) = SOME (mk_unary @{const_name uminus_class.uminus} t)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 161 | | core_term_parser (SMTLIB2.Sym "+", t :: ts) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 162 |       SOME (mk_lassoc' @{const_name plus_class.plus} t ts)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 163 | | core_term_parser (SMTLIB2.Sym "-", t :: ts) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 164 |       SOME (mk_lassoc' @{const_name minus_class.minus} t ts)
 | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 165 | | core_term_parser (SMTLIB2.Sym "*", t :: ts) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 166 |       SOME (mk_lassoc' @{const_name times_class.times} t ts)
 | 
| 57230 | 167 |   | core_term_parser (SMTLIB2.Sym "div", [t1, t2]) = SOME (mk_binary @{const_name z3div} t1 t2)
 | 
| 168 |   | core_term_parser (SMTLIB2.Sym "mod", [t1, t2]) = SOME (mk_binary @{const_name z3mod} t1 t2)
 | |
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 169 | | core_term_parser (SMTLIB2.Sym "<", [t1, t2]) = SOME (mk_less t1 t2) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 170 | | core_term_parser (SMTLIB2.Sym ">", [t1, t2]) = SOME (mk_less t2 t1) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 171 | | core_term_parser (SMTLIB2.Sym "<=", [t1, t2]) = SOME (mk_less_eq t1 t2) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 172 | | core_term_parser (SMTLIB2.Sym ">=", [t1, t2]) = SOME (mk_less_eq t2 t1) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 173 | | core_term_parser _ = NONE | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 174 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 175 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 176 | (* custom type and term parsers *) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 177 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 178 | type type_parser = SMTLIB2.tree * typ list -> typ option | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 179 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 180 | type term_parser = SMTLIB2.tree * term list -> term option | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 181 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 182 | fun id_ord ((id1, _), (id2, _)) = int_ord (id1, id2) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 183 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 184 | structure Parsers = Generic_Data | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 185 | ( | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 186 | type T = (int * type_parser) list * (int * term_parser) list | 
| 56122 | 187 | val empty : T = ([(serial (), core_type_parser)], [(serial (), core_term_parser)]) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 188 | val extend = I | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 189 | fun merge ((tys1, ts1), (tys2, ts2)) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 190 | (Ord_List.merge id_ord (tys1, tys2), Ord_List.merge id_ord (ts1, ts2)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 191 | ) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 192 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 193 | fun add_type_parser type_parser = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 194 | Parsers.map (apfst (Ord_List.insert id_ord (serial (), type_parser))) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 195 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 196 | fun add_term_parser term_parser = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 197 | Parsers.map (apsnd (Ord_List.insert id_ord (serial (), term_parser))) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 198 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 199 | fun get_type_parsers ctxt = map snd (fst (Parsers.get (Context.Proof ctxt))) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 200 | fun get_term_parsers ctxt = map snd (snd (Parsers.get (Context.Proof ctxt))) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 201 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 202 | fun apply_parsers parsers x = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 203 | let | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 204 | fun apply [] = NONE | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 205 | | apply (parser :: parsers) = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 206 | (case parser x of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 207 | SOME y => SOME y | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 208 | | NONE => apply parsers) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 209 | in apply parsers end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 210 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 211 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 212 | (* type and term parsing *) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 213 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 214 | exception SMTLIB2_PARSE of string * SMTLIB2.tree | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 215 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 216 | val desymbolize = Name.desymbolize (SOME false) o perhaps (try (unprefix "?")) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 217 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 218 | fun fresh_fun add name n T ({ctxt, id, syms, typs, funs, extra}: ('a, 'b) context) =
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 219 | let | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 220 | val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 221 | val t = Free (n', T) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 222 | val funs' = Symtab.update (name, t) funs | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 223 | in (t, mk_context ctxt' id syms typs funs' (add (n', T) extra)) end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 224 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 225 | fun declare_fun name = snd oo fresh_fun cons name (desymbolize name) | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 226 | fun declare_free name = fresh_fun (cons o pair name) name (desymbolize name) | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 227 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 228 | fun parse_type cx ty Ts = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 229 | (case apply_parsers (get_type_parsers (ctxt_of cx)) (ty, Ts) of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 230 | SOME T => T | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 231 | | NONE => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 232 | (case ty of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 233 | SMTLIB2.Sym name => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 234 | (case lookup_typ cx name of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 235 | SOME T => T | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 236 |           | NONE => raise SMTLIB2_PARSE ("unknown SMT type", ty))
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 237 |       | _ => raise SMTLIB2_PARSE ("bad SMT type format", ty)))
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 238 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 239 | fun parse_term t ts cx = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 240 | (case apply_parsers (get_term_parsers (ctxt_of cx)) (t, ts) of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 241 | SOME u => (u, cx) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 242 | | NONE => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 243 | (case t of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 244 | SMTLIB2.Sym name => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 245 | (case lookup_fun cx name of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 246 | SOME u => (Term.list_comb (u, ts), cx) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 247 | | NONE => | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 248 | if null ts then declare_free name Term.dummyT cx | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 249 |               else raise SMTLIB2_PARSE ("bad SMT term", t))
 | 
| 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 250 |       | _ => raise SMTLIB2_PARSE ("bad SMT term format", t)))
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 251 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 252 | fun type_of cx ty = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 253 | (case try (parse_type cx ty) [] of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 254 | SOME T => T | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 255 | | NONE => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 256 | (case ty of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 257 | SMTLIB2.S (ty' :: tys) => parse_type cx ty' (map (type_of cx) tys) | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 258 |       | _ => raise SMTLIB2_PARSE ("bad SMT type", ty)))
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 259 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 260 | fun dest_var cx (SMTLIB2.S [SMTLIB2.Sym name, ty]) = (name, (desymbolize name, type_of cx ty)) | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 261 |   | dest_var _ v = raise SMTLIB2_PARSE ("bad SMT quantifier variable format", v)
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 262 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 263 | fun dest_body (SMTLIB2.S (SMTLIB2.Sym "!" :: body :: _)) = dest_body body | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 264 | | dest_body body = body | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 265 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 266 | fun dest_binding (SMTLIB2.S [SMTLIB2.Sym name, t]) = (name, Tree t) | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 267 |   | dest_binding b = raise SMTLIB2_PARSE ("bad SMT let binding format", b)
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 268 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 269 | fun term_of t cx = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 270 | (case t of | 
| 57221 | 271 | SMTLIB2.S [SMTLIB2.Sym "forall", SMTLIB2.S vars, body] => quant HOLogic.mk_all vars body cx | 
| 272 | | SMTLIB2.S [SMTLIB2.Sym "exists", SMTLIB2.S vars, body] => quant HOLogic.mk_exists vars body cx | |
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 273 | | SMTLIB2.S [SMTLIB2.Sym "let", SMTLIB2.S bindings, body] => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 274 | with_bindings (map dest_binding bindings) (term_of body) cx | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 275 | | SMTLIB2.S (SMTLIB2.Sym "!" :: t :: _) => term_of t cx | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 276 | | SMTLIB2.S (f :: args) => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 277 | cx | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 278 | |> fold_map term_of args | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 279 | |-> parse_term f | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 280 | | SMTLIB2.Sym name => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 281 | (case lookup_binding cx name of | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 282 | Tree u => | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 283 | cx | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 284 | |> term_of u | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 285 | |-> (fn u' => pair u' o update_binding (name, Term u')) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 286 | | Term u => (u, cx) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 287 | | None => parse_term t [] cx | 
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 288 |       | _ => raise SMTLIB2_PARSE ("bad SMT term format", t))
 | 
| 56078 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 289 | | _ => parse_term t [] cx) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 290 | |
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 291 | and quant q vars body cx = | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 292 | let val vs = map (dest_var cx) vars | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 293 | in | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 294 | cx | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 295 | |> with_bindings (map (apsnd (Term o Free)) vs) (term_of (dest_body body)) | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 296 | |>> fold_rev (fn (_, (n, T)) => fn t => q (n, T, t)) vs | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 297 | end | 
| 
624faeda77b5
moved 'SMT2' (SMT-LIB-2-based SMT module) into Isabelle
 blanchet parents: diff
changeset | 298 | |
| 57219 
34018603e0d0
factor out SMT-LIB 2 type/term parsing from Z3-specific code
 blanchet parents: 
56811diff
changeset | 299 | end; |