author | wenzelm |
Wed, 18 Jun 2008 22:32:06 +0200 | |
changeset 27266 | a2db1e379778 |
parent 27197 | d1b8b6938b23 |
child 29565 | 3f8b24fcfbd6 |
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 |
||
8895 | 5 |
Utilities for input and output of types. Also the concrete syntax of |
6 |
types, which is required to bootstrap Pure. |
|
0 | 7 |
*) |
8 |
||
9 |
signature TYPE_EXT0 = |
|
2584 | 10 |
sig |
22704 | 11 |
val sort_of_term: (sort -> sort) -> term -> sort |
12 |
val term_sorts: (sort -> sort) -> term -> (indexname * sort) list |
|
12317 | 13 |
val typ_of_term: (indexname -> sort) -> (sort -> sort) -> term -> typ |
27266
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
14 |
val type_constraint: typ -> term -> term |
22764 | 15 |
val decode_term: (((string * int) * sort) list -> string * int -> sort) -> |
27197
d1b8b6938b23
map_const: soft version, no failure here (recovers hiding of consts, because a hidden name is illegal and rejected later);
wenzelm
parents:
26708
diff
changeset
|
16 |
(string -> bool * string) -> (string -> string option) -> |
22704 | 17 |
(typ -> typ) -> (sort -> sort) -> term -> term |
6901 | 18 |
val term_of_typ: bool -> typ -> term |
10572 | 19 |
val no_brackets: unit -> bool |
16614 | 20 |
val no_type_brackets: unit -> bool |
2584 | 21 |
end; |
0 | 22 |
|
23 |
signature TYPE_EXT = |
|
2584 | 24 |
sig |
0 | 25 |
include TYPE_EXT0 |
2584 | 26 |
val term_of_sort: sort -> term |
1511 | 27 |
val tappl_ast_tr': Ast.ast * Ast.ast list -> Ast.ast |
8895 | 28 |
val sortT: typ |
1511 | 29 |
val type_ext: SynExt.syn_ext |
2584 | 30 |
end; |
0 | 31 |
|
15750 | 32 |
structure TypeExt: TYPE_EXT = |
0 | 33 |
struct |
2584 | 34 |
|
35 |
(** input utils **) |
|
0 | 36 |
|
8895 | 37 |
(* sort_of_term *) |
2584 | 38 |
|
22704 | 39 |
fun sort_of_term (map_sort: sort -> sort) tm = |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
40 |
let |
18 | 41 |
fun classes (Const (c, _)) = [c] |
42 |
| classes (Free (c, _)) = [c] |
|
26039 | 43 |
| classes (Const ("_class", _) $ Free (c, _)) = [c] |
3778 | 44 |
| classes (Const ("_classes", _) $ Const (c, _) $ cs) = c :: classes cs |
45 |
| classes (Const ("_classes", _) $ Free (c, _) $ cs) = c :: classes cs |
|
26039 | 46 |
| classes (Const ("_classes", _) $ (Const ("_class", _) $ Free (c, _)) $ cs) = c :: classes cs |
8895 | 47 |
| classes tm = raise TERM ("sort_of_term: bad encoding of classes", [tm]); |
0 | 48 |
|
2584 | 49 |
fun sort (Const ("_topsort", _)) = [] |
50 |
| sort (Const (c, _)) = [c] |
|
51 |
| sort (Free (c, _)) = [c] |
|
26039 | 52 |
| sort (Const ("_class", _) $ Free (c, _)) = [c] |
3778 | 53 |
| sort (Const ("_sort", _) $ cs) = classes cs |
8895 | 54 |
| sort tm = raise TERM ("sort_of_term: bad encoding of sort", [tm]); |
22704 | 55 |
in map_sort (sort tm) end; |
8895 | 56 |
|
2584 | 57 |
|
22704 | 58 |
(* term_sorts *) |
8895 | 59 |
|
22704 | 60 |
fun term_sorts map_sort tm = |
8895 | 61 |
let |
22704 | 62 |
val sort_of = sort_of_term map_sort; |
63 |
||
64 |
fun add_env (Const ("_ofsort", _) $ Free (x, _) $ cs) = |
|
65 |
insert (op =) ((x, ~1), sort_of cs) |
|
66 |
| add_env (Const ("_ofsort", _) $ (Const ("_tfree", _) $ Free (x, _)) $ cs) = |
|
67 |
insert (op =) ((x, ~1), sort_of cs) |
|
68 |
| add_env (Const ("_ofsort", _) $ Var (xi, _) $ cs) = |
|
69 |
insert (op =) (xi, sort_of cs) |
|
70 |
| add_env (Const ("_ofsort", _) $ (Const ("_tvar", _) $ Var (xi, _)) $ cs) = |
|
71 |
insert (op =) (xi, sort_of cs) |
|
72 |
| add_env (Abs (_, _, t)) = add_env t |
|
73 |
| add_env (t1 $ t2) = add_env t1 #> add_env t2 |
|
74 |
| add_env _ = I; |
|
8895 | 75 |
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
|
76 |
|
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
77 |
|
2584 | 78 |
(* typ_of_term *) |
0 | 79 |
|
12317 | 80 |
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
|
81 |
let |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
82 |
fun typ_of (Free (x, _)) = |
5690 | 83 |
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
|
84 |
else Type (x, []) |
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
85 |
| typ_of (Var (xi, _)) = TVar (xi, get_sort xi) |
14255 | 86 |
| typ_of (Const ("_tfree",_) $ (t as Free x)) = typ_of t |
87 |
| typ_of (Const ("_tvar",_) $ (t as Var x)) = typ_of t |
|
12317 | 88 |
| typ_of (Const ("_ofsort", _) $ Free (x, _) $ _) = TFree (x, get_sort (x, ~1)) |
16614 | 89 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tfree",_) $ Free (x, _)) $ _) = |
90 |
TFree (x, get_sort (x, ~1)) |
|
12317 | 91 |
| typ_of (Const ("_ofsort", _) $ Var (xi, _) $ _) = TVar (xi, get_sort xi) |
16614 | 92 |
| typ_of (Const ("_ofsort", _) $ (Const ("_tvar",_) $ Var (xi, _)) $ _) = |
93 |
TVar (xi, get_sort xi) |
|
22704 | 94 |
| typ_of (Const ("_dummy_ofsort", _) $ t) = TFree ("'_dummy_", sort_of_term map_sort t) |
557
9d386e6c02b7
added raw_term_sorts and changed typ_of_term accordingly (part of fix
wenzelm
parents:
347
diff
changeset
|
95 |
| typ_of tm = |
0 | 96 |
let |
16614 | 97 |
val (t, ts) = Term.strip_comb tm; |
0 | 98 |
val a = |
18 | 99 |
(case t of |
0 | 100 |
Const (x, _) => x |
101 |
| Free (x, _) => x |
|
3778 | 102 |
| _ => raise TERM ("typ_of_term: bad encoding of type", [tm])); |
12317 | 103 |
in Type (a, map typ_of ts) end; |
8895 | 104 |
in typ_of t end; |
0 | 105 |
|
106 |
||
22764 | 107 |
(* decode_term -- transform parse tree into raw term *) |
22704 | 108 |
|
27266
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
109 |
fun type_constraint T t = |
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
110 |
if T = dummyT then t |
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
111 |
else Const ("_type_constraint_", T --> T) $ t; |
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
112 |
|
22764 | 113 |
fun decode_term get_sort map_const map_free map_type map_sort tm = |
22704 | 114 |
let |
115 |
val sort_env = term_sorts map_sort tm; |
|
116 |
val decodeT = map_type o typ_of_term (get_sort sort_env) map_sort; |
|
117 |
||
118 |
fun decode (Const ("_constrain", _) $ t $ typ) = |
|
27266
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
119 |
type_constraint (decodeT typ) (decode t) |
22704 | 120 |
| decode (Const ("_constrainAbs", _) $ (Abs (x, T, t)) $ typ) = |
121 |
if T = dummyT then Abs (x, decodeT typ, decode t) |
|
27266
a2db1e379778
added type_constraint (formerly in type_infer.ML, which is now loaded later);
wenzelm
parents:
27197
diff
changeset
|
122 |
else type_constraint (decodeT typ --> dummyT) (Abs (x, map_type T, decode t)) |
22704 | 123 |
| decode (Abs (x, T, t)) = Abs (x, map_type T, decode t) |
124 |
| decode (t $ u) = decode t $ decode u |
|
125 |
| decode (Const (a, T)) = |
|
126 |
let val c = |
|
27197
d1b8b6938b23
map_const: soft version, no failure here (recovers hiding of consts, because a hidden name is illegal and rejected later);
wenzelm
parents:
26708
diff
changeset
|
127 |
(case try (unprefix Lexicon.constN) a of SOME c => c | NONE => snd (map_const a)) |
22704 | 128 |
in Const (c, map_type T) end |
129 |
| decode (Free (a, T)) = |
|
130 |
(case (map_free a, map_const a) of |
|
131 |
(SOME x, _) => Free (x, map_type T) |
|
27197
d1b8b6938b23
map_const: soft version, no failure here (recovers hiding of consts, because a hidden name is illegal and rejected later);
wenzelm
parents:
26708
diff
changeset
|
132 |
| (_, (true, c)) => Const (c, map_type T) |
d1b8b6938b23
map_const: soft version, no failure here (recovers hiding of consts, because a hidden name is illegal and rejected later);
wenzelm
parents:
26708
diff
changeset
|
133 |
| (_, (false, c)) => (if NameSpace.is_qualified c then Const else Free) (c, map_type T)) |
22704 | 134 |
| decode (Var (xi, T)) = Var (xi, map_type T) |
135 |
| decode (t as Bound _) = t; |
|
136 |
in decode tm end; |
|
137 |
||
138 |
||
0 | 139 |
|
2584 | 140 |
(** output utils **) |
141 |
||
2699 | 142 |
(* term_of_sort *) |
2584 | 143 |
|
144 |
fun term_of_sort S = |
|
145 |
let |
|
5690 | 146 |
fun class c = Lexicon.const "_class" $ Lexicon.free c; |
2584 | 147 |
|
148 |
fun classes [] = sys_error "term_of_sort" |
|
149 |
| classes [c] = class c |
|
5690 | 150 |
| classes (c :: cs) = Lexicon.const "_classes" $ class c $ classes cs; |
2584 | 151 |
in |
152 |
(case S of |
|
5690 | 153 |
[] => Lexicon.const "_topsort" |
2584 | 154 |
| [c] => class c |
5690 | 155 |
| cs => Lexicon.const "_sort" $ classes cs) |
2584 | 156 |
end; |
157 |
||
158 |
||
159 |
(* term_of_typ *) |
|
0 | 160 |
|
161 |
fun term_of_typ show_sorts ty = |
|
162 |
let |
|
2584 | 163 |
fun of_sort t S = |
5690 | 164 |
if show_sorts then Lexicon.const "_ofsort" $ t $ term_of_sort S |
2584 | 165 |
else t; |
0 | 166 |
|
16614 | 167 |
fun term_of (Type (a, Ts)) = Term.list_comb (Lexicon.const a, map term_of Ts) |
5690 | 168 |
| term_of (TFree (x, S)) = of_sort (Lexicon.const "_tfree" $ Lexicon.free x) S |
169 |
| term_of (TVar (xi, S)) = of_sort (Lexicon.const "_tvar" $ Lexicon.var xi) S; |
|
8895 | 170 |
in term_of ty end; |
0 | 171 |
|
172 |
||
173 |
||
174 |
(** the type syntax **) |
|
175 |
||
10572 | 176 |
(* print mode *) |
177 |
||
178 |
val bracketsN = "brackets"; |
|
179 |
val no_bracketsN = "no_brackets"; |
|
180 |
||
181 |
fun no_brackets () = |
|
24613 | 182 |
find_first (fn mode => mode = bracketsN orelse mode = no_bracketsN) |
183 |
(print_mode_value ()) = SOME no_bracketsN; |
|
11312 | 184 |
|
185 |
val type_bracketsN = "type_brackets"; |
|
186 |
val no_type_bracketsN = "no_type_brackets"; |
|
187 |
||
188 |
fun no_type_brackets () = |
|
24613 | 189 |
find_first (fn mode => mode = type_bracketsN orelse mode = no_type_bracketsN) |
190 |
(print_mode_value ()) <> SOME type_bracketsN; |
|
10572 | 191 |
|
192 |
||
18 | 193 |
(* parse ast translations *) |
0 | 194 |
|
5690 | 195 |
fun tapp_ast_tr (*"_tapp"*) [ty, f] = Ast.Appl [f, ty] |
196 |
| 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
|
197 |
|
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
198 |
fun tappl_ast_tr (*"_tappl"*) [ty, tys, f] = |
5690 | 199 |
Ast.Appl (f :: ty :: Ast.unfold_ast "_types" tys) |
200 |
| tappl_ast_tr (*"_tappl"*) asts = raise Ast.AST ("tappl_ast_tr", asts); |
|
0 | 201 |
|
202 |
fun bracket_ast_tr (*"_bracket"*) [dom, cod] = |
|
5690 | 203 |
Ast.fold_ast_p "fun" (Ast.unfold_ast "_types" dom, cod) |
204 |
| bracket_ast_tr (*"_bracket"*) asts = raise Ast.AST ("bracket_ast_tr", asts); |
|
0 | 205 |
|
206 |
||
18 | 207 |
(* print ast translations *) |
0 | 208 |
|
5690 | 209 |
fun tappl_ast_tr' (f, []) = raise Ast.AST ("tappl_ast_tr'", [f]) |
210 |
| 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
|
211 |
| tappl_ast_tr' (f, ty :: tys) = |
5690 | 212 |
Ast.Appl [Ast.Constant "_tappl", ty, Ast.fold_ast "_types" tys, f]; |
0 | 213 |
|
214 |
fun fun_ast_tr' (*"fun"*) asts = |
|
16614 | 215 |
if no_brackets () orelse no_type_brackets () then raise Match |
10572 | 216 |
else |
217 |
(case Ast.unfold_ast_p "fun" (Ast.Appl (Ast.Constant "fun" :: asts)) of |
|
218 |
(dom as _ :: _ :: _, cod) |
|
219 |
=> Ast.Appl [Ast.Constant "_bracket", Ast.fold_ast "_types" dom, cod] |
|
220 |
| _ => raise Match); |
|
0 | 221 |
|
222 |
||
223 |
(* type_ext *) |
|
224 |
||
225 |
val sortT = Type ("sort", []); |
|
226 |
val classesT = Type ("classes", []); |
|
227 |
val typesT = Type ("types", []); |
|
228 |
||
5690 | 229 |
local open Lexicon SynExt in |
230 |
||
14903 | 231 |
val type_ext = syn_ext' false (K false) |
347
cd41a57221d0
changed translation of type applications according to new grammar;
wenzelm
parents:
330
diff
changeset
|
232 |
[Mfix ("_", tidT --> typeT, "", [], max_pri), |
239 | 233 |
Mfix ("_", tvarT --> typeT, "", [], max_pri), |
234 |
Mfix ("_", idT --> typeT, "", [], max_pri), |
|
3829 | 235 |
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
|
236 |
Mfix ("_::_", [tidT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
239 | 237 |
Mfix ("_::_", [tvarT, sortT] ---> typeT, "_ofsort", [max_pri, 0], max_pri), |
14838 | 238 |
Mfix ("'_()::_", sortT --> typeT, "_dummy_ofsort", [0], max_pri), |
239 | 239 |
Mfix ("_", idT --> sortT, "", [], max_pri), |
3829 | 240 |
Mfix ("_", longidT --> sortT, "", [], max_pri), |
2584 | 241 |
Mfix ("{}", sortT, "_topsort", [], max_pri), |
239 | 242 |
Mfix ("{_}", classesT --> sortT, "_sort", [], max_pri), |
243 |
Mfix ("_", idT --> classesT, "", [], max_pri), |
|
3829 | 244 |
Mfix ("_", longidT --> classesT, "", [], max_pri), |
239 | 245 |
Mfix ("_,_", [idT, classesT] ---> classesT, "_classes", [], max_pri), |
3829 | 246 |
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
|
247 |
Mfix ("_ _", [typeT, idT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
3829 | 248 |
Mfix ("_ _", [typeT, longidT] ---> typeT, "_tapp", [max_pri, 0], max_pri), |
9067 | 249 |
Mfix ("((1'(_,/ _')) _)", [typeT, typesT, idT] ---> typeT, "_tappl", [], max_pri), |
250 |
Mfix ("((1'(_,/ _')) _)", [typeT, typesT, longidT] ---> typeT, "_tappl", [], max_pri), |
|
239 | 251 |
Mfix ("_", typeT --> typesT, "", [], max_pri), |
252 |
Mfix ("_,/ _", [typeT, typesT] ---> typesT, "_types", [], max_pri), |
|
253 |
Mfix ("(_/ => _)", [typeT, typeT] ---> typeT, "fun", [1, 0], 0), |
|
624 | 254 |
Mfix ("([_]/ => _)", [typesT, typeT] ---> typeT, "_bracket", [0, 0], 0), |
2678 | 255 |
Mfix ("'(_')", typeT --> typeT, "", [0], max_pri), |
256 |
Mfix ("'_", typeT, "dummy", [], max_pri)] |
|
258 | 257 |
[] |
15750 | 258 |
(map SynExt.mk_trfun |
16614 | 259 |
[("_tapp", K tapp_ast_tr), ("_tappl", K tappl_ast_tr), ("_bracket", K bracket_ast_tr)], |
239 | 260 |
[], |
261 |
[], |
|
16614 | 262 |
map SynExt.mk_trfun [("fun", K fun_ast_tr')]) |
26708
fc2e7d2f763d
moved default token translations to proof_context.ML, if all fails the pretty printer falls back on plain output;
wenzelm
parents:
26039
diff
changeset
|
263 |
[] |
239 | 264 |
([], []); |
0 | 265 |
|
266 |
end; |
|
5690 | 267 |
|
268 |
end; |