src/HOL/Boogie/Tools/boogie_loader.ML
author boehmes
Mon Dec 07 09:12:16 2009 +0100 (2009-12-07)
changeset 34011 2b3f4fcbc066
parent 33893 24b648ea4834
child 34068 a78307d72e58
permissions -rw-r--r--
verbose output of loaded data makes a clear distinction between new and already existing data (types, constants, axioms)
boehmes@33419
     1
(*  Title:      HOL/Boogie/Tools/boogie_loader.ML
boehmes@33419
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@33419
     3
boehmes@33419
     4
Loading and interpreting Boogie-generated files.
boehmes@33419
     5
*)
boehmes@33419
     6
boehmes@33419
     7
signature BOOGIE_LOADER =
boehmes@33419
     8
sig
boehmes@33419
     9
  val load_b2i: bool -> Path.T -> theory -> theory
boehmes@33419
    10
end
boehmes@33419
    11
boehmes@33419
    12
structure Boogie_Loader: BOOGIE_LOADER =
boehmes@33419
    13
struct
boehmes@33419
    14
boehmes@34011
    15
fun log verbose text args x =
boehmes@34011
    16
  if verbose andalso not (null args)
boehmes@34011
    17
  then (Pretty.writeln (Pretty.big_list text (map Pretty.str args)); x)
boehmes@34011
    18
  else x
boehmes@33419
    19
boehmes@33419
    20
val isabelle_name =
boehmes@33419
    21
  let 
boehmes@33445
    22
    fun purge s = if Symbol.is_letter s orelse Symbol.is_digit s then s else
boehmes@33419
    23
      (case s of
boehmes@33419
    24
        "." => "_o_"
boehmes@33419
    25
      | "_" => "_n_"
boehmes@33419
    26
      | "$" => "_S_"
boehmes@33419
    27
      | "@" => "_G_"
boehmes@33419
    28
      | "#" => "_H_"
boehmes@33419
    29
      | "^" => "_T_"
boehmes@33419
    30
      | _   => ("_" ^ string_of_int (ord s) ^ "_"))
boehmes@33419
    31
  in prefix "b_" o translate_string purge end
boehmes@33419
    32
boehmes@33445
    33
val short_name =
boehmes@33445
    34
  translate_string (fn s => if Symbol.is_letdig s then s else "")
boehmes@33445
    35
boehmes@33445
    36
fun label_name line col = "L_" ^ string_of_int line ^ "_" ^ string_of_int col
boehmes@33445
    37
boehmes@34011
    38
datatype attribute_value = StringValue of string | TermValue of term
boehmes@33419
    39
boehmes@33419
    40
boehmes@33419
    41
boehmes@33419
    42
local
boehmes@33419
    43
  fun lookup_type_name thy name arity =
boehmes@33419
    44
    let val intern = Sign.intern_type thy name
boehmes@33419
    45
    in
boehmes@33419
    46
      if Sign.declared_tyname thy intern
boehmes@33419
    47
      then
boehmes@33419
    48
        if Sign.arity_number thy intern = arity then SOME intern
boehmes@33419
    49
        else error ("Boogie: type already declared with different arity: " ^
boehmes@33419
    50
          quote name)
boehmes@33419
    51
      else NONE
boehmes@33419
    52
    end
boehmes@33419
    53
boehmes@34011
    54
  fun log_new bname name = bname ^ " (as " ^ name ^ ")"
boehmes@34011
    55
  fun log_ex bname name = "[" ^ bname ^ " has already been declared as " ^
boehmes@34011
    56
    name ^ "]"
boehmes@34011
    57
boehmes@33419
    58
  fun declare (name, arity) thy =
boehmes@33419
    59
    let val isa_name = isabelle_name name
boehmes@33419
    60
    in
