author | wenzelm |
Fri, 19 Aug 1994 15:38:18 +0200 | |
changeset 557 | 9d386e6c02b7 |
parent 347 | cd41a57221d0 |
child 624 | 33b9b5da3e6f |
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 |
||
18 | 5 |
The concrete syntax of types (used to bootstrap Pure). |
6 |
||
7 |
TODO: |
|
8 |
term_of_typ: prune sorts |
|
0 | 9 |
*) |
10 |
||
11 |
signature TYPE_EXT0 = |
|
12 |
sig |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
13 |
val raw_term_sorts: term -> (indexname * sort) list |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
14 |
val typ_of_term: (indexname * sort) list -> (indexname -> sort) -> term -> typ |
0 | 15 |
end; |
16 |
||
17 |
signature TYPE_EXT = |
|
18 |
sig |
|
19 |
include TYPE_EXT0 |
|
239 | 20 |
structure SynExt: SYN_EXT |
21 |
local open SynExt SynExt.Ast in |
|
0 | 22 |
val term_of_typ: bool -> typ -> term |
23 |
val tappl_ast_tr': ast * ast list -> ast |
|
239 | 24 |
val type_ext: syn_ext |
0 | 25 |
end |
26 |
end; |
|
27 |
||
239 | 28 |
functor TypeExtFun(structure Lexicon: LEXICON and SynExt: SYN_EXT): TYPE_EXT = |
0 | 29 |
struct |
30 |
||
239 | 31 |
structure SynExt = SynExt; |
32 |
open Lexicon SynExt SynExt.Ast; |
|
0 | 33 |
|
34 |
||
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
35 |
(** raw_term_sorts **) |
0 | 36 |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
37 |
fun raw_term_sorts tm = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
38 |
let |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
39 |
fun show_var (xi as (x, i)) = if i < 0 then x else string_of_vname xi; |
18 | 40 |
|
41 |
fun classes (Const (c, _)) = [c] |
|
42 |
| classes (Free (c, _)) = [c] |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
43 |
| 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
|
44 |
| 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
|
45 |
| classes tm = raise_term "raw_term_sorts: bad encoding of classes" [tm]; |
0 | 46 |
|
47 |
fun sort (Const ("_emptysort", _)) = [] |
|
48 |
| sort (Const (s, _)) = [s] |
|
49 |
| sort (Free (s, _)) = [s] |
|
18 | 50 |
| 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
|
51 |
| 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
|
52 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
53 |
fun env_of (Const ("_ofsort", _) $ Free (x, _) $ srt) = [((x, ~1), sort srt)] |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
54 |
| env_of (Const ("_ofsort", _) $ Var (xi, _) $ srt) = [(xi, sort srt)] |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
55 |
| env_of (Abs (_, _, t)) = env_of t |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
56 |
| env_of (t1 $ t2) = env_of t1 @ env_of t2 |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
57 |
| env_of t = []; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
58 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
59 |
val env = distinct (env_of tm); |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
60 |
in |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
61 |
(case gen_duplicates eq_fst env of |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
62 |
[] => env |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
63 |
| dups => error ("Inconsistent sort constraints for type variable(s) " ^ |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
64 |
commas (map (quote o show_var o #1) dups))) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
65 |
end; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
66 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
67 |
|
0 | 68 |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
69 |
(** typ_of_term **) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
70 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
71 |
fun typ_of_term sort_env def_sort t = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
72 |
let |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
73 |
fun get_sort xi = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
74 |
(case assoc (sort_env, xi) of |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
75 |
None => def_sort xi |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
76 |
| Some s => s); |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
77 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
78 |
fun typ_of (Free (x, _)) = |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
79 |
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
|
80 |
else Type (x, []) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
81 |
| 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
|
82 |
| 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
|
83 |
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
|
84 |
| 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
|
85 |
TVar (xi, get_sort xi) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
86 |
| typ_of tm = |
0 | 87 |
let |
88 |
val (t, ts) = strip_comb tm; |
|
89 |
val a = |
|
18 | 90 |
(case t of |
0 | 91 |
Const (x, _) => x |
92 |
| Free (x, _) => x |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
93 |
| _ => raise_term "typ_of_term: bad encoding of type" [tm]); |
0 | 94 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
95 |
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
|
96 |
end; |
0 | 97 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
98 |
typ_of t |
0 | 99 |
end; |
100 |
||
101 |
||
102 |
||
103 |
(** term_of_typ **) |
|
104 |
||
105 |
fun term_of_typ show_sorts ty = |
|
106 |
let |
|
107 |
fun classes [] = raise Match |
|
18 | 108 |
| classes [c] = free c |
109 |
| classes (c :: cs) = const "_classes" $ free c $ classes cs; |
|
0 | 110 |
|
111 |
fun sort [] = const "_emptysort" |
|
18 | 112 |
| sort [s] = free s |
113 |
| sort ss = const "_sort" $ classes ss; |
|
0 | 114 |
|
115 |
fun of_sort t ss = |
|
116 |
if show_sorts then const "_ofsort" $ t $ sort ss else t; |
|
117 |
||
18 | 118 |
fun term_of (Type (a, tys)) = list_comb (const a, map term_of tys) |
119 |
| term_of (TFree (x, ss)) = of_sort (free x) ss |
|
120 |
| term_of (TVar (xi, ss)) = of_sort (var xi) ss; |
|
0 | 121 |
in |
18 | 122 |
term_of ty |
0 | 123 |
end; |
124 |
||
125 |
||
126 |
||
127 |
(** the type syntax **) |
|
128 |
||
18 | 129 |
(* parse ast translations *) |
0 | 130 |
|
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
131 |
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
|
132 |
| 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
|
133 |
|
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
134 |
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] = |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
135 |
Appl (f :: ty :: unfold_ast "_types" tys) |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
136 |
| tappl_ast_tr (*"_tappl"*) asts = raise_ast "tappl_ast_tr" asts; |
0 | 137 |
|
138 |
fun bracket_ast_tr (*"_bracket"*) [dom, cod] = |
|
139 |
fold_ast_p "fun" (unfold_ast "_types" dom, cod) |
|
140 |
| bracket_ast_tr (*"_bracket"*) asts = raise_ast "bracket_ast_tr" asts; |
|
141 |
||
142 |
||
18 | 143 |
(* print ast translations *) |
0 | 144 |
|
145 |
fun tappl_ast_tr' (f, []) = raise_ast "tappl_ast_tr'" [f] |
|
146 |
| 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
|
147 |
| tappl_ast_tr' (f, ty :: tys) = |
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
148 |
Appl [Constant "_tappl", ty, fold_ast "_types" tys, f]; |
0 | 149 |
|
150 |
fun fun_ast_tr' (*"fun"*) asts = |
|
151 |
(case unfold_ast_p "fun" (Appl (Constant "fun" :: asts)) of |
|
18 | 152 |
(dom as _ :: _ :: _, cod) |
0 | 153 |
=> Appl [Constant "_bracket", fold_ast "_types" dom, cod] |
154 |
| _ => raise Match); |
|
155 |
||
156 |
||
157 |
(* type_ext *) |
|
158 |
||
159 |
val sortT = Type ("sort", []); |
|
160 |
val classesT = Type ("classes", []); |
|
161 |
val typesT = Type ("types", []); |
|
162 |
||
239 | 163 |
val type_ext = syn_ext |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
164 |
[logic, "type"] [logic, "type"] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
165 |
[Mfix ("_", tidT --> typeT, "", [], max_pri), |
239 | 166 |
Mfix ("_", tvarT --> typeT, "", [], max_pri), |
167 |
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
|
168 |
Mfix ("_::_", [tidT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
239 | 169 |
Mfix ("_::_", [tvarT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
170 |
Mfix ("_", idT --> sortT, "", [], max_pri), |
|
171 |
Mfix ("{}", sortT, "_emptysort", [], max_pri), |
|
172 |
Mfix ("{_}", classesT --> sortT, "_sort", [], max_pri), |
|
173 |
Mfix ("_", idT --> classesT, "", [], max_pri), |
|
174 |
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
|
175 |
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
|
176 |
Mfix ("((1'(_,/ _'))_)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri), |
239 | 177 |
Mfix ("_", typeT --> typesT, "", [], max_pri), |
178 |
Mfix ("_,/ _", [typeT, typesT] ---> typesT, "_types", [], max_pri), |
|
179 |
Mfix ("(_/ => _)", [typeT, typeT] ---> typeT, "fun", [1, 0], 0), |
|
180 |
Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT, "_bracket", [0, 0], 0)] |
|
258 | 181 |
[] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
182 |
([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)], |
239 | 183 |
[], |
184 |
[], |
|
185 |
[("fun", fun_ast_tr')]) |
|
186 |
([], []); |
|
0 | 187 |
|
188 |
||
189 |
end; |
|
190 |