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