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