src/HOL/Boogie/Tools/boogie_loader.ML
author boehmes
Tue Nov 03 17:54:24 2009 +0100 (2009-11-03)
changeset 33419 8ae45e87b992
child 33424 a3b002e2cd55
permissions -rw-r--r--
added HOL-Boogie
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@33419
    15
fun log verbose text args thy =
boehmes@33419
    16
  if verbose
boehmes@33419
    17
  then (Pretty.writeln (Pretty.big_list text (map Pretty.str args)); thy)
boehmes@33419
    18
  else thy
boehmes@33419
    19
boehmes@33419
    20
val isabelle_name =
boehmes@33419
    21
  let 
boehmes@33419
    22
    fun purge s = if Symbol.is_letdig 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@33419
    33
datatype attribute_value = StringValue of string | TermValue of Term.term
boehmes@33419
    34
boehmes@33419
    35
boehmes@33419
    36
boehmes@33419
    37
local
boehmes@33419
    38
  fun lookup_type_name thy name arity =
boehmes@33419
    39
    let val intern = Sign.intern_type thy name
boehmes@33419
    40
    in
boehmes@33419
    41
      if Sign.declared_tyname thy intern
boehmes@33419
    42
      then
boehmes@33419
    43
        if Sign.arity_number thy intern = arity then SOME intern
boehmes@33419
    44
        else error ("Boogie: type already declared with different arity: " ^
boehmes@33419
    45
          quote name)
boehmes@33419
    46
      else NONE
boehmes@33419
    47
    end
boehmes@33419
    48
boehmes@33419
    49
  fun declare (name, arity) thy =
boehmes@33419
    50
    let val isa_name = isabelle_name name
boehmes@33419
    51
    in
