patterns.ml 53.7 KB
Newer Older
1
2
type capture = string
type fv = capture SortedList.t
3

4
5
exception Error of string

6

7
(* Syntactic algebra *)
8
(* Constraint: any node except Constr has fv<>[] ... *)
9
type d =
10
  | Constr of Types.descr
11
  | Cup of descr * descr
12
  | Cap of descr * descr
13
  | Times of node * node
14
  | Xml of node * node
15
16
17
  | Record of Types.label * node
  | Capture of capture
  | Constant of capture * Types.const
18
19
and node = {
  id : int;
20
21
  mutable descr : descr option;
  accept : Types.node;
22
  fv : fv
23
} and descr = Types.descr * fv * d
24

25
26
27
28
let id x = x.id
let descr x = match x.descr with Some d -> d | None -> failwith "Patterns.descr"
let fv x = x.fv
let accept x = Types.internalize x.accept
29
30
31

let printed = ref []
let to_print = ref []
32
33
let rec print ppf (a,_,d) = 
(*  Format.fprintf ppf "[%a]" Types.Print.print_descr a; *)
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  match d with
    | Constr t -> Types.Print.print_descr ppf t
    | Cup (p1,p2) -> Format.fprintf ppf "(%a | %a)" print p1 print p2
    | Cap (p1,p2) -> Format.fprintf ppf "(%a & %a)" print p1 print p2
    | Times (n1,n2) -> 
	Format.fprintf ppf "(P%i,P%i)" n1.id n2.id;
	to_print := n1 :: n2 :: !to_print
    | Xml (n1,n2) -> 
	Format.fprintf ppf "XML(P%i,P%i)" n1.id n2.id;
	to_print := n1 :: n2 :: !to_print
    | Record (l,n) -> 
	Format.fprintf ppf "{ %s =  P%i }" (Types.LabelPool.value l) n.id;
	to_print := n :: !to_print
    | Capture x ->
	Format.fprintf ppf "%s" x
    | Constant (x,c) ->
	Format.fprintf ppf "(%s := %a)" x Types.Print.print_const c

52
53
54
55
56
57
58
59
60
61
62
63
64
let dump_print ppf =
  while !to_print <> [] do
    let p = List.hd !to_print in
    to_print := List.tl !to_print;
    if not (List.mem p.id !printed) then
      ( printed := p.id :: !printed;
	Format.fprintf ppf "P%i:=%a\n" p.id print (descr p)
      )
  done

let print ppf d =
  Format.fprintf ppf "%a@\n" print d;
  dump_print ppf
65
66


67
68
69
70
71
let counter = State.ref "Patterns.counter" 0

let make fv =
  incr counter;
  { id = !counter; descr = None; accept = Types.make (); fv = fv }
72
73

let define x ((accept,fv,_) as d) =
74
  assert (x.fv = fv);
75
  Types.define x.accept accept;
76
  x.descr <- Some d
77

78
let constr x = (x,[],Constr x)
79
let cup ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
80
81
82
83
84
85
86
87
88
89
  if fv1 <> fv2 then (
    let x = match SortedList.diff fv1 fv2 with
      | x::_ -> x
      | [] -> match SortedList.diff fv2 fv1 with x::_ -> x | _ -> assert false
    in
    raise 
      (Error 
	 ("The capture variable " ^ x ^ 
	  " should appear on both side of this | pattern"))
  );
90
  (Types.cup acc1 acc2, SortedList.cup fv1 fv2, Cup (x1,x2))
91
let cap ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
92
93
94
95
96
97
98
99
100
  if not (SortedList.disjoint fv1 fv2) then (
    match SortedList.cap fv1 fv2 with
      | x::_ -> 
	  raise 
	  (Error 
	     ("The capture variable " ^ x ^ 
	      " cannot appear on both side of this & pattern"))
      | _ -> assert false
  );
101
  (Types.cap acc1 acc2, SortedList.cup fv1 fv2, Cap (x1,x2))
102
let times x y =
103
  (Types.times x.accept y.accept, SortedList.cup x.fv y.fv, Times (x,y))
104
105
let xml x y =
  (Types.xml x.accept y.accept, SortedList.cup x.fv y.fv, Xml (x,y))
106
let record l x = 
107
108
109
  (Types.record l false x.accept, x.fv, Record (l,x))
let capture x = (Types.any, [x], Capture x)
let constant x c = (Types.any, [x], Constant (x,c))
110

111

112

113
114
115
116

(* Static semantics *)

let cup_res v1 v2 = Types.Positive.cup [v1;v2]
117
let empty_res fv = List.map (fun v -> (v, Types.Positive.ty Types.empty)) fv
118
119
120
let times_res v1 v2 = Types.Positive.times v1 v2

module MemoFilter = Map.Make 
121
  (struct type t = Types.descr * node let compare = compare end)
122
123
124

let memo_filter = ref MemoFilter.empty

125
let rec filter_descr t (_,fv,d) : (capture, Types.Positive.v) SortedMap.t =
126
(* TODO: avoid is_empty t when t is not changing (Cap) *)
127
128
129
130
  if Types.is_empty t 
  then empty_res fv
  else
    match d with
131
      | Constr _ -> []
132
      | Cup ((a,_,_) as d1,d2) ->
133
	  SortedMap.union cup_res
134
135
	    (filter_descr (Types.cap t a) d1)
	    (filter_descr (Types.diff t a) d2)
136
      | Cap (d1,d2) ->
137
	  SortedMap.union cup_res (filter_descr t d1) (filter_descr t d2)
