/[svn]/types/types.ml
ViewVC logotype

Contents of /types/types.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 956 - (show annotations)
Tue Jul 10 18:13:56 2007 UTC (5 years, 10 months ago) by abate
File size: 51120 byte(s)
[r2004-01-20 16:12:13 by afrisch] Debut des types abstraits

Original author: afrisch
Date: 2004-01-20 16:12:15+00:00
1 open Ident
2 open Encodings
3
4 (* TODO:
5 - I store hash in types to avoid computing it several times.
6 Does not seem to help a lot.
7 *)
8
9 (*
10 To be sure not to use generic comparison ...
11 *)
12 let (=) : int -> int -> bool = (==)
13 let (<) : int -> int -> bool = (<)
14 let (<=) : int -> int -> bool = (<=)
15 let (<>) : int -> int -> bool = (<>)
16 let compare = 1
17
18
19 module CompUnit = struct
20 include Pool.Make(Utf8)
21 module Tbl = Inttbl
22
23 let pervasives = mk (U.mk "Pervasives")
24
25 let close_serialize_ref = ref (fun () -> assert false)
26
27 let depend = Inttbl.create ()
28
29 let serialize t cu =
30 if cu != pervasives then Inttbl.add depend cu ();
31 serialize t cu
32
33 let close_serialize () =
34 !close_serialize_ref ();
35 let deps = Inttbl.fold depend (fun cu () l -> cu :: l) [] in
36 Inttbl.clear depend;
37 deps
38
39
40 let stack = ref []
41 let current = ref dummy_min
42
43 let enter i = stack := !current :: !stack; current := i
44
45 let leave () =
46 match !stack with
47 | hd::tl -> current := hd; stack := tl
48 | _ -> assert false
49
50
51 let () = enter pervasives
52 end
53
54
55 type const =
56 | Integer of Intervals.V.t
57 | Atom of Atoms.V.t
58 | Char of Chars.V.t
59 | Pair of const * const
60 | Xml of const * const
61 | Record of const label_map
62 | String of U.uindex * U.uindex * U.t * const
63
64 module Const = struct
65 type t = const
66
67 let rec check = function
68 | Integer i -> Intervals.V.check i
69 | Atom i -> Atoms.V.check i
70 | Char i -> Chars.V.check i
71 | Pair (x,y) | Xml (x,y) -> check x; check y
72 | Record l -> LabelMap.iter check l
73 | String (i,j,s,q) -> U.check s; check q
74
75 let dump ppf _ =
76 Format.fprintf ppf "<Types.Const.t>"
77
78 let rec serialize s = function
79 | Integer x ->
80 Serialize.Put.bits 3 s 0;
81 Intervals.V.serialize s x
82 | Atom x ->
83 Serialize.Put.bits 3 s 1;
84 Atoms.V.serialize s x
85 | Char x ->
86 Serialize.Put.bits 3 s 2;
87 Chars.V.serialize s x
88 | Pair (x,y) ->
89 Serialize.Put.bits 3 s 3;
90 serialize s x;
91 serialize s y
92 | Xml (x,y) ->
93 Serialize.Put.bits 3 s 4;
94 serialize s x;
95 serialize s y
96 | Record r ->
97 Serialize.Put.bits 3 s 5;
98 LabelMap.serialize serialize s r
99 | String (i,j,st,q) ->
100 Serialize.Put.bits 3 s 6;
101 U.serialize_sub s st i j;
102 serialize s q
103
104 let rec deserialize s =
105 match Serialize.Get.bits 3 s with
106 | 0 ->
107 Integer (Intervals.V.deserialize s)
108 | 1 ->
109 Atom (Atoms.V.deserialize s)
110 | 2 ->
111 Char (Chars.V.deserialize s)
112 | 3 ->
113 let x = deserialize s in
114 let y = deserialize s in
115 Pair (x,y)
116 | 4 ->
117 let x = deserialize s in
118 let y = deserialize s in
119 Xml (x,y)
120 | 5 ->
121 Record (LabelMap.deserialize deserialize s)
122 | 6 ->
123 let st = U.deserialize s in
124 let q = deserialize s in
125 String (U.start_index st, U.end_index st, st, q)
126 | _ ->
127 assert false
128
129 let rec compare c1 c2 = match (c1,c2) with
130 | Integer x, Integer y -> Intervals.V.compare x y
131 | Integer _, _ -> -1
132 | _, Integer _ -> 1
133 | Atom x, Atom y -> Atoms.V.compare x y
134 | Atom _, _ -> -1
135 | _, Atom _ -> 1
136 | Char x, Char y -> Chars.V.compare x y
137 | Char _, _ -> -1
138 | _, Char _ -> 1
139 | Pair (x1,x2), Pair (y1,y2) ->
140 let c = compare x1 y1 in
141 if c <> 0 then c else compare x2 y2
142 | Pair (_,_), _ -> -1
143 | _, Pair (_,_) -> 1
144 | Xml (x1,x2), Xml (y1,y2) ->
145 let c = compare x1 y1 in
146 if c <> 0 then c else compare x2 y2
147 | Xml (_,_), _ -> -1
148 | _, Xml (_,_) -> 1
149 | Record x, Record y ->
150 LabelMap.compare compare x y
151 | Record _, _ -> -1
152 | _, Record _ -> 1
153 | String (i1,j1,s1,r1), String (i2,j2,s2,r2) ->
154 let c = Pervasives.compare i1 i2 in if c <> 0 then c
155 else let c = Pervasives.compare j1 j2 in if c <> 0 then c
156 else let c = U.compare s1 s2 in if c <> 0 then c (* Should compare
157 only the substring *)
158 else compare r1 r2
159
160 let rec hash = function
161 | Integer x -> 1 + 17 * (Intervals.V.hash x)
162 | Atom x -> 2 + 17 * (Atoms.V.hash x)
163 | Char x -> 3 + 17 * (Chars.V.hash x)
164 | Pair (x,y) -> 4 + 17 * (hash x) + 257 * (hash y)
165 | Xml (x,y) -> 5 + 17 * (hash x) + 257 * (hash y)
166 | Record x -> 6 + 17 * (LabelMap.hash hash x)
167 | String (i,j,s,r) -> 7 + 17 * (U.hash s) + 257 * hash r
168 (* Note: improve hash for String *)
169
170 let equal c1 c2 = compare c1 c2 = 0
171 end
172
173 module Abstract =
174 struct
175 module T = Custom.Pair(CompUnit)(Id)
176 type abs = T.t
177
178 module V =
179 struct
180 type t = abs * Obj.t
181 end
182
183 include SortedList.FiniteCofinite(T)
184
185 let print = function
186 | Finite [] -> [ ]
187 | Cofinite [] -> [ fun ppf -> Format.fprintf ppf "Abstract" ]
188 | _ -> failwith "Types.Abstract.print"
189
190 end
191
192
193 type pair_kind = [ `Normal | `XML ]
194
195
196 module rec Descr :
197 sig
198 (*
199 Want to write:
200 type s = { ... }
201 include Custom.T with type t = s
202 but a bug (?) in OCaml 3.07 makes it impossible
203 *)
204 type t = {
205 mutable hash: int;
206 atoms : Atoms.t;
207 ints : Intervals.t;
208 chars : Chars.t;
209 times : BoolPair.t;
210 xml : BoolPair.t;
211 arrow : BoolPair.t;
212 record: BoolRec.t;
213 abstract: Abstract.t;
214 absent: bool
215 }
216 val empty: t
217 val dump: Format.formatter -> t -> unit
218 val check: t -> unit
219 val equal: t -> t -> bool
220 val hash: t -> int
221 val compare:t -> t -> int
222 val serialize: t Serialize.Put.f
223 val deserialize: t Serialize.Get.f
224 end =
225 struct
226 type t = {
227 mutable hash: int;
228 atoms : Atoms.t;
229 ints : Intervals.t;
230 chars : Chars.t;
231 times : BoolPair.t;
232 xml : BoolPair.t;
233 arrow : BoolPair.t;
234 record: BoolRec.t;
235 abstract: Abstract.t;
236 absent: bool
237 }
238
239 let dump ppf _ =
240 Format.fprintf ppf "<Types.Descr.t>"
241
242 let empty = {
243 hash = 0;
244 times = BoolPair.empty;
245 xml = BoolPair.empty;
246 arrow = BoolPair.empty;
247 record= BoolRec.empty;
248 ints = Intervals.empty;
249 atoms = Atoms.empty;
250 chars = Chars.empty;
251 abstract = Abstract.empty;
252 absent= false;
253 }
254
255 let equal a b =
256 (a == b) || (
257 (Atoms.equal a.atoms b.atoms) &&
258 (Chars.equal a.chars b.chars) &&
259 (Intervals.equal a.ints b.ints) &&
260 (BoolPair.equal a.times b.times) &&
261 (BoolPair.equal a.xml b.xml) &&
262 (BoolPair.equal a.arrow b.arrow) &&
263 (BoolRec.equal a.record b.record) &&
264 (Abstract.equal a.abstract b.abstract) &&
265 (a.absent == b.absent)
266 )
267
268 let compare a b =
269 if a == b then 0
270 else let c = Atoms.compare a.atoms b.atoms in if c <> 0 then c
271 else let c = Chars.compare a.chars b.chars in if c <> 0 then c
272 else let c = Intervals.compare a.ints b.ints in if c <> 0 then c
273 else let c = BoolPair.compare a.times b.times in if c <> 0 then c
274 else let c = BoolPair.compare a.xml b.xml in if c <> 0 then c
275 else let c = BoolPair.compare a.arrow b.arrow in if c <> 0 then c
276 else let c = BoolRec.compare a.record b.record in if c <> 0 then c
277 else let c = Abstract.compare a.abstract b.abstract in if c <> 0 then c
278 else if a.absent && not b.absent then -1
279 else if b.absent && not a.absent then 1
280 else 0
281
282 let hash a =
283 if a.hash <> 0 then a.hash else (
284 let accu = Chars.hash a.chars in
285 let accu = 17 * accu + Intervals.hash a.ints in
286 let accu = 17 * accu + Atoms.hash a.atoms in
287 let accu = 17 * accu + BoolPair.hash a.times in
288 let accu = 17 * accu + BoolPair.hash a.xml in
289 let accu = 17 * accu + BoolPair.hash a.arrow in
290 let accu = 17 * accu + BoolRec.hash a.record in
291 let accu = 17 * accu + Abstract.hash a.abstract in
292 let accu = if a.absent then accu+5 else accu in
293 a.hash <- accu;
294 accu
295 )
296
297 let check a =
298 Chars.check a.chars;
299 Intervals.check a.ints;
300 Atoms.check a.atoms;
301 BoolPair.check a.times;
302 BoolPair.check a.xml;
303 BoolPair.check a.arrow;
304 BoolRec.check a.record;
305 Abstract.check a.abstract;
306 ()
307
308
309 let serialize t a =
310 Chars.serialize t a.chars;
311 Intervals.serialize t a.ints;
312 Atoms.serialize t a.atoms;
313 BoolPair.serialize t a.times;
314 BoolPair.serialize t a.xml;
315 BoolPair.serialize t a.arrow;
316 BoolRec.serialize t a.record;
317 Abstract.serialize t a.abstract;
318 Serialize.Put.bool t a.absent
319
320 let deserialize t =
321 let chars = Chars.deserialize t in
322 let ints = Intervals.deserialize t in
323 let atoms = Atoms.deserialize t in
324 let times = BoolPair.deserialize t in
325 let xml = BoolPair.deserialize t in
326 let arrow = BoolPair.deserialize t in
327 let record = BoolRec.deserialize t in
328 let abstract = Abstract.deserialize t in
329 let absent = Serialize.Get.bool t in
330 let d = { hash=0;
331 chars = chars; ints = ints; atoms = atoms; times = times; xml = xml;
332 arrow = arrow; record = record; abstract = abstract; absent = absent } in
333 check d;
334 d
335
336
337 end
338 and Node :
339 sig
340 type t = { id : int; comp_unit: CompUnit.t; mutable descr : Descr.t }
341 val dump: Format.formatter -> t -> unit
342 val check: t -> unit
343 val equal: t -> t -> bool
344 val hash: t -> int
345 val compare:t -> t -> int
346 val serialize: t Serialize.Put.f
347 val deserialize: t Serialize.Get.f
348 val mk: int -> Descr.t -> t
349 end =
350
351 struct
352 type t = { id : int; comp_unit : CompUnit.t; mutable descr : Descr.t }
353 let check n = ()
354 let dump ppf n = failwith "Types.Node.dump"
355 let hash x = x.id + 17 * x.comp_unit
356 let compare x y =
357 let c = x.id - y.id in
358 if c = 0 then x.comp_unit - y.comp_unit else c
359 let equal x y = x == y
360
361 let serialize_memo = Inttbl.create ()
362 let counter_serialize = ref 0
363
364 let () =
365 CompUnit.close_serialize_ref :=
366 (fun () ->
367 Inttbl.clear serialize_memo;
368 counter_serialize := 0)
369
370 let serialize t n =
371 if n.comp_unit == !CompUnit.current then (
372 Serialize.Put.bool t true;
373 try
374 let i = Inttbl.find serialize_memo n.id in
375 Serialize.Put.int t i
376 with Not_found ->
377 let i = !counter_serialize in
378 incr counter_serialize;
379 Inttbl.add serialize_memo n.id i;
380 Serialize.Put.int t i;
381 Descr.serialize t n.descr
382 ) else (
383 Serialize.Put.bool t false;
384 CompUnit.serialize t n.comp_unit;
385 Serialize.Put.int t n.id
386 )
387
388
389 let deserialize_memo = Inttbl.create ()
390
391 let find_tbl id =
392 try Inttbl.find deserialize_memo id
393 with Not_found ->
394 let tbl = Inttbl.create () in
395 Inttbl.add deserialize_memo id tbl;
396 tbl
397
398 let mk id d =
399 let n = { id = id; comp_unit = !CompUnit.current; descr = d } in
400 if !CompUnit.current == CompUnit.pervasives then
401 Inttbl.add (find_tbl CompUnit.pervasives) n.id n;
402 n
403
404 let deserialize t =
405 if Serialize.Get.bool t then
406 let i = Serialize.Get.int t in
407 let tbl = find_tbl !CompUnit.current in
408 try Inttbl.find tbl i
409 with Not_found ->
410 let n = { id = i; comp_unit = !CompUnit.current;
411 descr = Descr.empty } in
412 Inttbl.add tbl i n;
413 n.descr <- Descr.deserialize t;
414 n
415 else
416 let cu = CompUnit.deserialize t in
417 let i = Serialize.Get.int t in
418 try Inttbl.find (Inttbl.find deserialize_memo cu) i
419 with Not_found -> assert false
420
421 end
422
423 (* It is also possible to use Boolean instead of Bool here;
424 need to analyze when each one is more efficient *)
425 and BoolPair : Bool.S with type elem = Node.t * Node.t =
426 Bool.Make(Custom.Pair(Node)(Node))
427
428 and BoolRec : Bool.S with type elem = bool * Node.t label_map =
429 Bool.Make(Custom.Pair(Custom.Bool)(LabelSet.MakeMap(Node)))
430
431 module DescrHash = Hashtbl.Make(Descr)
432 module DescrMap = Map.Make(Descr)
433 module DescrSet = Set.Make(Descr)
434 module DescrSList = SortedList.Make(Descr)
435
436 type descr = Descr.t
437 type node = Node.t
438 include Descr
439
440 let hash_cons = DescrHash.create 17000
441
442 let count = State.ref "Types.count" 0
443
444 let () =
445 Stats.register Stats.Summary
446 (fun ppf -> Format.fprintf ppf "Allocated type nodes:%i@\n" !count)
447
448 let make () =
449 incr count;
450 Node.mk !count empty
451
452 let define n d =
453 DescrHash.add hash_cons d n;
454 n.Node.descr <- d
455 let cons d =
456 try DescrHash.find hash_cons d
457 with Not_found ->
458 incr count;
459 let n = Node.mk !count d in
460 DescrHash.add hash_cons d n; n
461
462 let any = {
463 hash = 0;
464 times = BoolPair.full;
465 xml = BoolPair.full;
466 arrow = BoolPair.full;
467 record= BoolRec.full;
468 ints = Intervals.any;
469 atoms = Atoms.any;
470 chars = Chars.any;
471 abstract = Abstract.any;
472 absent= false;
473 }
474
475 let non_constructed =
476 { any with
477 hash = 0;
478 times = empty.times; xml = empty.xml; record = empty.record }
479
480
481 let interval i = { empty with hash = 0; ints = i }
482 let times x y = { empty with hash = 0; times = BoolPair.atom (x,y) }
483 let xml x y = { empty with hash = 0; xml = BoolPair.atom (x,y) }
484 let arrow x y = { empty with hash = 0; arrow = BoolPair.atom (x,y) }
485 let record label t =
486 { empty with hash = 0;
487 record = BoolRec.atom (true,LabelMap.singleton label t) }
488 let record' (x : bool * node Ident.label_map) =
489 { empty with hash = 0; record = BoolRec.atom x }
490 let atom a = { empty with hash = 0; atoms = a }
491 let char c = { empty with hash = 0; chars = c }
492 let abstract a = { empty with hash = 0; abstract = a }
493
494 let get_abstract t = t.abstract
495
496 let cup x y =
497 if x == y then x else {
498 hash = 0;
499 times = BoolPair.cup x.times y.times;
500 xml = BoolPair.cup x.xml y.xml;
501 arrow = BoolPair.cup x.arrow y.arrow;
502 record= BoolRec.cup x.record y.record;
503 ints = Intervals.cup x.ints y.ints;
504 atoms = Atoms.cup x.atoms y.atoms;
505 chars = Chars.cup x.chars y.chars;
506 abstract = Abstract.cup x.abstract y.abstract;
507 absent= x.absent || y.absent;
508 }
509
510 let cap x y =
511 if x == y then x else {
512 hash = 0;
513 times = BoolPair.cap x.times y.times;
514 xml = BoolPair.cap x.xml y.xml;
515 record= BoolRec.cap x.record y.record;
516 arrow = BoolPair.cap x.arrow y.arrow;
517 ints = Intervals.cap x.ints y.ints;
518 atoms = Atoms.cap x.atoms y.atoms;
519 chars = Chars.cap x.chars y.chars;
520 abstract = Abstract.cap x.abstract y.abstract;
521 absent= x.absent && y.absent;
522 }
523
524 let diff x y =
525 if x == y then empty else {
526 hash = 0;
527 times = BoolPair.diff x.times y.times;
528 xml = BoolPair.diff x.xml y.xml;
529 arrow = BoolPair.diff x.arrow y.arrow;
530 record= BoolRec.diff x.record y.record;
531 ints = Intervals.diff x.ints y.ints;
532 atoms = Atoms.diff x.atoms y.atoms;
533 chars = Chars.diff x.chars y.chars;
534 abstract = Abstract.diff x.abstract y.abstract;
535 absent= x.absent && not y.absent;
536 }
537
538
539
540
541 (* TODO: optimize disjoint check for boolean combinations *)
542 let trivially_disjoint a b =
543 (Chars.disjoint a.chars b.chars) &&
544 (Intervals.disjoint a.ints b.ints) &&
545 (Atoms.disjoint a.atoms b.atoms) &&
546 (BoolPair.trivially_disjoint a.times b.times) &&
547 (BoolPair.trivially_disjoint a.xml b.xml) &&
548 (BoolPair.trivially_disjoint a.arrow b.arrow) &&
549 (BoolRec.trivially_disjoint a.record b.record) &&
550 (Abstract.disjoint a.abstract b.abstract) &&
551 (not (a.absent && b.absent))
552
553
554
555 let descr n = n.Node.descr
556 let internalize n = n
557 let id n = n.Node.id
558
559
560 let rec constant = function
561 | Integer i -> interval (Intervals.atom i)
562 | Atom a -> atom (Atoms.atom a)
563 | Char c -> char (Chars.atom c)
564 | Pair (x,y) -> times (const_node x) (const_node y)
565 | Xml (x,y) -> times (const_node x) (const_node y)
566 | Record x -> record' (false ,LabelMap.map const_node x)
567 | String (i,j,s,c) ->
568 if U.equal_index i j then constant c
569 else
570 let (ch,i') = U.next s i in
571 constant (Pair (Char (Chars.V.mk_int ch), String (i',j,s,c)))
572 and const_node c = cons (constant c)
573
574 let neg x = diff any x
575
576 let any_node = cons any
577
578 module LabelS = Set.Make(LabelPool)
579
580 let any_or_absent = { any with hash=0; absent = true }
581 let only_absent = { empty with hash=0; absent = true }
582
583 let get_record r =
584 let labs accu (_,r) =
585 List.fold_left
586 (fun accu (l,_) -> LabelS.add l accu) accu (LabelMap.get r) in
587 let extend descrs labs (o,r) =
588 let rec aux i labs r =
589 match labs with
590 | [] -> ()
591 | l1::labs ->
592 match r with
593 | (l2,x)::r when l1 == l2 ->
594 descrs.(i) <- cap descrs.(i) (descr x);
595 aux (i+1) labs r
596 | r ->
597 if not o then
598 descrs.(i) <- cap descrs.(i) only_absent; (* TODO:OPT *)
599 aux (i+1) labs r
600 in
601 aux 0 labs (LabelMap.get r);
602 o
603 in
604 let line (p,n) =
605 let labels =
606 List.fold_left labs (List.fold_left labs LabelS.empty p) n in
607 let labels = LabelS.elements labels in
608 let nlab = List.length labels in
609 let mk () = Array.create nlab any_or_absent in
610
611 let pos = mk () in
612 let opos = List.fold_left
613 (fun accu x ->
614 (extend pos labels x) && accu)
615 true p in
616 let p = (opos, pos) in
617
618 let n = List.map (fun x ->
619 let neg = mk () in
620 let o = extend neg labels x in
621 (o,neg)
622 ) n in
623 (labels,p,n)
624 in
625 List.map line (BoolRec.get r)
626
627
628
629
630
631 (* Subtyping algorithm *)
632
633 let diff_t d t = diff d (descr t)
634 let cap_t d t = cap d (descr t)
635 let cup_t d t = cup d (descr t)
636 let cap_product any_left any_right l =
637 List.fold_left
638 (fun (d1,d2) (t1,t2) -> (cap_t d1 t1, cap_t d2 t2))
639 (any_left,any_right)
640 l
641 let any_pair = { empty with hash = 0; times = any.times }
642
643
644 let rec exists max f =
645 (max > 0) && (f (max - 1) || exists (max - 1) f)
646
647 exception NotEmpty
648
649 type slot = { mutable status : status;
650 mutable notify : notify;
651 mutable active : bool }
652 and status = Empty | NEmpty | Maybe
653 and notify = Nothing | Do of slot * (slot -> unit) * notify
654
655 let slot_empty = { status = Empty; active = false; notify = Nothing }
656 let slot_not_empty = { status = NEmpty; active = false; notify = Nothing }
657
658 let rec notify = function
659 | Nothing -> ()
660 | Do (n,f,rem) ->
661 if n.status == Maybe then (try f n with NotEmpty -> ());
662 notify rem
663
664 let rec iter_s s f = function
665 | [] -> ()
666 | arg::rem -> f arg s; iter_s s f rem
667
668
669 let set s =
670 s.status <- NEmpty;
671 notify s.notify;
672 s.notify <- Nothing;
673 raise NotEmpty
674
675 let rec big_conj f l n =
676 match l with
677 | [] -> set n
678 | [arg] -> f arg n
679 | arg::rem ->
680 let s =
681 { status = Maybe; active = false;
682 notify = Do (n,(big_conj f rem), Nothing) } in
683 try
684 f arg s;
685 if s.active then n.active <- true
686 with NotEmpty -> if n.status == NEmpty then raise NotEmpty
687
688 let guard a f n =
689 match a with
690 | { status = Empty } -> ()
691 | { status = Maybe } as s ->
692 n.active <- true;
693 s.notify <- Do (n,f,s.notify)
694 | { status = NEmpty } -> f n
695
696
697 (* Fast approximation *)
698
699 module ClearlyEmpty =
700 struct
701
702 let memo = DescrHash.create 33000
703 let marks = ref []
704
705 let rec slot d =
706 if not ((Intervals.is_empty d.ints) &&
707 (Atoms.is_empty d.atoms) &&
708 (Chars.is_empty d.chars) &&
709 (Abstract.is_empty d.abstract) &&
710 (not d.absent)) then slot_not_empty
711 else try DescrHash.find memo d
712 with Not_found ->
713 let s = { status = Maybe; active = false; notify = Nothing } in
714 DescrHash.add memo d s;
715 (try
716 iter_s s check_times (BoolPair.get d.times);
717 iter_s s check_xml (BoolPair.get d.xml);
718 iter_s s check_arrow (BoolPair.get d.arrow);
719 iter_s s check_record (get_record d.record);
720 if s.active then marks := s :: !marks else s.status <- Empty;
721 with
722 NotEmpty -> ());
723 s
724
725 and check_times (left,right) s =
726 let (accu1,accu2) = cap_product any any left in
727 let single_right (t1,t2) s =
728 let t1 = descr t1 and t2 = descr t2 in
729 if trivially_disjoint accu1 t1 || trivially_disjoint accu2 t2 then set s
730 else
731 let accu1 = diff accu1 t1 in guard (slot accu1) set s;
732 let accu2 = diff accu2 t2 in guard (slot accu2) set s in
733 guard (slot accu1) (guard (slot accu2) (big_conj single_right right)) s
734
735 and check_xml (left,right) s =
736 let (accu1,accu2) = cap_product any any_pair left in
737 let single_right (t1,t2) s =
738 let t1 = descr t1 and t2 = descr t2 in
739 if trivially_disjoint accu1 t1 || trivially_disjoint accu2 t2 then set s
740 else
741 let accu1 = diff accu1 t1 in guard (slot accu1) set s;
742 let accu2 = diff accu2 t2 in guard (slot accu2) set s in
743 guard (slot accu1) (guard (slot accu2) (big_conj single_right right)) s
744
745 and check_arrow (left,right) s =
746 let single_right (s1,s2) s =
747 let accu1 = descr s1 and accu2 = neg (descr s2) in
748 let single_left (t1,t2) s =
749 let accu1 = diff_t accu1 t1 in guard (slot accu1) set s;
750 let accu2 = cap_t accu2 t2 in guard (slot accu2) set s
751 in
752 guard (slot accu1) (big_conj single_left left) s
753 in
754 big_conj single_right right s
755
756 and check_record (labels,(oleft,left),rights) s =
757 let rec single_right (oright,right) s =
758 let next =
759 (oleft && (not oright)) ||
760 exists (Array.length left)
761 (fun i -> trivially_disjoint left.(i) right.(i))
762 in
763 if next then set s
764 else
765 for i = 0 to Array.length left - 1 do
766 let di = diff left.(i) right.(i) in guard (slot di) set s
767 done
768 in
769 let rec start i s =
770 if (i < 0) then big_conj single_right rights s
771 else guard (slot left.(i)) (start (i - 1)) s
772 in
773 start (Array.length left - 1) s
774
775
776 let is_empty d =
777 let s = slot d in
778 List.iter
779 (fun s' ->
780 if s'.status == Maybe then s'.status <- Empty; s'.notify <- Nothing)
781 !marks;
782 marks := [];
783 s.status == Empty
784 end
785
786 let clearly_disjoint t1 t2 =
787 (*
788 if trivially_disjoint t1 t2 then true
789 else
790 if ClearlyEmpty.is_empty (cap t1 t2) then
791 (Printf.eprintf "!\n"; true) else false
792 *)
793 trivially_disjoint t1 t2 || ClearlyEmpty.is_empty (cap t1 t2)
794
795 (* TODO: need to invesigate when ClearEmpty is a good thing... *)
796
797 let memo = DescrHash.create 33000
798 let marks = ref []
799
800 let rec slot d =
801 if not ((Intervals.is_empty d.ints) &&
802 (Atoms.is_empty d.atoms) &&
803 (Chars.is_empty d.chars) &&
804 (Abstract.is_empty d.abstract) &&
805 (not d.absent)) then slot_not_empty
806 else try DescrHash.find memo d
807 with Not_found ->
808 let s = { status = Maybe; active = false; notify = Nothing } in
809 DescrHash.add memo d s;
810 (try
811 iter_s s check_times (BoolPair.get d.times);
812 iter_s s check_xml (BoolPair.get d.xml);
813 iter_s s check_arrow (BoolPair.get d.arrow);
814 iter_s s check_record (get_record d.record);
815 if s.active then marks := s :: !marks else s.status <- Empty;
816 with
817 NotEmpty -> ());
818 s
819
820 and check_times (left,right) s =
821 let rec aux accu1 accu2 right s = match right with
822 | (t1,t2)::right ->
823 let t1 = descr t1 and t2 = descr t2 in
824 if trivially_disjoint accu1 t1 ||
825 trivially_disjoint accu2 t2 then (
826 aux accu1 accu2 right s )
827 else (
828 let accu1' = diff accu1 t1 in
829 guard (slot accu1') (aux accu1' accu2 right) s;
830
831 let accu2' = diff accu2 t2 in
832 guard (slot accu2') (aux accu1 accu2' right) s
833 )
834 | [] -> set s
835 in
836 let (accu1,accu2) = cap_product any any left in
837 guard (slot accu1) (guard (slot accu2) (aux accu1 accu2 right)) s
838
839 and check_xml (left,right) s =
840 let rec aux accu1 accu2 right s = match right with
841 | (t1,t2)::right ->
842 let t1 = descr t1 and t2 = descr t2 in
843 if clearly_disjoint accu1 t1 ||
844 trivially_disjoint accu2 t2 then (
845 aux accu1 accu2 right s )
846 else (
847 let accu1' = diff accu1 t1 in
848 guard (slot accu1') (aux accu1' accu2 right) s;
849
850 let accu2' = diff accu2 t2 in
851 guard (slot accu2') (aux accu1 accu2' right) s
852 )
853 | [] -> set s
854 in
855 let (accu1,accu2) = cap_product any any_pair left in
856 guard (slot accu1) (guard (slot accu2) (aux accu1 accu2 right)) s
857
858 and check_arrow (left,right) s =
859 let single_right (s1,s2) s =
860 let rec aux accu1 accu2 left s = match left with
861 | (t1,t2)::left ->
862 let accu1' = diff_t accu1 t1 in
863 guard (slot accu1') (aux accu1' accu2 left) s;
864
865 let accu2' = cap_t accu2 t2 in
866 guard (slot accu2') (aux accu1 accu2' left) s
867 | [] -> set s
868 in
869 let accu1 = descr s1 in
870 guard (slot accu1) (aux accu1 (neg (descr s2)) left) s
871 in
872 big_conj single_right right s
873
874 and check_record (labels,(oleft,left),rights) s =
875 let rec aux rights s = match rights with
876 | [] -> set s
877 | (oright,right)::rights ->
878 let next =
879 (oleft && (not oright)) ||
880 exists (Array.length left)
881 (fun i -> trivially_disjoint left.(i) right.(i))
882 in
883 if next then aux rights s
884 else
885 for i = 0 to Array.length left - 1 do
886 let back = left.(i) in
887 let di = diff back right.(i) in
888 guard (slot di) (fun s ->
889 left.(i) <- di;
890 aux rights s;
891 left.(i) <- back;
892 ) s
893 (* TODO: are side effects correct ? *)
894 done
895 in
896 let rec start i s =
897 if (i < 0) then aux rights s
898 else
899 guard (slot left.(i)) (start (i - 1)) s
900 in
901 start (Array.length left - 1) s
902
903
904 (*
905 let timer_subtype = Stats.Timer.create "Types.is_empty"
906 *)
907
908 let is_empty d =
909 (* Stats.Timer.start timer_subtype;*)
910 let s = slot d in
911 List.iter
912 (fun s' ->
913 if s'.status == Maybe then s'.status <- Empty; s'.notify <- Nothing)
914 !marks;
915 marks := [];
916 (* Stats.Timer.stop timer_subtype *)
917 (s.status == Empty)
918
919 (*
920 let is_empty d =
921 (* let b1 = ClearlyEmpty.is_empty d in
922 let b2 = is_empty d in
923 assert (b2 || not b1);
924 Printf.eprintf "b1 = %b; b2 = %b\n" b1 b2;
925 b2 *)
926 if ClearlyEmpty.is_empty d then (Printf.eprintf "!\n"; true) else is_empty d
927 *)
928
929 let non_empty d =
930 not (is_empty d)
931
932 let subtype d1 d2 =
933 is_empty (diff d1 d2)
934
935 module Product =
936 struct
937 type t = (descr * descr) list
938
939 let other ?(kind=`Normal) d =
940 match kind with
941 | `Normal -> { d with hash = 0; times = empty.times }
942 | `XML -> { d with hash = 0; xml = empty.xml }
943
944 let is_product ?kind d = is_empty (other ?kind d)
945
946 let need_second = function _::_::_ -> true | _ -> false
947
948 let normal_aux = function
949 | ([] | [ _ ]) as d -> d
950 | d ->
951
952 let res = ref [] in
953
954 let add (t1,t2) =
955 let rec loop t1 t2 = function
956 | [] -> res := (ref (t1,t2)) :: !res
957 | ({contents = (d1,d2)} as r)::l ->
958 (*OPT*)
959 (* if equal_descr d1 t1 then r := (d1,cup d2 t2) else*)
960
961 let i = cap t1 d1 in
962 if is_empty i then loop t1 t2 l
963 else (
964 r := (i, cup t2 d2);
965 let k = diff d1 t1 in
966 if non_empty k then res := (ref (k,d2)) :: !res;
967
968 let j = diff t1 d1 in
969 if non_empty j then loop j t2 l
970 )
971 in
972 loop t1 t2 !res
973 in
974 List.iter add d;
975 List.map (!) !res
976
977
978 (* Partitioning:
979
980 (t,s) - ((t1,s1) | (t2,s2) | ... | (tn,sn))
981 =
982 (t & t1, s - s1) | ... | (t & tn, s - sn) | (t - (t1|...|tn), s)
983
984 *)
985 let get_aux any_right d =
986 let accu = ref [] in
987 let line (left,right) =
988 let (d1,d2) = cap_product any any_right left in
989 if (non_empty d1) && (non_empty d2) then
990 let right = List.map (fun (t1,t2) -> descr t1, descr t2) right in
991 let right = normal_aux right in
992 let resid1 = ref d1 in
993 let () =
994 List.iter
995 (fun (t1,t2) ->
996 let t1 = cap d1 t1 in
997 if (non_empty t1) then
998 let () = resid1 := diff !resid1 t1 in
999 let t2 = diff d2 t2 in
1000 if (non_empty t2) then accu := (t1,t2) :: !accu
1001 ) right in
1002 if non_empty !resid1 then accu := (!resid1, d2) :: !accu
1003 in
1004 List.iter line (BoolPair.get d);
1005 !accu
1006 (* Maybe, can improve this function with:
1007 (t,s) \ (t1,s1) = (t&t',s\s') | (t\t',s),
1008 don't call normal_aux *)
1009
1010
1011 let get ?(kind=`Normal) d =
1012 match kind with
1013 | `Normal -> get_aux any d.times
1014 | `XML -> get_aux any_pair d.xml
1015
1016 let pi1 = List.fold_left (fun acc (t1,_) -> cup acc t1) empty
1017 let pi2 = List.fold_left (fun acc (_,t2) -> cup acc t2) empty
1018 let pi2_restricted restr =
1019 List.fold_left (fun acc (t1,t2) ->
1020 if is_empty (cap t1 restr) then acc
1021 else cup acc t2) empty
1022
1023 let restrict_1 rects pi1 =
1024 let aux acc (t1,t2) =
1025 let t1 = cap t1 pi1 in if is_empty t1 then acc else (t1,t2)::acc in
1026 List.fold_left aux [] rects
1027
1028 type normal = t
1029
1030 module Memo = Map.Make(BoolPair)
1031
1032 (* TODO: try with an hashtable *)
1033 (* Also, avoid lookup for simple products (t1,t2) *)
1034 let memo = ref Memo.empty
1035 let normal_times d =
1036 try Memo.find d !memo
1037 with
1038 Not_found ->
1039 let gd = get_aux any d in
1040 let n = normal_aux gd in
1041 (* Could optimize this call to normal_aux because one already
1042 know that each line is normalized ... *)
1043 memo := Memo.add d n !memo;
1044 n
1045
1046 let memo_xml = ref Memo.empty
1047 let normal_xml d =
1048 try Memo.find d !memo_xml
1049 with
1050 Not_found ->
1051 let gd = get_aux any_pair d in
1052 let n = normal_aux gd in
1053 memo_xml := Memo.add d n !memo_xml;
1054 n
1055
1056 let normal ?(kind=`Normal) d =
1057 match kind with
1058 | `Normal -> normal_times d.times
1059 | `XML -> normal_xml d.xml
1060
1061
1062 let merge_same_2 r =
1063 let r =
1064 List.fold_left
1065 (fun accu (t1,t2) ->
1066 let t = try DescrMap.find t2 accu with Not_found -> empty in
1067 DescrMap.add t2 (cup t t1) accu
1068 ) DescrMap.empty r in
1069 DescrMap.fold (fun t2 t1 accu -> (t1,t2)::accu) r []
1070
1071
1072 let constraint_on_2 n t1 =
1073 List.fold_left
1074 (fun accu (d1,d2) ->
1075 if is_empty (cap d1 t1) then accu else cap accu d2)
1076 any
1077 n
1078
1079 let any = { empty with hash = 0; times = any.times }
1080 and any_xml = { empty with hash = 0; xml = any.xml }
1081 let is_empty d = d == []
1082 end
1083
1084 module Record =
1085 struct
1086 let has_record d = not (is_empty { empty with hash= 0; record = d.record })
1087 let or_absent d = { d with hash = 0; absent = true }
1088 let any_or_absent = or_absent any
1089 let has_absent d = d.absent
1090
1091 let only_absent = {empty with hash = 0; absent = true}
1092 let only_absent_node = cons only_absent
1093
1094 module T = struct
1095 type t = descr
1096 let any = any_or_absent
1097 let cap = cap
1098 let cup = cup
1099 let diff = diff
1100 let is_empty = is_empty
1101 let empty = empty
1102 end
1103 module R = struct
1104 type t = descr
1105 let any = { empty with hash = 0; record = any.record }
1106 let cap = cap
1107 let cup = cup
1108 let diff = diff
1109 let is_empty = is_empty
1110 let empty = empty
1111 end
1112 module TR = Normal.Make(T)(R)
1113
1114 let any_record = { empty with hash = 0; record = BoolRec.full }
1115
1116 let atom o l =
1117 if o && LabelMap.is_empty l then any_record else
1118 { empty with hash = 0; record = BoolRec.atom (o,l) }
1119
1120 type zor = Pair of descr * descr | Any
1121
1122 let aux_split d l=
1123 let f (o,r) =
1124 try
1125 let (lt,rem) = LabelMap.assoc_remove l r in
1126 Pair (descr lt, atom o rem)
1127 with Not_found ->
1128 if o then
1129 if LabelMap.is_empty r then Any else
1130 Pair (any_or_absent, { empty with hash=0; record = BoolRec.atom (o,r) })
1131 else
1132 Pair (only_absent,
1133 { empty with hash = 0; record = BoolRec.atom (o,r) })
1134 in
1135 List.fold_left
1136 (fun b (p,n) ->
1137 let rec aux_p accu = function
1138 | x::p ->
1139 (match f x with
1140 | Pair (t1,t2) -> aux_p ((t1,t2)::accu) p
1141 | Any -> aux_p accu p)
1142 | [] -> aux_n accu [] n
1143 and aux_n p accu = function
1144 | x::n ->
1145 (match f x with
1146 | Pair (t1,t2) -> aux_n p ((t1,t2)::accu) n
1147 | Any -> b)
1148 | [] -> (p,accu) :: b in
1149 aux_p [] p)
1150 []
1151 (BoolRec.get d.record)
1152
1153 let split (d : descr) l =
1154 TR.boolean (aux_split d l)
1155
1156 let split_normal d l =
1157 TR.boolean_normal (aux_split d l)
1158
1159
1160 let project d l =
1161 let t = TR.pi1 (split d l) in
1162 if t.absent then raise Not_found;
1163 t
1164
1165 let project_opt d l =
1166 let t = TR.pi1 (split d l) in
1167 { t with hash = 0; absent = false }
1168
1169 let condition d l t =
1170 TR.pi2_restricted t (split d l)
1171
1172 (* TODO: eliminate this cap ... (reord l only_absent_node) when
1173 not necessary. eg. {| ..... |} \ l *)
1174
1175 let remove_field d l =
1176 cap (TR.pi2 (split d l)) (record l only_absent_node)
1177
1178 let first_label d =
1179 let min = ref LabelPool.dummy_max in
1180 let aux (_,r) =
1181 match LabelMap.get r with
1182 (l,_)::_ -> if (l:int) < !min then min := l | _ -> () in
1183 BoolRec.iter aux d.record;
1184 !min
1185
1186 let empty_cases d =
1187 let x = BoolRec.compute
1188 ~empty:0 ~full:3 ~cup:(lor) ~cap:(land)
1189 ~diff:(fun a b -> a land lnot b)
1190 ~atom:(function (o,r) ->
1191 assert (LabelMap.get r == []);
1192 if o then 3 else 1
1193 )
1194 d.record in
1195 (x land 2 <> 0, x land 1 <> 0)
1196
1197 let has_empty_record d =
1198 BoolRec.compute
1199 ~empty:false ~full:true ~cup:(||) ~cap:(&&)
1200 ~diff:(fun a b -> a && not b)
1201 ~atom:(function (o,r) ->
1202 List.for_all
1203 (fun (l,t) -> (descr t).absent)
1204 (LabelMap.get r)
1205 )
1206 d.record
1207
1208
1209 (*TODO: optimize merge
1210 - pre-compute the sequence of labels
1211 - remove empty or full { l = t }
1212 *)
1213
1214 let merge d1 d2 =
1215 let res = ref empty in
1216 let rec aux accu d1 d2 =
1217 let l = min (first_label d1) (first_label d2) in
1218 if l = LabelPool.dummy_max then
1219 let (some1,none1) = empty_cases d1
1220 and (some2,none2) = empty_cases d2 in
1221 let none = none1 && none2 and some = some1 || some2 in
1222 let accu = LabelMap.from_list (fun _ _ -> assert false) accu in
1223 (* approx for the case (some && not none) ... *)
1224 res := cup !res (record' (some, accu))
1225 else
1226 let l1 = split d1 l and l2 = split d2 l in
1227 let loop (t1,d1) (t2,d2) =
1228 let t =
1229 if t2.absent
1230 then cup t1 { t2 with hash = 0; absent = false }
1231 else t2
1232 in
1233 aux ((l,cons t)::accu) d1 d2
1234 in
1235 List.iter (fun x -> List.iter (loop x) l2) l1
1236
1237 in
1238 aux [] d1 d2;
1239 !res
1240
1241 let any = { empty with hash = 0; record = any.record }
1242
1243 let get d =
1244 let rec aux r accu d =
1245 let l = first_label d in
1246 if l == LabelPool.dummy_max then
1247 let (o1,o2) = empty_cases d in
1248 if o1 || o2 then (LabelMap.from_list_disj r,o1,o2)::accu else accu
1249 else
1250 List.fold_left
1251 (fun accu (t1,t2) ->
1252 let x = (t1.absent, { t1 with hash = 0; absent = false }) in
1253 aux ((l,x)::r) accu t2)
1254 accu
1255 (split d l)
1256 in
1257 aux [] [] d
1258 end
1259
1260
1261 module Print =
1262 struct
1263 let rec print_const ppf = function
1264 | Integer i -> Intervals.V.print ppf i
1265 | Atom a -> Atoms.V.print_quote ppf a
1266 | Char c -> Chars.V.print ppf c
1267 | Pair (x,y) -> Format.fprintf ppf "(%a,%a)" print_const x print_const y
1268 | Xml (x,y) -> Format.fprintf ppf "XML(%a,%a)" print_const x print_const y
1269 | Record r ->
1270 Format.fprintf ppf "Record{";
1271 List.iter
1272 (fun (l,c) ->
1273 Format.fprintf ppf "%a : %a; "
1274 Label.print (LabelPool.value l)
1275 print_const c)
1276 (LabelMap.get r);
1277 Format.fprintf ppf "}"
1278 | String (i,j,s,c) ->
1279 Format.fprintf ppf "\"%a\" @ %a"
1280 U.print (U.mk (U.get_substr s i j))
1281 print_const c
1282
1283 let nil_atom = Atoms.V.mk_ascii "nil"
1284 let nil_type = atom (Atoms.atom nil_atom)
1285 let (seqs_node,seqs_descr) =
1286 let n = make () in
1287 let d = cup nil_type (times any_node n) in
1288 define n d;
1289 (n, d)
1290
1291 let is_regexp t = subtype t seqs_descr
1292
1293 module S = struct
1294 type t = { id : int;
1295 mutable def : d list;
1296 mutable state : [ `Expand | `None | `Marked | `Named of U.t ] }
1297 and d =
1298 | Name of U.t
1299 | Regexp of t Pretty.regexp
1300 | Atomic of (Format.formatter -> unit)
1301 | Pair of t * t
1302 | Char of Chars.V.t
1303 | Xml of [ `Tag of (Format.formatter -> unit) | `Type of t ] * t * t
1304 | Record of (bool * t) label_map * bool * bool
1305 | Arrows of (t * t) list * (t * t) list
1306 | Neg of t
1307 let compare x y = x.id - y.id
1308 end
1309 module Decompile = Pretty.Decompile(DescrHash)(S)
1310 open S
1311
1312 module DescrPairMap = Map.Make(Custom.Pair(Descr)(Descr))
1313
1314 let named = State.ref "Types.Print.named" DescrMap.empty
1315 let named_xml = State.ref "Types.Print.named_xml" DescrPairMap.empty
1316 let register_global (name : U.t) d =
1317 if equal { d with hash = 0; xml = BoolPair.empty } empty then
1318 (let l = (*Product.merge_same_2*) (Product.get ~kind:`XML d) in
1319 match l with
1320 | [(t1,t2)] -> named_xml := DescrPairMap.add (t1,t2) name !named_xml
1321 | _ -> ());
1322 named := DescrMap.add d name !named
1323
1324 let memo = DescrHash.create 63
1325 let counter = ref 0
1326 let alloc def = { id = (incr counter; !counter); def = def; state = `None }
1327
1328 let count_name = ref 0
1329 let name () =
1330 incr count_name;
1331 U.mk ("X" ^ (string_of_int !count_name))
1332
1333 let to_print = ref []
1334
1335 let trivial_rec b =
1336 b == BoolRec.empty ||
1337 (is_empty { empty with hash = 0; record = BoolRec.diff BoolRec.full b})
1338
1339 let trivial_pair b = b == BoolPair.empty || b == BoolPair.full
1340
1341 let worth_abbrev d =
1342 not (trivial_pair d.times && trivial_pair d.xml &&
1343 trivial_pair d.arrow && trivial_rec d.record)
1344
1345 let worth_complement d =
1346 let aux f x y = if f x y = 0 then 1 else 0 in
1347 let n =
1348 aux Atoms.compare d.atoms any.atoms +
1349 aux Chars.compare d.chars any.chars +
1350 aux Intervals.compare d.ints any.ints +
1351 aux BoolPair.compare d.times any.times +
1352 aux BoolPair.compare d.xml any.xml +
1353 aux BoolPair.compare d.arrow any.arrow +
1354 aux BoolRec.compare d.record any.record +
1355 aux Abstract.compare d.abstract any.abstract
1356 in
1357 n >= 4
1358
1359 let rec prepare d =
1360 try DescrHash.find memo d
1361 with Not_found ->
1362 try
1363 let n = DescrMap.find d !named in
1364 let s = alloc [] in
1365 s.state <- `Named n;
1366 DescrHash.add memo d s;
1367 s
1368 with Not_found ->
1369 if worth_complement d then
1370 alloc [Neg (prepare (neg d))]
1371 else
1372 let slot = alloc [] in
1373 if not (worth_abbrev d) then slot.state <- `Expand;
1374 DescrHash.add memo d slot;
1375 let (seq,not_seq) =
1376 if (subtype { empty with hash = 0; times = d.times } seqs_descr) then
1377 (cap d seqs_descr, diff d seqs_descr)
1378 else
1379 (empty, d) in
1380
1381 let add u = slot.def <- u :: slot.def in
1382 if (non_empty seq) then
1383 add (Regexp (decompile seq));
1384 List.iter
1385 (fun (t1,t2) -> add (Pair (prepare t1, prepare t2)))
1386 (Product.get not_seq);
1387 List.iter
1388 (fun (t1,t2) ->
1389 try
1390 let n = DescrPairMap.find (t1,t2) !named_xml in
1391 add (Name n)
1392 with
1393 Not_found ->
1394 let tag =
1395 match Atoms.print_tag t1.atoms with
1396 | Some a when is_empty { t1 with hash=0; atoms = Atoms.empty } -> `Tag a
1397 | _ -> `Type (prepare t1) in
1398 assert (equal { t2 with hash=0; times = empty.times } empty);
1399 List.iter
1400 (fun (ta,tb) -> add (Xml (tag, prepare ta, prepare tb)))
1401 (Product.get t2)
1402 )
1403 ((*Product.merge_same_2*) (Product.get ~kind:`XML not_seq));
1404 List.iter
1405 (fun (r,some,none) ->
1406 let r = LabelMap.map (fun (o,t) -> (o, prepare t)) r in
1407 add (Record (r,some,none)))
1408 (Record.get not_seq);
1409 (match Chars.is_char not_seq.chars with
1410 | Some c -> add (Char c)
1411 | None ->
1412 List.iter (fun x -> add (Atomic x)) (Chars.print not_seq.chars));
1413 List.iter (fun x -> add (Atomic x)) (Intervals.print not_seq.ints);
1414 List.iter (fun x -> add (Atomic x)) (Atoms.print not_seq.atoms);
1415 List.iter (fun x -> add (Atomic x)) (Abstract.print not_seq.abstract);
1416 List.iter
1417 (fun (p,n) ->
1418 let aux (t,s) = prepare (descr t), prepare (descr s) in
1419 let p = List.map aux p and n = List.map aux n in
1420 add (Arrows (p,n)))
1421 (BoolPair.get not_seq.arrow);
1422 slot.def <- List.rev slot.def;
1423 slot
1424
1425
1426 and decompile d =
1427 Decompile.decompile
1428 (fun t ->
1429 let tr = Product.get t in
1430 let tr = List.map (fun (l,t) -> prepare l, t) tr in
1431 tr, Atoms.contains nil_atom t.atoms)
1432 d
1433
1434 let gen = ref 0
1435
1436 let rec assign_name s =
1437 incr gen;
1438 match s.state with
1439 | `None ->
1440 let g = !gen in
1441 s.state <- `Marked;
1442 List.iter assign_name_rec s.def;
1443 if (s.state == `Marked) && (!gen == g) then s.state <- `None
1444 | `Marked -> s.state <- `Named (name ()); to_print := s :: !to_print
1445 | _ -> ()
1446 and assign_name_rec = function
1447 | Neg t -> assign_name t
1448 | Name _ | Char _ | Atomic _ -> ()
1449 | Regexp r -> assign_name_regexp r
1450 | Pair (t1,t2) -> assign_name t1; assign_name t2
1451 | Xml (tag,t2,t3) ->
1452 (match tag with `Type t -> assign_name t | _ -> ());
1453 assign_name t2;
1454 assign_name t3
1455 | Record (r,_,_) ->
1456 List.iter (fun (_,(_,t)) -> assign_name t) (LabelMap.get r)
1457 | Arrows (p,n) ->
1458 List.iter (fun (t1,t2) -> assign_name t1; assign_name t2) p;
1459 List.iter (fun (t1,t2) -> assign_name t1; assign_name t2) n
1460 and assign_name_regexp = function
1461 | Pretty.Epsilon | Pretty.Empty -> ()
1462 | Pretty.Alt (r1,r2)
1463 | Pretty.Seq (r1,r2) -> assign_name_regexp r1; assign_name_regexp r2
1464 | Pretty.Star r | Pretty.Plus r -> assign_name_regexp r
1465 | Pretty.Trans t -> assign_name t
1466
1467 let rec do_print_slot pri ppf s =
1468 match s.state with
1469 | `Named n -> Format.fprintf ppf "%a" U.print n
1470 | _ -> do_print_slot_real pri ppf s.def
1471 and do_print_slot_real pri ppf def =
1472 let rec aux ppf = function
1473 | [] -> Format.fprintf ppf "Empty"
1474 | [ h ] -> do_print ppf h
1475 | h :: t -> Format.fprintf ppf "%a |@ %a" do_print h aux t
1476 in
1477 if (pri >= 2) && (List.length def >= 2)
1478 then Format.fprintf ppf "@[(%a)@]" aux def
1479 else aux ppf def
1480 and do_print ppf = function
1481 | Neg t -> Format.fprintf ppf "Any \\ (@[%a@])" (do_print_slot 0) t
1482 | Name n -> Format.fprintf ppf "%a" U.print n
1483 | Char c -> Chars.V.print ppf c
1484 | Regexp r -> Format.fprintf ppf "@[[ %a ]@]" (do_print_regexp 0) r
1485 | Atomic a -> a ppf
1486 | Pair (t1,t2) ->
1487 Format.fprintf ppf "@[(%a,%a)@]"
1488 (do_print_slot 0) t1
1489 (do_print_slot 0) t2
1490 | Xml (tag,attr,t) ->
1491 Format.fprintf ppf "<%a%a>%a"
1492 do_print_tag tag
1493 do_print_attr attr
1494 (do_print_slot 0) t
1495 | Record (r,some,none) ->
1496 if some then Format.fprintf ppf "@[{"
1497 else Format.fprintf ppf "@[{|";
1498 do_print_record ppf r;
1499 if not none then Format.fprintf ppf ";@ ...";
1500 if some then Format.fprintf ppf " }@]"
1501 else Format.fprintf ppf " |}@]"
1502 | Arrows (p,n) ->
1503 (match p with
1504 | [] -> Format.fprintf ppf "Arrow"
1505 | (t,s)::l ->
1506 Format.fprintf ppf "%a" do_print_arrow (t,s);
1507 List.iter
1508 (fun (t,s) ->
1509 Format.fprintf ppf " &@ %a" do_print_arrow (t,s)
1510 ) l);
1511 List.iter
1512 (fun (t,s) ->
1513 Format.fprintf ppf " \\@ %a" do_print_arrow (t,s)
1514 ) n
1515 and do_print_arrow ppf (t,s) =
1516 Format.fprintf ppf "%a -> %a"
1517 (do_print_slot 0) t
1518 (do_print_slot 0) s
1519 and do_print_tag ppf = function
1520 | `Tag s -> s ppf
1521 | `Type t -> Format.fprintf ppf "(%a)" (do_print_slot 0) t
1522 and do_print_attr ppf = function
1523 | { state = `Marked|`Expand;
1524 def = [ Record (r,true,true) ] } -> do_print_record ppf r
1525 | t -> Format.fprintf ppf " %a" (do_print_slot 2) t
1526 and do_print_record ppf r =
1527 let first = ref true in
1528 List.iter
1529 (fun (l,(o,t)) ->
1530 let sep = if !first then (first := false; "") else ";" in
1531 let opt = if o then "?" else "" in
1532 Format.fprintf ppf "%s@ @[%a =%s@] %a" sep
1533 Label.print (LabelPool.value l) opt (do_print_slot 0) t
1534 ) (LabelMap.get r)
1535 and do_print_regexp pri ppf = function
1536 | Pretty.Empty -> Format.fprintf ppf "Empty" (*assert false *)
1537 | Pretty.Epsilon -> ()
1538 | Pretty.Seq (Pretty.Trans { def = [ Char _ ] }, _) as r->
1539 (match extract_string [] r with
1540 | s, None ->
1541 Format.fprintf ppf "'";
1542 List.iter (Chars.V.print_in_string ppf) s;
1543 Format.fprintf ppf "'"
1544 | s, Some r ->
1545 if pri >= 3 then Format.fprintf ppf "@[(";
1546 Format.fprintf ppf "'";
1547 List.iter (Chars.V.print_in_string ppf) s;
1548 Format.fprintf ppf "' %a" (do_print_regexp 2) r;
1549 if pri >= 3 then Format.fprintf ppf ")@]")
1550 | Pretty.Seq (r1,r2) ->
1551 if pri >= 3 then Format.fprintf ppf "@[(";
1552 Format.fprintf ppf "%a@ %a"
1553 (do_print_regexp 2) r1
1554 (do_print_regexp 2) r2;
1555 if pri >= 3 then Format.fprintf ppf ")@]"
1556 | Pretty.Alt (r,Pretty.Epsilon) | Pretty.Alt (Pretty.Epsilon,r) ->
1557 Format.fprintf ppf "@[%a@]?" (do_print_regexp 3) r
1558 | Pretty.Alt (r1,r2) ->
1559 if pri >= 2 then Format.fprintf ppf "@[(";
1560 Format.fprintf ppf "%a |@ %a"
1561 (do_print_regexp 1) r1
1562 (do_print_regexp 1) r2;
1563 if pri >= 2 then Format.fprintf ppf ")@]"
1564 | Pretty.Star r ->
1565 Format.fprintf ppf "@[%a@]*" (do_print_regexp 3) r
1566 | Pretty.Plus r ->
1567 Format.fprintf ppf "@[%a@]+" (do_print_regexp 3) r
1568 | Pretty.Trans t ->
1569 do_print_slot pri ppf t
1570 and extract_string accu = function
1571 | Pretty.Seq (Pretty.Trans { def = [ Char c ] }, r) ->
1572 extract_string (c :: accu) r
1573 | Pretty.Trans { def = [ Char c ] } ->
1574 (List.rev (c :: accu), None)
1575 | r -> (List.rev accu,Some r)
1576
1577
1578 let get_name = function
1579 | { state = `Named n } -> n
1580 | _ -> assert false
1581
1582 let print ppf d =
1583 let t = prepare d in
1584 assign_name t;
1585 Format.fprintf ppf "@[@[%a@]" (do_print_slot 0) t;
1586 (match List.rev !to_print with
1587 | [] -> ()
1588 | s::t ->
1589 Format.fprintf ppf
1590 " where@ @[<v>%a = @[%a@]" U.print (get_name s)
1591 (do_print_slot_real 0) s.def;
1592 List.iter
1593 (fun s ->
1594 Format.fprintf ppf " and@ %a = @[%a@]" U.print
1595 (get_name s) (do_print_slot_real 0) s.def)
1596 t;
1597 Format.fprintf ppf "@]"
1598 );
1599 Format.fprintf ppf "@]";
1600 count_name := 0;
1601 to_print := [];
1602 DescrHash.clear memo
1603 end
1604
1605 module Positive =
1606 struct
1607 type rhs = [ `Type of descr | `Cup of v list | `Times of v * v | `Xml of v * v ]
1608 and v = { mutable def : rhs; mutable node : node option }
1609
1610
1611 let rec make_descr seen v =
1612 if List.memq v seen then empty
1613 else
1614 let seen = v :: seen in
1615 match v.def with
1616 | `Type d -> d
1617 | `Cup vl ->
1618 List.fold_left (fun acc v -> cup acc (make_descr seen v)) empty vl
1619 | `Times (v1,v2) -> times (make_node v1) (make_node v2)
1620 | `Xml (v1,v2) -> xml (make_node v1) (make_node v2)
1621
1622 and make_node v =
1623 match v.node with
1624 | Some n -> n
1625 | None ->
1626 let n = make () in
1627 v.node <- Some n;
1628 let d = make_descr [] v in
1629 define n d;
1630 n
1631
1632 let forward () = { def = `Cup []; node = None }
1633 let def v d = v.def <- d
1634 let cons d = let v = forward () in def v d; v
1635 let ty d = cons (`Type d)
1636 let cup vl = cons (`Cup vl)
1637 let times d1 d2 = cons (`Times (d1,d2))
1638 let xml d1 d2 = cons (`Xml (d1,d2))
1639 let define v1 v2 = def v1 (`Cup [v2])
1640
1641 let solve v = internalize (make_node v)
1642 end
1643
1644
1645 let memo_normalize = ref DescrMap.empty
1646
1647
1648 let rec rec_normalize d =
1649 try DescrMap.find d !memo_normalize
1650 with Not_found ->
1651 let n = make () in
1652 memo_normalize := DescrMap.add d n !memo_normalize;
1653 let times =
1654 List.fold_left
1655 (fun accu (d1,d2) -> BoolPair.cup accu (BoolPair.atom (rec_normalize d1, rec_normalize d2)))
1656 BoolPair.empty (Product.normal d)
1657 in
1658 let xml =
1659 List.fold_left
1660 (fun accu (d1,d2) -> BoolPair.cup accu (BoolPair.atom (rec_normalize d1, rec_normalize d2)))
1661 BoolPair.empty (Product.normal ~kind:`XML d)
1662 in
1663 let record = d.record
1664 in
1665 define n { d with hash=0; times = times; xml = xml; record = record };
1666 n
1667
1668 let normalize n =
1669 descr (internalize (rec_normalize n))
1670
1671 module Arrow =
1672 struct
1673 let check_simple left (s1,s2) =
1674 let rec aux accu1 accu2 = function
1675 | (t1,t2)::left ->
1676 let accu1' = diff_t accu1 t1 in
1677 if non_empty accu1' then aux accu1 accu2 left;
1678 let accu2' = cap_t accu2 t2 in
1679 if non_empty accu2' then aux accu1 accu2 left
1680 | [] -> raise NotEmpty
1681 in
1682 let accu1 = descr s1 in
1683 (is_empty accu1) ||
1684 (try aux accu1 (diff any (descr s2)) left; true with NotEmpty -> false)
1685
1686 let check_line_non_empty (left,right) =
1687 not (List.exists (check_simple left) right)
1688
1689 let sample t =
1690 let (left,right) = List.find check_line_non_empty (BoolPair.get t.arrow) in
1691 List.fold_left (fun accu (t,s) -> cap accu (arrow t s))
1692 { empty with hash=0; arrow = any.arrow } left
1693
1694
1695 let check_strenghten t s =
1696 (*
1697 let left = match (BoolPair.get t.arrow) with [ (p,[]) ] -> p | _ -> assert false in
1698 let rec aux = function
1699 | [] -> raise Not_found
1700 | (p,n) :: rem ->
1701 if (List.for_all (fun (a,b) -> check_simple left a b) p) &&
1702 (List.for_all (fun (a,b) -> not (check_simple left a b)) n) then
1703 { empty with hash=0; arrow = Obj.magic [ (SortedList.cup left p, n) ] } (* rework this ! *)
1704 else aux rem
1705 in
1706 aux (BoolPair.get s.arrow)
1707 *)
1708 if subtype t s then t else raise Not_found
1709
1710 let check_simple_iface left s1 s2 =
1711 let rec aux accu1 accu2 = function
1712 | (t1,t2)::left ->
1713 let accu1' = diff accu1 t1 in
1714 if non_empty accu1' then aux accu1 accu2 left;
1715 let accu2' = cap accu2 t2 in
1716 if non_empty accu2' then aux accu1 accu2 left
1717 | [] -> raise NotEmpty
1718 in
1719 let accu1 = descr s1 in
1720 (is_empty accu1) ||
1721 (try aux accu1 (diff any (descr s2)) left; true with NotEmpty -> false)
1722
1723 let check_iface iface s =
1724 let rec aux = function
1725 | [] -> false
1726 | (p,n) :: rem ->
1727 ((List.for_all (fun (a,b) -> check_simple_iface iface a b) p) &&
1728 (List.for_all (fun (a,b) -> not (check_simple_iface iface a b)) n))
1729 || (aux rem)
1730 in
1731 aux (BoolPair.get s.arrow)
1732
1733 type t = descr * (descr * descr) list list
1734
1735 let get t =
1736 List.fold_left
1737 (fun ((dom,arr) as accu) (left,right) ->
1738 if check_line_non_empty (left,right)
1739 then
1740 let left = List.map (fun (t,s) -> (descr t, descr s)) left in
1741 let d = List.fold_left (fun d (t,_) -> cup d t) empty left in
1742 (cap dom d, left :: arr)
1743 else accu
1744 )
1745 (any, [])
1746 (BoolPair.get t.arrow)
1747
1748 let domain (dom,_) = dom
1749
1750 let apply_simple t result left =
1751 let rec aux result accu1 accu2 = function
1752 | (t1,s1)::left ->
1753 let result =
1754 let accu1 = diff accu1 t1 in
1755 if non_empty accu1 then aux result accu1 accu2 left
1756 else result in
1757 let result =
1758 let accu2 = cap accu2 s1 in
1759 aux result accu1 accu2 left in
1760 result
1761 | [] ->
1762 if subtype accu2 result
1763 then result
1764 else cup result accu2
1765 in
1766 aux result t any left
1767
1768 let apply (_,arr) t =
1769 List.fold_left (apply_simple t) empty arr
1770
1771 let need_arg (dom, arr) =
1772 List.exists (function [_] -> false | _ -> true) arr
1773
1774 let apply_noarg (_,arr) =
1775 List.fold_left
1776 (fun accu ->
1777 function
1778 | [(t,s)] -> cup accu s
1779 | _ -> assert false
1780 )
1781 empty arr
1782
1783 let any = { empty with hash=0; arrow = any.arrow }
1784 let is_empty (_,arr) = arr == []
1785 end
1786
1787
1788 module Int = struct
1789 let has_int d i = Intervals.contains i d.ints
1790 let get d = d.ints
1791 let any = { empty with hash=0; ints = any.ints }
1792 end
1793
1794 module Atom = struct
1795 let has_atom d a = Atoms.contains a d.atoms
1796 let get d = d.atoms
1797 let any = { empty with hash=0; atoms = any.atoms }
1798 end
1799
1800 module Char = struct
1801 let has_char d c = Chars.contains c d.chars
1802 let is_empty d = Chars.is_empty d.chars
1803 let get d = d.chars
1804 let any = { empty with hash=0; chars = any.chars }
1805 end
1806
1807 (* <helpers> *)
1808
1809 let choice_of_list = List.fold_left cup empty
1810
1811 let xml' tag attrs cont = xml (cons tag) (cons (times (cons attrs) (cons cont)))
1812
1813 let rec_of_list ?(opened=true) l =
1814 let map =
1815 List.fold_left
1816 (fun acc (qname, typ) ->
1817 LabelMap.union_disj acc
1818 (LabelMap.singleton (LabelPool.mk qname) (cons typ)))
1819 LabelMap.empty
1820 l
1821 in
1822 record' (opened, map)
1823
1824 let rec_of_list' ?(opened=true) l =
1825 let map =
1826 List.fold_left
1827 (fun acc (opt, qname, typ) ->
1828 LabelMap.union_disj acc
1829 (LabelMap.singleton (LabelPool.mk qname)
1830 (if opt then cons (Record.or_absent typ) else (cons typ))))
1831 LabelMap.empty
1832 l
1833 in
1834 record' (opened, map)
1835
1836 let empty_closed_record = rec_of_list ~opened:false []
1837 let empty_opened_record = rec_of_list ~opened:true []
1838
1839 (* </helpers> *)
1840

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