src/HOL/SPARK/Tools/spark_vcs.ML
author wenzelm
Mon Nov 19 20:23:47 2012 +0100 (2012-11-19 ago)
changeset 50126 3dec88149176
parent 48453 2421ff8c57a5
child 51717 9e7d1c139569
permissions -rw-r--r--
theorem status about oracles/futures is no longer printed by default;
renamed Proofterm/Thm.status_of to Proofterm/Thm.peek_status to emphasize its semantics;
berghofe@41561
     1
(*  Title:      HOL/SPARK/Tools/spark_vcs.ML
berghofe@41561
     2
    Author:     Stefan Berghofer
berghofe@41561
     3
    Copyright:  secunet Security Networks AG
berghofe@41561
     4
berghofe@41561
     5
Store for verification conditions generated by SPARK/Ada.
berghofe@41561
     6
*)
berghofe@41561
     7
berghofe@41561
     8
signature SPARK_VCS =
berghofe@41561
     9
sig
berghofe@42356
    10
  val set_vcs: Fdl_Parser.decls -> Fdl_Parser.rules ->
berghofe@42396
    11
    (string * string) * Fdl_Parser.vcs -> Path.T -> string -> theory -> theory
berghofe@41561
    12
  val add_proof_fun: (typ option -> 'a -> term) ->
berghofe@41561
    13
    string * ((string list * string) option * 'a) ->
berghofe@41561
    14
    theory -> theory
berghofe@46725
    15
  val add_type: string * (typ * (string * string) list) -> theory -> theory
berghofe@48167
    16
  val lookup_vc: theory -> bool -> string -> (Element.context_i list *
berghofe@41896
    17
    (string * thm list option * Element.context_i * Element.statement_i)) option
berghofe@48167
    18
  val get_vcs: theory -> bool ->
berghofe@41896
    19
    Element.context_i list * (binding * thm) list * (string *
berghofe@41896
    20
    (string * thm list option * Element.context_i * Element.statement_i)) list
berghofe@41896
    21
  val mark_proved: string -> thm list -> theory -> theory
berghofe@48167
    22
  val close: bool -> theory -> theory
berghofe@41561
    23
  val is_closed: theory -> bool
berghofe@41561
    24
end;
berghofe@41561
    25
berghofe@41561
    26
structure SPARK_VCs: SPARK_VCS =
berghofe@41561
    27
struct
berghofe@41561
    28
berghofe@41561
    29
open Fdl_Parser;
berghofe@41561
    30
berghofe@41561
    31
berghofe@42356
    32
(** theory data **)
berghofe@42356
    33
berghofe@42356
    34
fun err_unfinished () = error "An unfinished SPARK environment is still open."
berghofe@42356
    35
berghofe@42356
    36
val strip_number = pairself implode o take_suffix Fdl_Lexer.is_digit o raw_explode;
berghofe@42356
    37
berghofe@42356
    38
val name_ord = prod_ord string_ord (option_ord int_ord) o
berghofe@42356
    39
  pairself (strip_number ##> Int.fromString);
berghofe@42356
    40
berghofe@42356
    41
structure VCtab = Table(type key = string val ord = name_ord);
berghofe@42356
    42
berghofe@42356
    43
structure VCs = Theory_Data
berghofe@42356
    44
(
berghofe@42356
    45
  type T =
berghofe@42356
    46
    {pfuns: ((string list * string) option * term) Symtab.table,
berghofe@46725
    47
     type_map: (typ * (string * string) list) Symtab.table,
berghofe@42356
    48
     env:
berghofe@42356
    49
       {ctxt: Element.context_i list,
berghofe@42356
    50
        defs: (binding * thm) list,
berghofe@42356
    51
        types: fdl_type tab,
berghofe@42356
    52
        funs: (string list * string) tab,
berghofe@46567
    53
        pfuns: ((string list * string) option * term) Symtab.table,
berghofe@42356
    54
        ids: (term * string) Symtab.table * Name.context,
berghofe@42356
    55
        proving: bool,
berghofe@42356
    56
        vcs: (string * thm list option *
berghofe@42356
    57
          (string * expr) list * (string * expr) list) VCtab.table,
berghofe@42396
    58
        path: Path.T,
berghofe@42396
    59
        prefix: string} option}
berghofe@42356
    60
  val empty : T = {pfuns = Symtab.empty, type_map = Symtab.empty, env = NONE}
berghofe@42356
    61
  val extend = I
berghofe@42356
    62
  fun merge ({pfuns = pfuns1, type_map = type_map1, env = NONE},
berghofe@42356
    63
        {pfuns = pfuns2, type_map = type_map2, env = NONE}) =
berghofe@42356
    64
        {pfuns = Symtab.merge (eq_pair (op =) (op aconv)) (pfuns1, pfuns2),
berghofe@42356
    65
         type_map = Symtab.merge (op =) (type_map1, type_map2),
berghofe@42356
    66
         env = NONE}
berghofe@42356
    67
    | merge _ = err_unfinished ()
berghofe@42356
    68
)
berghofe@42356
    69
berghofe@42356
    70
berghofe@41561
    71
(** utilities **)
berghofe@41561
    72
berghofe@42356
    73
val to_lower = raw_explode #> map Symbol.to_ascii_lower #> implode;
berghofe@42356
    74
berghofe@42356
    75
val lcase_eq = (op =) o pairself (to_lower o Long_Name.base_name);
berghofe@42356
    76
berghofe@42396
    77
fun lookup_prfx "" tab s = Symtab.lookup tab s
berghofe@42396
    78
  | lookup_prfx prfx tab s = (case Symtab.lookup tab s of
berghofe@42396
    79
        NONE => Symtab.lookup tab (prfx ^ "__" ^ s)
berghofe@42396
    80
      | x => x);
berghofe@42396
    81
berghofe@42396
    82
fun strip_prfx s =
berghofe@42396
    83
  let
berghofe@42396
    84
    fun strip ys [] = ("", implode ys)
berghofe@42396
    85
      | strip ys ("_" :: "_" :: xs) = (implode (rev xs), implode ys)
berghofe@42396
    86
      | strip ys (x :: xs) = strip (x :: ys) xs
berghofe@42396
    87
  in strip [] (rev (raw_explode s)) end;
berghofe@42396
    88
berghofe@46725
    89
fun unprefix_pfun "" s = s
berghofe@46725
    90
  | unprefix_pfun prfx s =
berghofe@46725
    91
      let val (prfx', s') = strip_prfx s
berghofe@46725
    92
      in if prfx = prfx' then s' else s end;
berghofe@46725
    93
berghofe@41561
    94
fun mk_unop s t =
berghofe@41561
    95
  let val T = fastype_of t
berghofe@41561
    96
  in Const (s, T --> T) $ t end;
berghofe@41561
    97
berghofe@41561
    98
fun mk_times (t, u) =
berghofe@41561
    99
  let
berghofe@41561
   100
    val setT = fastype_of t;
berghofe@41561
   101
    val T = HOLogic.dest_setT setT;
berghofe@41561
   102
    val U = HOLogic.dest_setT (fastype_of u)
berghofe@41561
   103
  in
berghofe@41561
   104
    Const (@{const_name Sigma}, setT --> (T --> HOLogic.mk_setT U) -->
berghofe@41561
   105
      HOLogic.mk_setT (HOLogic.mk_prodT (T, U))) $ t $ Abs ("", T, u)
berghofe@41561
   106
  end;
berghofe@41561
   107
berghofe@42396
   108
fun get_type thy prfx ty =
berghofe@42356
   109
  let val {type_map, ...} = VCs.get thy
berghofe@42396
   110
  in lookup_prfx prfx type_map ty end;
berghofe@42356
   111
berghofe@46725
   112
fun mk_type' _ _ "integer" = (HOLogic.intT, [])
berghofe@46725
   113
  | mk_type' _ _ "boolean" = (HOLogic.boolT, [])
berghofe@46725
   114
  | mk_type' thy prfx ty =
berghofe@42396
   115
      (case get_type thy prfx ty of
berghofe@42356
   116
         NONE =>
berghofe@46725
   117
           (Syntax.check_typ (Proof_Context.init_global thy)
berghofe@46725
   118
              (Type (Sign.full_name thy (Binding.name ty), [])),
berghofe@46725
   119
            [])
berghofe@46725
   120
       | SOME p => p);
berghofe@46725
   121
berghofe@46725
   122
fun mk_type thy prfx ty = fst (mk_type' thy prfx ty);
berghofe@41561
   123
berghofe@41561
   124
val booleanN = "boolean";
berghofe@41561
   125
val integerN = "integer";
berghofe@41561
   126
berghofe@41561
   127
fun define_overloaded (def_name, eq) lthy =
berghofe@41561
   128
  let
berghofe@41561
   129
    val ((c, _), rhs) = eq |> Syntax.check_term lthy |>
berghofe@41561
   130
      Logic.dest_equals |>> dest_Free;
berghofe@41561
   131
    val ((_, (_, thm)), lthy') = Local_Theory.define
berghofe@41561
   132
      ((Binding.name c, NoSyn), ((Binding.name def_name, []), rhs)) lthy
wenzelm@42361
   133
    val ctxt_thy = Proof_Context.init_global (Proof_Context.theory_of lthy');
wenzelm@42361
   134
    val thm' = singleton (Proof_Context.export lthy' ctxt_thy) thm
berghofe@41561
   135
  in (thm', lthy') end;
berghofe@41561
   136
berghofe@41561
   137
fun strip_underscores s =
berghofe@41561
   138
  strip_underscores (unsuffix "_" s) handle Fail _ => s;
berghofe@41561
   139
berghofe@41561
   140
fun strip_tilde s =
berghofe@41561
   141
  unsuffix "~" s ^ "_init" handle Fail _ => s;
berghofe@41561
   142
berghofe@41561
   143
val mangle_name = strip_underscores #> strip_tilde;
berghofe@41561
   144
berghofe@42396
   145
fun mk_variables thy prfx xs ty (tab, ctxt) =
berghofe@41561
   146
  let
berghofe@42396
   147
    val T = mk_type thy prfx ty;
wenzelm@43326
   148
    val (ys, ctxt') = fold_map Name.variant (map mangle_name xs) ctxt;
berghofe@41561
   149
    val zs = map (Free o rpair T) ys;
berghofe@41561
   150
  in (zs, (fold (Symtab.update o apsnd (rpair ty)) (xs ~~ zs) tab, ctxt')) end;
berghofe@41561
   151
berghofe@42356
   152
fun get_record_info thy T = (case Record.dest_recTs T of
berghofe@42356
   153
    [(tyname, [@{typ unit}])] =>
berghofe@42356
   154
      Record.get_info thy (Long_Name.qualifier tyname)
berghofe@42356
   155
  | _ => NONE);
berghofe@42356
   156
berghofe@46725
   157
fun find_field [] fname fields =
berghofe@46725
   158
      find_first (curry lcase_eq fname o fst) fields
berghofe@46725
   159
  | find_field cmap fname fields =
berghofe@46725
   160
      (case AList.lookup (op =) cmap fname of
berghofe@46725
   161
         NONE => NONE
berghofe@46725
   162
       | SOME fname' => SOME (fname', the (AList.lookup (op =) fields fname')));
berghofe@42356
   163
berghofe@42356
   164
fun find_field' fname = get_first (fn (flds, fldty) =>
berghofe@42356
   165
  if member (op =) flds fname then SOME fldty else NONE);
berghofe@42356
   166
berghofe@42356
   167
fun assoc_ty_err thy T s msg =
berghofe@42356
   168
  error ("Type " ^ Syntax.string_of_typ_global thy T ^
berghofe@42356
   169
    " associated with SPARK type " ^ s ^ "\n" ^ msg);
berghofe@42356
   170
berghofe@41561
   171
berghofe@41561
   172
(** generate properties of enumeration types **)
berghofe@41561
   173
berghofe@42356
   174
fun add_enum_type tyname tyname' thy =
berghofe@41561
   175
  let
wenzelm@45906
   176
    val {case_name, ...} = the (Datatype.get_info thy tyname');
wenzelm@45906
   177
    val cs = map Const (the (Datatype.get_constrs thy tyname'));
berghofe@42356
   178
    val k = length cs;
berghofe@41561
   179
    val T = Type (tyname', []);
berghofe@41561
   180
    val p = Const (@{const_name pos}, T --> HOLogic.intT);
berghofe@41561
   181
    val v = Const (@{const_name val}, HOLogic.intT --> T);
berghofe@41561
   182
    val card = Const (@{const_name card},
berghofe@41561
   183
      HOLogic.mk_setT T --> HOLogic.natT) $ HOLogic.mk_UNIV T;
berghofe@41561
   184
berghofe@41561
   185
    fun mk_binrel_def s f = Logic.mk_equals
berghofe@41561
   186
      (Const (s, T --> T --> HOLogic.boolT),
berghofe@41561
   187
       Abs ("x", T, Abs ("y", T,
berghofe@41561
   188
         Const (s, HOLogic.intT --> HOLogic.intT --> HOLogic.boolT) $
berghofe@41561
   189
           (f $ Bound 1) $ (f $ Bound 0))));
berghofe@41561
   190
berghofe@41561
   191
    val (((def1, def2), def3), lthy) = thy |>
berghofe@41561
   192
berghofe@42416
   193
      Class.instantiation ([tyname'], [], @{sort spark_enum}) |>
berghofe@41561
   194
berghofe@41561
   195
      define_overloaded ("pos_" ^ tyname ^ "_def", Logic.mk_equals
berghofe@41561
   196
        (p,
berghofe@41561
   197
         list_comb (Const (case_name, replicate k HOLogic.intT @
berghofe@41561
   198
             [T] ---> HOLogic.intT),
berghofe@41561
   199
           map (HOLogic.mk_number HOLogic.intT) (0 upto k - 1)))) ||>>
berghofe@41561
   200
berghofe@41561
   201
      define_overloaded ("less_eq_" ^ tyname ^ "_def",
berghofe@41561
   202
        mk_binrel_def @{const_name less_eq} p) ||>>
berghofe@41561
   203
      define_overloaded ("less_" ^ tyname ^ "_def",
berghofe@41561
   204
        mk_binrel_def @{const_name less} p);
berghofe@41561
   205
berghofe@41561
   206
    val UNIV_eq = Goal.prove lthy [] []
berghofe@41561
   207
      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41561
   208
         (HOLogic.mk_UNIV T, HOLogic.mk_set T cs)))
berghofe@41561
   209
      (fn _ =>
berghofe@41561
   210
         rtac @{thm subset_antisym} 1 THEN
berghofe@41561
   211
         rtac @{thm subsetI} 1 THEN
wenzelm@45906
   212
         Datatype_Aux.exh_tac (K (#exhaust (Datatype.the_info
wenzelm@42361
   213
           (Proof_Context.theory_of lthy) tyname'))) 1 THEN
berghofe@41561
   214
         ALLGOALS (asm_full_simp_tac (simpset_of lthy)));
berghofe@41561
   215
berghofe@41561
   216
    val finite_UNIV = Goal.prove lthy [] []
berghofe@41561
   217
      (HOLogic.mk_Trueprop (Const (@{const_name finite},
berghofe@41561
   218
         HOLogic.mk_setT T --> HOLogic.boolT) $ HOLogic.mk_UNIV T))
berghofe@41561
   219
      (fn _ => simp_tac (simpset_of lthy addsimps [UNIV_eq]) 1);
berghofe@41561
   220
berghofe@41561
   221
    val card_UNIV = Goal.prove lthy [] []
berghofe@41561
   222
      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41561
   223
         (card, HOLogic.mk_number HOLogic.natT k)))
berghofe@41561
   224
      (fn _ => simp_tac (simpset_of lthy addsimps [UNIV_eq]) 1);
berghofe@41561
   225
berghofe@41561
   226
    val range_pos = Goal.prove lthy [] []
berghofe@41561
   227
      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41561
   228
         (Const (@{const_name image}, (T --> HOLogic.intT) -->
berghofe@41561
   229
            HOLogic.mk_setT T --> HOLogic.mk_setT HOLogic.intT) $
berghofe@41561
   230
              p $ HOLogic.mk_UNIV T,
berghofe@41561
   231
          Const (@{const_name atLeastLessThan}, HOLogic.intT -->
berghofe@41561
   232
            HOLogic.intT --> HOLogic.mk_setT HOLogic.intT) $
berghofe@41561
   233
              HOLogic.mk_number HOLogic.intT 0 $
berghofe@41561
   234
              (@{term int} $ card))))
berghofe@41561
   235
      (fn _ =>
berghofe@41561
   236
         simp_tac (simpset_of lthy addsimps [card_UNIV]) 1 THEN
berghofe@41561
   237
         simp_tac (simpset_of lthy addsimps [UNIV_eq, def1]) 1 THEN
berghofe@41561
   238
         rtac @{thm subset_antisym} 1 THEN
berghofe@41561
   239
         simp_tac (simpset_of lthy) 1 THEN
berghofe@41561
   240
         rtac @{thm subsetI} 1 THEN
berghofe@41561
   241
         asm_full_simp_tac (simpset_of lthy addsimps @{thms interval_expand}
berghofe@41561
   242
           delsimps @{thms atLeastLessThan_iff}) 1);
berghofe@41561
   243
berghofe@41561
   244
    val lthy' =
berghofe@41561
   245
      Class.prove_instantiation_instance (fn _ =>
berghofe@41561
   246
        Class.intro_classes_tac [] THEN
berghofe@41561
   247
        rtac finite_UNIV 1 THEN
berghofe@41561
   248
        rtac range_pos 1 THEN
berghofe@41561
   249
        simp_tac (HOL_basic_ss addsimps [def3]) 1 THEN
berghofe@41561
   250
        simp_tac (HOL_basic_ss addsimps [def2]) 1) lthy;
berghofe@41561
   251
berghofe@41561
   252
    val (pos_eqs, val_eqs) = split_list (map_index (fn (i, c) =>
berghofe@41561
   253
      let
berghofe@41561
   254
        val n = HOLogic.mk_number HOLogic.intT i;
berghofe@41561
   255
        val th = Goal.prove lthy' [] []
berghofe@41561
   256
          (HOLogic.mk_Trueprop (HOLogic.mk_eq (p $ c, n)))
berghofe@41561
   257
          (fn _ => simp_tac (simpset_of lthy' addsimps [def1]) 1);
berghofe@41561
   258
        val th' = Goal.prove lthy' [] []
berghofe@41561
   259
          (HOLogic.mk_Trueprop (HOLogic.mk_eq (v $ n, c)))
berghofe@41561
   260
          (fn _ =>
berghofe@41561
   261
             rtac (@{thm inj_pos} RS @{thm injD}) 1 THEN
berghofe@41561
   262
             simp_tac (simpset_of lthy' addsimps
berghofe@41561
   263
               [@{thm pos_val}, range_pos, card_UNIV, th]) 1)
berghofe@41561
   264
      in (th, th') end) cs);
berghofe@41561
   265
berghofe@41561
   266
    val first_el = Goal.prove lthy' [] []
berghofe@41561
   267
      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41561
   268
         (Const (@{const_name first_el}, T), hd cs)))
berghofe@41561
   269
      (fn _ => simp_tac (simpset_of lthy' addsimps
berghofe@41561
   270
         [@{thm first_el_def}, hd val_eqs]) 1);
berghofe@41561
   271
berghofe@41561
   272
    val last_el = Goal.prove lthy' [] []
berghofe@41561
   273
      (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41561
   274
         (Const (@{const_name last_el}, T), List.last cs)))
berghofe@41561
   275
      (fn _ => simp_tac (simpset_of lthy' addsimps
berghofe@41561
   276
         [@{thm last_el_def}, List.last val_eqs, card_UNIV]) 1);
berghofe@41561
   277
  in
berghofe@42356
   278
    lthy' |>
berghofe@42356
   279
    Local_Theory.note
wenzelm@45592
   280
      ((Binding.name (tyname ^ "_card"), @{attributes [simp]}), [card_UNIV]) ||>>
berghofe@42356
   281
    Local_Theory.note
wenzelm@45592
   282
      ((Binding.name (tyname ^ "_pos"), @{attributes [simp]}), pos_eqs) ||>>
berghofe@42356
   283
    Local_Theory.note
wenzelm@45592
   284
      ((Binding.name (tyname ^ "_val"), @{attributes [simp]}), val_eqs) ||>>
berghofe@42356
   285
    Local_Theory.note
wenzelm@45592
   286
      ((Binding.name (tyname ^ "_first_el"), @{attributes [simp]}), [first_el]) ||>>
berghofe@42356
   287
    Local_Theory.note
wenzelm@45592
   288
      ((Binding.name (tyname ^ "_last_el"), @{attributes [simp]}), [last_el]) |> snd |>
berghofe@42356
   289
    Local_Theory.exit_global
berghofe@41561
   290
  end;
berghofe@41561
   291
berghofe@41561
   292
berghofe@42396
   293
fun check_no_assoc thy prfx s = case get_type thy prfx s of
berghofe@42356
   294
    NONE => ()
berghofe@42356
   295
  | SOME _ => error ("Cannot associate a type with " ^ s ^
berghofe@42356
   296
      "\nsince it is no record or enumeration type");
berghofe@42356
   297
berghofe@42356
   298
fun check_enum [] [] = NONE 
berghofe@42356
   299
  | check_enum els [] = SOME ("has no element(s) " ^ commas els)
berghofe@42356
   300
  | check_enum [] cs = SOME ("has extra element(s) " ^
berghofe@42356
   301
      commas (map (Long_Name.base_name o fst) cs))
berghofe@42356
   302
  | check_enum (el :: els) ((cname, _) :: cs) =
berghofe@42356
   303
      if lcase_eq (el, cname) then check_enum els cs
berghofe@42356
   304
      else SOME ("either has no element " ^ el ^
berghofe@42356
   305
        " or it is at the wrong position");
berghofe@42356
   306
berghofe@46725
   307
fun invert_map [] = I
berghofe@46725
   308
  | invert_map cmap =
berghofe@46725
   309
      map (apfst (the o AList.lookup (op =) (map swap cmap)));
berghofe@46725
   310
 
berghofe@42396
   311
fun add_type_def prfx (s, Basic_Type ty) (ids, thy) =
berghofe@42396
   312
      (check_no_assoc thy prfx s;
berghofe@42356
   313
       (ids,
berghofe@42356
   314
        Typedecl.abbrev_global (Binding.name s, [], NoSyn)
berghofe@42396
   315
          (mk_type thy prfx ty) thy |> snd))
berghofe@41561
   316
berghofe@42396
   317
  | add_type_def prfx (s, Enum_Type els) ((tab, ctxt), thy) =
berghofe@42356
   318
      let
berghofe@42396
   319
        val (thy', tyname) = (case get_type thy prfx s of
berghofe@42356
   320
            NONE =>
berghofe@42356
   321
              let
berghofe@42356
   322
                val tyb = Binding.name s;
berghofe@42356
   323
                val tyname = Sign.full_name thy tyb
berghofe@42356
   324
              in
berghofe@42356
   325
                (thy |>
wenzelm@45701
   326
                 Datatype.add_datatype {strict = true, quiet = true}
berghofe@46725
   327
                   [((tyb, [], NoSyn),
berghofe@46725
   328
                     map (fn s => (Binding.name s, [], NoSyn)) els)] |> snd |>
berghofe@42356
   329
                 add_enum_type s tyname,
berghofe@42356
   330
                 tyname)
berghofe@42356
   331
              end
berghofe@46725
   332
          | SOME (T as Type (tyname, []), cmap) =>
wenzelm@45906
   333
              (case Datatype.get_constrs thy tyname of
berghofe@42356
   334
                 NONE => assoc_ty_err thy T s "is not a datatype"
berghofe@42396
   335
               | SOME cs =>
berghofe@46725
   336
                   let val (prfx', _) = strip_prfx s
berghofe@42396
   337
                   in
berghofe@46725
   338
                     case check_enum (map (unprefix_pfun prfx') els)
berghofe@46725
   339
                         (invert_map cmap cs) of
berghofe@42396
   340
                       NONE => (thy, tyname)
berghofe@42396
   341
                     | SOME msg => assoc_ty_err thy T s msg
berghofe@46567
   342
                   end)
berghofe@46725
   343
          | SOME (T, _) => assoc_ty_err thy T s "is not a datatype");
wenzelm@45906
   344
        val cs = map Const (the (Datatype.get_constrs thy' tyname));
berghofe@42356
   345
      in
berghofe@42356
   346
        ((fold (Symtab.update_new o apsnd (rpair s)) (els ~~ cs) tab,
berghofe@42356
   347
          fold Name.declare els ctxt),
berghofe@42356
   348
         thy')
berghofe@42356
   349
      end
berghofe@41561
   350
berghofe@42396
   351
  | add_type_def prfx (s, Array_Type (argtys, resty)) (ids, thy) =
berghofe@42396
   352
      (check_no_assoc thy prfx s;
berghofe@42356
   353
       (ids,
berghofe@42356
   354
        Typedecl.abbrev_global (Binding.name s, [], NoSyn)
berghofe@42396
   355
          (foldr1 HOLogic.mk_prodT (map (mk_type thy prfx) argtys) -->
berghofe@42396
   356
             mk_type thy prfx resty) thy |> snd))
berghofe@41561
   357
berghofe@42396
   358
  | add_type_def prfx (s, Record_Type fldtys) (ids, thy) =
berghofe@41561
   359
      (ids,
berghofe@42356
   360
       let val fldTs = maps (fn (flds, ty) =>
berghofe@42396
   361
         map (rpair (mk_type thy prfx ty)) flds) fldtys
berghofe@42396
   362
       in case get_type thy prfx s of
berghofe@42356
   363
           NONE =>
wenzelm@44653
   364
             Record.add_record ([], Binding.name s) NONE
berghofe@42356
   365
               (map (fn (fld, T) => (Binding.name fld, T, NoSyn)) fldTs) thy
berghofe@46725
   366
         | SOME (rT, cmap) =>
berghofe@42356
   367
             (case get_record_info thy rT of
berghofe@42356
   368
                NONE => assoc_ty_err thy rT s "is not a record type"
berghofe@42356
   369
              | SOME {fields, ...} =>
berghofe@46725
   370
                  let val fields' = invert_map cmap fields
berghofe@46725
   371
                  in
berghofe@46725
   372
                    (case subtract (lcase_eq o pairself fst) fldTs fields' of
berghofe@46725
   373
                       [] => ()
berghofe@46725
   374
                     | flds => assoc_ty_err thy rT s ("has extra field(s) " ^
berghofe@46725
   375
                         commas (map (Long_Name.base_name o fst) flds));
berghofe@46725
   376
                     map (fn (fld, T) =>
berghofe@46725
   377
                       case AList.lookup lcase_eq fields' fld of
berghofe@46725
   378
                         NONE => assoc_ty_err thy rT s ("has no field " ^ fld)
berghofe@46725
   379
                       | SOME U => T = U orelse assoc_ty_err thy rT s
berghofe@46725
   380
                           ("has field " ^
berghofe@46725
   381
                            fld ^ " whose type\n" ^
berghofe@46725
   382
                            Syntax.string_of_typ_global thy U ^
berghofe@46725
   383
                            "\ndoes not match declared type\n" ^
berghofe@46725
   384
                            Syntax.string_of_typ_global thy T)) fldTs;
berghofe@46725
   385
                     thy)
berghofe@46725
   386
                  end)
berghofe@42356
   387
       end)
berghofe@41561
   388
berghofe@42396
   389
  | add_type_def prfx (s, Pending_Type) (ids, thy) =
berghofe@44030
   390
      (ids,
berghofe@44030
   391
       case get_type thy prfx s of
berghofe@44030
   392
         SOME _ => thy
berghofe@44030
   393
       | NONE => Typedecl.typedecl_global
berghofe@44030
   394
           (Binding.name s, [], NoSyn) thy |> snd);
berghofe@41561
   395
berghofe@41561
   396
berghofe@42396
   397
fun term_of_expr thy prfx types pfuns =
berghofe@41561
   398
  let
berghofe@41561
   399
    fun tm_of vs (Funct ("->", [e, e'])) =
berghofe@41561
   400
          (HOLogic.mk_imp (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   401
berghofe@41561
   402
      | tm_of vs (Funct ("<->", [e, e'])) =
berghofe@41561
   403
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   404
berghofe@41561
   405
      | tm_of vs (Funct ("or", [e, e'])) =
berghofe@41561
   406
          (HOLogic.mk_disj (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   407
berghofe@41561
   408
      | tm_of vs (Funct ("and", [e, e'])) =
berghofe@41561
   409
          (HOLogic.mk_conj (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   410
berghofe@41561
   411
      | tm_of vs (Funct ("not", [e])) =
berghofe@41561
   412
          (HOLogic.mk_not (fst (tm_of vs e)), booleanN)
berghofe@41561
   413
berghofe@41561
   414
      | tm_of vs (Funct ("=", [e, e'])) =
berghofe@41561
   415
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   416
berghofe@41561
   417
      | tm_of vs (Funct ("<>", [e, e'])) = (HOLogic.mk_not
berghofe@41561
   418
          (HOLogic.mk_eq (fst (tm_of vs e), fst (tm_of vs e'))), booleanN)
berghofe@41561
   419
berghofe@41561
   420
      | tm_of vs (Funct ("<", [e, e'])) = (HOLogic.mk_binrel @{const_name less}
berghofe@41561
   421
          (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   422
berghofe@41561
   423
      | tm_of vs (Funct (">", [e, e'])) = (HOLogic.mk_binrel @{const_name less}
berghofe@41561
   424
          (fst (tm_of vs e'), fst (tm_of vs e)), booleanN)
berghofe@41561
   425
berghofe@41561
   426
      | tm_of vs (Funct ("<=", [e, e'])) = (HOLogic.mk_binrel @{const_name less_eq}
berghofe@41561
   427
          (fst (tm_of vs e), fst (tm_of vs e')), booleanN)
berghofe@41561
   428
berghofe@41561
   429
      | tm_of vs (Funct (">=", [e, e'])) = (HOLogic.mk_binrel @{const_name less_eq}
berghofe@41561
   430
          (fst (tm_of vs e'), fst (tm_of vs e)), booleanN)
berghofe@41561
   431
berghofe@41561
   432
      | tm_of vs (Funct ("+", [e, e'])) = (HOLogic.mk_binop @{const_name plus}
berghofe@41561
   433
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   434
berghofe@41561
   435
      | tm_of vs (Funct ("-", [e, e'])) = (HOLogic.mk_binop @{const_name minus}
berghofe@41561
   436
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   437
berghofe@41561
   438
      | tm_of vs (Funct ("*", [e, e'])) = (HOLogic.mk_binop @{const_name times}
berghofe@41561
   439
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   440
berghofe@41561
   441
      | tm_of vs (Funct ("/", [e, e'])) = (HOLogic.mk_binop @{const_name divide}
berghofe@41561
   442
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   443
berghofe@41561
   444
      | tm_of vs (Funct ("div", [e, e'])) = (HOLogic.mk_binop @{const_name sdiv}
berghofe@41561
   445
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   446
berghofe@41635
   447
      | tm_of vs (Funct ("mod", [e, e'])) = (HOLogic.mk_binop @{const_name mod}
berghofe@41561
   448
          (fst (tm_of vs e), fst (tm_of vs e')), integerN)
berghofe@41561
   449
berghofe@41561
   450
      | tm_of vs (Funct ("-", [e])) =
berghofe@41561
   451
          (mk_unop @{const_name uminus} (fst (tm_of vs e)), integerN)
berghofe@41561
   452
berghofe@41561
   453
      | tm_of vs (Funct ("**", [e, e'])) =
berghofe@41561
   454
          (Const (@{const_name power}, HOLogic.intT --> HOLogic.natT -->
berghofe@41561
   455
             HOLogic.intT) $ fst (tm_of vs e) $
berghofe@41561
   456
               (@{const nat} $ fst (tm_of vs e')), integerN)
berghofe@41561
   457
berghofe@41561
   458
      | tm_of (tab, _) (Ident s) =
berghofe@41561
   459
          (case Symtab.lookup tab s of
berghofe@41561
   460
             SOME t_ty => t_ty
berghofe@42499
   461
           | NONE => (case lookup_prfx prfx pfuns s of
berghofe@42499
   462
               SOME (SOME ([], resty), t) => (t, resty)
berghofe@42499
   463
             | _ => error ("Undeclared identifier " ^ s)))
berghofe@41561
   464
berghofe@41561
   465
      | tm_of _ (Number i) = (HOLogic.mk_number HOLogic.intT i, integerN)
berghofe@41561
   466
berghofe@41561
   467
      | tm_of vs (Quantifier (s, xs, ty, e)) =
berghofe@41561
   468
          let
berghofe@42396
   469
            val (ys, vs') = mk_variables thy prfx xs ty vs;
berghofe@41561
   470
            val q = (case s of
berghofe@41561
   471
                "for_all" => HOLogic.mk_all
berghofe@41561
   472
              | "for_some" => HOLogic.mk_exists)
berghofe@41561
   473
          in
berghofe@41561
   474
            (fold_rev (fn Free (x, T) => fn t => q (x, T, t))
berghofe@41561
   475
               ys (fst (tm_of vs' e)),
berghofe@41561
   476
             booleanN)
berghofe@41561
   477
          end
berghofe@41561
   478
berghofe@41561
   479
      | tm_of vs (Funct (s, es)) =
berghofe@41561
   480
berghofe@41561
   481
          (* record field selection *)
berghofe@41561
   482
          (case try (unprefix "fld_") s of
berghofe@41561
   483
             SOME fname => (case es of
berghofe@41561
   484
               [e] =>
berghofe@42356
   485
                 let
berghofe@42356
   486
                   val (t, rcdty) = tm_of vs e;
berghofe@46725
   487
                   val (rT, cmap) = mk_type' thy prfx rcdty
berghofe@42356
   488
                 in case (get_record_info thy rT, lookup types rcdty) of
berghofe@42356
   489
                     (SOME {fields, ...}, SOME (Record_Type fldtys)) =>
berghofe@46725
   490
                       (case (find_field cmap fname fields,
berghofe@42356
   491
                            find_field' fname fldtys) of
berghofe@42356
   492
                          (SOME (fname', fT), SOME fldty) =>
berghofe@42356
   493
                            (Const (fname', rT --> fT) $ t, fldty)
berghofe@42356
   494
                        | _ => error ("Record " ^ rcdty ^
berghofe@41561
   495
                            " has no field named " ^ fname))
berghofe@41561
   496
                   | _ => error (rcdty ^ " is not a record type")
berghofe@41561
   497
                 end
berghofe@41561
   498
             | _ => error ("Function " ^ s ^ " expects one argument"))
berghofe@41561
   499
           | NONE =>
berghofe@41561
   500
berghofe@41561
   501
          (* record field update *)
berghofe@41561
   502
          (case try (unprefix "upf_") s of
berghofe@41561
   503
             SOME fname => (case es of
berghofe@41561
   504
               [e, e'] =>
berghofe@41561
   505
                 let
berghofe@41561
   506
                   val (t, rcdty) = tm_of vs e;
berghofe@46725
   507
                   val (rT, cmap) = mk_type' thy prfx rcdty;
berghofe@41561
   508
                   val (u, fldty) = tm_of vs e';
berghofe@42396
   509
                   val fT = mk_type thy prfx fldty
berghofe@42356
   510
                 in case get_record_info thy rT of
berghofe@42356
   511
                     SOME {fields, ...} =>
berghofe@46725
   512
                       (case find_field cmap fname fields of
berghofe@42356
   513
                          SOME (fname', fU) =>
berghofe@42356
   514
                            if fT = fU then
berghofe@42356
   515
                              (Const (fname' ^ "_update",
berghofe@41561
   516
                                 (fT --> fT) --> rT --> rT) $
berghofe@41561
   517
                                   Abs ("x", fT, u) $ t,
berghofe@41561
   518
                               rcdty)
berghofe@42356
   519
                            else error ("Type\n" ^
berghofe@42356
   520
                              Syntax.string_of_typ_global thy fT ^
berghofe@42356
   521
                              "\ndoes not match type\n" ^
berghofe@42356
   522
                              Syntax.string_of_typ_global thy fU ^
berghofe@42356
   523
                              "\nof field " ^ fname)
berghofe@41561
   524
                        | NONE => error ("Record " ^ rcdty ^
berghofe@41561
   525
                            " has no field named " ^ fname))
berghofe@41561
   526
                   | _ => error (rcdty ^ " is not a record type")
berghofe@41561
   527
                 end
berghofe@41561
   528
             | _ => error ("Function " ^ s ^ " expects two arguments"))
berghofe@41561
   529
           | NONE =>
berghofe@41561
   530
berghofe@41561
   531
          (* enumeration type to integer *)
berghofe@41561
   532
          (case try (unsuffix "__pos") s of
berghofe@41561
   533
             SOME tyname => (case es of
berghofe@41561
   534
               [e] => (Const (@{const_name pos},
berghofe@42396
   535
                   mk_type thy prfx tyname --> HOLogic.intT) $ fst (tm_of vs e),
berghofe@42396
   536
                 integerN)
berghofe@41561
   537
             | _ => error ("Function " ^ s ^ " expects one argument"))
berghofe@41561
   538
           | NONE =>
berghofe@41561
   539
berghofe@41561
   540
          (* integer to enumeration type *)
berghofe@41561
   541
          (case try (unsuffix "__val") s of
berghofe@41561
   542
             SOME tyname => (case es of
berghofe@41561
   543
               [e] => (Const (@{const_name val},
berghofe@42396
   544
                   HOLogic.intT --> mk_type thy prfx tyname) $ fst (tm_of vs e),
berghofe@42396
   545
                 tyname)
berghofe@41561
   546
             | _ => error ("Function " ^ s ^ " expects one argument"))
berghofe@41561
   547
           | NONE =>
berghofe@41561
   548
berghofe@41561
   549
          (* successor / predecessor of enumeration type element *)
berghofe@41561
   550
          if s = "succ" orelse s = "pred" then (case es of
berghofe@41561
   551
              [e] =>
berghofe@41561
   552
                let
berghofe@41561
   553
                  val (t, tyname) = tm_of vs e;
berghofe@42396
   554
                  val T = mk_type thy prfx tyname
berghofe@41561
   555
                in (Const
berghofe@41561
   556
                  (if s = "succ" then @{const_name succ}
berghofe@41561
   557
                   else @{const_name pred}, T --> T) $ t, tyname)
berghofe@41561
   558
                end
berghofe@41561
   559
            | _ => error ("Function " ^ s ^ " expects one argument"))
berghofe@41561
   560
berghofe@41561
   561
          (* user-defined proof function *)
berghofe@41561
   562
          else
berghofe@42396
   563
            (case lookup_prfx prfx pfuns s of
berghofe@41561
   564
               SOME (SOME (_, resty), t) =>
berghofe@41561
   565
                 (list_comb (t, map (fst o tm_of vs) es), resty)
berghofe@41561
   566
             | _ => error ("Undeclared proof function " ^ s))))))
berghofe@41561
   567
berghofe@41561
   568
      | tm_of vs (Element (e, es)) =
berghofe@41561
   569
          let val (t, ty) = tm_of vs e
berghofe@41561
   570
          in case lookup types ty of
berghofe@41561
   571
              SOME (Array_Type (_, elty)) =>
berghofe@41561
   572
                (t $ foldr1 HOLogic.mk_prod (map (fst o tm_of vs) es), elty)
berghofe@41561
   573
            | _ => error (ty ^ " is not an array type")
berghofe@41561
   574
          end
berghofe@41561
   575
berghofe@41561
   576
      | tm_of vs (Update (e, es, e')) =
berghofe@41561
   577
          let val (t, ty) = tm_of vs e
berghofe@41561
   578
          in case lookup types ty of
berghofe@41561
   579
              SOME (Array_Type (idxtys, elty)) =>
berghofe@41561
   580
                let
berghofe@42396
   581
                  val T = foldr1 HOLogic.mk_prodT
berghofe@42396
   582
                    (map (mk_type thy prfx) idxtys);
berghofe@42396
   583
                  val U = mk_type thy prfx elty;
berghofe@41561
   584
                  val fT = T --> U
berghofe@41561
   585
                in
berghofe@41561
   586
                  (Const (@{const_name fun_upd}, fT --> T --> U --> fT) $
berghofe@41561
   587
                     t $ foldr1 HOLogic.mk_prod (map (fst o tm_of vs) es) $
berghofe@41561
   588
                       fst (tm_of vs e'),
berghofe@41561
   589
                   ty)
berghofe@41561
   590
                end
berghofe@41561
   591
            | _ => error (ty ^ " is not an array type")
berghofe@41561
   592
          end
berghofe@41561
   593
berghofe@41561
   594
      | tm_of vs (Record (s, flds)) =
berghofe@42356
   595
          let
berghofe@46725
   596
            val (T, cmap) = mk_type' thy prfx s;
berghofe@42356
   597
            val {extension = (ext_name, _), fields, ...} =
berghofe@42356
   598
              (case get_record_info thy T of
berghofe@42356
   599
                 NONE => error (s ^ " is not a record type")
berghofe@42356
   600
               | SOME info => info);
berghofe@42356
   601
            val flds' = map (apsnd (tm_of vs)) flds;
berghofe@46725
   602
            val fnames = fields |> invert_map cmap |>
berghofe@46725
   603
              map (Long_Name.base_name o fst);
berghofe@42356
   604
            val fnames' = map fst flds;
berghofe@42356
   605
            val (fvals, ftys) = split_list (map (fn s' =>
berghofe@42356
   606
              case AList.lookup lcase_eq flds' s' of
berghofe@42356
   607
                SOME fval_ty => fval_ty
berghofe@42356
   608
              | NONE => error ("Field " ^ s' ^ " missing in record " ^ s))
berghofe@42356
   609
                  fnames);
berghofe@42356
   610
            val _ = (case subtract lcase_eq fnames fnames' of
berghofe@42356
   611
                [] => ()
berghofe@42356
   612
              | xs => error ("Extra field(s) " ^ commas xs ^
berghofe@42356
   613
                  " in record " ^ s));
berghofe@42356
   614
            val _ = (case duplicates (op =) fnames' of
berghofe@42356
   615
                [] => ()
berghofe@42356
   616
              | xs => error ("Duplicate field(s) " ^ commas xs ^
berghofe@42356
   617
                  " in record " ^ s))
berghofe@42356
   618
          in
berghofe@42356
   619
            (list_comb
berghofe@42356
   620
               (Const (ext_name,
berghofe@42396
   621
                  map (mk_type thy prfx) ftys @ [HOLogic.unitT] ---> T),
berghofe@42356
   622
                fvals @ [HOLogic.unit]),
berghofe@42356
   623
             s)
berghofe@42356
   624
          end
berghofe@41561
   625
berghofe@41561
   626
      | tm_of vs (Array (s, default, assocs)) =
berghofe@41561
   627
          (case lookup types s of
berghofe@41561
   628
             SOME (Array_Type (idxtys, elty)) =>
berghofe@41561
   629
               let
berghofe@42396
   630
                 val Ts = map (mk_type thy prfx) idxtys;
berghofe@41561
   631
                 val T = foldr1 HOLogic.mk_prodT Ts;
berghofe@42396
   632
                 val U = mk_type thy prfx elty;
berghofe@41561
   633
                 fun mk_idx' T (e, NONE) = HOLogic.mk_set T [fst (tm_of vs e)]
berghofe@41561
   634
                   | mk_idx' T (e, SOME e') = Const (@{const_name atLeastAtMost},
berghofe@41561
   635
                       T --> T --> HOLogic.mk_setT T) $
berghofe@41561
   636
                         fst (tm_of vs e) $ fst (tm_of vs e');
berghofe@41561
   637
                 fun mk_idx idx =
berghofe@41561
   638
                   if length Ts <> length idx then
berghofe@41561
   639
                     error ("Arity mismatch in construction of array " ^ s)
berghofe@41561
   640
                   else foldr1 mk_times (map2 mk_idx' Ts idx);
berghofe@41561
   641
                 fun mk_upd (idxs, e) t =
berghofe@41561
   642
                   if length idxs = 1 andalso forall (is_none o snd) (hd idxs)
berghofe@41561
   643
                   then
berghofe@41561
   644
                     Const (@{const_name fun_upd}, (T --> U) -->
berghofe@41561
   645
                         T --> U --> T --> U) $ t $
berghofe@41561
   646
                       foldl1 HOLogic.mk_prod
berghofe@41561
   647
                         (map (fst o tm_of vs o fst) (hd idxs)) $
berghofe@41561
   648
                       fst (tm_of vs e)
berghofe@41561
   649
                   else
berghofe@41561
   650
                     Const (@{const_name fun_upds}, (T --> U) -->
berghofe@41561
   651
                         HOLogic.mk_setT T --> U --> T --> U) $ t $
berghofe@41561
   652
                       foldl1 (HOLogic.mk_binop @{const_name sup})
berghofe@41561
   653
                         (map mk_idx idxs) $
berghofe@41561
   654
                       fst (tm_of vs e)
berghofe@41561
   655
               in
berghofe@41561
   656
                 (fold mk_upd assocs
berghofe@41561
   657
                    (case default of
berghofe@41561
   658
                       SOME e => Abs ("x", T, fst (tm_of vs e))
berghofe@41561
   659
                     | NONE => Const (@{const_name undefined}, T --> U)),
berghofe@41561
   660
                  s)
berghofe@41561
   661
               end
berghofe@41561
   662
           | _ => error (s ^ " is not an array type"))
berghofe@41561
   663
berghofe@41561
   664
  in tm_of end;
berghofe@41561
   665
berghofe@41561
   666
berghofe@42396
   667
fun term_of_rule thy prfx types pfuns ids rule =
berghofe@42396
   668
  let val tm_of = fst o term_of_expr thy prfx types pfuns ids
berghofe@41561
   669
  in case rule of
berghofe@41561
   670
      Inference_Rule (es, e) => Logic.list_implies
berghofe@41561
   671
        (map (HOLogic.mk_Trueprop o tm_of) es, HOLogic.mk_Trueprop (tm_of e))
berghofe@41561
   672
    | Substitution_Rule (es, e, e') => Logic.list_implies
berghofe@41561
   673
        (map (HOLogic.mk_Trueprop o tm_of) es,
berghofe@41561
   674
         HOLogic.mk_Trueprop (HOLogic.mk_eq (tm_of e, tm_of e')))
berghofe@41561
   675
  end;
berghofe@41561
   676
berghofe@41561
   677
berghofe@41561
   678
val builtin = Symtab.make (map (rpair ())
berghofe@41561
   679
  ["->", "<->", "or", "and", "not", "=", "<>", "<", ">", "<=", ">=",
berghofe@41561
   680
   "+", "-", "*", "/", "div", "mod", "**"]);
berghofe@41561
   681
berghofe@41561
   682
fun complex_expr (Number _) = false
berghofe@41561
   683
  | complex_expr (Ident _) = false 
berghofe@41561
   684
  | complex_expr (Funct (s, es)) =
berghofe@41561
   685
      not (Symtab.defined builtin s) orelse exists complex_expr es
berghofe@41561
   686
  | complex_expr (Quantifier (_, _, _, e)) = complex_expr e
berghofe@41561
   687
  | complex_expr _ = true;
berghofe@41561
   688
berghofe@41561
   689
fun complex_rule (Inference_Rule (es, e)) =
berghofe@41561
   690
      complex_expr e orelse exists complex_expr es
berghofe@41561
   691
  | complex_rule (Substitution_Rule (es, e, e')) =
berghofe@41561
   692
      complex_expr e orelse complex_expr e' orelse
berghofe@41561
   693
      exists complex_expr es;
berghofe@41561
   694
berghofe@41561
   695
val is_pfun =
berghofe@41561
   696
  Symtab.defined builtin orf
berghofe@41561
   697
  can (unprefix "fld_") orf can (unprefix "upf_") orf
berghofe@41561
   698
  can (unsuffix "__pos") orf can (unsuffix "__val") orf
berghofe@41561
   699
  equal "succ" orf equal "pred";
berghofe@41561
   700
berghofe@41561
   701
fun fold_opt f = the_default I o Option.map f;
berghofe@41561
   702
fun fold_pair f g (x, y) = f x #> g y;
berghofe@41561
   703
berghofe@41561
   704
fun fold_expr f g (Funct (s, es)) = f s #> fold (fold_expr f g) es
berghofe@41561
   705
  | fold_expr f g (Ident s) = g s
berghofe@41561
   706
  | fold_expr f g (Number _) = I
berghofe@41561
   707
  | fold_expr f g (Quantifier (_, _, _, e)) = fold_expr f g e
berghofe@41561
   708
  | fold_expr f g (Element (e, es)) =
berghofe@41561
   709
      fold_expr f g e #> fold (fold_expr f g) es
berghofe@41561
   710
  | fold_expr f g (Update (e, es, e')) =
berghofe@41561
   711
      fold_expr f g e #> fold (fold_expr f g) es #> fold_expr f g e'
berghofe@41561
   712
  | fold_expr f g (Record (_, flds)) = fold (fold_expr f g o snd) flds
berghofe@41561
   713
  | fold_expr f g (Array (_, default, assocs)) =
berghofe@41561
   714
      fold_opt (fold_expr f g) default #>
berghofe@41561
   715
      fold (fold_pair
berghofe@41561
   716
        (fold (fold (fold_pair
berghofe@41561
   717
          (fold_expr f g) (fold_opt (fold_expr f g)))))
berghofe@41561
   718
        (fold_expr f g)) assocs;
berghofe@41561
   719
berghofe@42499
   720
fun add_expr_pfuns funs = fold_expr
berghofe@42499
   721
  (fn s => if is_pfun s then I else insert (op =) s)
berghofe@42499
   722
  (fn s => if is_none (lookup funs s) then I else insert (op =) s);
berghofe@41561
   723
berghofe@41561
   724
val add_expr_idents = fold_expr (K I) (insert (op =));
berghofe@41561
   725
berghofe@42396
   726
fun pfun_type thy prfx (argtys, resty) =
berghofe@42396
   727
  map (mk_type thy prfx) argtys ---> mk_type thy prfx resty;
berghofe@41561
   728
berghofe@42396
   729
fun check_pfun_type thy prfx s t optty1 optty2 =
berghofe@41561
   730
  let
berghofe@41561
   731
    val T = fastype_of t;
berghofe@41561
   732
    fun check ty =
berghofe@42396
   733
      let val U = pfun_type thy prfx ty
berghofe@41561
   734
      in
berghofe@41561
   735
        T = U orelse
berghofe@41561
   736
        error ("Type\n" ^
berghofe@41561
   737
          Syntax.string_of_typ_global thy T ^
berghofe@41561
   738
          "\nof function " ^
berghofe@41561
   739
          Syntax.string_of_term_global thy t ^
berghofe@41561
   740
          " associated with proof function " ^ s ^
berghofe@41561
   741
          "\ndoes not match declared type\n" ^
berghofe@41561
   742
          Syntax.string_of_typ_global thy U)
berghofe@41561
   743
      end
berghofe@41561
   744
  in (Option.map check optty1; Option.map check optty2; ()) end;
berghofe@41561
   745
berghofe@41561
   746
fun upd_option x y = if is_some x then x else y;
berghofe@41561
   747
berghofe@42396
   748
fun check_pfuns_types thy prfx funs =
berghofe@41561
   749
  Symtab.map (fn s => fn (optty, t) =>
berghofe@46567
   750
   let val optty' = lookup funs (unprefix_pfun prfx s)
berghofe@41561
   751
   in
berghofe@42396
   752
     (check_pfun_type thy prfx s t optty optty';
berghofe@41561
   753
      (NONE |> upd_option optty |> upd_option optty', t))
berghofe@41561
   754
   end);
berghofe@41561
   755
berghofe@41561
   756
berghofe@41561
   757
(** the VC store **)
berghofe@41561
   758
berghofe@48167
   759
fun pp_vcs msg vcs = Pretty.big_list msg (map (Pretty.str o fst) vcs);
berghofe@48167
   760
berghofe@48167
   761
fun pp_open_vcs [] = Pretty.str "All verification conditions have been proved."
berghofe@48167
   762
  | pp_open_vcs vcs = pp_vcs
berghofe@48167
   763
      "The following verification conditions remain to be proved:" vcs;
berghofe@48167
   764
berghofe@48167
   765
fun partition_vcs vcs = VCtab.fold_rev
berghofe@48167
   766
  (fn (name, (trace, SOME thms, ps, cs)) =>
berghofe@48167
   767
        apfst (cons (name, (trace, thms, ps, cs)))
berghofe@48167
   768
    | (name, (trace, NONE, ps, cs)) =>
berghofe@48167
   769
        apsnd (cons (name, (trace, ps, cs))))
berghofe@48167
   770
  vcs ([], []);
berghofe@48167
   771
berghofe@48167
   772
fun insert_break prt = Pretty.blk (0, [Pretty.fbrk, prt]);
berghofe@48167
   773
berghofe@48167
   774
fun print_open_vcs f vcs =
berghofe@48167
   775
  (Pretty.writeln (f (pp_open_vcs (snd (partition_vcs vcs)))); vcs);
berghofe@41561
   776
berghofe@46567
   777
fun set_env ctxt defs types funs ids vcs path prefix thy = VCs.map (fn
berghofe@42356
   778
    {pfuns, type_map, env = NONE} =>
berghofe@46567
   779
      {pfuns = pfuns,
berghofe@42356
   780
       type_map = type_map,
berghofe@46567
   781
       env = SOME
berghofe@46567
   782
         {ctxt = ctxt, defs = defs, types = types, funs = funs,
berghofe@46567
   783
          pfuns = check_pfuns_types thy prefix funs pfuns,
berghofe@48167
   784
          ids = ids, proving = false, vcs = print_open_vcs I vcs, path = path,
berghofe@46567
   785
          prefix = prefix}}
berghofe@41561
   786
  | _ => err_unfinished ()) thy;
berghofe@41561
   787
berghofe@41561
   788
fun mk_pat s = (case Int.fromString s of
berghofe@41561
   789
    SOME i => [HOLogic.mk_Trueprop (Var (("C", i), HOLogic.boolT))]
berghofe@41561
   790
  | NONE => error ("Bad conclusion identifier: C" ^ s));
berghofe@41561
   791
berghofe@48167
   792
fun mk_vc thy prfx types pfuns ids name_concl (tr, proved, ps, cs) =
berghofe@41561
   793
  let val prop_of =
berghofe@42396
   794
    HOLogic.mk_Trueprop o fst o term_of_expr thy prfx types pfuns ids
berghofe@41561
   795
  in
berghofe@41561
   796
    (tr, proved,
berghofe@41561
   797
     Element.Assumes (map (fn (s', e) =>
berghofe@41561
   798
       ((Binding.name ("H" ^ s'), []), [(prop_of e, [])])) ps),
berghofe@41561
   799
     Element.Shows (map (fn (s', e) =>
berghofe@48167
   800
       (if name_concl then (Binding.name ("C" ^ s'), [])
berghofe@48167
   801
        else Attrib.empty_binding,
berghofe@48167
   802
        [(prop_of e, mk_pat s')])) cs))
berghofe@41561
   803
  end;
berghofe@41561
   804
berghofe@41561
   805
fun fold_vcs f vcs =
berghofe@41561
   806
  VCtab.fold (fn (_, (_, _, ps, cs)) => fold f ps #> fold f cs) vcs;
berghofe@41561
   807
berghofe@42499
   808
fun pfuns_of_vcs prfx funs pfuns vcs =
berghofe@42499
   809
  fold_vcs (add_expr_pfuns funs o snd) vcs [] |>
berghofe@42396
   810
  filter (is_none o lookup_prfx prfx pfuns);
berghofe@41561
   811
berghofe@42396
   812
fun declare_missing_pfuns thy prfx funs pfuns vcs (tab, ctxt) =
berghofe@41561
   813
  let
berghofe@41561
   814
    val (fs, (tys, Ts)) =
berghofe@42499
   815
      pfuns_of_vcs prfx funs pfuns vcs |>
berghofe@41561
   816
      map_filter (fn s => lookup funs s |>
berghofe@42396
   817
        Option.map (fn ty => (s, (SOME ty, pfun_type thy prfx ty)))) |>
berghofe@41561
   818
      split_list ||> split_list;
wenzelm@43326
   819
    val (fs', ctxt') = fold_map Name.variant fs ctxt
berghofe@41561
   820
  in
berghofe@41561
   821
    (fold Symtab.update_new (fs ~~ (tys ~~ map Free (fs' ~~ Ts))) pfuns,
berghofe@41561
   822
     Element.Fixes (map2 (fn s => fn T =>
berghofe@41561
   823
       (Binding.name s, SOME T, NoSyn)) fs' Ts),
berghofe@41561
   824
     (tab, ctxt'))
berghofe@41561
   825
  end;
berghofe@41561
   826
berghofe@46567
   827
fun map_pfuns f {pfuns, type_map, env} =
berghofe@46567
   828
  {pfuns = f pfuns, type_map = type_map, env = env}
berghofe@46567
   829
berghofe@46567
   830
fun map_pfuns_env f {pfuns, type_map,
berghofe@46567
   831
      env = SOME {ctxt, defs, types, funs, pfuns = pfuns_env,
berghofe@46567
   832
        ids, proving, vcs, path, prefix}} =
berghofe@46567
   833
  if proving then err_unfinished ()
berghofe@46567
   834
  else
berghofe@46567
   835
    {pfuns = pfuns, type_map = type_map,
berghofe@46567
   836
     env = SOME {ctxt = ctxt, defs = defs, types = types, funs = funs,
berghofe@46567
   837
       pfuns = f pfuns_env, ids = ids, proving = false, vcs = vcs,
berghofe@46567
   838
       path = path, prefix = prefix}};
berghofe@46567
   839
berghofe@41561
   840
fun add_proof_fun prep (s, (optty, raw_t)) thy =
berghofe@46567
   841
  VCs.map (fn data as {env, ...} =>
berghofe@46567
   842
    let
berghofe@46567
   843
      val (optty', prfx, map_pf) = (case env of
berghofe@46567
   844
          SOME {funs, prefix, ...} =>
berghofe@46567
   845
            (lookup funs (unprefix_pfun prefix s),
berghofe@46567
   846
             prefix, map_pfuns_env)
berghofe@46567
   847
        | NONE => (NONE, "", map_pfuns));
berghofe@46567
   848
      val optty'' = NONE |> upd_option optty |> upd_option optty';
berghofe@46567
   849
      val t = prep (Option.map (pfun_type thy prfx) optty'') raw_t;
berghofe@46567
   850
      val _ = (case fold_aterms (fn u =>
berghofe@46567
   851
          if is_Var u orelse is_Free u then insert (op =) u else I) t [] of
berghofe@46567
   852
          [] => ()
berghofe@46567
   853
        | ts => error ("Term\n" ^ Syntax.string_of_term_global thy t ^
berghofe@46567
   854
            "\nto be associated with proof function " ^ s ^
berghofe@46567
   855
            " contains free variable(s) " ^
berghofe@46567
   856
            commas (map (Syntax.string_of_term_global thy) ts)));
berghofe@46567
   857
    in
berghofe@46567
   858
      (check_pfun_type thy prfx s t optty optty';
berghofe@46567
   859
       if is_some optty'' orelse is_none env then
berghofe@46567
   860
         map_pf (Symtab.update_new (s, (optty'', t))) data
berghofe@46567
   861
           handle Symtab.DUP _ => error ("Proof function " ^ s ^
berghofe@46567
   862
             " already associated with function")
berghofe@46567
   863
       else error ("Undeclared proof function " ^ s))
berghofe@46567
   864
    end) thy;
berghofe@41561
   865
berghofe@46725
   866
fun check_mapping _ _ [] _ = ()
berghofe@46725
   867
  | check_mapping err s cmap cs =
berghofe@46725
   868
      (case duplicates (op = o pairself fst) cmap of
berghofe@46725
   869
         [] => (case duplicates (op = o pairself snd) cmap of
berghofe@46725
   870
             [] => (case subtract (op = o apsnd snd) cs cmap of
berghofe@46725
   871
                 [] => (case subtract (op = o apfst snd) cmap cs of
berghofe@46725
   872
                     [] => ()
berghofe@46725
   873
                   | zs => err ("has extra " ^ s ^ "(s) " ^ commas zs))
berghofe@46725
   874
               | zs => err ("does not have " ^ s ^ "(s) " ^
berghofe@46725
   875
                   commas (map snd zs)))
berghofe@46725
   876
           | zs => error ("Several SPARK names are mapped to " ^
berghofe@46725
   877
               commas (map snd zs)))
berghofe@46725
   878
       | zs => error ("The SPARK names " ^ commas (map fst zs) ^
berghofe@46725
   879
           " are mapped to more than one name"));
berghofe@46725
   880
berghofe@46725
   881
fun add_type (s, (T as Type (tyname, Ts), cmap)) thy =
berghofe@46725
   882
      let val cmap' = map (apsnd (Sign.intern_const thy)) cmap
berghofe@46725
   883
      in
berghofe@46725
   884
        thy |>
berghofe@46725
   885
        VCs.map (fn
berghofe@46725
   886
            {env = SOME _, ...} => err_unfinished ()
berghofe@46725
   887
          | {pfuns, type_map, env} =>
berghofe@46725
   888
              {pfuns = pfuns,
berghofe@46725
   889
               type_map = Symtab.update_new (s, (T, cmap')) type_map,
berghofe@46725
   890
               env = env}
berghofe@46725
   891
                handle Symtab.DUP _ => error ("SPARK type " ^ s ^
berghofe@46725
   892
                  " already associated with type")) |>
berghofe@46725
   893
        (fn thy' =>
berghofe@46725
   894
           case Datatype.get_constrs thy' tyname of
berghofe@46725
   895
             NONE => (case get_record_info thy' T of
berghofe@46725
   896
               NONE => thy'
berghofe@46725
   897
             | SOME {fields, ...} =>
berghofe@46725
   898
                 (check_mapping (assoc_ty_err thy' T s) "field"
berghofe@46725
   899
                    cmap' (map fst fields);
berghofe@46725
   900
                  thy'))
berghofe@46725
   901
           | SOME cs =>
berghofe@46725
   902
               if null Ts then
berghofe@46725
   903
                 (map
berghofe@46725
   904
                    (fn (_, Type (_, [])) => ()
berghofe@46725
   905
                      | (cname, _) => assoc_ty_err thy' T s
berghofe@46725
   906
                          ("has illegal constructor " ^
berghofe@46725
   907
                           Long_Name.base_name cname)) cs;
berghofe@46725
   908
                  check_mapping (assoc_ty_err thy' T s) "constructor"
berghofe@46725
   909
                    cmap' (map fst cs);
berghofe@46725
   910
                  add_enum_type s tyname thy')
berghofe@46725
   911
               else assoc_ty_err thy' T s "is illegal")
berghofe@46725
   912
      end
berghofe@46725
   913
  | add_type (s, (T, _)) thy = assoc_ty_err thy T s "is illegal";
berghofe@42356
   914
berghofe@41561
   915
val is_closed = is_none o #env o VCs.get;
berghofe@41561
   916
berghofe@48167
   917
fun lookup_vc thy name_concl name =
berghofe@41561
   918
  (case VCs.get thy of
berghofe@46567
   919
    {env = SOME {vcs, types, funs, pfuns, ids, ctxt, prefix, ...}, ...} =>
berghofe@41561
   920
      (case VCtab.lookup vcs name of
berghofe@42396
   921
         SOME vc =>
berghofe@41561
   922
           let val (pfuns', ctxt', ids') =
berghofe@42396
   923
             declare_missing_pfuns thy prefix funs pfuns vcs ids
berghofe@48167
   924
           in
berghofe@48167
   925
             SOME (ctxt @ [ctxt'],
berghofe@48167
   926
               mk_vc thy prefix types pfuns' ids' name_concl vc)
berghofe@48167
   927
           end
berghofe@41561
   928
       | NONE => NONE)
berghofe@41561
   929
  | _ => NONE);
berghofe@41561
   930
berghofe@48167
   931
fun get_vcs thy name_concl = (case VCs.get thy of
berghofe@46567
   932
    {env = SOME {vcs, types, funs, pfuns, ids, ctxt, defs, prefix, ...}, ...} =>
berghofe@41561
   933
      let val (pfuns', ctxt', ids') =
berghofe@42396
   934
        declare_missing_pfuns thy prefix funs pfuns vcs ids
berghofe@41561
   935
      in
berghofe@41561
   936
        (ctxt @ [ctxt'], defs,
berghofe@41561
   937
         VCtab.dest vcs |>
berghofe@48167
   938
         map (apsnd (mk_vc thy prefix types pfuns' ids' name_concl)))
berghofe@41561
   939
      end
berghofe@41561
   940
  | _ => ([], [], []));
berghofe@41561
   941
berghofe@41896
   942
fun mark_proved name thms = VCs.map (fn
berghofe@42356
   943
    {pfuns, type_map,
berghofe@46567
   944
     env = SOME {ctxt, defs, types, funs, pfuns = pfuns_env,
berghofe@46567
   945
        ids, vcs, path, prefix, ...}} =>
berghofe@41561
   946
      {pfuns = pfuns,
berghofe@42356
   947
       type_map = type_map,
berghofe@41561
   948
       env = SOME {ctxt = ctxt, defs = defs,
berghofe@46567
   949
         types = types, funs = funs, pfuns = pfuns_env,
berghofe@46567
   950
         ids = ids,
berghofe@41561
   951
         proving = true,
berghofe@48167
   952
         vcs = print_open_vcs insert_break (VCtab.map_entry name
berghofe@48167
   953
           (fn (trace, _, ps, cs) => (trace, SOME thms, ps, cs)) vcs),
berghofe@42396
   954
         path = path,
berghofe@42396
   955
         prefix = prefix}}
berghofe@41561
   956
  | x => x);
berghofe@41561
   957
berghofe@48167
   958
fun close incomplete thy =
berghofe@42356
   959
  thy |>
berghofe@42356
   960
  VCs.map (fn
berghofe@42356
   961
      {pfuns, type_map, env = SOME {vcs, path, ...}} =>
berghofe@48167
   962
        let
berghofe@48167
   963
          val (proved, unproved) = partition_vcs vcs;
berghofe@48167
   964
          val _ = Thm.join_proofs (maps (#2 o snd) proved);
berghofe@48167
   965
          val (proved', proved'') = List.partition (fn (_, (_, thms, _, _)) =>
wenzelm@50126
   966
            exists (#oracle o Thm.peek_status) thms) proved
berghofe@48167
   967
        in
berghofe@48167
   968
          (if null unproved then ()
berghofe@48167
   969
           else (if incomplete then warning else error)
berghofe@48167
   970
             (Pretty.string_of (pp_open_vcs unproved));
berghofe@48167
   971
           if null proved' then ()
berghofe@48167
   972
           else warning (Pretty.string_of (pp_vcs
berghofe@48167
   973
             "The following VCs are not marked as proved \
berghofe@48167
   974
             \because their proofs contain oracles:" proved'));
berghofe@48167
   975
           File.write (Path.ext "prv" path)
berghofe@48167
   976
             (implode (map (fn (s, _) => snd (strip_number s) ^
berghofe@48167
   977
                " -- proved by " ^ Distribution.version ^ "\n") proved''));
berghofe@48167
   978
           {pfuns = pfuns, type_map = type_map, env = NONE})
berghofe@48167
   979
        end
berghofe@42356
   980
    | _ => error "No SPARK environment is currently open") |>
berghofe@42356
   981
  Sign.parent_path;
berghofe@41561
   982
berghofe@41561
   983
berghofe@41561
   984
(** set up verification conditions **)
berghofe@41561
   985
berghofe@41561
   986
fun partition_opt f =
berghofe@41561
   987
  let
berghofe@41561
   988
    fun part ys zs [] = (rev ys, rev zs)
berghofe@41561
   989
      | part ys zs (x :: xs) = (case f x of
berghofe@41561
   990
            SOME y => part (y :: ys) zs xs
berghofe@41561
   991
          | NONE => part ys (x :: zs) xs)
berghofe@41561
   992
  in part [] [] end;
berghofe@41561
   993
berghofe@41561
   994
fun dest_def (id, (Substitution_Rule ([], Ident s, rhs))) = SOME (id, (s, rhs))
berghofe@41561
   995
  | dest_def _ = NONE;
berghofe@41561
   996
berghofe@41561
   997
fun mk_rulename (s, i) = Binding.name (s ^ string_of_int i);
berghofe@41561
   998
berghofe@42396
   999
fun add_const prfx (s, ty) ((tab, ctxt), thy) =
berghofe@41561
  1000
  let
berghofe@42396
  1001
    val T = mk_type thy prfx ty;
berghofe@41561
  1002
    val b = Binding.name s;
berghofe@41561
  1003
    val c = Const (Sign.full_name thy b, T)
berghofe@41561
  1004
  in
berghofe@41561
  1005
    (c,
berghofe@41561
  1006
     ((Symtab.update (s, (c, ty)) tab, Name.declare s ctxt),
berghofe@41561
  1007
      Sign.add_consts_i [(b, T, NoSyn)] thy))
berghofe@41561
  1008
  end;
berghofe@41561
  1009
berghofe@42396
  1010
fun add_def prfx types pfuns consts (id, (s, e)) (ids as (tab, ctxt), thy) =
berghofe@41561
  1011
  (case lookup consts s of
berghofe@41561
  1012
     SOME ty =>
berghofe@41561
  1013
       let
berghofe@42396
  1014
         val (t, ty') = term_of_expr thy prfx types pfuns ids e;
berghofe@42396
  1015
         val T = mk_type thy prfx ty;
berghofe@42396
  1016
         val T' = mk_type thy prfx ty';
berghofe@41878
  1017
         val _ = T = T' orelse
berghofe@41561
  1018
           error ("Declared type " ^ ty ^ " of " ^ s ^
berghofe@41561
  1019
             "\ndoes not match type " ^ ty' ^ " in definition");
berghofe@41561
  1020
         val id' = mk_rulename id;
berghofe@41561
  1021
         val lthy = Named_Target.theory_init thy;
berghofe@41561
  1022
         val ((t', (_, th)), lthy') = Specification.definition
berghofe@41561
  1023
           (NONE, ((id', []), HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@41878
  1024
             (Free (s, T), t)))) lthy;
wenzelm@42361
  1025
         val phi = Proof_Context.export_morphism lthy' lthy
berghofe@41561
  1026
       in
berghofe@41561
  1027
         ((id', Morphism.thm phi th),
berghofe@41561
  1028
          ((Symtab.update (s, (Morphism.term phi t', ty)) tab,
berghofe@41561
  1029
            Name.declare s ctxt),
berghofe@41561
  1030
           Local_Theory.exit_global lthy'))
berghofe@41561
  1031
       end
berghofe@41561
  1032
   | NONE => error ("Undeclared constant " ^ s));
berghofe@41561
  1033
berghofe@42396
  1034
fun add_var prfx (s, ty) (ids, thy) =
berghofe@42396
  1035
  let val ([Free p], ids') = mk_variables thy prfx [s] ty ids
berghofe@41561
  1036
  in (p, (ids', thy)) end;
berghofe@41561
  1037
berghofe@42396
  1038
fun add_init_vars prfx vcs (ids_thy as ((tab, _), _)) =
berghofe@42396
  1039
  fold_map (add_var prfx)
berghofe@41561
  1040
    (map_filter
berghofe@41561
  1041
       (fn s => case try (unsuffix "~") s of
berghofe@41561
  1042
          SOME s' => (case Symtab.lookup tab s' of
berghofe@41561
  1043
            SOME (_, ty) => SOME (s, ty)
berghofe@41561
  1044
          | NONE => error ("Undeclared identifier " ^ s'))
berghofe@41561
  1045
        | NONE => NONE)
berghofe@41561
  1046
       (fold_vcs (add_expr_idents o snd) vcs []))
berghofe@41561
  1047
    ids_thy;
berghofe@41561
  1048
berghofe@41561
  1049
fun is_trivial_vc ([], [(_, Ident "true")]) = true
berghofe@41561
  1050
  | is_trivial_vc _ = false;
berghofe@41561
  1051
berghofe@41561
  1052
fun rulenames rules = commas
berghofe@41561
  1053
  (map (fn ((s, i), _) => s ^ "(" ^ string_of_int i ^ ")") rules);
berghofe@41561
  1054
berghofe@41561
  1055
(* sort definitions according to their dependency *)
berghofe@42499
  1056
fun sort_defs _ _ _ _ [] sdefs = rev sdefs
berghofe@42499
  1057
  | sort_defs prfx funs pfuns consts defs sdefs =
berghofe@41561
  1058
      (case find_first (fn (_, (_, e)) =>
berghofe@42499
  1059
         forall (is_some o lookup_prfx prfx pfuns)
berghofe@42499
  1060
           (add_expr_pfuns funs e []) andalso
berghofe@41561
  1061
         forall (fn id =>
berghofe@41561
  1062
           member (fn (s, (_, (s', _))) => s = s') sdefs id orelse
berghofe@41878
  1063
           consts id)
berghofe@41561
  1064
             (add_expr_idents e [])) defs of
berghofe@42499
  1065
         SOME d => sort_defs prfx funs pfuns consts
berghofe@41561
  1066
           (remove (op =) d defs) (d :: sdefs)
berghofe@41561
  1067
       | NONE => error ("Bad definitions: " ^ rulenames defs));
berghofe@41561
  1068
berghofe@42356
  1069
fun set_vcs ({types, vars, consts, funs} : decls)
berghofe@48453
  1070
      (rules, _) ((_, ident), vcs) path opt_prfx thy =
berghofe@41561
  1071
  let
berghofe@48453
  1072
    val prfx' =
berghofe@48453
  1073
      if opt_prfx = "" then
berghofe@48453
  1074
        space_implode "__" (Long_Name.explode (Long_Name.qualifier ident))
berghofe@48453
  1075
      else opt_prfx;
berghofe@48453
  1076
    val prfx = to_lower prfx';
berghofe@41561
  1077
    val {pfuns, ...} = VCs.get thy;
berghofe@41878
  1078
    val (defs, rules') = partition_opt dest_def rules;
berghofe@41561
  1079
    val consts' =
berghofe@41878
  1080
      subtract (fn ((_, (s, _)), (s', _)) => s = s') defs (items consts);
berghofe@41561
  1081
    (* ignore all complex rules in rls files *)
berghofe@41561
  1082
    val (rules'', other_rules) =
berghofe@41561
  1083
      List.partition (complex_rule o snd) rules';
berghofe@41561
  1084
    val _ = if null rules'' then ()
berghofe@41561
  1085
      else warning ("Ignoring rules: " ^ rulenames rules'');
berghofe@41561
  1086
berghofe@41561
  1087
    val vcs' = VCtab.make (maps (fn (tr, vcs) =>
berghofe@41896
  1088
      map (fn (s, (ps, cs)) => (s, (tr, NONE, ps, cs)))
berghofe@41561
  1089
        (filter_out (is_trivial_vc o snd) vcs)) vcs);
berghofe@41561
  1090
berghofe@41561
  1091
    val _ = (case filter_out (is_some o lookup funs)
berghofe@42499
  1092
        (pfuns_of_vcs prfx funs pfuns vcs') of
berghofe@41561
  1093
        [] => ()
berghofe@41561
  1094
      | fs => error ("Undeclared proof function(s) " ^ commas fs));
berghofe@41561
  1095
berghofe@41561
  1096
    val (((defs', vars''), ivars), (ids, thy')) =
berghofe@41561
  1097
      ((Symtab.empty |>
wenzelm@45740
  1098
        Symtab.update ("false", (@{term False}, booleanN)) |>
wenzelm@45740
  1099
        Symtab.update ("true", (@{term True}, booleanN)),
berghofe@42356
  1100
        Name.context),
berghofe@46724
  1101
       thy |> Sign.add_path
berghofe@48453
  1102
         ((if prfx' = "" then "" else prfx' ^ "__") ^ Long_Name.base_name ident)) |>
berghofe@42396
  1103
      fold (add_type_def prfx) (items types) |>
berghofe@42396
  1104
      fold (snd oo add_const prfx) consts' |> (fn ids_thy as ((tab, _), _) =>
berghofe@41878
  1105
        ids_thy |>
berghofe@42396
  1106
        fold_map (add_def prfx types pfuns consts)
berghofe@42499
  1107
          (sort_defs prfx funs pfuns (Symtab.defined tab) defs []) ||>>
berghofe@42396
  1108
        fold_map (add_var prfx) (items vars) ||>>
berghofe@42396
  1109
        add_init_vars prfx vcs');
berghofe@41561
  1110
berghofe@41561
  1111
    val ctxt =
berghofe@41561
  1112
      [Element.Fixes (map (fn (s, T) =>
berghofe@41561
  1113
         (Binding.name s, SOME T, NoSyn)) (vars'' @ ivars)),
berghofe@41561
  1114
       Element.Assumes (map (fn (id, rl) =>
berghofe@41561
  1115
         ((mk_rulename id, []),
berghofe@42396
  1116
          [(term_of_rule thy' prfx types pfuns ids rl, [])]))
berghofe@41561
  1117
           other_rules),
wenzelm@42440
  1118
       Element.Notes ("", [((Binding.name "defns", []), map (rpair [] o single o snd) defs')])]
berghofe@41561
  1119
          
berghofe@41561
  1120
  in
berghofe@46567
  1121
    set_env ctxt defs' types funs ids vcs' path prfx thy'
berghofe@41561
  1122
  end;
berghofe@41561
  1123
berghofe@41561
  1124
end;