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

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 107 - (hide annotations)
Tue Jul 10 17:06:47 2007 UTC (5 years, 10 months ago) by abate
File size: 23580 byte(s)
[r2002-11-10 16:13:31 by cvscast] Empty log message

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

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