138
139
      | Times (p1,p2) -> filter_prod fv p1 p2 t
      | Xml (p1,p2) -> filter_prod ~kind:`XML fv p1 p2 t
140
141
142
      | Record (l,p) ->
	  filter_node (Types.Record.project t l) p
      | Capture c ->
143
	  [(c, Types.Positive.ty t)]
144
      | Constant (c, cst) ->
145
146
	  [(c, Types.Positive.ty (Types.constant cst))]

147
148
149
150
151
152
153
154
155
156
157
158
and filter_prod ?kind fv p1 p2 t =
  List.fold_left 
    (fun accu (d1,d2) ->
       let term = 
	 SortedMap.union times_res (filter_node d1 p1) (filter_node d2 p2)
       in
       SortedMap.union cup_res accu term
    )
    (empty_res fv)
    (Types.Product.normal ?kind t)


159
and filter_node t p : (capture, Types.Positive.v) SortedMap.t =
160
161
  try MemoFilter.find (t,p) !memo_filter
  with Not_found ->
162
163
    let (_,fv,_) as d = descr p in
    let res = List.map (fun v -> (v,Types.Positive.forward ())) fv in
164
165
    memo_filter := MemoFilter.add (t,p) res !memo_filter;
    let r = filter_descr t (descr p) in
166
    List.iter2 (fun (_,r) (_,v) -> Types.Positive.define v r) r res;
167
168
169
170
171
    r

let filter t p =
  let r = filter_node t p in
  memo_filter :=  MemoFilter.empty;
172
  List.map (fun (c,v) -> (c,Types.Positive.solve v)) r
173

174

175

176
177
(* Returns a pattern q equivalent to p when applied to a
   value of type t *)
178
(*
179
180
module Compiler = 
struct
181
182
  type dispatcher = {
    did      : int;
183
    nb_codes : int;
184
185
    results  : res;
    t        : Types.descr;
186
    pats     : (node option * Types.descr) SortedList.t;
187
    mutable actions  : actions option;
188
  }
189
  and bind = (capture, int) SortedMap.t
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  and res = [ `Return of Types.descr * int 
	    | `Fail 
	    | `Branch of (bind * res * res) ]
  and 'a dispatch = dispatcher * 'a array
  and actions = { 
    act_basic: basic_actions;
    act_prod : prod_actions 
  }
  and prod_actions = (int * prod_src list) dispatch dispatch
  and basic_actions = (Types.descr * (int * basic_src list)) SortedList.t
  and prod_src = [ `Capture |  `Const of Types.const 
		 | `Combine of int * int | `Left of int | `Right of int ]
  and basic_src = [ `Capture |  `Const of Types.const ]

204

205
206
207

  module DispMap = Map.Make(
    struct
208
      type t = Types.descr * (node option * Types.descr) SortedList.t
209
210
211
212
213
214
      let compare = compare
    end
  )
    
  let dispatchers = ref DispMap.empty
  let nb_disp = ref 0		  
215
216
217
218

  let rec make_res codes pos t l =
    if Types.is_empty t then `Fail
    else match l with
219
220
      | [] -> 
	  incr codes; `Return (t, !codes - 1)
221
222
      | (p,restr)::rem ->
	  let (pos,bind,a) = match p with
223
	    | Some p -> 
224
225
		let pos = ref pos in
		let bind = List.map (fun v -> incr pos; (v,!pos-1)) (fv p) in
226
227
		(!pos,bind,Types.cap restr (Types.descr (accept p)))
	    | None -> (pos,[],restr)
228
229
	  in
(*	  assert (Types.subtype restr t);*)
230
231
	  let yes = make_res codes pos (Types.cap t a) rem
	  and no = make_res codes pos (Types.diff t a) rem in
232
233
	  `Branch (bind,yes,no)
	    
234
    
235
  let make_dispatcher t pats =
236
    try DispMap.find (t,pats) !dispatchers
237
238
    with Not_found ->
      incr nb_disp;
239
240
      let nbc = ref 0 in
      let res = make_res nbc 0 t pats in
241
242
243
244
245
246
      let d = { did = !nb_disp; 
		pats = pats;
		t = t;
		results = res; 
		nb_codes = !nbc; 
		actions = None } in
247
      dispatchers := DispMap.add (t,pats) d !dispatchers;
248
249
      d

250
251
252
253
254
255
256
  let rec find_code accu = function
    | (`Return (_,c),[]) -> 
	(c,List.rev accu)
    | (`Branch (_,_,no),None::rem) -> 
	find_code accu (no,rem)
    | (`Branch (_,yes,_),Some x::rem) -> 
	find_code (List.rev_append x accu) (yes,rem)
257
    | (`Fail,_) -> assert false
258
259
260
    | _ -> assert false 


261
262
263
264
265
266
267
268
269
270
271
272
  let dispatcher t (args : (node option * Types.descr * bind option ref) 
		      list) f =
(*    let args = 
      List.map
	(function 
	   | (`Pat p, s, r) -> (`Pat p, Types.cap t s, r)
	   | (`Typ c, s, r) -> 
	       let s = Types.cap t s in
	       (`Typ (Types.cap c s), s, r)) args in *)
    let args = List.map (fun (p,restr,flag) -> ((p,restr),[flag])) args in
    let args = SortedMap.from_list (@) args in
    let pats = List.map fst args in
273
274
275
276
    let d = make_dispatcher t pats in
    let res = Array.create d.nb_codes (Obj.magic 0) in
    let rec aux = function
      | (`Fail,_) -> ()
277
      | (`Return (t,c), []) -> res.(c) <- f t
278
279
280
      | (`Branch (bind,yes,no), (_,fls)::rem) ->
	  List.iter (fun r -> r := Some bind) fls; aux (yes,rem);
	  List.iter (fun r -> r := None) fls; aux (no,rem)
281
282
283
284
285
286
287
      | _ -> assert false
    in
    aux (d.results,args);
    (d,res)

	   

288
289
290
  let sort_list l =
    Array.of_list (SortedList.from_list l)

291
292
293
  type 'a pat =
    | One
    | Zero
294
295
    | Capt of Types.descr * capture
    | Const of Types.descr * capture * Types.const
296
297
298
299
300
301
302
    | Alt of 'a pat * 'a pat
    | And of 'a pat * 'a pat
    | Atom of 'a

  let rec print f ppf = function
    | One -> Format.fprintf ppf "One"
    | Zero -> Format.fprintf ppf "Zero"
303
304
305
306
307
    | Capt (t,x) -> 
	Format.fprintf ppf "[%a]%s" Types.Print.print_descr t x
    | Const (t,x,c) -> 
	Format.fprintf ppf "[%a](%s:=%a)" Types.Print.print_descr t 
	x Types.Print.print_const c
308
309
310
311
312
313
314
315
316
317
318
319
320
    | Alt (p1,p2) -> Format.fprintf ppf "(%a | %a)" (print f) p1 (print f) p2
    | And (p1,p2) -> Format.fprintf ppf "(%a & %a)" (print f) p1 (print f) p2
    | Atom a -> Format.fprintf ppf "%a" f a

  let alt = function
    | (Zero,p) | (p,Zero) -> p
    | (p1,p2) -> Alt (p1,p2)

  let and_ = function
    | (Zero,_) | (_,Zero) -> Zero
    | (One,p) | (p,One) -> p
    | (p1,p2) -> And (p1,p2)

321
322
323
324
(*
debug compile2 (Int,Int)|(Char,Char) (Int,x)|(x,Char);;
*)

325
(*
326
327
328
  let atom s a p =
    if Types.is_empty (Types.cap s a) then Zero else
      Atom (s, p)
329
*)
330
331
332
333

  let rec map f = function
    | One -> One
    | Zero -> Zero
334
335
    | Capt (t,x) -> Capt (t,x)
    | Const (t,x,c) -> Const (t,x,c)
336
337
338
    | Alt (p1,p2) -> alt (map f p1, map f p2)
    | And (p1,p2) -> and_ (map f p1, map f p2)
    | Atom a -> f a
339
340

  let rec get f (a,_,d) s =
341
342
    let s = Types.cap s a in
    if Types.is_empty s then Zero 
343
344
345
346
347
348
349
    else match d with
      | Cup ((a1,_,_) as d1,d2) ->
	  let p1 = get f d1 s in
	  let p2 = get f d2 (Types.diff s a1) in
	  alt (p1,p2)
      | Cap ((a1,_,_) as d1,d2) ->
	  let p1 = get f d1 s in
350
	  let p2 = get f d2 s in
351
352
	  and_ (p1,p2)
      | Capture x ->
353
354
	  Capt (s,x)
      | Constant (s,x,c) ->
355
356
357
	  Const (x,c)
      | d -> f d s

358
359
360
361
  let rec get_final f = function
    | Atom x -> f x
    | One -> Some []
    | Zero -> None
362
363
    | Capt (s,x) -> Some [x, `Capture]
    | Const (s,x,c) -> Some [x, `Const c]
364
365
366
367
368
369
370
371
372
373
374
375
376
    | Alt (p1,p2) ->
	(match get_final f p1 with
	  | Some _ as x -> x
	  | None -> get_final f p2)
    | And (p1,p2) ->
	(match get_final f p1 with
	  | Some x -> 
	      (match get_final f p2 with
		 | Some y -> Some (SortedMap.union_disj x y)
		 | None -> None)
	  | None -> None)

  let get_final f p =
377
    match get_final f t p with
378
379
380
381
382
383
384
385
386
387
388
389
      | None -> None
      | Some l -> Some (List.map snd l)


  let map_list f =
    List.map (map f)

  let pi1 d = Types.Product.pi1 (Types.Product.get d)
  let pi2 d d1 = Types.Product.pi2 (Types.Product.restrict_1
				      (Types.Product.get d) d1)

  let prepare_prod' =
390
391
392
    get (fun d r ->
	   match d with
	     | Times (n1,n2) ->
393
394
395
		 Atom (Some (n1,n2),r)
	     | Constr _ ->
		 Atom (None,r)
396
397
398
	     | _ -> Zero
	)

399
400
  let prepare_prod (p,restr) =
    match p with
401
402
      | Some p -> prepare_prod' (descr p) restr
      | None -> Atom (None, restr)
403
404
	  (* TODO: special case here ... restr<=t...*)

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  let map_prod1 collect (p,r) =
    let (n1,n2) = match p with 
      | Some (n1,n2) -> Some n1, Some n2 
      | None -> None, None in
    let l =
      List.map 
	(fun (r1,r2) ->
	   let fl = ref None in
	   collect := (n1,r1,fl) :: !collect;
	   (fl,n2,r2)
	) (Types.Product.normal r) in
    Atom l


  let map_prod2 collect l =
    let l = 
      List.fold_left
	(fun accu (fl1,n2,r2) ->
	   match !fl1 with
	     | None -> accu
	     | Some bind ->
		 let fl2 = ref None in
		 collect := (n2, r2, fl2) :: !collect;
		 (bind,fl2)::accu
	) [] l in
    Atom l

  let rec prod_final = function
    | [] -> None
    | (bind1,{contents = Some bind2})::_ ->
	Some (SortedMap.combine	
436
437
		(fun x -> `Left x) (fun x -> `Right x)
		(fun x y -> `Combine (x,y))
438
439
440
441
442
443
		bind1 bind2)
    | _::rem -> prod_final rem


  let dispatch_prod (res:res) t (pats:(node option * Types.descr) list) : 
    prod_actions =
