patterns.ml 21.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
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
587
588
589
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
type capture = string
type fv = capture SortedList.t

exception IllFormedCup of fv * fv
exception IllFormedCap of fv * fv

(* Syntactic algebra *)

type d =
  | Constr of Types.node
  | Cup of descr * descr
  | Cap of descr * descr
  | Times of node * node
  | Record of Types.label * node
  | Capture of capture
  | Constant of capture * Types.const
and node = {
  id : int;
  mutable descr : descr option;
  accept : Types.node;
  fv : fv
} and descr = Types.descr * fv * d

let make =
  let counter = ref 0 in
  fun fv ->
    incr counter;
    { id = !counter; descr = None; accept = Types.make (); fv = fv }

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

let constr x = (Types.descr x,[],Constr x)
let cup ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
  if fv1 <> fv2 then raise (IllFormedCup (fv1,fv2));
  (Types.cup acc1 acc2, SortedList.cup fv1 fv2, Cup (x1,x2))
let cap ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
  if not (SortedList.disjoint fv1 fv2) then raise (IllFormedCap (fv1,fv2));
  (Types.cap acc1 acc2, SortedList.cup fv1 fv2, Cap (x1,x2))
let times x y =
  (Types.times x.accept y.accept, SortedList.cup x.fv y.fv, Times (x,y))
let record l x = 
  (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))


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


(* Static semantics *)

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

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

let memo_filter = ref MemoFilter.empty

let rec filter_descr t (_,fv,d) : (capture, Types.Positive.v) SortedMap.t =
  if Types.is_empty t 
  then empty_res fv
  else
    match d with
      | Constr _ -> []
      | Cup ((a,_,_) as d1,d2) ->
	  SortedMap.union cup_res
	    (filter_descr (Types.cap t a) d1)
	    (filter_descr (Types.diff t a) d2)
      | Cap (d1,d2) ->
	  SortedMap.union cup_res (filter_descr t d1) (filter_descr t d2)
      | Times (p1,p2) ->
	  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 t)
      | Record (l,p) ->
	  filter_node (Types.Record.project t l) p
      | Capture c ->
	  [(c, Types.Positive.ty t)]
      | Constant (c, cst) ->
	  [(c, Types.Positive.ty (Types.constant cst))]

and filter_node t p : (capture, Types.Positive.v) SortedMap.t =
  try MemoFilter.find (t,p) !memo_filter
  with Not_found ->
    let (_,fv,_) as d = descr p in
    let res = List.map (fun v -> (v,Types.Positive.forward ())) fv in
    memo_filter := MemoFilter.add (t,p) res !memo_filter;
    let r = filter_descr t (descr p) in
    List.iter2 (fun (_,r) (_,v) -> Types.Positive.define v r) r res;
    r

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



(* Normal forms for patterns and compilation *)

