author | wenzelm |
Tue, 25 Mar 2008 19:39:58 +0100 | |
changeset 26392 | 748b263f0e40 |
parent 26327 | fc8df36e2644 |
child 26517 | ef036a63f6e9 |
permissions | -rw-r--r-- |
256 | 1 |
(* Title: Pure/type.ML |
0 | 2 |
ID: $Id$ |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
3 |
Author: Tobias Nipkow, Lawrence C Paulson, and Markus Wenzel |
0 | 4 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
5 |
Type signatures and certified types, special treatment of type vars, |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
6 |
matching and unification of types, extend and merge type signatures. |
0 | 7 |
*) |
8 |
||
9 |
signature TYPE = |
|
2964 | 10 |
sig |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
11 |
(*type signatures and certified types*) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
12 |
datatype decl = |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
13 |
LogicalType of int | |
14989 | 14 |
Abbreviation of string list * typ * bool | |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
15 |
Nonterminal |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
16 |
type tsig |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
17 |
val rep_tsig: tsig -> |
19642 | 18 |
{classes: NameSpace.T * Sorts.algebra, |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
19 |
default: sort, |
20674 | 20 |
types: (decl * serial) NameSpace.table, |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
21 |
log_types: string list, |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
22 |
witness: (typ * sort) option} |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
23 |
val empty_tsig: tsig |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
24 |
val defaultS: tsig -> sort |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
25 |
val logical_types: tsig -> string list |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
26 |
val universal_witness: tsig -> (typ * sort) option |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
27 |
val eq_sort: tsig -> sort * sort -> bool |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
28 |
val subsort: tsig -> sort * sort -> bool |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
29 |
val of_sort: tsig -> typ * sort -> bool |
19464 | 30 |
val inter_sort: tsig -> sort * sort -> sort |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
31 |
val cert_class: tsig -> class -> class |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
32 |
val cert_sort: tsig -> sort -> sort |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
33 |
val witness_sorts: tsig -> sort list -> sort list -> (typ * sort) list |
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
34 |
type mode |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
35 |
val mode_default: mode |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
36 |
val mode_syntax: mode |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
37 |
val mode_abbrev: mode |
24484 | 38 |
val get_mode: Proof.context -> mode |
39 |
val set_mode: mode -> Proof.context -> Proof.context |
|
40 |
val restore_mode: Proof.context -> Proof.context -> Proof.context |
|
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
41 |
val cert_typ_mode: mode -> tsig -> typ -> typ |
14993 | 42 |
val cert_typ: tsig -> typ -> typ |
19464 | 43 |
val arity_number: tsig -> string -> int |
44 |
val arity_sorts: Pretty.pp -> tsig -> string -> sort -> sort list |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
45 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
46 |
(*special treatment of type vars*) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
47 |
val strip_sorts: typ -> typ |
24982 | 48 |
val similar_types: term * term -> bool |
621 | 49 |
val no_tvars: typ -> typ |
21116 | 50 |
val varify: (string * sort) list -> term -> ((string * sort) * indexname) list * term |
16289 | 51 |
val freeze_thaw_type: typ -> typ * (typ -> typ) |
52 |
val freeze_type: typ -> typ |
|
53 |
val freeze_thaw: term -> term * (term -> term) |
|
54 |
val freeze: term -> term |
|
2964 | 55 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
56 |
(*matching and unification*) |
2964 | 57 |
exception TYPE_MATCH |
25324 | 58 |
type tyenv = (sort * typ) Vartab.table |
26327 | 59 |
val lookup: tyenv -> indexname * sort -> typ option |
16946 | 60 |
val typ_match: tsig -> typ * typ -> tyenv -> tyenv |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
61 |
val typ_instance: tsig -> typ * typ -> bool |
26327 | 62 |
val typ_of_sort: Sorts.algebra -> typ -> sort |
63 |
-> sort Vartab.table -> sort Vartab.table |
|
16946 | 64 |
val raw_match: typ * typ -> tyenv -> tyenv |
19694 | 65 |
val raw_matches: typ list * typ list -> tyenv -> tyenv |
16946 | 66 |
val raw_instance: typ * typ -> bool |
2964 | 67 |
exception TUNIFY |
16946 | 68 |
val unify: tsig -> typ * typ -> tyenv * int -> tyenv * int |
69 |
val raw_unify: typ * typ -> tyenv -> tyenv |
|
19696 | 70 |
val raw_unifys: typ list * typ list -> tyenv -> tyenv |
17804 | 71 |
val could_unify: typ * typ -> bool |
19696 | 72 |
val could_unifys: typ list * typ list -> bool |
16650 | 73 |
val eq_type: tyenv -> typ * typ -> bool |
0 | 74 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
75 |
(*extend and merge type signatures*) |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
76 |
val add_class: Pretty.pp -> NameSpace.naming -> bstring * class list -> tsig -> tsig |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
77 |
val hide_classes: bool -> string list -> tsig -> tsig |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
78 |
val set_defsort: sort -> tsig -> tsig |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
79 |
val add_types: NameSpace.naming -> (bstring * int) list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
80 |
val add_abbrevs: NameSpace.naming -> (string * string list * typ) list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
81 |
val add_nonterminals: NameSpace.naming -> string list -> tsig -> tsig |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
82 |
val hide_types: bool -> string list -> tsig -> tsig |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
83 |
val add_arity: Pretty.pp -> arity -> tsig -> tsig |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
84 |
val add_classrel: Pretty.pp -> class * class -> tsig -> tsig |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
85 |
val merge_tsigs: Pretty.pp -> tsig * tsig -> tsig |
2964 | 86 |
end; |
87 |
||
88 |
structure Type: TYPE = |
|
0 | 89 |
struct |
90 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
91 |
(** type signatures and certified types **) |
2964 | 92 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
93 |
(* type declarations *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
94 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
95 |
datatype decl = |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
96 |
LogicalType of int | |
14989 | 97 |
Abbreviation of string list * typ * bool | |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
98 |
Nonterminal; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
99 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
100 |
fun str_of_decl (LogicalType _) = "logical type constructor" |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
101 |
| str_of_decl (Abbreviation _) = "type abbreviation" |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
102 |
| str_of_decl Nonterminal = "syntactic type"; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
103 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
104 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
105 |
(* type tsig *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
106 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
107 |
datatype tsig = |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
108 |
TSig of { |
19642 | 109 |
classes: NameSpace.T * Sorts.algebra, (*order-sorted algebra of type classes*) |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
110 |
default: sort, (*default sort on input*) |
20674 | 111 |
types: (decl * serial) NameSpace.table, (*declared types*) |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
112 |
log_types: string list, (*logical types sorted by number of arguments*) |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
113 |
witness: (typ * sort) option}; (*witness for non-emptiness of strictest sort*) |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
114 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
115 |
fun rep_tsig (TSig comps) = comps; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
116 |
|
19642 | 117 |
fun make_tsig (classes, default, types, log_types, witness) = |
118 |
TSig {classes = classes, default = default, types = types, |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
119 |
log_types = log_types, witness = witness}; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
120 |
|
19642 | 121 |
fun build_tsig ((space, classes), default, types) = |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
122 |
let |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
123 |
val log_types = |
17756 | 124 |
Symtab.fold (fn (c, (LogicalType n, _)) => cons (c, n) | _ => I) (snd types) [] |
125 |
|> Library.sort (Library.int_ord o pairself snd) |> map fst; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
126 |
val witness = |
21934 | 127 |
(case Sorts.witness_sorts classes log_types [] [Sorts.minimal_classes classes] of |
15531 | 128 |
[w] => SOME w | _ => NONE); |
19642 | 129 |
in make_tsig ((space, classes), default, types, log_types, witness) end; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
130 |
|
19642 | 131 |
fun map_tsig f (TSig {classes, default, types, log_types = _, witness = _}) = |
132 |
build_tsig (f (classes, default, types)); |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
133 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
134 |
val empty_tsig = |
19642 | 135 |
build_tsig ((NameSpace.empty, Sorts.empty_algebra), [], NameSpace.empty_table); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
136 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
137 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
138 |
(* classes and sorts *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
139 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
140 |
fun defaultS (TSig {default, ...}) = default; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
141 |
fun logical_types (TSig {log_types, ...}) = log_types; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
142 |
fun universal_witness (TSig {witness, ...}) = witness; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
143 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
144 |
fun eq_sort (TSig {classes, ...}) = Sorts.sort_eq (#2 classes); |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
145 |
fun subsort (TSig {classes, ...}) = Sorts.sort_le (#2 classes); |
19642 | 146 |
fun of_sort (TSig {classes, ...}) = Sorts.of_sort (#2 classes); |
19464 | 147 |
fun inter_sort (TSig {classes, ...}) = Sorts.inter_sort (#2 classes); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
148 |
|
19642 | 149 |
fun cert_class (TSig {classes, ...}) = Sorts.certify_class (#2 classes); |
150 |
fun cert_sort (TSig {classes, ...}) = Sorts.certify_sort (#2 classes); |
|
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
151 |
|
19642 | 152 |
fun witness_sorts (tsig as TSig {classes, log_types, ...}) = |
153 |
Sorts.witness_sorts (#2 classes) log_types; |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
154 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
155 |
|
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
156 |
(* certification mode *) |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
157 |
|
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
158 |
datatype mode = Mode of {normalize: bool, logical: bool}; |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
159 |
|
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
160 |
val mode_default = Mode {normalize = true, logical = true}; |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
161 |
val mode_syntax = Mode {normalize = true, logical = false}; |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
162 |
val mode_abbrev = Mode {normalize = false, logical = false}; |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
163 |
|
24484 | 164 |
structure Mode = ProofDataFun |
165 |
( |
|
166 |
type T = mode; |
|
167 |
fun init _ = mode_default; |
|
168 |
); |
|
169 |
||
170 |
val get_mode = Mode.get; |
|
171 |
fun set_mode mode = Mode.map (K mode); |
|
172 |
fun restore_mode ctxt = set_mode (get_mode ctxt); |
|
173 |
||
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
174 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
175 |
(* certified types *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
176 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
177 |
fun bad_nargs t = "Bad number of arguments for type constructor: " ^ quote t; |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
178 |
fun undecl_type c = "Undeclared type constructor: " ^ quote c; |
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
179 |
|
14998 | 180 |
local |
181 |
||
182 |
fun inst_typ env (Type (c, Ts)) = Type (c, map (inst_typ env) Ts) |
|
18957 | 183 |
| inst_typ env (T as TFree (x, _)) = the_default T (AList.lookup (op =) env x) |
14998 | 184 |
| inst_typ _ T = T; |
185 |
||
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
186 |
in |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
187 |
|
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
188 |
fun cert_typ_mode (Mode {normalize, logical}) tsig ty = |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
189 |
let |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
190 |
val TSig {types = (_, types), ...} = tsig; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
191 |
fun err msg = raise TYPE (msg, [ty], []); |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
192 |
|
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
193 |
val check_logical = |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
194 |
if logical then fn c => err ("Illegal occurrence of syntactic type: " ^ quote c) |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
195 |
else fn _ => (); |
14989 | 196 |
|
197 |
fun cert (T as Type (c, Ts)) = |
|
198 |
let |
|
199 |
val Ts' = map cert Ts; |
|
200 |
fun nargs n = if length Ts <> n then err (bad_nargs c) else (); |
|
201 |
in |
|
17412 | 202 |
(case Symtab.lookup types c of |
15531 | 203 |
SOME (LogicalType n, _) => (nargs n; Type (c, Ts')) |
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
204 |
| SOME (Abbreviation (vs, U, syn), _) => |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
205 |
(nargs (length vs); |
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
206 |
if syn then check_logical c else (); |
14993 | 207 |
if normalize then inst_typ (vs ~~ Ts') U |
14989 | 208 |
else Type (c, Ts')) |
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
209 |
| SOME (Nonterminal, _) => (nargs 0; check_logical c; T) |
15531 | 210 |
| NONE => err (undecl_type c)) |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
211 |
end |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
212 |
| cert (TFree (x, S)) = TFree (x, cert_sort tsig S) |
14989 | 213 |
| cert (TVar (xi as (_, i), S)) = |
14993 | 214 |
if i < 0 then |
215 |
err ("Malformed type variable: " ^ quote (Term.string_of_vname xi)) |
|
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
216 |
else TVar (xi, cert_sort tsig S); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
217 |
|
14989 | 218 |
val ty' = cert ty; |
14993 | 219 |
in if ty = ty' then ty else ty' end; (*avoid copying of already normal type*) |
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
220 |
|
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
221 |
val cert_typ = cert_typ_mode mode_default; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
222 |
|
14998 | 223 |
end; |
224 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
225 |
|
19464 | 226 |
(* type arities *) |
227 |
||
228 |
fun arity_number (TSig {types = (_, types), ...}) a = |
|
229 |
(case Symtab.lookup types a of |
|
230 |
SOME (LogicalType n, _) => n |
|
231 |
| _ => error (undecl_type a)); |
|
232 |
||
233 |
fun arity_sorts _ tsig a [] = replicate (arity_number tsig a) [] |
|
19642 | 234 |
| arity_sorts pp (TSig {classes, ...}) a S = Sorts.mg_domain (#2 classes) a S |
235 |
handle Sorts.CLASS_ERROR err => Sorts.class_error pp err; |
|
19464 | 236 |
|
237 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
238 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
239 |
(** special treatment of type vars **) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
240 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
241 |
(* strip_sorts *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
242 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
243 |
fun strip_sorts (Type (a, Ts)) = Type (a, map strip_sorts Ts) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
244 |
| strip_sorts (TFree (x, _)) = TFree (x, []) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
245 |
| strip_sorts (TVar (xi, _)) = TVar (xi, []); |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
246 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
247 |
|
25384
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents:
25324
diff
changeset
|
248 |
(* equivalence up to renaming of atomic types *) |
24982 | 249 |
|
250 |
local |
|
251 |
||
252 |
fun standard_types t = |
|
253 |
let |
|
25384
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents:
25324
diff
changeset
|
254 |
val Ts = fold_types (fold_atyps (insert (op =))) t []; |
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents:
25324
diff
changeset
|
255 |
val Ts' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length Ts)); |
7b9dfd4774a6
similar_types: uniform treatment of TFrees/TVars;
wenzelm
parents:
25324
diff
changeset
|
256 |
in map_types (map_atyps (perhaps (AList.lookup (op =) (Ts ~~ Ts')))) t end; |
24982 | 257 |
|
258 |
in |
|
259 |
||
260 |
val similar_types = op aconv o pairself (Term.map_types strip_sorts o standard_types); |
|
261 |
||
262 |
end; |
|
263 |
||
264 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
265 |
(* no_tvars *) |
621 | 266 |
|
267 |
fun no_tvars T = |
|
12501 | 268 |
(case typ_tvars T of [] => T |
269 |
| vs => raise TYPE ("Illegal schematic type variable(s): " ^ |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
270 |
commas_quote (map (Term.string_of_vname o #1) vs), [T], [])); |
621 | 271 |
|
7641 | 272 |
|
19806 | 273 |
(* varify *) |
621 | 274 |
|
21116 | 275 |
fun varify fixed t = |
621 | 276 |
let |
19305 | 277 |
val fs = Term.fold_types (Term.fold_atyps |
278 |
(fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t []; |
|
621 | 279 |
val ixns = add_term_tvar_ixns (t, []); |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19806
diff
changeset
|
280 |
val fmap = fs ~~ map (rpair 0) (Name.variant_list (map #1 ixns) (map fst fs)) |
2964 | 281 |
fun thaw (f as (a, S)) = |
17184 | 282 |
(case AList.lookup (op =) fmap f of |
15531 | 283 |
NONE => TFree f |
16946 | 284 |
| SOME xi => TVar (xi, S)); |
21116 | 285 |
in (fmap, map_types (map_type_tfree thaw) t) end; |
2964 | 286 |
|
287 |
||
7641 | 288 |
(* freeze_thaw: freeze TVars in a term; return the "thaw" inverse *) |
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
289 |
|
7641 | 290 |
local |
291 |
||
16289 | 292 |
fun new_name (ix, (pairs, used)) = |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19806
diff
changeset
|
293 |
let val v = Name.variant used (string_of_indexname ix) |
16289 | 294 |
in ((ix, v) :: pairs, v :: used) end; |
621 | 295 |
|
16289 | 296 |
fun freeze_one alist (ix, sort) = |
17184 | 297 |
TFree (the (AList.lookup (op =) alist ix), sort) |
15531 | 298 |
handle Option => |
3790 | 299 |
raise TYPE ("Failure during freezing of ?" ^ string_of_indexname ix, [], []); |
2964 | 300 |
|
17184 | 301 |
fun thaw_one alist (a, sort) = TVar (the (AList.lookup (op =) alist a), sort) |
16289 | 302 |
handle Option => TFree (a, sort); |
416 | 303 |
|
10495 | 304 |
in |
305 |
||
306 |
(*this sort of code could replace unvarifyT*) |
|
7641 | 307 |
fun freeze_thaw_type T = |
308 |
let |
|
309 |
val used = add_typ_tfree_names (T, []) |
|
310 |
and tvars = map #1 (add_typ_tvars (T, [])); |
|
23178 | 311 |
val (alist, _) = List.foldr new_name ([], used) tvars; |
7641 | 312 |
in (map_type_tvar (freeze_one alist) T, map_type_tfree (thaw_one (map swap alist))) end; |
313 |
||
16289 | 314 |
val freeze_type = #1 o freeze_thaw_type; |
315 |
||
3411
163f8f4a42d7
Removal of freeze_vars and thaw_vars. New freeze_thaw
paulson
parents:
3175
diff
changeset
|
316 |
fun freeze_thaw t = |
7641 | 317 |
let |
318 |
val used = it_term_types add_typ_tfree_names (t, []) |
|
319 |
and tvars = map #1 (it_term_types add_typ_tvars (t, [])); |
|
23178 | 320 |
val (alist, _) = List.foldr new_name ([], used) tvars; |
7641 | 321 |
in |
322 |
(case alist of |
|
323 |
[] => (t, fn x => x) (*nothing to do!*) |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20071
diff
changeset
|
324 |
| _ => (map_types (map_type_tvar (freeze_one alist)) t, |
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20071
diff
changeset
|
325 |
map_types (map_type_tfree (thaw_one (map swap alist))))) |
7641 | 326 |
end; |
327 |
||
16289 | 328 |
val freeze = #1 o freeze_thaw; |
329 |
||
7641 | 330 |
end; |
331 |
||
256 | 332 |
|
333 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
334 |
(** matching and unification of types **) |
8899 | 335 |
|
15797 | 336 |
type tyenv = (sort * typ) Vartab.table; |
256 | 337 |
|
15797 | 338 |
fun tvar_clash ixn S S' = raise TYPE ("Type variable " ^ |
339 |
quote (Term.string_of_vname ixn) ^ " has two distinct sorts", |
|
340 |
[TVar (ixn, S), TVar (ixn, S')], []); |
|
0 | 341 |
|
26327 | 342 |
fun lookup tye (ixn, S) = |
17412 | 343 |
(case Vartab.lookup tye ixn of |
15797 | 344 |
NONE => NONE |
16289 | 345 |
| SOME (S', T) => if S = S' then SOME T else tvar_clash ixn S S'); |
346 |
||
0 | 347 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
348 |
(* matching *) |
0 | 349 |
|
2964 | 350 |
exception TYPE_MATCH; |
0 | 351 |
|
26327 | 352 |
fun typ_of_sort algebra = |
353 |
let |
|
354 |
val inters = Sorts.inter_sort algebra; |
|
355 |
fun of_sort _ [] = I |
|
356 |
| of_sort (TVar (v, S)) S' = Vartab.map_default (v, []) |
|
357 |
(fn S'' => inters (S, inters (S', S''))) |
|
358 |
| of_sort (TFree (_, S)) S' = if Sorts.sort_le algebra (S, S') then I |
|
359 |
else raise Sorts.CLASS_ERROR (Sorts.NoSubsort (S, S')) |
|
360 |
| of_sort (Type (a, Ts)) S = |
|
361 |
fold2 of_sort Ts (Sorts.mg_domain algebra a S) |
|
362 |
in of_sort end; |
|
363 |
||
16946 | 364 |
fun typ_match tsig = |
2964 | 365 |
let |
16340 | 366 |
fun match (TVar (v, S), T) subs = |
26327 | 367 |
(case lookup subs (v, S) of |
15531 | 368 |
NONE => |
17412 | 369 |
if of_sort tsig (T, S) then Vartab.update_new (v, (S, T)) subs |
14993 | 370 |
else raise TYPE_MATCH |
15531 | 371 |
| SOME U => if U = T then subs else raise TYPE_MATCH) |
16340 | 372 |
| match (Type (a, Ts), Type (b, Us)) subs = |
2964 | 373 |
if a <> b then raise TYPE_MATCH |
16885 | 374 |
else matches (Ts, Us) subs |
16340 | 375 |
| match (TFree x, TFree y) subs = |
2964 | 376 |
if x = y then subs else raise TYPE_MATCH |
16885 | 377 |
| match _ _ = raise TYPE_MATCH |
378 |
and matches (T :: Ts, U :: Us) subs = matches (Ts, Us) (match (T, U) subs) |
|
379 |
| matches _ subs = subs; |
|
16946 | 380 |
in match end; |
0 | 381 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
382 |
fun typ_instance tsig (T, U) = |
16946 | 383 |
(typ_match tsig (U, T) Vartab.empty; true) handle TYPE_MATCH => false; |
384 |
||
385 |
(*purely structural matching*) |
|
386 |
fun raw_match (TVar (v, S), T) subs = |
|
26327 | 387 |
(case lookup subs (v, S) of |
17412 | 388 |
NONE => Vartab.update_new (v, (S, T)) subs |
16946 | 389 |
| SOME U => if U = T then subs else raise TYPE_MATCH) |
390 |
| raw_match (Type (a, Ts), Type (b, Us)) subs = |
|
391 |
if a <> b then raise TYPE_MATCH |
|
392 |
else raw_matches (Ts, Us) subs |
|
393 |
| raw_match (TFree x, TFree y) subs = |
|
394 |
if x = y then subs else raise TYPE_MATCH |
|
395 |
| raw_match _ _ = raise TYPE_MATCH |
|
396 |
and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs) |
|
19694 | 397 |
| raw_matches ([], []) subs = subs |
398 |
| raw_matches _ _ = raise TYPE_MATCH; |
|
16946 | 399 |
|
400 |
fun raw_instance (T, U) = |
|
401 |
(raw_match (U, T) Vartab.empty; true) handle TYPE_MATCH => false; |
|
2964 | 402 |
|
0 | 403 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
404 |
(* unification *) |
2964 | 405 |
|
0 | 406 |
exception TUNIFY; |
407 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
408 |
(*occurs_check*) |
2964 | 409 |
fun occurs v tye = |
410 |
let |
|
411 |
fun occ (Type (_, Ts)) = exists occ Ts |
|
412 |
| occ (TFree _) = false |
|
15797 | 413 |
| occ (TVar (w, S)) = |
2964 | 414 |
eq_ix (v, w) orelse |
26327 | 415 |
(case lookup tye (w, S) of |
15531 | 416 |
NONE => false |
417 |
| SOME U => occ U); |
|
0 | 418 |
in occ end; |
419 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
420 |
(*chase variable assignments; if devar returns a type var then it must be unassigned*) |
16885 | 421 |
fun devar tye (T as TVar v) = |
26327 | 422 |
(case lookup tye v of |
16885 | 423 |
SOME U => devar tye U |
15531 | 424 |
| NONE => T) |
16885 | 425 |
| devar tye T = T; |
0 | 426 |
|
17804 | 427 |
(*order-sorted unification*) |
19642 | 428 |
fun unify (tsig as TSig {classes = (_, classes), ...}) TU (tyenv, maxidx) = |
2964 | 429 |
let |
430 |
val tyvar_count = ref maxidx; |
|
24848 | 431 |
fun gen_tyvar S = TVar ((Name.aT, inc tyvar_count), S); |
2964 | 432 |
|
19642 | 433 |
fun mg_domain a S = Sorts.mg_domain classes a S |
434 |
handle Sorts.CLASS_ERROR _ => raise TUNIFY; |
|
2964 | 435 |
|
16885 | 436 |
fun meet (_, []) tye = tye |
437 |
| meet (TVar (xi, S'), S) tye = |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
438 |
if Sorts.sort_le classes (S', S) then tye |
17412 | 439 |
else Vartab.update_new |
17221 | 440 |
(xi, (S', gen_tyvar (Sorts.inter_sort classes (S', S)))) tye |
16885 | 441 |
| meet (TFree (_, S'), S) tye = |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
442 |
if Sorts.sort_le classes (S', S) then tye |
2964 | 443 |
else raise TUNIFY |
16885 | 444 |
| meet (Type (a, Ts), S) tye = meets (Ts, mg_domain a S) tye |
445 |
and meets (T :: Ts, S :: Ss) tye = meets (Ts, Ss) (meet (devar tye T, S) tye) |
|
446 |
| meets _ tye = tye; |
|
2964 | 447 |
|
16885 | 448 |
fun unif (ty1, ty2) tye = |
449 |
(case (devar tye ty1, devar tye ty2) of |
|
2964 | 450 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
15797 | 451 |
if eq_ix (v, w) then |
452 |
if S1 = S2 then tye else tvar_clash v S1 S2 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
453 |
else if Sorts.sort_le classes (S1, S2) then |
17412 | 454 |
Vartab.update_new (w, (S2, T)) tye |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
455 |
else if Sorts.sort_le classes (S2, S1) then |
17412 | 456 |
Vartab.update_new (v, (S1, U)) tye |
2964 | 457 |
else |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
458 |
let val S = gen_tyvar (Sorts.inter_sort classes (S1, S2)) in |
17412 | 459 |
Vartab.update_new (v, (S1, S)) (Vartab.update_new (w, (S2, S)) tye) |
2964 | 460 |
end |
461 |
| (TVar (v, S), T) => |
|
462 |
if occurs v tye T then raise TUNIFY |
|
17412 | 463 |
else meet (T, S) (Vartab.update_new (v, (S, T)) tye) |
2964 | 464 |
| (T, TVar (v, S)) => |
465 |
if occurs v tye T then raise TUNIFY |
|
17412 | 466 |
else meet (T, S) (Vartab.update_new (v, (S, T)) tye) |
2964 | 467 |
| (Type (a, Ts), Type (b, Us)) => |
468 |
if a <> b then raise TUNIFY |
|
16885 | 469 |
else unifs (Ts, Us) tye |
470 |
| (T, U) => if T = U then tye else raise TUNIFY) |
|
471 |
and unifs (T :: Ts, U :: Us) tye = unifs (Ts, Us) (unif (T, U) tye) |
|
472 |
| unifs _ tye = tye; |
|
473 |
in (unif TU tyenv, ! tyvar_count) end; |
|
0 | 474 |
|
16946 | 475 |
(*purely structural unification*) |
476 |
fun raw_unify (ty1, ty2) tye = |
|
477 |
(case (devar tye ty1, devar tye ty2) of |
|
478 |
(T as TVar (v, S1), U as TVar (w, S2)) => |
|
479 |
if eq_ix (v, w) then |
|
480 |
if S1 = S2 then tye else tvar_clash v S1 S2 |
|
17412 | 481 |
else Vartab.update_new (w, (S2, T)) tye |
16946 | 482 |
| (TVar (v, S), T) => |
483 |
if occurs v tye T then raise TUNIFY |
|
17412 | 484 |
else Vartab.update_new (v, (S, T)) tye |
16946 | 485 |
| (T, TVar (v, S)) => |
486 |
if occurs v tye T then raise TUNIFY |
|
17412 | 487 |
else Vartab.update_new (v, (S, T)) tye |
16946 | 488 |
| (Type (a, Ts), Type (b, Us)) => |
489 |
if a <> b then raise TUNIFY |
|
490 |
else raw_unifys (Ts, Us) tye |
|
491 |
| (T, U) => if T = U then tye else raise TUNIFY) |
|
492 |
and raw_unifys (T :: Ts, U :: Us) tye = raw_unifys (Ts, Us) (raw_unify (T, U) tye) |
|
19696 | 493 |
| raw_unifys ([], []) tye = tye |
494 |
| raw_unifys _ _ = raise TUNIFY; |
|
16946 | 495 |
|
17804 | 496 |
(*fast unification filter*) |
497 |
fun could_unify (Type (a, Ts), Type (b, Us)) = a = b andalso could_unifys (Ts, Us) |
|
498 |
| could_unify (TFree (a, _), TFree (b, _)) = a = b |
|
499 |
| could_unify (TVar _, _) = true |
|
500 |
| could_unify (_, TVar _) = true |
|
501 |
| could_unify _ = false |
|
502 |
and could_unifys (T :: Ts, U :: Us) = could_unify (T, U) andalso could_unifys (Ts, Us) |
|
19696 | 503 |
| could_unifys ([], []) = true |
504 |
| could_unifys _ = false; |
|
450 | 505 |
|
17804 | 506 |
|
507 |
(*equality with respect to a type environment*) |
|
16650 | 508 |
fun eq_type tye (T, T') = |
16885 | 509 |
(case (devar tye T, devar tye T') of |
16650 | 510 |
(Type (s, Ts), Type (s', Ts')) => |
511 |
s = s' andalso ListPair.all (eq_type tye) (Ts, Ts') |
|
512 |
| (U, U') => U = U'); |
|
513 |
||
450 | 514 |
|
0 | 515 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
516 |
(** extend and merge type signatures **) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
517 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
518 |
(* classes *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
519 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
520 |
fun add_class pp naming (c, cs) tsig = |
19642 | 521 |
tsig |> map_tsig (fn ((space, classes), default, types) => |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
522 |
let |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
523 |
val c' = NameSpace.full naming c; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
524 |
val cs' = map (cert_class tsig) cs |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
525 |
handle TYPE (msg, _, _) => error msg; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
526 |
val space' = space |> NameSpace.declare naming c'; |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
527 |
val classes' = classes |> Sorts.add_class pp (c', cs'); |
19642 | 528 |
in ((space', classes'), default, types) end); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
529 |
|
19642 | 530 |
fun hide_classes fully cs = map_tsig (fn ((space, classes), default, types) => |
531 |
((fold (NameSpace.hide fully) cs space, classes), default, types)); |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
532 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
533 |
|
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
534 |
(* arities *) |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
535 |
|
19642 | 536 |
fun add_arity pp (t, Ss, S) tsig = tsig |> map_tsig (fn ((space, classes), default, types) => |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
537 |
let |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
538 |
val _ = |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
539 |
(case Symtab.lookup (#2 types) t of |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
540 |
SOME (LogicalType n, _) => if length Ss <> n then error (bad_nargs t) else () |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
541 |
| SOME (decl, _) => error ("Illegal " ^ str_of_decl decl ^ ": " ^ quote t) |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
542 |
| NONE => error (undecl_type t)); |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
543 |
val (Ss', S') = (map (cert_sort tsig) Ss, cert_sort tsig S) |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
544 |
handle TYPE (msg, _, _) => error msg; |
19642 | 545 |
val classes' = classes |> Sorts.add_arities pp ((t, map (fn c' => (c', Ss')) S')); |
546 |
in ((space, classes'), default, types) end); |
|
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
547 |
|
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
548 |
|
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
549 |
(* classrel *) |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
550 |
|
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
551 |
fun add_classrel pp rel tsig = |
19642 | 552 |
tsig |> map_tsig (fn ((space, classes), default, types) => |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
553 |
let |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
554 |
val rel' = pairself (cert_class tsig) rel |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
555 |
handle TYPE (msg, _, _) => error msg; |
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
556 |
val classes' = classes |> Sorts.add_classrel pp rel; |
19642 | 557 |
in ((space, classes'), default, types) end); |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
558 |
|
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
559 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
560 |
(* default sort *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
561 |
|
19642 | 562 |
fun set_defsort S tsig = tsig |> map_tsig (fn (classes, _, types) => |
563 |
(classes, cert_sort tsig S handle TYPE (msg, _, _) => error msg, types)); |
|
0 | 564 |
|
565 |
||
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
566 |
(* types *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
567 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
568 |
local |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
569 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
570 |
fun err_neg_args c = |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
571 |
error ("Negative number of arguments in type constructor declaration: " ^ quote c); |
949
83c588d6fee9
Changed treatment of during type inference internally generated type
nipkow
parents:
621
diff
changeset
|
572 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
573 |
fun err_in_decls c decl decl' = |
14906 | 574 |
let val s = str_of_decl decl and s' = str_of_decl decl' in |
575 |
if s = s' then error ("Duplicate declaration of " ^ s ^ ": " ^ quote c) |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
576 |
else error ("Conflict of " ^ s ^ " with " ^ s' ^ ": " ^ quote c) |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
577 |
end; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
578 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
579 |
fun new_decl naming (c, decl) (space, types) = |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
580 |
let |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
581 |
val c' = NameSpace.full naming c; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
582 |
val space' = NameSpace.declare naming c' space; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
583 |
val types' = |
17412 | 584 |
(case Symtab.lookup types c' of |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
585 |
SOME (decl', _) => err_in_decls c' decl decl' |
20674 | 586 |
| NONE => Symtab.update (c', (decl, serial ())) types); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
587 |
in (space', types') end; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
588 |
|
17412 | 589 |
fun the_decl (_, types) = fst o the o Symtab.lookup types; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
590 |
|
19642 | 591 |
fun map_types f = map_tsig (fn (classes, default, types) => |
19579
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents:
19530
diff
changeset
|
592 |
let |
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents:
19530
diff
changeset
|
593 |
val (space', tab') = f types; |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21116
diff
changeset
|
594 |
val _ = NameSpace.intern space' "dummy" = "dummy" orelse |
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21116
diff
changeset
|
595 |
error "Illegal declaration of dummy type"; |
19642 | 596 |
in (classes, default, (space', tab')) end); |
2964 | 597 |
|
14989 | 598 |
fun syntactic types (Type (c, Ts)) = |
17412 | 599 |
(case Symtab.lookup types c of SOME (Nonterminal, _) => true | _ => false) |
14989 | 600 |
orelse exists (syntactic types) Ts |
601 |
| syntactic _ _ = false; |
|
602 |
||
19579
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents:
19530
diff
changeset
|
603 |
fun add_abbrev naming (a, vs, rhs) tsig = tsig |> map_types (fn types => |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
604 |
let |
19250 | 605 |
fun err msg = cat_error msg ("The error(s) above occurred in type abbreviation: " ^ quote a); |
24274
cb9236269af1
type mode: models certification mode (default, syntax, abbrev);
wenzelm
parents:
23655
diff
changeset
|
606 |
val rhs' = strip_sorts (no_tvars (cert_typ_mode mode_syntax tsig rhs)) |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
607 |
handle TYPE (msg, _, _) => err msg; |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
608 |
in |
18964 | 609 |
(case duplicates (op =) vs of |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
610 |
[] => [] |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
611 |
| dups => err ("Duplicate variables on lhs: " ^ commas_quote dups)); |
20951
868120282837
gen_rem(s) abandoned in favour of remove / subtract
haftmann
parents:
20674
diff
changeset
|
612 |
(case subtract (op =) vs (map (#1 o #1) (typ_tvars rhs')) of |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
613 |
[] => [] |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
614 |
| extras => err ("Extra variables on rhs: " ^ commas_quote extras)); |
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
615 |
types |> new_decl naming (a, Abbreviation (vs, rhs', syntactic (#2 types) rhs')) |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
616 |
end); |
0 | 617 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
618 |
in |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
619 |
|
19579
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents:
19530
diff
changeset
|
620 |
fun add_types naming ps = map_types (fold (new_decl naming) (ps |> map (fn (c, n) => |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
621 |
if n < 0 then err_neg_args c else (c, LogicalType n)))); |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
622 |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
623 |
val add_abbrevs = fold o add_abbrev; |
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
624 |
|
19579
b802d1804b77
replaced Sorts.DOMAIN by general Sorts.CLASS_ERROR;
wenzelm
parents:
19530
diff
changeset
|
625 |
fun add_nonterminals naming = map_types o fold (new_decl naming) o map (rpair Nonterminal); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
626 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
627 |
fun merge_types (types1, types2) = |
20674 | 628 |
NameSpace.merge_tables (Library.eq_snd (op = : serial * serial -> bool)) (types1, types2) |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
23178
diff
changeset
|
629 |
handle Symtab.DUP d => err_in_decls d (the_decl types1 d) (the_decl types2 d); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
630 |
|
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
631 |
end; |
0 | 632 |
|
19642 | 633 |
fun hide_types fully cs = map_tsig (fn (classes, default, (space, types)) => |
634 |
(classes, default, (fold (NameSpace.hide fully) cs space, types))); |
|
16370
033d890fe91f
name space of classes and types maintained in tsig;
wenzelm
parents:
16340
diff
changeset
|
635 |
|
3790 | 636 |
|
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
637 |
(* merge type signatures *) |
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
638 |
|
14830
faa4865ba1ce
removed norm_typ; improved output; refer to Pretty.pp;
wenzelm
parents:
14790
diff
changeset
|
639 |
fun merge_tsigs pp (tsig1, tsig2) = |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
640 |
let |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
641 |
val (TSig {classes = (space1, classes1), default = default1, types = types1, |
19642 | 642 |
log_types = _, witness = _}) = tsig1; |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
643 |
val (TSig {classes = (space2, classes2), default = default2, types = types2, |
19642 | 644 |
log_types = _, witness = _}) = tsig2; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
645 |
|
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
646 |
val space' = NameSpace.merge (space1, space2); |
19642 | 647 |
val classes' = Sorts.merge_algebra pp (classes1, classes2); |
19515
9f650083da65
build classes/arities: refer to operations in sorts.ML;
wenzelm
parents:
19464
diff
changeset
|
648 |
val default' = Sorts.inter_sort classes' (default1, default2); |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
649 |
val types' = merge_types (types1, types2); |
19642 | 650 |
in build_tsig ((space', classes'), default', types') end; |
14790
0d984ee030a1
major cleanup of tsig datastructures and extend/merge operations; fixes old bugs in classes/arities code; proper treatment of nonterminals and syntax-only types;
wenzelm
parents:
13666
diff
changeset
|
651 |
|
0 | 652 |
end; |