src/HOL/SMT_Examples/boogie.ML
author haftmann
Tue Oct 13 09:21:15 2015 +0200 (2015-10-13)
changeset 61424 c3658c18b7bc
parent 59936 b8ffc3dc9e24
child 67399 eab6ce8368fa
permissions -rw-r--r--
prod_case as canonical name for product type eliminator
boehmes@52722
     1
(*  Title:      HOL/SMT_Examples/boogie.ML
boehmes@52722
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@52722
     3
wenzelm@52734
     4
Proving Boogie-generated verification conditions.
boehmes@52722
     5
*)
boehmes@52722
     6
boehmes@52722
     7
signature BOOGIE =
boehmes@52722
     8
sig
wenzelm@55788
     9
  val boogie_prove: theory -> string list -> unit
blanchet@57232
    10
end;
boehmes@52722
    11
boehmes@52722
    12
structure Boogie: BOOGIE =
boehmes@52722
    13
struct
boehmes@52722
    14
boehmes@52722
    15
(* utility functions *)
boehmes@52722
    16
boehmes@52722
    17
val as_int = fst o read_int o raw_explode
boehmes@52722
    18
boehmes@52722
    19
val isabelle_name =
wenzelm@52732
    20
  let
boehmes@52722
    21
    fun purge s = if Symbol.is_letter s orelse Symbol.is_digit s then s else
boehmes@52722
    22
      (case s of
boehmes@52722
    23
        "." => "_o_"
boehmes@52722
    24
      | "_" => "_n_"
boehmes@52722
    25
      | "$" => "_S_"
boehmes@52722
    26
      | "@" => "_G_"
boehmes@52722
    27
      | "#" => "_H_"
boehmes@52722
    28
      | "^" => "_T_"
boehmes@52722
    29
      | _   => ("_" ^ string_of_int (ord s) ^ "_"))
boehmes@52722
    30
  in prefix "b_" o translate_string purge end
boehmes@52722
    31
boehmes@52722
    32
boehmes@52722
    33
(* context *)
boehmes@52722
    34
boehmes@52722
    35
type context =
boehmes@52722
    36
  typ Symtab.table * (term * bool) Symtab.table * term list * term list
boehmes@52722
    37
boehmes@52722
    38
val empty_context: context = (Symtab.empty, Symtab.empty, [], [])
boehmes@52722
    39
boehmes@52722
    40
fun add_type name (tds, fds, axs, vcs) =
boehmes@52722
    41
  let
boehmes@52722
    42
    val T = TFree (isabelle_name name, @{sort type})
boehmes@52722
    43
    val tds' = Symtab.update (name, T) tds
