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

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 431 - (hide 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 abate 237 (* TODO:
2 abate 276 - rewrite type-checking of operators to propagate constraint
3 abate 278 - optimize computation of pattern free variables
4     - check whether it is worth using recursive hash-consing internally
5 abate 276 *)
6 abate 237
7 abate 276
8 abate 320 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 abate 5 (* I. Transform the abstract syntax of types and patterns into
15     the internal form *)
16    
17     open Location
18     open Ast
19 abate 225 open Ident
20 abate 5
21 abate 140 module S = struct type t = string let compare = compare end
22 abate 225 module TypeEnv = Map.Make(S)
23 abate 140
24 abate 9 exception NonExhaustive of Types.descr
25 abate 421 exception Constraint of Types.descr * Types.descr
26 abate 19 exception ShouldHave of Types.descr * string
27 abate 355 exception ShouldHave2 of Types.descr * string * Types.descr
28 abate 233 exception WrongLabel of Types.descr * label
29 abate 374 exception UnboundId of id
30 abate 421 exception Error of string
31 abate 5
32 abate 9 let raise_loc loc exn = raise (Location (loc,exn))
33 abate 421 let error loc msg = raise_loc loc (Error msg)
34 abate 9
35 abate 5
36 abate 278 (* Eliminate Recursion, propagate Sequence Capture Variables *)
37 abate 5
38 abate 278 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 abate 71
44 abate 278 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 abate 71
71 abate 278 let rec hash_derecurs = function
72 abate 426 | 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 abate 278 and hash_derecurs_regexp = function
99 abate 426 | 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 abate 107
112 abate 278 let rec equal_derecurs p1 p2 = (p1 == p2) || match p1,p2 with
113 abate 426 | PAlias s1, PAlias s2 ->
114     s1 == s2
115     | PType t1, PType t2 ->
116     Types.equal_descr t1 t2
117 abate 278 | 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 abate 426 | 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 abate 278 | _ -> false
135     and equal_derecurs_regexp r1 r2 = match r1,r2 with
136 abate 426 | PEpsilon, PEpsilon ->
137     true
138     | PElem p1, PElem p2 ->
139     equal_derecurs p1 p2
140 abate 278 | PSeq (p1,q1), PSeq (p2,q2)
141 abate 426 | PAlt (p1,q1), PAlt (p2,q2) ->
142     (equal_derecurs_regexp p1 p2) && (equal_derecurs_regexp q1 q2)
143 abate 278 | PStar p1, PStar p2
144 abate 426 | PWeakStar p1, PWeakStar p2 ->
145     equal_derecurs_regexp p1 p2
146 abate 278 | _ -> false
147 abate 5
148 abate 278 module DerecursTable = Hashtbl.Make(
149     struct
150     type t = derecurs
151     let hash = hash_derecurs
152     let equal = equal_derecurs
153     end
154     )
155 abate 5
156 abate 278 module RE = Hashtbl.Make(
157     struct
158     type t = derecurs_regexp * derecurs
159 abate 426 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 abate 278 end
164     )
165 abate 71
166 abate 278
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 abate 426 with Not_found ->
176     raise_loc_generic p.loc ("Undefined type/pattern " ^ v))
177 abate 278 | 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 abate 426 let constant_nil t v =
191     PAnd (t, PConstant (v, Types.Atom Sequence.nil_atom)) in
192 abate 278 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 abate 426 | 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 abate 71
212 abate 140
213 abate 278 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 abate 5
219 abate 278 (* Stratification and recursive hash-consing *)
220 abate 5
221 abate 277 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 abate 278 | IOptional of descr
230 abate 277 | 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 abate 278 mutable d : descr option
239 abate 277 }
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 abate 426 | IRecord (o1,r1), IRecord (o2,r2) ->
280     (o1 = o2) && (LabelMap.equal equal_slot r1 r2)
281 abate 277 | ICapture x1, ICapture x2 -> Id.equal x1 x2
282 abate 426 | IConstant (x1,y1), IConstant (x2,y2) ->
283     (Id.equal x1 x2) && (Types.equal_const y1 y2)
284 abate 277 | _ -> 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 abate 278 let equal s1 s2 =
308     (s1 == s2) ||
309     (incr gen; rank := 0;
310     let e = equal_slot s1 s2 in
311 abate 355 (* if e then Printf.eprintf "Recursive hash-consig: Equal\n"; *)
312 abate 278 e)
313 abate 277 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 abate 278 | IType _ -> IdSet.empty
324 abate 277 | 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 abate 426 | IRecord (o,r) ->
332     List.fold_left IdSet.cup IdSet.empty (LabelMap.map_to_list fv_slot r)
333 abate 277 | ICapture x | IConstant (x,_) -> IdSet.singleton x
334 abate 278
335 abate 277
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 abate 278
345     let todo_fv = ref []
346 abate 277
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 abate 278 todo_fv := s :: !todo_fv;
355 abate 277 s
356 abate 278
357     let flush_fv () =
358     List.iter compute_fv !todo_fv;
359     todo_fv := []
360 abate 277
361 abate 278 let compile_slot_hash = DerecursTable.create 67
362     let compile_hash = DerecursTable.create 67
363    
364 abate 277 let defs = ref []
365 abate 278
366     let rec compile p =
367     try DerecursTable.find compile_hash p
368 abate 277 with Not_found ->
369 abate 278 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 abate 426 | PEpsilon ->
400     (match q with
401     | PRegexp (r,q) -> aux accu r q
402     | _ -> (compile q) :: accu)
403 abate 278 | 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 abate 277 let s = mk () in
418 abate 278 defs := (s,p) :: !defs;
419     DerecursTable.add compile_slot_hash p s;
420 abate 277 s
421 abate 278
422 abate 277
423     let rec flush_defs () =
424     match !defs with
425     | [] -> ()
426 abate 278 | (s,p)::t -> defs := t; s.d <- Some (compile p); flush_defs ()
427 abate 277
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 abate 431
496 abate 277 let register_global_types b =
497 abate 278 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 abate 431 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 abate 278
524     let dump_global_types ppf =
525     TypeEnv.iter (fun v _ -> Format.fprintf ppf " %s" v) !glb
526 abate 277
527    
528     let typ p =
529 abate 278 let s = compile_slot (derecurs !glb p) in
530 abate 277 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 abate 278 let s = compile_slot (derecurs !glb p) in
537 abate 277 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 abate 5
543    
544     (* II. Build skeleton *)
545    
546 abate 225 module Fv = IdSet
547 abate 6
548 abate 427 type branch = Branch of Typed.branch * branch list
549 abate 314
550 abate 427 let cur_branch : branch list ref = ref []
551    
552 abate 316 let exp loc fv e =
553 abate 6 fv,
554     { Typed.exp_loc = loc;
555 abate 5 Typed.exp_typ = Types.empty;
556 abate 316 Typed.exp_descr = e;
557 abate 5 }
558 abate 316
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 abate 421 (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 abate 316 | 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 abate 421 | Map (e,b) ->
628 abate 316 let (fv1,e) = expr loc e
629     and (fv2,b) = branches b in
630 abate 421 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 abate 331 | Xtrans (e,b) ->
636 abate 316 let (fv1,e) = expr loc e
637     and (fv2,b) = branches b in
638 abate 331 exp loc (Fv.cup fv1 fv2) (Typed.Xtrans (e, b))
639 abate 316 | 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 abate 5
645 abate 316 and branches b =
646 abate 6 let fv = ref Fv.empty in
647 abate 19 let accept = ref Types.empty in
648 abate 314 let branch (p,e) =
649 abate 427 let cur_br = !cur_branch in
650     cur_branch := [];
651 abate 316 let (fv2,e) = expr noloc e in
652     let br_loc = merge_loc p.loc e.Typed.exp_loc in
653 abate 314 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 abate 427 cur_branch := Branch (br, !cur_branch) :: cur_br;
664 abate 314 br in
665     let b = List.map branch b in
666 abate 19 (!fv,
667     {
668     Typed.br_typ = Types.empty;
669     Typed.br_branches = b;
670 abate 45 Typed.br_accept = !accept;
671     Typed.br_compiled = None;
672 abate 19 }
673     )
674 abate 5
675 abate 122 let expr = expr noloc
676    
677 abate 277 let let_decl p e =
678     let (_,e) = expr e in
679     { Typed.let_pat = pat p;
680 abate 66 Typed.let_body = e;
681     Typed.let_compiled = None }
682    
683     (* III. Type-checks *)
684    
685     type env = Types.descr Env.t
686 abate 6
687     open Typed
688    
689 abate 421 let require loc t s =
690     if not (Types.subtype t s) then raise_loc loc (Constraint (t, s))
691 abate 17
692 abate 421 let check loc t s =
693     require loc t s; t
694 abate 17
695 abate 421 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 abate 19 let rec type_check env e constr precise =
711     let d = type_check' e.exp_loc env e.exp_descr constr precise in
712 abate 421 let d = if precise then d else constr in
713 abate 6 e.exp_typ <- Types.cup e.exp_typ d;
714     d
715    
716 abate 19 and type_check' loc env e constr precise = match e with
717 abate 54 | Forget (e,t) ->
718     let t = Types.descr t in
719     ignore (type_check env e t false);
720 abate 421 check loc t constr
721    
722 abate 19 | Abstraction a ->
723     let t =
724     try Types.Arrow.check_strenghten a.fun_typ constr
725     with Not_found ->
726 abate 421 should_have loc constr
727     "but the interface of the abstraction is not compatible"
728 abate 19 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 abate 374 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 abate 65 ignore (type_check_branches loc env t1 a.fun_body t2 false)
738 abate 19 ) a.fun_iface;
739     t
740 abate 64
741 abate 19 | Match (e,b) ->
742     let t = type_check env e b.br_accept true in
743 abate 65 type_check_branches loc env t b constr precise
744 abate 30
745 abate 64 | Try (e,b) ->
746     let te = type_check env e constr precise in
747 abate 65 let tb = type_check_branches loc env Types.any b constr precise in
748 abate 64 Types.cup te tb
749    
750 abate 110 | Pair (e1,e2) ->
751     type_check_pair loc env e1 e2 constr precise
752 abate 421
753 abate 110 | Xml (e1,e2) ->
754     type_check_pair ~kind:`XML loc env e1 e2 constr precise
755 abate 159
756 abate 29 | RecordLitt r ->
757 abate 421 type_record loc env r constr precise
758 abate 31
759 abate 421 | 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 abate 86 | 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 abate 110 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 abate 421 check loc res constr
777 abate 19
778 abate 421 | UnaryOp (o,e) ->
779 abate 426 let t = o.un_op_typer loc
780     (type_check env e) constr precise in
781 abate 421 check loc t constr
782    
783     | BinaryOp (o,e1,e2) ->
784 abate 426 let t = o.bin_op_typer loc
785     (type_check env e1)
786     (type_check env e2) constr precise in
787 abate 421 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 abate 110 and type_check_pair ?(kind=`Normal) loc env e1 e2 constr precise =
824 abate 361 let rects = Types.Product.normal ~kind constr in
825 abate 110 if Types.Product.is_empty rects then
826     (match kind with
827 abate 421 | `Normal -> should_have loc constr "but it is a pair"
828     | `XML -> should_have loc constr "but it is an XML element");
829 abate 334 let need_s = Types.Product.need_second rects in
830 abate 355 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 abate 334
836 abate 110 if precise then
837 abate 355 match kind with
838     | `Normal -> Types.times (Types.cons t1) (Types.cons t2)
839     | `XML -> Types.xml (Types.cons t1) (Types.cons t2)
840 abate 110 else
841     constr
842    
843 abate 421 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 abate 110
870 abate 19
871 abate 65 and type_check_branches loc env targ brs constr precise =
872 abate 374 if Types.is_empty targ then Types.empty
873 abate 9 else (
874     brs.br_typ <- Types.cup brs.br_typ targ;
875 abate 65 branches_aux loc env targ
876 abate 19 (if precise then Types.empty else constr)
877     constr precise brs.br_branches
878 abate 9 )
879 abate 6
880 abate 65 and branches_aux loc env targ tres constr precise = function
881 abate 374 | [] -> tres
882 abate 6 | 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 abate 65 then branches_aux loc env targ tres constr precise rem
889 abate 6 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 abate 19 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 abate 9 let targ'' = Types.diff targ acc in
898     if (Types.non_empty targ'') then
899 abate 65 branches_aux loc env targ'' tres constr precise rem
900 abate 9 else
901     tres
902 abate 6 )
903 abate 16
904 abate 421 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 abate 66 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 abate 431 (fun accu -> function
935     | { exp_descr=Abstraction { fun_typ = t; fun_name = Some f } } ->
936     (f,t) :: accu
937     | _ -> assert false
938     ) [] l
939 abate 66 in
940     let env' = List.fold_left (fun env (x,t) -> Env.add x t env) env types in
941 abate 431 List.iter (fun e -> ignore (type_check env' e Types.any false)) l;
942 abate 66 types
943    
944 abate 427
945     let rec unused_branches b =
946 abate 314 List.iter
947 abate 427 (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 abate 314
954 abate 427 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