module NF = 
struct

  type 'a sl = 'a SortedList.t
  type ('a,'b) sm = ('a,'b) SortedMap.t

  type source = 
      [ `Catch | `Const of Types.const 
      | `Left | `Right | `Recompose 
      | `Field of Types.label 
      ]
  type result = (capture, source) sm

  type nf = {
    v     : fv;
    a     : Types.descr;
    basic : (result, Types.descr) sm;
    prod  : (result * Types.descr * node sl * node sl) sl;
    record: (result * Types.descr * (Types.label, node sl) sm) sl;
  }

  let empty = { v = []; a = Types.empty; basic = []; prod = []; record = [] }
  let any_basic = Types.neg (Types.cup Types.Product.any Types.Record.any)


  let restrict t nf =
    let map_filter f l =
      let g accu x = match f x with Some y -> y::accu | None -> accu in
      SortedList.from_list (List.fold_left g [] l) in

    let aux_basic (res,bt) = 
      let bt = Types.cap t bt in
      if Types.is_empty bt then None else Some (res,bt) in

    let aux_prod (res,bt,p,q) =
      let bt = Types.cap t bt in
      if Types.is_empty bt then None else Some (res,bt,p,q) in

    let aux_record (res,bt,r) =
      let bt = Types.cap t bt in
      if Types.is_empty bt then None else Some (res,bt,r) in

    {  v = nf.v;
       a = Types.cap t nf.a;
       basic = map_filter aux_basic nf.basic;
       prod = map_filter aux_prod nf.prod;
       record = map_filter aux_record nf.record;
    }

  let fus = SortedMap.union_disj
  let slcup = SortedList.cup

  let cap nf1 nf2 =
    let aux f x1 x2 = 
      SortedList.from_list 
	(List.fold_left (fun accu a -> List.fold_left (f a) accu x2) [] x1) in

    let aux_basic (res1,t1) accu (res2,t2) =
      let t = Types.cap t1 t2 in
      if Types.is_empty t then accu 
      else (fus res1 res2, t)::accu in

    let aux_prod (res1,t1,p1,q1) accu (res2,t2,p2,q2) =
      let t = Types.cap t1 t2 in
      if Types.is_empty t then accu 
      else (fus res1 res2, t, slcup p1 p2, slcup q1 q2)::accu in

    let aux_record (res1,t1,r1) accu (res2,t2,r2) =
      let t = Types.cap t1 t2 in
      if Types.is_empty t then accu 
      else (fus res1 res2, t, SortedMap.union slcup r1 r2)
	::accu in

    { v = SortedList.cup nf1.v nf2.v;
      a = Types.cap nf1.a nf2.a;
      basic = SortedMap.from_sorted_list Types.cup 
		(aux aux_basic nf1.basic nf2.basic);
      prod = aux aux_prod nf1.prod nf2.prod;
      record = aux aux_record nf1.record nf2.record;
    }


		  
  let cup acc1 nf1 nf2 =
    let nf2 = restrict (Types.neg acc1) nf2 in
    { v = SortedList.cup nf1.v nf2.v;
      a = Types.cup nf1.a nf2.a;
      basic = SortedMap.union Types.cup nf1.basic nf2.basic;
      prod  = SortedList.cup nf1.prod nf2.prod;
      record = SortedList.cup nf1.record nf2.record;
    }

  let times acc p q = 
    let src_p = List.map (fun v -> (v,`Left)) p.fv
    and src_q = List.map (fun v -> (v,`Right)) q.fv in
    let src = SortedMap.union (fun _ _ -> `Recompose) src_p src_q in 
    { empty with 
	v = SortedList.cup p.fv q.fv; 
	a = acc;
	prod = [ src, acc, [p], [q] ] }

  let record acc l p =
    let src = List.map (fun v -> (v, `Field l)) p.fv in
    { empty with
	v = p.fv;
	a = acc;
	record = [ src, acc, [l,[p]] ] }

  let any =
    { v = [];
      a = Types.any;
      basic = [ [], any_basic ]; 
      prod  = [ [], Types.Product.any,[],[] ];
      record = [ [], Types.Record.any,[] ];
    }

  let capture x =
    let l = [x,`Catch] in
    { v = [x];
      a = Types.any;
      basic = [ l, any_basic ]; 
      prod  = [ l, Types.Product.any,[],[] ];
      record = [ l, Types.Record.any,[] ];
    }

  let constant x c =
    let l = [x,`Const c] in
    { v = [x];
      a = Types.any;
      basic = [ l, any_basic ]; 
      prod  = [ l, Types.Product.any,[],[] ];
      record = [ l, Types.Record.any,[] ];
    }

  let constr t =
    { v = [];
      a = t;
      basic = [ [], Types.cap t any_basic ];
      prod  = [ [], Types.cap t Types.Product.any,[],[] ];
      record = [ [], Types.cap t Types.Record.any,[] ];
    }

