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

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 76 - (hide annotations)
Tue Jul 10 17:04:07 2007 UTC (5 years, 10 months ago) by abate
File size: 22351 byte(s)
[r2002-11-05 16:09:14 by cvscast] Empty log message

Original author: cvscast
Date: 2002-11-05 16:09:15+00:00
1 abate 5 (* I. Transform the abstract syntax of types and patterns into
2     the internal form *)
3    
4     open Location
5     open Ast
6    
7 abate 9 exception Pattern of string
8     exception NonExhaustive of Types.descr
9 abate 28 exception MultipleLabel of Types.label
10 abate 9 exception Constraint of Types.descr * Types.descr * string
11 abate 19 exception ShouldHave of Types.descr * string
12 abate 26 exception WrongLabel of Types.descr * Types.label
13 abate 36 exception UnboundId of string
14 abate 5
15 abate 9 let raise_loc loc exn = raise (Location (loc,exn))
16    
17 abate 5 (* Internal representation as a graph (desugar recursive types and regexp),
18     to compute freevars, etc... *)
19    
20 abate 9 type ti = {
21 abate 5 id : int;
22     mutable loc' : loc;
23     mutable fv : string SortedList.t option;
24     mutable descr': descr;
25     mutable type_node: Types.node option;
26     mutable pat_node: Patterns.node option
27     }
28     and descr =
29 abate 9 [ `Alias of string * ti
30 abate 5 | `Type of Types.descr
31     | `Or of ti * ti
32 abate 54 | `And of ti * ti * bool
33 abate 5 | `Diff of ti * ti
34     | `Times of ti * ti
35     | `Arrow of ti * ti
36     | `Record of Types.label * bool * ti
37     | `Capture of Patterns.capture
38     | `Constant of Patterns.capture * Types.const
39     ]
40    
41    
42    
43     module S = struct type t = string let compare = compare end
44     module StringMap = Map.Make(S)
45     module StringSet = Set.Make(S)
46    
47     let mk' =
48     let counter = ref 0 in
49 abate 13 fun loc ->
50 abate 5 incr counter;
51 abate 9 let rec x = {
52     id = !counter;
53 abate 13 loc' = loc;
54 abate 9 fv = None;
55     descr' = `Alias ("__dummy__", x);
56     type_node = None;
57     pat_node = None
58     } in
59 abate 5 x
60    
61     let cons loc d =
62 abate 13 let x = mk' loc in
63 abate 5 x.descr' <- d;
64     x
65    
66     (* Note:
67     Compilation of Regexp is implemented as a ``rewriting'' of
68     the parsed syntax, in order to be able to print its result
69     (for debugging for instance)
70    
71     It would be possible (and a little more efficient) to produce
72     directly ti nodes.
73     *)
74    
75     module Regexp = struct
76     let defs = ref []
77     let name =
78     let c = ref 0 in
79     fun () ->
80     incr c;
81     "#" ^ (string_of_int !c)
82    
83     let rec seq_vars accu = function
84     | Epsilon | Elem _ -> accu
85     | Seq (r1,r2) | Alt (r1,r2) -> seq_vars (seq_vars accu r1) r2
86     | Star r | WeakStar r -> seq_vars accu r
87     | SeqCapture (v,r) -> seq_vars (StringSet.add v accu) r
88    
89 abate 71 let uniq_id = let r = ref 0 in fun () -> incr r; !r
90    
91     type flat = [ `Epsilon
92     | `Elem of int * Ast.ppat (* the int arg is used to
93     to stop generic comparison *)
94     | `Seq of flat * flat
95     | `Alt of flat * flat
96     | `Star of flat
97     | `WeakStar of flat ]
98    
99     let rec propagate vars : regexp -> flat = function
100 abate 5 | Epsilon -> `Epsilon
101 abate 71 | Elem x -> let p = vars x in `Elem (uniq_id (),p)
102 abate 5 | Seq (r1,r2) -> `Seq (propagate vars r1,propagate vars r2)
103     | Alt (r1,r2) -> `Alt (propagate vars r1, propagate vars r2)
104     | Star r -> `Star (propagate vars r)
105     | WeakStar r -> `WeakStar (propagate vars r)
106 abate 71 | SeqCapture (v,x) ->
107     let v= mk noloc (Capture v) in
108     propagate (fun p -> mk noloc (And (vars p,v,true))) x
109 abate 5
110     let cup r1 r2 =
111     match (r1,r2) with
112     | (_, `Empty) -> r1
113     | (`Empty, _) -> r2
114     | (`Res t1, `Res t2) -> `Res (mk noloc (Or (t1,t2)))
115    
116 abate 71 (*TODO: review this compilation schema to avoid explosion when
117     coding (Optional x) by (Or(Epsilon,x)); memoization ... *)
118    
119     module Memo = Map.Make(struct type t = flat list let compare = compare end)
120     module Coind = Set.Make(struct type t = flat list let compare = compare end)
121     let memo = ref Memo.empty
122    
123 abate 5 let rec compile fin e seq : [`Res of Ast.ppat | `Empty] =
124 abate 71 if Coind.mem seq !e then `Empty
125     else (
126     e := Coind.add seq !e;
127 abate 5 match seq with
128     | [] ->
129     `Res fin
130     | `Epsilon :: rest ->
131     compile fin e rest
132 abate 71 | `Elem (_,p) :: rest ->
133     `Res (mk noloc (Prod (p, guard_compile fin rest)))
134 abate 5 | `Seq (r1,r2) :: rest ->
135     compile fin e (r1 :: r2 :: rest)
136     | `Alt (r1,r2) :: rest ->
137     cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
138 abate 71 | `Star r :: rest ->
139     cup (compile fin e (r::seq)) (compile fin e rest)
140     | `WeakStar r :: rest ->
141     cup (compile fin e rest) (compile fin e (r::seq))
142     )
143 abate 5 and guard_compile fin seq =
144 abate 71 try Memo.find seq !memo
145 abate 5 with
146     Not_found ->
147     let n = name () in
148     let v = mk noloc (PatVar n) in
149 abate 71 memo := Memo.add seq v !memo;
150     let d = compile fin (ref Coind.empty) seq in
151 abate 5 (match d with
152     | `Empty -> assert false
153     | `Res d -> defs := (n,d) :: !defs);
154     v
155    
156    
157     let atom_nil = Types.mk_atom "nil"
158     let constant_nil v t =
159 abate 54 mk noloc (And (t, (mk noloc (Constant (v, Types.Atom atom_nil))), true))
160 abate 5
161     let compile regexp queue : ppat =
162     let vars = seq_vars StringSet.empty regexp in
163 abate 71 let fin = StringSet.fold constant_nil vars queue in
164     let n = guard_compile fin [propagate (fun p -> p) regexp] in
165     memo := Memo.empty;
166 abate 5 let d = !defs in
167     defs := [];
168     mk noloc (Recurs (n,d))
169     end
170    
171     let compile_regexp = Regexp.compile
172    
173    
174     let rec compile env { loc = loc; descr = d } : ti =
175     match (d : Ast.ppat') with
176     | PatVar s ->
177     (try StringMap.find s env
178 abate 9 with Not_found ->
179     raise_loc loc (Pattern ("Undefined type variable " ^ s))
180 abate 5 )
181 abate 13 | Recurs (t, b) -> compile (compile_many env b) t
182 abate 5 | Regexp (r,q) -> compile env (Regexp.compile r q)
183     | Internal t -> cons loc (`Type t)
184     | Or (t1,t2) -> cons loc (`Or (compile env t1, compile env t2))
185 abate 54 | And (t1,t2,e) -> cons loc (`And (compile env t1, compile env t2,e))
186 abate 5 | Diff (t1,t2) -> cons loc (`Diff (compile env t1, compile env t2))
187     | Prod (t1,t2) -> cons loc (`Times (compile env t1, compile env t2))
188     | Arrow (t1,t2) -> cons loc (`Arrow (compile env t1, compile env t2))
189     | Record (l,o,t) -> cons loc (`Record (l,o,compile env t))
190     | Constant (x,v) -> cons loc (`Constant (x,v))
191     | Capture x -> cons loc (`Capture x)
192    
193 abate 13 and compile_many env b =
194     let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
195     let env =
196     List.fold_left (fun env (v,t,x) -> StringMap.add v x env) env b in
197     List.iter (fun (v,t,x) -> x.descr' <- `Alias (v, compile env t)) b;
198     env
199    
200    
201 abate 71 let comp_fv_seen = ref []
202     let comp_fv_res = ref []
203     let rec comp_fv s =
204     if List.memq s !comp_fv_seen then ()
205     else (
206     comp_fv_seen := s :: !comp_fv_seen;
207     (match s.descr' with
208     | `Alias (_,x) -> comp_fv x
209     | `Or (s1,s2)
210     | `And (s1,s2,_)
211     | `Diff (s1,s2)
212     | `Times (s1,s2)
213     | `Arrow (s1,s2) -> comp_fv s1; comp_fv s2
214     | `Record (l,opt,s) -> comp_fv s
215     | `Type _ -> ()
216     | `Capture x
217     | `Constant (x,_) -> comp_fv_res := x :: !comp_fv_res);
218     if (!comp_fv_res = []) then s.fv <- Some [];
219     (* TODO: check that the above line is correct *)
220     )
221    
222    
223    
224     let fv s =
225 abate 5 match s.fv with
226     | Some l -> l
227 abate 71 | None ->
228     comp_fv s;
229     let l = SortedList.from_list !comp_fv_res in
230     comp_fv_res := [];
231     comp_fv_seen := [];
232     s.fv <- Some l;
233 abate 5 l
234    
235     let rec typ seen s : Types.descr =
236     match s.descr' with
237 abate 9 | `Alias (v,x) ->
238     if List.memq s seen then
239     raise_loc s.loc'
240     (Pattern
241     ("Unguarded recursion on variable " ^ v ^ " in this type"))
242 abate 5 else typ (s :: seen) x
243     | `Type t -> t
244     | `Or (s1,s2) -> Types.cup (typ seen s1) (typ seen s2)
245 abate 54 | `And (s1,s2,_) -> Types.cap (typ seen s1) (typ seen s2)
246 abate 5 | `Diff (s1,s2) -> Types.diff (typ seen s1) (typ seen s2)
247     | `Times (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
248     | `Arrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
249     | `Record (l,o,s) -> Types.record l o (typ_node s)
250 abate 9 | `Capture _ | `Constant _ -> assert false
251 abate 5
252     and typ_node s : Types.node =
253     match s.type_node with
254     | Some x -> x
255     | None ->
256     let x = Types.make () in
257     s.type_node <- Some x;
258     let t = typ [] s in
259     Types.define x t;
260     x
261    
262 abate 71 let type_node s =
263     let s = typ_node s in
264     let s = Types.internalize s in
265     (* Types.define s (Types.normalize (Types.descr s)); *)
266     s
267 abate 5
268     let rec pat seen s : Patterns.descr =
269     if fv s = [] then Patterns.constr (type_node s) else
270     match s.descr' with
271 abate 9 | `Alias (v,x) ->
272     if List.memq s seen then
273     raise_loc s.loc'
274     (Pattern
275     ("Unguarded recursion on variable " ^ v ^ " in this pattern"))
276 abate 5 else pat (s :: seen) x
277     | `Or (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
278 abate 54 | `And (s1,s2,e) -> Patterns.cap (pat seen s1) (pat seen s2) e
279 abate 5 | `Diff (s1,s2) when fv s2 = [] ->
280     let s2 = Types.cons (Types.neg (Types.descr (type_node s2)))in
281 abate 54 Patterns.cap (pat seen s1) (Patterns.constr s2) true
282 abate 9 | `Diff _ ->
283     raise_loc s.loc' (Pattern "Difference not allowed in patterns")
284 abate 5 | `Times (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
285     | `Record (l,false,s) -> Patterns.record l (pat_node s)
286 abate 9 | `Record _ ->
287     raise_loc s.loc'
288     (Pattern "Optional field not allowed in record patterns")
289 abate 5 | `Capture x -> Patterns.capture x
290     | `Constant (x,c) -> Patterns.constant x c
291 abate 9 | `Arrow _ ->
292     raise_loc s.loc' (Pattern "Arrow not allowed in patterns")
293     | `Type _ -> assert false
294 abate 5
295     and pat_node s : Patterns.node =
296     match s.pat_node with
297     | Some x -> x
298     | None ->
299     let x = Patterns.make (fv s) in
300     s.pat_node <- Some x;
301     let t = pat [] s in
302     Patterns.define x t;
303     x
304    
305 abate 13 let global_types = ref StringMap.empty
306    
307     let mk_typ e =
308 abate 71 if fv e = [] then type_node e
309 abate 13 else raise_loc e.loc' (Pattern "Capture variables are not allowed in types")
310    
311 abate 5
312 abate 13 let typ e =
313     mk_typ (compile !global_types e)
314    
315 abate 5 let pat e =
316 abate 13 let e = compile !global_types e in
317 abate 5 pat_node e
318    
319 abate 13 let register_global_types b =
320     let env = compile_many !global_types b in
321 abate 15 List.iter (fun (v,_) ->
322     let d = Types.descr (mk_typ (StringMap.find v env)) in
323 abate 71 (* let d = Types.normalize d in*)
324     Types.Print.register_global v d;
325     ()
326 abate 15 ) b;
327 abate 13 global_types := env
328 abate 5
329    
330     (* II. Build skeleton *)
331    
332 abate 6 module Fv = StringSet
333    
334 abate 5 let rec expr { loc = loc; descr = d } =
335 abate 6 let (fv,td) =
336 abate 5 match d with
337 abate 18 | DebugTyper t -> (Fv.empty, Typed.DebugTyper (typ t))
338 abate 54 | Forget (e,t) ->
339     let (fv,e) = expr e and t = typ t in
340     (fv, Typed.Forget (e,t))
341 abate 6 | Var s -> (Fv.singleton s, Typed.Var s)
342     | Apply (e1,e2) ->
343     let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
344     (Fv.union fv1 fv2, Typed.Apply (e1,e2))
345 abate 5 | Abstraction a ->
346 abate 6 let iface = List.map (fun (t1,t2) -> (typ t1, typ t2)) a.fun_iface in
347     let t = List.fold_left
348     (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2))
349     Types.any iface in
350 abate 9 let iface = List.map
351     (fun (t1,t2) -> (Types.descr t1, Types.descr t2))
352     iface in
353 abate 6 let (fv0,body) = branches a.fun_body in
354     let fv = match a.fun_name with
355     | None -> fv0
356     | Some f -> Fv.remove f fv0 in
357     (fv,
358     Typed.Abstraction
359     { Typed.fun_name = a.fun_name;
360     Typed.fun_iface = iface;
361     Typed.fun_body = body;
362     Typed.fun_typ = t;
363 abate 69 Typed.fun_fv = Fv.elements fv
364 abate 6 }
365     )
366     | Cst c -> (Fv.empty, Typed.Cst c)
367     | Pair (e1,e2) ->
368     let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
369     (Fv.union fv1 fv2, Typed.Pair (e1,e2))
370 abate 26 | Dot (e,l) ->
371     let (fv,e) = expr e in
372 abate 27 (fv, Typed.Dot (e,l))
373 abate 6 | RecordLitt r ->
374     let fv = ref Fv.empty in
375 abate 47 let r = List.sort (fun (l1,_) (l2,_) -> compare l1 l2) r in
376 abate 6 let r = List.map
377     (fun (l,e) ->
378 abate 47 let (fv2,e) = expr e in fv := Fv.union !fv fv2; (l,e))
379     r in
380     let rec check = function
381     | (l1,_) :: (l2,_) :: _ when l1 = l2 ->
382     raise_loc loc (MultipleLabel l1)
383     | _ :: rem -> check rem
384     | _ -> () in
385     check r;
386 abate 6 (!fv, Typed.RecordLitt r)
387 abate 16 | Op (op,le) ->
388     let (fvs,ltes) = List.split (List.map expr le) in
389     let fv = List.fold_left Fv.union Fv.empty fvs in
390     (fv, Typed.Op (op,ltes))
391 abate 6 | Match (e,b) ->
392     let (fv1,e) = expr e
393     and (fv2,b) = branches b in
394     (Fv.union fv1 fv2, Typed.Match (e, b))
395     | Map (e,b) ->
396     let (fv1,e) = expr e
397     and (fv2,b) = branches b in
398     (Fv.union fv1 fv2, Typed.Map (e, b))
399 abate 64 | Try (e,b) ->
400     let (fv1,e) = expr e
401     and (fv2,b) = branches b in
402     (Fv.union fv1 fv2, Typed.Try (e, b))
403 abate 5 in
404 abate 6 fv,
405     { Typed.exp_loc = loc;
406 abate 5 Typed.exp_typ = Types.empty;
407     Typed.exp_descr = td;
408     }
409    
410 abate 6 and branches b =
411     let fv = ref Fv.empty in
412 abate 19 let accept = ref Types.empty in
413 abate 6 let b = List.map
414     (fun (p,e) ->
415     let (fv2,e) = expr e in
416 abate 69 let p = pat p in
417     let fv2 = List.fold_right Fv.remove (Patterns.fv p) fv2 in
418 abate 6 fv := Fv.union !fv fv2;
419 abate 19 accept := Types.cup !accept (Types.descr (Patterns.accept p));
420 abate 6 { Typed.br_used = false;
421 abate 19 Typed.br_pat = p;
422 abate 6 Typed.br_body = e }
423     ) b in
424 abate 19 (!fv,
425     {
426     Typed.br_typ = Types.empty;
427     Typed.br_branches = b;
428 abate 45 Typed.br_accept = !accept;
429     Typed.br_compiled = None;
430 abate 19 }
431     )
432 abate 5
433 abate 66 let let_decl p e =
434     let (_,e) = expr e in
435     { Typed.let_pat = pat p;
436     Typed.let_body = e;
437     Typed.let_compiled = None }
438    
439     (* III. Type-checks *)
440    
441 abate 6 module Env = StringMap
442 abate 66 type env = Types.descr Env.t
443 abate 6
444     open Typed
445    
446 abate 66 let warning loc msg =
447     Format.fprintf Format.std_formatter
448     "Warning %a:@\n%s@\n" Location.print_loc loc msg
449 abate 17
450     let check loc t s msg =
451     if not (Types.subtype t s) then raise_loc loc (Constraint (t, s, msg))
452    
453 abate 19 let rec type_check env e constr precise =
454 abate 31 (* Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
455 abate 37 Types.Print.print_descr constr precise;
456     *)
457 abate 19 let d = type_check' e.exp_loc env e.exp_descr constr precise in
458 abate 6 e.exp_typ <- Types.cup e.exp_typ d;
459     d
460    
461 abate 19 and type_check' loc env e constr precise = match e with
462 abate 54 | Forget (e,t) ->
463     let t = Types.descr t in
464     ignore (type_check env e t false);
465     t
466 abate 19 | Abstraction a ->
467     let t =
468     try Types.Arrow.check_strenghten a.fun_typ constr
469     with Not_found ->
470     raise_loc loc
471     (ShouldHave
472     (constr, "but the interface of the abstraction is not compatible"))
473     in
474     let env = match a.fun_name with
475     | None -> env
476     | Some f -> Env.add f a.fun_typ env in
477     List.iter
478     (fun (t1,t2) ->
479 abate 65 ignore (type_check_branches loc env t1 a.fun_body t2 false)
480 abate 19 ) a.fun_iface;
481     t
482 abate 64
483 abate 19 | Match (e,b) ->
484     let t = type_check env e b.br_accept true in
485 abate 65 type_check_branches loc env t b constr precise
486 abate 30
487 abate 64 | Try (e,b) ->
488     let te = type_check env e constr precise in
489 abate 65 let tb = type_check_branches loc env Types.any b constr precise in
490 abate 64 Types.cup te tb
491    
492 abate 19 | Pair (e1,e2) ->
493     let rects = Types.Product.get constr in
494     if Types.Product.is_empty rects then
495     raise_loc loc (ShouldHave (constr,"but it is a pair."));
496     let pi1 = Types.Product.pi1 rects in
497    
498     let t1 = type_check env e1 (Types.Product.pi1 rects)
499     (precise || (Types.Product.need_second rects))in
500     let rects = Types.Product.restrict_1 rects t1 in
501     let t2 = type_check env e2 (Types.Product.pi2 rects) precise in
502     if precise then
503     Types.times (Types.cons t1) (Types.cons t2)
504     else
505     constr
506 abate 30
507 abate 29 | RecordLitt r ->
508     let rconstr = Types.Record.get constr in
509     if Types.Record.is_empty rconstr then
510     raise_loc loc (ShouldHave (constr,"but it is a record."));
511    
512     let (rconstr,res) =
513     List.fold_left
514     (fun (rconstr,res) (l,e) ->
515     let rconstr = Types.Record.restrict_label_present rconstr l in
516     let pi = Types.Record.project_field rconstr l in
517     if Types.Record.is_empty rconstr then
518     raise_loc loc
519     (ShouldHave (constr,(Printf.sprintf
520     "Field %s is not allowed here."
521     (Types.label_name l)
522     )
523     ));
524     let t = type_check env e pi true in
525     let rconstr = Types.Record.restrict_field rconstr l t in
526    
527     let res =
528     if precise
529     then Types.cap res (Types.record l false (Types.cons t))
530     else res in
531     (rconstr,res)
532     ) (rconstr, if precise then Types.Record.any else constr) r
533     in
534     res
535    
536 abate 31 | Map (e,b) ->
537     let t = type_check env e (Sequence.star b.br_accept) true in
538    
539     let constr' = Sequence.approx (Types.cap Sequence.any constr) in
540     let exact = Types.subtype (Sequence.star constr') constr in
541 abate 54 (* Note:
542     - could be more precise by integrating the decomposition
543     of constr inside Sequence.map.
544     *)
545     let res =
546     Sequence.map
547     (fun t ->
548 abate 65 type_check_branches loc env t b constr' (precise || (not exact)))
549 abate 54 t in
550     if not exact then check loc res constr "";
551     if precise then res else constr
552 abate 31 | Op ("@", [e1;e2]) ->
553     let constr' = Sequence.star
554     (Sequence.approx (Types.cap Sequence.any constr)) in
555     let exact = Types.subtype constr' constr in
556     if exact then
557     let t1 = type_check env e1 constr' precise
558     and t2 = type_check env e2 constr' precise in
559     if precise then Sequence.concat t1 t2 else constr
560     else
561     (* Note:
562     the knownledge of t1 may makes it useless to
563     check t2 with 'precise' ... *)
564     let t1 = type_check env e1 constr' true
565     and t2 = type_check env e2 constr' true in
566     let res = Sequence.concat t1 t2 in
567     check loc res constr "";
568     if precise then res else constr
569 abate 32 | Op ("flatten", [e]) ->
570     let constr' = Sequence.star
571     (Sequence.approx (Types.cap Sequence.any constr)) in
572     let sconstr' = Sequence.star constr' in
573     let exact = Types.subtype constr' constr in
574     if exact then
575     let t = type_check env e sconstr' precise in
576     if precise then Sequence.flatten t else constr
577     else
578     let t = type_check env e sconstr' true in
579     let res = Sequence.flatten t in
580     check loc res constr "";
581     if precise then res else constr
582 abate 19 | _ ->
583     let t : Types.descr = compute_type' loc env e in
584     check loc t constr "";
585     t
586    
587     and compute_type env e =
588     type_check env e Types.any true
589    
590 abate 6 and compute_type' loc env = function
591 abate 18 | DebugTyper t -> Types.descr t
592 abate 36 | Var s ->
593     (try Env.find s env
594     with Not_found -> raise_loc loc (UnboundId s)
595     )
596 abate 6 | Apply (e1,e2) ->
597 abate 19 let t1 = type_check env e1 Types.Arrow.any true in
598     let t1 = Types.Arrow.get t1 in
599     let dom = Types.Arrow.domain t1 in
600     if Types.Arrow.need_arg t1 then
601     let t2 = type_check env e2 dom true in
602     Types.Arrow.apply t1 t2
603     else
604     (ignore (type_check env e2 dom false); Types.Arrow.apply_noarg t1)
605 abate 6 | Cst c -> Types.constant c
606 abate 26 | Dot (e,l) ->
607     let t = type_check env e Types.Record.any true in
608     (try (Types.Record.project t l)
609     with Not_found -> raise_loc loc (WrongLabel(t,l)))
610 abate 16 | Op (op, el) ->
611     let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
612     type_op loc op args
613 abate 17 | Map (e,b) ->
614     let t = compute_type env e in
615 abate 65 Sequence.map (fun t -> type_check_branches loc env t b Types.any true) t
616 abate 30
617     (* We keep these cases here to allow comparison and benchmarking ...
618     Just comment the corresponding cases in type_check' to
619     activate these ones.
620     *)
621     | Pair (e1,e2) ->
622     let t1 = compute_type env e1
623     and t2 = compute_type env e2 in
624     Types.times (Types.cons t1) (Types.cons t2)
625     | RecordLitt r ->
626     List.fold_left
627     (fun accu (l,e) ->
628     let t = compute_type env e in
629     let t = Types.record l false (Types.cons t) in
630     Types.cap accu t
631     ) Types.Record.any r
632    
633    
634 abate 19 | _ -> assert false
635 abate 6
636 abate 65 and type_check_branches loc env targ brs constr precise =
637 abate 6 if Types.is_empty targ then Types.empty
638 abate 9 else (
639     brs.br_typ <- Types.cup brs.br_typ targ;
640 abate 65 branches_aux loc env targ
641 abate 19 (if precise then Types.empty else constr)
642     constr precise brs.br_branches
643 abate 9 )
644 abate 6
645 abate 65 and branches_aux loc env targ tres constr precise = function
646     | [] -> raise_loc loc (NonExhaustive targ)
647 abate 6 | b :: rem ->
648     let p = b.br_pat in
649     let acc = Types.descr (Patterns.accept p) in
650    
651     let targ' = Types.cap targ acc in
652     if Types.is_empty targ'
653 abate 65 then branches_aux loc env targ tres constr precise rem
654 abate 6 else
655     ( b.br_used <- true;
656     let res = Patterns.filter targ' p in
657     let env' = List.fold_left
658     (fun env (x,t) -> Env.add x (Types.descr t) env)
659     env res in
660 abate 19 let t = type_check env' b.br_body constr precise in
661     let tres = if precise then Types.cup t tres else tres in
662 abate 9 let targ'' = Types.diff targ acc in
663     if (Types.non_empty targ'') then
664 abate 65 branches_aux loc env targ'' tres constr precise rem
665 abate 9 else
666     tres
667 abate 6 )
668 abate 16
669 abate 66 and type_let_decl env l =
670     let acc = Types.descr (Patterns.accept l.let_pat) in
671     let t = type_check env l.let_body acc true in
672     let res = Patterns.filter t l.let_pat in
673     List.map (fun (x,t) -> (x, Types.descr t)) res
674    
675     and type_rec_funs env l =
676     let types =
677     List.fold_left
678     (fun accu -> function {let_body={exp_descr=Abstraction a}} as l ->
679     let t = a.fun_typ in
680     let acc = Types.descr (Patterns.accept l.let_pat) in
681     if not (Types.subtype t acc) then
682     raise_loc l.let_body.exp_loc (NonExhaustive (Types.diff t acc));
683     let res = Patterns.filter t l.let_pat in
684     List.fold_left (fun accu (x,t) -> (x, Types.descr t)::accu) accu res
685     | _ -> assert false) [] l
686     in
687     let env' = List.fold_left (fun env (x,t) -> Env.add x t env) env types in
688     List.iter
689     (function { let_body = { exp_descr = Abstraction a } } as l ->
690     ignore (type_check env' l.let_body Types.any false)
691     | _ -> assert false) l;
692     types
693    
694    
695 abate 16 and type_op loc op args =
696     match (op,args) with
697 abate 51 | "+", [loc1,t1; loc2,t2] ->
698 abate 16 type_int_binop Intervals.add loc1 t1 loc2 t2
699 abate 51 | "-", [loc1,t1; loc2,t2] ->
700     type_int_binop Intervals.sub loc1 t1 loc2 t2
701     | ("*" | "/"), [loc1,t1; loc2,t2] ->
702 abate 16 type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
703 abate 51 | "@", [loc1,t1; loc2,t2] ->
704 abate 17 check loc1 t1 Sequence.any
705     "The first argument of @ must be a sequence";
706     Sequence.concat t1 t2
707 abate 51 | "flatten", [loc1,t1] ->
708 abate 17 check loc1 t1 Sequence.seqseq
709     "The argument of flatten must be a sequence of sequences";
710     Sequence.flatten t1
711 abate 58 | "load_xml", [loc1,t1] ->
712     check loc1 t1 Sequence.string
713     "The argument of load_xml must be a string (filename)";
714     Types.any
715 abate 64 | "raise", [loc1,t1] ->
716     Types.empty
717 abate 76 | "print_xml", [loc1,t1] ->
718     Sequence.string
719 abate 66 | "int_of", [loc1,t1] ->
720     check loc1 t1 Sequence.string
721     "The argument of int_of must a string";
722     if not (Types.subtype t1 Builtin.intstr) then
723     warning loc "This application of int_of may fail";
724     Types.interval Intervals.any
725 abate 16 | _ -> assert false
726    
727     and type_int_binop f loc1 t1 loc2 t2 =
728     if not (Types.Int.is_int t1) then
729     raise_loc loc1
730     (Constraint
731     (t1,Types.Int.any,
732     "The first argument must be an integer"));
733     if not (Types.Int.is_int t2) then
734     raise_loc loc2
735     (Constraint
736 abate 37 (t2,Types.Int.any,
737 abate 16 "The second argument must be an integer"));
738     Types.Int.put
739     (f (Types.Int.get t1) (Types.Int.get t2));
740    
741    

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