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

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Tue Jul 10 17:00:40 2007 UTC (5 years, 10 months ago) by abate
File size: 19313 byte(s)
[r2002-10-26 16:18:24 by cvscast] Empty log message

Original author: cvscast
Date: 2002-10-26 16:18:25+00:00
1 (* I. Transform the abstract syntax of types and patterns into
2 the internal form *)
3
4 open Location
5 open Ast
6
7 exception Pattern of string
8 exception NonExhaustive of Types.descr
9 exception MultipleLabel of Types.label
10 exception Constraint of Types.descr * Types.descr * string
11 exception ShouldHave of Types.descr * string
12 exception WrongLabel of Types.descr * Types.label
13 exception UnboundId of string
14
15 let raise_loc loc exn = raise (Location (loc,exn))
16
17 (* Internal representation as a graph (desugar recursive types and regexp),
18 to compute freevars, etc... *)
19
20 type ti = {
21 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 [ `Alias of string * ti
30 | `Type of Types.descr
31 | `Or of ti * ti
32 | `And of ti * ti
33 | `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 fun loc ->
50 incr counter;
51 let rec x = {
52 id = !counter;
53 loc' = loc;
54 fv = None;
55 descr' = `Alias ("__dummy__", x);
56 type_node = None;
57 pat_node = None
58 } in
59 x
60
61 let cons loc d =
62 let x = mk' loc in
63 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 memo = Hashtbl.create 51
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 let rec propagate vars = function
91 | Epsilon -> `Epsilon
92 | Elem x -> `Elem (vars,x)
93 | Seq (r1,r2) -> `Seq (propagate vars r1,propagate vars r2)
94 | Alt (r1,r2) -> `Alt (propagate vars r1, propagate vars r2)
95 | Star r -> `Star (propagate vars r)
96 | WeakStar r -> `WeakStar (propagate vars r)
97 | SeqCapture (v,x) -> propagate (StringSet.add v vars) x
98
99 let cup r1 r2 =
100 match (r1,r2) with
101 | (_, `Empty) -> r1
102 | (`Empty, _) -> r2
103 | (`Res t1, `Res t2) -> `Res (mk noloc (Or (t1,t2)))
104
105 let rec compile fin e seq : [`Res of Ast.ppat | `Empty] =
106 if List.mem seq e then `Empty
107 else
108 let e = seq :: e in
109 match seq with
110 | [] ->
111 `Res fin
112 | `Epsilon :: rest ->
113 compile fin e rest
114 | `Elem (vars,x) :: rest ->
115 let capt = StringSet.fold
116 (fun v t -> mk noloc (And (t, (mk noloc (Capture v)))))
117 vars x in
118 `Res (mk noloc (Prod (capt, guard_compile fin rest)))
119 | `Seq (r1,r2) :: rest ->
120 compile fin e (r1 :: r2 :: rest)
121 | `Alt (r1,r2) :: rest ->
122 cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
123 | `Star r :: rest -> cup (compile fin e (r::seq)) (compile fin e rest)
124 | `WeakStar r :: rest -> cup (compile fin e rest) (compile fin e (r::seq))
125
126 and guard_compile fin seq =
127 try Hashtbl.find memo seq
128 with
129 Not_found ->
130 let n = name () in
131 let v = mk noloc (PatVar n) in
132 Hashtbl.add memo seq v;
133 let d = compile fin [] seq in
134 (match d with
135 | `Empty -> assert false
136 | `Res d -> defs := (n,d) :: !defs);
137 v
138
139
140 let atom_nil = Types.mk_atom "nil"
141 let constant_nil v t =
142 mk noloc (And (t, (mk noloc (Constant (v, Types.Atom atom_nil)))))
143
144 let compile regexp queue : ppat =
145 let vars = seq_vars StringSet.empty regexp in
146 let fin = StringSet.fold constant_nil vars queue in
147 let n = guard_compile fin [propagate StringSet.empty regexp] in
148 Hashtbl.clear memo;
149 let d = !defs in
150 defs := [];
151 mk noloc (Recurs (n,d))
152 end
153
154 let compile_regexp = Regexp.compile
155
156
157 let rec compile env { loc = loc; descr = d } : ti =
158 match (d : Ast.ppat') with
159 | PatVar s ->
160 (try StringMap.find s env
161 with Not_found ->
162 raise_loc loc (Pattern ("Undefined type variable " ^ s))
163 )
164 | Recurs (t, b) -> compile (compile_many env b) t
165 | Regexp (r,q) -> compile env (Regexp.compile r q)
166 | Internal t -> cons loc (`Type t)
167 | Or (t1,t2) -> cons loc (`Or (compile env t1, compile env t2))
168 | And (t1,t2) -> cons loc (`And (compile env t1, compile env t2))
169 | Diff (t1,t2) -> cons loc (`Diff (compile env t1, compile env t2))
170 | Prod (t1,t2) -> cons loc (`Times (compile env t1, compile env t2))
171 | Arrow (t1,t2) -> cons loc (`Arrow (compile env t1, compile env t2))
172 | Record (l,o,t) -> cons loc (`Record (l,o,compile env t))
173 | Constant (x,v) -> cons loc (`Constant (x,v))
174 | Capture x -> cons loc (`Capture x)
175
176 and compile_many env b =
177 let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
178 let env =
179 List.fold_left (fun env (v,t,x) -> StringMap.add v x env) env b in
180 List.iter (fun (v,t,x) -> x.descr' <- `Alias (v, compile env t)) b;
181 env
182
183
184 let rec comp_fv seen s =
185 match s.fv with
186 | Some l -> l
187 | None ->
188 let l =
189 match s.descr' with
190 | `Alias (_,x) -> if List.memq s seen then [] else comp_fv (s :: seen) x
191 | `Or (s1,s2)
192 | `And (s1,s2)
193 | `Diff (s1,s2)
194 | `Times (s1,s2)
195 | `Arrow (s1,s2) -> SortedList.cup (comp_fv seen s1) (comp_fv seen s2)
196 | `Record (l,opt,s) -> comp_fv seen s
197 | `Type _ -> []
198 | `Capture x
199 | `Constant (x,_) -> [x]
200 in
201 if seen = [] then s.fv <- Some l;
202 l
203
204
205 let fv = comp_fv []
206
207 let rec typ seen s : Types.descr =
208 match s.descr' with
209 | `Alias (v,x) ->
210 if List.memq s seen then
211 raise_loc s.loc'
212 (Pattern
213 ("Unguarded recursion on variable " ^ v ^ " in this type"))
214 else typ (s :: seen) x
215 | `Type t -> t
216 | `Or (s1,s2) -> Types.cup (typ seen s1) (typ seen s2)
217 | `And (s1,s2) -> Types.cap (typ seen s1) (typ seen s2)
218 | `Diff (s1,s2) -> Types.diff (typ seen s1) (typ seen s2)
219 | `Times (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
220 | `Arrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
221 | `Record (l,o,s) -> Types.record l o (typ_node s)
222 | `Capture _ | `Constant _ -> assert false
223
224 and typ_node s : Types.node =
225 match s.type_node with
226 | Some x -> x
227 | None ->
228 let x = Types.make () in
229 s.type_node <- Some x;
230 let t = typ [] s in
231 Types.define x t;
232 x
233
234 let type_node s = Types.internalize (typ_node s)
235
236 let rec pat seen s : Patterns.descr =
237 if fv s = [] then Patterns.constr (type_node s) else
238 match s.descr' with
239 | `Alias (v,x) ->
240 if List.memq s seen then
241 raise_loc s.loc'
242 (Pattern
243 ("Unguarded recursion on variable " ^ v ^ " in this pattern"))
244 else pat (s :: seen) x
245 | `Or (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
246 | `And (s1,s2) -> Patterns.cap (pat seen s1) (pat seen s2)
247 | `Diff (s1,s2) when fv s2 = [] ->
248 let s2 = Types.cons (Types.neg (Types.descr (type_node s2)))in
249 Patterns.cap (pat seen s1) (Patterns.constr s2)
250 | `Diff _ ->
251 raise_loc s.loc' (Pattern "Difference not allowed in patterns")
252 | `Times (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
253 | `Record (l,false,s) -> Patterns.record l (pat_node s)
254 | `Record _ ->
255 raise_loc s.loc'
256 (Pattern "Optional field not allowed in record patterns")
257 | `Capture x -> Patterns.capture x
258 | `Constant (x,c) -> Patterns.constant x c
259 | `Arrow _ ->
260 raise_loc s.loc' (Pattern "Arrow not allowed in patterns")
261 | `Type _ -> assert false
262
263 and pat_node s : Patterns.node =
264 match s.pat_node with
265 | Some x -> x
266 | None ->
267 let x = Patterns.make (fv s) in
268 s.pat_node <- Some x;
269 let t = pat [] s in
270 Patterns.define x t;
271 x
272
273 let global_types = ref StringMap.empty
274
275 let mk_typ e =
276 if fv e = [] then type_node e
277 else raise_loc e.loc' (Pattern "Capture variables are not allowed in types")
278
279
280 let typ e =
281 mk_typ (compile !global_types e)
282
283 let pat e =
284 let e = compile !global_types e in
285 pat_node e
286
287 let register_global_types b =
288 let env = compile_many !global_types b in
289 List.iter (fun (v,_) ->
290 let d = Types.descr (mk_typ (StringMap.find v env)) in
291 let d = Types.normalize d in
292 Types.Print.register_global v d
293 ) b;
294 global_types := env
295
296
297 (* II. Build skeleton *)
298
299 module Fv = StringSet
300
301 let rec expr { loc = loc; descr = d } =
302 let (fv,td) =
303 match d with
304 | DebugTyper t -> (Fv.empty, Typed.DebugTyper (typ t))
305 | Var s -> (Fv.singleton s, Typed.Var s)
306 | Apply (e1,e2) ->
307 let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
308 (Fv.union fv1 fv2, Typed.Apply (e1,e2))
309 | Abstraction a ->
310 let iface = List.map (fun (t1,t2) -> (typ t1, typ t2)) a.fun_iface in
311 let t = List.fold_left
312 (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2))
313 Types.any iface in
314 let iface = List.map
315 (fun (t1,t2) -> (Types.descr t1, Types.descr t2))
316 iface in
317 let (fv0,body) = branches a.fun_body in
318 let fv = match a.fun_name with
319 | None -> fv0
320 | Some f -> Fv.remove f fv0 in
321 (fv,
322 Typed.Abstraction
323 { Typed.fun_name = a.fun_name;
324 Typed.fun_iface = iface;
325 Typed.fun_body = body;
326 Typed.fun_typ = t;
327 Typed.fun_fv = Fv.elements fv0
328 }
329 )
330 | Cst c -> (Fv.empty, Typed.Cst c)
331 | Pair (e1,e2) ->
332 let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
333 (Fv.union fv1 fv2, Typed.Pair (e1,e2))
334 | Dot (e,l) ->
335 let (fv,e) = expr e in
336 (fv, Typed.Dot (e,l))
337 | RecordLitt r ->
338 (* Note: quadratic check for non duplication of labels.
339 Should improve that to O(n log n) for dealing
340 with huge number of attributes ?
341 *)
342 let fv = ref Fv.empty in
343 let labs = ref [] in
344 let r = List.map
345 (fun (l,e) ->
346 let (fv2,e) = expr e in
347 if (List.mem l !labs) then
348 raise_loc loc (MultipleLabel l);
349 labs := l :: !labs;
350 fv := Fv.union !fv fv2;
351 (l,e)
352 ) r in
353 (!fv, Typed.RecordLitt r)
354 | Op (op,le) ->
355 let (fvs,ltes) = List.split (List.map expr le) in
356 let fv = List.fold_left Fv.union Fv.empty fvs in
357 (fv, Typed.Op (op,ltes))
358 | Match (e,b) ->
359 let (fv1,e) = expr e
360 and (fv2,b) = branches b in
361 (Fv.union fv1 fv2, Typed.Match (e, b))
362 | Map (e,b) ->
363 let (fv1,e) = expr e
364 and (fv2,b) = branches b in
365 (Fv.union fv1 fv2, Typed.Map (e, b))
366 in
367 fv,
368 { Typed.exp_loc = loc;
369 Typed.exp_typ = Types.empty;
370 Typed.exp_descr = td;
371 }
372
373 and branches b =
374 let fv = ref Fv.empty in
375 let accept = ref Types.empty in
376 let b = List.map
377 (fun (p,e) ->
378 let (fv2,e) = expr e in
379 fv := Fv.union !fv fv2;
380 let p = pat p in
381 accept := Types.cup !accept (Types.descr (Patterns.accept p));
382 { Typed.br_used = false;
383 Typed.br_pat = p;
384 Typed.br_body = e }
385 ) b in
386 (!fv,
387 {
388 Typed.br_typ = Types.empty;
389 Typed.br_branches = b;
390 Typed.br_accept = !accept;
391 Typed.br_compiled = None;
392 }
393 )
394
395 module Env = StringMap
396
397 open Typed
398
399
400 let check loc t s msg =
401 if not (Types.subtype t s) then raise_loc loc (Constraint (t, s, msg))
402
403 let rec type_check env e constr precise =
404 (* Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
405 Types.Print.print_descr constr precise;
406 *)
407 let d = type_check' e.exp_loc env e.exp_descr constr precise in
408 e.exp_typ <- Types.cup e.exp_typ d;
409 d
410
411 and type_check' loc env e constr precise = match e with
412 | Abstraction a ->
413 let t =
414 try Types.Arrow.check_strenghten a.fun_typ constr
415 with Not_found ->
416 raise_loc loc
417 (ShouldHave
418 (constr, "but the interface of the abstraction is not compatible"))
419 in
420 let env = match a.fun_name with
421 | None -> env
422 | Some f -> Env.add f a.fun_typ env in
423 List.iter
424 (fun (t1,t2) ->
425 ignore (type_check_branches loc env t1 a.fun_body t2 false)
426 ) a.fun_iface;
427 t
428 | Match (e,b) ->
429 let t = type_check env e b.br_accept true in
430 type_check_branches loc env t b constr precise
431
432 | Pair (e1,e2) ->
433 let rects = Types.Product.get constr in
434 if Types.Product.is_empty rects then
435 raise_loc loc (ShouldHave (constr,"but it is a pair."));
436 let pi1 = Types.Product.pi1 rects in
437
438 let t1 = type_check env e1 (Types.Product.pi1 rects)
439 (precise || (Types.Product.need_second rects))in
440 let rects = Types.Product.restrict_1 rects t1 in
441 let t2 = type_check env e2 (Types.Product.pi2 rects) precise in
442 if precise then
443 Types.times (Types.cons t1) (Types.cons t2)
444 else
445 constr
446
447 | RecordLitt r ->
448 let rconstr = Types.Record.get constr in
449 if Types.Record.is_empty rconstr then
450 raise_loc loc (ShouldHave (constr,"but it is a record."));
451
452 let (rconstr,res) =
453 List.fold_left
454 (fun (rconstr,res) (l,e) ->
455 let rconstr = Types.Record.restrict_label_present rconstr l in
456 let pi = Types.Record.project_field rconstr l in
457 if Types.Record.is_empty rconstr then
458 raise_loc loc
459 (ShouldHave (constr,(Printf.sprintf
460 "Field %s is not allowed here."
461 (Types.label_name l)
462 )
463 ));
464 let t = type_check env e pi true in
465 let rconstr = Types.Record.restrict_field rconstr l t in
466
467 let res =
468 if precise
469 then Types.cap res (Types.record l false (Types.cons t))
470 else res in
471 (rconstr,res)
472 ) (rconstr, if precise then Types.Record.any else constr) r
473 in
474 res
475
476 | Map (e,b) ->
477 let t = type_check env e (Sequence.star b.br_accept) true in
478
479 let constr' = Sequence.approx (Types.cap Sequence.any constr) in
480 let exact = Types.subtype (Sequence.star constr') constr in
481
482 if exact then (
483 (* Note: typing mail fail because of the approx on t *)
484 let res = type_check_branches loc env (Sequence.approx t)
485 b constr' precise in
486 if precise then Sequence.star res else constr
487 )
488 else
489 (* Note:
490 - could be more precise by integrating the decomposition
491 of constr inside Sequence.map.
492 *)
493 let res =
494 Sequence.map
495 (fun t -> type_check_branches loc env t b constr' true)
496 t in
497 if not exact then check loc res constr "";
498 if precise then res else constr
499 | Op ("@", [e1;e2]) ->
500 let constr' = Sequence.star
501 (Sequence.approx (Types.cap Sequence.any constr)) in
502 let exact = Types.subtype constr' constr in
503 if exact then
504 let t1 = type_check env e1 constr' precise
505 and t2 = type_check env e2 constr' precise in
506 if precise then Sequence.concat t1 t2 else constr
507 else
508 (* Note:
509 the knownledge of t1 may makes it useless to
510 check t2 with 'precise' ... *)
511 let t1 = type_check env e1 constr' true
512 and t2 = type_check env e2 constr' true in
513 let res = Sequence.concat t1 t2 in
514 check loc res constr "";
515 if precise then res else constr
516 | Op ("flatten", [e]) ->
517 let constr' = Sequence.star
518 (Sequence.approx (Types.cap Sequence.any constr)) in
519 let sconstr' = Sequence.star constr' in
520 let exact = Types.subtype constr' constr in
521 if exact then
522 let t = type_check env e sconstr' precise in
523 if precise then Sequence.flatten t else constr
524 else
525 let t = type_check env e sconstr' true in
526 let res = Sequence.flatten t in
527 check loc res constr "";
528 if precise then res else constr
529 | _ ->
530 let t : Types.descr = compute_type' loc env e in
531 check loc t constr "";
532 t
533
534 and compute_type env e =
535 type_check env e Types.any true
536
537 and compute_type' loc env = function
538 | DebugTyper t -> Types.descr t
539 | Var s ->
540 (try Env.find s env
541 with Not_found -> raise_loc loc (UnboundId s)
542 )
543 | Apply (e1,e2) ->
544 let t1 = type_check env e1 Types.Arrow.any true in
545 let t1 = Types.Arrow.get t1 in
546 let dom = Types.Arrow.domain t1 in
547 if Types.Arrow.need_arg t1 then
548 let t2 = type_check env e2 dom true in
549 Types.Arrow.apply t1 t2
550 else
551 (ignore (type_check env e2 dom false); Types.Arrow.apply_noarg t1)
552 | Cst c -> Types.constant c
553 | Dot (e,l) ->
554 let t = type_check env e Types.Record.any true in
555 (try (Types.Record.project t l)
556 with Not_found -> raise_loc loc (WrongLabel(t,l)))
557 | Op (op, el) ->
558 let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
559 type_op loc op args
560 | Map (e,b) ->
561 let t = compute_type env e in
562 Sequence.map (fun t -> type_check_branches loc env t b Types.any true) t
563
564 (* We keep these cases here to allow comparison and benchmarking ...
565 Just comment the corresponding cases in type_check' to
566 activate these ones.
567 *)
568 | Pair (e1,e2) ->
569 let t1 = compute_type env e1
570 and t2 = compute_type env e2 in
571 Types.times (Types.cons t1) (Types.cons t2)
572 | RecordLitt r ->
573 List.fold_left
574 (fun accu (l,e) ->
575 let t = compute_type env e in
576 let t = Types.record l false (Types.cons t) in
577 Types.cap accu t
578 ) Types.Record.any r
579
580
581 | _ -> assert false
582
583 and type_check_branches loc env targ brs constr precise =
584 if Types.is_empty targ then Types.empty
585 else (
586 brs.br_typ <- Types.cup brs.br_typ targ;
587 branches_aux loc env targ
588 (if precise then Types.empty else constr)
589 constr precise brs.br_branches
590 )
591
592 and branches_aux loc env targ tres constr precise = function
593 | [] -> raise_loc loc (NonExhaustive targ)
594 | b :: rem ->
595 let p = b.br_pat in
596 let acc = Types.descr (Patterns.accept p) in
597
598 let targ' = Types.cap targ acc in
599 if Types.is_empty targ'
600 then branches_aux loc env targ tres constr precise rem
601 else
602 ( b.br_used <- true;
603 let res = Patterns.filter targ' p in
604 let env' = List.fold_left
605 (fun env (x,t) -> Env.add x (Types.descr t) env)
606 env res in
607 let t = type_check env' b.br_body constr precise in
608 let tres = if precise then Types.cup t tres else tres in
609 let targ'' = Types.diff targ acc in
610 if (Types.non_empty targ'') then
611 branches_aux loc env targ'' tres constr precise rem
612 else
613 tres
614 )
615
616 and type_op loc op args =
617 match (op,args) with
618 | ("+", [loc1,t1; loc2,t2]) ->
619 type_int_binop Intervals.add loc1 t1 loc2 t2
620 | ("*", [loc1,t1; loc2,t2]) ->
621 type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
622 | ("@", [loc1,t1; loc2,t2]) ->
623 check loc1 t1 Sequence.any
624 "The first argument of @ must be a sequence";
625 Sequence.concat t1 t2
626 | ("flatten", [loc1,t1]) ->
627 check loc1 t1 Sequence.seqseq
628 "The argument of flatten must be a sequence of sequences";
629 Sequence.flatten t1
630 | _ -> assert false
631
632 and type_int_binop f loc1 t1 loc2 t2 =
633 if not (Types.Int.is_int t1) then
634 raise_loc loc1
635 (Constraint
636 (t1,Types.Int.any,
637 "The first argument must be an integer"));
638 if not (Types.Int.is_int t2) then
639 raise_loc loc2
640 (Constraint
641 (t2,Types.Int.any,
642 "The second argument must be an integer"));
643 Types.Int.put
644 (f (Types.Int.get t1) (Types.Int.get t2));
645
646

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