author | schirmer |
Thu, 06 Nov 2003 20:45:02 +0100 | |
changeset 14255 | e6e3e3f0deed |
parent 14086 | a9be38579840 |
child 14838 | b12855d44c97 |
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 |
|
12785 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
0 | 5 |
|
8895 | 6 |
Utilities for input and output of types. Also the concrete syntax of |
7 |
types, which is required to bootstrap Pure. |
|
0 | 8 |
*) |
9 |
||
10 |
signature TYPE_EXT0 = |
|
2584 | 11 |
sig |
8895 | 12 |
val sort_of_term: term -> sort |
3778 | 13 |
val raw_term_sorts: term -> (indexname * sort) list |
12317 | 14 |
val typ_of_term: (indexname -> sort) -> (sort -> sort) -> term -> typ |
6901 | 15 |
val term_of_typ: bool -> typ -> term |
10572 | 16 |
val no_brackets: unit -> bool |
2584 | 17 |
end; |
0 | 18 |
|
19 |
signature TYPE_EXT = |
|
2584 | 20 |
sig |
0 | 21 |
include TYPE_EXT0 |
2584 | 22 |
val term_of_sort: sort -> term |
1511 | 23 |
val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
8895 | 24 |
val sortT: typ |
1511 | 25 |
val type_ext: SynExt.syn_ext |
2584 | 26 |
end; |
0 | 27 |
|
1511 | 28 |
structure TypeExt : TYPE_EXT = |
0 | 29 |
struct |
2584 | 30 |
|
31 |
||
32 |
(** input utils **) |
|
0 | 33 |
|
8895 | 34 |
(* sort_of_term *) |
2584 | 35 |
|
8895 | 36 |
fun sort_of_term tm = |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
37 |
let |
18 | 38 |
fun classes (Const (c, _)) = [c] |
39 |
| classes (Free (c, _)) = [c] |
|
3778 | 40 |
| classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs |
41 |
| classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs |
|
8895 | 42 |
| classes tm = raise TERM ("sort_of_term: bad encoding of classes", [tm]); |
0 | 43 |
|
2584 | 44 |
fun sort (Const ("_topsort", _)) = [] |
45 |
| sort (Const (c, _)) = [c] |
|
46 |
| sort (Free (c, _)) = [c] |
|
14255 | 47 |
| sort (Const ("_class",_) $ Free (c, _)) = [c] |
3778 | 48 |
| sort (Const ("_sort", _) $ cs) = classes cs |
8895 | 49 |
| sort tm = raise TERM ("sort_of_term: bad encoding of sort", [tm]); |
50 |
in sort tm end; |
|
51 |
||
2584 | 52 |
|
8895 | 53 |
(* raw_term_sorts *) |
54 |
||
55 |
fun raw_term_sorts tm = |
|
56 |
let |
|
57 |
fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env = ((x, ~1), sort_of_term cs) ins env |
|
14255 | 58 |
| add_env (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ cs) env |
59 |
= ((x, ~1), sort_of_term cs) ins env |
|
8895 | 60 |
| add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env = (xi, sort_of_term cs) ins env |
14255 | 61 |
| add_env (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ cs) env |
62 |
= (xi, sort_of_term cs) ins env |
|
3778 | 63 |
| add_env (Abs (_, _, t)) env = add_env t env |
64 |
| add_env (t1 $ t2) env = add_env t1 (add_env t2 env) |
|
65 |
| add_env t env = env; |
|
8895 | 66 |
in add_env tm [] end; |
557
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 |
|
2584 | 69 |
(* typ_of_term *) |
0 | 70 |
|
12317 | 71 |
fun typ_of_term get_sort map_sort t = |
557
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 typ_of (Free (x, _)) = |
5690 | 74 |
if Lexicon.is_tid x then TFree (x, get_sort (x, ~1)) |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
75 |
else Type (x, []) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
76 |
| typ_of (Var (xi, _)) = TVar (xi, get_sort xi) |
14255 | 77 |
| typ_of (Const ("_tfree",_) $ (t as Free x)) = typ_of t |
78 |
| typ_of (Const ("_tvar",_) $ (t as Var x)) = typ_of t |
|
12317 | 79 |
| typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1)) |
14255 | 80 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) |
81 |
= TFree (x, get_sort (x, ~1)) |
|
12317 | 82 |
| typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi) |
14255 | 83 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) |
84 |
= TVar (xi, get_sort xi) |
|
12317 | 85 |
| typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", map_sort (sort_of_term t)) |
14255 | 86 |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
87 |
| typ_of tm = |
0 | 88 |
let |
89 |
val (t, ts) = strip_comb tm; |
|
90 |
val a = |
|
18 | 91 |
(case t of |
0 | 92 |
Const (x, _) => x |
93 |
| Free (x, _) => x |
|
3778 | 94 |
| _ => raise TERM ("typ_of_term: bad encoding of type", [tm])); |
12317 | 95 |
in Type (a, map typ_of ts) end; |
8895 | 96 |
in typ_of t end; |
0 | 97 |
|
98 |
||
99 |
||
2584 | 100 |
(** output utils **) |
101 |
||
2699 | 102 |
(* term_of_sort *) |
2584 | 103 |
|
104 |
fun term_of_sort S = |
|
105 |
let |
|
5690 | 106 |
fun class c = Lexicon.const "_class" $ Lexicon.free c; |
2584 | 107 |
|
108 |
fun classes [] = sys_error "term_of_sort" |
|
109 |
| classes [c] = class c |
|
5690 | 110 |
| classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs; |
2584 | 111 |
in |
112 |
(case S of |
|
5690 | 113 |
[] => Lexicon.const "_topsort" |
2584 | 114 |
| [c] => class c |
5690 | 115 |
| cs => Lexicon.const "_sort" $ classes cs) |
2584 | 116 |
end; |
117 |
||
118 |
||
119 |
(* term_of_typ *) |
|
0 | 120 |
|
121 |
fun term_of_typ show_sorts ty = |
|
122 |
let |
|
2584 | 123 |
fun of_sort t S = |
5690 | 124 |
if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S |
2584 | 125 |
else t; |
0 | 126 |
|
5690 | 127 |
fun term_of (Type (a, Ts)) = list_comb (Lexicon.const a, map term_of Ts) |
128 |
| term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S |
|
129 |
| term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S; |
|
8895 | 130 |
in term_of ty end; |
0 | 131 |
|
132 |
||
133 |
||
134 |
(** the type syntax **) |
|
135 |
||
10572 | 136 |
(* print mode *) |
137 |
||
138 |
val bracketsN = "brackets"; |
|
139 |
val no_bracketsN = "no_brackets"; |
|
140 |
||
141 |
fun no_brackets () = |
|
11312 | 142 |
Library.find_first (equal bracketsN orf equal no_bracketsN) (! print_mode) |
143 |
= Some no_bracketsN; |
|
144 |
||
145 |
val type_bracketsN = "type_brackets"; |
|
146 |
val no_type_bracketsN = "no_type_brackets"; |
|
147 |
||
148 |
fun no_type_brackets () = |
|
149 |
Library.find_first (equal type_bracketsN orf equal no_type_bracketsN) |
|
150 |
(! print_mode) |
|
14086
a9be38579840
Type antiquotations do not use the bracket syntax by default any longer.
nipkow
parents:
12785
diff
changeset
|
151 |
<> Some type_bracketsN; |
10572 | 152 |
|
153 |
||
18 | 154 |
(* parse ast translations *) |
0 | 155 |
|
5690 | 156 |
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty] |
157 |
| tapp_ast_tr (*"_tapp"*) asts = raise Ast.AST ("tapp_ast_tr", asts); |
|
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
158 |
|
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
159 |
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] = |
5690 | 160 |
Ast.Appl (f :: ty :: Ast.unfold_ast "_types" tys) |
161 |
| tappl_ast_tr (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts); |
|
0 | 162 |
|
163 |
fun bracket_ast_tr (*"_bracket"*) [dom, cod] = |
|
5690 | 164 |
Ast.fold_ast_p "fun" (Ast.unfold_ast "_types" dom, cod) |
165 |
| bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts); |
|
0 | 166 |
|
167 |
||
18 | 168 |
(* print ast translations *) |
0 | 169 |
|
5690 | 170 |
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f]) |
171 |
| tappl_ast_tr' (f, [ty]) = Ast.Appl [Ast.Constant "_tapp", ty, f] |
|
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
172 |
| tappl_ast_tr' (f, ty :: tys) = |
5690 | 173 |
Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f]; |
0 | 174 |
|
175 |
fun fun_ast_tr' (*"fun"*) asts = |
|
11312 | 176 |
if no_brackets() orelse no_type_brackets() then raise Match |
10572 | 177 |
else |
178 |
(case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of |
|
179 |
(dom as _ :: _ :: _, cod) |
|
180 |
=> Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod] |
|
181 |
| _ => raise Match); |
|
0 | 182 |
|
183 |
||
184 |
(* type_ext *) |
|
185 |
||
186 |
val sortT = Type ("sort", []); |
|
187 |
val classesT = Type ("classes", []); |
|
188 |
val typesT = Type ("types", []); |
|
189 |
||
5690 | 190 |
local open Lexicon SynExt in |
191 |
||
7500 | 192 |
val type_ext = mk_syn_ext false ["dummy"] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
193 |
[Mfix ("_", tidT --> typeT, "", [], max_pri), |
239 | 194 |
Mfix ("_", tvarT --> typeT, "", [], max_pri), |
195 |
Mfix ("_", idT --> typeT, "", [], max_pri), |
|
3829 | 196 |
Mfix ("_", longidT --> typeT, "", [], max_pri), |
330
2fda15dd1e0f
changed the way a grammar is generated to allow the new parser to work;
clasohm
parents:
258
diff
changeset
|
197 |
Mfix ("_::_", [tidT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
239 | 198 |
Mfix ("_::_", [tvarT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
12317 | 199 |
Mfix ("'_::_", sortT --> typeT, "_dummy_ofsort", [0], max_pri), |
239 | 200 |
Mfix ("_", idT --> sortT, "", [], max_pri), |
3829 | 201 |
Mfix ("_", longidT --> sortT, "", [], max_pri), |
2584 | 202 |
Mfix ("{}", sortT, "_topsort", [], max_pri), |
239 | 203 |
Mfix ("{_}", classesT --> sortT, "_sort", [], max_pri), |
204 |
Mfix ("_", idT --> classesT, "", [], max_pri), |
|
3829 | 205 |
Mfix ("_", longidT --> classesT, "", [], max_pri), |
239 | 206 |
Mfix ("_,_", [idT, classesT] ---> classesT, "_classes", [], max_pri), |
3829 | 207 |
Mfix ("_,_", [longidT, 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
|
208 |
Mfix ("_ _", [typeT, idT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
3829 | 209 |
Mfix ("_ _", [typeT, longidT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
9067 | 210 |
Mfix ("((1'(_,/ _')) _)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri), |
211 |
Mfix ("((1'(_,/ _')) _)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri), |
|
239 | 212 |
Mfix ("_", typeT --> typesT, "", [], max_pri), |
213 |
Mfix ("_,/ _", [typeT, typesT] ---> typesT, "_types", [], max_pri), |
|
214 |
Mfix ("(_/ => _)", [typeT, typeT] ---> typeT, "fun", [1, 0], 0), |
|
624 | 215 |
Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT, "_bracket", [0, 0], 0), |
2678 | 216 |
Mfix ("'(_')", typeT --> typeT, "", [0], max_pri), |
217 |
Mfix ("'_", typeT, "dummy", [], max_pri)] |
|
258 | 218 |
[] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
219 |
([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)], |
239 | 220 |
[], |
221 |
[], |
|
222 |
[("fun", fun_ast_tr')]) |
|
2699 | 223 |
TokenTrans.token_translation |
239 | 224 |
([], []); |
0 | 225 |
|
226 |
end; |
|
5690 | 227 |
|
228 |
end; |