src/HOL/Tools/SMT/z3_proof_parser.ML
author boehmes
Wed, 15 Dec 2010 10:12:48 +0100
changeset 41131 fc9d503c3d67
parent 41130 130771a48c70
child 41193 dc33b8ea4526
permissions -rw-r--r--
fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     1
(*  Title:      HOL/Tools/SMT/z3_proof_parser.ML
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     3
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     4
Parser for Z3 proofs.
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     5
*)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     6
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     7
signature Z3_PROOF_PARSER =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
     8
sig
41123
boehmes
parents: 40663
diff changeset
     9
  (*proof rules*)
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    10
  datatype rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    11
    Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    12
    Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    13
    Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    14
    Dest_Eq_Res | Quant_Inst | Hypothesis | Lemma | Unit_Resolution |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    15
    Iff_True | Iff_False | Commutativity | Def_Axiom | Intro_Def | Apply_Def |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    16
    Iff_Oeq | Nnf_Pos | Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    17
    Modus_Ponens_Oeq | Th_Lemma of string list
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    18
  val string_of_rule: rule -> string
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    19
41123
boehmes
parents: 40663
diff changeset
    20
  (*proof parser*)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    21
  datatype proof_step = Proof_Step of {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    22
    rule: rule,
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    23
    args: cterm list,
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    24
    prems: int list,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    25
    prop: cterm }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    26
  val parse: Proof.context -> typ Symtab.table -> term Symtab.table ->
41131
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
    27
    string list ->
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
    28
    (int * cterm) list * (int * proof_step) list * string list * Proof.context
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    29
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    30
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    31
structure Z3_Proof_Parser: Z3_PROOF_PARSER =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    32
struct
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    33
40662
798aad2229c0 added prove reconstruction for injective functions;
boehmes
parents: 40627
diff changeset
    34
structure U = SMT_Utils
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    35
structure I = Z3_Interface
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    36
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    37
41123
boehmes
parents: 40663
diff changeset
    38