boehmes@33419
    52
      (case lookup_type_name thy isa_name arity of
boehmes@33419
    53
        SOME type_name => ((type_name, false), thy)
boehmes@33419
    54
      | NONE =>
boehmes@33419
    55
          let
boehmes@33419
    56
            val bname = Binding.name isa_name
boehmes@33419
    57
            val args = Name.variant_list [] (replicate arity "'")
boehmes@33419
    58
            val (T, thy') =
boehmes@33419
    59
              ObjectLogic.typedecl (Binding.name isa_name, args, NoSyn) thy
boehmes@33419
    60
            val type_name = fst (Term.dest_Type T)
boehmes@33419
    61
          in ((type_name, true), thy') end)
boehmes@33419
    62
    end
boehmes@33419
    63
boehmes@33419
    64
  fun type_names ((name, _), (new_name, new)) =
boehmes@33419
    65
    if new then SOME (new_name ^ " (was " ^ name ^ ")") else NONE
boehmes@33419
    66
in
boehmes@33419
    67
fun declare_types verbose tys =
boehmes@33419
    68
  fold_map declare tys #-> (fn tds =>
boehmes@33419
    69
  log verbose "Declared types:" (map_filter type_names (tys ~~ tds)) #>
boehmes@33419
    70
  rpair (Symtab.make (map fst tys ~~ map fst tds)))
boehmes@33419
    71
end
boehmes@33419
    72
boehmes@33419
    73
boehmes@33419
    74
boehmes@33419
    75
local
boehmes@33419
    76
  val quote_mixfix =
boehmes@33419
    77
    Symbol.explode #> map
boehmes@33419
    78
      (fn "_" => "'_"
boehmes@33419
    79
        | "(" => "'("
boehmes@33419
    80
        | ")" => "')"
boehmes@33419
    81
        | "/" => "'/"
boehmes@33419
    82
        | s => s) #>
boehmes@33419
    83
    implode
boehmes@33419
    84
boehmes@33419
    85
  fun mk_syntax name i =
boehmes@33419
    86
    let
boehmes@33419
    87
      val syn = quote_mixfix name
boehmes@33419
    88
      val args = concat (separate ",/ " (replicate i "_"))
boehmes@33419
    89
    in
boehmes@33419
    90
      if i = 0 then Mixfix (syn, [], 1000)
boehmes@33419
    91
      else Mixfix (syn ^ "()'(/" ^ args ^ "')", replicate i 0, 1000)
boehmes@33419
    92
    end
boehmes@33419
    93
boehmes@33419
    94
  fun process_attributes T =
boehmes@33419
    95
    let
boehmes@33419
    96
      fun const name = SOME (Const (name, T))
boehmes@33419
    97
boehmes@33419
    98
      fun choose builtin =
boehmes@33419
    99
        (case builtin of
boehmes@33419
   100
          "bvnot" => const @{const_name z3_bvnot}
boehmes@33419
   101
        | "bvand" => const @{const_name z3_bvand}
boehmes@33419
   102
        | "bvor" => const @{const_name z3_bvor}
boehmes@33419
   103
        | "bvxor" => const @{const_name z3_bvxor}
boehmes@33419
   104
        | "bvadd" => const @{const_name z3_bvadd}
boehmes@33419
   105
        | "bvneg" => const @{const_name z3_bvneg}
boehmes@33419
   106
        | "bvsub" => const @{const_name z3_bvsub}
boehmes@33419
   107
        | "bvmul" => const @{const_name z3_bvmul}
boehmes@33419
   108
        | "bvudiv" => const @{const_name z3_bvudiv}
boehmes@33419
   109
        | "bvurem" => const @{const_name z3_bvurem}
boehmes@33419
   110
        | "bvsdiv" => const @{const_name z3_bvsdiv}
boehmes@33419
   111
        | "bvsrem" => const @{const_name z3_bvsrem}
boehmes@33419
   112
        | "bvshl" => const @{const_name z3_bvshl}
boehmes@33419
   113
        | "bvlshr" => const @{const_name z3_bvlshr}
boehmes@33419
   114
        | "bvashr" => const @{const_name z3_bvashr}
boehmes@33419
   115
        | "bvult" => const @{const_name z3_bvult}
boehmes@33419
   116
        | "bvule" => const @{const_name z3_bvule}
boehmes@33419
   117
        | "bvugt" => const @{const_name z3_bvugt}
boehmes@33419
   118
        | "bvuge" => const @{const_name z3_bvuge}
boehmes@33419
   119
        | "bvslt" => const @{const_name z3_bvslt}
boehmes@33419
   120
        | "bvsle" => const @{const_name z3_bvsle}
boehmes@33419
   121
        | "bvsgt" => const @{const_name z3_bvsgt}
boehmes@33419
   122
        | "bvsge" => const @{const_name z3_bvsge}
boehmes@33419
   123
        | "sign_extend" => const @{const_name z3_sign_extend}
boehmes@33419
   124
        | _ => NONE)
boehmes@33419
   125
boehmes@33419
   126
      fun is_builtin att =
boehmes@33419
   127
        (case att of
boehmes@33419
   128
          ("bvbuiltin", [StringValue builtin]) => choose builtin
boehmes@33419
   129
        | ("bvint", [StringValue "ITE"]) => const @{const_name If}
boehmes@33419
   130
        | _ => NONE)
boehmes@33419
   131
    in get_first is_builtin end
boehmes@33419
   132
boehmes@33419
   133
  fun lookup_const thy name T =
boehmes@33419
   134
    let
boehmes@33419
   135
      val intern = Sign.intern_const thy name
boehmes@33419
   136
      val is_type_instance = Type.typ_instance o Sign.tsig_of
boehmes@33419
   137
    in
boehmes@33419
   138
      if Sign.declared_const thy intern
boehmes@33419
   139
      then
boehmes@33419
   140
        if is_type_instance thy (T, Sign.the_const_type thy intern)
boehmes@33419
   141
        then SOME (Const (intern, T))
boehmes@33419
   142
        else error ("Boogie: function already declared with different type: " ^
boehmes@33419
   143
          quote name)
boehmes@33419
   144
      else NONE
boehmes@33419
   145
    end
boehmes@33419
   146
boehmes@33419
   147
  fun declare (name, ((Ts, T), atts)) thy =
boehmes@33419
   148
    let val isa_name = isabelle_name name and U = Ts ---> T
boehmes@33419
   149
    in
boehmes@33419
   150
      (case lookup_const thy isa_name U of
boehmes@33419
   151
        SOME t => (((name, t), false), thy)
boehmes@33419
   152
      | NONE => 
boehmes@33419
   153
          (case process_attributes U atts of
boehmes@33419
   154
            SOME t => (((name, t), false), thy)
boehmes@33419
   155
          | NONE =>
boehmes@33419
   156
              thy
boehmes@33419
   157
              |> Sign.declare_const ((Binding.name isa_name, U),
boehmes@33419
   158
                   mk_syntax name (length Ts))
boehmes@33419
   159
              |> apfst (rpair true o pair name)))
boehmes@33419
   160
    end
boehmes@33419
   161
boehmes@33419
   162
  fun const_names ((name, _), ((_, t), new)) =
boehmes@33419
   163
    if new then SOME (fst (Term.dest_Const t) ^ " (as " ^ name ^ ")") else NONE
boehmes@33419
   164
in
boehmes@33419
   165
fun declare_functions verbose fns =
boehmes@33419
   166
  fold_map declare fns #-> (fn fds =>
boehmes@33419
   167
  log verbose "Declared constants:" (map_filter const_names (fns ~~ fds)) #>
boehmes@33419
   168
  rpair (Symtab.make (map fst fds)))
boehmes@33419
   169
end
boehmes@33419
   170
boehmes@33419
   171
boehmes@33419
   172
boehmes@33419
   173
local
boehmes@33419
   174
  fun name_axioms axs =
boehmes@33419
   175
    let fun mk_name idx = "axiom_" ^ string_of_int (idx + 1)
boehmes@33419
   176
    in map_index (fn (idx, t) => (mk_name idx, HOLogic.mk_Trueprop t)) axs end
boehmes@33419
   177
boehmes@33419
   178
  fun only_new_boogie_axioms thy =
boehmes@33419
   179
    let val baxs = map Thm.prop_of (Boogie_Axioms.get (ProofContext.init thy))
boehmes@33419
   180
    in filter_out (member (op aconv) baxs o snd) end
boehmes@33419
   181
in
boehmes@33419
   182
fun add_axioms verbose axs thy =
boehmes@33419
   183
  let val axs' = only_new_boogie_axioms thy (name_axioms axs)
boehmes@33419
   184
  in
boehmes@33419
   185
    thy
boehmes@33419
   186
    |> PureThy.add_axioms (map (rpair [] o apfst Binding.name) axs')
boehmes@33419
   187
    |-> Context.theory_map o fold Boogie_Axioms.add_thm
boehmes@33419
   188
    |> log verbose "The following axioms were added:" (map fst axs')
boehmes@33419
   189
    |> Context.theory_map (fn context => fold Split_VC_SMT_Rules.add_thm
boehmes@33419
   190
         (Boogie_Axioms.get (Context.proof_of context)) context)
boehmes@33419
   191
  end
boehmes@33419
   192
end
boehmes@33419
   193
boehmes@33419
   194
boehmes@33419
   195
boehmes@33419
   196
fun add_vcs verbose vcs thy =
boehmes@33419
   197
  let
boehmes@33419
   198
    val reused = duplicates (op =) (map (fst o fst) vcs)
boehmes@33419
   199
    fun rename (n, i) = isabelle_name n ^
boehmes@33419
   200
      (if member (op =) reused n then "_" ^ string_of_int i else "")
boehmes@33419
   201
boehmes@33419
   202
    fun decorate (name, t) = (rename name, HOLogic.mk_Trueprop t)
boehmes@33419
   203
    val vcs' = map decorate vcs
boehmes@33419
   204
  in
boehmes@33419
   205
    thy
boehmes@33419
   206
    |> Boogie_VCs.set vcs'
boehmes@33419
   207
    |> log verbose "The following verification conditions were loaded:"
boehmes@33419
   208
         (map fst vcs')
boehmes@33419
   209
  end
boehmes@33419
   210
boehmes@33419
   211
boehmes@33419
   212
boehmes@33419
   213
local
boehmes@33419
   214
  fun mk_bitT i T =
boehmes@33419
   215
    if i = 0
boehmes@33419
   216
    then Type (@{type_name "Numeral_Type.bit0"}, [T])
boehmes@33419
   217
    else Type (@{type_name "Numeral_Type.bit1"}, [T])
boehmes@33419
   218
boehmes@33419
   219
  fun mk_binT size = 
boehmes@33419
   220
    if size = 0 then @{typ "Numeral_Type.num0"}
boehmes@33419
   221
    else if size = 1 then @{typ "Numeral_Type.num1"}
boehmes@33419
   222
    else let val (q, r) = Integer.div_mod size 2 in mk_bitT r (mk_binT q) end
boehmes@33419
   223
in
boehmes@33419
   224
fun mk_wordT size =
boehmes@33419
   225
  if size >= 0 then Type (@{type_name "word"}, [mk_binT size])
boehmes@33419
   226
  else raise TYPE ("mk_wordT: " ^ quote (string_of_int size), [], [])
boehmes@33419
   227
end
boehmes@33419
   228
boehmes@33419
   229
local
boehmes@33419
   230
  fun dest_binT T =
boehmes@33419
   231
    (case T of
boehmes@33419
   232
      Type (@{type_name "Numeral_Type.num0"}, _) => 0
boehmes@33419
   233
    | Type (@{type_name "Numeral_Type.num1"}, _) => 1
boehmes@33419
   234
    | Type (@{type_name "Numeral_Type.bit0"}, [T]) => 2 * dest_binT T
boehmes@33419
   235
    | Type (@{type_name "Numeral_Type.bit1"}, [T]) => 1 + 2 * dest_binT T
boehmes@33419
   236
    | _ => raise TYPE ("dest_binT", [T], []))
boehmes@33419
   237
in
boehmes@33419
   238
val dest_wordT = (fn
boehmes@33419
   239
    Type (@{type_name "word"}, [T]) => dest_binT T
boehmes@33419
   240
  | T => raise TYPE ("dest_wordT", [T], []))
boehmes@33419
   241
end
boehmes@33419
   242
boehmes@33419
   243
fun mk_arrayT (Ts, T) = Type (@{type_name "fun"}, [HOLogic.mk_tupleT Ts, T])
boehmes@33419
   244
boehmes@33419
   245
boehmes@33419
   246
boehmes@33419
   247
datatype token = Token of string | Newline | EOF
boehmes@33419
   248
boehmes@33419
   249
fun tokenize path =
boehmes@33419
   250
  let
boehmes@33419
   251
    fun blank c = (c = #" " orelse c = #"\t" orelse c = #"\n" orelse c = #"\r")
boehmes@33419
   252
    fun split line (i, tss) = (i + 1,
boehmes@33419
   253
      map (pair i) (map Token (String.tokens blank line) @ [Newline]) :: tss)
boehmes@33419
   254
  in apsnd (flat o rev) (File.fold_lines split path (1, [])) end
boehmes@33419
   255
boehmes@33419
   256
fun stopper i = Scan.stopper (K (i, EOF)) (fn (_, EOF) => true | _ => false)
boehmes@33419
   257
boehmes@33419
   258
fun scan_err msg [] = "Boogie (at end of input): " ^ msg
boehmes@33419
   259
  | scan_err msg ((i, _) :: _) = "Boogie (at line " ^ string_of_int i ^ "): " ^
boehmes@33419
   260
      msg
boehmes@33419
   261
boehmes@33419
   262
fun scan_fail msg = Scan.fail_with (scan_err msg)
boehmes@33419
   263
boehmes@33419
   264
fun finite scan path =
boehmes@33419
   265
  let val (i, ts) = tokenize path
boehmes@33419
   266
  in
boehmes@33419
   267
    (case Scan.error (Scan.finite (stopper i) scan) ts of
boehmes@33419
   268
      (x, []) => x
boehmes@33419
   269
    | (_, ts) => error (scan_err "unparsed input" ts))
boehmes@33419
   270
  end
boehmes@33419
   271
boehmes@33419
   272
fun read_int' s = (case read_int (explode s) of (i, []) => SOME i | _ => NONE)
boehmes@33419
   273
boehmes@33419
   274
fun $$$ s = Scan.one (fn (_, Token s') => s = s' | _ => false)
boehmes@33419
   275
val str = Scan.some (fn (_, Token s) => SOME s | _ => NONE)
boehmes@33419
   276
val num = Scan.some (fn (_, Token s) => read_int' s | _ => NONE)
boehmes@33419
   277
boehmes@33419
   278
fun scan_line key scan =
boehmes@33419
   279
  $$$ key |-- scan --| Scan.one (fn (_, Newline) => true | _ => false)
boehmes@33419
   280
fun scan_line' key = scan_line key (Scan.succeed ())
boehmes@33419
   281
boehmes@33419
   282
fun scan_count scan i =
boehmes@33419
   283
  if i > 0 then scan ::: scan_count scan (i - 1) else Scan.succeed []
boehmes@33419
   284
boehmes@33419
   285
fun scan_lookup kind tab key =
boehmes@33419
   286
  (case Symtab.lookup tab key of
boehmes@33419
   287
    SOME value => Scan.succeed value
boehmes@33419
   288
  | NONE => scan_fail ("undefined " ^ kind ^ ": " ^ quote key))
boehmes@33419
   289
boehmes@33419
   290
fun typ tds =
boehmes@33419
   291
  let
boehmes@33419
   292
    fun tp st =
boehmes@33419
   293
     (scan_line' "bool" >> K @{typ bool} ||
boehmes@33419
   294
      scan_line' "int" >> K @{typ int} ||
boehmes@33419
   295
      scan_line "bv" num >> mk_wordT ||
boehmes@33419
   296
      scan_line "type-con" (str -- num) :|-- (fn (name, arity) =>
boehmes@33419
   297
        scan_lookup "type constructor" tds name -- scan_count tp arity >>
boehmes@33419
   298
        Type) ||
boehmes@33419
   299
      scan_line "array" num :|-- (fn arity =>
boehmes@33419
   300
        scan_count tp (arity - 1) -- tp >> mk_arrayT) ||
boehmes@33419
   301
      scan_fail "illegal type") st
boehmes@33419
   302
  in tp end
boehmes@33419
   303
boehmes@33419
   304
local
boehmes@33419
   305
  fun mk_nary _ t [] = t
boehmes@33419
   306
    | mk_nary f _ (t :: ts) = fold f ts t
boehmes@33419
   307
boehmes@33419
   308
  fun mk_distinct T ts =
boehmes@33419
   309
    Const (@{const_name distinct}, HOLogic.listT T --> @{typ bool}) $ 
boehmes@33419
   310
      HOLogic.mk_list T ts
boehmes@33419
   311
boehmes@33419
   312
  fun quant name f = scan_line name (num -- num -- num) >> pair f
boehmes@33419
   313
  val quants =
boehmes@33419
   314
    quant "forall" HOLogic.all_const ||
boehmes@33419
   315
    quant "exists" HOLogic.exists_const ||
boehmes@33419
   316
    scan_fail "illegal quantifier kind"
boehmes@33419
   317
  fun mk_quant q (n, T) t = q T $ Term.absfree (isabelle_name n, T, t)
boehmes@33419
   318
boehmes@33419
   319
  val patternT = @{typ pattern}
boehmes@33419
   320
  fun mk_pattern _ [] = raise TERM ("mk_pattern", [])
boehmes@33419
   321
    | mk_pattern n [t] = Const (n, Term.fastype_of t --> patternT) $ t
boehmes@33419
   322
    | mk_pattern n (t :: ts) =
boehmes@33419
   323
        let val T = patternT --> Term.fastype_of t --> patternT
boehmes@33419
   324
        in Const (@{const_name andpat}, T) $ mk_pattern n ts $ t end
boehmes@33419
   325
  fun patt n c scan =
boehmes@33419
   326
    scan_line n num :|-- scan_count scan >> (mk_pattern c)
boehmes@33419
   327
  fun pattern scan =
boehmes@33419
   328
    patt "pat" @{const_name pat} scan ||
boehmes@33419
   329
    patt "nopat" @{const_name nopat} scan ||
boehmes@33419
   330
    scan_fail "illegal pattern kind"
boehmes@33419
   331
  fun mk_trigger [] t = t
boehmes@33419
   332
    | mk_trigger ps t = @{term trigger} $ HOLogic.mk_list patternT ps $ t
boehmes@33419
   333
boehmes@33419
   334
  val label_kind =
boehmes@33419
   335
    $$$ "pos" >> K @{term block_at} ||
boehmes@33419
   336
    $$$ "neg" >> K @{term assert_at} ||
boehmes@33419
   337
    scan_fail "illegal label kind"
boehmes@33419
   338
boehmes@33419
   339
  fun mk_select (m, k) =
boehmes@33419
   340
    let
boehmes@33419
   341
      val mT = Term.fastype_of m and kT = Term.fastype_of k
boehmes@33419
   342
      val vT = Term.range_type mT
boehmes@33419
   343
    in Const (@{const_name boogie_select}, mT --> kT --> vT) $ m $ k end
boehmes@33419
   344
boehmes@33419
   345
  fun mk_store ((m, k), v) =
boehmes@33419
   346
    let
boehmes@33419
   347
      val mT = Term.fastype_of m and kT = Term.fastype_of k
boehmes@33419
   348
      val vT = Term.fastype_of v
boehmes@33419
   349
    in
boehmes@33419
   350
      Const (@{const_name boogie_store}, mT --> kT --> vT --> mT) $ m $ k $ v
boehmes@33419
   351
    end
boehmes@33419
   352
  
boehmes@33419
   353
  fun mk_extract ((msb, lsb), t) =
boehmes@33419
   354
    let
boehmes@33419
   355
      val dT = Term.fastype_of t and rT = mk_wordT (msb - lsb)
boehmes@33419
   356
      val nT = @{typ nat}
boehmes@33419
   357
      val mk_nat_num = HOLogic.mk_number @{typ nat}
boehmes@33419
   358
      val m = HOLogic.mk_number @{typ nat}
boehmes@33419
   359
    in
boehmes@33419
   360
      Const (@{const_name boogie_bv_extract}, [nT, nT, dT] ---> rT) $
boehmes@33419
   361
        mk_nat_num msb $ mk_nat_num lsb $ t
boehmes@33419
   362
    end
boehmes@33419
   363
boehmes@33419
   364
  fun mk_concat (t1, t2) =
boehmes@33419
   365
    let
boehmes@33419
   366
      val T1 = Term.fastype_of t1 and T2 = Term.fastype_of t2
boehmes@33419
   367
      val U = mk_wordT (dest_wordT T1 + dest_wordT T2)
boehmes@33419
   368
    in Const (@{const_name boogie_bv_concat}, [T1, T2] ---> U) $ t1 $ t2 end
boehmes@33419
   369
in
boehmes@33419
   370
fun expr tds fds =
boehmes@33419
   371
  let
boehmes@33419
   372
    fun binop t (u1, u2) = t $ u1 $ u2
boehmes@33419
   373
    fun binexp s f = scan_line' s |-- exp -- exp >> f
boehmes@33419
   374
boehmes@33419
   375
    and exp st =
boehmes@33419
   376
     (scan_line' "true" >> K @{term True} ||
boehmes@33419
   377
      scan_line' "false" >> K @{term False} ||
boehmes@33419
   378
      scan_line' "not" |-- exp >> HOLogic.mk_not ||
boehmes@33419
   379
      scan_line "and" num :|-- scan_count exp >> 
boehmes@33419
   380
        mk_nary (curry HOLogic.mk_conj) @{term True} ||
boehmes@33419
   381
      scan_line "or" num :|-- scan_count exp >>
boehmes@33419
   382
        mk_nary (curry HOLogic.mk_disj) @{term False} ||
boehmes@33419
   383
      binexp "implies" (binop @{term "op -->"}) ||
boehmes@33419
   384
      scan_line "distinct" num :|-- scan_count exp >>
boehmes@33419
   385
        (fn [] => @{term True}
boehmes@33419
   386
          | ts as (t :: _) => mk_distinct (Term.fastype_of t) ts) ||
boehmes@33419
   387
      binexp "=" HOLogic.mk_eq ||
boehmes@33419
   388
      scan_line "var" str -- typ tds >> (Free o apfst isabelle_name) ||
boehmes@33419
   389
      scan_line "fun" (str -- num) :|-- (fn (name, arity) =>
boehmes@33419
   390
        scan_lookup "constant" fds name -- scan_count exp arity >>
boehmes@33419
   391
        Term.list_comb) ||
boehmes@33419
   392
      quants :|-- (fn (q, ((n, k), i)) =>
boehmes@33419
   393
        scan_count (scan_line "var" str -- typ tds) n --
boehmes@33419
   394
        scan_count (pattern exp) k --
boehmes@33419
   395
        scan_count (attribute tds fds) i --
boehmes@33419
   396
        exp >> (fn (((vs, ps), _), t) =>
boehmes@33419
   397
          fold_rev (mk_quant q) vs (mk_trigger ps t))) ||
boehmes@33419
   398
      scan_line "label" (label_kind -- num -- num) -- exp >>
boehmes@33419
   399
        (fn (((l, line), col), t) =>
boehmes@33419
   400
          if line = 0 orelse col = 0 then t
boehmes@33419
   401
          else l $ Free ("Line_" ^ string_of_int line ^ "_Column_" ^
boehmes@33419
   402
            string_of_int col, @{typ bool}) $ t) ||
boehmes@33419
   403
      scan_line "int-num" num >> HOLogic.mk_number @{typ int} ||
boehmes@33419
   404
      binexp "<" (binop @{term "op < :: int => _"}) ||
boehmes@33419
   405
      binexp "<=" (binop @{term "op <= :: int => _"}) ||
boehmes@33419
   406
      binexp ">" (binop @{term "op < :: int => _"} o swap) ||
boehmes@33419
   407
      binexp ">=" (binop @{term "op <= :: int => _"} o swap) ||
boehmes@33419
   408
      binexp "+" (binop @{term "op + :: int => _"}) ||
boehmes@33419
   409
      binexp "-" (binop @{term "op + :: int => _"}) ||
boehmes@33419
   410
      binexp "*" (binop @{term "op + :: int => _"}) ||
boehmes@33419
   411
      binexp "/" (binop @{term boogie_div}) ||
boehmes@33419
   412
      binexp "%" (binop @{term boogie_mod}) ||
boehmes@33419
   413
      scan_line "select" num :|-- (fn arity =>
boehmes@33419
   414
        exp -- (scan_count exp (arity - 1) >> HOLogic.mk_tuple) >>
boehmes@33419
   415
          mk_select) ||
boehmes@33419
   416
      scan_line "store" num :|-- (fn arity =>
boehmes@33419
   417
        exp -- (scan_count exp (arity - 2) >> HOLogic.mk_tuple) -- exp >> 
boehmes@33419
   418
          mk_store) ||
boehmes@33419
   419
      scan_line "bv-num" (num -- num) >> (fn (size, i) =>
boehmes@33419
   420
        HOLogic.mk_number (mk_wordT size) i) ||
boehmes@33419
   421
      scan_line "bv-extract" (num -- num) -- exp >> mk_extract ||
boehmes@33419
   422
      binexp "bv-concat" mk_concat ||
boehmes@33419
   423
      scan_fail "illegal expression") st
boehmes@33419
   424
  in exp end
boehmes@33419
   425
boehmes@33419
   426
and attribute tds fds =
boehmes@33419
   427
  let
boehmes@33419
   428
    val attr_val = 
boehmes@33419
   429
      scan_line' "expr-attr" |-- expr tds fds >> TermValue ||
boehmes@33419
   430
      scan_line "string-attr" (Scan.repeat1 str) >>
boehmes@33419
   431
        (StringValue o space_implode " ") ||
boehmes@33419
   432
      scan_fail "illegal attribute value"
boehmes@33419
   433
  in
boehmes@33419
   434
    scan_line "attribute" (str -- num) :|-- (fn (name, i) =>
boehmes@33419
   435
      scan_count attr_val i >> pair name) ||
boehmes@33419
   436
    scan_fail "illegal attribute"
boehmes@33419
   437
  end
boehmes@33419
   438
end
boehmes@33419
   439
boehmes@33419
   440
fun type_decls verbose = Scan.depend (fn thy => 
boehmes@33419
   441
  Scan.repeat (scan_line "type-decl" (str -- num -- num) :|-- (fn (ty, i) =>
boehmes@33419
   442
    scan_count (attribute Symtab.empty Symtab.empty) i >> K ty)) >>
boehmes@33419
   443
    (fn tys => declare_types verbose tys thy))
boehmes@33419
   444
boehmes@33419
   445
fun fun_decls verbose tds = Scan.depend (fn thy =>
boehmes@33419
   446
  Scan.repeat (scan_line "fun-decl" (str -- num -- num) :|--
boehmes@33419
   447
    (fn ((name, arity), i) =>
boehmes@33419
   448
      scan_count (typ tds) (arity - 1) -- typ tds --
boehmes@33419
   449
      scan_count (attribute tds Symtab.empty) i >> pair name)) >>
boehmes@33419
   450
    (fn fns => declare_functions verbose fns thy))
boehmes@33419
   451
boehmes@33419
   452
fun axioms verbose tds fds = Scan.depend (fn thy =>
boehmes@33419
   453
  Scan.repeat (scan_line "axiom" num :|-- (fn i =>
boehmes@33419
   454
    expr tds fds --| scan_count (attribute tds fds) i)) >>
boehmes@33419
   455
    (fn axs => (add_axioms verbose axs thy, ())))
boehmes@33419
   456
boehmes@33419
   457
fun var_decls tds fds = Scan.depend (fn thy =>
boehmes@33419
   458
  Scan.repeat (scan_line "var-decl" (str -- num) :|-- (fn (_, i) =>
boehmes@33419
   459
    typ tds -- scan_count (attribute tds fds) i >> K ())) >> K (thy, ()))
boehmes@33419
   460
boehmes@33419
   461
fun vcs verbose tds fds = Scan.depend (fn thy =>
boehmes@33419
   462
  Scan.repeat (scan_line "vc" (str -- num) -- 
boehmes@33419
   463
    (expr tds fds >> Sign.cert_term thy)) >>
boehmes@33419
   464
    (fn vcs => ((), add_vcs verbose vcs thy)))
boehmes@33419
   465
boehmes@33419
   466
fun parse verbose thy = Scan.pass thy
boehmes@33419
   467
 (type_decls verbose :|-- (fn tds =>
boehmes@33419
   468
  fun_decls verbose tds :|-- (fn fds =>
boehmes@33419
   469
  axioms verbose tds fds |--
boehmes@33419
   470
  var_decls tds fds |--
boehmes@33419
   471
  vcs verbose tds fds)))
boehmes@33419
   472
boehmes@33419
   473
fun load_b2i verbose path thy = finite (parse verbose thy) path
boehmes@33419
   474
boehmes@33419
   475
end