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