/[svn]/typing/typer.ml
ViewVC logotype

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 384 - (show annotations)
Tue Jul 10 17:30:36 2007 UTC (5 years, 11 months ago) by abate
File size: 32588 byte(s)
[r2003-05-21 21:24:56 by cvscast] Manual

Original author: cvscast
Date: 2003-05-21 21:24:56+00:00
1 (* TODO:
2 - rewrite type-checking of operators to propagate constraint
3 - optimize computation of pattern free variables
4 - check whether it is worth using recursive hash-consing internally
5 *)
6
7
8 let warning loc msg =
9 Format.fprintf !Location.warning_ppf "Warning %a:@\n%a%s@\n"
10 Location.print_loc loc
11 Location.html_hilight loc
12 msg
13
14 (* I. Transform the abstract syntax of types and patterns into
15 the internal form *)
16
17 open Location
18 open Ast
19 open Ident
20
21 module S = struct type t = string let compare = compare end
22 module TypeEnv = Map.Make(S)
23 module Env = Map.Make(Id)
24
25
26 exception NonExhaustive of Types.descr
27 exception Constraint of Types.descr * Types.descr * string
28 exception ShouldHave of Types.descr * string
29 exception ShouldHave2 of Types.descr * string * Types.descr
30 exception WrongLabel of Types.descr * label
31 exception UnboundId of id
32
33 let raise_loc loc exn = raise (Location (loc,exn))
34
35
36 (* Eliminate Recursion, propagate Sequence Capture Variables *)
37
38 let rec seq_vars accu = function
39 | Epsilon | Elem _ -> accu
40 | Seq (r1,r2) | Alt (r1,r2) -> seq_vars (seq_vars accu r1) r2
41 | Star r | WeakStar r -> seq_vars accu r
42 | SeqCapture (v,r) -> seq_vars (IdSet.add v accu) r
43
44 type derecurs_slot = {
45 ploc : Location.loc;
46 pid : int;
47 mutable ploop : bool;
48 mutable pdescr : derecurs option
49 } and derecurs =
50 | PAlias of derecurs_slot
51 | PType of Types.descr
52 | POr of derecurs * derecurs
53 | PAnd of derecurs * derecurs
54 | PDiff of derecurs * derecurs
55 | PTimes of derecurs * derecurs
56 | PXml of derecurs * derecurs
57 | PArrow of derecurs * derecurs
58 | POptional of derecurs
59 | PRecord of bool * derecurs label_map
60 | PCapture of id
61 | PConstant of id * Types.const
62 | PRegexp of derecurs_regexp * derecurs
63 and derecurs_regexp =
64 | PEpsilon
65 | PElem of derecurs
66 | PSeq of derecurs_regexp * derecurs_regexp
67 | PAlt of derecurs_regexp * derecurs_regexp
68 | PStar of derecurs_regexp
69 | PWeakStar of derecurs_regexp
70
71 let rec hash_derecurs = function
72 | PAlias s -> s.pid
73 | PType t -> 1 + 17 * (Types.hash_descr t)
74 | POr (p1,p2) -> 2 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
75 | PAnd (p1,p2) -> 3 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
76 | PDiff (p1,p2) -> 4 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
77 | PTimes (p1,p2) -> 5 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
78 | PXml (p1,p2) -> 6 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
79 | PArrow (p1,p2) -> 7 + 17 * (hash_derecurs p1) + 257 * (hash_derecurs p2)
80 | POptional p -> 8 + 17 * (hash_derecurs p)
81 | PRecord (o,r) -> (if o then 9 else 10) + 17 * (LabelMap.hash hash_derecurs r)
82 | PCapture x -> 11 + 17 * (Id.hash x)
83 | PConstant (x,c) -> 12 + 17 * (Id.hash x) + 257 * (Types.hash_const c)
84 | PRegexp (p,q) -> 13 + 17 * (hash_derecurs_regexp p) + 257 * (hash_derecurs q)
85 and hash_derecurs_regexp = function
86 | PEpsilon -> 1
87 | PElem p -> 2 + 17 * (hash_derecurs p)
88 | PSeq (p1,p2) -> 3 + 17 * (hash_derecurs_regexp p1) + 257 * (hash_derecurs_regexp p2)
89 | PAlt (p1,p2) -> 4 + 17 * (hash_derecurs_regexp p1) + 257 * (hash_derecurs_regexp p2)
90 | PStar p -> 5 + 17 * (hash_derecurs_regexp p)
91 | PWeakStar p -> 6 + 17 * (hash_derecurs_regexp p)
92
93 let rec equal_derecurs p1 p2 = (p1 == p2) || match p1,p2 with
94 | PAlias s1, PAlias s2 -> s1 == s2
95 | PType t1, PType t2 -> Types.equal_descr t1 t2
96 | POr (p1,q1), POr (p2,q2)
97 | PAnd (p1,q1), PAnd (p2,q2)
98 | PDiff (p1,q1), PDiff (p2,q2)
99 | PTimes (p1,q1), PTimes (p2,q2)
100 | PXml (p1,q1), PXml (p2,q2)
101 | PArrow (p1,q1), PArrow (p2,q2) -> (equal_derecurs p1 p2) && (equal_derecurs q1 q2)
102 | POptional p1, POptional p2 -> equal_derecurs p1 p2
103 | PRecord (o1,r1), PRecord (o2,r2) -> (o1 == o2) && (LabelMap.equal equal_derecurs r1 r2)
104 | PCapture x1, PCapture x2 -> Id.equal x1 x2
105 | PConstant (x1,c1), PConstant (x2,c2) -> (Id.equal x1 x2) && (Types.equal_const c1 c2)
106 | PRegexp (p1,q1), PRegexp (p2,q2) -> (equal_derecurs_regexp p1 p2) && (equal_derecurs q1 q2)
107 | _ -> false
108 and equal_derecurs_regexp r1 r2 = match r1,r2 with
109 | PEpsilon, PEpsilon -> true
110 | PElem p1, PElem p2 -> equal_derecurs p1 p2
111 | PSeq (p1,q1), PSeq (p2,q2)
112 | PAlt (p1,q1), PAlt (p2,q2) -> (equal_derecurs_regexp p1 p2) && (equal_derecurs_regexp q1 q2)
113 | PStar p1, PStar p2
114 | PWeakStar p1, PWeakStar p2 -> equal_derecurs_regexp p1 p2
115 | _ -> false
116
117 module DerecursTable = Hashtbl.Make(
118 struct
119 type t = derecurs
120 let hash = hash_derecurs
121 let equal = equal_derecurs
122 end
123 )
124
125 module RE = Hashtbl.Make(
126 struct
127 type t = derecurs_regexp * derecurs
128 let hash (p,q) = (hash_derecurs_regexp p) + 17 * (hash_derecurs q)
129 let equal (p1,q1) (p2,q2) = (equal_derecurs_regexp p1 p2) && (equal_derecurs q1 q2)
130 end
131 )
132
133
134 let counter = State.ref "Typer.counter - derecurs" 0
135 let mk_slot loc =
136 incr counter;
137 { ploop = false; ploc = loc; pid = !counter; pdescr = None }
138
139 let rec derecurs env p = match p.descr with
140 | PatVar v ->
141 (try PAlias (TypeEnv.find v env)
142 with Not_found -> raise_loc_generic p.loc ("Undefined type/pattern " ^ v))
143 | Recurs (p,b) -> derecurs (derecurs_def env b) p
144 | Internal t -> PType t
145 | Or (p1,p2) -> POr (derecurs env p1, derecurs env p2)
146 | And (p1,p2) -> PAnd (derecurs env p1, derecurs env p2)
147 | Diff (p1,p2) -> PDiff (derecurs env p1, derecurs env p2)
148 | Prod (p1,p2) -> PTimes (derecurs env p1, derecurs env p2)
149 | XmlT (p1,p2) -> PXml (derecurs env p1, derecurs env p2)
150 | Arrow (p1,p2) -> PArrow (derecurs env p1, derecurs env p2)
151 | Optional p -> POptional (derecurs env p)
152 | Record (o,r) -> PRecord (o, LabelMap.map (derecurs env) r)
153 | Capture x -> PCapture x
154 | Constant (x,c) -> PConstant (x,c)
155 | Regexp (r,q) ->
156 let constant_nil t v = PAnd (t, PConstant (v, Types.Atom Sequence.nil_atom)) in
157 let vars = seq_vars IdSet.empty r in
158 let q = IdSet.fold constant_nil (derecurs env q) vars in
159 let r = derecurs_regexp (fun p -> p) env r in
160 PRegexp (r, q)
161 and derecurs_regexp vars env = function
162 | Epsilon -> PEpsilon
163 | Elem p -> PElem (vars (derecurs env p))
164 | Seq (p1,p2) -> PSeq (derecurs_regexp vars env p1, derecurs_regexp vars env p2)
165 | Alt (p1,p2) -> PAlt (derecurs_regexp vars env p1, derecurs_regexp vars env p2)
166 | Star p -> PStar (derecurs_regexp vars env p)
167 | WeakStar p -> PWeakStar (derecurs_regexp vars env p)
168 | SeqCapture (x,p) -> derecurs_regexp (fun p -> PAnd (vars p, PCapture x)) env p
169
170
171 and derecurs_def env b =
172 let b = List.map (fun (v,p) -> (v,p,mk_slot p.loc)) b in
173 let env = List.fold_left (fun env (v,p,s) -> TypeEnv.add v s env) env b in
174 List.iter (fun (v,p,s) -> s.pdescr <- Some (derecurs env p)) b;
175 env
176
177 (* Stratification and recursive hash-consing *)
178
179 type descr =
180 | IType of Types.descr
181 | IOr of descr * descr
182 | IAnd of descr * descr
183 | IDiff of descr * descr
184 | ITimes of slot * slot
185 | IXml of slot * slot
186 | IArrow of slot * slot
187 | IOptional of descr
188 | IRecord of bool * slot label_map
189 | ICapture of id
190 | IConstant of id * Types.const
191 and slot = {
192 mutable fv : fv option;
193 mutable hash : int option;
194 mutable rank1: int; mutable rank2: int;
195 mutable gen1 : int; mutable gen2: int;
196 mutable d : descr option
197 }
198
199 let descr s =
200 match s.d with
201 | Some d -> d
202 | None -> assert false
203
204 let gen = ref 0
205 let rank = ref 0
206
207 let rec hash_descr = function
208 | IType x -> Types.hash_descr x
209 | IOr (d1,d2) -> 1 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
210 | IAnd (d1,d2) -> 2 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
211 | IDiff (d1,d2) -> 3 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
212 | IOptional d -> 4 + 17 * (hash_descr d)
213 | ITimes (s1,s2) -> 5 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
214 | IXml (s1,s2) -> 6 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
215 | IArrow (s1,s2) -> 7 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
216 | IRecord (o,r) -> (if o then 8 else 9) + 17 * (LabelMap.hash hash_slot r)
217 | ICapture x -> 10 + 17 * (Id.hash x)
218 | IConstant (x,y) -> 11 + 17 * (Id.hash x) + 257 * (Types.hash_const y)
219 and hash_slot s =
220 if s.gen1 = !gen then 13 * s.rank1
221 else (
222 incr rank;
223 s.rank1 <- !rank; s.gen1 <- !gen;
224 hash_descr (descr s)
225 )
226
227 let rec equal_descr d1 d2 =
228 match (d1,d2) with
229 | IType x1, IType x2 -> Types.equal_descr x1 x2
230 | IOr (x1,y1), IOr (x2,y2)
231 | IAnd (x1,y1), IAnd (x2,y2)
232 | IDiff (x1,y1), IDiff (x2,y2) -> (equal_descr x1 x2) && (equal_descr y1 y2)
233 | IOptional x1, IOptional x2 -> equal_descr x1 x2
234 | ITimes (x1,y1), ITimes (x2,y2)
235 | IXml (x1,y1), IXml (x2,y2)
236 | IArrow (x1,y1), IArrow (x2,y2) -> (equal_slot x1 x2) && (equal_slot y1 y2)
237 | IRecord (o1,r1), IRecord (o2,r2) -> (o1 = o2) && (LabelMap.equal equal_slot r1 r2)
238 | ICapture x1, ICapture x2 -> Id.equal x1 x2
239 | IConstant (x1,y1), IConstant (x2,y2) -> (Id.equal x1 x2) && (Types.equal_const y1 y2)
240 | _ -> false
241 and equal_slot s1 s2 =
242 ((s1.gen1 = !gen) && (s2.gen2 = !gen) && (s1.rank1 = s2.rank2))
243 ||
244 ((s1.gen1 <> !gen) && (s2.gen2 <> !gen) && (
245 incr rank;
246 s1.rank1 <- !rank; s1.gen1 <- !gen;
247 s2.rank2 <- !rank; s2.gen2 <- !gen;
248 equal_descr (descr s1) (descr s2)
249 ))
250
251 module Arg = struct
252 type t = slot
253
254 let hash s =
255 match s.hash with
256 | Some h -> h
257 | None ->
258 incr gen; rank := 0;
259 let h = hash_slot s in
260 s.hash <- Some h;
261 h
262
263 let equal s1 s2 =
264 (s1 == s2) ||
265 (incr gen; rank := 0;
266 let e = equal_slot s1 s2 in
267 (* if e then Printf.eprintf "Recursive hash-consig: Equal\n"; *)
268 e)
269 end
270 module SlotTable = Hashtbl.Make(Arg)
271
272 let rec fv_slot s =
273 match s.fv with
274 | Some x -> x
275 | None ->
276 if s.gen1 = !gen then IdSet.empty
277 else (s.gen1 <- !gen; fv_descr (descr s))
278 and fv_descr = function
279 | IType _ -> IdSet.empty
280 | IOr (d1,d2)
281 | IAnd (d1,d2)
282 | IDiff (d1,d2) -> IdSet.cup (fv_descr d1) (fv_descr d2)
283 | IOptional d -> fv_descr d
284 | ITimes (s1,s2)
285 | IXml (s1,s2)
286 | IArrow (s1,s2) -> IdSet.cup (fv_slot s1) (fv_slot s2)
287 | IRecord (o,r) -> List.fold_left IdSet.cup IdSet.empty (LabelMap.map_to_list fv_slot r)
288 | ICapture x | IConstant (x,_) -> IdSet.singleton x
289
290
291 let compute_fv s =
292 match s.fv with
293 | Some x -> ()
294 | None ->
295 incr gen;
296 let x = fv_slot s in
297 s.fv <- Some x
298
299
300 let todo_fv = ref []
301
302 let mk () =
303 let s =
304 { d = None;
305 fv = None;
306 hash = None;
307 rank1 = 0; rank2 = 0;
308 gen1 = 0; gen2 = 0 } in
309 todo_fv := s :: !todo_fv;
310 s
311
312 let flush_fv () =
313 List.iter compute_fv !todo_fv;
314 todo_fv := []
315
316 let compile_slot_hash = DerecursTable.create 67
317 let compile_hash = DerecursTable.create 67
318
319 let defs = ref []
320
321 let rec compile p =
322 try DerecursTable.find compile_hash p
323 with Not_found ->
324 let c = real_compile p in
325 DerecursTable.replace compile_hash p c;
326 c
327 and real_compile = function
328 | PAlias v ->
329 if v.ploop then
330 raise_loc_generic v.ploc ("Unguarded recursion on type/pattern");
331 v.ploop <- true;
332 let r = match v.pdescr with Some x -> compile x | _ -> assert false in
333 v.ploop <- false;
334 r
335 | PType t -> IType t
336 | POr (t1,t2) -> IOr (compile t1, compile t2)
337 | PAnd (t1,t2) -> IAnd (compile t1, compile t2)
338 | PDiff (t1,t2) -> IDiff (compile t1, compile t2)
339 | PTimes (t1,t2) -> ITimes (compile_slot t1, compile_slot t2)
340 | PXml (t1,t2) -> IXml (compile_slot t1, compile_slot t2)
341 | PArrow (t1,t2) -> IArrow (compile_slot t1, compile_slot t2)
342 | POptional t -> IOptional (compile t)
343 | PRecord (o,r) -> IRecord (o, LabelMap.map compile_slot r)
344 | PConstant (x,v) -> IConstant (x,v)
345 | PCapture x -> ICapture x
346 | PRegexp (r,q) -> compile_regexp r q
347 and compile_regexp r q =
348 let memo = RE.create 17 in
349 let rec aux accu r q =
350 if RE.mem memo (r,q) then accu
351 else (
352 RE.add memo (r,q) ();
353 match r with
354 | PEpsilon -> (match q with PRegexp (r,q) -> aux accu r q | _ -> (compile q) :: accu)
355 | PElem p -> ITimes (compile_slot p, compile_slot q) :: accu
356 | PSeq (r1,r2) -> aux accu r1 (PRegexp (r2,q))
357 | PAlt (r1,r2) -> aux (aux accu r1 q) r2 q
358 | PStar r1 -> aux (aux accu r1 (PRegexp (r,q))) PEpsilon q
359 | PWeakStar r1 -> aux (aux accu PEpsilon q) r1 (PRegexp (r,q))
360 )
361 in
362 let accu = aux [] r q in
363 match accu with
364 | [] -> assert false
365 | p::l -> List.fold_left (fun acc p -> IOr (p,acc)) p l
366 and compile_slot p =
367 try DerecursTable.find compile_slot_hash p
368 with Not_found ->
369 let s = mk () in
370 defs := (s,p) :: !defs;
371 DerecursTable.add compile_slot_hash p s;
372 s
373
374
375 let rec flush_defs () =
376 match !defs with
377 | [] -> ()
378 | (s,p)::t -> defs := t; s.d <- Some (compile p); flush_defs ()
379
380 let typ_nodes = SlotTable.create 67
381 let pat_nodes = SlotTable.create 67
382
383 let rec typ = function
384 | IType t -> t
385 | IOr (s1,s2) -> Types.cup (typ s1) (typ s2)
386 | IAnd (s1,s2) -> Types.cap (typ s1) (typ s2)
387 | IDiff (s1,s2) -> Types.diff (typ s1) (typ s2)
388 | ITimes (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
389 | IXml (s1,s2) -> Types.xml (typ_node s1) (typ_node s2)
390 | IArrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
391 | IOptional s -> Types.Record.or_absent (typ s)
392 | IRecord (o,r) -> Types.record' (o, LabelMap.map typ_node r)
393 | ICapture x | IConstant (x,_) -> assert false
394
395 and typ_node s : Types.node =
396 try SlotTable.find typ_nodes s
397 with Not_found ->
398 let x = Types.make () in
399 SlotTable.add typ_nodes s x;
400 Types.define x (typ (descr s));
401 x
402
403 let rec pat d : Patterns.descr =
404 if IdSet.is_empty (fv_descr d)
405 then Patterns.constr (typ d)
406 else pat_aux d
407
408
409 and pat_aux = function
410 | IOr (s1,s2) -> Patterns.cup (pat s1) (pat s2)
411 | IAnd (s1,s2) -> Patterns.cap (pat s1) (pat s2)
412 | IDiff (s1,s2) when IdSet.is_empty (fv_descr s2) ->
413 let s2 = Types.neg (typ s2) in
414 Patterns.cap (pat s1) (Patterns.constr s2)
415 | IDiff _ ->
416 raise (Patterns.Error "Difference not allowed in patterns")
417 | ITimes (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
418 | IXml (s1,s2) -> Patterns.xml (pat_node s1) (pat_node s2)
419 | IOptional _ ->
420 raise (Patterns.Error "Optional field not allowed in record patterns")
421 | IRecord (o,r) ->
422 let pats = ref [] in
423 let aux l s =
424 if IdSet.is_empty (fv_slot s) then typ_node s
425 else
426 ( pats := Patterns.record l (pat_node s) :: !pats;
427 Types.any_node )
428 in
429 let constr = Types.record' (o,LabelMap.mapi aux r) in
430 List.fold_left Patterns.cap (Patterns.constr constr) !pats
431 (* TODO: can avoid constr when o=true, and all fields have fv *)
432 | ICapture x -> Patterns.capture x
433 | IConstant (x,c) -> Patterns.constant x c
434 | IArrow _ ->
435 raise (Patterns.Error "Arrow not allowed in patterns")
436 | IType _ -> assert false
437
438 and pat_node s : Patterns.node =
439 try SlotTable.find pat_nodes s
440 with Not_found ->
441 let x = Patterns.make (fv_slot s) in
442 SlotTable.add pat_nodes s x;
443 Patterns.define x (pat (descr s));
444 x
445
446 let glb = State.ref "Typer.glb_env" TypeEnv.empty
447
448
449 let register_global_types b =
450 List.iter
451 (fun (v,p) ->
452 if TypeEnv.mem v !glb
453 then raise_loc_generic p.loc ("Multiple definition for type " ^ v)
454 ) b;
455 glb := derecurs_def !glb b;
456 let b = List.map (fun (v,p) -> (v,p,compile (derecurs !glb p))) b in
457 flush_defs ();
458 flush_fv ();
459 List.iter
460 (fun (v,p,s) ->
461 if not (IdSet.is_empty (fv_descr s)) then
462 raise_loc_generic p.loc "Capture variables are not allowed in types";
463 let t = typ s in
464 if (p.loc <> noloc) && (Types.is_empty t) then
465 warning p.loc ("This definition yields an empty type for " ^ v);
466 Types.Print.register_global v t) b
467
468 let dump_global_types ppf =
469 TypeEnv.iter (fun v _ -> Format.fprintf ppf " %s" v) !glb
470
471
472 let typ p =
473 let s = compile_slot (derecurs !glb p) in
474 flush_defs ();
475 flush_fv ();
476 if IdSet.is_empty (fv_slot s) then typ_node s
477 else raise_loc_generic p.loc "Capture variables are not allowed in types"
478
479 let pat p =
480 let s = compile_slot (derecurs !glb p) in
481 flush_defs ();
482 flush_fv ();
483 try pat_node s
484 with Patterns.Error e -> raise_loc_generic p.loc e
485 | Location (loc,exn) when loc = noloc -> raise (Location (p.loc, exn))
486
487
488 (* II. Build skeleton *)
489
490 module Fv = IdSet
491
492 let all_branches = ref []
493
494 (* IDEA: introduce a node Loc in the AST to override nolocs
495 in sub-expressions *)
496
497 let exp loc fv e =
498 fv,
499 { Typed.exp_loc = loc;
500 Typed.exp_typ = Types.empty;
501 Typed.exp_descr = e;
502 }
503
504
505 let rec expr loc = function
506 | LocatedExpr (loc,e) -> expr loc e
507 | Forget (e,t) ->
508 let (fv,e) = expr loc e and t = typ t in
509 exp loc fv (Typed.Forget (e,t))
510 | Var s ->
511 exp loc (Fv.singleton s) (Typed.Var s)
512 | Apply (e1,e2) ->
513 let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
514 exp loc (Fv.cup fv1 fv2) (Typed.Apply (e1,e2))
515 | Abstraction a ->
516 let iface = List.map (fun (t1,t2) -> (typ t1, typ t2))
517 a.fun_iface in
518 let t = List.fold_left
519 (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2))
520 Types.any iface in
521 let iface = List.map
522 (fun (t1,t2) -> (Types.descr t1, Types.descr t2))
523 iface in
524 let (fv0,body) = branches a.fun_body in
525 let fv = match a.fun_name with
526 | None -> fv0
527 | Some f -> Fv.remove f fv0 in
528 let e = Typed.Abstraction
529 { Typed.fun_name = a.fun_name;
530 Typed.fun_iface = iface;
531 Typed.fun_body = body;
532 Typed.fun_typ = t;
533 Typed.fun_fv = fv
534 } in
535 exp loc fv e
536 | Cst c ->
537 exp loc Fv.empty (Typed.Cst c)
538 | Pair (e1,e2) ->
539 let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
540 exp loc (Fv.cup fv1 fv2) (Typed.Pair (e1,e2))
541 | Xml (e1,e2) ->
542 let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
543 exp loc (Fv.cup fv1 fv2) (Typed.Xml (e1,e2))
544 | Dot (e,l) ->
545 let (fv,e) = expr loc e in
546 exp loc fv (Typed.Dot (e,l))
547 | RemoveField (e,l) ->
548 let (fv,e) = expr loc e in
549 exp loc fv (Typed.RemoveField (e,l))
550 | RecordLitt r ->
551 let fv = ref Fv.empty in
552 let r = LabelMap.map
553 (fun e ->
554 let (fv2,e) = expr loc e
555 in fv := Fv.cup !fv fv2; e)
556 r in
557 exp loc !fv (Typed.RecordLitt r)
558 | Op (op,le) ->
559 let (fvs,ltes) = List.split (List.map (expr loc) le) in
560 let fv = List.fold_left Fv.cup Fv.empty fvs in
561 exp loc fv (Typed.Op (op,ltes))
562 | Match (e,b) ->
563 let (fv1,e) = expr loc e
564 and (fv2,b) = branches b in
565 exp loc (Fv.cup fv1 fv2) (Typed.Match (e, b))
566 | Map (def,e,b) ->
567 let (fv1,e) = expr loc e
568 and (fv2,b) = branches b in
569 exp loc (Fv.cup fv1 fv2) (Typed.Map (def,e, b))
570 | Xtrans (e,b) ->
571 let (fv1,e) = expr loc e
572 and (fv2,b) = branches b in
573 exp loc (Fv.cup fv1 fv2) (Typed.Xtrans (e, b))
574 | Try (e,b) ->
575 let (fv1,e) = expr loc e
576 and (fv2,b) = branches b in
577 exp loc (Fv.cup fv1 fv2) (Typed.Try (e, b))
578
579
580 and branches b =
581 let fv = ref Fv.empty in
582 let accept = ref Types.empty in
583 let branch (p,e) =
584 let (fv2,e) = expr noloc e in
585 let br_loc = merge_loc p.loc e.Typed.exp_loc in
586 let p = pat p in
587 let fv2 = Fv.diff fv2 (Patterns.fv p) in
588 fv := Fv.cup !fv fv2;
589 accept := Types.cup !accept (Types.descr (Patterns.accept p));
590 let br =
591 {
592 Typed.br_loc = br_loc;
593 Typed.br_used = br_loc = noloc;
594 Typed.br_pat = p;
595 Typed.br_body = e } in
596 all_branches := br :: !all_branches;
597 br in
598 let b = List.map branch b in
599 (!fv,
600 {
601 Typed.br_typ = Types.empty;
602 Typed.br_branches = b;
603 Typed.br_accept = !accept;
604 Typed.br_compiled = None;
605 }
606 )
607
608 let expr = expr noloc
609
610 let let_decl p e =
611 let (_,e) = expr e in
612 { Typed.let_pat = pat p;
613 Typed.let_body = e;
614 Typed.let_compiled = None }
615
616 (* III. Type-checks *)
617
618 let int_cup_record = Types.cup Types.Int.any Types.Record.any
619
620
621 type env = Types.descr Env.t
622
623 open Typed
624
625
626 let check loc t s msg =
627 if not (Types.subtype t s) then raise_loc loc (Constraint (t, s, msg))
628
629 let rec type_check env e constr precise =
630 (* Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
631 Types.Print.print_descr constr precise;
632 *)
633 let d = type_check' e.exp_loc env e.exp_descr constr precise in
634 e.exp_typ <- Types.cup e.exp_typ d;
635 d
636
637 and type_check' loc env e constr precise = match e with
638 | Forget (e,t) ->
639 let t = Types.descr t in
640 ignore (type_check env e t false);
641 t
642 | Abstraction a ->
643 let t =
644 try Types.Arrow.check_strenghten a.fun_typ constr
645 with Not_found ->
646 raise_loc loc
647 (ShouldHave
648 (constr, "but the interface of the abstraction is not compatible"))
649 in
650 let env = match a.fun_name with
651 | None -> env
652 | Some f -> Env.add f a.fun_typ env in
653 List.iter
654 (fun (t1,t2) ->
655 let acc = a.fun_body.br_accept in
656 if not (Types.subtype t1 acc) then
657 raise_loc loc (NonExhaustive (Types.diff t1 acc));
658 ignore (type_check_branches loc env t1 a.fun_body t2 false)
659 ) a.fun_iface;
660 t
661
662 | Match (e,b) ->
663 let t = type_check env e b.br_accept true in
664 type_check_branches loc env t b constr precise
665
666 | Try (e,b) ->
667 let te = type_check env e constr precise in
668 let tb = type_check_branches loc env Types.any b constr precise in
669 Types.cup te tb
670
671 | Pair (e1,e2) ->
672 type_check_pair loc env e1 e2 constr precise
673 | Xml (e1,e2) ->
674 type_check_pair ~kind:`XML loc env e1 e2 constr precise
675
676 | RecordLitt r ->
677 (* try to get rid of precise = true for values of fields *)
678 (* also: the use equivalent of need_second to optimize... *)
679 let precise = true in
680 if not (Types.Record.has_record constr) then
681 raise_loc loc (ShouldHave (constr,"but it is a record."));
682 let (rconstr,res) =
683 List.fold_left
684 (fun (rconstr,res) (l,e) ->
685 (* could compute (split l e) once... *)
686 let pi = Types.Record.project_opt rconstr l in
687 if Types.is_empty pi then
688 raise_loc loc
689 (ShouldHave (constr,(Printf.sprintf
690 "Field %s is not allowed here."
691 (U.to_string (LabelPool.value l))
692 )
693 ));
694 let t = type_check env e pi true in
695 let rconstr = Types.Record.condition rconstr l t in
696 let res = if precise then (l,Types.cons t) :: res else res in
697 (rconstr,res)
698 ) (constr, []) (LabelMap.get r)
699 in
700 if not (Types.Record.has_empty_record rconstr) then
701 raise_loc loc
702 (ShouldHave (constr,"More field should be present"));
703 let t =
704 Types.record' (false, LabelMap.from_list (fun _ _ -> assert false) res)
705 in
706 check loc t constr "";
707 t
708 (*
709 if precise then
710 Types.record' (false, LabelMap.from_list (fun _ _ -> assert false) res)
711 else constr
712 *)
713 | Map (def,e,b) ->
714 let acc = if def then Sequence.any else Sequence.star b.br_accept in
715 let t = type_check env e acc true in
716
717 let constr' = Sequence.approx (Types.cap Sequence.any constr) in
718 let exact = Types.subtype (Sequence.star constr') constr in
719 (* Note:
720 - could be more precise by integrating the decomposition
721 of constr inside Sequence.map.
722 *)
723 let res =
724 Sequence.map
725 (fun t ->
726 let res =
727 type_check_branches loc env t b constr' (precise || (not exact)) in
728 if def && not (Types.subtype t b.br_accept)
729 then Types.cup res Sequence.nil_type
730 else res)
731 t in
732 if not exact then check loc res constr "";
733 if precise then res else constr
734 | Op ("@", [e1;e2]) ->
735 let constr' = Sequence.star
736 (Sequence.approx (Types.cap Sequence.any constr)) in
737 let exact = Types.subtype constr' constr in
738 if exact then
739 let t1 = type_check env e1 constr' precise
740 and t2 = type_check env e2 constr' precise in
741 if precise then Sequence.concat t1 t2 else constr
742 else
743 (* Note:
744 the knownledge of t1 may makes it useless to
745 check t2 with 'precise' ... *)
746 let t1 = type_check env e1 constr' true
747 and t2 = type_check env e2 constr' true in
748 let res = Sequence.concat t1 t2 in
749 check loc res constr "";
750 if precise then res else constr
751 | Apply (e1,e2) ->
752 let t1 = type_check env e1 Types.Arrow.any true in
753 let t1 = Types.Arrow.get t1 in
754 let dom = Types.Arrow.domain t1 in
755 let res =
756 if Types.Arrow.need_arg t1 then
757 let t2 = type_check env e2 dom true in
758 Types.Arrow.apply t1 t2
759 else
760 (ignore (type_check env e2 dom false); Types.Arrow.apply_noarg t1)
761 in
762 check loc res constr "";
763 res
764 | Op ("flatten", [e]) ->
765 let constr' = Sequence.star
766 (Sequence.approx (Types.cap Sequence.any constr)) in
767 let sconstr' = Sequence.star constr' in
768 let exact = Types.subtype constr' constr in
769 if exact then
770 let t = type_check env e sconstr' precise in
771 if precise then Sequence.flatten t else constr
772 else
773 let t = type_check env e sconstr' true in
774 let res = Sequence.flatten t in
775 check loc res constr "";
776 if precise then res else constr
777 | Op ("atom_of", [e]) ->
778 let t = type_check env e Sequence.string false in
779 Types.atom Atoms.any
780 | _ ->
781 let t : Types.descr = compute_type' loc env e in
782 check loc t constr "";
783 t
784
785 and type_check_pair ?(kind=`Normal) loc env e1 e2 constr precise =
786 let rects = Types.Product.normal ~kind constr in
787 if Types.Product.is_empty rects then
788 (match kind with
789 | `Normal -> raise_loc loc (ShouldHave (constr,"but it is a pair."))
790 | `XML -> raise_loc loc (ShouldHave (constr,"but it is an XML element.")));
791 let need_s = Types.Product.need_second rects in
792 let t1 = type_check env e1 (Types.Product.pi1 rects) (precise || need_s) in
793 let c2 = Types.Product.constraint_on_2 rects t1 in
794 if Types.is_empty c2 then
795 raise_loc loc (ShouldHave2 (constr,"but the first component has type",t1));
796 let t2 = type_check env e2 c2 precise in
797
798 if precise then
799 match kind with
800 | `Normal -> Types.times (Types.cons t1) (Types.cons t2)
801 | `XML -> Types.xml (Types.cons t1) (Types.cons t2)
802 else
803 constr
804
805
806 and compute_type env e =
807 type_check env e Types.any true
808
809 and compute_type' loc env = function
810 | Var s ->
811 (try Env.find s env
812 with Not_found -> raise_loc loc (UnboundId s)
813 )
814 | Cst c -> Types.constant c
815 | Dot (e,l) ->
816 let t = type_check env e Types.Record.any true in
817 (try (Types.Record.project t l)
818 with Not_found -> raise_loc loc (WrongLabel(t,l)))
819 | RemoveField (e,l) ->
820 let t = type_check env e Types.Record.any true in
821 Types.Record.remove_field t l
822 | Op (op, el) ->
823 let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
824 type_op loc op args
825 | Xtrans (e,b) ->
826 let t = type_check env e Sequence.any true in
827 let r =
828 Sequence.map_tree
829 (fun t ->
830 let resid = Types.diff t b.br_accept in
831 let res = type_check_branches loc env t b Sequence.any true in
832 (res,resid)
833 ) t
834 in
835 r
836
837 (* We keep these cases here to allow comparison and benchmarking ...
838 Just comment the corresponding cases in type_check' to
839 activate these ones.
840 *)
841 | Pair (e1,e2) ->
842 let t1 = compute_type env e1
843 and t2 = compute_type env e2 in
844 Types.times (Types.cons t1) (Types.cons t2)
845 | RecordLitt r ->
846 let r = LabelMap.map (fun e -> Types.cons (compute_type env e)) r in
847 Types.record' (false,r)
848 | _ -> assert false
849
850 and type_check_branches loc env targ brs constr precise =
851 if Types.is_empty targ then Types.empty
852 else (
853 brs.br_typ <- Types.cup brs.br_typ targ;
854 branches_aux loc env targ
855 (if precise then Types.empty else constr)
856 constr precise brs.br_branches
857 )
858
859 and branches_aux loc env targ tres constr precise = function
860 | [] -> tres
861 | b :: rem ->
862 let p = b.br_pat in
863 let acc = Types.descr (Patterns.accept p) in
864
865 let targ' = Types.cap targ acc in
866 if Types.is_empty targ'
867 then branches_aux loc env targ tres constr precise rem
868 else
869 ( b.br_used <- true;
870 let res = Patterns.filter targ' p in
871 let env' = List.fold_left
872 (fun env (x,t) -> Env.add x (Types.descr t) env)
873 env res in
874 let t = type_check env' b.br_body constr precise in
875 let tres = if precise then Types.cup t tres else tres in
876 let targ'' = Types.diff targ acc in
877 if (Types.non_empty targ'') then
878 branches_aux loc env targ'' tres constr precise rem
879 else
880 tres
881 )
882
883 and type_let_decl env l =
884 let acc = Types.descr (Patterns.accept l.let_pat) in
885 let t = type_check env l.let_body acc true in
886 let res = Patterns.filter t l.let_pat in
887 List.map (fun (x,t) -> (x, Types.descr t)) res
888
889 and type_rec_funs env l =
890 let types =
891 List.fold_left
892 (fun accu -> function {let_body={exp_descr=Abstraction a}} as l ->
893 let t = a.fun_typ in
894 let acc = Types.descr (Patterns.accept l.let_pat) in
895 if not (Types.subtype t acc) then
896 raise_loc l.let_body.exp_loc (NonExhaustive (Types.diff t acc));
897 let res = Patterns.filter t l.let_pat in
898 List.fold_left (fun accu (x,t) -> (x, Types.descr t)::accu) accu res
899 | _ -> assert false) [] l
900 in
901 let env' = List.fold_left (fun env (x,t) -> Env.add x t env) env types in
902 List.iter
903 (function { let_body = { exp_descr = Abstraction a } } as l ->
904 ignore (type_check env' l.let_body Types.any false)
905 | _ -> assert false) l;
906 types
907
908
909 and type_op loc op args =
910 match (op,args) with
911 | "+", [loc1,t1; loc2,t2] ->
912 if (Types.is_empty t1) || (Types.is_empty t2) then Types.empty
913 else if Types.subtype t1 Types.Int.any
914 then (
915 check loc2 t2 Types.Int.any
916 "The second argument of + must be an integer as the first one";
917 Types.interval
918 (Intervals.add (Types.Int.get t1) (Types.Int.get t2))
919 )
920 else (
921 check loc1 t1 Types.Record.any
922 "The first argument of + must be a record or an integer";
923 check loc2 t2 Types.Record.any
924 "The second argument of + must be a record as the first one";
925 Types.Record.merge t1 t2
926 )
927 | "-", [loc1,t1; loc2,t2] ->
928 type_int_binop Intervals.sub loc1 t1 loc2 t2
929 | ("*" | "/" | "mod"), [loc1,t1; loc2,t2] ->
930 type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
931 | "@", [loc1,t1; loc2,t2] ->
932 check loc1 t1 Sequence.any
933 "The first argument of @ must be a sequence";
934 Sequence.concat t1 t2
935 | "flatten", [loc1,t1] ->
936 check loc1 t1 Sequence.seqseq
937 "The argument of flatten must be a sequence of sequences";
938 Sequence.flatten t1
939 | "load_xml", [loc1,t1] ->
940 check loc1 t1 Sequence.string
941 "The argument of load_xml must be a string (filename)";
942 Types.any
943 | ("load_file" | "load_file_utf8"), [loc1,t1] ->
944 check loc1 t1 Sequence.string
945 "The argument of load_file must be a string (filename)";
946 Sequence.string
947 | "load_html", [loc1,t1] ->
948 check loc1 t1 Sequence.string
949 "The argument of load_html must be a string (filename)";
950 Types.any
951 | "raise", [loc1,t1] ->
952 Types.empty
953 | ("print_xml" | "print_xml_utf8"), [loc1,t1] ->
954 Sequence.string
955 | "print", [loc1,t1] ->
956 check loc1 t1 Sequence.string
957 "The argument of print must be a string";
958 Sequence.nil_type
959 | ("dump_to_file" | "dump_to_file_utf8"), [loc1,t1; loc2,t2] ->
960 check loc1 t1 Sequence.string
961 "The argument of dump_to_file must be a string (filename)";
962 check loc2 t2 Sequence.string
963 "The argument of dump_to_file must be a string (value to dump)";
964 Sequence.nil_type
965 | "int_of", [loc1,t1] ->
966 check loc1 t1 Sequence.string
967 "The argument of int_of must be a string";
968 if not (Types.subtype t1 Builtin.intstr) then
969 warning loc "This application of int_of may fail";
970 Types.interval Intervals.any
971 | "string_of", [loc1,t1] ->
972 Sequence.string
973 | "=", [loc1,t1; loc2,t2] ->
974 (* could prevent comparision of functional value here... *)
975 (* could also handle the case when t1 and t2 are the same
976 singleton type *)
977 if Types.is_empty (Types.cap t1 t2) then
978 Builtin.false_type
979 else
980 Builtin.bool
981 | ("<=" | "<" | ">" | ">=" ), [loc1,t1; loc2,t2] ->
982 (* could prevent comparision of functional value here... *)
983 Builtin.bool
984 | _ -> assert false
985
986 and type_int_binop f loc1 t1 loc2 t2 =
987 check loc1 t1 Types.Int.any
988 "The first argument must be an integer";
989 check loc2 t2 Types.Int.any
990 "The second argument must be an integer";
991 Types.interval
992 (f (Types.Int.get t1) (Types.Int.get t2))
993
994
995
996
997 let report_unused_branches () =
998 List.iter
999 (fun b ->
1000 if not b.br_used then
1001 warning b.br_loc "This branch is not used"
1002 )
1003 !all_branches;
1004 all_branches := []
1005

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5