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

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 431 - (show annotations)
Tue Jul 10 17:34:25 2007 UTC (5 years, 10 months ago) by abate
File size: 28930 byte(s)
[r2003-05-25 16:53:21 by cvscast] toplevel

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

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