| author | wenzelm | 
| Tue, 23 Oct 2001 19:14:13 +0200 | |
| changeset 11903 | 938dd8bca661 | 
| parent 11720 | 5341e38309e8 | 
| child 12068 | 469f372d63db | 
| permissions | -rw-r--r-- | 
| 19 | 1 | (* Title: Pure/sign.ML | 
| 0 | 2 | ID: $Id$ | 
| 251 | 3 | Author: Lawrence C Paulson and Markus Wenzel | 
| 0 | 4 | |
| 251 | 5 | The abstract type "sg" of signatures. | 
| 0 | 6 | *) | 
| 7 | ||
| 3956 | 8 | (*base names*) | 
| 9 | type bstring = string; | |
| 10 | type bclass = class; | |
| 11 | (*external forms -- partially qualified names*) | |
| 3805 | 12 | type xstring = string; | 
| 13 | type xclass = class; | |
| 14 | type xsort = sort; | |
| 15 | type xtyp = typ; | |
| 16 | type xterm = term; | |
| 17 | ||
| 19 | 18 | signature SIGN = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 19 | sig | 
| 1501 | 20 | type sg | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 21 | type sg_ref | 
| 4256 | 22 | type data | 
| 2197 | 23 | val rep_sg: sg -> | 
| 3975 | 24 |    {self: sg_ref,
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 25 | tsig: Type.type_sig, | 
| 2197 | 26 | const_tab: typ Symtab.table, | 
| 27 | syn: Syntax.syntax, | |
| 11501 | 28 | path: string list option, | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 29 | spaces: (string * NameSpace.T) list, | 
| 4256 | 30 | data: data} | 
| 3975 | 31 | val name_of: sg -> string | 
| 32 | val stamp_names_of: sg -> string list | |
| 10932 | 33 | val exists_stamp: string -> sg -> bool | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 34 | val tsig_of: sg -> Type.type_sig | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 35 | val deref: sg_ref -> sg | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 36 | val self_ref: sg -> sg_ref | 
| 1501 | 37 | val subsig: sg * sg -> bool | 
| 9031 | 38 | val joinable: sg * sg -> bool | 
| 1501 | 39 | val eq_sg: sg * sg -> bool | 
| 40 | val same_sg: sg * sg -> bool | |
| 41 | val is_draft: sg -> bool | |
| 4951 | 42 | val is_stale: sg -> bool | 
| 1501 | 43 | val const_type: sg -> string -> typ option | 
| 44 | val classes: sg -> class list | |
| 4844 | 45 | val defaultS: sg -> sort | 
| 1501 | 46 | val subsort: sg -> sort * sort -> bool | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 47 | val nodup_vars: term -> term | 
| 1501 | 48 | val norm_sort: sg -> sort -> sort | 
| 4568 | 49 | val of_sort: sg -> typ * sort -> bool | 
| 7640 | 50 | val witness_sorts: sg -> sort list -> sort list -> (typ * sort) list | 
| 51 | val univ_witness: sg -> (typ * sort) option | |
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 52 | val typ_instance: sg -> typ * typ -> bool | 
| 3810 | 53 | val classK: string | 
| 54 | val typeK: string | |
| 55 | val constK: string | |
| 3956 | 56 | val full_name: sg -> bstring -> string | 
| 4844 | 57 | val full_name_path: sg -> string -> bstring -> string | 
| 3956 | 58 | val base_name: string -> bstring | 
| 3810 | 59 | val intern: sg -> string -> xstring -> string | 
| 60 | val extern: sg -> string -> string -> xstring | |
| 3937 | 61 | val cond_extern: sg -> string -> string -> xstring | 
| 6845 | 62 | val cond_extern_table: sg -> string -> 'a Symtab.table -> (xstring * 'a) list | 
| 3805 | 63 | val intern_class: sg -> xclass -> class | 
| 64 | val intern_tycon: sg -> xstring -> string | |
| 65 | val intern_const: sg -> xstring -> string | |
| 3937 | 66 | val intern_sort: sg -> xsort -> sort | 
| 67 | val intern_typ: sg -> xtyp -> typ | |
| 68 | val intern_term: sg -> xterm -> term | |
| 69 | val intern_tycons: sg -> xtyp -> typ | |
| 1501 | 70 | val pretty_sg: sg -> Pretty.T | 
| 4051 | 71 | val str_of_sg: sg -> string | 
| 1501 | 72 | val pprint_sg: sg -> pprint_args -> unit | 
| 73 | val pretty_term: sg -> term -> Pretty.T | |
| 74 | val pretty_typ: sg -> typ -> Pretty.T | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 75 | val pretty_sort: sg -> sort -> Pretty.T | 
| 4249 | 76 | val pretty_classrel: sg -> class * class -> Pretty.T | 
| 77 | val pretty_arity: sg -> string * sort list * sort -> Pretty.T | |
| 1501 | 78 | val string_of_term: sg -> term -> string | 
| 79 | val string_of_typ: sg -> typ -> string | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 80 | val string_of_sort: sg -> sort -> string | 
| 3855 | 81 | val str_of_sort: sg -> sort -> string | 
| 82 | val str_of_classrel: sg -> class * class -> string | |
| 83 | val str_of_arity: sg -> string * sort list * sort -> string | |
| 1501 | 84 | val pprint_term: sg -> term -> pprint_args -> unit | 
| 85 | val pprint_typ: sg -> typ -> pprint_args -> unit | |
| 8898 | 86 | val certify_class: sg -> class -> class | 
| 87 | val certify_sort: sg -> sort -> sort | |
| 1501 | 88 | val certify_typ: sg -> typ -> typ | 
| 9504 | 89 | val certify_typ_no_norm: sg -> typ -> typ | 
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 90 | val certify_tycon: sg -> string -> string | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 91 | val certify_tyabbr: sg -> string -> string | 
| 11720 | 92 | val certify_tyname: sg -> string -> string | 
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 93 | val certify_const: sg -> string -> string | 
| 1501 | 94 | val certify_term: sg -> term -> term * typ * int | 
| 8898 | 95 | val read_sort: sg -> string -> sort | 
| 4227 | 96 | val read_raw_typ: sg * (indexname -> sort option) -> string -> typ | 
| 1501 | 97 | val read_typ: sg * (indexname -> sort option) -> string -> typ | 
| 9504 | 98 | val read_typ_no_norm: sg * (indexname -> sort option) -> string -> typ | 
| 1501 | 99 | val infer_types: sg -> (indexname -> typ option) -> | 
| 100 | (indexname -> sort option) -> string list -> bool | |
| 4249 | 101 | -> xterm list * typ -> term * (indexname * typ) list | 
| 102 | val infer_types_simult: sg -> (indexname -> typ option) -> | |
| 103 | (indexname -> sort option) -> string list -> bool | |
| 104 | -> (xterm list * typ) list -> term list * (indexname * typ) list | |
| 8607 | 105 | val read_def_terms: | 
| 106 | sg * (indexname -> typ option) * (indexname -> sort option) -> | |
| 107 | string list -> bool -> (string * typ) list -> term list * (indexname * typ) list | |
| 8802 | 108 | val simple_read_term: sg -> typ -> string -> term | 
| 3956 | 109 | val add_classes: (bclass * xclass list) list -> sg -> sg | 
| 110 | val add_classes_i: (bclass * class list) list -> sg -> sg | |
| 3805 | 111 | val add_classrel: (xclass * xclass) list -> sg -> sg | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 112 | val add_classrel_i: (class * class) list -> sg -> sg | 
| 8898 | 113 | val add_defsort: string -> sg -> sg | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 114 | val add_defsort_i: sort -> sg -> sg | 
| 3956 | 115 | val add_types: (bstring * int * mixfix) list -> sg -> sg | 
| 4844 | 116 | val add_nonterminals: bstring list -> sg -> sg | 
| 3956 | 117 | val add_tyabbrs: (bstring * string list * string * mixfix) list -> sg -> sg | 
| 118 | val add_tyabbrs_i: (bstring * string list * typ * mixfix) list -> sg -> sg | |
| 8898 | 119 | val add_arities: (xstring * string list * string) list -> sg -> sg | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 120 | val add_arities_i: (string * sort list * sort) list -> sg -> sg | 
| 3956 | 121 | val add_consts: (bstring * string * mixfix) list -> sg -> sg | 
| 122 | val add_consts_i: (bstring * typ * mixfix) list -> sg -> sg | |
| 123 | val add_syntax: (bstring * string * mixfix) list -> sg -> sg | |
| 124 | val add_syntax_i: (bstring * typ * mixfix) list -> sg -> sg | |
| 125 | val add_modesyntax: (string * bool) * (bstring * string * mixfix) list -> sg -> sg | |
| 126 | val add_modesyntax_i: (string * bool) * (bstring * typ * mixfix) list -> sg -> sg | |
| 1501 | 127 | val add_trfuns: | 
| 4344 | 128 | (string * (ast list -> ast)) list * | 
| 129 | (string * (term list -> term)) list * | |
| 130 | (string * (term list -> term)) list * | |
| 131 | (string * (ast list -> ast)) list -> sg -> sg | |
| 2385 | 132 | val add_trfunsT: | 
| 4344 | 133 | (string * (bool -> typ -> term list -> term)) list -> sg -> sg | 
| 2693 | 134 | val add_tokentrfuns: | 
| 6311 | 135 | (string * string * (string -> string * real)) list -> sg -> sg | 
| 4619 | 136 | val add_trrules: (xstring * string) Syntax.trrule list -> sg -> sg | 
| 1810 
0eef167ebe1b
Translation infixes <->, etc., no longer available at top-level
 paulson parents: 
