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