author | wenzelm |
Fri, 28 Feb 1997 16:45:38 +0100 | |
changeset 2699 | 932fae4271d7 |
parent 2678 | d5fe793293ac |
child 3778 | b70c41bc7491 |
permissions | -rw-r--r-- |
18 | 1 |
(* Title: Pure/Syntax/type_ext.ML |
0 | 2 |
ID: $Id$ |
3 |
Author: Tobias Nipkow and Markus Wenzel, TU Muenchen |
|
4 |
||
2584 | 5 |
Utilities for input and output of types. Also the concrete syntax of |
6 |
types, which is used to bootstrap Pure. |
|
0 | 7 |
*) |
8 |
||
9 |
signature TYPE_EXT0 = |
|
2584 | 10 |
sig |
11 |
val raw_term_sorts: (sort * sort -> bool) -> term -> (indexname * sort) list |
|
12 |
val typ_of_term: (indexname -> sort) -> term -> typ |
|
13 |
end; |
|
0 | 14 |
|
15 |
signature TYPE_EXT = |
|
2584 | 16 |
sig |
0 | 17 |
include TYPE_EXT0 |
2584 | 18 |
val term_of_sort: sort -> term |
1511 | 19 |
val term_of_typ: bool -> typ -> term |
20 |
val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
|
21 |
val type_ext: SynExt.syn_ext |
|
2584 | 22 |
end; |
0 | 23 |
|
1511 | 24 |
structure TypeExt : TYPE_EXT = |
0 | 25 |
struct |
2584 | 26 |
|
1511 | 27 |
open Lexicon SynExt Ast; |
0 | 28 |
|
2584 | 29 |
|
30 |
(** input utils **) |
|
0 | 31 |
|
2584 | 32 |
(* raw_term_sorts *) |
33 |
||
34 |
fun raw_term_sorts eq_sort tm = |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
35 |
let |
18 | 36 |
fun classes (Const (c, _)) = [c] |
37 |
| classes (Free (c, _)) = [c] |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
38 |
| classes (Const ("_classes", _) $ Const (c, _) $ cls) = c :: classes cls |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
39 |
| classes (Const ("_classes", _) $ Free (c, _) $ cls) = c :: classes cls |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
40 |
| classes tm = raise_term "raw_term_sorts: bad encoding of classes" [tm]; |
0 | 41 |
|
2584 | 42 |
fun sort (Const ("_topsort", _)) = [] |
43 |
| sort (Const (c, _)) = [c] |
|
44 |
| sort (Free (c, _)) = [c] |
|
18 | 45 |
| sort (Const ("_sort", _) $ cls) = classes cls |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
46 |
| sort tm = raise_term "raw_term_sorts: bad encoding of sort" [tm]; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
47 |
|
2584 | 48 |
fun eq ((xi, S), (xi', S')) = |
49 |
xi = xi' andalso eq_sort (S, S'); |
|
50 |
||
51 |
fun env_of (Const ("_ofsort", _) $ Free (x, _) $ cls) = [((x, ~1), sort cls)] |
|
52 |
| env_of (Const ("_ofsort", _) $ Var (xi, _) $ cls) = [(xi, sort cls)] |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
53 |
| env_of (Abs (_, _, t)) = env_of t |
2584 | 54 |
| env_of (t1 $ t2) = gen_union eq (env_of t1, env_of t2) |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
55 |
| env_of t = []; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
56 |
|
2584 | 57 |
val env = env_of tm; |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
58 |
in |
2584 | 59 |
(case gen_duplicates eq_fst env of |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
60 |
[] => env |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
61 |
| dups => error ("Inconsistent sort constraints for type variable(s) " ^ |
2584 | 62 |
commas (map (quote o string_of_vname' o #1) dups))) |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
63 |
end; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
64 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
65 |
|
2584 | 66 |
(* typ_of_term *) |
0 | 67 |
|
2584 | 68 |
fun typ_of_term get_sort t = |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
69 |
let |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
70 |
fun typ_of (Free (x, _)) = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
71 |
if is_tid x then TFree (x, get_sort (x, ~1)) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
72 |
else Type (x, []) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
73 |
| typ_of (Var (xi, _)) = TVar (xi, get_sort xi) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
74 |
| typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
75 |
TFree (x, get_sort (x, ~1)) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
76 |
| typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
77 |
TVar (xi, get_sort xi) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
78 |
| typ_of tm = |
0 | 79 |
let |
80 |
val (t, ts) = strip_comb tm; |
|
81 |
val a = |
|
18 | 82 |
(case t of |
0 | 83 |
Const (x, _) => x |
84 |
| Free (x, _) => x |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
85 |
| _ => raise_term "typ_of_term: bad encoding of type" [tm]); |
0 | 86 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
87 |
Type (a, map typ_of ts) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
88 |
end; |
0 | 89 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
90 |
typ_of t |
0 | 91 |
end; |
92 |
||
93 |
||
94 |
||
2584 | 95 |
(** output utils **) |
96 |
||
2699 | 97 |
(* term_of_sort *) |
2584 | 98 |
|
99 |
fun term_of_sort S = |
|
100 |
let |
|
2699 | 101 |
fun class c = const "_class" $ free c; |
2584 | 102 |
|
103 |
fun classes [] = sys_error "term_of_sort" |
|
104 |
| classes [c] = class c |
|
105 |
| classes (c :: cs) = const "_classes" $ class c $ classes cs; |
|
106 |
in |
|
107 |
(case S of |
|
108 |
[] => const "_topsort" |
|
109 |
| [c] => class c |
|
110 |
| cs => const "_sort" $ classes cs) |
|
111 |
end; |
|
112 |
||
113 |
||
114 |
(* term_of_typ *) |
|
0 | 115 |
|
116 |
fun term_of_typ show_sorts ty = |
|
117 |
let |
|
2584 | 118 |
fun of_sort t S = |
119 |
if show_sorts then const "_ofsort" $ t $ term_of_sort S |
|
120 |
else t; |
|
0 | 121 |
|
2699 | 122 |
fun term_of (Type (a, Ts)) = list_comb (const a, map term_of Ts) |
123 |
| term_of (TFree (x, S)) = of_sort (const "_tfree" $ free x) S |
|
124 |
| term_of (TVar (xi, S)) = of_sort (const "_tvar" $ var xi) S; |
|
0 | 125 |
in |
18 | 126 |
term_of ty |
0 | 127 |
end; |
128 |
||
129 |
||
130 |
||
131 |
(** the type syntax **) |
|
132 |
||
18 | 133 |
(* parse ast translations *) |
0 | 134 |
|
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
135 |
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Appl [f, ty] |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
136 |
| tapp_ast_tr (*"_tapp"*) asts = raise_ast "tapp_ast_tr" asts; |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
137 |
|
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
138 |
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] = |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
139 |
Appl (f :: ty :: unfold_ast "_types" tys) |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
140 |
| tappl_ast_tr (*"_tappl"*) asts = raise_ast "tappl_ast_tr" asts; |
0 | 141 |
|
142 |
fun bracket_ast_tr (*"_bracket"*) [dom, cod] = |
|
143 |
fold_ast_p "fun" (unfold_ast "_types" dom, cod) |
|
144 |
| bracket_ast_tr (*"_bracket"*) asts = raise_ast "bracket_ast_tr" asts; |
|
145 |
||
146 |
||
18 | 147 |
(* print ast translations *) |
0 | 148 |
|
149 |
fun tappl_ast_tr' (f, []) = raise_ast "tappl_ast_tr'" [f] |
|
150 |
| tappl_ast_tr' (f, [ty]) = Appl [Constant "_tapp", ty, f] |
|
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
151 |
| tappl_ast_tr' (f, ty :: tys) = |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
152 |
Appl [Constant "_tappl", ty, fold_ast "_types" tys, f]; |
0 | 153 |
|
154 |
fun fun_ast_tr' (*"fun"*) asts = |
|
155 |
(case unfold_ast_p "fun" (Appl (Constant "fun" :: asts)) of |
|
18 | 156 |
(dom as _ :: _ :: _, cod) |
0 | 157 |
=> Appl [Constant "_bracket", fold_ast "_types" dom, cod] |
158 |
| _ => raise Match); |
|
159 |
||
160 |
||
161 |
(* type_ext *) |
|
162 |
||
163 |
val sortT = Type ("sort", []); |
|
164 |
val classesT = Type ("classes", []); |
|
165 |
val typesT = Type ("types", []); |
|
166 |
||
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
640
diff
changeset
|
167 |
val type_ext = mk_syn_ext false [] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
168 |
[Mfix ("_", tidT --> typeT, "", [], max_pri), |
239 | 169 |
Mfix ("_", tvarT --> typeT, "", [], max_pri), |
170 |
Mfix ("_", idT --> typeT, "", [], max_pri), |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
171 |
Mfix ("_::_", [tidT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
239 | 172 |
Mfix ("_::_", [tvarT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
173 |
Mfix ("_", idT --> sortT, "", [], max_pri), |
|
2584 | 174 |
Mfix ("{}", sortT, "_topsort", [], max_pri), |
239 | 175 |
Mfix ("{_}", classesT --> sortT, "_sort", [], max_pri), |
176 |
Mfix ("_", idT --> classesT, "", [], max_pri), |
|
177 |
Mfix ("_,_", [idT, classesT] ---> classesT, "_classes", [], max_pri), |
|
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
178 |
Mfix ("_ _", [typeT, idT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
179 |
Mfix ("((1'(_,/ _'))_)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri), |
239 | 180 |
Mfix ("_", typeT --> typesT, "", [], max_pri), |
181 |
Mfix ("_,/ _", [typeT, typesT] ---> typesT, "_types", [], max_pri), |
|
182 |
Mfix ("(_/ => _)", [typeT, typeT] ---> typeT, "fun", [1, 0], 0), |
|
624 | 183 |
Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT, "_bracket", [0, 0], 0), |
2678 | 184 |
Mfix ("'(_')", typeT --> typeT, "", [0], max_pri), |
185 |
Mfix ("'_", typeT, "dummy", [], max_pri)] |
|
258 | 186 |
[] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
187 |
([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)], |
239 | 188 |
[], |
189 |
[], |
|
190 |
[("fun", fun_ast_tr')]) |
|
2699 | 191 |
TokenTrans.token_translation |
239 | 192 |
([], []); |
0 | 193 |
|
194 |
end; |