1580diff
changeset | 137 | val add_trrules_i: ast Syntax.trrule list -> sg -> sg | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 138 | val add_path: string -> sg -> sg | 
| 3810 | 139 | val add_space: string * string list -> sg -> sg | 
| 8725 | 140 | val hide_space: string * string list -> sg -> sg | 
| 141 | val hide_space_i: string * string list -> sg -> sg | |
| 1501 | 142 | val add_name: string -> sg -> sg | 
| 4256 | 143 | val data_kinds: data -> string list | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 144 | val merge_refs: sg_ref * sg_ref -> sg_ref | 
| 4627 | 145 | val merge: sg * sg -> sg | 
| 3975 | 146 | val prep_ext: sg -> sg | 
| 6546 | 147 | val copy: sg -> sg | 
| 10932 | 148 | val PureN: string | 
| 149 | val CPureN: string | |
| 4627 | 150 | val nontriv_merge: sg * sg -> sg | 
| 3995 | 151 | val pre_pure: sg | 
| 1501 | 152 | val const_of_class: class -> string | 
| 153 | val class_of_const: string -> class | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 154 | end; | 
| 0 | 155 | |
| 6191 | 156 | signature PRIVATE_SIGN = | 
| 5642 | 157 | sig | 
| 158 | include SIGN | |
| 6546 | 159 | val init_data: Object.kind * (Object.T * (Object.T -> Object.T) * (Object.T -> Object.T) * | 
| 5642 | 160 | (Object.T * Object.T -> Object.T) * (sg -> Object.T -> unit)) -> sg -> sg | 
| 161 | val get_data: Object.kind -> (Object.T -> 'a) -> sg -> 'a | |
| 162 |   val put_data: Object.kind -> ('a -> Object.T) -> 'a -> sg -> sg
 | |
| 163 | val print_data: Object.kind -> sg -> unit | |
| 164 | end; | |
| 165 | ||
| 6191 | 166 | structure Sign: PRIVATE_SIGN = | 
| 143 | 167 | struct | 
| 0 | 168 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 169 | |
| 251 | 170 | (** datatype sg **) | 
| 171 | ||
| 4256 | 172 | (* types sg, data, sg_ref *) | 
| 173 | ||
| 19 | 174 | datatype sg = | 
| 3975 | 175 | Sg of | 
| 176 |    {id: string ref,                             (*id*)
 | |
| 177 | stamps: string ref list} * (*unique theory indentifier*) | |
| 178 |    {self: sg_ref,                               (*mutable self reference*)
 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 179 | tsig: Type.type_sig, (*order-sorted signature of types*) | 
| 3805 | 180 | const_tab: typ Symtab.table, (*type schemes of constants*) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 181 | syn: Syntax.syntax, (*syntax for parsing and printing*) | 
| 11501 | 182 | path: string list option, (*current name space entry prefix*) | 
| 3975 | 183 | spaces: (string * NameSpace.T) list, (*name spaces for consts, types etc.*) | 
| 4256 | 184 | data: data} (*anytype data*) | 
| 185 | and data = | |
| 186 | Data of | |
| 10404 | 187 | (Object.kind * (*kind (for authorization)*) | 
| 188 | (Object.T * (*value*) | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 189 | ((Object.T -> Object.T) * (*prepare extend method*) | 
| 6546 | 190 | (Object.T -> Object.T) * (*copy method*) | 
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 191 | (Object.T * Object.T -> Object.T) * (*merge and prepare extend method*) | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 192 | (sg -> Object.T -> unit)))) (*print method*) | 
| 4256 | 193 | Symtab.table | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 194 | and sg_ref = | 
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 195 | SgRef of sg ref option; | 
| 0 | 196 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 197 | (*make signature*) | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 198 | fun make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps) = | 
| 3975 | 199 |   Sg ({id = id, stamps = stamps}, {self = self, tsig = tsig, const_tab = const_tab,
 | 
| 200 | syn = syn, path = path, spaces = spaces, data = data}); | |
| 201 | ||
| 202 | ||
| 4256 | 203 | (* basic operations *) | 
| 3975 | 204 | |
| 205 | fun rep_sg (Sg (_, args)) = args; | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 206 | |
| 3975 | 207 | (*show stamps*) | 
| 208 | fun stamp_names_of (Sg ({stamps, ...}, _)) = rev (map ! stamps);
 | |
| 10932 | 209 | fun exists_stamp name (Sg ({stamps, ...}, _)) = exists (equal name o !) stamps;
 | 
| 3975 | 210 | fun pretty_sg sg = Pretty.str_list "{" "}" (stamp_names_of sg);
 | 
| 211 | val str_of_sg = Pretty.str_of o pretty_sg; | |
| 212 | val pprint_sg = Pretty.pprint o pretty_sg; | |
| 213 | ||
| 402 | 214 | val tsig_of = #tsig o rep_sg; | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 215 | |
| 3975 | 216 | fun const_type (Sg (_, {const_tab, ...})) c = Symtab.lookup (const_tab, c);
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 217 | |
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 218 | |
| 3995 | 219 | (* id and self *) | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 220 | |
| 3975 | 221 | fun check_stale (sg as Sg ({id, ...},
 | 
| 222 |         {self = SgRef (Some (ref (Sg ({id = id', ...}, _)))), ...})) =
 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 223 | if id = id' then sg | 
| 3975 | 224 |       else raise TERM ("Stale signature: " ^ str_of_sg sg, [])
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 225 | | check_stale _ = sys_error "Sign.check_stale"; | 
| 0 | 226 | |
| 4951 | 227 | fun is_stale sg = (check_stale sg; false) handle TERM _ => true; | 
| 228 | ||
| 3995 | 229 | fun self_ref (sg as Sg (_, {self, ...})) = (check_stale sg; self);
 | 
| 230 | ||
| 231 | fun deref (SgRef (Some (ref sg))) = sg | |
| 232 | | deref (SgRef None) = sys_error "Sign.deref"; | |
| 233 | ||
| 3975 | 234 | fun name_of (sg as Sg ({id = ref name, ...}, _)) =
 | 
| 4844 | 235 | if name = "" orelse ord name = ord "#" then | 
| 3975 | 236 |     raise TERM ("Nameless signature " ^ str_of_sg sg, [])
 | 
| 237 | else name; | |
| 238 | ||
| 206 
0d624d1ba9cc
added subsig: sg * sg -> bool to test if one signature is contained in another.
 nipkow parents: 
200diff
changeset | 239 | |
| 2979 | 240 | (* inclusion and equality *) | 
| 2180 
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2144diff
changeset | 241 | |
| 2185 | 242 | local | 
| 243 | (*avoiding polymorphic equality: factor 10 speedup*) | |
| 244 | fun mem_stamp (_:string ref, []) = false | |
| 245 | | mem_stamp (x, y :: ys) = x = y orelse mem_stamp (x, ys); | |
| 246 | ||
| 247 | fun subset_stamp ([], ys) = true | |
| 248 | | subset_stamp (x :: xs, ys) = | |
| 249 | mem_stamp (x, ys) andalso subset_stamp (xs, ys); | |
| 2180 
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2144diff
changeset | 250 | |
| 2185 | 251 | (*fast partial test*) | 
| 252 | fun fast_sub ([]: string ref list, _) = true | |
| 253 | | fast_sub (_, []) = false | |
| 254 | | fast_sub (x :: xs, y :: ys) = | |
| 255 | if x = y then fast_sub (xs, ys) | |
| 256 | else fast_sub (x :: xs, ys); | |
| 257 | in | |
| 3975 | 258 |   fun eq_sg (sg1 as Sg ({id = id1, ...}, _), sg2 as Sg ({id = id2, ...}, _)) =
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 259 | (check_stale sg1; check_stale sg2; id1 = id2); | 
| 2185 | 260 | |
| 3975 | 261 |   fun subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 262 | eq_sg (sg1, sg2) orelse subset_stamp (s1, s2); | 
| 2180 
934572a94139
Removal of polymorphic equality via mem, subset, eq_set, etc
 paulson parents: 
2144diff
changeset | 263 | |
| 3975 | 264 |   fun fast_subsig (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 265 | eq_sg (sg1, sg2) orelse fast_sub (s1, s2); | 
| 2185 | 266 | end; | 
| 267 | ||
| 402 | 268 | |
| 9031 | 269 | fun joinable (sg1, sg2) = subsig (sg1, sg2) orelse subsig (sg2, sg1); | 
| 270 | ||
| 2185 | 271 | (*test if same theory names are contained in signatures' stamps, | 
| 272 | i.e. if signatures belong to same theory but not necessarily to the | |
| 273 | same version of it*) | |
| 3975 | 274 | fun same_sg (sg1 as Sg ({stamps = s1, ...}, _), sg2 as Sg ({stamps = s2, ...}, _)) =
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 275 | eq_sg (sg1, sg2) orelse eq_set_string (pairself (map (op !)) (s1, s2)); | 
| 2185 | 276 | |
| 277 | (*test for drafts*) | |
| 4844 | 278 | fun is_draft (Sg ({stamps = ref name :: _, ...}, _)) = name = "" orelse ord name = ord "#";
 | 
| 386 | 279 | |
| 0 | 280 | |
| 4568 | 281 | (* classes and sorts *) | 
| 282 | ||
| 7640 | 283 | val classes = Type.classes o tsig_of; | 
| 4844 | 284 | val defaultS = Type.defaultS o tsig_of; | 
| 4568 | 285 | val subsort = Type.subsort o tsig_of; | 
| 286 | val norm_sort = Type.norm_sort o tsig_of; | |
| 7640 | 287 | val of_sort = Type.of_sort o tsig_of; | 
| 288 | val witness_sorts = Type.witness_sorts o tsig_of; | |
| 289 | val univ_witness = Type.univ_witness o tsig_of; | |
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 290 | fun typ_instance sg (T, U) = Type.typ_instance (tsig_of sg, T, U); | 
| 4568 | 291 | |
| 4256 | 292 | |
| 293 | (** signature data **) | |
| 294 | ||
| 295 | (* errors *) | |
| 296 | ||
| 4261 | 297 | fun of_theory sg = "\nof theory " ^ str_of_sg sg; | 
| 4256 | 298 | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 299 | fun err_inconsistent kinds = | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 300 |   error ("Attempt to merge different versions of " ^ commas_quote kinds ^ " data");
 | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 301 | |
| 6961 | 302 | fun err_method name kind e = | 
| 303 |   (writeln ("Error while invoking " ^ quote kind ^ " " ^ name ^ " method"); raise e);
 | |
| 4256 | 304 | |
| 305 | fun err_dup_init sg kind = | |
| 306 |   error ("Duplicate initialization of " ^ quote kind ^ " data" ^ of_theory sg);
 | |
| 307 | ||
| 308 | fun err_uninit sg kind = | |
| 10404 | 309 |   error ("Tried to access uninitialized " ^ quote kind ^ " data" ^
 | 
| 310 | of_theory sg); | |
| 4256 | 311 | |
| 6040 | 312 | (*Trying to access theory data using get / put operations from a different | 
| 313 | instance of the TheoryDataFun result. Typical cure: re-load all files*) | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 314 | fun err_access sg kind = | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 315 |   error ("Unauthorized access to " ^ quote kind ^ " data" ^ of_theory sg);
 | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 316 | |
| 4256 | 317 | |
| 318 | (* prepare data *) | |
| 319 | ||
| 4489 | 320 | val empty_data = Data Symtab.empty; | 
| 4256 | 321 | |
| 322 | fun merge_data (Data tab1, Data tab2) = | |
| 323 | let | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 324 | val data1 = map snd (Symtab.dest tab1); | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 325 | val data2 = map snd (Symtab.dest tab2); | 
| 4256 | 326 | val all_data = data1 @ data2; | 
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 327 | val kinds = gen_distinct Object.eq_kind (map fst all_data); | 
| 4256 | 328 | |
| 329 | fun entry data kind = | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 330 | (case gen_assoc Object.eq_kind (data, kind) of | 
| 4256 | 331 | None => [] | 
| 332 | | Some x => [(kind, x)]); | |
| 333 | ||
| 6546 | 334 | fun merge_entries [(kind, (e, mths as (_, ext, _, _)))] = | 
| 6961 | 335 | (kind, (ext e handle exn => err_method "prep_ext" (Object.name_of_kind kind) exn, mths)) | 
| 6546 | 336 | | merge_entries [(kind, (e1, mths as (_, _, mrg, _))), (_, (e2, _))] = | 
| 6961 | 337 | (kind, (mrg (e1, e2) | 
| 338 | handle exn => err_method "merge" (Object.name_of_kind kind) exn, mths)) | |
| 4256 | 339 | | merge_entries _ = sys_error "merge_entries"; | 
| 340 | ||
| 341 | val data = map (fn k => merge_entries (entry data1 k @ entry data2 k)) kinds; | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 342 | val data_idx = map (fn (k, x) => (Object.name_of_kind k, (k, x))) data; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 343 | in | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 344 | Data (Symtab.make data_idx) | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 345 | handle Symtab.DUPS dups => err_inconsistent dups | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 346 | end; | 
| 4256 | 347 | |
| 348 | fun prep_ext_data data = merge_data (data, empty_data); | |
| 349 | ||
| 6546 | 350 | fun init_data_sg sg (Data tab) kind e cp ext mrg prt = | 
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 351 | let val name = Object.name_of_kind kind in | 
| 6546 | 352 | Data (Symtab.update_new ((name, (kind, (e, (cp, ext, mrg, prt)))), tab)) | 
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 353 | handle Symtab.DUP _ => err_dup_init sg name | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 354 | end; | 
| 4256 | 355 | |
| 356 | ||
| 357 | (* access data *) | |
| 358 | ||
| 359 | fun data_kinds (Data tab) = map fst (Symtab.dest tab); | |
| 360 | ||
| 361 | fun lookup_data sg tab kind = | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 362 | let val name = Object.name_of_kind kind in | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 363 | (case Symtab.lookup (tab, name) of | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 364 | Some (k, x) => | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 365 | if Object.eq_kind (kind, k) then x | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 366 | else err_access sg name | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 367 | | None => err_uninit sg name) | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 368 | end; | 
| 4256 | 369 | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 370 | fun get_data kind f (sg as Sg (_, {data = Data tab, ...})) =
 | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 371 | let val x = fst (lookup_data sg tab kind) | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 372 | in f x handle Match => Object.kind_error kind end; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 373 | |
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 374 | fun print_data kind (sg as Sg (_, {data = Data tab, ...})) =
 | 
| 6546 | 375 | let val (e, (_, _, _, prt)) = lookup_data sg tab kind | 
| 6961 | 376 |   in prt sg e handle exn => err_method ("print" ^ of_theory sg) (Object.name_of_kind kind) exn end;
 | 
| 4256 | 377 | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 378 | fun put_data_sg sg (Data tab) kind f x = | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 379 | Data (Symtab.update ((Object.name_of_kind kind, | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 380 | (kind, (f x, snd (lookup_data sg tab kind)))), tab)); | 
| 4256 | 381 | |
| 382 | ||
| 383 | ||
| 384 | (** build signatures **) | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 385 | |
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 386 | fun ext_stamps stamps (id as ref name) = | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 387 | let val stmps = (case stamps of ref "#" :: ss => ss | ss => ss) in | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 388 | if exists (equal name o !) stmps then | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 389 |       error ("Theory already contains a " ^ quote name ^ " component")
 | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 390 | else id :: stmps | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 391 | end; | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 392 | |
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 393 | fun create_sign self stamps name (syn, tsig, ctab, (path, spaces), data) = | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 394 | let | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 395 | val id = ref name; | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 396 | val sign = | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 397 | make_sign (id, self, tsig, ctab, syn, path, spaces, data, ext_stamps stamps id); | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 398 | in | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 399 | (case self of | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 400 | SgRef (Some r) => r := sign | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 401 | | _ => sys_error "Sign.create_sign"); | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 402 | sign | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 403 | end; | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 404 | |
| 3975 | 405 | fun extend_sign keep extfun name decls | 
| 406 |     (sg as Sg ({id = _, stamps}, {self, tsig, const_tab, syn, path, spaces, data})) =
 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 407 | let | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 408 | val _ = check_stale sg; | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 409 | val (self', data') = | 
| 3975 | 410 | if is_draft sg andalso keep then (self, data) | 
| 4256 | 411 | else (SgRef (Some (ref sg)), prep_ext_data data); | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 412 | in | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 413 | create_sign self' stamps name | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 414 | (extfun (syn, tsig, const_tab, (path, spaces), data') decls) | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 415 | end; | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 416 | |
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 417 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 418 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 419 | (** name spaces **) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 420 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 421 | (* kinds *) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 422 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 423 | val classK = "class"; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 424 | val typeK = "type"; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 425 | val constK = "const"; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 426 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 427 | |
| 8725 | 428 | (* declare and retrieve names *) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 429 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 430 | fun space_of spaces kind = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 431 | if_none (assoc (spaces, kind)) NameSpace.empty; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 432 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 433 | (*input and output of qualified names*) | 
| 3995 | 434 | fun intrn spaces kind = NameSpace.intern (space_of spaces kind); | 
| 435 | fun extrn spaces kind = NameSpace.extern (space_of spaces kind); | |
| 5175 | 436 | fun cond_extrn spaces kind = NameSpace.cond_extern (space_of spaces kind); | 
| 6845 | 437 | fun cond_extrn_table spaces kind tab = NameSpace.cond_extern_table (space_of spaces kind) tab; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 438 | |
| 8725 | 439 | (*add / hide names*) | 
| 440 | fun change_space f spaces kind x = overwrite (spaces, (kind, f (space_of spaces kind, x))); | |
| 8730 | 441 | fun add_names x = change_space NameSpace.extend x; | 
| 442 | fun hide_names x = change_space NameSpace.hide x; | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 443 | |
| 3810 | 444 | (*make full names*) | 
| 11501 | 445 | fun full _ "" = error "Attempt to declare empty name \"\"" | 
| 446 | | full None name = name | |
| 447 | | full (Some path) name = | |
| 448 | if NameSpace.is_qualified name then | |
| 449 |         error ("Attempt to declare qualified name " ^ quote name)
 | |
| 450 | else NameSpace.pack (path @ [name]); | |
| 3937 | 451 | |
| 452 | (*base name*) | |
| 453 | val base_name = NameSpace.base; | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 454 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 455 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 456 | (* intern / extern names *) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 457 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 458 | local | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 459 | (*prepare mapping of names*) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 460 | fun mapping f add_xs t = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 461 | let | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 462 | fun f' x = let val y = f x in if x = y then None else Some (x, y) end; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 463 | val table = mapfilter f' (add_xs (t, [])); | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 464 | fun lookup x = if_none (assoc (table, x)) x; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 465 | in lookup end; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 466 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 467 | (*intern / extern typ*) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 468 | fun trn_typ trn T = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 469 | T |> map_typ | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 470 | (mapping (trn classK) add_typ_classes T) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 471 | (mapping (trn typeK) add_typ_tycons T); | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 472 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 473 | (*intern / extern term*) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 474 | fun trn_term trn t = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 475 | t |> map_term | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 476 | (mapping (trn classK) add_term_classes t) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 477 | (mapping (trn typeK) add_term_tycons t) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 478 | (mapping (trn constK) add_term_consts t); | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 479 | |
| 3975 | 480 | val spaces_of = #spaces o rep_sg; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 481 | in | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 482 | fun intrn_class spaces = intrn spaces classK; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 483 | fun extrn_class spaces = extrn spaces classK; | 
| 3937 | 484 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 485 | val intrn_sort = map o intrn_class; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 486 | val intrn_typ = trn_typ o intrn; | 
| 3937 | 487 | val intrn_term = trn_term o intrn; | 
| 488 | ||
| 489 | val extrn_sort = map o extrn_class; | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 490 | val extrn_typ = trn_typ o extrn; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 491 | val extrn_term = trn_term o extrn; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 492 | |
| 3805 | 493 | fun intrn_tycons spaces T = | 
| 494 | map_typ I (mapping (intrn spaces typeK) add_typ_tycons T) T; | |
| 495 | ||
| 3810 | 496 | val intern = intrn o spaces_of; | 
| 3855 | 497 | val extern = extrn o spaces_of; | 
| 5175 | 498 | val cond_extern = cond_extrn o spaces_of; | 
| 6845 | 499 | fun cond_extern_table sg = cond_extrn_table (spaces_of sg); | 
| 3937 | 500 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 501 | val intern_class = intrn_class o spaces_of; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 502 | val intern_sort = intrn_sort o spaces_of; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 503 | val intern_typ = intrn_typ o spaces_of; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 504 | val intern_term = intrn_term o spaces_of; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 505 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 506 | fun intern_tycon sg = intrn (spaces_of sg) typeK; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 507 | fun intern_const sg = intrn (spaces_of sg) constK; | 
| 3937 | 508 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 509 | val intern_tycons = intrn_tycons o spaces_of; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 510 | |
| 3975 | 511 | val full_name = full o #path o rep_sg; | 
| 11501 | 512 | |
| 513 | fun full_name_path sg elems = | |
| 514 | full (Some (if_none (#path (rep_sg sg)) [] @ NameSpace.unpack elems)); | |
| 515 | ||
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 516 | end; | 
| 620 | 517 | |
| 402 | 518 | |
| 0 | 519 | |
| 4249 | 520 | (** pretty printing of terms, types etc. **) | 
| 3937 | 521 | |
| 4249 | 522 | fun pretty_term (sg as Sg ({stamps, ...}, {syn, spaces, ...})) t =
 | 
| 3937 | 523 | Syntax.pretty_term syn | 
| 4249 | 524 | (exists (equal "CPure" o !) stamps) | 
| 5175 | 525 | (if ! NameSpace.long_names then t else extrn_term spaces t); | 
| 3937 | 526 | |
| 3975 | 527 | fun pretty_typ (Sg (_, {syn, spaces, ...})) T =
 | 
| 3937 | 528 | Syntax.pretty_typ syn | 
| 5175 | 529 | (if ! NameSpace.long_names then T else extrn_typ spaces T); | 
| 3937 | 530 | |
| 3975 | 531 | fun pretty_sort (Sg (_, {syn, spaces, ...})) S =
 | 
| 3937 | 532 | Syntax.pretty_sort syn | 
| 5175 | 533 | (if ! NameSpace.long_names then S else extrn_sort spaces S); | 
| 3937 | 534 | |
| 535 | fun pretty_classrel sg (c1, c2) = Pretty.block | |
| 536 | [pretty_sort sg [c1], Pretty.str " <", Pretty.brk 1, pretty_sort sg [c2]]; | |
| 537 | ||
| 538 | fun pretty_arity sg (t, Ss, S) = | |
| 539 | let | |
| 540 | val t' = cond_extern sg typeK t; | |
| 541 | val dom = | |
| 542 | if null Ss then [] | |
| 543 |       else [Pretty.list "(" ")" (map (pretty_sort sg) Ss), Pretty.brk 1];
 | |
| 544 | in | |
| 545 | Pretty.block | |
| 546 | ([Pretty.str (t' ^ " ::"), Pretty.brk 1] @ dom @ [pretty_sort sg S]) | |
| 547 | end; | |
| 548 | ||
| 549 | fun string_of_term sg t = Pretty.string_of (pretty_term sg t); | |
| 550 | fun string_of_typ sg T = Pretty.string_of (pretty_typ sg T); | |
| 551 | fun string_of_sort sg S = Pretty.string_of (pretty_sort sg S); | |
| 552 | ||
| 553 | fun str_of_sort sg S = Pretty.str_of (pretty_sort sg S); | |
| 554 | fun str_of_classrel sg c1_c2 = Pretty.str_of (pretty_classrel sg c1_c2); | |
| 555 | fun str_of_arity sg ar = Pretty.str_of (pretty_arity sg ar); | |
| 556 | ||
| 557 | fun pprint_term sg = Pretty.pprint o Pretty.quote o (pretty_term sg); | |
| 558 | fun pprint_typ sg = Pretty.pprint o Pretty.quote o (pretty_typ sg); | |
| 559 | ||
| 560 | ||
| 561 | ||
| 8898 | 562 | (** read sorts **) (*exception ERROR*) | 
| 563 | ||
| 564 | fun err_in_sort s = | |
| 565 |   error ("The error(s) above occurred in sort " ^ quote s);
 | |
| 566 | ||
| 567 | fun rd_sort syn tsig spaces str = | |
| 568 | let val S = intrn_sort spaces (Syntax.read_sort syn str handle ERROR => err_in_sort str) | |
| 569 | in Type.cert_sort tsig S handle TYPE (msg, _, _) => (error_msg msg; err_in_sort str) end; | |
| 570 | ||
| 571 | (*read and certify sort wrt a signature*) | |
| 572 | fun read_sort (sg as Sg (_, {tsig, syn, spaces, ...})) str =
 | |
| 573 | (check_stale sg; rd_sort syn tsig spaces str); | |
| 574 | ||
| 575 | fun cert_sort _ tsig _ = Type.cert_sort tsig; | |
| 576 | ||
| 577 | ||
| 578 | ||
| 562 | 579 | (** read types **) (*exception ERROR*) | 
| 580 | ||
| 581 | fun err_in_type s = | |
| 582 |   error ("The error(s) above occurred in type " ^ quote s);
 | |
| 583 | ||
| 4227 | 584 | fun rd_raw_typ syn tsig spaces def_sort str = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 585 | intrn_tycons spaces | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 586 | (Syntax.read_typ syn (Type.get_sort tsig def_sort (intrn_sort spaces)) str | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 587 | handle ERROR => err_in_type str); | 
| 4227 | 588 | |
| 589 | fun read_raw_typ (sg as Sg (_, {tsig, syn, spaces, ...}), def_sort) str =
 | |
| 590 | (check_stale sg; rd_raw_typ syn tsig spaces def_sort str); | |
| 591 | ||
| 562 | 592 | (*read and certify typ wrt a signature*) | 
| 9504 | 593 | local | 
| 594 | fun read_typ_aux cert (sg, def_sort) str = | |
| 595 | (cert (tsig_of sg) (read_raw_typ (sg, def_sort) str) | |
| 596 | handle TYPE (msg, _, _) => (error_msg msg; err_in_type str)); | |
| 597 | in | |
| 598 | val read_typ = read_typ_aux Type.cert_typ; | |
| 599 | val read_typ_no_norm = read_typ_aux Type.cert_typ_no_norm; | |
| 600 | end; | |
| 562 | 601 | |
| 602 | ||
| 603 | ||
| 8898 | 604 | (** certify classes, sorts, types and terms **) (*exception TYPE*) | 
| 251 | 605 | |
| 8898 | 606 | val certify_class = Type.cert_class o tsig_of; | 
| 607 | val certify_sort = Type.cert_sort o tsig_of; | |
| 3975 | 608 | val certify_typ = Type.cert_typ o tsig_of; | 
| 9504 | 609 | val certify_typ_no_norm = Type.cert_typ_no_norm o tsig_of; | 
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 610 | |
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 611 | fun certify_tycon sg c = | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 612 | if is_some (Symtab.lookup (#tycons (Type.rep_tsig (tsig_of sg)), c)) then c | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 613 |   else raise TYPE ("Undeclared type constructor " ^ quote c, [], []);
 | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 614 | |
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 615 | fun certify_tyabbr sg c = | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 616 | if is_some (Symtab.lookup (#abbrs (Type.rep_tsig (tsig_of sg)), c)) then c | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 617 |   else raise TYPE ("Unknown type abbreviation " ^ quote c, [], []);
 | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 618 | |
| 11720 | 619 | fun certify_tyname sg c = | 
| 620 | certify_tycon sg c handle TYPE _ => certify_tyabbr sg c handle TYPE _ => | |
| 621 |     raise TYPE ("Unknown type name " ^ quote c, [], []);
 | |
| 622 | ||
| 10443 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 623 | fun certify_const sg c = | 
| 
0a68dc9edba5
added certify_tycon, certify_tyabbr, certify_const;
 wenzelm parents: 
10404diff
changeset | 624 |   if is_some (const_type sg c) then c else raise TYPE ("Undeclared constant " ^ quote c, [], []);
 | 
| 251 | 625 | |
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 626 | |
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 627 | (* certify_term *) | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 628 | |
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 629 | (*check for duplicate occurrences of TFree/TVar with distinct sorts*) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 630 | fun nodup_tvars (env, Type (_, Ts)) = nodup_tvars_list (env, Ts) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 631 | | nodup_tvars (env as (tfrees, tvars), T as TFree (v as (a, S))) = | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 632 | (case assoc_string (tfrees, a) of | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 633 | Some S' => | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 634 | if S = S' then env | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 635 |           else raise TYPE ("Type variable " ^ quote a ^
 | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 636 | " has two distinct sorts", [TFree (a, S'), T], []) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 637 | | None => (v :: tfrees, tvars)) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 638 | | nodup_tvars (env as (tfrees, tvars), T as TVar (v as (a, S))) = | 
| 2979 | 639 | (case assoc_string_int (tvars, a) of | 
| 640 | Some S' => | |
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 641 | if S = S' then env | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 642 |           else raise TYPE ("Type variable " ^ quote (Syntax.string_of_vname a) ^
 | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 643 | " has two distinct sorts", [TVar (a, S'), T], []) | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 644 | | None => (tfrees, v :: tvars)) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 645 | (*equivalent to foldl nodup_tvars_list, but 3X faster under Poly/ML*) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 646 | and nodup_tvars_list (env, []) = env | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 647 | | nodup_tvars_list (env, T :: Ts) = nodup_tvars_list (nodup_tvars (env, T), Ts); | 
| 1494 
22f67e796445
added nodup_Vars check in cterm_of. Prevents same var with distinct types.
 nipkow parents: 
1460diff
changeset | 648 | |
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 649 | (*check for duplicate occurrences of Free/Var with distinct types*) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 650 | fun nodup_vars tm = | 
| 2979 | 651 | let | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 652 | fun nodups (envs as (env as (frees, vars), envT)) tm = | 
| 2979 | 653 | (case tm of | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 654 | Const (c, T) => (env, nodup_tvars (envT, T)) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 655 | | Free (v as (a, T)) => | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 656 | (case assoc_string (frees, a) of | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 657 | Some T' => | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 658 | if T = T' then (env, nodup_tvars (envT, T)) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 659 |               else raise TYPE ("Variable " ^ quote a ^
 | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 660 | " has two distinct types", [T', T], []) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 661 | | None => ((v :: frees, vars), nodup_tvars (envT, T))) | 
| 2979 | 662 | | Var (v as (ixn, T)) => | 
| 663 | (case assoc_string_int (vars, ixn) of | |
| 664 | Some T' => | |
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 665 | if T = T' then (env, nodup_tvars (envT, T)) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 666 |               else raise TYPE ("Variable " ^ quote (Syntax.string_of_vname ixn) ^
 | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 667 | " has two distinct types", [T', T], []) | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 668 | | None => ((frees, v :: vars), nodup_tvars (envT, T))) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 669 | | Bound _ => envs | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 670 | | Abs (_, T, t) => nodups (env, nodup_tvars (envT, T)) t | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 671 | | s $ t => nodups (nodups envs s) t) | 
| 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 672 | in nodups (([], []), ([], [])) tm; tm end; | 
| 2979 | 673 | |
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 674 | (*compute and check type of the term*) | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 675 | fun type_check sg tm = | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 676 | let | 
| 8725 | 677 | val prt = setmp Syntax.show_brackets true (pretty_term sg); | 
| 678 | val prT = pretty_typ sg; | |
| 251 | 679 | |
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 680 | fun err_appl why bs t T u U = | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 681 | let | 
| 10404 | 682 | val xs = map Free bs; (*we do not rename here*) | 
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 683 | val t' = subst_bounds (xs, t); | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 684 | val u' = subst_bounds (xs, u); | 
| 10404 | 685 | val text = cat_lines (TypeInfer.appl_error prt prT why t' T u' U); | 
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 686 | in raise TYPE (text, [T, U], [t', u']) end; | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 687 | |
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 688 | fun typ_of (_, Const (_, T)) = T | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 689 | | typ_of (_, Free (_, T)) = T | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 690 | | typ_of (_, Var (_, T)) = T | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 691 | | typ_of (bs, Bound i) = snd (nth_elem (i, bs) handle LIST _ => | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 692 |           raise TYPE ("Loose bound variable: B." ^ string_of_int i, [], [Bound i]))
 | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 693 | | typ_of (bs, Abs (x, T, body)) = T --> typ_of ((x, T) :: bs, body) | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 694 | | typ_of (bs, t $ u) = | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 695 | let val T = typ_of (bs, t) and U = typ_of (bs, u) in | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 696 | (case T of | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 697 |               Type ("fun", [T1, T2]) =>
 | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 698 | if T1 = U then T2 else err_appl "Incompatible operand type." bs t T u U | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 699 | | _ => err_appl "Operator not of function type." bs t T u U) | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 700 | end; | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 701 | |
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 702 | in typ_of ([], tm) end; | 
| 0 | 703 | |
| 2979 | 704 | |
| 3975 | 705 | fun certify_term sg tm = | 
| 251 | 706 | let | 
| 3969 | 707 | val _ = check_stale sg; | 
| 3975 | 708 | val tsig = tsig_of sg; | 
| 3969 | 709 | |
| 3975 | 710 | fun show_const a T = quote a ^ " :: " ^ quote (string_of_typ sg T); | 
| 169 | 711 | |
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 712 | fun atom_err (errs, Const (a, T)) = | 
| 3975 | 713 | (case const_type sg a of | 
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 714 |           None => ("Undeclared constant " ^ show_const a T) :: errs
 | 
| 3975 | 715 | | Some U => | 
| 10404 | 716 | if typ_instance sg (T, U) then errs | 
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 717 |             else ("Illegal type for constant " ^ show_const a T) :: errs)
 | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 718 | | atom_err (errs, Var ((x, i), _)) = | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 719 |           if i < 0 then ("Negative index for Var " ^ quote x) :: errs else errs
 | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 720 | | atom_err (errs, _) = errs; | 
| 251 | 721 | |
| 722 | val norm_tm = | |
| 2185 | 723 | (case it_term_types (Type.typ_errors tsig) (tm, []) of | 
| 7068 | 724 | [] => Type.norm_term tsig tm | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 725 | | errs => raise TYPE (cat_lines errs, [], [tm])); | 
| 8290 
7015d6b11b56
nodup_vars: fixed omission of 2 minor cases; account for Frees as well;
 wenzelm parents: 
7640diff
changeset | 726 | val _ = nodup_vars norm_tm; | 
| 251 | 727 | in | 
| 4961 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 728 | (case foldl_aterms atom_err ([], norm_tm) of | 
| 
27f559b54c57
certify_term: type_check replaces Term.type_of, providing sensible
 wenzelm parents: 
4951diff
changeset | 729 | [] => (norm_tm, type_check sg norm_tm, maxidx_of_term norm_tm) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 730 | | errs => raise TYPE (cat_lines errs, [], [norm_tm])) | 
| 251 | 731 | end; | 
| 732 | ||
| 733 | ||
| 583 | 734 | (** infer_types **) (*exception ERROR*) | 
| 251 | 735 | |
| 2979 | 736 | (* | 
| 737 | def_type: partial map from indexnames to types (constrains Frees, Vars) | |
| 738 | def_sort: partial map from indexnames to sorts (constrains TFrees, TVars) | |
| 739 | used: list of already used type variables | |
| 740 | freeze: if true then generated parameters are turned into TFrees, else TVars | |
| 4249 | 741 | |
| 742 | termss: lists of alternative parses (only one combination should be type-correct) | |
| 743 | typs: expected types | |
| 2979 | 744 | *) | 
| 745 | ||
| 4249 | 746 | fun infer_types_simult sg def_type def_sort used freeze args = | 
| 251 | 747 | let | 
| 3975 | 748 | val tsig = tsig_of sg; | 
| 8725 | 749 | val prt = setmp Syntax.show_brackets true (pretty_term sg); | 
| 750 | val prT = pretty_typ sg; | |
| 4249 | 751 | |
| 752 | val termss = foldr multiply (map fst args, [[]]); | |
| 753 | val typs = | |
| 754 | map (fn (_, T) => certify_typ sg T handle TYPE (msg, _, _) => error msg) args; | |
| 169 | 755 | |
| 4249 | 756 | fun infer ts = OK | 
| 757 | (Type.infer_types prt prT tsig (const_type sg) def_type def_sort | |
| 758 | (intern_const sg) (intern_tycons sg) (intern_sort sg) used freeze typs ts) | |
| 759 | handle TYPE (msg, _, _) => Error msg; | |
| 623 | 760 | |
| 4249 | 761 | val err_results = map infer termss; | 
| 762 | val errs = mapfilter get_error err_results; | |
| 763 | val results = mapfilter get_ok err_results; | |
| 764 | ||
| 10404 | 765 | val ambiguity = length termss; (* FIXME !? *) | 
| 4249 | 766 | (* FIXME to syntax.ML!? *) | 
| 767 | fun ambig_msg () = | |
| 768 | if ambiguity > 1 andalso ambiguity <= ! Syntax.ambiguity_level | |
| 769 | then | |
| 770 | error_msg "Got more than one parse tree.\n\ | |
| 3805 | 771 | \Retry with smaller Syntax.ambiguity_level for more information." | 
| 952 
9e10962866b0
Removed an old bug which made some simultaneous instantiations fail if they
 nipkow parents: 
949diff
changeset | 772 | else (); | 
| 4249 | 773 | in | 
| 774 | if null results then (ambig_msg (); error (cat_lines errs)) | |
| 775 | else if length results = 1 then | |
| 776 | (if ambiguity > ! Syntax.ambiguity_level then | |
| 777 | warning "Fortunately, only one parse tree is type correct.\n\ | |
| 778 | \You may still want to disambiguate your grammar or your input." | |
| 779 | else (); hd results) | |
| 780 |     else (ambig_msg (); error ("More than one term is type correct:\n" ^
 | |
| 781 | (cat_lines (map (Pretty.string_of o prt) (flat (map fst results)))))) | |
| 782 | end; | |
| 623 | 783 | |
| 4249 | 784 | |
| 785 | fun infer_types sg def_type def_sort used freeze tsT = | |
| 786 | apfst hd (infer_types_simult sg def_type def_sort used freeze [tsT]); | |
| 251 | 787 | |
| 788 | ||
| 8607 | 789 | (** read_def_terms **) | 
| 790 | ||
| 791 | (*read terms, infer types*) | |
| 792 | fun read_def_terms (sign, types, sorts) used freeze sTs = | |
| 793 | let | |
| 794 | val syn = #syn (rep_sg sign); | |
| 795 | fun read (s, T) = | |
| 796 | let val T' = certify_typ sign T handle TYPE (msg, _, _) => error msg | |
| 797 | in (Syntax.read syn T' s, T') end; | |
| 798 | val tsTs = map read sTs; | |
| 799 | in infer_types_simult sign types sorts used freeze tsTs end; | |
| 800 | ||
| 8802 | 801 | fun simple_read_term sign T s = | 
| 802 | (read_def_terms (sign, K None, K None) [] true [(s, T)] | |
| 803 |     handle ERROR => error ("The error(s) above occurred for " ^ s)) |> #1 |> hd;
 | |
| 804 | ||
| 8607 | 805 | |
| 2979 | 806 | |
| 623 | 807 | (** extend signature **) (*exception ERROR*) | 
| 808 | ||
| 620 | 809 | (** signature extension functions **) (*exception ERROR*) | 
| 386 | 810 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 811 | fun decls_of path name_of mfixs = | 
| 3810 | 812 | map (fn (x, y, mx) => (full path (name_of x mx), y)) mfixs; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 813 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 814 | fun no_read _ _ _ decl = decl; | 
| 386 | 815 | |
| 816 | ||
| 817 | (* add default sort *) | |
| 818 | ||
| 8898 | 819 | fun ext_defS prep_sort (syn, tsig, ctab, (path, spaces), data) S = | 
| 820 | (syn, Type.ext_tsig_defsort tsig (prep_sort syn tsig spaces S), ctab, (path, spaces), data); | |
| 821 | ||
| 8927 | 822 | fun ext_defsort arg = ext_defS rd_sort arg; | 
| 823 | fun ext_defsort_i arg = ext_defS cert_sort arg; | |
| 386 | 824 | |
| 825 | ||
| 826 | (* add type constructors *) | |
| 827 | ||
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 828 | fun ext_types (syn, tsig, ctab, (path, spaces), data) types = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 829 | let val decls = decls_of path Syntax.type_name types in | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 830 | (Syntax.extend_type_gram syn types, | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 831 | Type.ext_tsig_types tsig decls, ctab, | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 832 | (path, add_names spaces typeK (map fst decls)), data) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 833 | end; | 
| 386 | 834 | |
| 4844 | 835 | fun ext_nonterminals sg nonterms = | 
| 836 | ext_types sg (map (fn n => (n, 0, Syntax.NoSyn)) nonterms); | |
| 837 | ||
| 386 | 838 | |
| 839 | (* add type abbreviations *) | |
| 840 | ||
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 841 | fun read_abbr syn tsig spaces (t, vs, rhs_src) = | 
| 4227 | 842 | (t, vs, rd_raw_typ syn tsig spaces (K None) rhs_src) | 
| 386 | 843 |     handle ERROR => error ("in type abbreviation " ^ t);
 | 
| 844 | ||
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 845 | fun ext_abbrs rd_abbr (syn, tsig, ctab, (path, spaces), data) abbrs = | 
| 386 | 846 | let | 
| 847 | fun mfix_of (t, vs, _, mx) = (t, length vs, mx); | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 848 | val syn' = Syntax.extend_type_gram syn (map mfix_of abbrs); | 
| 386 | 849 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 850 | val abbrs' = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 851 | map (fn (t, vs, rhs, mx) => | 
| 3810 | 852 | (full path (Syntax.type_name t mx), vs, rhs)) abbrs; | 
| 3805 | 853 | val spaces' = add_names spaces typeK (map #1 abbrs'); | 
| 854 | val decls = map (rd_abbr syn' tsig spaces') abbrs'; | |
| 386 | 855 | in | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 856 | (syn', Type.ext_tsig_abbrs tsig decls, ctab, (path, spaces'), data) | 
| 386 | 857 | end; | 
| 858 | ||
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 859 | fun ext_tyabbrs abbrs = ext_abbrs read_abbr abbrs; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 860 | fun ext_tyabbrs_i abbrs = ext_abbrs no_read abbrs; | 
| 386 | 861 | |
| 862 | ||
| 863 | (* add type arities *) | |
| 864 | ||
| 8898 | 865 | fun ext_ars int prep_sort (syn, tsig, ctab, (path, spaces), data) arities = | 
| 386 | 866 | let | 
| 8898 | 867 | val prepS = prep_sort syn tsig spaces; | 
| 868 | fun prep_arity (c, Ss, S) = | |
| 869 | (if int then intrn spaces typeK c else c, map prepS Ss, prepS S); | |
| 870 | val tsig' = Type.ext_tsig_arities tsig (map prep_arity arities); | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 871 | val log_types = Type.logical_types tsig'; | 
| 386 | 872 | in | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 873 | (Syntax.extend_log_types syn log_types, tsig', ctab, (path, spaces), data) | 
| 386 | 874 | end; | 
| 875 | ||
| 8927 | 876 | fun ext_arities arg = ext_ars true rd_sort arg; | 
| 877 | fun ext_arities_i arg = ext_ars false cert_sort arg; | |
| 8898 | 878 | |
| 386 | 879 | |
| 880 | (* add term constants and syntax *) | |
| 881 | ||
| 3805 | 882 | fun const_name path c mx = | 
| 3810 | 883 | full path (Syntax.const_name c mx); | 
| 3805 | 884 | |
| 386 | 885 | fun err_in_const c = | 
| 886 |   error ("in declaration of constant " ^ quote c);
 | |
| 887 | ||
| 888 | fun err_dup_consts cs = | |
| 889 |   error ("Duplicate declaration of constant(s) " ^ commas_quote cs);
 | |
| 890 | ||
| 251 | 891 | |
| 3805 | 892 | fun read_const syn tsig (path, spaces) (c, ty_src, mx) = | 
| 4227 | 893 | (c, rd_raw_typ syn tsig spaces (K None) ty_src, mx) | 
| 3805 | 894 | handle ERROR => err_in_const (const_name path c mx); | 
| 386 | 895 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 896 | fun ext_cnsts rd_const syn_only prmode (syn, tsig, ctab, (path, spaces), data) raw_consts = | 
| 386 | 897 | let | 
| 2979 | 898 | fun prep_const (c, ty, mx) = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 899 | (c, compress_type (Type.varifyT (Type.cert_typ tsig (Type.no_tvars ty))), mx) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 900 | handle TYPE (msg, _, _) => | 
| 3805 | 901 | (error_msg msg; err_in_const (const_name path c mx)); | 
| 386 | 902 | |
| 3805 | 903 | val consts = map (prep_const o rd_const syn tsig (path, spaces)) raw_consts; | 
| 386 | 904 | val decls = | 
| 905 | if syn_only then [] | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 906 | else decls_of path Syntax.const_name consts; | 
| 386 | 907 | in | 
| 2197 | 908 | (Syntax.extend_const_gram syn prmode consts, tsig, | 
| 4489 | 909 | Symtab.extend (ctab, decls) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 910 | handle Symtab.DUPS cs => err_dup_consts cs, | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 911 | (path, add_names spaces constK (map fst decls)), data) | 
| 386 | 912 | end; | 
| 913 | ||
| 4007 | 914 | fun ext_consts_i sg = ext_cnsts no_read false ("", true) sg;
 | 
| 915 | fun ext_consts sg = ext_cnsts read_const false ("", true) sg;
 | |
| 916 | fun ext_syntax_i sg = ext_cnsts no_read true ("", true) sg;
 | |
| 917 | fun ext_syntax sg = ext_cnsts read_const true ("", true) sg;
 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 918 | fun ext_modesyntax_i sg (prmode, consts) = ext_cnsts no_read true prmode sg consts; | 
| 2197 | 919 | fun ext_modesyntax sg (prmode, consts) = ext_cnsts read_const true prmode sg consts; | 
| 386 | 920 | |
| 921 | ||
| 922 | (* add type classes *) | |
| 923 | ||
| 924 | fun const_of_class c = c ^ "_class"; | |
| 925 | ||
| 926 | fun class_of_const c_class = | |
| 927 | let | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 928 | val c = implode (take (size c_class - size "_class", explode c_class)); | 
| 386 | 929 | in | 
| 930 | if const_of_class c = c_class then c | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 931 |     else raise TERM ("class_of_const: bad name " ^ quote c_class, [])
 | 
| 386 | 932 | end; | 
| 933 | ||
| 934 | ||
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 935 | fun ext_classes int (syn, tsig, ctab, (path, spaces), data) classes = | 
| 386 | 936 | let | 
| 562 | 937 | val names = map fst classes; | 
| 386 | 938 | val consts = | 
| 939 | map (fn c => (const_of_class c, a_itselfT --> propT, NoSyn)) names; | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 940 | |
| 3810 | 941 | val full_names = map (full path) names; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 942 | val spaces' = add_names spaces classK full_names; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 943 | val intrn = if int then map (intrn_class spaces') else I; | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 944 | val classes' = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 945 | ListPair.map (fn (c, (_, cs)) => (c, intrn cs)) (full_names, classes); | 
| 386 | 946 | in | 
| 947 | ext_consts_i | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 948 | (Syntax.extend_consts syn names, | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 949 | Type.ext_tsig_classes tsig classes', ctab, (path, spaces'), data) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 950 | consts | 
| 386 | 951 | end; | 
| 952 | ||
| 953 | ||
| 2963 | 954 | (* add to classrel *) | 
| 421 | 955 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 956 | fun ext_classrel int (syn, tsig, ctab, (path, spaces), data) pairs = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 957 | let val intrn = if int then map (pairself (intrn_class spaces)) else I in | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 958 | (syn, Type.ext_tsig_classrel tsig (intrn pairs), ctab, (path, spaces), data) | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 959 | end; | 
| 421 | 960 | |
| 961 | ||
| 4619 | 962 | (* add translation rules *) | 
| 963 | ||
| 964 | fun ext_trrules (syn, tsig, ctab, (path, spaces), data) args = | |
| 965 | (Syntax.extend_trrules syn | |
| 966 | (map (Syntax.map_trrule (fn (root, str) => (intrn spaces typeK root, str))) args), | |
| 967 | tsig, ctab, (path, spaces), data); | |
| 968 | ||
| 969 | ||
| 1159 | 970 | (* add to syntax *) | 
| 386 | 971 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 972 | fun ext_syn extfun (syn, tsig, ctab, names, data) args = | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 973 | (extfun syn args, tsig, ctab, names, data); | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 974 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 975 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 976 | (* add to path *) | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 977 | |
| 4844 | 978 | fun ext_path (syn, tsig, ctab, (path, spaces), data) elems = | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 979 | let | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 980 | val path' = | 
| 11501 | 981 | if elems = "//" then None | 
| 982 | else if elems = "/" then Some [] | |
| 983 | else if elems = ".." andalso is_some path andalso path <> Some [] then | |
| 984 | Some (fst (split_last (the path))) | |
| 985 | else Some (if_none path [] @ NameSpace.unpack elems); | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 986 | in | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 987 | (syn, tsig, ctab, (path', spaces), data) | 
| 10404 | 988 | end; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 989 | |
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 990 | |
| 8725 | 991 | (* change name space *) | 
| 992 | ||
| 993 | fun ext_add_space (syn, tsig, ctab, (path, spaces), data) (kind, names) = | |
| 994 | (syn, tsig, ctab, (path, add_names spaces kind names), data); | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 995 | |
| 8725 | 996 | fun ext_hide_space (syn, tsig, ctab, (path, spaces), data) (kind, xnames) = | 
| 997 | (syn, tsig, ctab, (path, hide_names spaces kind (map (intrn spaces kind) xnames)), data); | |
| 998 | ||
| 999 | fun ext_hide_space_i (syn, tsig, ctab, (path, spaces), data) (kind, names) = | |
| 1000 | (syn, tsig, ctab, (path, hide_names spaces kind names), data); | |
| 386 | 1001 | |
| 1002 | ||
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1003 | (* signature data *) | 
| 386 | 1004 | |
| 6546 | 1005 | fun ext_init_data sg (syn, tsig, ctab, names, data) (kind, (e, cp, ext, mrg, prt)) = | 
| 1006 | (syn, tsig, ctab, names, init_data_sg sg data kind e cp ext mrg prt); | |
| 386 | 1007 | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1008 | fun ext_put_data sg (syn, tsig, ctab, names, data) (kind, f, x) = | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1009 | (syn, tsig, ctab, names, put_data_sg sg data kind f x); | 
| 386 | 1010 | |
| 1011 | ||
| 6546 | 1012 | fun copy_data (k, (e, mths as (cp, _, _, _))) = | 
| 6961 | 1013 | (k, (cp e handle exn => err_method "copy" (Object.name_of_kind k) exn, mths)); | 
| 6546 | 1014 | |
| 1015 | fun copy (sg as Sg ({id = _, stamps}, {self, tsig, const_tab, syn, path, spaces, data})) =
 | |
| 1016 | let | |
| 1017 | val _ = check_stale sg; | |
| 1018 | val self' = SgRef (Some (ref sg)); | |
| 1019 | val Data tab = data; | |
| 1020 | val data' = Data (Symtab.map copy_data tab); | |
| 1021 | in create_sign self' stamps "#" (syn, tsig, const_tab, (path, spaces), data') end; | |
| 1022 | ||
| 1023 | ||
| 386 | 1024 | (* the external interfaces *) | 
| 1025 | ||
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1026 | val add_classes = extend_sign true (ext_classes true) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1027 | val add_classes_i = extend_sign true (ext_classes false) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1028 | val add_classrel = extend_sign true (ext_classrel true) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1029 | val add_classrel_i = extend_sign true (ext_classrel false) "#"; | 
| 8898 | 1030 | val add_defsort = extend_sign true ext_defsort "#"; | 
| 1031 | val add_defsort_i = extend_sign true ext_defsort_i "#"; | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1032 | val add_types = extend_sign true ext_types "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1033 | val add_nonterminals = extend_sign true ext_nonterminals "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1034 | val add_tyabbrs = extend_sign true ext_tyabbrs "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1035 | val add_tyabbrs_i = extend_sign true ext_tyabbrs_i "#"; | 
| 8898 | 1036 | val add_arities = extend_sign true ext_arities "#"; | 
| 1037 | val add_arities_i = extend_sign true ext_arities_i "#"; | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1038 | val add_consts = extend_sign true ext_consts "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1039 | val add_consts_i = extend_sign true ext_consts_i "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1040 | val add_syntax = extend_sign true ext_syntax "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1041 | val add_syntax_i = extend_sign true ext_syntax_i "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1042 | val add_modesyntax = extend_sign true ext_modesyntax "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1043 | val add_modesyntax_i = extend_sign true ext_modesyntax_i "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1044 | val add_trfuns = extend_sign true (ext_syn Syntax.extend_trfuns) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1045 | val add_trfunsT = extend_sign true (ext_syn Syntax.extend_trfunsT) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1046 | val add_tokentrfuns = extend_sign true (ext_syn Syntax.extend_tokentrfuns) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1047 | val add_trrules = extend_sign true ext_trrules "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1048 | val add_trrules_i = extend_sign true (ext_syn Syntax.extend_trrules_i) "#"; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1049 | val add_path = extend_sign true ext_path "#"; | 
| 8725 | 1050 | val add_space = extend_sign true ext_add_space "#"; | 
| 1051 | val hide_space = extend_sign true ext_hide_space "#"; | |
| 1052 | val hide_space_i = extend_sign true ext_hide_space_i "#"; | |
| 4998 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1053 | fun init_data arg sg = extend_sign true (ext_init_data sg) "#" arg sg; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1054 | fun put_data k f x sg = extend_sign true (ext_put_data sg) "#" (k, f, x) sg; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1055 | fun add_name name sg = extend_sign true K name () sg; | 
| 
28fe46a570d7
improved data: secure version using Object.T and Object.kind;
 wenzelm parents: 
4961diff
changeset | 1056 | fun prep_ext sg = extend_sign false K "#" () sg; | 
| 386 | 1057 | |
| 1058 | ||
| 3867 | 1059 | |
| 10404 | 1060 | (** merge signatures **) (*exception TERM*) | 
| 3867 | 1061 | |
| 4228 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1062 | (* merge_stamps *) | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1063 | |
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1064 | fun merge_stamps stamps1 stamps2 = | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1065 | let val stamps = merge_rev_lists stamps1 stamps2 in | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1066 | (case duplicates (map ! stamps) of | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1067 | [] => stamps | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1068 |     | dups => raise TERM ("Attempt to merge different versions of theories "
 | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1069 | ^ commas_quote dups, [])) | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1070 | end; | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1071 | |
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1072 | |
| 4627 | 1073 | (* implicit merge -- trivial only *) | 
| 3877 | 1074 | |
| 4228 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1075 | fun merge_refs (sgr1 as SgRef (Some (ref (sg1 as Sg ({stamps = s1, ...}, _)))),
 | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1076 |         sgr2 as SgRef (Some (ref (sg2 as Sg ({stamps = s2, ...}, _))))) =
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1077 | if fast_subsig (sg2, sg1) then sgr1 | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1078 | else if fast_subsig (sg1, sg2) then sgr2 | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1079 | else if subsig (sg2, sg1) then sgr1 | 
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1080 | else if subsig (sg1, sg2) then sgr2 | 
| 4228 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1081 | else (merge_stamps s1 s2; (*check for different versions*) | 
| 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1082 |         raise TERM ("Attempt to do non-trivial merge of signatures", []))
 | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1083 | | merge_refs _ = sys_error "Sign.merge_refs"; | 
| 3867 | 1084 | |
| 4627 | 1085 | val merge = deref o merge_refs o pairself self_ref; | 
| 1086 | ||
| 3867 | 1087 | |
| 10404 | 1088 | (* proper merge *) (*exception TERM/ERROR*) | 
| 143 | 1089 | |
| 10932 | 1090 | val PureN = "Pure"; | 
| 1091 | val CPureN = "CPure"; | |
| 1092 | ||
| 6961 | 1093 | fun nontriv_merge (sg1, sg2) = | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1094 | if subsig (sg2, sg1) then sg1 | 
| 251 | 1095 | else if subsig (sg1, sg2) then sg2 | 
| 386 | 1096 | else if is_draft sg1 orelse is_draft sg2 then | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1097 |     raise TERM ("Attempt to merge draft signatures", [])
 | 
| 10932 | 1098 | else if exists_stamp PureN sg1 andalso exists_stamp CPureN sg2 orelse | 
| 1099 | exists_stamp CPureN sg1 andalso exists_stamp PureN sg2 then | |
| 1100 |     raise TERM ("Cannot merge Pure and CPure signatures", [])
 | |
| 251 | 1101 | else | 
| 1102 | (*neither is union already; must form union*) | |
| 1103 | let | |
| 3975 | 1104 |       val Sg ({id = _, stamps = stamps1}, {self = _, tsig = tsig1, const_tab = const_tab1,
 | 
| 1105 | syn = syn1, path = _, spaces = spaces1, data = data1}) = sg1; | |
| 1106 |       val Sg ({id = _, stamps = stamps2}, {self = _, tsig = tsig2, const_tab = const_tab2,
 | |
| 1107 | syn = syn2, path = _, spaces = spaces2, data = data2}) = sg2; | |
| 386 | 1108 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1109 | val id = ref ""; | 
| 3975 | 1110 | val self_ref = ref sg1; (*dummy value*) | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1111 | val self = SgRef (Some self_ref); | 
| 402 | 1112 | |
| 4228 
22e3f0368c85
merge_refs: check for different versions of theories;
 wenzelm parents: 
4227diff
changeset | 1113 | val stamps = merge_stamps stamps1 stamps2; | 
| 2185 | 1114 | val tsig = Type.merge_tsigs (tsig1, tsig2); | 
| 251 | 1115 | val const_tab = Symtab.merge (op =) (const_tab1, const_tab2) | 
| 386 | 1116 | handle Symtab.DUPS cs => | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1117 |           raise TERM ("Incompatible types for constant(s) " ^ commas_quote cs, []);
 | 
| 386 | 1118 | val syn = Syntax.merge_syntaxes syn1 syn2; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1119 | |
| 11501 | 1120 | val path = Some []; | 
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1121 | val kinds = distinct (map fst (spaces1 @ spaces2)); | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1122 | val spaces = | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1123 | kinds ~~ | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1124 | ListPair.map NameSpace.merge | 
| 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1125 | (map (space_of spaces1) kinds, map (space_of spaces2) kinds); | 
| 3867 | 1126 | |
| 4256 | 1127 | val data = merge_data (data1, data2); | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1128 | |
| 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1129 | val sign = make_sign (id, self, tsig, const_tab, syn, path, spaces, data, stamps); | 
| 251 | 1130 | in | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1131 | self_ref := sign; sign | 
| 251 | 1132 | end; | 
| 143 | 1133 | |
| 0 | 1134 | |
| 1135 | ||
| 3995 | 1136 | (** partial Pure signature **) | 
| 0 | 1137 | |
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1138 | val dummy_sg = make_sign (ref "", SgRef None, Type.tsig0, | 
| 11501 | 1139 | Symtab.empty, Syntax.pure_syn, Some [], [], empty_data, []); | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1140 | |
| 3995 | 1141 | val pre_pure = | 
| 3967 
edd5ff9371f8
sg_ref: automatic adjustment of thms of draft theories;
 wenzelm parents: 
3956diff
changeset | 1142 | create_sign (SgRef (Some (ref dummy_sg))) [] "#" | 
| 11501 | 1143 | (Syntax.pure_syn, Type.tsig0, Symtab.empty, (Some [], []), empty_data); | 
| 0 | 1144 | |
| 3791 
c5db2c87a646
now supports qualified names (intern vs. extern) !!!
 wenzelm parents: 
3552diff
changeset | 1145 | |
| 0 | 1146 | end; |