typer.ml 36.5 KB
Newer Older
1
(* TODO:
2
3
 - rewrite type-checking of operators to propagate constraint
 - rewrite translation of types and patterns -> hash cons
4
5
6
     CONTINUE THIS...
     Problem with same name for recursive binders in different
     recursive type/patterns definitions because of hash-consing
7
8
*)

9

10
11
(* I. Transform the abstract syntax of types and patterns into
      the internal form *)
12
13
14

open Location
open Ast
15
open Ident
16

17
module S = struct type t = string let compare = compare end
18
module StringSet = Set.Make(S)
19
20
module TypeEnv = Map.Make(S)
module Env = Map.Make(Ident.Id)
21

22

23
24
exception NonExhaustive of Types.descr
exception Constraint of Types.descr * Types.descr * string
25
exception ShouldHave of Types.descr * string
26
exception WrongLabel of Types.descr * label
27
exception UnboundId of string
28
29

let raise_loc loc exn = raise (Location (loc,exn))
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

(* Note:
   Compilation of Regexp is implemented as a ``rewriting'' of
   the parsed syntax, in order to be able to print its result
   (for debugging for instance)
   
   It would be possible (and a little more efficient) to produce
   directly ti nodes.
*)
    
module Regexp = struct
  let defs = ref []
  let name =
    let c = ref 0 in
    fun () ->
      incr c;
      "#" ^ (string_of_int !c)

  let rec seq_vars accu = function
    | Epsilon | Elem _ -> accu
    | Seq (r1,r2) | Alt (r1,r2) -> seq_vars (seq_vars accu r1) r2
    | Star r | WeakStar r -> seq_vars accu r
52
    | SeqCapture (v,r) -> seq_vars (IdSet.add v accu) r
53

54
55
  let uniq_id = let r = ref 0 in fun () -> incr r; !r

56
57
58
  type flat =  
    | REpsilon 
    | RElem of int * Ast.ppat  (* the int arg is used
59
					    to stop generic comparison *)
60
61
62
63
    | RSeq of flat * flat
    | RAlt of flat * flat
    | RStar of flat
    | RWeakStar of flat
64

65
66
  let re_loc = ref noloc

67
  let rec propagate vars : regexp -> flat = function
68
69
70
71
72
73
    | Epsilon -> REpsilon
    | Elem x -> let p = vars x in RElem (uniq_id (),p)
    | Seq (r1,r2) -> RSeq (propagate vars r1,propagate vars r2)
    | Alt (r1,r2) -> RAlt (propagate vars r1, propagate vars r2)
    | Star r -> RStar (propagate vars r)
    | WeakStar r -> RWeakStar (propagate vars r)
74
    | SeqCapture (v,x) -> 
75
76
	let v= mk_loc !re_loc (Capture v) in
	propagate (fun p -> mk_loc !re_loc (And (vars p,v))) x
77

78
  let dummy_pat = mknoloc (PatVar "DUMMY")
79
80
81
  let cup r1 r2 =
    if r1 == dummy_pat then r2 else
      if r2 == dummy_pat then r1 else
82
	mk_loc !re_loc (Or (r1,r2))
83

84
85
86
87
88
89
90
(*TODO: review this compilation schema to avoid explosion when
  coding (Optional x) by  (Or(Epsilon,x)); memoization ... *)

  module Memo = Map.Make(struct type t = flat list let compare = compare end)
  module Coind = Set.Make(struct type t = flat list let compare = compare end)
  let memo = ref Memo.empty

91

92
93
  let rec compile fin e seq : Ast.ppat = 
    if Coind.mem seq !e then dummy_pat
94
    else (
95
      e := Coind.add seq !e;
96
97
      match seq with
	| [] ->
98
99
	    fin
	| REpsilon :: rest -> 
100
	    compile fin e rest
101
	| RElem (_,p) :: rest -> 
102
	    mk_loc !re_loc (Prod (p, guard_compile fin rest))
103
	| RSeq (r1,r2) :: rest -> 
104
	    compile fin e (r1 :: r2 :: rest)
105
	| RAlt (r1,r2) :: rest -> 
106
	    cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
107
	| RStar r :: rest -> 
108
	    cup (compile fin e (r::seq)) (compile fin e rest) 
109
	| RWeakStar r :: rest -> 
110
111
	    cup (compile fin e rest) (compile fin e (r::seq))
    )
112
  and guard_compile fin seq =
113
    try Memo.find seq !memo
114
115
116
    with
	Not_found ->
          let n = name () in
117
	  let v = mk_loc !re_loc (PatVar n) in
118
119
          memo := Memo.add seq v !memo;
	  let d = compile fin (ref Coind.empty) seq in
120
121
	  assert (d != dummy_pat);
	  defs := (n,d) :: !defs;
122
123
	  v

124
  let constant_nil t v = 
125
126
    mk_loc !re_loc 
      (And (t, (mk_loc !re_loc (Constant (v, Types.Atom Sequence.nil_atom)))))
127

128
129
  let compile loc regexp queue : ppat =
    re_loc := loc;
130
131
    let vars = seq_vars IdSet.empty regexp in
    let fin = IdSet.fold constant_nil queue vars in
132
133
    let re = propagate (fun p -> p) regexp in
    let n = guard_compile fin [re] in
134
    memo := Memo.empty; 
135
136
    let d = !defs in
    defs := [];
137
    mk_loc !re_loc (Recurs (n,d))
138

139
(*
140
141
142
143
144
145
146
147
148
149
  module H = Hashtbl.Make(
    struct
      type t = Ast.regexp * Ast.ppat
      let equal (r1,p1) (r2,p2) = 
	(Ast.equal_regexp r1 r2) &&
	(Ast.equal_ppat p1 p2)
      let hash (r,p) = 
	(Ast.hash_regexp r) + 16637 * (Ast.hash_ppat p)
    end)
  let hash = H.create 67
150

151
152
153
154
155
156
157
158
159
160
  let compile loc regexp queue : ppat =
    try 
      let c = H.find hash (regexp,queue) in
(*      Printf.eprintf "regexp cached\n"; flush stderr; *)
      c
    with
	Not_found ->
	  let c = compile loc regexp queue in
	  H.add hash (regexp,queue) c;
	  c
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
*)
end

