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

4
5
exception IllFormedCup of fv * fv
exception IllFormedCap of fv * fv
6

7
(* Syntactic algebra *)
8

9
type d =
10
  | Constr of Types.node
11
  | Cup of descr * descr
12
  | Cap of descr * descr
13
  | Times of node * node
14
15
16
  | Record of Types.label * node
  | Capture of capture
  | Constant of capture * Types.const
17
18
and node = {
  id : int;
19
20
  mutable descr : descr option;
  accept : Types.node;
21
  fv : fv
22
} and descr = Types.descr * fv * d
23

24
25
26
27
28
let make =
  let counter = ref 0 in
  fun fv ->
    incr counter;
    { id = !counter; descr = None; accept = Types.make (); fv = fv }
29
30

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

35
let constr x = (Types.descr x,[],Constr x)
36
let cup ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
37
38
  if fv1 <> fv2 then raise (IllFormedCup (fv1,fv2));
  (Types.cup acc1 acc2, SortedList.cup fv1 fv2, Cup (x1,x2))
39
let cap ((acc1,fv1,_) as x1) ((acc2,fv2,_) as x2) = 
40
41
  if not (SortedList.disjoint fv1 fv2) then raise (IllFormedCap (fv1,fv2));
  (Types.cap acc1 acc2, SortedList.cup fv1 fv2, Cap (x1,x2))
42
let times x y =
43
  (Types.times x.accept y.accept, SortedList.cup x.fv y.fv, Times (x,y))
44
let record l x = 
45
46
47
  (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))
48

49
50
51
52
53

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
54

55
56
57
58

(* Static semantics *)

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

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

let memo_filter = ref MemoFilter.empty

67
let rec filter_descr t (_,fv,d) : (capture, Types.Positive.v) SortedMap.t =
68
69
70
71
  if Types.is_empty t 
  then empty_res fv
  else
    match d with
72
      | Constr _ -> []
73
      | Cup ((a,_,_) as d1,d2) ->
74
	  SortedMap.union cup_res
75
76
	    (filter_descr (Types.cap t a) d1)
	    (filter_descr (Types.diff t a) d2)
77
      | Cap (d1,d2) ->
78
79
80
81
82
83
84
85
86
87
88
89
90
	  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)
91
92
93
      | Record (l,p) ->
	  filter_node (Types.Record.project t l) p
      | Capture c ->
94
	  [(c, Types.Positive.ty t)]
95
      | Constant (c, cst) ->
96
97
98
	  [(c, Types.Positive.ty (Types.constant cst))]

and filter_node t p : (capture, Types.Positive.v) SortedMap.t =
99
100
  try MemoFilter.find (t,p) !memo_filter
  with Not_found ->
101
102
    let (_,fv,_) as d = descr p in
    let res = List.map (fun v -> (v,Types.Positive.forward ())) fv in
103
104
    memo_filter := MemoFilter.add (t,p) res !memo_filter;
    let r = filter_descr t (descr p) in
105
    List.iter2 (fun (_,r) (_,v) -> Types.Positive.define v r) r res;
106
107
108
109
110
    r

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

113

114

115
(* Normal forms for patterns and compilation *)
116

117
118
119
120
121
122
123
124
125
126
127
128
129
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

130
  type 'a line = (result * 'a, Types.descr) sm
131
132
133
  type nf = {
    v     : fv;
    a     : Types.descr;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    basic : unit line;
    prod  : (node sl * node sl) line;
    record: ((Types.label, node sl) sm) line
  }

  type 'a nline = (result *  'a) list
  type record =
      [ `Success
      | `Fail
      | `Dispatch of (nf * record) list
      | `Label of Types.label * (nf * record) list * record ]
  type normal = {
    nbasic : Types.descr nline;
    nprod  : (nf * nf) nline;
    nrecord: record nline
149
  }
150

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

154

155
  let restrict t nf =
156
157
158
159
160
161
    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
162
163
    {  v = nf.v;
       a = Types.cap t nf.a;
164
165
166
       basic = filter nf.basic;
       prod = filter nf.prod;
       record = filter nf.record;
167
    }
168

169
170
  let fus = SortedMap.union_disj
  let slcup = SortedList.cup
171

172
  let cap nf1 nf2 =
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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 () () = ()
    and merge_prod (p1,q1) (p2,q2) = slcup p1 p1, slcup q1 q2
    and merge_record r1 r2 = SortedMap.union slcup r1 r2 in
189
190
    { v = SortedList.cup nf1.v nf2.v;
      a = Types.cap nf1.a nf2.a;
191
192
193
      basic = merge merge_basic nf1.basic nf2.basic;
      prod = merge merge_prod nf1.prod nf2.prod;
      record = merge merge_record nf1.record nf2.record;
194
    }
195

196

197
198
199
		  
  let cup acc1 nf1 nf2 =
    let nf2 = restrict (Types.neg acc1) nf2 in
200
    { v = nf1.v; (* = nf2.v *)
201
202
      a = Types.cup nf1.a nf2.a;
      basic = SortedMap.union Types.cup nf1.basic nf2.basic;
203
204
      prod  = SortedMap.union Types.cup nf1.prod nf2.prod;
      record = SortedMap.union Types.cup nf1.record nf2.record;
205
    }
206

207
208
209
210
211
212
213
  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;