(* Put a pattern in normal form *)
  let rec nf (acc,fv,d) =
    if Types.is_empty acc 
    then empty
    else match d with
      | Constr t -> constr (Types.descr t)
      | Cap (p,q) -> cap (nf p) (nf q)
      | Cup ((acc1,_,_) as p,q) -> cup acc1 (nf p) (nf q)
      | Times (p,q) -> times acc p q
      | Capture x -> capture x
      | Constant (x,c) -> constant x c
      | Record (l,p) -> record acc l p

  let bigcap = List.fold_left (fun a p -> cap a (nf (descr p))) any

  let rec num i = function [] -> [] | h::t -> (h,i)::(num (i+1) t)


  module Dispatch = struct
    type t = { 
      fv    : fv list;
      masks : (mask * int) list;
      basic : (Types.descr * (result option list)) list;
      prod  : prod;
      record: record;
    }
    and prod = disp * (mask * disp * (mask * prod_result) list) list 
    and prod_result = (result * (int * int)) option list

    and record = 
	[ `Label of Types.label * disp * (mask * record) list * record option
	| `Result of record_result ]
    and record_result = (result * (Types.label * int) list) option list

    and mask = bool list
    and disp = Types.descr * nf SortedList.t
  end
      
  let collect f pp =
    let aux accu (res,x) = (f x) :: accu in
    SortedList.from_list (List.fold_left (List.fold_left aux) [] pp)

  let rec map_map f = function 
    | [] -> [] 
    | x::l -> 
	try let y = f x in y::(map_map f l)
	with Not_found -> map_map f l

  let get_option = function [x] -> Some x | [] -> None | _ -> assert false
  let extract_unique f l = get_option (map_map f l)
(* Could optimize to extract directly the first (and single) *)

(* Basic (and arrow) types *)

  let filter_basic ty =
    let aux (k,t) = if Types.subtype ty t then k else raise Not_found in
    List.map (extract_unique aux)
   
  let dispatch_basic t pl =
    let rec aux pl accu t l = 
      if Types.is_empty t then accu 
      else match l with
	| [] ->
	    let pl = filter_basic t pl in
	    (t, pl) :: accu
	| ty :: rem ->
	    let accu = aux pl accu (Types.cap t ty) rem in
	    let accu = aux pl accu (Types.diff t ty) rem in
	    accu
    in
    let pl = List.map (fun p -> p.basic) pl in
    let tests = collect (fun x -> x) pl in
    let t = Types.cap any_basic t in
    aux pl [] t tests

(* Product types *)

  let filter_prod1 success =
    let aux (res,(p,q)) = (res,(List.assoc p success,q)) in
    List.map (map_map aux)

  let filter_prod2 success =
    let aux (res,(i,q)) = (res,(i,List.assoc q success)) in
    List.map (extract_unique aux)

  let prepare_prod p =
    let line accu (res,t,pl,ql) =
      let p = bigcap pl and q = bigcap ql in
      let aux accu (t1,t2) = (res,(restrict t1 p,restrict t2 q))::accu in
      List.fold_left aux accu (Types.Product.normal t) in
    List.fold_left line [] p.prod

  let rec dispatch_prod t pl =
    let pl = List.map prepare_prod pl in
    let tests = collect (fun (p,_) -> p) pl in
    let t = Types.Product.get t in
    let disp = aux_prod1 t pl [] [] [] 0 tests in
    let pi1 = Types.Product.pi1 t in
    ((pi1,tests),disp)

  and aux_prod2 t pl accu mask success j tests = 
    if Types.is_empty t then accu 
    else match tests with
      | [] ->
	  let pl = filter_prod2 success pl in
	  (List.rev mask, pl) :: accu
      | p :: rem -> 
	  let accu =
	    let t = Types.cap t p.a in
	    aux_prod2 t pl accu (true::mask) ((p,j)::success) (j+1) rem in
	  let accu =
	    let t = Types.diff t p.a in
	    aux_prod2 t pl accu (false::mask) success (j+1) rem in
	  accu

  and aux_prod1 t pl accu mask success i tests =
    if t = [] then accu 
    else match tests with
      | [] ->
	  let pl = filter_prod1 success pl in
	  let t = Types.Product.pi2 t in
	  let tests = collect (fun (_,q)-> q) pl in
	  let disp = aux_prod2 t pl [] [] [] 0 tests in
	  let mask = List.rev mask in
	  (mask, (t,tests), disp) :: accu
      | p :: rem ->
	  let accu = 
	    let t = Types.Product.restrict_1 t p.a in
	    aux_prod1 t pl accu (true::mask) ((p,i)::success) (i+1) rem in
	  let accu =
	    let t = Types.Product.restrict_1 t (Types.neg p.a) in
	    aux_prod1 t pl accu (false::mask) success (i+1) rem in
	  accu