(* proof rules *)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    39
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    40
datatype rule = True_Axiom | Asserted | Goal | Modus_Ponens | Reflexivity |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    41
  Symmetry | Transitivity | Transitivity_Star | Monotonicity | Quant_Intro |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    42
  Distributivity | And_Elim | Not_Or_Elim | Rewrite | Rewrite_Star |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    43
  Pull_Quant | Pull_Quant_Star | Push_Quant | Elim_Unused_Vars | Dest_Eq_Res |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    44
  Quant_Inst | Hypothesis | Lemma | Unit_Resolution | Iff_True | Iff_False |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    45
  Commutativity | Def_Axiom | Intro_Def | Apply_Def | Iff_Oeq | Nnf_Pos |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    46
  Nnf_Neg | Nnf_Star | Cnf_Star | Skolemize | Modus_Ponens_Oeq |
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    47
  Th_Lemma of string list
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    48
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    49
val rule_names = Symtab.make [
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    50
  ("true-axiom", True_Axiom),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    51
  ("asserted", Asserted),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    52
  ("goal", Goal),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    53
  ("mp", Modus_Ponens),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    54
  ("refl", Reflexivity),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    55
  ("symm", Symmetry),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    56
  ("trans", Transitivity),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    57
  ("trans*", Transitivity_Star),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    58
  ("monotonicity", Monotonicity),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    59
  ("quant-intro", Quant_Intro),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    60
  ("distributivity", Distributivity),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    61
  ("and-elim", And_Elim),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    62
  ("not-or-elim", Not_Or_Elim),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    63
  ("rewrite", Rewrite),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    64
  ("rewrite*", Rewrite_Star),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    65
  ("pull-quant", Pull_Quant),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    66
  ("pull-quant*", Pull_Quant_Star),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    67
  ("push-quant", Push_Quant),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    68
  ("elim-unused", Elim_Unused_Vars),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    69
  ("der", Dest_Eq_Res),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    70
  ("quant-inst", Quant_Inst),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    71
  ("hypothesis", Hypothesis),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    72
  ("lemma", Lemma),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    73
  ("unit-resolution", Unit_Resolution),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    74
  ("iff-true", Iff_True),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    75
  ("iff-false", Iff_False),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    76
  ("commutativity", Commutativity),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    77
  ("def-axiom", Def_Axiom),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    78
  ("intro-def", Intro_Def),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    79
  ("apply-def", Apply_Def),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    80
  ("iff~", Iff_Oeq),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    81
  ("nnf-pos", Nnf_Pos),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    82
  ("nnf-neg", Nnf_Neg),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    83
  ("nnf*", Nnf_Star),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    84
  ("cnf*", Cnf_Star),
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    85
  ("sk", Skolemize),
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    86
  ("mp~", Modus_Ponens_Oeq),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    87
  ("th-lemma", Th_Lemma [])]
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    88
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    89
fun string_of_rule (Th_Lemma args) = space_implode " " ("th-lemma" :: args)
40516
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
    90
  | string_of_rule r =
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
    91
      let fun eq_rule (s, r') = if r = r' then SOME s else NONE 
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
    92
      in the (Symtab.get_first eq_rule rule_names) end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    93
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    94
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    95
41123
boehmes
parents: 40663
diff changeset
    96
(* certified terms and variables *)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
    97
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
    98
val (var_prefix, decl_prefix) = ("v", "sk")
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
    99
(*
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   100
  "decl_prefix" is for skolem constants (represented by free variables),
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   101
  "var_prefix" is for pseudo-schematic variables (schematic with respect
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   102
  to the Z3 proof, but represented by free variables).
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   103
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   104
  Both prefixes must be distinct to avoid name interferences.
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   105
  More precisely, the naming of pseudo-schematic variables must be
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   106
  context-independent modulo the current proof context to be able to
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   107
  use fast inference kernel rules during proof reconstruction.
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   108
*)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   109
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   110
val maxidx_of = #maxidx o Thm.rep_cterm
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   111
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   112
fun mk_inst ctxt vars =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   113
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   114
    val max = fold (Integer.max o fst) vars 0
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   115
    val ns = fst (Variable.variant_fixes (replicate (max + 1) var_prefix) ctxt)
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40662
diff changeset
   116
    fun mk (i, v) = (v, U.certify ctxt (Free (nth ns i, #T (Thm.rep_cterm v))))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   117
  in map mk vars end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   118
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   119
fun close ctxt (ct, vars) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   120
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   121
    val inst = mk_inst ctxt vars
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   122
    val names = fold (Term.add_free_names o Thm.term_of o snd) inst []
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   123
  in (Thm.instantiate_cterm ([], inst) ct, names) end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   124
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   125
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   126
fun mk_bound ctxt (i, T) =
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   127
  let val ct = U.certify ctxt (Var ((Name.uu, 0), T))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   128
  in (ct, [(i, ct)]) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   129
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   130
local
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   131
  fun mk_quant1 ctxt q T (ct, vars) =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   132
    let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   133
      val cv =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   134
        (case AList.lookup (op =) vars 0 of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   135
          SOME cv => cv
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   136
        | _ => U.certify ctxt (Var ((Name.uu, maxidx_of ct + 1), T)))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   137
      fun dec (i, v) = if i = 0 then NONE else SOME (i-1, v)
40662
798aad2229c0 added prove reconstruction for injective functions;
boehmes
parents: 40627
diff changeset
   138
    in (Thm.capply (U.instT' cv q) (Thm.cabs cv ct), map_filter dec vars) end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   139
40662
798aad2229c0 added prove reconstruction for injective functions;
boehmes
parents: 40627
diff changeset
   140
  fun quant name = U.mk_const_pat @{theory} name (U.destT1 o U.destT1)
798aad2229c0 added prove reconstruction for injective functions;
boehmes
parents: 40627
diff changeset
   141
  val forall = quant @{const_name All}
798aad2229c0 added prove reconstruction for injective functions;
boehmes
parents: 40627
diff changeset
   142
  val exists = quant @{const_name Ex}
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   143
in
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   144
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   145
fun mk_quant is_forall ctxt =
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   146
  fold_rev (mk_quant1 ctxt (if is_forall then forall else exists))
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   147
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   148
end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   149
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   150
local
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   151
  fun equal_var cv (_, cu) = (cv aconvc cu)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   152
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   153
  fun prep (ct, vars) (maxidx, all_vars) =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   154
    let
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   155
      val maxidx' = maxidx_of ct + maxidx + 1
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   156
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   157
      fun part (v as (i, cv)) =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   158
        (case AList.lookup (op =) all_vars i of
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   159
          SOME cu => apfst (if cu aconvc cv then I else cons (cv, cu))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   160
        | NONE =>
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   161
            if not (exists (equal_var cv) all_vars) then apsnd (cons v)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   162
            else
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   163
              let val cv' = Thm.incr_indexes_cterm maxidx' cv
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   164
              in apfst (cons (cv, cv')) #> apsnd (cons (i, cv')) end)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   165
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   166
      val (inst, vars') =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   167
        if null vars then ([], vars)
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   168
        else fold part vars ([], [])
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   169
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   170
    in (Thm.instantiate_cterm ([], inst) ct, (maxidx', vars' @ all_vars)) end
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   171
in
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   172
fun mk_fun f ts =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   173
  let val (cts, (_, vars)) = fold_map prep ts (~1, [])
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   174
  in f cts |> Option.map (rpair vars) end
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   175
end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   176
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   177
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   178
41123
boehmes
parents: 40663
diff changeset
   179
(* proof parser *)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   180
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   181
datatype proof_step = Proof_Step of {
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   182
  rule: rule,
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   183
  args: cterm list,
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   184
  prems: int list,
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   185
  prop: cterm }
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   186
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   187
41123
boehmes
parents: 40663
diff changeset
   188
(** parser context **)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   189
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   190
val not_false = Thm.cterm_of @{theory} (@{const Not} $ @{const False})
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   191
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   192
fun make_context ctxt typs terms =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   193
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   194
    val ctxt' = 
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   195
      ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   196
      |> Symtab.fold (Variable.declare_typ o snd) typs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   197
      |> Symtab.fold (Variable.declare_term o snd) terms
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   198
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   199
    fun cert @{const True} = not_false
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40662
diff changeset
   200
      | cert t = U.certify ctxt' t
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   201
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   202
  in (typs, Symtab.map (K cert) terms, Inttab.empty, [], [], ctxt') end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   203
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   204
fun fresh_name n (typs, terms, exprs, steps, vars, ctxt) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   205
  let val (n', ctxt') = yield_singleton Variable.variant_fixes n ctxt
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   206
  in (n', (typs, terms, exprs, steps, vars, ctxt')) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   207
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   208
fun context_of (_, _, _, _, _, ctxt) = ctxt
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   209
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   210
fun add_decl (n, T) (cx as (_, terms, _, _, _, _)) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   211
  (case Symtab.lookup terms n of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   212
    SOME _ => cx
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   213
  | NONE => cx |> fresh_name (decl_prefix ^ n)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   214
      |> (fn (m, (typs, terms, exprs, steps, vars, ctxt)) =>
40663
e080c9e68752 share and use more utility functions;
boehmes
parents: 40662
diff changeset
   215
           let val upd = Symtab.update (n, U.certify ctxt (Free (m, T)))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   216
           in (typs, upd terms, exprs, steps, vars, ctxt) end))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   217
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   218
fun mk_typ (typs, _, _, _, _, ctxt) (s as I.Sym (n, _)) = 
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   219
  (case I.mk_builtin_typ ctxt s of
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   220
    SOME T => SOME T
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   221
  | NONE => Symtab.lookup typs n)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   222
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   223
fun mk_num (_, _, _, _, _, ctxt) (i, T) =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   224
  mk_fun (K (I.mk_builtin_num ctxt i T)) []
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   225
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   226
fun mk_app (_, terms, _, _, _, ctxt) (s as I.Sym (n, _), es) =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   227
  mk_fun (fn cts =>
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   228
    (case I.mk_builtin_fun ctxt s cts of
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   229
      SOME ct => SOME ct
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   230
    | NONE =>
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   231
        Symtab.lookup terms n |> Option.map (Drule.list_comb o rpair cts))) es
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   232
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   233
fun add_expr k t (typs, terms, exprs, steps, vars, ctxt) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   234
  (typs, terms, Inttab.update (k, t) exprs, steps, vars, ctxt)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   235
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   236
fun lookup_expr (_, _, exprs, _, _, _) = Inttab.lookup exprs
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   237
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   238
fun add_proof_step k ((r, args), prop) cx =
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   239
  let
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   240
    val (typs, terms, exprs, steps, vars, ctxt) = cx
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   241
    val (ct, vs) = close ctxt prop
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   242
    fun part (SOME e, _) (cts, ps) = (close ctxt e :: cts, ps)
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   243
      | part (NONE, i) (cts, ps) = (cts, i :: ps)
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   244
    val (args', prems) = fold (part o `(lookup_expr cx)) args ([], [])
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   245
    val (cts, vss) = split_list args'
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   246
    val step =
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   247
      Proof_Step {rule=r, args=rev cts, prems=rev prems, prop=U.mk_cprop ct}
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   248
    val vars' = fold (union (op =)) (vs :: vss) vars
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   249
  in (typs, terms, exprs, (k, step) :: steps, vars', ctxt) end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   250
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   251
fun finish (_, _, _, steps, vars, ctxt) =
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   252
  let
41131
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   253
    fun coll (p as (k, Proof_Step {prems, rule, prop, ...})) (ars, ps, ids) =
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   254
      (case rule of
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   255
        Asserted => ((k, prop) :: ars, ps, ids)
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   256
      | Goal => ((k, prop) :: ars, ps, ids)
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   257
      | _ =>
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   258
          if Inttab.defined ids k then
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   259
            (ars, p :: ps, fold (Inttab.update o rpair ()) prems ids)
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   260
          else (ars, ps, ids))
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   261
41131
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   262
    val (ars, steps', _) = fold coll steps ([], [], Inttab.make [(~1, ())])
fc9d503c3d67 fixed proof reconstruction for lambda-lifted problems (which broke when the lambda-lifting code was changed to operate on terms instead of on theorems)
boehmes
parents: 41130
diff changeset
   263
  in (ars, steps', vars, ctxt) end
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   264
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   265
41123
boehmes
parents: 40663
diff changeset
   266
(** core parser **)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   267
40424
7550b2cba1cb better modularization: moved SMT configuration options and diagnostics as well as SMT failure and exception into separate structures (both of which are loaded first and consequently are available to other SMT structures)
boehmes
parents: 40162
diff changeset
   268
fun parse_exn line_no msg = raise SMT_Failure.SMT (SMT_Failure.Other_Failure
40162
7f58a9a843c2 joined setup of SMT solvers in one place; turned Z3-specific options into SMT options (renamed configuration options from z3_* to smt_*); more detailed SMT exception; improved SMT filter interface
boehmes
parents: 39020
diff changeset
   269
  ("Z3 proof parser (line " ^ string_of_int line_no ^ "): " ^ msg))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   270
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   271
fun scan_exn msg ((line_no, _), _) = parse_exn line_no msg
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   272
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   273
fun with_info f cx =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   274
  (case f ((NONE, 1), cx) of
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   275
    ((SOME _, _), cx') => cx'
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   276
  | ((_, line_no), _) => parse_exn line_no "bad proof")
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   277
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   278
fun parse_line _ _ (st as ((SOME _, _), _)) = st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   279
  | parse_line scan line ((_, line_no), cx) =
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40579
diff changeset
   280
      let val st = ((line_no, cx), raw_explode line)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   281
      in
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   282
        (case Scan.catch (Scan.finite' Symbol.stopper (Scan.option scan)) st of
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   283
          (SOME r, ((_, cx'), _)) => ((r, line_no+1), cx')
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   284
        | (NONE, _) => parse_exn line_no ("bad proof line: " ^ quote line))
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   285
      end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   286
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   287
fun with_context f x ((line_no, cx), st) =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   288
  let val (y, cx') = f x cx
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   289
  in (y, ((line_no, cx'), st)) end
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   290
  
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   291
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   292
fun lookup_context f x (st as ((_, cx), _)) = (f cx x, st)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   293
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   294
41123
boehmes
parents: 40663
diff changeset
   295
(** parser combinators and parsers for basic entities **)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   296
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   297
fun $$ s = Scan.lift (Scan.$$ s)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   298
fun this s = Scan.lift (Scan.this_string s)
40516
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   299
val is_blank = Symbol.is_ascii_blank
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   300
fun blank st = Scan.lift (Scan.many1 is_blank) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   301
fun sep scan = blank |-- scan
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   302
fun seps scan = Scan.repeat (sep scan)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   303
fun seps1 scan = Scan.repeat1 (sep scan)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   304
fun seps_by scan_sep scan = scan ::: Scan.repeat (scan_sep |-- scan)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   305
40516
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   306
val lpar = "(" and rpar = ")"
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   307
val lbra = "[" and rbra = "]"
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   308
fun par scan = $$ lpar |-- scan --| $$ rpar
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   309
fun bra scan = $$ lbra |-- scan --| $$ rbra
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   310
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   311
val digit = (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   312
  "0" => SOME 0 | "1" => SOME 1 | "2" => SOME 2 | "3" => SOME 3 |
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   313
  "4" => SOME 4 | "5" => SOME 5 | "6" => SOME 6 | "7" => SOME 7 |
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   314
  "8" => SOME 8 | "9" => SOME 9 | _ => NONE)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   315
36940
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   316
fun digits st = (Scan.lift (Scan.many1 Symbol.is_ascii_digit) >> implode) st
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   317
36940
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   318
fun nat_num st = (Scan.lift (Scan.repeat1 (Scan.some digit)) >> (fn ds =>
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   319
  fold (fn d => fn i => i * 10 + d) ds 0)) st
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   320
36940
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   321
fun int_num st = (Scan.optional ($$ "-" >> K (fn i => ~i)) I :|--
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   322
  (fn sign => nat_num >> sign)) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   323
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   324
val is_char = Symbol.is_ascii_letter orf Symbol.is_ascii_digit orf
40627
becf5d5187cc renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents: 40579
diff changeset
   325
  member (op =) (raw_explode "_+*-/%~=<>$&|?!.@^#")
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   326
36940
b4417ddad979 make SML/NJ happy
blanchet
parents: 36899
diff changeset
   327
fun name st = (Scan.lift (Scan.many1 is_char) >> implode) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   328
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   329
fun sym st =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   330
  (name -- Scan.optional (bra (seps_by ($$ ":") sym)) [] >> I.Sym) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   331
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   332
fun id st = ($$ "#" |-- nat_num) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   333
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   334
41123
boehmes
parents: 40663
diff changeset
   335
(** parsers for various parts of Z3 proofs **)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   336
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   337
fun sort st = Scan.first [
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   338
  this "array" |-- bra (sort --| $$ ":" -- sort) >> (op -->),
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   339
  par (this "->" |-- seps1 sort) >> ((op --->) o split_last),
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   340
  sym :|-- (fn s as I.Sym (n, _) => lookup_context mk_typ s :|-- (fn
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   341
    SOME T => Scan.succeed T
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   342
  | NONE => scan_exn ("unknown sort: " ^ quote n)))] st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   343
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   344
fun bound st = (par (this ":var" |-- sep nat_num -- sep sort) :|--
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   345
  lookup_context (mk_bound o context_of)) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   346
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   347
fun numb (n as (i, _)) = lookup_context mk_num n :|-- (fn
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   348
    SOME n' => Scan.succeed n'
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   349
  | NONE => scan_exn ("unknown number: " ^ quote (string_of_int i)))
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   350
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   351
fun appl (app as (I.Sym (n, _), _)) = lookup_context mk_app app :|-- (fn 
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   352
    SOME app' => Scan.succeed app'
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   353
  | NONE => scan_exn ("unknown function symbol: " ^ quote n))
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   354
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   355
fun bv_size st = (digits >> (fn sz => I.Sym ("bv", [I.Sym (sz, [])]))) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   356
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   357
fun bv_number_sort st = (bv_size :|-- lookup_context mk_typ :|-- (fn
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   358
    SOME cT => Scan.succeed cT
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   359
  | NONE => scan_exn ("unknown sort: " ^ quote "bv"))) st
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   360
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   361
fun bv_number st =
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   362
  (this "bv" |-- bra (nat_num --| $$ ":" -- bv_number_sort) :|-- numb) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   363
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   364
fun frac_number st = (
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   365
  int_num --| $$ "/" -- int_num --| this "::" -- sort :|-- (fn ((i, j), T) =>
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   366
    numb (i, T) -- numb (j, T) :|-- (fn (n, m) =>
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   367
      appl (I.Sym ("/", []), [n, m])))) st
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   368
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   369
fun plain_number st = (int_num --| this "::" -- sort :|-- numb) st
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   370
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   371
fun number st = Scan.first [bv_number, frac_number, plain_number] st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   372
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   373
fun constant st = ((sym >> rpair []) :|-- appl) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   374
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   375
fun expr_id st = (id :|-- (fn i => lookup_context lookup_expr i :|-- (fn
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   376
    SOME e => Scan.succeed e
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   377
  | NONE => scan_exn ("unknown term id: " ^ quote (string_of_int i))))) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   378
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   379
fun arg st = Scan.first [expr_id, number, constant] st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   380
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   381
fun application st = par ((sym -- Scan.repeat1 (sep arg)) :|-- appl) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   382
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   383
fun variables st = par (this "vars" |-- seps1 (par (name |-- sep sort))) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   384
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   385
fun pats st = seps (par ((this ":pat" || this ":nopat") |-- seps1 id)) st
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   386
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   387
val ctrue = Thm.cterm_of @{theory} @{const True}
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   388
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   389
fun pattern st = par (this "pattern" |-- Scan.repeat1 (sep arg) >>
40579
98ebd2300823 use the const antiquotation for constants (this checks that the constant is declared, whereas the more general term antiquotation treats undeclared names as free variable)
boehmes
parents: 40516
diff changeset
   390
  (the o mk_fun (K (SOME ctrue)))) st
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   391
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   392
fun quant_kind st = st |> (
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   393
  this "forall" >> K (mk_quant true o context_of) ||
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   394
  this "exists" >> K (mk_quant false o context_of))
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   395
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   396
fun quantifier st =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   397
  (par (quant_kind -- sep variables --| pats -- sep arg) :|--
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   398
     lookup_context (fn cx => fn ((mk_q, Ts), body) => mk_q cx Ts body)) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   399
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   400
fun expr k =
36899
bcd6fce5bf06 layered SMT setup, adapted SMT clients, added further tests, made Z3 proof abstraction configurable
boehmes
parents: 36898
diff changeset
   401
  Scan.first [bound, quantifier, pattern, application, number, constant] :|--
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   402
  with_context (pair NONE oo add_expr k)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   403
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   404
val rule_arg = id
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   405
  (* if this is modified, then 'th_lemma_arg' needs reviewing *)
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   406
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   407
val th_lemma_arg = Scan.unless (sep rule_arg >> K "" || $$ rbra) (sep name)
40516
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   408
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   409
fun rule_name st = ((name >> `(Symtab.lookup rule_names)) :|-- (fn 
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   410
    (SOME (Th_Lemma _), _) => Scan.repeat th_lemma_arg >> Th_Lemma
40516
516a367eb38c preliminary support for newer versions of Z3
boehmes
parents: 40424
diff changeset
   411
  | (SOME r, _) => Scan.succeed r
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   412
  | (NONE, n) => scan_exn ("unknown proof rule: " ^ quote n))) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   413
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   414
fun rule f k =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   415
  bra (rule_name -- seps id) --| $$ ":" -- sep arg #->
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   416
  with_context (pair (f k) oo add_proof_step k)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   417
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   418
fun decl st = (this "decl" |-- sep name --| sep (this "::") -- sep sort :|--
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   419
  with_context (pair NONE oo add_decl)) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   420
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   421
fun def st = (id --| sep (this ":=")) st
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   422
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   423
fun node st = st |> (
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   424
  decl ||
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   425
  def :|-- (fn k => sep (expr k) || sep (rule (K NONE) k)) ||
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   426
  rule SOME ~1)
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   427
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   428
41123
boehmes
parents: 40663
diff changeset
   429
(** overall parser **)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   430
41123
boehmes
parents: 40663
diff changeset
   431
(*
boehmes
parents: 40663
diff changeset
   432
  Currently, terms are parsed bottom-up (i.e., along with parsing the proof
boehmes
parents: 40663
diff changeset
   433
  text line by line), but proofs are reconstructed top-down (i.e. by an
boehmes
parents: 40663
diff changeset
   434
  in-order top-down traversal of the proof tree/graph).  The latter approach
boehmes
parents: 40663
diff changeset
   435
  was taken because some proof texts comprise irrelevant proof steps which
boehmes
parents: 40663
diff changeset
   436
  will thus not be reconstructed.  This approach might also be beneficial
boehmes
parents: 40663
diff changeset
   437
  for constructing terms, but it would also increase the complexity of the
boehmes
parents: 40663
diff changeset
   438
  (otherwise rather modular) code.
boehmes
parents: 40663
diff changeset
   439
*)
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   440
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   441
fun parse ctxt typs terms proof_text =
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   442
  make_context ctxt typs terms
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   443
  |> with_info (fold (parse_line node) proof_text)
41130
130771a48c70 adapted the Z3 proof parser to recent changes in Z3's proof format;
boehmes
parents: 41123
diff changeset
   444
  |> finish
36898
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   445
8e55aa1306c5 integrated SMT into the HOL image
boehmes
parents:
diff changeset
   446
end