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