boehmes@52722
    44
  in (tds', fds, axs, vcs) end
boehmes@52722
    45
boehmes@52722
    46
fun add_func name Ts T unique (tds, fds, axs, vcs) =
boehmes@52722
    47
  let
boehmes@52722
    48
    val t = Free (isabelle_name name, Ts ---> T)
boehmes@52722
    49
    val fds' = Symtab.update (name, (t, unique)) fds
boehmes@52722
    50
  in (tds, fds', axs, vcs) end
boehmes@52722
    51
boehmes@52722
    52
fun add_axiom t (tds, fds, axs, vcs) = (tds, fds, t :: axs, vcs)
boehmes@52722
    53
boehmes@52722
    54
fun add_vc t (tds, fds, axs, vcs) = (tds, fds, axs, t :: vcs)
boehmes@52722
    55
boehmes@52722
    56
fun lookup_type (tds, _, _, _) name =
boehmes@52722
    57
  (case Symtab.lookup tds name of
boehmes@52722
    58
    SOME T => T
boehmes@52722
    59
  | NONE => error "Undeclared type")
boehmes@52722
    60
boehmes@52722
    61
fun lookup_func (_, fds, _, _) name =
boehmes@52722
    62
  (case Symtab.lookup fds name of
boehmes@52722
    63
    SOME t_unique => t_unique
boehmes@52722
    64
  | NONE => error "Undeclared function")
boehmes@52722
    65
boehmes@52722
    66
boehmes@52722
    67
(* constructors *)
boehmes@52722
    68
boehmes@52722
    69
fun mk_var name T = Free ("V_" ^ isabelle_name name, T)
boehmes@52722
    70
boehmes@52722
    71
fun mk_arrayT (Ts, T) = Type (@{type_name "fun"}, [HOLogic.mk_tupleT Ts, T])
boehmes@52722
    72
boehmes@52722
    73
fun mk_binary t (t1, t2) = t $ t1 $ t2
boehmes@52722
    74
boehmes@52722
    75
fun mk_nary _ t [] = t
boehmes@52722
    76
  | mk_nary f _ ts = uncurry (fold_rev f) (split_last ts)
boehmes@52722
    77
boehmes@52722
    78
fun mk_distinct [] = @{const HOL.True}
boehmes@52722
    79
  | mk_distinct [_] = @{const HOL.True}
boehmes@52722
    80
  | mk_distinct (t :: ts) =
boehmes@52722
    81
      let
boehmes@52722
    82
        fun mk_noteq u u' =
boehmes@52722
    83
          HOLogic.mk_conj (HOLogic.mk_not (HOLogic.mk_eq (t, u)), u')
boehmes@52722
    84
      in fold_rev mk_noteq ts (mk_distinct ts) end
boehmes@52722
    85
boehmes@52722
    86
fun mk_store m k v =
boehmes@52722
    87
  let
boehmes@52722
    88
    val mT = Term.fastype_of m and kT = Term.fastype_of k
boehmes@52722
    89
    val vT = Term.fastype_of v
boehmes@52722
    90
  in Const (@{const_name fun_upd}, mT --> kT --> vT --> mT) $ m $ k $ v end
boehmes@52722
    91
boehmes@52722
    92
fun mk_quant q (Free (x, T)) t = q T $ absfree (x, T) t
boehmes@52722
    93
  | mk_quant _ t _ = raise TERM ("bad variable", [t])
boehmes@52722
    94
blanchet@58061
    95
val patternT = @{typ "SMT.pattern"}
boehmes@52722
    96
boehmes@52722
    97
fun mk_pat t =
blanchet@58061
    98
  Const (@{const_name "SMT.pat"}, Term.fastype_of t --> patternT) $ t
boehmes@52722
    99
boehmes@52722
   100
fun mk_pattern [] = raise TERM ("mk_pattern", [])
blanchet@58061
   101
  | mk_pattern ts = SMT_Util.mk_symb_list patternT (map mk_pat ts)
boehmes@52722
   102
boehmes@52722
   103
fun mk_trigger [] t = t
boehmes@52722
   104
  | mk_trigger pss t =
blanchet@58061
   105
      @{term "SMT.trigger"} $
blanchet@58061
   106
        SMT_Util.mk_symb_list @{typ "SMT.pattern SMT.symb_list"} (map mk_pattern pss) $ t
boehmes@52722
   107
boehmes@52722
   108
boehmes@52722
   109
(* parser *)
boehmes@52722
   110
boehmes@52722
   111
fun repeat f n ls =
boehmes@52722
   112
  let fun apply (xs, ls) = f ls |>> (fn x => x :: xs)
boehmes@52722
   113
  in funpow (as_int n) apply ([], ls) |>> rev end
boehmes@52722
   114
boehmes@52722
   115
fun parse_type _ (["bool"] :: ls) = (@{typ bool}, ls)
boehmes@52722
   116
  | parse_type _ (["int"] :: ls) = (@{typ int}, ls)
boehmes@52722
   117
  | parse_type cx (["array", arity] :: ls) =
boehmes@52722
   118
      repeat (parse_type cx) arity ls |>> mk_arrayT o split_last
boehmes@52722
   119
  | parse_type cx (("type-con" :: name :: _) :: ls) = (lookup_type cx name, ls)
boehmes@52722
   120
  | parse_type _ _ = error "Bad type"
boehmes@52722
   121
boehmes@52722
   122
fun parse_expr _ (["true"] :: ls) = (@{term True}, ls)
boehmes@52722
   123
  | parse_expr _ (["false"] :: ls) = (@{term False}, ls)
boehmes@52722
   124
  | parse_expr cx (["not"] :: ls) = parse_expr cx ls |>> HOLogic.mk_not
blanchet@57232
   125
  | parse_expr cx (["and", n] :: ls) = parse_nary_expr cx n HOLogic.mk_conj @{term True} ls
blanchet@57232
   126
  | parse_expr cx (["or", n] :: ls) = parse_nary_expr cx n HOLogic.mk_disj @{term False} ls
blanchet@57232
   127
  | parse_expr cx (["implies"] :: ls) = parse_bin_expr cx (mk_binary @{term HOL.implies}) ls
boehmes@52722
   128
  | parse_expr cx (["="] :: ls) = parse_bin_expr cx HOLogic.mk_eq ls
boehmes@52722
   129
  | parse_expr cx (["var", name] :: ls) = parse_type cx ls |>> mk_var name
boehmes@52722
   130
  | parse_expr cx (["fun", name, n] :: ls) =
boehmes@52722
   131
      let val (t, _) = lookup_func cx name
boehmes@52722
   132
      in repeat (parse_expr cx) n ls |>> curry Term.list_comb t end
boehmes@52722
   133
  | parse_expr cx (("label" :: _) :: ls) = parse_expr cx ls
blanchet@57232
   134
  | parse_expr _ (["int-num", n] :: ls) = (HOLogic.mk_number @{typ int} (as_int n), ls)
blanchet@57232
   135
  | parse_expr cx (["<"] :: ls) = parse_bin_expr cx (mk_binary @{term "op < :: int => _"}) ls
blanchet@57232
   136
  | parse_expr cx (["<="] :: ls) = parse_bin_expr cx (mk_binary @{term "op <= :: int => _"}) ls
blanchet@57232
   137
  | parse_expr cx ([">"] :: ls) = parse_bin_expr cx (mk_binary @{term "op < :: int => _"}o swap) ls
boehmes@52722
   138
  | parse_expr cx ([">="] :: ls) =
boehmes@52722
   139
      parse_bin_expr cx (mk_binary @{term "op <= :: int => _"} o swap) ls
blanchet@57232
   140
  | parse_expr cx (["+"] :: ls) = parse_bin_expr cx (mk_binary @{term "op + :: int => _"}) ls
blanchet@57232
   141
  | parse_expr cx (["-"] :: ls) = parse_bin_expr cx (mk_binary @{term "op - :: int => _"}) ls
blanchet@57232
   142
  | parse_expr cx (["*"] :: ls) = parse_bin_expr cx (mk_binary @{term "op * :: int => _"}) ls
blanchet@57232
   143
  | parse_expr cx (["/"] :: ls) = parse_bin_expr cx (mk_binary @{term boogie_div}) ls
blanchet@57232
   144
  | parse_expr cx (["%"] :: ls) = parse_bin_expr cx (mk_binary @{term boogie_mod}) ls
boehmes@52722
   145
  | parse_expr cx (["select", n] :: ls) =
boehmes@52722
   146
      repeat (parse_expr cx) n ls
boehmes@52722
   147
      |>> (fn ts => hd ts $ HOLogic.mk_tuple (tl ts))
boehmes@52722
   148
  | parse_expr cx (["store", n] :: ls) =
boehmes@52722
   149
      repeat (parse_expr cx) n ls
boehmes@52722
   150
      |>> split_last
boehmes@52722
   151
      |>> (fn (ts, t) => mk_store (hd ts) (HOLogic.mk_tuple (tl ts)) t)
boehmes@52722
   152
  | parse_expr cx (["forall", vars, pats, atts] :: ls) =
boehmes@52722
   153
      parse_quant cx HOLogic.all_const vars pats atts ls
boehmes@52722
   154
  | parse_expr cx (["exists", vars, pats, atts] :: ls) =
boehmes@52722
   155
      parse_quant cx HOLogic.exists_const vars pats atts ls
boehmes@52722
   156
  | parse_expr _ _ = error "Bad expression"
boehmes@52722
   157
boehmes@52722
   158
and parse_bin_expr cx f ls = ls |> parse_expr cx ||>> parse_expr cx |>> f
boehmes@52722
   159
boehmes@52722
   160
and parse_nary_expr cx n f c ls =
boehmes@52722
   161
  repeat (parse_expr cx) n ls |>> mk_nary (curry f) c
boehmes@52722
   162
boehmes@52722
   163
and parse_quant cx q vars pats atts ls =
boehmes@52722
   164
  let
boehmes@52722
   165
    val ((((vs, pss), _), t), ls') =
boehmes@52722
   166
      ls
boehmes@52722
   167
      |> repeat (parse_var cx) vars
boehmes@52722
   168
      ||>> repeat (parse_pat cx) pats
boehmes@52722
   169
      ||>> repeat (parse_attr cx) atts
boehmes@52722
   170
      ||>> parse_expr cx
boehmes@52722
   171
  in (fold_rev (mk_quant q) vs (mk_trigger pss t), ls') end
boehmes@52722
   172
boehmes@52722
   173
and parse_var cx (["var", name] :: ls) = parse_type cx ls |>> mk_var name
boehmes@52722
   174
  | parse_var _ _ = error "Bad variable"
boehmes@52722
   175
boehmes@52722
   176
and parse_pat cx (["pat", n] :: ls) = repeat (parse_expr cx) n ls
boehmes@52722
   177
  | parse_pat _ _ = error "Bad pattern"
boehmes@52722
   178
boehmes@52722
   179
and parse_attr cx (["attribute", name, n] :: ls) =
boehmes@52722
   180
      let
boehmes@52722
   181
        fun attr (["expr-attr"] :: ls) = parse_expr cx ls |>> K ()
boehmes@52722
   182
          | attr (("string-attr" :: _) :: ls) = ((), ls)
boehmes@52722
   183
          | attr _ = error "Bad attribute value"
boehmes@52722
   184
      in repeat attr n ls |>> K name end
boehmes@52722
   185
  | parse_attr _ _ = error "Bad attribute"
boehmes@52722
   186
boehmes@52722
   187
fun parse_func cx arity n ls =
boehmes@52722
   188
  let
boehmes@52722
   189
    val ((Ts, atts), ls') =
boehmes@52722
   190
      ls |> repeat (parse_type cx) arity ||>> repeat (parse_attr cx) n
boehmes@52722
   191
    val unique = member (op =) atts "unique"
boehmes@52722
   192
  in ((split_last Ts, unique), ls') end
boehmes@52722
   193
boehmes@52722
   194
fun parse_decl (("type-decl" :: name :: _) :: ls) cx = (ls, add_type name cx)
boehmes@52722
   195
  | parse_decl (["fun-decl", name, arity, n] :: ls) cx =
boehmes@52722
   196
      let val (((Ts, T), unique), ls') = parse_func cx arity n ls
boehmes@52722
   197
      in (ls', add_func name Ts T unique cx) end
boehmes@52722
   198
  | parse_decl (("axiom" :: _) :: ls) cx =
boehmes@52722
   199
      let val (t, ls') = parse_expr cx ls
boehmes@52722
   200
      in (ls', add_axiom t cx) end
boehmes@52722
   201
  | parse_decl (("var-decl" :: _) :: ls) cx =
boehmes@52722
   202
      parse_type cx ls |> snd |> rpair cx
boehmes@52722
   203
  | parse_decl (("vc" :: _) :: ls) cx =
boehmes@52722
   204
      let val (t, ls') = parse_expr cx ls
boehmes@52722
   205
      in (ls', add_vc t cx) end
boehmes@52722
   206
  | parse_decl _ _ = error "Bad declaration"
boehmes@52722
   207
boehmes@52722
   208
fun parse_lines [] cx = cx
boehmes@52722
   209
  | parse_lines ls cx = parse_decl ls cx |-> parse_lines
boehmes@52722
   210
boehmes@52722
   211
wenzelm@55788
   212
(* splitting of text lines into a lists of tokens *)
boehmes@52722
   213
boehmes@52722
   214
fun is_blank c = (c = " " orelse c = "\t" orelse c = "\r" orelse c = "\n")
boehmes@52722
   215
wenzelm@55788
   216
val token_lines =
wenzelm@55788
   217
  map (String.tokens (is_blank o str))
wenzelm@55788
   218
  #> filter (fn [] => false | _ => true)
boehmes@52722
   219
boehmes@52722
   220
boehmes@52722
   221
(* proving verification conditions *)
boehmes@52722
   222
boehmes@52722
   223
fun add_unique_axioms (tds, fds, axs, vcs) =
boehmes@52722
   224
  Symtab.fold (fn (_, (t, true)) => cons t | _ => I) fds []
boehmes@52722
   225
  |> map (swap o Term.dest_Free)
boehmes@52722
   226
  |> AList.group (op =)
boehmes@52722
   227
  |> map (fn (T, ns) => mk_distinct (map (Free o rpair T) ns))
boehmes@52722
   228
  |> (fn axs' => (tds, fds, axs' @ axs, vcs))
boehmes@52722
   229
boehmes@52722
   230
fun build_proof_context thy (tds, fds, axs, vcs) =
boehmes@52722
   231
  let
boehmes@52722
   232
    val vc =
boehmes@52722
   233
      (case vcs of
boehmes@52722
   234
        [vc] => vc
boehmes@52722
   235
      | _ => error "Bad number of verification conditions")
boehmes@52722
   236
  in
boehmes@52722
   237
    Proof_Context.init_global thy
boehmes@52722
   238
    |> Symtab.fold (fn (_, T) => Variable.declare_typ T) tds
boehmes@52722
   239
    |> Symtab.fold (fn (_, (t, _)) => Variable.declare_term t) fds
boehmes@52722
   240
    |> fold Variable.declare_term axs
boehmes@52722
   241
    |> fold Variable.declare_term vcs
boehmes@52722
   242
    |> pair (map HOLogic.mk_Trueprop axs, HOLogic.mk_Trueprop vc)
boehmes@52722
   243
  end
boehmes@52722
   244
boehmes@52722
   245
val boogie_rules =
haftmann@61424
   246
  [@{thm fst_conv}, @{thm snd_conv}, @{thm prod.collapse}] @
boehmes@52722
   247
  [@{thm fun_upd_same}, @{thm fun_upd_apply}]
boehmes@52722
   248
boehmes@52722
   249
fun boogie_tac ctxt axioms =
blanchet@58061
   250
  ALLGOALS (SMT_Solver.smt_tac ctxt (boogie_rules @ axioms))
boehmes@52722
   251
wenzelm@55788
   252
fun boogie_prove thy lines =
boehmes@52722
   253
  let
boehmes@52722
   254
    val ((axioms, vc), ctxt) =
boehmes@52722
   255
      empty_context
wenzelm@55788
   256
      |> parse_lines (token_lines lines)
boehmes@52722
   257
      |> add_unique_axioms
wenzelm@54447
   258
      |> build_proof_context thy
boehmes@52722
   259
blanchet@57232
   260
    val _ = Goal.prove ctxt [] axioms vc (fn {prems, context} => boogie_tac context prems)
wenzelm@52732
   261
    val _ = writeln "Verification condition proved successfully"
boehmes@52722
   262
wenzelm@54447
   263
  in () end
wenzelm@54447
   264
wenzelm@54447
   265
wenzelm@54447
   266
(* Isar command *)
wenzelm@54447
   267
wenzelm@54447
   268
val _ =
wenzelm@59936
   269
  Outer_Syntax.command @{command_keyword boogie_file}
wenzelm@54447
   270
    "prove verification condition from .b2i file"
wenzelm@56208
   271
    (Resources.provide_parse_files "boogie_file" >> (fn files =>
wenzelm@54447
   272
      Toplevel.theory (fn thy =>
wenzelm@54447
   273
        let
wenzelm@55788
   274
          val ([{lines, ...}], thy') = files thy;
wenzelm@55788
   275
          val _ = boogie_prove thy' lines;
wenzelm@54447
   276
        in thy' end)))
boehmes@52722
   277
blanchet@57232
   278
end;