(* Record types *)

  type record =
      [ `Success
      | `Fail
      | `Dispatch of (nf * record) list
      | `Label of Types.label * (nf * record) list * record ]

  let map_record f = 
    let rec aux = function
      | [] -> []
      | h::t -> (match f h with (_,_,`Fail) -> aux t | x -> x :: (aux t)) in
    List.map aux

  let label_found l = 
    map_record 
      (function
	 | (res, catch, `Label (l1, pr, _)) when l1 = l -> 
	     (res, catch, `Dispatch pr)
	 | x -> x)

  let label_disp l success =
    map_record
      (function
	 | (res, catch, `Dispatch disp) ->
	     let aux (p,rem) = (List.assoc p success, rem) in
	     (match extract_unique aux disp with
		| None -> (res, catch, `Fail)
		| Some (i,rem) -> (res, (l, i)::catch, rem))
	 | x -> x)

  let label_not_found l = 
    map_record 
      (function
	 | (res, catch, `Label (l1, _, ab)) when l1 = l -> (res, catch, ab)
	 | x -> x)


  let dummy_label = Types.label ""

  let collect_first_label pp =
    let f = ref true and m = ref dummy_label in
    let aux = function
      | (_, _, `Label (l, _, _)) ->
	  if (!f) then (f := false; m := l) else if (l < !m) then m:= l;
      | _ -> () in
    List.iter (List.iter aux) pp;
    if !f then None else Some !m

  let collect_record pp =
    let aux accu = function
      | (res,catch,`Dispatch disp) ->  
	  List.fold_left (fun accu (p,_) -> p :: accu) accu disp
      | _ -> accu in
    SortedList.from_list (List.fold_left (List.fold_left aux) [] pp)

  let final_record =
    let aux = function
      | [(res, catch, `Success)] -> Some (res, catch)
      | [] -> None
      | _ -> assert false in
    List.map aux

  let rec cap_record nr fields = 
    match (nr,fields) with
    | (`Success, []) -> `Success
    | (`Fail,_) -> `Fail
    | (`Success, (l2,pl)::fields) ->
	`Label (l2, [bigcap pl, cap_record nr fields], `Fail)
    | (`Label (l1, _, _), (l2,pl)::fields) 
	when l2 < l1 ->
	`Label (l2, [bigcap pl, cap_record nr fields], `Fail)
    | (`Label (l1, pr, _), (l2,pl)::fields) 
	when l1 = l2 ->
	let p = bigcap pl in
	let pr = 
	  List.map (fun (t,x) -> (restrict t p, cap_record x fields)) pr in
	`Label (l1, pr, `Fail)
    | (`Label (l1, pr, ab),_) ->
	let pr = 
	  List.map (fun (t,x) -> (constr t, cap_record x fields)) pr in
	`Label (l1, pr, cap_record ab fields)


  let prepare_record =
    map_record 
      (function (res,t,fields) ->
	 let nr = Types.Record.normal t in
	 let x = cap_record nr fields in
	 (res, [], x)
      )


(* combiner les restrict field, ... *)
  let rec dispatch_record t pl =
    let pl = prepare_record (List.map (fun p -> p.record) pl) in
    let t = Types.Record.get t in
    aux_record1 t pl

  and aux_record1 t pl =
    match collect_first_label pl with
      | None -> `Result (final_record pl)
      | Some l ->
	  let (disp,pr) = 
	    let pl = label_found l pl in
	    let tests = collect_record pl in
	    let t = Types.Record.restrict_label_present t l in
	    let disp = aux_record2 t pl l [] [] [] 0 tests in
	    ((Types.Record.project_field t l, tests),disp)
	  in
	  let ab = 
	    let pl = label_not_found l pl in
	    let t = Types.Record.restrict_label_absent t l in
	    if Types.Record.is_empty t then None 
	    else Some (aux_record1 t pl)
	  in
	  `Label (l, disp, pr, ab)
	    
  and aux_record2 t pl l accu mask success i tests =
    if Types.Record.is_empty t then accu
    else match tests with
      | [] ->
	  let pl = label_disp l success pl in
	  let disp = aux_record1 t pl in
	  (List.rev mask, disp) :: accu
      | p :: rem ->
	  let accu =
	    let t = Types.Record.restrict_field t l p.a in
	    aux_record2 t pl l accu (true::mask) ((p,i)::success) (i+1) rem in
	  let accu =
	    let t = Types.Record.restrict_field t l (Types.neg p.a) in
	    aux_record2 t pl l accu (false::mask) success (i+1) rem in
	  accu
	    
  let mask l = List.map (function None -> false | Some _ -> true) l

  let rec dispatch (t : Types.descr) (pl : nf list) =
    let pl = List.map (restrict t) pl in
    let basic = dispatch_basic t pl
    and prod = dispatch_prod t pl 
    and record = dispatch_record t pl in

    let masks =
      let accu = ref [] in
      let acc r = accu := (mask r) :: !accu in
      List.iter (fun (_,r) -> acc r) basic;
      List.iter (fun (_,_,l) -> List.iter (fun (_,r) -> acc r) l) (snd prod);
      num 0 (SortedList.from_list !accu) in

    { 
      Dispatch.fv = List.map (fun p -> p.v) pl;
      Dispatch.masks = masks;
      Dispatch.basic = basic;
      Dispatch.prod = prod;
      Dispatch.record = record;
    }


  let to_print = ref []
  let memo = ref []
  let count = ref 0

  let print_fv ppf fv =
    List.iter (fun x -> Format.fprintf ppf "{%s}" x) fv

  let res_basic x = function
    | `Catch -> "v"
    | `Const c -> "const"
    | _ -> assert false

  let res_prod i j x res = 
    match res with
      | `Left -> Printf.sprintf "l%i_%s" i x
      | `Right -> Printf.sprintf "r%i_%s" j x
      | `Recompose -> Printf.sprintf "(l%i_%s,r%i_%s)" i x j x
      | _ -> res_basic x res

  let res_record m x res = 
    match res with
      | `Field l -> 
	  Printf.sprintf "f%s_%i_%s" (Types.label_name l) (List.assoc l m) x
      | _ -> res_basic x res


  let print_mask ppf =
    List.iter (fun b -> Format.fprintf ppf "%i" (if b then 1 else 0))

  let compute_vars mask pl =
    List.fold_right2
      (fun (b,i) p acc -> 
	 if b then List.fold_right (fun v acc -> (i,v)::acc) p.v acc
	 else acc
      )
      (num 0 mask) pl []

  let compute_result f result =
    List.fold_right
      (function
	 | None -> (fun acc -> acc)
	 | Some x ->
	     let (r,res) = f x in
	     List.fold_right (fun (x,s) acc -> res x s :: acc) r
      )
      result []

  let print_success ppf (mask,pl,pr) =
    let vars = 
      let v = compute_vars mask pl in
      if v = [] then "" else
	let v = List.map (fun (i,v) -> Format.sprintf "%s%i_%s" pr i v) v in
	" (" ^ String.concat ", " v ^ ")" in
    Format.fprintf ppf "`S%a%s" print_mask mask vars

  let print_result f ppf result =
    let res = compute_result f result in
    let res = if res = [] then "" else " (" ^ String.concat ", " res ^ ")" in
    Format.fprintf ppf "`S%a%s" print_mask (mask result) res


  let no t pl = 
    try List.assoc (t,pl) !memo 
    with Not_found ->
      incr count;
      let d = dispatch t pl in
      memo := ((t,pl),!count) :: !memo;
      to_print := (!count, d) :: !to_print;
      !count

  let rec show ppf num d =
    Format.fprintf ppf "let matcher_%i v = match v with@\n" num;
    show_basic ppf d.Dispatch.basic;
    show_prod ppf d.Dispatch.prod;
    show_record ppf d.Dispatch.record;
    Format.fprintf ppf "@\n"

  and show_basic ppf =
    let pr_basic ppf t =
      if Types.subtype any_basic t then Format.fprintf ppf "AnyBasic"
      else Types.Print.print_descr ppf t in
    let case (t,result) = 
      Format.fprintf ppf "  | @[%a@] -> %a@\n" 
	pr_basic t
	(print_result (fun res -> (res,res_basic))) result in
    List.iter case
      
  and show_prod ppf = function
    | (_, []) -> ()
    | ((t1,pl1), cases1) ->
	Format.fprintf ppf 
	"  | (v1,v2) -> match matcher_%i v1 with@\n" (no t1 pl1);
	List.iter (case_prod1 ppf pl1) cases1
  and case_prod2 ppf pl2 (mask2, result) =
    Format.fprintf ppf "          | %a -> %a@\n" 
      print_success (mask2,pl2,"r")
      (print_result (fun (res,(i,j)) -> (res,res_prod i j))) result
  and case_prod1 ppf pl1 (mask1, (t2,pl2), cases2) =
    Format.fprintf ppf "      | %a -> match matcher_%i v2 with@\n" 
      print_success (mask1,pl1,"l")
      (no t2 pl2);
    List.iter (case_prod2 ppf pl2) cases2

  and show_record ppf r =
    Format.fprintf ppf "  | Record r -> @\n";
    Format.fprintf ppf "     @[%a@]@\n" show_record_aux r

  and show_record_aux ppf = function
    | `Result r  ->
	print_result (fun (res,m) -> (res, res_record m)) ppf r
    | `Label (l, (t,pl), cases, ab) -> 
	let ln = Types.label_name l in
	Format.fprintf ppf "match matcher_%i r.%s with@\n" (no t pl) ln;
	if cases <> [] then
	  (
	    let case (mask, rem) =
	      Format.fprintf ppf "  | %a -> @\n    @[%a@]@\n"
		print_success (mask, pl, Printf.sprintf "f%s_" ln)
		show_record_aux rem
	    in
	    List.iter case cases;
	  );
	(match ab with
	   | Some ab ->
	       Format.fprintf ppf 
	      "  | absent -> @\n    @[%a@]@\n" show_record_aux ab
	   | None -> ()
	)
	
	
    
  let show ppf t pl =
    ignore (no t pl);
    let rec loop () = 
      match !to_print with
	| (n,d)::r -> to_print:=r; show ppf n d; loop ()
	| [] -> ()
    in
    loop ()

  let get i =
    fst (List.find (fun (_,j) -> i = j) !memo)

end   

(*
  let test_filter t p = 
    let t = Syntax.make_type (Syntax.parse t)
    and p = Syntax.make_pat (Syntax.parse p) in
    let r = Patterns.filter (Types.descr t) p in
    List.iter (fun (v,t) -> 
                 let t = Types.normalize t in
                 Format.fprintf Format.std_formatter "@[%s => %a@]@\n"
                       v Types.Print.print t) r;;
test_filter "[ (1 2 3?)* ]" "[ (x::(1 2) 3?)* ]";;
*)

(*
709
710
let pat s = Patterns.descr (Typer.pat (Parser.From_string.pat s));;
let typ s = Types.descr (Typer.typ (Parser.From_string.pat s));;
711
712
713
714
715
let f s = Patterns.NF.nf (pat s);;
let show' t l = Patterns.NF.show Format.std_formatter t (List.map f l);;
let show l = show' Types.any l;;
let showt t l = show' (typ t) l;;

716
717
 showt " [(`A `B `C?)*] "  [" [ (((x::`A) `B (x::`C))|_)* ] "];;

718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
show ["{x=2;y=3}"];;
show [" [((x::1)|(y::2))*] "];;

let g s =
   Patterns.NF.ProdMap.fold (fun k x acc -> (k,x)::acc) (Patterns.NF.left (f s)) [];;

 Patterns.NF.dispatch Types.any [ f "(x,y)" ];;
 Patterns.NF.show Format.std_formatter 0 Types.any [ f "(x,y)" ];;
 Patterns.NF.show Format.std_formatter 0 Types.any [ f "(0--100,x) | (_,(x:=10))" ];;

let t = Types.descr (Syntax.make_type (Syntax.parse "[_*]")) in
Patterns.NF.show Format.std_formatter 0 t [ f "[((x::5)|_)*]" ];;


Patterns.NF.show Format.std_formatter 0 Types.any [ f "(x,y)"; f "(x,y)"];;
Patterns.NF.show Format.std_formatter 0 Types.any [ f "x"; f "y"];;

show [ "((x,_),_)"; "((_,x),_)" ];;
showt " [ (1 3?)* ]" [ " [(x::1 3?)*] " ];;
showt " [ (1 3?)* ]" [ " [(1 (x::3)?)*] " ];;
#install_printer Types.Print.print;;
#install_printer Types.Print.print_descr;;
let (t,[p1;p2]) = Patterns.NF.get 5;;
*)