444
    let pats = List.map prepare_prod pats in
445
    let lefts = ref [] in
446
447
448
449
    let pats =  map_list (map_prod1 lefts) pats in
    dispatcher (pi1 t) !lefts
      (fun t1 ->
	 let rights = ref [] in
450
	 let pats = map_list (map_prod2 rights) pats in
451
452
	 dispatcher (pi2 t t1) !rights
	   (fun t2 ->
453
	    let pats = List.map (get_final (prod_final)) pats in
454
455
456
457
458
459
460
461
462
463
464
465
	    find_code [] (res,pats)
	   )
      )

  let any_basic = Types.neg (List.fold_left Types.cup Types.empty
			       [Types.Product.any_xml;
				Types.Product.any;
				Types.Record.any])

  let prepare_basic' =
    get (fun d r ->
	   match d with
466
	     | Constr _ -> Atom r
467
468
469
470
	     | _ -> Zero)

  let prepare_basic (p,restr) =
    match p with
471
472
      | Some p -> prepare_basic' (descr p) restr
      | None -> Atom restr
473
474
475
476
477
478
479
480
481
482
483
484
485
      
  let basic_final t =
    get_final (
      fun s ->
	if Types.subtype t s then Some []
	else (assert (Types.is_empty (Types.cap t s)); None)
    )

  let dispatch_basic res t pats : basic_actions =
    let types = ref [] in
    let rec aux = function
      | `Fail -> ()
      | `Branch (bind,yes,no) -> aux yes; aux no
