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