214
	prod = [ (src, ([p], [q])), acc ] }
215
216
217
218
219
220

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

  let any =
    { v = [];
      a = Types.any;
226
227
228
      basic = [ ([],()), any_basic ]; 
      prod  = [ ([],([],[])), Types.Product.any ];
      record = [ ([],[]), Types.Record.any ];
229
    }
230

231
232
233
234
  let capture x =
    let l = [x,`Catch] in
    { v = [x];
      a = Types.any;
235
236
237
      basic = [ (l,()), any_basic ]; 
      prod  = [ (l,([],[])), Types.Product.any  ];
      record = [ (l,[]), Types.Record.any ];
238
    }
239

240
241
242
243
  let constant x c =
    let l = [x,`Const c] in
    { v = [x];
      a = Types.any;
244
245
246
      basic = [ (l,()), any_basic ]; 
      prod  = [ (l,([],[])), Types.Product.any  ];
      record = [ (l,[]), Types.Record.any ];
247
    }
248

249
250
251
  let constr t =
    { v = [];
      a = t;
252
253
254
      basic = [ ([],()), Types.cap t any_basic ];
      prod  = [ ([],([],[])), Types.cap t Types.Product.any ];
      record = [ ([],[]), Types.cap t Types.Record.any ];
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
(* 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;
281
      record: record option;
282
283
284
    }
    and prod = disp * (mask * disp * (mask * prod_result) list) list 
    and prod_result = (result * (int * int)) option list
285

286
287
288
289
    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
290

291
292
    and mask = bool list
    and disp = Types.descr * nf SortedList.t
293
  end
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

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

    and prod =
      let line accu (((res,(pl,ql)),acc)) =
	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 acc) in
      List.fold_left line []

    and record =
      let rec aux nr fields = 
	match (nr,fields) with
	  | (`Success, []) -> `Success
	  | (`Fail,_) -> `Fail
	  | (`Success, (l2,pl)::fields) ->
	      `Label (l2, [bigcap pl, aux nr fields], `Fail)
	  | (`Label (l1, _, _), (l2,pl)::fields) when l2 < l1 ->
	      `Label (l2, [bigcap pl, aux 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, aux x fields)) pr in
	      `Label (l1, pr, `Fail)
	  | (`Label (l1, pr, ab),_) ->
	      let pr = 
		List.map (fun (t,x) -> (constr t, aux x fields)) pr in
	      `Label (l1, pr, aux ab fields)
      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
    { nbasic  = basic nf.basic;
      nprod   = prod nf.prod;
      nrecord = record nf.record;
    }

339
340
341
  let collect f pp =
    let aux accu (res,x) = (f x) :: accu in
    SortedList.from_list (List.fold_left (List.fold_left aux) [] pp)
342

343
344
345
346
347
  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
348

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  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
371
    let pl = List.map (fun p -> p.nbasic) pl in
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    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 rec dispatch_prod t pl =
387
    let pl = List.map (fun p -> p.nprod) pl in
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    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
408

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  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 *)
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
  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

(* combiner les restrict field, ... *)
  let rec dispatch_record t pl =
488
489
490
    let pl = List.map 
	       (fun p -> List.map (fun (res,r) -> (res,[],r)) p.nrecord
	       ) pl in
491
    let t = Types.Record.get t in
492
    if Types.Record.is_empty t then None else Some (aux_record1 t pl)
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
  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
527
	  accu
528

529
530
531
  let mask l = List.map (function None -> false | Some _ -> true) l

  let rec dispatch (t : Types.descr) (pl : nf list) =
532
533
    let fv = List.map (fun p -> p.v) pl in
    let pl = List.map (fun p -> normal (restrict t p)) pl in
534
535
536
537
538
539
540
541
542
543
544
545
    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

    { 
546
      Dispatch.fv = fv;
547
548
549
550
      Dispatch.masks = masks;
      Dispatch.basic = basic;
      Dispatch.prod = prod;
      Dispatch.record = record;
551
552
    }

553

554
555
556
  let to_print = ref []
  let memo = ref []
  let count = ref 0
557

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

561
562
563
564
  let res_basic x = function
    | `Catch -> "v"
    | `Const c -> "const"
    | _ -> assert false
565

566
567
568
569
570
571
  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
572

573
574
575
576
577
  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
578

579

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

583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  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 []
600

601
602
603
604
605
606
607
  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
608

609
610
611
612
  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
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 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
640
      
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  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

657
658
659
660
661
  and show_record ppf = function
    | None -> ()
    | Some r ->
	Format.fprintf ppf "  | Record r -> @\n";
	Format.fprintf ppf "     @[%a@]@\n" show_record_aux r
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

  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 ()
695

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

699
end   
700

701
702
703
704
705
706
707
708
709
710
711
(*
  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?)* ]";;
*)
712

713
(*
714
715
let pat s = Patterns.descr (Typer.pat (Parser.From_string.pat s));;
let typ s = Types.descr (Typer.typ (Parser.From_string.pat s));;
716
717
718
719
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;;
720

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

723
724
show ["{x=2;y=3}"];;
show [" [((x::1)|(y::2))*] "];;
725

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

729
730
731
 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))" ];;
732

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

736

737
738
739
740
741
742
743
744
745
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;;
746
*)
747
748