486
487
488
      | `Return (t,c) -> 
(*	  Format.fprintf Format.std_formatter "<<<%a -> %i>>>@\n" 
	    Types.Print.print_descr t c; *)
489
490
491
492
	  let t = Types.cap t any_basic in
	  if not (Types.is_empty t) then types := t :: !types in
    aux res;
    let pats = List.map prepare_basic pats in
493
494
495
496
497
498
    Format.fprintf Format.std_formatter "BASIC:%i@\n" (List.length !types);
    List.iter (fun p -> 
		 Format.fprintf Format.std_formatter
		   "==> %a@\n"
		   (print Types.Print.print_descr) p
	      ) pats;
499
500
501
    List.map
      (fun t ->
	 let pats = List.map (basic_final t) pats in
502
503
504
505
506
507
508
509
510
	 Format.fprintf Format.std_formatter "BASIC:";
	 List.iter (function 
		      | Some _ -> 
			  Format.fprintf Format.std_formatter "YES"
		      | None -> 
			  Format.fprintf Format.std_formatter "NO "
		   ) pats;
	 Format.fprintf Format.std_formatter "@\n";
	 
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
	 (t, find_code [] (res,pats))
      ) !types


  let get_actions disp =
    match disp.actions with
      | Some a -> a
      | None ->
	  let a = {
	    act_basic = dispatch_basic disp.results disp.t disp.pats;
	    act_prod = dispatch_prod disp.results disp.t disp.pats
	  } in
	  disp.actions <- Some a;
	  a

  let to_print = ref ([] : dispatcher list)
  let printed = ref ([] : dispatcher list)


  let print_act_basic ppf b =
    List.iter 
      (fun (d,(code,bind)) ->
	 Format.fprintf ppf "| %a -> %i( " 
	 Types.Print.print_descr d
	 code;
	 List.iter 
	   (function
	      | `Capture -> 
		  Format.fprintf ppf "v "
	      | `Const c -> 
		  Format.fprintf ppf "%a " Types.Print.print_const c)
	   bind;
	 Format.fprintf ppf ")@\n" 
      ) b

  let print_act_prod ppf (disp1,b1) =
    Format.fprintf ppf "| (v1,v2) -> match v1 with disp%i@\n" disp1.did;
    to_print := disp1 :: !to_print;
    for i = 0 to Array.length b1 - 1 do
     let (disp2,b2) = b1.(i) in
     to_print := disp2 :: !to_print;
      Format.fprintf ppf " | %i(l) -> match v2 with disp%i@\n" i disp2.did;
      for j = 0 to Array.length b2 - 1 do
	let (code,bind) = b2.(j) in
	Format.fprintf ppf "  | %i(r) -> %i(" j code;
	List.iter 
	  (function
	     | `Capture -> 
		 Format.fprintf ppf "v "
	     | `Const c -> 
		 Format.fprintf ppf "%a " Types.Print.print_const c
	     | `Left x ->
		 Format.fprintf ppf "l%i " x
	     | `Right x ->
		 Format.fprintf ppf "r%i " x
	     | `Combine (x,y) ->
		 Format.fprintf ppf "(l%i,r%i) " x y
	  )
	  bind;
	Format.fprintf ppf ")@\n" 
      done;
    done
		 
  let rec print_disp ppf disp =
    Format.fprintf ppf "Dispatcher [%i]: 0..%i@\n" disp.did (disp.nb_codes - 1);
    let a = get_actions disp in
    print_act_basic ppf a.act_basic;
    print_act_prod ppf a.act_prod;

    let rec loop () = 
      match !to_print with
	| [] -> ()
	| d::q -> 
	    to_print := q;
	    if List.memq d !printed then loop ()
	    else (printed := d :: !printed; print_disp ppf d)
587
    in
588
589
    loop ()

590

591
(*
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
let rec collect typ f (a,_,d) s =
  if Types.is_empty (Types.cap s a) then () else
  match d with
    | Constr t -> if not (Types.subtype s a) then typ s (Types.cap s t)
    | Cup ((a1,_,_) as d1,d2) -> 
	collect typ f d1 s; collect typ f d2 (Types.diff s a1)
    | Cap ((a1,_,_) as d1,d2) ->
	collect typ f d1 s;
	collect typ f d2 (Types.cap s a1)
    | Capture _ | Constant (_,_) -> ()
    | d -> f s d

let get_prod =
  get (function Times (n1,n2) -> Some n1 | _ -> None)
let get_record =
  get (function Record (l,n) -> Some (l,n) | _ -> None)

let print_prod =
  print (fun ppf p1 ->
	   Format.fprintf ppf "(P%i)" p1.id
	)
let print_record =
  print (fun ppf (l,p) ->
	   Format.fprintf ppf "{ %s = P%i }" (Types.LabelPool.value l) p.id
	)

let demo ppf p t =
  collect 
    (fun w t -> 
       Format.fprintf ppf "TYP1:%a // %a@\n"
         Types.Print.print_descr t
         Types.Print.print_descr w;
       let n = Types.Product.normal t in
       let pi1 = Types.Product.pi1 (Types.Product.get w) in
       List.iter (fun (d1,d2) ->
		    Format.fprintf ppf "=> %a // %a@\n"
		    Types.Print.print_descr d1
		    Types.Print.print_descr pi1
		 ) n
    )
    (fun w -> function
       | Times (n1,n2) -> 
	   let pi1 = Types.Product.pi1 (Types.Product.get w) in
	   Format.fprintf ppf "PAT1:%i // %a@\n" n1.id 
	     Types.Print.print_descr pi1; 
	   to_print := n1 :: !to_print
       | _ -> ()) p t

640
*)
641
642
643
644
645
646
647
end

let demo ppf p t =
(*
  Compiler.demo ppf p t;
  dump_print ppf
*)
648
(*
649
650
  Format.fprintf ppf "PROD:%a@\n" Compiler.print_prod (Compiler.get_prod p (Types.cap Types.Product.any t));
  Format.fprintf ppf "REC :%a@\n" Compiler.print_record (Compiler.get_record p (Types.cap Types.Record.any t))
651
652
*)
  ()
653

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674