(*
type descr = 
  | IType of Types.descr
  | IOr of descr * descr
  | IAnd of descr * descr
  | IDiff of descr * descr
  | ITimes of slot * slot
  | IXml of slot * slot
  | IArrow of slot * slot
  | IOptional of slot descr
  | IRecord of bool * slot label_map
  | ICapture of id
  | IConstant of id * Types.const
and slot = {
  mutable fv : fv option;
  mutable hash : int option;
  mutable rank1: int; mutable rank2: int;
  mutable gen1 : int; mutable gen2: int;
  mutable d    : slot descr option
}
    
let descr s = 
  match s.d with
    | Some d -> d
    | None -> assert false
	
let gen = ref 0
let rank = ref 0
	     
let rec hash_descr = function
  | IType x -> Types.hash_descr x
  | IOr (d1,d2) -> 1 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
  | IAnd (d1,d2) -> 2 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
  | IDiff (d1,d2) -> 3 + 17 * (hash_descr d1) + 257 * (hash_descr d2)
  | IOptional d -> 4 + 17 * (hash_descr d)
  | ITimes (s1,s2) -> 5 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
  | IXml (s1,s2) -> 6 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
  | IArrow (s1,s2) -> 7 + 17 * (hash_slot s1) + 257 * (hash_slot s2)
  | IRecord (o,r) -> (if o then 8 else 9) + 17 * (LabelMap.hash hash_slot r)
  | ICapture x -> 10 + 17 * (Id.hash x)
  | IConstant (x,y) -> 11 + 17 * (Id.hash x) + 257 * (Types.hash_const y)
and hash_slot s =
  if s.gen1 = !gen then 13 * s.rank1
  else (
    incr rank;
    s.rank1 <- !rank; s.gen1 <- !gen;
    hash_descr (descr s)
  )
    
let rec equal_descr d1 d2 = 
  (d1 == d2) ||
  match (d1,d2) with
  | IType x1, IType x2 -> Types.equal_descr x1 x2
  | IOr (x1,y1), IOr (x2,y2) 
  | IAnd (x1,y1), IAnd (x2,y2) 
  | IDiff (x1,y1), IDiff (x2,y2) -> (equal_descr x1 x2) && (equal_descr y1 y2)
  | IOptional x1, IOptional x2 -> equal_descr x1 x2
  | ITimes (x1,y1), ITimes (x2,y2) 
  | IXml (x1,y1), IXml (x2,y2) 
  | IArrow (x1,y1), IArrow (x2,y2) -> (equal_slot x1 x2) && (equal_slot y1 y2)
  | IRecord (o1,r1), IRecord (o2,r2) -> (o1 = o2) && (LabelMap.equal equal_slot r1 r2)
  | ICapture x1, ICapture x2 -> Id.equal x1 x2
  | IConstant (x1,y1), IConstant (x2,y2) -> (Id.equal x1 x2) && (Types.equal_const y1 y2)
  | _ -> false
and equal_slot s1 s2 =
  ((s1.gen1 = !gen) && (s2.gen2 = !gen) && (s1.rank1 = s2.rank2))
  ||
  ((s1.gen1 <> !gen) && (s2.gen2 <> !gen) && (
     incr rank;
     s1.rank1 <- !rank; s1.gen1 <- !gen;
     s2.rank2 <- !rank; s2.gen2 <- !gen;
     equal_descr (descr s1) (descr s2)
   ))
  
module Arg = struct
  type t = slot
      
  let hash s =
    match s.hash with
      | Some h -> h
      | None ->
	  incr gen; rank := 0; 
	  let h = hash_slot s in
	  s.hash <- Some h;
	  h
	    
  let equal s1 s2 = incr gen; rank := 0; equal_slot s1 s2
251
end
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
module SlotTable = Hashtbl.Make(Arg)
  
let rec fv_slot s =
  match s.fv with
    | Some x -> x
    | None ->
	if s.gen1 = !gen then IdSet.empty 
	else (s.gen1 <- !gen; fv_descr (descr s))
and fv_descr = function
  | IOr (d1,d2)
  | IAnd (d1,d2)  
  | IDiff (d1,d2) -> IdSet.cup (fv_descr d1) (fv_descr d2)
  | IOptional d -> fv_descr d
  | ITimes (s1,s2)  
  | IXml (s1,s2)  
  | IArrow (s1,s2) -> IdSet.cup (fv_slot s1) (fv_slot s2)
  | IRecord (o,r) -> List.fold_left IdSet.cup IdSet.empty (LabelMap.map_to_list fv_slot r)
  | ICapture x | IConstant (x,_) -> IdSet.singleton x
  | _ -> IdSet.empty
      
let compute_fv s =
  match s.fv with
    | Some x -> ()
    | None ->
	incr gen;
	let x = fv_slot s in
	s.fv <- Some x
	  
	  
let counter = ref 0
let todo = ref []
let rec flush_fv () =
  List.iter compute_fv !todo;
  todo := []
    
let mk () =   
  let s = 
    { d = None;
      fv = None;
      hash = None;
      rank1 = 0; rank2 = 0;
      gen1 = 0; gen2 = 0 } in
  todo := s :: !todo;
  s
    
let compile_hash = Ast.PpatTable.create 65
let defs = ref []
let rec compile env { loc = loc; descr = d } = 
  match (d : Ast.ppat') with
    | PatVar v -> 
	let (d,loop) = 
	  try TypeEnv.find v env
	  with Not_found -> 
	    raise_loc_generic loc ("Undefined type variable " ^ v) in
	if !loop then 
	  raise_loc_generic loc ("Unguarded recursion on type/pattern variable " ^ v);
	loop := true;
	let r = compile env d in
	loop := false;
	r
    | Recurs (t, b) -> compile (compile_many env b) t
    | Regexp (r,q) -> compile env (Regexp.compile loc r q)
    | Internal t -> IType t
    | Or (t1,t2) -> IOr (compile env t1, compile env t2)
    | And (t1,t2) -> IAnd (compile env t1, compile env t2)
    | Diff (t1,t2) -> IDiff (compile env t1, compile env t2)
    | Prod (t1,t2) -> ITimes (compile_slot env t1, compile_slot env t2)
    | XmlT (t1,t2) -> IXml (compile_slot env t1, compile_slot env t2)
    | Arrow (t1,t2) -> IArrow (compile_slot env t1, compile_slot env t2)
    | Optional t -> IOptional (compile env t)
    | Record (o,r) ->  IRecord (o, LabelMap.map (compile_slot env) r)
    | Constant (x,v) -> IConstant (x,v)
    | Capture x -> ICapture x
and compile_slot env ({ loc = loc; descr = d } as p) =
  try Ast.PpatTable.find compile_hash p
  with Not_found ->
    let s = mk () in
    defs := (s,p,env) :: !defs;
    Ast.PpatTable.add compile_hash p s;
    s
      
and compile_many env b = 
  List.fold_left (fun env (v,p) -> TypeEnv.add v (p,ref false) env) env b
    
let rec flush_defs () = 
  match !defs with
    | [] -> ()
    | (s,p,env)::t -> defs := t; s.d <- Some (compile env p); flush_defs ()
	
let typ_nodes = SlotTable.create 67
let pat_nodes = SlotTable.create 67
		  
let rec typ = function
  | IType t -> t
  | IOr (s1,s2) -> Types.cup (typ s1) (typ s2)
  | IAnd (s1,s2) ->  Types.cap (typ s1) (typ s2)
  | IDiff (s1,s2) -> Types.diff (typ s1) (typ s2)
  | ITimes (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
  | IXml (s1,s2) -> Types.xml (typ_node s1) (typ_node s2)
  | IArrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
  | IOptional s -> Types.Record.or_absent (typ s)
  | IRecord (o,r) -> Types.record' (o, LabelMap.map typ_node r)
  | ICapture x | IConstant (x,_) -> assert false
      
and typ_node s : Types.node =
  try SlotTable.find typ_nodes s
  with Not_found ->
    let x = Types.make () in
    SlotTable.add typ_nodes s x;
    Types.define x (typ (descr s));
    x
      
let rec pat d : Patterns.descr =
  if IdSet.is_empty (fv_descr d)
  then Patterns.constr (typ d)
  else pat_aux d
    
    
and pat_aux = function
  | IOr (s1,s2) -> Patterns.cup (pat s1) (pat s2)
  | IAnd (s1,s2) -> Patterns.cap (pat s1) (pat s2)
  | IDiff (s1,s2) when IdSet.is_empty (fv_descr s2) ->
      let s2 = Types.neg (typ s2) in
      Patterns.cap (pat s1) (Patterns.constr s2)
  | IDiff _ ->
      raise (Patterns.Error "Difference not allowed in patterns")
  | ITimes (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
  | IXml (s1,s2) -> Patterns.xml (pat_node s1) (pat_node s2)
  | IOptional _ -> 
      raise (Patterns.Error "Optional field not allowed in record patterns")
  | IRecord (o,r) ->
      let pats = ref [] in
      let aux l s = 
	if IdSet.is_empty (fv_slot s) then typ_node s
	else
	  ( pats := Patterns.record l (pat_node s) :: !pats;
	    Types.any_node )
      in
      let constr = Types.record' (o,LabelMap.mapi aux r) in
      List.fold_left Patterns.cap (Patterns.constr constr) !pats
	(* TODO: can avoid constr when o=true, and all fields have fv *)
  | ICapture x -> Patterns.capture x
  | IConstant (x,c) -> Patterns.constant x c
  | IArrow _ ->
      raise (Patterns.Error "Arrow not allowed in patterns")
  | IType _ -> assert false
      
and pat_node s : Patterns.node =
  try SlotTable.find pat_nodes s
  with Not_found ->
    let x = Patterns.make (fv_slot s) in
    SlotTable.add pat_nodes s x;
    Patterns.define x (pat (descr s));
    x
      
let glb = State.ref "Typer.glb_env" TypeEnv.empty
	    
	    
let register_global_types b =
  List.iter (fun (v, { loc = loc }) ->
	       if TypeEnv.mem v !glb
	       then raise_loc_generic loc ("Multiple definition for type " ^ v)
	    ) b;
  glb := compile_many !glb b;
  let b = List.map (fun (v,p) -> (v,p,compile !glb p)) b in
  flush_defs ();
  flush_fv ();
  List.iter (fun (v,p,s) -> 
	       if not (IdSet.is_empty (fv_descr s)) then
		 raise_loc_generic p.loc "Capture variables are not allowed in types";
	       Types.Print.register_global v (typ s)) b
    
    
let typ p =
  let s = compile_slot !glb p in
  flush_defs ();
  flush_fv ();
  if IdSet.is_empty (fv_slot s) then typ_node s
  else raise_loc_generic p.loc "Capture variables are not allowed in types"
    
let pat p = 
  let s = compile_slot !glb p in
  flush_defs ();
  flush_fv ();
  try pat_node s
  with Patterns.Error e -> raise_loc_generic p.loc e
    | Location (loc,exn) when loc = noloc -> raise (Location (p.loc, exn))
	
	
*)
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
(* Internal representation as a graph (desugar recursive types and regexp),
   to compute freevars, etc... *)

type ti = {
  id : int; 
  mutable seen : bool;
  mutable loc' : loc;
  mutable fv : fv option; 
  mutable descr': descr;
  mutable type_node: Types.node option;
  mutable pat_node: Patterns.node option
} 
and descr =
  | IAlias of string * ti
  | IType of Types.descr
  | IOr of ti * ti
  | IAnd of ti * ti
  | IDiff of ti * ti
  | ITimes of ti * ti
  | IXml of ti * ti
  | IArrow of ti * ti
  | IOptional of ti
  | IRecord of bool * ti label_map
  | ICapture of id
  | IConstant of id * Types.const



type glb = ti TypeEnv.t

let mk' =
  let counter = ref 0 in
  fun loc ->
    incr counter;
    let rec x = { 
      id = !counter; 
      seen = false;
      loc' = loc; 
      fv = None; 
      descr' = IAlias ("__dummy__", x);
      type_node = None; 
      pat_node = None 
    } in
    x

let cons loc d =
  let x = mk' loc in
  x.descr' <- d;
  x
    
493
494
495
496
497


let rec compile env { loc = loc; descr = d } : ti = 
  match (d : Ast.ppat') with
  | PatVar s -> 
498
      (try TypeEnv.find s env
499
       with Not_found -> 
500
	 raise_loc_generic loc ("Undefined type variable " ^ s)
501
      )
502
  | Recurs (t, b) -> compile (compile_many env b) t
503
  | Regexp (r,q) -> compile env (Regexp.compile loc r q)
504
505
506
507
508
509
510
  | Internal t -> cons loc (IType t)
  | Or (t1,t2) -> cons loc (IOr (compile env t1, compile env t2))
  | And (t1,t2) -> cons loc (IAnd (compile env t1, compile env t2))
  | Diff (t1,t2) -> cons loc (IDiff (compile env t1, compile env t2))
  | Prod (t1,t2) -> cons loc (ITimes (compile env t1, compile env t2))
  | XmlT (t1,t2) -> cons loc (IXml (compile env t1, compile env t2))
  | Arrow (t1,t2) -> cons loc (IArrow (compile env t1, compile env t2))
511
  | Optional t -> cons loc (IOptional (compile env t))
512
  | Record (o,r) ->  cons loc (IRecord (o, LabelMap.map (compile env) r))
513
514
  | Constant (x,v) -> cons loc (IConstant (x,v))
  | Capture x -> cons loc (ICapture x)
515

516
517
518
and compile_many env b = 
  let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
  let env = 
519
    List.fold_left (fun env (v,t,x) -> TypeEnv.add v x env) env b in
520
  List.iter (fun (v,t,x) -> x.descr' <- IAlias (v, compile env t)) b;
521
522
  env

523
524
525
module IntSet = 
  Set.Make(struct type t = int let compare (x:int) y = compare x y end)

526
let comp_fv_seen = ref []
527
let comp_fv_res = ref IdSet.empty
528
let rec comp_fv s =
529
  match s.fv with
530
    | Some fv -> comp_fv_res := IdSet.cup fv !comp_fv_res
531
532
    | None ->
	(match s.descr' with
533
	   | IAlias (_,x) -> 
534
	       if x.seen then ()
535
	       else ( 
536
537
		 x.seen <- true;
		 comp_fv_seen := x :: !comp_fv_seen; 
538
539
		 comp_fv x
	       ) 
540
541
542
543
544
	   | IOr (s1,s2) 
	   | IAnd (s1,s2)
	   | IDiff (s1,s2)
	   | ITimes (s1,s2) | IXml (s1,s2)
	   | IArrow (s1,s2) -> comp_fv s1; comp_fv s2
545
	   | IOptional r -> comp_fv r
546
	   | IRecord (_,r) -> LabelMap.iter comp_fv r
547
548
	   | IType _ -> ()
	   | ICapture x
549
	   | IConstant (x,_) -> comp_fv_res := IdSet.add x !comp_fv_res
550
	)
551
552
553


let fv s =   
554
555
  match s.fv with
    | Some l -> l
556
557
    | None -> 
	comp_fv s;
558
	let l = !comp_fv_res in
559
	comp_fv_res := IdSet.empty;
560
561
	List.iter (fun n -> n.seen <- false) !comp_fv_seen;
	comp_fv_seen := [];
562
	s.fv <- Some l; 
563
564
565
566
	l

let rec typ seen s : Types.descr =
  match s.descr' with
567
    | IAlias (v,x) ->
568
	if IntSet.mem s.id seen then 
569
570
	  raise_loc_generic s.loc' 
	    ("Unguarded recursion on variable " ^ v ^ " in this type")
571
	else typ (IntSet.add s.id seen) x
572
573
574
575
576
577
578
    | IType t -> t
    | IOr (s1,s2) -> Types.cup (typ seen s1) (typ seen s2)
    | IAnd (s1,s2) ->  Types.cap (typ seen s1) (typ seen s2)
    | IDiff (s1,s2) -> Types.diff (typ seen s1) (typ seen s2)
    | ITimes (s1,s2) ->	Types.times (typ_node s1) (typ_node s2)
    | IXml (s1,s2) ->	Types.xml (typ_node s1) (typ_node s2)
    | IArrow (s1,s2) ->	Types.arrow (typ_node s1) (typ_node s2)
579
    | IOptional s -> Types.Record.or_absent (typ seen s)
580
    | IRecord (o,r) -> 
581
	Types.record' 
582
	  (o, LabelMap.map typ_node r)
583
    | ICapture x | IConstant (x,_) -> assert false
584
585
586
587
588
589
590

and typ_node s : Types.node =
  match s.type_node with
    | Some x -> x
    | None ->
	let x = Types.make () in
	s.type_node <- Some x;
591
	let t = typ IntSet.empty s in
592
593
594
	Types.define x t;
	x

595
596
597
let type_node s = 
  let s = typ_node s in
  let s = Types.internalize s in
598
(*  Types.define s (Types.normalize (Types.descr s)); *)
599
  s
600
601

let rec pat seen s : Patterns.descr =
602
  if IdSet.is_empty (fv s) 
603
604
  then Patterns.constr (Types.descr (type_node s)) 
  else
605
606
607
608
609
610
    try pat_aux seen s
    with Patterns.Error e -> raise_loc_generic s.loc' e
      | Location (loc,exn) when loc = noloc -> raise (Location (s.loc', exn))


and pat_aux seen s = match s.descr' with
611
  | IAlias (v,x) ->
612
      if IntSet.mem s.id seen 
613
614
615
      then raise 
	(Patterns.Error
	   ("Unguarded recursion on variable " ^ v ^ " in this pattern"));
616
      pat (IntSet.add s.id seen) x
617
618
  | IOr (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
  | IAnd (s1,s2) -> Patterns.cap (pat seen s1) (pat seen s2)
619
  | IDiff (s1,s2) when IdSet.is_empty (fv s2) ->
620
621
      let s2 = Types.neg (Types.descr (type_node s2)) in
      Patterns.cap (pat seen s1) (Patterns.constr s2)
622
  | IDiff _ ->
623
      raise (Patterns.Error "Difference not allowed in patterns")
624
625
  | ITimes (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
  | IXml (s1,s2) -> Patterns.xml (pat_node s1) (pat_node s2)
626
627
628
629
  | IOptional _ -> 
      raise 
      (Patterns.Error 
	 "Optional field not allowed in record patterns")
630
  | IRecord (o,r) ->
631
      let pats = ref [] in
632
633
      let aux l s = 
	if IdSet.is_empty (fv s) then type_node s
634
	else
635
636
637
638
	  ( pats := Patterns.record l (pat_node s) :: !pats;
	    Types.any_node )
      in
      let constr = Types.record' (o,LabelMap.mapi aux r) in
639
640
      List.fold_left Patterns.cap (Patterns.constr constr) !pats
(* TODO: can avoid constr when o=true, and all fields have fv *)
641
642
643
  | ICapture x ->  Patterns.capture x
  | IConstant (x,c) -> Patterns.constant x c
  | IArrow _ ->
644
      raise (Patterns.Error "Arrow not allowed in patterns")
645
  | IType _ -> assert false
646
647
648
649
650

and pat_node s : Patterns.node =
  match s.pat_node with
    | Some x -> x
    | None ->
651
	let x = Patterns.make (fv s) in
652
	s.pat_node <- Some x;
653
	let t = pat IntSet.empty s in
654
655
656
	Patterns.define x t;
	x

657
let mk_typ e =
658
  if IdSet.is_empty (fv e) then type_node e
659
  else raise_loc_generic e.loc' "Capture variables are not allowed in types"
660
661
    

662
663
664
665
let glb = State.ref "Typer.glb_env" TypeEnv.empty

let typ e =
  mk_typ (compile !glb e)
666

667
668
let pat e =
  pat_node (compile !glb e)
669

670
671
672
673
let register_global_types b =
  let env' = compile_many !glb b in
  List.iter
    (fun (v,{ loc = loc }) -> 
674
       let t = TypeEnv.find v env' in
675
676
677
       let d = Types.descr (mk_typ t) in
       (*	       let d = Types.normalize d in*)
       Types.Print.register_global v d;
678
       if TypeEnv.mem v !glb
679
       then raise_loc_generic loc ("Multiple definition for type " ^ v);
680
681
       glb := TypeEnv.add v t !glb
    ) b
682
683
684



685
686
(* II. Build skeleton *)

687
module Fv = IdSet
688

689
690
691
(* IDEA: introduce a node Loc in the AST to override nolocs
   in sub-expressions *)
   
692
let rec expr loc' { loc = loc; descr = d } = 
693
  let loc =  if loc = noloc then loc' else loc in
694
  let (fv,td) = 
695
    match d with
696
      | Forget (e,t) ->
697
	  let (fv,e) = expr loc e and t = typ t in
698
	  (fv, Typed.Forget (e,t))
699
700
      | Var s -> (Fv.singleton s, Typed.Var s)
      | Apply (e1,e2) -> 
701
	  let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
702
	  (Fv.cup fv1 fv2, Typed.Apply (e1,e2))
703
      | Abstraction a ->
704
	  let iface = List.map (fun (t1,t2) -> (typ t1, typ t2)) 
705
			a.fun_iface in
706
707
708
	  let t = List.fold_left 
		    (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2)) 
		    Types.any iface in
709
710
711
	  let iface = List.map 
			(fun (t1,t2) -> (Types.descr t1, Types.descr t2)) 
			iface in
712
	  let (fv0,body) = branches loc a.fun_body in
713
714
715
716
717
718
719
720
721
	  let fv = match a.fun_name with
	    | None -> fv0
	    | Some f -> Fv.remove f fv0 in
	  (fv,
	   Typed.Abstraction 
	     { Typed.fun_name = a.fun_name;
	       Typed.fun_iface = iface;
	       Typed.fun_body = body;
	       Typed.fun_typ = t;
722
	       Typed.fun_fv = fv
723
724
725
726
	     }
	  )
      | Cst c -> (Fv.empty, Typed.Cst c)
      | Pair (e1,e2) ->
727
	  let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
728
	  (Fv.cup fv1 fv2, Typed.Pair (e1,e2))
729
      | Xml (e1,e2) ->
730
	  let (fv1,e1) = expr loc e1 and (fv2,e2) = expr loc e2 in
731
	  (Fv.cup fv1 fv2, Typed.Xml (e1,e2))
732
      | Dot (e,l) ->
733
	  let (fv,e) = expr loc e in
734
	  (fv,  Typed.Dot (e,l))
735
      | RemoveField (e,l) ->
736
	  let (fv,e) = expr loc e in
737
	  (fv,  Typed.RemoveField (e,l))
738
739
      | RecordLitt r -> 
	  let fv = ref Fv.empty in
740
741
	  let r = LabelMap.map 
		    (fun e -> 
742
		       let (fv2,e) = expr loc e 
743
		       in fv := Fv.cup !fv fv2; e)
744
		    r in
745
	  (!fv, Typed.RecordLitt r)
746
      | Op (op,le) ->
747
	  let (fvs,ltes) = List.split (List.map (expr loc) le) in
748
	  let fv = List.fold_left Fv.cup Fv.empty fvs in
749
	  (fv, Typed.Op (op,ltes))
750
      | Match (e,b) -> 
751
752
	  let (fv1,e) = expr loc e
	  and (fv2,b) = branches loc b in
753
	  (Fv.cup fv1 fv2, Typed.Match (e, b))
754
      | Map (e,b) ->
755
756
	  let (fv1,e) = expr loc e
	  and (fv2,b) = branches loc b in
757
	  (Fv.cup fv1 fv2, Typed.Map (e, b))
758
759
      | Ttree (e,b) ->
	  let b = b @ [ (mknoloc (Internal Types.any)), mknoloc MatchFail ] in
760
761
	  let (fv1,e) = expr loc e
	  and (fv2,b) = branches loc b in
762
763
	  (Fv.cup fv1 fv2, Typed.Ttree (e, b))
      | MatchFail -> (Fv.empty, Typed.MatchFail)
764
      | Try (e,b) ->
765
766
	  let (fv1,e) = expr loc e
	  and (fv2,b) = branches loc b in
767
	  (Fv.cup fv1 fv2, Typed.Try (e, b))
768
  in
769
770
  fv,
  { Typed.exp_loc = loc;
771
772
773
774
    Typed.exp_typ = Types.empty;
    Typed.exp_descr = td;
  }
	      
775
  and branches loc b = 
776
    let fv = ref Fv.empty in
777
    let accept = ref Types.empty in
778
779
    let b = List.map 
	      (fun (p,e) ->
780
781
		 let (fv2,e) = expr loc e in
		 let p = pat p in
782
783
		 let fv2 = Fv.diff fv2 (Patterns.fv p) in
		 fv := Fv.cup !fv fv2;
784
		 accept := Types.cup !accept (Types.descr (Patterns.accept p));
785
		 { Typed.br_used = false;
786
		   Typed.br_pat = p;
787
788
		   Typed.br_body = e }
	      ) b in
789
790
791
792
    (!fv, 
     { 
       Typed.br_typ = Types.empty; 
       Typed.br_branches = b; 
793
794
       Typed.br_accept = !accept;
       Typed.br_compiled = None;
795
796
     } 
    )
797

798
799
let expr = expr noloc

800
801
802
let let_decl p e =
  let (_,e) = expr e in
  { Typed.let_pat = pat p;
803
804
805
806
807
    Typed.let_body = e;
    Typed.let_compiled = None }

(* III. Type-checks *)

808
809
810
let int_cup_record = Types.cup Types.Int.any Types.Record.any


811
type env = Types.descr Env.t
812

813
814
let match_fail = ref Types.empty

815
816
open Typed

817
let warning loc msg =
818
819
820
821
  Format.fprintf !Location.warning_ppf "Warning %a:@\n%a%s@\n" 
    Location.print_loc loc
    Location.html_hilight loc
    msg
822
823
824
825

let check loc t s msg =
  if not (Types.subtype t s) then raise_loc loc (Constraint (t, s, msg))

826
let rec type_check env e constr precise = 
827
(*  Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
828
829
    Types.Print.print_descr constr precise; 
*)
830
  let d = type_check' e.exp_loc env e.exp_descr constr precise in
831
832
833
  e.exp_typ <- Types.cup e.exp_typ d;
  d

834
and type_check' loc env e constr precise = match e with
835
836
837
838
  | Forget (e,t) ->
      let t = Types.descr t in
      ignore (type_check env e t false);
      t
839
  | Abstraction a ->
840
841
842
843
844
845
846
      let t =
	try Types.Arrow.check_strenghten a.fun_typ constr 
	with Not_found -> 
	  raise_loc loc 
	  (ShouldHave
	     (constr, "but the interface of the abstraction is not compatible"))
      in
847
848
849
      let env = match a.fun_name with
	| None -> env
	| Some f -> Env.add f a.fun_typ env in
850
851
      List.iter 
	(fun (t1,t2) ->
852
	   ignore (type_check_branches loc env t1 a.fun_body t2 false)
853
854
	) a.fun_iface;
      t
855

856
857
  | Match (e,b) ->
      let t = type_check env e b.br_accept true in
858
      type_check_branches loc env t b constr precise
859
860
861

  | Try (e,b) ->
      let te = type_check env e constr precise in
862
      let tb = type_check_branches loc env Types.any b constr precise in
863
      Types.cup te tb
864

865
866
867
868
  | Pair (e1,e2) ->
      type_check_pair loc env e1 e2 constr precise
  | Xml (e1,e2) ->
      type_check_pair ~kind:`XML loc env e1 e2 constr precise
869

870
  | RecordLitt r ->
871
(* try to get rid of precise = true for values of fields *)
872
      if not (Types.Record.has_record constr) then
873
874
	raise_loc loc (ShouldHave (constr,"but it is a record."));
      let (rconstr,res) = 
875
	List.fold_left
876
	  (fun (rconstr,res) (l,e) ->
877
878
879
	     (* could compute (split l e) once... *)
	     let pi = Types.Record.project_opt rconstr l in
	     if Types.is_empty pi then 
880
881
882
	       raise_loc loc 
		 (ShouldHave (constr,(Printf.sprintf 
					"Field %s is not allowed here."
883
					(LabelPool.value l)
884
885
886
				     )
			     ));
	     let t = type_check env e pi true in
887
888
	     let rconstr = Types.Record.condition rconstr l t in
	     let res = if precise then (l,Types.cons t) :: res else res in
889
	     (rconstr,res)
890
	  ) (constr, []) (LabelMap.get r)
891
      in
892
893
894
895
896
897
      if not (Types.Record.has_empty_record rconstr) then
	raise_loc loc 
	  (ShouldHave (constr,"More field should be present"));
      if precise then
	Types.record' (false, LabelMap.from_list (fun _ _ -> assert false) res)
      else constr
898
899
900
901
902
  | Map (e,b) ->
      let t = type_check env e (Sequence.star b.br_accept) true in

      let constr' = Sequence.approx (Types.cap Sequence.any constr) in
      let exact = Types.subtype (Sequence.star constr') constr in
903
904
905
906
907
908
909
      (* Note: 
	 - could be more precise by integrating the decomposition
	 of constr inside Sequence.map.
      *)
      let res = 
	Sequence.map 
	  (fun t -> 
910
	     type_check_branches loc env t b constr' (precise || (not exact)))
911
912
913
	  t in
      if not exact then check loc res constr "";
      if precise then res else constr
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
  | Op ("@", [e1;e2]) ->
      let constr' = Sequence.star 
		      (Sequence.approx (Types.cap Sequence.any constr)) in
      let exact = Types.subtype constr' constr in
      if exact then
	let t1 = type_check env e1 constr' precise
	and t2 = type_check env e2 constr' precise in
	if precise then Sequence.concat t1 t2 else constr
      else
	(* Note:
	   the knownledge of t1 may makes it useless to
	   check t2 with 'precise' ... *)
	let t1 = type_check env e1 constr' true
	and t2 = type_check env e2 constr' true in
	let res = Sequence.concat t1 t2 in
	check loc res constr "";
	if precise then res else constr
931
  | Apply (e1,e2) ->
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
      let constr' = Sequence.star 
		      (Sequence.approx (Types.cap Sequence.any constr)) in
      let t1 = type_check env e1 (Types.cup Types.Arrow.any constr') true in
      let t1_fun = Types.Arrow.get t1 in

      let has_fun = not (Types.Arrow.is_empty t1_fun)
      and has_seq = not (Types.subtype t1 Types.Arrow.any) in

      let constr' =
	Types.cap 
	  (if has_fun then Types.Arrow.domain t1_fun else Types.any)
	  (if has_seq then constr' else Types.any)
      in
      let need_arg = has_fun && Types.Arrow.need_arg t1_fun in
      let precise  = need_arg || has_seq in
      let t2 = type_check env e2 constr' precise in
      let res = Types.cup 
		  (if has_fun then 
		     if need_arg then Types.Arrow.apply t1_fun t2
		     else Types.Arrow.apply_noarg t1_fun
		   else Types.empty)
		  (if has_seq then Sequence.concat t1 t2
		   else Types.empty)
      in
      check loc res constr "";
      res
959
*)
960
961
962
      let t1 = type_check env e1 Types.Arrow.any true in
      let t1 = Types.Arrow.get t1 in
      let dom = Types.Arrow.domain t1 in
963
964
965
966
967
968
969
970
971
      let res =
	if Types.Arrow.need_arg t1 then
	  let t2 = type_check env e2 dom true in
	  Types.Arrow.apply t1 t2
	else
	  (ignore (type_check env e2 dom false); Types.Arrow.apply_noarg t1)
      in
      check loc res constr "";
      res
972
973
974
975
976
977
978
979
980
981
982
983
984
  | Op ("flatten", [e]) ->
      let constr' = Sequence.star 
		      (Sequence.approx (Types.cap Sequence.any constr)) in
      let sconstr' = Sequence.star constr' in
      let exact = Types.subtype constr' constr in
      if exact then
	let t = type_check env e sconstr' precise in
	if precise then Sequence.flatten t else constr
      else
	let t = type_check env e sconstr' true in
	let res = Sequence.flatten t in
	check loc res constr "";
	if precise then res else constr
985
986
987
988
989
  | _ -> 
      let t : Types.descr = compute_type' loc env e in
      check loc t constr "";
      t

990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
and type_check_pair ?(kind=`Normal) loc env e1 e2 constr precise =
  let rects = Types.Product.get ~kind constr in
  if Types.Product.is_empty rects then 
    (match kind with
      | `Normal -> raise_loc loc (ShouldHave (constr,"but it is a pair."))
      | `XML -> raise_loc loc (ShouldHave (constr,"but it is an XML element.")));
  let pi1 = Types.Product.pi1 rects in
  
  let t1 = type_check env e1 (Types.Product.pi1 rects) 
	     (precise || (Types.Product.need_second rects))in
  let rects = Types.Product.restrict_1 rects t1 in
  let t2 = type_check env e2 (Types.Product.pi2 rects) precise in
  if precise then 
    match kind with
      | `Normal -> Types.times (Types.cons t1) (Types.cons t2)
      | `XML -> Types.xml (Types.cons t1) (Types.cons t2)
  else
    constr


1010
1011
1012
1013
and compute_type env e =
  type_check env e Types.any true

and compute_type' loc env = function
1014
1015
  | Var s -> 
      (try Env.find s env 
1016
       with Not_found -> raise_loc loc (UnboundId (Id.value s))
1017
      )
1018
  | Cst c -> Types.constant c
1019
1020
1021
1022
  | Dot (e,l) ->
      let t = type_check env e Types.Record.any true in
         (try (Types.Record.project t l) 
          with Not_found -> raise_loc loc (WrongLabel(t,l)))
1023
1024
1025
  | RemoveField (e,l) ->
      let t = type_check env e Types.Record.any true in
      Types.Record.remove_field t l
1026
1027
1028
  | Op (op, el) ->
      let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
      type_op loc op args
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
  | Ttree (e,b) ->
      let t = type_check env e Sequence.any true in
      let r = 
	Sequence.map_tree 
	  (fun t -> 
	     let res = type_check_branches loc env t b Sequence.any true in
	     let resid = !match_fail in
	     match_fail := Types.empty;
	     (res,resid)
	  ) t
      in
      r
1041
1042
1043
1044
1045

(* We keep these cases here to allow comparison and benchmarking ...
   Just comment the corresponding cases in type_check' to
   activate these ones.
*)
1046
1047
1048
  | Map (e,b) ->
      let t = compute_type env e in
      Sequence.map (fun t -> type_check_branches loc env t b Types.any true) t
1049
1050
1051
1052
1053
  | Pair (e1,e2) -> 
      let t1 = compute_type env e1 
      and t2 = compute_type env e2 in
      Types.times (Types.cons t1) (Types.cons t2)
  | RecordLitt r ->
1054
      let r = LabelMap.map (fun e -> Types.cons (compute_type env e)) r in
1055
      Types.record' (false,r)
1056
  | _ -> assert false
1057

1058
and type_check_branches loc env targ brs constr precise =
1059
  if Types.is_empty targ then Types.empty 
1060
1061
  else (
    brs.br_typ <- Types.cup brs.br_typ targ;
1062
    branches_aux loc env targ 
1063
1064
      (if precise then Types.empty else constr) 
      constr precise brs.br_branches
1065
  )
1066
    
1067
1068
and branches_aux loc env targ tres constr precise = function
  | [] -> raise_loc loc (NonExhaustive targ)
1069
1070
1071
  | { br_body = { exp_descr = MatchFail } } :: _ ->
      match_fail := Types.cup !match_fail targ;
      tres
1072
1073
1074
1075
1076
1077
  | b :: rem ->
      let p = b.br_pat in
      let acc = Types.descr (Patterns.accept p) in

      let targ' = Types.cap targ acc in
      if Types.is_empty targ' 
1078
      then branches_aux loc env targ tres constr precise rem
1079
1080
1081
1082
1083
1084
      else 
	( b.br_used <- true;
	  let res = Patterns.filter targ' p in
	  let env' = List.fold_left 
		       (fun env (x,t) -> Env.add x (Types.descr t) env) 
		       env res in
1085
1086
	  let t = type_check env' b.br_body constr precise in
	  let tres = if precise then Types.cup t tres else tres in
1087
1088
	  let targ'' = Types.diff targ acc in
	  if (Types.non_empty targ'') then 
1089
	    branches_aux loc env targ'' tres constr precise rem 
1090
1091
	  else
	    tres
1092
	)
1093

1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
and type_let_decl env l =
  let acc = Types.descr (Patterns.accept l.let_pat) in
  let t = type_check env l.let_body acc true in
  let res = Patterns.filter t l.let_pat in
  List.map (fun (x,t) -> (x, Types.descr t)) res

and type_rec_funs env l =
  let types = 
    List.fold_left
      (fun accu -> function  {let_body={exp_descr=Abstraction a}} as l ->
	 let t = a.fun_typ in
	 let acc = Types.descr (Patterns.accept l.let_pat) in
	 if not (Types.subtype t acc) then
	   raise_loc l.let_body.exp_loc (NonExhaustive (Types.diff t acc));
	 let res = Patterns.filter t l.let_pat in
	 List.fold_left (fun accu (x,t) -> (x, Types.descr t)::accu) accu res
	 | _ -> assert false) [] l
  in
  let env' = List.fold_left (fun env (x,t) -> Env.add x t env) env types in
  List.iter 
    (function  { let_body = { exp_descr = Abstraction a } } as l ->
       ignore (type_check env' l.let_body Types.any false)
       | _ -> assert false) l;
  types


1120
1121
and type_op loc op args =
  match (op,args) with
1122
    | "+", [loc1,t1; loc2,t2] ->
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
	check loc1 t1 int_cup_record
	"The first argument of + must be an integer or a record";
	let int = Types.Int.get t1 in
	let int = if Intervals.is_empty int then None else Some int in
	let r = if Types.Record.has_record t1 then Some t1 else None in
	(match (int,r) with
	   | Some t1, None ->
	       if not (Types.Int.is_int t2) then
		 raise_loc loc2
		   (Constraint 
		      (t2,Types.Int.any,
		       "The second argument of + must be an integer"));
	       Types.Int.put
		 (Intervals.add t1 (Types.Int.get t2));
	   | None, Some r1 ->
	       check loc2 t2 Types.Record.any 
	       "The second argument of + must be a record";
	       Types.Record.merge r1 t2
	   | None, None ->
	       Types.empty
	   | Some t1, Some r1 ->
	       check loc2 t2 int_cup_record
	       "The second argument of + must be an integer or a record";
	       Types.cup 
		 (Types.Int.put (Intervals.add t1 (Types.Int.get t2)))
		 (Types.Record.merge r1 t2)
	)
1150
1151
    | "-", [loc1,t1; loc2,t2] ->
	type_int_binop Intervals.sub loc1 t1 loc2 t2
1152
    | ("*" | "/" | "mod"), [loc1,t1; loc2,t2] ->
1153
	type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
1154
    | "@", [loc1,t1; loc2,t2] ->
1155
1156
1157
	check loc1 t1 Sequence.any
	  "The first argument of @ must be a sequence";
	Sequence.concat t1 t2
1158
    | "flatten", [loc1,t1] ->
1159
1160
1161
	check loc1 t1 Sequence.seqseq 
	  "The argument of flatten must be a sequence of sequences";
	Sequence.flatten t1
1162
1163
1164
1165
    | "load_xml", [loc1,t1] ->
	check loc1 t1 Sequence.string
	  "The argument of load_xml must be a string (filename)";
	Types.any
1166
1167
1168
1169
    | "load_file", [loc1,t1] ->
	check loc1 t1 Sequence.string
	  "The argument of load_file must be a string (filename)";
	Sequence.string
1170
1171
1172
1173
    | "load_html", [loc1,t1] ->
	check loc1 t1 Sequence.string
	  "The argument of load_html must be a string (filename)";
	Types.any
1174
1175
    | "raise", [loc1,t1] ->
	Types.empty