boehmes@33419
    61
      (case lookup_type_name thy isa_name arity of
boehmes@34011
    62
        SOME type_name => (((name, type_name), log_ex name type_name), thy)
boehmes@33419
    63
      | NONE =>
boehmes@33419
    64
          let
boehmes@33419
    65
            val args = Name.variant_list [] (replicate arity "'")
boehmes@33419
    66
            val (T, thy') =
boehmes@33419
    67
              ObjectLogic.typedecl (Binding.name isa_name, args, NoSyn) thy
boehmes@33419
    68
            val type_name = fst (Term.dest_Type T)
boehmes@34011
    69
          in (((name, type_name), log_new name type_name), thy') end)
boehmes@33419
    70
    end
boehmes@33419
    71
in
boehmes@33419
    72
fun declare_types verbose tys =
boehmes@34011
    73
  fold_map declare tys #>> split_list #-> (fn (tds, logs) =>
boehmes@34011
    74
  log verbose "Declared types:" logs #>
boehmes@34011
    75
  rpair (Symtab.make tds))
boehmes@33419
    76
end
boehmes@33419
    77
boehmes@33419
    78
boehmes@33419
    79
boehmes@33419
    80
local
boehmes@33419
    81
  val quote_mixfix =
boehmes@33419
    82
    Symbol.explode #> map
boehmes@33419
    83
      (fn "_" => "'_"
boehmes@33419
    84
        | "(" => "'("
boehmes@33419
    85
        | ")" => "')"
boehmes@33419
    86
        | "/" => "'/"
boehmes@33419
    87
        | s => s) #>
boehmes@33419
    88
    implode
boehmes@33419
    89
boehmes@33419
    90
  fun mk_syntax name i =
boehmes@33419
    91
    let
boehmes@33419
    92
      val syn = quote_mixfix name
boehmes@33419
    93
      val args = concat (separate ",/ " (replicate i "_"))
boehmes@33419
    94
    in
boehmes@33419
    95
      if i = 0 then Mixfix (syn, [], 1000)
boehmes@33419
    96
      else Mixfix (syn ^ "()'(/" ^ args ^ "')", replicate i 0, 1000)
boehmes@33419
    97
    end
boehmes@33419
    98
boehmes@33893
    99
  fun maybe_builtin T =
boehmes@33419
   100
    let
boehmes@33419
   101
      fun const name = SOME (Const (name, T))
boehmes@33419
   102
boehmes@33419
   103
      fun choose builtin =
boehmes@33419
   104
        (case builtin of
boehmes@33419
   105
          "bvnot" => const @{const_name z3_bvnot}
boehmes@33419
   106
        | "bvand" => const @{const_name z3_bvand}
boehmes@33419
   107
        | "bvor" => const @{const_name z3_bvor}
boehmes@33419
   108
        | "bvxor" => const @{const_name z3_bvxor}
boehmes@33419
   109
        | "bvadd" => const @{const_name z3_bvadd}
boehmes@33419
   110
        | "bvneg" => const @{const_name z3_bvneg}
boehmes@33419
   111
        | "bvsub" => const @{const_name z3_bvsub}
boehmes@33419
   112
        | "bvmul" => const @{const_name z3_bvmul}
boehmes@33419
   113
        | "bvudiv" => const @{const_name z3_bvudiv}
boehmes@33419
   114
        | "bvurem" => const @{const_name z3_bvurem}
boehmes@33419
   115
        | "bvsdiv" => const @{const_name z3_bvsdiv}
boehmes@33419
   116
        | "bvsrem" => const @{const_name z3_bvsrem}
boehmes@33419
   117
        | "bvshl" => const @{const_name z3_bvshl}
boehmes@33419
   118
        | "bvlshr" => const @{const_name z3_bvlshr}
boehmes@33419
   119
        | "bvashr" => const @{const_name z3_bvashr}
boehmes@33419
   120
        | "bvult" => const @{const_name z3_bvult}
boehmes@33419
   121
        | "bvule" => const @{const_name z3_bvule}
boehmes@33419
   122
        | "bvugt" => const @{const_name z3_bvugt}
boehmes@33419
   123
        | "bvuge" => const @{const_name z3_bvuge}
boehmes@33419
   124
        | "bvslt" => const @{const_name z3_bvslt}
boehmes@33419
   125
        | "bvsle" => const @{const_name z3_bvsle}
boehmes@33419
   126
        | "bvsgt" => const @{const_name z3_bvsgt}
boehmes@33419
   127
        | "bvsge" => const @{const_name z3_bvsge}
boehmes@33419
   128
        | "sign_extend" => const @{const_name z3_sign_extend}
boehmes@33419
   129
        | _ => NONE)
boehmes@33419
   130
boehmes@33419
   131
      fun is_builtin att =
boehmes@33419
   132
        (case att of
boehmes@33419
   133
          ("bvbuiltin", [StringValue builtin]) => choose builtin
boehmes@33419
   134
        | ("bvint", [StringValue "ITE"]) => const @{const_name If}
boehmes@33419
   135
        | _ => NONE)
boehmes@33419
   136
    in get_first is_builtin end
boehmes@33419
   137
boehmes@33419
   138
  fun lookup_const thy name T =
boehmes@33465
   139
    let val intern = Sign.intern_const thy name
boehmes@33419
   140
    in
boehmes@33419
   141
      if Sign.declared_const thy intern
boehmes@33419
   142
      then
boehmes@33465
   143
        if Sign.typ_instance thy (T, Sign.the_const_type thy intern)
boehmes@33419
   144
        then SOME (Const (intern, T))
boehmes@33419
   145
        else error ("Boogie: function already declared with different type: " ^
boehmes@33419
   146
          quote name)
boehmes@33419
   147
      else NONE
boehmes@33419
   148
    end
boehmes@33419
   149
boehmes@34011
   150
  fun log_term thy t = Syntax.string_of_term_global thy t
boehmes@34011
   151
  fun log_new thy name t = name ^ " (as " ^ log_term thy t ^ ")"
boehmes@34011
   152
  fun log_ex thy name t = "[" ^ name ^ " has already been declared as " ^
boehmes@34011
   153
    log_term thy t ^ "]"
boehmes@34011
   154
  fun log_builtin thy name t = "[" ^ name ^ " has been identified as " ^
boehmes@34011
   155
    log_term thy t ^ "]"
boehmes@33419
   156
boehmes@34011
   157
  fun declare' name isa_name T arity atts thy =
boehmes@34011
   158
    (case lookup_const thy isa_name T of
boehmes@34011
   159
      SOME t => (((name, t), log_ex thy name t), thy)
boehmes@34011
   160
    | NONE =>
boehmes@34011
   161
        (case maybe_builtin T atts of
boehmes@34011
   162
          SOME t => (((name, t), log_builtin thy name t), thy)
boehmes@34011
   163
        | NONE =>
boehmes@34011
   164
            thy
boehmes@34011
   165
            |> Sign.declare_const ((Binding.name isa_name, T),
boehmes@34011
   166
                 mk_syntax name arity)
boehmes@34011
   167
            |> (fn (t, thy') => (((name, t), log_new thy' name t), thy'))))
boehmes@34011
   168
  fun declare (name, ((Ts, T), atts)) =
boehmes@34011
   169
    declare' name (isabelle_name name) (Ts ---> T) (length Ts) atts
boehmes@33893
   170
boehmes@33893
   171
  fun uniques fns fds =
boehmes@33893
   172
    let
boehmes@33893
   173
      fun is_unique (name, (([], T), atts)) =
boehmes@33893
   174
            (case AList.lookup (op =) atts "unique" of
boehmes@33893
   175
              SOME _ => Symtab.lookup fds name
boehmes@33893
   176
            | NONE => NONE)
boehmes@33893
   177
        | is_unique _ = NONE
boehmes@33893
   178
      fun mk_unique_axiom T ts =
boehmes@33893
   179
        Const (@{const_name distinct}, HOLogic.listT T --> @{typ bool}) $
boehmes@33893
   180
          HOLogic.mk_list T ts
boehmes@33893
   181
    in
boehmes@33893
   182
      map_filter is_unique fns
boehmes@33893
   183
      |> map (swap o Term.dest_Const)
boehmes@33893
   184
      |> AList.group (op =)
boehmes@33893
   185
      |> map (fn (T, ns) => mk_unique_axiom T (map (Const o rpair T) ns))
boehmes@33893
   186
    end
boehmes@33419
   187
in
boehmes@33419
   188
fun declare_functions verbose fns =
boehmes@34011
   189
  fold_map declare fns #>> split_list #-> (fn (fds, logs) =>
boehmes@34011
   190
  log verbose "Loaded constants:" logs #>
boehmes@34011
   191
  rpair (` (uniques fns) (Symtab.make fds)))
boehmes@33419
   192
end
boehmes@33419
   193
boehmes@33419
   194
boehmes@33419
   195
boehmes@33419
   196
local
boehmes@33419
   197
  fun name_axioms axs =
boehmes@33419
   198
    let fun mk_name idx = "axiom_" ^ string_of_int (idx + 1)
boehmes@33419
   199
    in map_index (fn (idx, t) => (mk_name idx, HOLogic.mk_Trueprop t)) axs end
boehmes@33419
   200
boehmes@34011
   201
  datatype kind = Unused of thm | Used of thm | New of string
boehmes@34011
   202
boehmes@34011
   203
  fun mark (name, t) axs =
boehmes@34011
   204
    (case Termtab.lookup axs t of
boehmes@34011
   205
      SOME (Unused thm) => Termtab.update (t, Used thm) axs
boehmes@34011
   206
    | NONE => Termtab.update (t, New name) axs
boehmes@34011
   207
    | SOME _ => axs)
boehmes@34011
   208
boehmes@34011
   209
  val sort_fst_str = sort (prod_ord fast_string_ord (K EQUAL)) 
boehmes@34011
   210
  fun split_list_kind thy axs =
boehmes@34011
   211
    let
boehmes@34011
   212
      fun split (_, Used thm) (used, new) = (thm :: used, new)
boehmes@34011
   213
        | split (t, New name) (used, new) = (used, (name, t) :: new)
boehmes@34011
   214
        | split (t, Unused thm) (used, new) =
boehmes@34011
   215
           (warning (Pretty.str_of
boehmes@34011
   216
             (Pretty.big_list "This background axiom has not been loaded:"
boehmes@34011
   217
               [Display.pretty_thm_global thy thm]));
boehmes@34011
   218
            (used, new))
boehmes@34011
   219
    in apsnd sort_fst_str (fold split axs ([], [])) end
boehmes@34011
   220
boehmes@34011
   221
  fun mark_axioms thy axs =
boehmes@34011
   222
    Boogie_Axioms.get (ProofContext.init thy)
boehmes@34011
   223
    |> Termtab.make o map (fn thm => (Thm.prop_of thm, Unused thm))
boehmes@34011
   224
    |> fold mark axs
boehmes@34011
   225
    |> split_list_kind thy o Termtab.dest
boehmes@33419
   226
in
boehmes@33419
   227
fun add_axioms verbose axs thy =
boehmes@34011
   228
  let val (used, new) = mark_axioms thy (name_axioms axs)
boehmes@33419
   229
  in
boehmes@33419
   230
    thy
boehmes@34011
   231
    |> PureThy.add_axioms (map (rpair [] o apfst Binding.name) new)
boehmes@33419
   232
    |-> Context.theory_map o fold Boogie_Axioms.add_thm
boehmes@34011
   233
    |> log verbose "The following axioms were added:" (map fst new)
boehmes@34011
   234
    |> (fn thy' => log verbose "The following axioms already existed:"
boehmes@34011
   235
         (map (Display.string_of_thm_global thy') used) thy')
boehmes@33419
   236
    |> Context.theory_map (fn context => fold Split_VC_SMT_Rules.add_thm
boehmes@33419
   237
         (Boogie_Axioms.get (Context.proof_of context)) context)
boehmes@33419
   238
  end
boehmes@33419
   239
end
boehmes@33419
   240
boehmes@33419
   241
boehmes@33419
   242
boehmes@33445
   243
local
boehmes@33445
   244
  fun burrow_distinct eq f xs =
boehmes@33445
   245
    let
boehmes@33445
   246
      val ys = distinct eq xs
boehmes@33445
   247
      val tab = ys ~~ f ys
boehmes@33445
   248
    in map (the o AList.lookup eq tab) xs end
boehmes@33445
   249
boehmes@33445
   250
  fun indexed names =
boehmes@33445
   251
    let
boehmes@33445
   252
      val dup = member (op =) (duplicates (op =) (map fst names))
boehmes@33445
   253
      fun make_name (n, i) = n ^ (if dup n then "_" ^ string_of_int i else "")
boehmes@33445
   254
    in map make_name names end
boehmes@33445
   255
boehmes@33445
   256
  fun rename idx_names =
boehmes@33445
   257
    idx_names
boehmes@33445
   258
    |> burrow_fst (burrow_distinct (op =)
boehmes@33445
   259
         (map short_name #> ` (duplicates (op =)) #-> Name.variant_list))
boehmes@33445
   260
    |> indexed
boehmes@33445
   261
in
boehmes@33419
   262
fun add_vcs verbose vcs thy =
boehmes@33419
   263
  let
boehmes@33445
   264
    val vcs' =
boehmes@33445
   265
      vcs
boehmes@33445
   266
      |> burrow_fst rename
boehmes@33445
   267
      |> map (apsnd HOLogic.mk_Trueprop)
boehmes@33419
   268
  in
boehmes@33419
   269
    thy
boehmes@33419
   270
    |> Boogie_VCs.set vcs'
boehmes@33419
   271
    |> log verbose "The following verification conditions were loaded:"
boehmes@33419
   272
         (map fst vcs')
boehmes@33419
   273
  end
boehmes@33445
   274
end
boehmes@33419
   275
boehmes@33419
   276
boehmes@33419
   277
boehmes@33419
   278
local
boehmes@33419
   279
  fun mk_bitT i T =
boehmes@33419
   280
    if i = 0
boehmes@33419
   281
    then Type (@{type_name "Numeral_Type.bit0"}, [T])
boehmes@33419
   282
    else Type (@{type_name "Numeral_Type.bit1"}, [T])
boehmes@33419
   283
boehmes@33419
   284
  fun mk_binT size = 
boehmes@33419
   285
    if size = 0 then @{typ "Numeral_Type.num0"}
boehmes@33419
   286
    else if size = 1 then @{typ "Numeral_Type.num1"}
boehmes@33419
   287
    else let val (q, r) = Integer.div_mod size 2 in mk_bitT r (mk_binT q) end
boehmes@33419
   288
in
boehmes@33419
   289
fun mk_wordT size =
boehmes@33419
   290
  if size >= 0 then Type (@{type_name "word"}, [mk_binT size])
boehmes@33419
   291
  else raise TYPE ("mk_wordT: " ^ quote (string_of_int size), [], [])
boehmes@33419
   292
end
boehmes@33419
   293
boehmes@33419
   294
local
boehmes@33419
   295
  fun dest_binT T =
boehmes@33419
   296
    (case T of
boehmes@33419
   297
      Type (@{type_name "Numeral_Type.num0"}, _) => 0
boehmes@33419
   298
    | Type (@{type_name "Numeral_Type.num1"}, _) => 1
boehmes@33419
   299
    | Type (@{type_name "Numeral_Type.bit0"}, [T]) => 2 * dest_binT T
boehmes@33419
   300
    | Type (@{type_name "Numeral_Type.bit1"}, [T]) => 1 + 2 * dest_binT T
boehmes@33419
   301
    | _ => raise TYPE ("dest_binT", [T], []))
boehmes@33419
   302
in
boehmes@33419
   303
val dest_wordT = (fn
boehmes@33419
   304
    Type (@{type_name "word"}, [T]) => dest_binT T
boehmes@33419
   305
  | T => raise TYPE ("dest_wordT", [T], []))
boehmes@33419
   306
end
boehmes@33419
   307
boehmes@33419
   308
fun mk_arrayT (Ts, T) = Type (@{type_name "fun"}, [HOLogic.mk_tupleT Ts, T])
boehmes@33419
   309
boehmes@33419
   310
boehmes@33419
   311
boehmes@33419
   312
datatype token = Token of string | Newline | EOF
boehmes@33419
   313
boehmes@33419
   314
fun tokenize path =
boehmes@33419
   315
  let
boehmes@33419
   316
    fun blank c = (c = #" " orelse c = #"\t" orelse c = #"\n" orelse c = #"\r")
boehmes@33419
   317
    fun split line (i, tss) = (i + 1,
boehmes@33419
   318
      map (pair i) (map Token (String.tokens blank line) @ [Newline]) :: tss)
boehmes@33419
   319
  in apsnd (flat o rev) (File.fold_lines split path (1, [])) end
boehmes@33419
   320
boehmes@33419
   321
fun stopper i = Scan.stopper (K (i, EOF)) (fn (_, EOF) => true | _ => false)
boehmes@33419
   322
boehmes@33419
   323
fun scan_err msg [] = "Boogie (at end of input): " ^ msg
boehmes@33419
   324
  | scan_err msg ((i, _) :: _) = "Boogie (at line " ^ string_of_int i ^ "): " ^
boehmes@33419
   325
      msg
boehmes@33419
   326
boehmes@33419
   327
fun scan_fail msg = Scan.fail_with (scan_err msg)
boehmes@33419
   328
boehmes@33419
   329
fun finite scan path =
boehmes@33419
   330
  let val (i, ts) = tokenize path
boehmes@33419
   331
  in
boehmes@33419
   332
    (case Scan.error (Scan.finite (stopper i) scan) ts of
boehmes@33419
   333
      (x, []) => x
boehmes@33419
   334
    | (_, ts) => error (scan_err "unparsed input" ts))
boehmes@33419
   335
  end
boehmes@33419
   336
boehmes@33419
   337
fun read_int' s = (case read_int (explode s) of (i, []) => SOME i | _ => NONE)
boehmes@33419
   338
boehmes@33419
   339
fun $$$ s = Scan.one (fn (_, Token s') => s = s' | _ => false)
boehmes@33497
   340
fun str st = Scan.some (fn (_, Token s) => SOME s | _ => NONE) st
boehmes@33497
   341
fun num st = Scan.some (fn (_, Token s) => read_int' s | _ => NONE) st
boehmes@33419
   342
boehmes@33419
   343
fun scan_line key scan =
boehmes@33419
   344
  $$$ key |-- scan --| Scan.one (fn (_, Newline) => true | _ => false)
boehmes@33419
   345
fun scan_line' key = scan_line key (Scan.succeed ())
boehmes@33419
   346
boehmes@33419
   347
fun scan_count scan i =
boehmes@33419
   348
  if i > 0 then scan ::: scan_count scan (i - 1) else Scan.succeed []
boehmes@33419
   349
boehmes@33419
   350
fun scan_lookup kind tab key =
boehmes@33419
   351
  (case Symtab.lookup tab key of
boehmes@33419
   352
    SOME value => Scan.succeed value
boehmes@33419
   353
  | NONE => scan_fail ("undefined " ^ kind ^ ": " ^ quote key))
boehmes@33419
   354
boehmes@33419
   355
fun typ tds =
boehmes@33419
   356
  let
boehmes@33419
   357
    fun tp st =
boehmes@33419
   358
     (scan_line' "bool" >> K @{typ bool} ||
boehmes@33419
   359
      scan_line' "int" >> K @{typ int} ||
boehmes@33419
   360
      scan_line "bv" num >> mk_wordT ||
boehmes@33419
   361
      scan_line "type-con" (str -- num) :|-- (fn (name, arity) =>
boehmes@33419
   362
        scan_lookup "type constructor" tds name -- scan_count tp arity >>
boehmes@33419
   363
        Type) ||
boehmes@33419
   364
      scan_line "array" num :|-- (fn arity =>
boehmes@33419
   365
        scan_count tp (arity - 1) -- tp >> mk_arrayT) ||
boehmes@33419
   366
      scan_fail "illegal type") st
boehmes@33419
   367
  in tp end
boehmes@33419
   368
boehmes@33419
   369
local
boehmes@33419
   370
  fun mk_nary _ t [] = t
boehmes@33661
   371
    | mk_nary f _ ts = uncurry (fold_rev f) (split_last ts)
boehmes@33419
   372
boehmes@33419
   373
  fun mk_distinct T ts =
boehmes@33419
   374
    Const (@{const_name distinct}, HOLogic.listT T --> @{typ bool}) $ 
boehmes@33419
   375
      HOLogic.mk_list T ts
boehmes@33419
   376
boehmes@33419
   377
  fun quant name f = scan_line name (num -- num -- num) >> pair f
boehmes@33419
   378
  val quants =
boehmes@33419
   379
    quant "forall" HOLogic.all_const ||
boehmes@33419
   380
    quant "exists" HOLogic.exists_const ||
boehmes@33419
   381
    scan_fail "illegal quantifier kind"
boehmes@33445
   382
  fun mk_quant q (n, T) t = q T $ Term.absfree (n, T, t)
boehmes@33419
   383
boehmes@33419
   384
  val patternT = @{typ pattern}
boehmes@33419
   385
  fun mk_pattern _ [] = raise TERM ("mk_pattern", [])
boehmes@33419
   386
    | mk_pattern n [t] = Const (n, Term.fastype_of t --> patternT) $ t
boehmes@33419
   387
    | mk_pattern n (t :: ts) =
boehmes@33419
   388
        let val T = patternT --> Term.fastype_of t --> patternT
boehmes@33419
   389
        in Const (@{const_name andpat}, T) $ mk_pattern n ts $ t end
boehmes@33419
   390
  fun patt n c scan =
boehmes@33419
   391
    scan_line n num :|-- scan_count scan >> (mk_pattern c)
boehmes@33419
   392
  fun pattern scan =
boehmes@33419
   393
    patt "pat" @{const_name pat} scan ||
boehmes@33419
   394
    patt "nopat" @{const_name nopat} scan ||
boehmes@33419
   395
    scan_fail "illegal pattern kind"
boehmes@33419
   396
  fun mk_trigger [] t = t
boehmes@33419
   397
    | mk_trigger ps t = @{term trigger} $ HOLogic.mk_list patternT ps $ t
boehmes@33419
   398
boehmes@33419
   399
  val label_kind =
boehmes@33419
   400
    $$$ "pos" >> K @{term block_at} ||
boehmes@33419
   401
    $$$ "neg" >> K @{term assert_at} ||
boehmes@33419
   402
    scan_fail "illegal label kind"
boehmes@33419
   403
boehmes@33419
   404
  fun mk_select (m, k) =
boehmes@33419
   405
    let
boehmes@33419
   406
      val mT = Term.fastype_of m and kT = Term.fastype_of k
boehmes@33419
   407
      val vT = Term.range_type mT
boehmes@33419
   408
    in Const (@{const_name boogie_select}, mT --> kT --> vT) $ m $ k end
boehmes@33419
   409
boehmes@33419
   410
  fun mk_store ((m, k), v) =
boehmes@33419
   411
    let
boehmes@33419
   412
      val mT = Term.fastype_of m and kT = Term.fastype_of k
boehmes@33419
   413
      val vT = Term.fastype_of v
boehmes@33419
   414
    in
boehmes@33419
   415
      Const (@{const_name boogie_store}, mT --> kT --> vT --> mT) $ m $ k $ v
boehmes@33419
   416
    end
boehmes@33419
   417
  
boehmes@33419
   418
  fun mk_extract ((msb, lsb), t) =
boehmes@33419
   419
    let
boehmes@33419
   420
      val dT = Term.fastype_of t and rT = mk_wordT (msb - lsb)
boehmes@33419
   421
      val nT = @{typ nat}
boehmes@33419
   422
      val mk_nat_num = HOLogic.mk_number @{typ nat}
boehmes@33419
   423
    in
boehmes@33419
   424
      Const (@{const_name boogie_bv_extract}, [nT, nT, dT] ---> rT) $
boehmes@33419
   425
        mk_nat_num msb $ mk_nat_num lsb $ t
boehmes@33419
   426
    end
boehmes@33419
   427
boehmes@33419
   428
  fun mk_concat (t1, t2) =
boehmes@33419
   429
    let
boehmes@33419
   430
      val T1 = Term.fastype_of t1 and T2 = Term.fastype_of t2
boehmes@33419
   431
      val U = mk_wordT (dest_wordT T1 + dest_wordT T2)
boehmes@33419
   432
    in Const (@{const_name boogie_bv_concat}, [T1, T2] ---> U) $ t1 $ t2 end
boehmes@33445
   433
boehmes@33445
   434
  val var_name = str >> prefix "V"
boehmes@33445
   435
  val dest_var_name = unprefix "V"
boehmes@33445
   436
  fun rename_variables t =
boehmes@33445
   437
    let
boehmes@33445
   438
      fun make_context names = Name.make_context (duplicates (op =) names)
boehmes@33445
   439
      fun short_var_name n = short_name (dest_var_name n)
boehmes@33445
   440
boehmes@33445
   441
      val (names, nctxt) =
boehmes@33445
   442
        Term.add_free_names t []
boehmes@33445
   443
        |> map_filter (try (fn n => (n, short_var_name n)))
boehmes@33445
   444
        |> split_list
boehmes@33445
   445
        ||>> (fn names => Name.variants names (make_context names))
boehmes@33445
   446
        |>> Symtab.make o (op ~~)
boehmes@33445
   447
boehmes@33445
   448
      fun rename_free n = the_default n (Symtab.lookup names n)
boehmes@33445
   449
      fun rename_abs n = yield_singleton Name.variants (short_var_name n)
boehmes@33445
   450
boehmes@33445
   451
      fun rename _ (Free (n, T)) = Free (rename_free n, T)
boehmes@33445
   452
        | rename nctxt (Abs (n, T, t)) =
boehmes@33445
   453
            let val (n', nctxt') = rename_abs n nctxt
boehmes@33445
   454
            in Abs (n', T, rename nctxt' t) end
boehmes@33445
   455
        | rename nctxt (t $ u) = rename nctxt t $ rename nctxt u
boehmes@33445
   456
        | rename _ t = t
boehmes@33445
   457
    in rename nctxt t end
boehmes@33419
   458
in
boehmes@33419
   459
fun expr tds fds =
boehmes@33419
   460
  let
boehmes@33419
   461
    fun binop t (u1, u2) = t $ u1 $ u2
boehmes@33419
   462
    fun binexp s f = scan_line' s |-- exp -- exp >> f
boehmes@33419
   463
boehmes@33419
   464
    and exp st =
boehmes@33419
   465
     (scan_line' "true" >> K @{term True} ||
boehmes@33419
   466
      scan_line' "false" >> K @{term False} ||
boehmes@33419
   467
      scan_line' "not" |-- exp >> HOLogic.mk_not ||
boehmes@33419
   468
      scan_line "and" num :|-- scan_count exp >> 
boehmes@33419
   469
        mk_nary (curry HOLogic.mk_conj) @{term True} ||
boehmes@33419
   470
      scan_line "or" num :|-- scan_count exp >>
boehmes@33419
   471
        mk_nary (curry HOLogic.mk_disj) @{term False} ||
boehmes@33419
   472
      binexp "implies" (binop @{term "op -->"}) ||
boehmes@33419
   473
      scan_line "distinct" num :|-- scan_count exp >>
boehmes@33419
   474
        (fn [] => @{term True}
boehmes@33419
   475
          | ts as (t :: _) => mk_distinct (Term.fastype_of t) ts) ||
boehmes@33419
   476
      binexp "=" HOLogic.mk_eq ||
boehmes@33445
   477
      scan_line "var" var_name -- typ tds >> Free ||
boehmes@33419
   478
      scan_line "fun" (str -- num) :|-- (fn (name, arity) =>
boehmes@33419
   479
        scan_lookup "constant" fds name -- scan_count exp arity >>
boehmes@33419
   480
        Term.list_comb) ||
boehmes@33419
   481
      quants :|-- (fn (q, ((n, k), i)) =>
boehmes@33445
   482
        scan_count (scan_line "var" var_name -- typ tds) n --
boehmes@33419
   483
        scan_count (pattern exp) k --
boehmes@33419
   484
        scan_count (attribute tds fds) i --
boehmes@33419
   485
        exp >> (fn (((vs, ps), _), t) =>
boehmes@33419
   486
          fold_rev (mk_quant q) vs (mk_trigger ps t))) ||
boehmes@33419
   487
      scan_line "label" (label_kind -- num -- num) -- exp >>
boehmes@33419
   488
        (fn (((l, line), col), t) =>
boehmes@33419
   489
          if line = 0 orelse col = 0 then t
boehmes@33445
   490
          else l $ Free (label_name line col, @{typ bool}) $ t) ||
boehmes@33419
   491
      scan_line "int-num" num >> HOLogic.mk_number @{typ int} ||
boehmes@33419
   492
      binexp "<" (binop @{term "op < :: int => _"}) ||
boehmes@33419
   493
      binexp "<=" (binop @{term "op <= :: int => _"}) ||
boehmes@33419
   494
      binexp ">" (binop @{term "op < :: int => _"} o swap) ||
boehmes@33419
   495
      binexp ">=" (binop @{term "op <= :: int => _"} o swap) ||
boehmes@33419
   496
      binexp "+" (binop @{term "op + :: int => _"}) ||
boehmes@33661
   497
      binexp "-" (binop @{term "op - :: int => _"}) ||
boehmes@33661
   498
      binexp "*" (binop @{term "op * :: int => _"}) ||
boehmes@33419
   499
      binexp "/" (binop @{term boogie_div}) ||
boehmes@33419
   500
      binexp "%" (binop @{term boogie_mod}) ||
boehmes@33419
   501
      scan_line "select" num :|-- (fn arity =>
boehmes@33419
   502
        exp -- (scan_count exp (arity - 1) >> HOLogic.mk_tuple) >>
boehmes@33419
   503
          mk_select) ||
boehmes@33419
   504
      scan_line "store" num :|-- (fn arity =>
boehmes@33419
   505
        exp -- (scan_count exp (arity - 2) >> HOLogic.mk_tuple) -- exp >> 
boehmes@33419
   506
          mk_store) ||
boehmes@33419
   507
      scan_line "bv-num" (num -- num) >> (fn (size, i) =>
boehmes@33419
   508
        HOLogic.mk_number (mk_wordT size) i) ||
boehmes@33419
   509
      scan_line "bv-extract" (num -- num) -- exp >> mk_extract ||
boehmes@33419
   510
      binexp "bv-concat" mk_concat ||
boehmes@33419
   511
      scan_fail "illegal expression") st
boehmes@33445
   512
  in exp >> rename_variables end
boehmes@33419
   513
boehmes@33419
   514
and attribute tds fds =
boehmes@33419
   515
  let
boehmes@33419
   516
    val attr_val = 
boehmes@33419
   517
      scan_line' "expr-attr" |-- expr tds fds >> TermValue ||
boehmes@33419
   518
      scan_line "string-attr" (Scan.repeat1 str) >>
boehmes@33419
   519
        (StringValue o space_implode " ") ||
boehmes@33419
   520
      scan_fail "illegal attribute value"
boehmes@33419
   521
  in
boehmes@33419
   522
    scan_line "attribute" (str -- num) :|-- (fn (name, i) =>
boehmes@33419
   523
      scan_count attr_val i >> pair name) ||
boehmes@33419
   524
    scan_fail "illegal attribute"
boehmes@33419
   525
  end
boehmes@33419
   526
end
boehmes@33419
   527
boehmes@33419
   528
fun type_decls verbose = Scan.depend (fn thy => 
boehmes@33419
   529
  Scan.repeat (scan_line "type-decl" (str -- num -- num) :|-- (fn (ty, i) =>
boehmes@33419
   530
    scan_count (attribute Symtab.empty Symtab.empty) i >> K ty)) >>
boehmes@33419
   531
    (fn tys => declare_types verbose tys thy))
boehmes@33419
   532
boehmes@33419
   533
fun fun_decls verbose tds = Scan.depend (fn thy =>
boehmes@33419
   534
  Scan.repeat (scan_line "fun-decl" (str -- num -- num) :|--
boehmes@33419
   535
    (fn ((name, arity), i) =>
boehmes@33419
   536
      scan_count (typ tds) (arity - 1) -- typ tds --
boehmes@33419
   537
      scan_count (attribute tds Symtab.empty) i >> pair name)) >>
boehmes@33419
   538
    (fn fns => declare_functions verbose fns thy))
boehmes@33419
   539
boehmes@33893
   540
fun axioms verbose tds fds unique_axs = Scan.depend (fn thy =>
boehmes@33419
   541
  Scan.repeat (scan_line "axiom" num :|-- (fn i =>
boehmes@33419
   542
    expr tds fds --| scan_count (attribute tds fds) i)) >>
boehmes@33893
   543
    (fn axs => (add_axioms verbose (unique_axs @ axs) thy, ())))
boehmes@33419
   544
boehmes@33419
   545
fun var_decls tds fds = Scan.depend (fn thy =>
boehmes@33419
   546
  Scan.repeat (scan_line "var-decl" (str -- num) :|-- (fn (_, i) =>
boehmes@33419
   547
    typ tds -- scan_count (attribute tds fds) i >> K ())) >> K (thy, ()))
boehmes@33419
   548
boehmes@33419
   549
fun vcs verbose tds fds = Scan.depend (fn thy =>
boehmes@33419
   550
  Scan.repeat (scan_line "vc" (str -- num) -- 
boehmes@33419
   551
    (expr tds fds >> Sign.cert_term thy)) >>
boehmes@33419
   552
    (fn vcs => ((), add_vcs verbose vcs thy)))
boehmes@33419
   553
boehmes@33419
   554
fun parse verbose thy = Scan.pass thy
boehmes@33419
   555
 (type_decls verbose :|-- (fn tds =>
boehmes@33893
   556
  fun_decls verbose tds :|-- (fn (unique_axs, fds) =>
boehmes@33893
   557
  axioms verbose tds fds unique_axs |--
boehmes@33419
   558
  var_decls tds fds |--
boehmes@33419
   559
  vcs verbose tds fds)))
boehmes@33419
   560
boehmes@33419
   561
fun load_b2i verbose path thy = finite (parse verbose thy) path
boehmes@33419
   562
boehmes@33419
   563
end