let rec restrict ((a,fv,d) as p) t =
  (* TODO OPT: Don't call cup,cap .... *)
  match d with
    | Constr s ->
	constr (Types.cap t a)
	(* Could return any type  (t&s)|u  with u&t=0 *)
    | Cup (((a1,_,_) as p1),((a2,_,_) as p2)) ->
	let p1 = 
	  if Types.is_empty (Types.cap t a1) then None 
	  else Some (restrict p1 t) in
	let p2 =
	  let t' = Types.diff t a1 in
	  if Types.is_empty (Types.cap t' a2) then None 
	  else Some (restrict p2 t') in
	(match (p1,p2) with
	   | Some p1, Some p2 -> cup p1 p2
	   | Some p1, None -> p1
	   | None, Some p2 -> p2
	   | _ -> assert false)
    | Cap (p1,p2) -> cap (restrict p1 t) (restrict p2 t)
675
676
(*    | Capture _ | Constant (_,_) -> p *)
    | _ -> p (* (Types.cap a t, fv, d) *)
677
678
679
680
681
  
let restrict ((a,fv,_) as p) t =
  if Types.is_empty (Types.cap a t) then `Reject
  else if (fv = []) && (Types.subtype t a) then `Accept
  else `Pat (restrict p t)
682
*)
683
684


685
(* Normal forms for patterns and compilation *)
686

687
688
689
690
691
module Normal : sig 
  type 'a sl = 'a SortedList.t
  type ('a,'b) sm = ('a,'b) SortedMap.t

  type source = 
692
693
694
    | SCatch | SConst of Types.const 
    | SLeft | SRight | SRecompose 
    | SField of Types.label 
695
696
697
698
699
700
  type result = (capture, source) sm

  type nnf = node sl * Types.descr
  type 'a nline = (result *  'a) list
  type record =
      [ `Success
701
702
      | `SomeField
      | `NoField
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
      | `Fail
      | `Dispatch of (nnf * record) list
      | `Label of Types.label * (nnf * record) list * record ]
  type t = {
    nfv    : fv;
    ncatchv: fv;
    na     : Types.descr;
    nbasic : Types.descr nline;
    nprod  : (nnf * nnf) nline;
    nxml   : (nnf * nnf) nline;
    nrecord: record nline
  }

  val any_basic: Types.descr
  val normal: Types.descr -> node list -> t
end = 
719
struct
720
721
722
723
724
725
  let any_basic = Types.neg (List.fold_left Types.cup Types.empty
			       [Types.Product.any_xml;
				Types.Product.any;
				Types.Record.any])


726
727
728
729
  type 'a sl = 'a SortedList.t
  type ('a,'b) sm = ('a,'b) SortedMap.t

  type source = 
730
731
732
    | SCatch | SConst of Types.const 
    | SLeft | SRight | SRecompose 
    | SField of Types.label 
733
734
  type result = (capture, source) sm

735
  type 'a line = (result * 'a, Types.descr) sm
736
737
  type nf = {
    v     : fv;
738
    catchv: fv;  (* Variables catching the value *)
739
    a     : Types.descr;
740
741
    basic : unit line;
    prod  : (node sl * node sl) line;
742
743
744
    xml   : (node sl * node sl) line;
    record: ((Types.label, node sl) sm) line;

745
  }
746
747
748

  type nnf = node sl * Types.descr   (* pl,t;   t <= \accept{pl} *)
  type 'a nline = (result *  'a) sl
749
750
  type record =
      [ `Success
751
752
      | `SomeField
      | `NoField
753
      | `Fail
754
755
      | `Dispatch of (nnf * record) list
      | `Label of Types.label * (nnf * record) list * record ]
756
  type t = {
757
    nfv    : fv;
758
    ncatchv: fv;
759
    na     : Types.descr;
760
    nbasic : Types.descr nline;
761
762
    nprod  : (nnf * nnf) nline;
    nxml   : (nnf * nnf) nline;
763
    nrecord: record nline
764
  }
765

766
767
768
769
770
771
772
773
774
775
776
  let rec print_record ppf = function
    | `Success -> Format.fprintf ppf "Success"
    | `SomeField -> Format.fprintf ppf "SomeField"
    | `NoField -> Format.fprintf ppf "NoField"
    | `Fail -> Format.fprintf ppf "Fail"
    | `Dispatch _ -> Format.fprintf ppf "Dispatch"
    | `Label (l,pr,ab) ->
	Format.fprintf ppf "Label(%s@[" (Types.LabelPool.value l);
	List.iter (fun (_,r) -> Format.fprintf ppf ",%a" print_record r) pr;
	Format.fprintf ppf ",%a@])" print_record ab

777
778
  let fus = SortedMap.union_disj
  let slcup = SortedList.cup
779

780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
  let nempty = { nfv = []; ncatchv = []; na = Types.empty;
		 nbasic = []; nprod = []; nxml = []; nrecord = [] }


  let ncup nf1 nf2 = 
    (* assert (Types.is_empty (Types.cap nf1.na nf2.na)); *)
    (* assert (nf1.nfv = nf2.nfv); *)
    { nfv = nf1.nfv;
      ncatchv = SortedList.cap nf1.ncatchv nf2.ncatchv;
      na      = Types.cup nf1.na nf2.na;
      nbasic  = SortedList.cup nf1.nbasic nf2.nbasic;
      nprod   = SortedList.cup nf1.nprod nf2.nprod;
      nxml    = SortedList.cup nf1.nxml nf2.nxml;
      nrecord = SortedList.cup nf1.nrecord nf2.nrecord;
    }

  let double_fold f l1 l2 =
    SortedList.from_list
      (List.fold_left 
	 (fun accu x1 ->
	    List.fold_left
	    (fun accu x2 ->
	       f accu x1 x2
	    )
	    accu l2
	 ) [] l1)
	 
  let ncap nf1 nf2 =
    let prod accu (res1,((pl1,t1),(ql1,s1))) (res2,((pl2,t2),(ql2,s2))) =
      let t = Types.cap t1 t2 in
      if Types.is_empty t then accu else
	let s = Types.cap s1 s2  in
	if Types.is_empty s then accu else
	  (fus res1 res2, ((slcup pl1 pl2,t),(slcup ql1 ql2,s))) :: accu
    in
    let basic accu (res1,t1) (res2,t2) =
      let t = Types.cap t1 t2 in
      if Types.is_empty t then accu else
	(fus res1 res2, t) :: accu
    in
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
    let record accu (res1,rec1) (res2,rec2) =
      let rec aux extra1 rec1 extra2 rec2 = 
	let rec1 =
	  if extra1 then
	    match rec1 with 
	      | `SomeField -> `Success
	      | `NoField -> `Fail
	      | x -> x
	  else rec1
	and rec2 = 
	  if extra2 then
	    match rec2 with 
	      | `SomeField -> `Success
	      | `NoField -> `Fail
	      | x -> x
	  else rec2
	in
	match (rec1,rec2) with
	| `Success, r | r, `Success -> r
	| `Fail, _ | _, `Fail -> `Fail

	| `SomeField, `Label (l, pr, ab) ->
	    (match aux false `SomeField extra2 ab with
	       | `Fail when pr = [] -> `Fail
	       | ab -> `Label (l, pr, ab))
	| `Label (l, pr, ab), `SomeField ->
	    (match aux false `SomeField extra1 ab with
	       | `Fail when pr = [] -> `Fail
	       | ab -> `Label (l, pr, ab))

	| `NoField, `Label (l,pr,ab) ->
	    (match aux false `NoField extra2 ab with 
	       | `Fail -> `Fail 
	       | ab -> `Label (l, [], ab))

	| `Label (l, pr, ab), `NoField ->
	    (match aux false `NoField extra1 ab with 
	       | `Fail -> `Fail 
	       | ab -> `Label (l, [], ab))

	| `SomeField, `NoField | `NoField,`SomeField -> 
	    `Fail
	| `NoField, `NoField -> `NoField
	| `SomeField, `SomeField -> `SomeField
	| `Label (l1,pr1,ab1), `Label (l2,pr2,ab2) ->
(*TODO: eliminate `Fail *)
	    if (l1 < l2) then
	      `Label (l1, 
		      List.map (fun (d,r) -> (d, aux extra1 r true rec2)) pr1,
		      aux extra1 ab1 extra2 rec2)
	    else if (l2 < l1) then
	      `Label (l2, 
		      List.map (fun (d,r) -> (d, aux extra2 r true rec1)) pr2,
		      aux extra2 ab2 extra1 rec1)
	    else
	      let pr = 
		double_fold
		  (fun accu ((d1,t1),r1) ((d2,t2),r2) ->
		     let r = aux extra1 r1 extra2 r2 in
		     match r with 
		       | `Fail -> accu 
		       | x -> ((slcup d1 d2, Types.cap t1 t2),x)::accu)
		  pr1 pr2 in
	      `Label (l1, pr, aux extra1 ab1 extra2 ab2)
	| `Dispatch _, _ | _, `Dispatch _ -> assert false in
      let res = aux false rec1 false rec2 in
(*      Format.fprintf Format.std_formatter 
	"ncap; @\nrecord1=%a; @\nrecord2=%a;@\n result=%a@\n"
	print_record rec1
	print_record rec2
	print_record res; *)
      match res with
	| `Fail -> accu
	| r -> (fus res1 res2, r) :: accu
    in
895
896
897
898
899
900
    { nfv = SortedList.cup nf1.nfv nf2.nfv;
      ncatchv = SortedList.cup nf1.ncatchv nf2.ncatchv;
      na = Types.cap nf1.na nf2.na;
      nbasic = double_fold basic nf1.nbasic nf2.nbasic;
      nprod = double_fold prod nf1.nprod nf2.nprod;
      nxml = double_fold prod nf1.nxml nf2.nxml;
901
      nrecord = double_fold record nf1.nrecord nf2.nrecord;
902
903
    }

904
905
  let nnode p = [p], Types.descr p.accept

906
  let ntimes acc p q = 
907
908
909
910
    let src_p = List.map (fun v -> (v,SLeft)) p.fv
    and src_q = List.map (fun v -> (v,SRight)) q.fv in
    let src = SortedMap.union (fun _ _ -> SRecompose) src_p src_q in 
(*    let rects = Types.Product.normal acc in
911
    let prod = List.map (fun (t1,t2) -> (src, (([p],t1),([q],t2)))) rects in
912
*)
913
914
915
    { nempty with 
	nfv = SortedList.cup p.fv q.fv; 
	na = acc;
916
917
918
919
920
921
922
923
924
925
926
	nprod = [ (src, (nnode p, nnode q)) ];
    }

  let nxml acc p q = 
    let src_p = List.map (fun v -> (v,SLeft)) p.fv
    and src_q = List.map (fun v -> (v,SRight)) q.fv in
    let src = SortedMap.union (fun _ _ -> SRecompose) src_p src_q in 
    { nempty with 
	nfv = SortedList.cup p.fv q.fv; 
	na = acc;
	nxml =  [ (src, (nnode p, nnode q)) ];
927
928
    }
    
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  let nrecord acc l p =
    let src = List.map (fun v -> (v, SField l)) p.fv in
    let r = Types.Record.normal acc in
    { nempty with
	nfv = p.fv;
	na = acc;
	nrecord = [ src, `Label (l,[nnode p, `Success],`Fail) ] }

  let nconstr t =
    let rec aux_record = function
      | `Success -> `Success
      | `Fail -> `Fail
      | `NoField -> `NoField
      | `SomeField -> `SomeField
      | `Label (l, pr, ab) ->
	  `Label (l, 
		  List.map (fun (t,r) -> ([],t), aux_record r) pr,
		  aux_record ab) in
    { nempty with
	na = t;
	nbasic = [ [], Types.cap t any_basic ];
	nprod = 
	  List.map 
	    (fun (t1,t2) -> [], (([],t1),([],t2)))
	    (Types.Product.normal t);
	nxml= 
	  List.map 
	    (fun (t1,t2) -> [], (([],t1),([],t2)))
	    (Types.Product.normal ~kind:`XML t);
	nrecord = [ [], aux_record (Types.Record.normal t) ]
    }

  let nconstant x c = 
    let l = [x,SConst c] in
    { nfv = [x];
      ncatchv = [];
      na = Types.any;
      nbasic = [ (l,any_basic) ]; 
      nprod  = [ (l,(([], Types.any),([], Types.any))) ];
      nxml   = [ (l,(([], Types.any),([], Types.any))) ];
      nrecord = [ (l,`Success) ];
    }

  let ncapture x = 
    let l = [x,SCatch] in
    { nfv = [x];
      ncatchv = [x];
      na = Types.any;
      nbasic = [ (l,any_basic) ]; 
      nprod  = [ (l,(([], Types.any),([], Types.any))) ];
      nxml   = [ (l,(([], Types.any),([], Types.any))) ];
      nrecord = [ (l,`Success) ];
    }

  let rec nnormal (acc,fv,d) =
    if Types.is_empty acc 
    then nempty
    else match d with
      | Constr t -> nconstr t
      | Cap (p,q) -> ncap (nnormal p) (nnormal q)
      | Cup ((acc1,_,_) as p,q) -> 
	  ncup (nnormal p) (ncap (nnormal q) (nconstr (Types.neg acc1)))
      | Times (p,q) -> ntimes acc p q
      | Xml (p,q) -> nxml acc p q
      | Capture x -> ncapture x
      | Constant (x,c) -> nconstant x c
      | Record (l,p) -> nrecord acc l p
   
  let remove_catchv n =
    let ncv = n.ncatchv in
    let nlines l = 
      let l = List.map (fun (res,x) -> (SortedMap.diff res ncv,x)) l in
(*       let l = SortedList.from_list l in (* Can get rid of it ? *) *)
      l in
    { nfv     = SortedList.diff n.nfv ncv;
      ncatchv = n.ncatchv;
      na      = n.na;
      nbasic  = nlines n.nbasic;
      nprod   = nlines n.nprod;
      nxml    = nlines n.nxml;
      nrecord = nlines n.nrecord;
    }

  let normal t pl =
    remove_catchv
      (List.fold_left (fun a p -> ncap a (nnormal (descr p))) (nconstr t) pl)
1015

1016
(*
1017
1018
  let empty = { v = []; catchv = []; 
		a = Types.empty; 
1019
		basic = []; prod = []; xml = []; record = [] }
1020
  let restrict t nf =
1021
1022
1023
1024
1025
1026
    let rec filter = function
      | (key,acc) :: rem -> 
	  let acc = Types.cap t acc in
	  if Types.is_empty acc then filter rem else (key,acc) :: (filter rem)
      | [] -> []
    in
1027
    {  v = nf.v;
1028
       catchv = nf.catchv;
1029
       a = Types.cap t nf.a;
1030
1031
       basic = filter nf.basic;
       prod = filter nf.prod;
1032
       xml = filter nf.xml;
1033
       record = filter nf.record;
1034
    }
1035

1036

1037
  let cap nf1 nf2 =
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
    let merge f lines1 lines2 =
      let m =
	List.fold_left 
	  (fun accu ((res1,x1),acc1) ->
	     List.fold_left
	     (fun accu ((res2,x2),acc2) ->
		let acc = Types.cap acc1 acc2 in
		if Types.is_empty acc then accu
		else ((fus res1 res2, f x1 x2),acc) :: accu
	     ) accu lines2
	  ) [] lines1 in
      SortedMap.from_list Types.cup m
    in
    let merge_basic () () = ()
1052
    and merge_prod (p1,q1) (p2,q2) = slcup p1 p2, slcup q1 q2
1053
    and merge_record r1 r2 = SortedMap.union slcup r1 r2 in
1054
    { v = SortedList.cup nf1.v nf2.v;
1055
      catchv = SortedList.cup nf1.catchv nf2.catchv;
1056
      a = Types.cap nf1.a nf2.a;
1057
1058
      basic = merge merge_basic nf1.basic nf2.basic;
      prod = merge merge_prod nf1.prod nf2.prod;
1059
      xml = merge merge_prod nf1.xml nf2.xml;
1060
      record = merge merge_record nf1.record nf2.record;
1061
    }
1062

1063

1064
1065
1066
		  
  let cup acc1 nf1 nf2 =
    let nf2 = restrict (Types.neg acc1) nf2 in
1067
    { v = nf1.v; (* = nf2.v *)
1068
      catchv = SortedList.cap nf1.catchv nf2.catchv;
1069
1070
      a = Types.cup nf1.a nf2.a;
      basic = SortedMap.union Types.cup nf1.basic nf2.basic;
1071
      prod  = SortedMap.union Types.cup nf1.prod nf2.prod;
1072
      xml   = SortedMap.union Types.cup nf1.xml nf2.xml;
1073
      record = SortedMap.union Types.cup nf1.record nf2.record;
1074
    }
1075

1076
  let times acc p q = 
1077
1078
1079
    let src_p = List.map (fun v -> (v,SLeft)) p.fv
    and src_q = List.map (fun v -> (v,SRight)) q.fv in
    let src = SortedMap.union (fun _ _ -> SRecompose) src_p src_q in 
1080
1081
1082
    { empty with 
	v = SortedList.cup p.fv q.fv; 
	a = acc;
1083
	prod = [ (src, ([p], [q])), acc ] }
1084

1085
  let xml acc p q = 
1086
1087
1088
    let src_p = List.map (fun v -> (v,SLeft)) p.fv
    and src_q = List.map (fun v -> (v,SRight)) q.fv in
    let src = SortedMap.union (fun _ _ -> SRecompose) src_p src_q in 
1089
1090
1091
1092
1093
    { empty with 
	v = SortedList.cup p.fv q.fv; 
	a = acc;
	xml = [ (src, ([p], [q])), acc ] }

1094
  let record acc l p =
1095
    let src = List.map (fun v -> (v, SField l)) p.fv in
1096
1097
1098
    { empty with
	v = p.fv;
	a = acc;
1099
	record = [ (src, [l,[p]]), acc ] }
1100
1101

  let any =
1102
1103
    { v = []; 
      catchv = [];
1104
      a = Types.any;
1105
1106
      basic = [ ([],()), any_basic ]; 
      prod  = [ ([],([],[])), Types.Product.any ];
1107
      xml   = [ ([],([],[])), Types.Product.any_xml ];
1108
      record = [ ([],[]), Types.Record.any ];
1109
    }
1110

1111
  let capture x =
1112
    let l = [x,SCatch] in
1113
    { v = [x];
1114
      catchv = [x];
1115
      a = Types.any;
1116
1117
      basic = [ (l,()), any_basic ]; 
      prod  = [ (l,([],[])), Types.Product.any  ];
1118
      xml  = [ (l,([],[])), Types.Product.any_xml  ];
1119
      record = [ (l,[]), Types.Record.any ];
1120
    }
1121

1122
  let constant x c =
1123
    let l = [x,SConst c] in
1124
    { v = [x];
1125
      catchv = [];
1126
      a = Types.any;
1127
1128
      basic = [ (l,()), any_basic ]; 
      prod  = [ (l,([],[])), Types.Product.any  ];
1129
      xml   = [ (l,([],[])), Types.Product.any_xml  ];
1130
      record = [ (l,[]), Types.Record.any ];
1131
    }
1132

1133
1134
  let constr t =
    { v = [];
1135
      catchv = [];
1136
      a = t;
1137
1138
      basic = [ ([],()), Types.cap t any_basic ];
      prod  = [ ([],([],[])), Types.cap t Types.Product.any ];
1139
      xml   = [ ([],([],[])), Types.cap t Types.Product.any_xml ];
1140
      record = [ ([],[]), Types.cap t Types.Record.any ];
1141
    }
1142

1143
1144
1145
1146
1147
(* Put a pattern in normal form *)
  let rec nf (acc,fv,d) =
    if Types.is_empty acc 
    then empty
    else match d with
1148
1149
      | Constr t -> constr t
      | Cap (p,q) -> cap (nf p) (nf q)
1150
1151
      | Cup ((acc1,_,_) as p,q) -> cup acc1 (nf p) (nf q)
      | Times (p,q) -> times acc p q
1152
      | Xml (p,q) -> xml acc p q
1153
1154
1155
1156
      | Capture x -> capture x
      | Constant (x,c) -> constant x c
      | Record (l,p) -> record acc l p

1157
1158
1159
1160
  let normal nf =
    let basic =
      List.map (fun ((res,()),acc) -> (res,acc)) 

1161
    and prod ?kind l =
1162
      let line accu (((res,(pl,ql)),acc)) =
1163
	let aux accu (t1,t2) = (res,( (pl,t1), (ql,t2) ))::accu in
1164
	let t = Types.Product.normal ?kind acc in
1165
	List.fold_left aux accu t in
1166
      List.fold_left line [] l
1167
   
1168
1169
1170
1171
1172

    and record =
      let rec aux nr fields = 
	match (nr,fields) with
	  | (`Success, []) -> `Success
1173
1174
1175
1176
1177
	  | (`SomeField, []) -> `SomeField
	  | (`NoField, []) -> `NoField
	  | (`Fail,_) | (`NoField,_::_) -> `Fail
	  | ((`Success|`SomeField), (l2,pl)::fields) ->
	      `Label (l2, [(pl,Types.any), aux `Success fields], `Fail)
1178
	  | (`Label (l1, _, _), (l2,pl)::fields) when l2 < l1 ->
