author | wenzelm |
Tue, 20 Oct 1998 16:30:27 +0200 | |
changeset 5690 | 4b056ee5435c |
parent 3829 | d7333ef9e72c |
child 6901 | 5e20ddfdf3c7 |
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 |
3778 | 11 |
val raw_term_sorts: term -> (indexname * sort) list |
2584 | 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 |
|
27 |
||
28 |
(** input utils **) |
|
0 | 29 |
|
2584 | 30 |
(* raw_term_sorts *) |
31 |
||
3778 | 32 |
fun raw_term_sorts tm = |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
33 |
let |
18 | 34 |
fun classes (Const (c, _)) = [c] |
35 |
| classes (Free (c, _)) = [c] |
|
3778 | 36 |
| classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs |
37 |
| classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs |
|
38 |
| classes tm = raise TERM ("raw_term_sorts: bad encoding of classes", [tm]); |
|
0 | 39 |
|
2584 | 40 |
fun sort (Const ("_topsort", _)) = [] |
41 |
| sort (Const (c, _)) = [c] |
|
42 |
| sort (Free (c, _)) = [c] |
|
3778 | 43 |
| sort (Const ("_sort", _) $ cs) = classes cs |
44 |
| sort tm = raise TERM ("raw_term_sorts: bad encoding of sort", [tm]); |
|
2584 | 45 |
|
3778 | 46 |
fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) env = ((x, ~1), sort cs) ins env |
47 |
| add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) env = (xi, sort cs) ins env |
|
48 |
| add_env (Abs (_, _, t)) env = add_env t env |
|
49 |
| add_env (t1 $ t2) env = add_env t1 (add_env t2 env) |
|
50 |
| add_env t env = env; |
|
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
51 |
in |
3778 | 52 |
add_env tm [] |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
53 |
end; |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
54 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
55 |
|
2584 | 56 |
(* typ_of_term *) |
0 | 57 |
|
2584 | 58 |
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
|
59 |
let |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
60 |
fun typ_of (Free (x, _)) = |
5690 | 61 |
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
|
62 |
else Type (x, []) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
63 |
| 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
|
64 |
| 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
|
65 |
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
|
66 |
| 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
|
67 |
TVar (xi, get_sort xi) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
68 |
| typ_of tm = |
0 | 69 |
let |
70 |
val (t, ts) = strip_comb tm; |
|
71 |
val a = |
|
18 | 72 |
(case t of |
0 | 73 |
Const (x, _) => x |
74 |
| Free (x, _) => x |
|
3778 | 75 |
| _ => raise TERM ("typ_of_term: bad encoding of type", [tm])); |
0 | 76 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
77 |
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
|
78 |
end; |
0 | 79 |
in |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
80 |
typ_of t |
0 | 81 |
end; |
82 |
||
83 |
||
84 |
||
2584 | 85 |
(** output utils **) |
86 |
||
2699 | 87 |
(* term_of_sort *) |
2584 | 88 |
|
89 |
fun term_of_sort S = |
|
90 |
let |
|
5690 | 91 |
fun class c = Lexicon.const "_class" $ Lexicon.free c; |
2584 | 92 |
|
93 |
fun classes [] = sys_error "term_of_sort" |
|
94 |
| classes [c] = class c |
|
5690 | 95 |
| classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs; |
2584 | 96 |
in |
97 |
(case S of |
|
5690 | 98 |
[] => Lexicon.const "_topsort" |
2584 | 99 |
| [c] => class c |
5690 | 100 |
| cs => Lexicon.const "_sort" $ classes cs) |
2584 | 101 |
end; |
102 |
||
103 |
||
104 |
(* term_of_typ *) |
|
0 | 105 |
|
106 |
fun term_of_typ show_sorts ty = |
|
107 |
let |
|
2584 | 108 |
fun of_sort t S = |
5690 | 109 |
if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S |
2584 | 110 |
else t; |
0 | 111 |
|
5690 | 112 |
fun term_of (Type (a, Ts)) = list_comb (Lexicon.const a, map term_of Ts) |
113 |
| term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S |
|
114 |
| term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S; |
|
0 | 115 |
in |
18 | 116 |
term_of ty |
0 | 117 |
end; |
118 |
||
119 |
||
120 |
||
121 |
(** the type syntax **) |
|
122 |
||
18 | 123 |
(* parse ast translations *) |
0 | 124 |
|
5690 | 125 |
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty] |
126 |
| 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
|
127 |
|
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
128 |
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] = |
5690 | 129 |
Ast.Appl (f :: ty :: Ast.unfold_ast "_types" tys) |
130 |
| tappl_ast_tr (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts); |
|
0 | 131 |
|
132 |
fun bracket_ast_tr (*"_bracket"*) [dom, cod] = |
|
5690 | 133 |
Ast.fold_ast_p "fun" (Ast.unfold_ast "_types" dom, cod) |
134 |
| bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts); |
|
0 | 135 |
|
136 |
||
18 | 137 |
(* print ast translations *) |
0 | 138 |
|
5690 | 139 |
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f]) |
140 |
| 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
|
141 |
| tappl_ast_tr' (f, ty :: tys) = |
5690 | 142 |
Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f]; |
0 | 143 |
|
144 |
fun fun_ast_tr' (*"fun"*) asts = |
|
5690 | 145 |
(case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of |
18 | 146 |
(dom as _ :: _ :: _, cod) |
5690 | 147 |
=> Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod] |
0 | 148 |
| _ => raise Match); |
149 |
||
150 |
||
151 |
(* type_ext *) |
|
152 |
||
153 |
val sortT = Type ("sort", []); |
|
154 |
val classesT = Type ("classes", []); |
|
155 |
val typesT = Type ("types", []); |
|
156 |
||
5690 | 157 |
local open Lexicon SynExt in |
158 |
||
764
b60e77395d1a
changed Pure's grammar and the way types are converted to nonterminals
clasohm
parents:
640
diff
changeset
|
159 |
val type_ext = mk_syn_ext false [] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
160 |
[Mfix ("_", tidT --> typeT, "", [], max_pri), |
239 | 161 |
Mfix ("_", tvarT --> typeT, "", [], max_pri), |
162 |
Mfix ("_", idT --> typeT, "", [], max_pri), |
|
3829 | 163 |
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
|
164 |
Mfix ("_::_", [tidT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
239 | 165 |
Mfix ("_::_", [tvarT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
166 |
Mfix ("_", idT --> sortT, "", [], max_pri), |
|
3829 | 167 |
Mfix ("_", longidT --> sortT, "", [], max_pri), |
2584 | 168 |
Mfix ("{}", sortT, "_topsort", [], max_pri), |
239 | 169 |
Mfix ("{_}", classesT --> sortT, "_sort", [], max_pri), |
170 |
Mfix ("_", idT --> classesT, "", [], max_pri), |
|
3829 | 171 |
Mfix ("_", longidT --> classesT, "", [], max_pri), |
239 | 172 |
Mfix ("_,_", [idT, classesT] ---> classesT, "_classes", [], max_pri), |
3829 | 173 |
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
|
174 |
Mfix ("_ _", [typeT, idT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
3829 | 175 |
Mfix ("_ _", [typeT, longidT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
330
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), |
3829 | 177 |
Mfix ("((1'(_,/ _'))_)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri), |
239 | 178 |
Mfix ("_", typeT --> typesT, "", [], max_pri), |
179 |
Mfix ("_,/ _", [typeT, typesT] ---> typesT, "_types", [], max_pri), |
|
180 |
Mfix ("(_/ => _)", [typeT, typeT] ---> typeT, "fun", [1, 0], 0), |
|
624 | 181 |
Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT, "_bracket", [0, 0], 0), |
2678 | 182 |
Mfix ("'(_')", typeT --> typeT, "", [0], max_pri), |
183 |
Mfix ("'_", typeT, "dummy", [], max_pri)] |
|
258 | 184 |
[] |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
185 |
([("_tapp", tapp_ast_tr), ("_tappl", tappl_ast_tr), ("_bracket", bracket_ast_tr)], |
239 | 186 |
[], |
187 |
[], |
|
188 |
[("fun", fun_ast_tr')]) |
|
2699 | 189 |
TokenTrans.token_translation |
239 | 190 |
([], []); |
0 | 191 |
|
192 |
end; |
|
5690 | 193 |
|
194 |
||
195 |
end; |