1179
	      assert false
1180
	      `Label (l2, [(pl,Types.any), aux nr fields], `Fail)
1181
1182
1183
(* Errr... here, should remember that SomeOtherField has been seen, no ?
   Actually, case cannot happen, I guess *)

1184
1185
	  | (`Label (l1, pr, _), (l2,pl)::fields) when l1 = l2 ->
	      let pr = 
1186
		List.map (fun (t,x) -> (((pl,t) : nnf), aux x fields)) pr in
1187
1188
	      `Label (l1, pr, `Fail)
	  | (`Label (l1, pr, ab),_) ->
1189
	      let aux_ab = aux ab fields in
1190
	      let pr = 
1191
		List.map (fun (t,x) -> (([],t), 
1192
1193
1194
1195
1196
(* Types.Record.normal enforce physical equility
   in case of a ? field *)
					if x==ab then aux_ab else
					aux x fields)) pr in
	      `Label (l1, pr, aux_ab)
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
      in

      let line accu ((res,fields),acc) =
	let nr = Types.Record.normal acc in
	let x = aux nr fields in
	match x with 
	  | `Fail -> accu 
	  | x -> (res,x) :: accu in
      List.fold_left line []
    in
1207
    let nlines l = 
1208
1209
1210
      let l = List.map (fun (res,x) -> (SortedMap.diff res nf.catchv,x)) l in
(*       let l = SortedList.from_list l in (* Can get rid of it ? *) *)
      l in
1211
1212
    { nfv     = SortedList.diff nf.v nf.catchv; 
      ncatchv = nf.catchv;
1213
      na      = nf.a;
1214
1215
      nbasic  = nlines (basic nf.basic);
      nprod   = nlines (prod nf.prod);
1216
      nxml    = nlines (prod ~kind:`XML nf.xml);
1217
      nrecord = nlines (record nf.record);
1218
    }
1219

1220
1221
  let normal t pl =
    normal (List.fold_left (fun a p -> cap a (nf (descr p))) (constr t) pl)
1222
    *)
1223
end
1224
1225


1226
1227
module Compile = 
struct
1228
  type actions =
1229
1230
    | AIgnore of result
    | AKind of actions_kind
1231
  and actions_kind = {
1232
1233
    basic: (Types.descr * result) list;
    prod: result dispatch dispatch;
1234
    xml: result dispatch dispatch;
1235
1236
1237
1238
    record: record option;
  }
  and record = 
      [ `Label of Types.label * record dispatch * record option
1239
      | `Result of result
1240
      | `Result_other of Types.label list * result * result ]
1241
      
1242
  and 'a dispatch =
1243
1244
1245
1246
    | Dispatch of dispatcher * 'a array
    | TailCall of dispatcher
    | Ignore of 'a
    | Impossible
1247
1248

  and result = int * source array
1249
  and source = 
1250
1251
1252
    | Catch | Const of Types.const 
    | Left of int | Right of int | Recompose of int * int
    | Field of Types.label * int
1253
1254
1255
1256
1257
1258
      
  and return_code = 
      Types.descr * int *   (* accepted type, arity *)
      (int * (capture, int) SortedMap.t) list

  and interface =
1259
1260
    [ `Result of int
    | `Switch of interface * interface
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
    | `None ]

  and dispatcher = {
    id : int;
    t  : Types.descr;
    pl : Normal.t array;
    interface : interface;
    codes : return_code array;
    mutable actions : actions option
  }
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285

  let array_for_all f a =
    let rec aux f a</