typer.ml 27.1 KB
Newer Older
1
2
(* I. Transform the abstract syntax of types and patterns into
      the internal form *)
3
4
5

open Location
open Ast
6
open Ident
7

8
module S = struct type t = string let compare = compare end
9
10
11
module TypeEnv = Map.Make(S)
module Env = Map.Make(Ident.Id)
(*
12
module StringSet = Set.Make(S)
13
*)
14

15
exception NonExhaustive of Types.descr
16
exception MultipleLabel of Types.label
17
exception Constraint of Types.descr * Types.descr * string
18
exception ShouldHave of Types.descr * string
19
exception WrongLabel of Types.descr * Types.label
20
exception UnboundId of string
21
22

let raise_loc loc exn = raise (Location (loc,exn))
23
24
25
26

(* Internal representation as a graph (desugar recursive types and regexp),
   to compute freevars, etc... *)

27
type ti = {
28
  id : int; 
29
  mutable seen : bool;
30
  mutable loc' : loc;
31
  mutable fv : fv option; 
32
33
34
35
36
  mutable descr': descr;
  mutable type_node: Types.node option;
  mutable pat_node: Patterns.node option
} 
and descr =
37
38
39
40
41
42
43
44
  | 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
45
46
  | IOptional of ti
  | IRecord of bool * (Types.label * ti) list
47
48
  | ICapture of id
  | IConstant of id * Types.const
49
50
    

51
type glb = ti TypeEnv.t
52

53
54
let mk' =
  let counter = ref 0 in
55
  fun loc ->
56
    incr counter;
57
58
    let rec x = { 
      id = !counter; 
59
      seen = false;
60
      loc' = loc; 
61
      fv = None; 
62
      descr' = IAlias ("__dummy__", x);
63
64
65
      type_node = None; 
      pat_node = None 
    } in
66
67
68
    x

let cons loc d =
69
  let x = mk' loc in
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  x.descr' <- d;
  x
    
(* 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
94
    | SeqCapture (v,r) -> seq_vars (IdSet.add v accu) r
95

96
97
  let uniq_id = let r = ref 0 in fun () -> incr r; !r

98
99
100
  type flat =  
    | REpsilon 
    | RElem of int * Ast.ppat  (* the int arg is used
101
					    to stop generic comparison *)
102
103
104
105
    | RSeq of flat * flat
    | RAlt of flat * flat
    | RStar of flat
    | RWeakStar of flat
106

107
108
  let re_loc = ref noloc

109
  let rec propagate vars : regexp -> flat = function
110
111
112
113
114
115
    | 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)
116
    | SeqCapture (v,x) -> 
117
	let v= mk !re_loc (Capture v) in
118
	propagate (fun p -> mk !re_loc (And (vars p,v))) x
119

120
121
122
123
124
  let dummy_pat = mk noloc (PatVar "DUMMY")
  let cup r1 r2 =
    if r1 == dummy_pat then r2 else
      if r2 == dummy_pat then r1 else
	mk !re_loc (Or (r1,r2))
125

126
127
128
129
130
131
132
(*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

133

134
135
  let rec compile fin e seq : Ast.ppat = 
    if Coind.mem seq !e then dummy_pat
136
    else (
137
      e := Coind.add seq !e;
138
139
      match seq with
	| [] ->
140
141
	    fin
	| REpsilon :: rest -> 
142
	    compile fin e rest
143
144
145
	| RElem (_,p) :: rest -> 
	    mk !re_loc (Prod (p, guard_compile fin rest))
	| RSeq (r1,r2) :: rest -> 
146
	    compile fin e (r1 :: r2 :: rest)
147
	| RAlt (r1,r2) :: rest -> 
148
	    cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
149
	| RStar r :: rest -> 
150
	    cup (compile fin e (r::seq)) (compile fin e rest) 
151
	| RWeakStar r :: rest -> 
152
153
	    cup (compile fin e rest) (compile fin e (r::seq))
    )
154
  and guard_compile fin seq =
155
    try Memo.find seq !memo
156
157
158
    with
	Not_found ->
          let n = name () in
159
	  let v = mk !re_loc (PatVar n) in
160
161
          memo := Memo.add seq v !memo;
	  let d = compile fin (ref Coind.empty) seq in
162
163
	  assert (d != dummy_pat);
	  defs := (n,d) :: !defs;
164
165
	  v

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
(*
  type trans = [ `Alt of gnode * gnode | `Elem of Ast.ppat * gnode | `Final ]
  and gnode = 
      {
	mutable seen  : bool;
	mutable compile : bool;
	name  : string;
	mutable trans : trans;
      }

  let new_node() = { seen = false; compile = false; 
		     name = name(); trans = `Final }
  let to_compile = ref []

  let rec compile after = function
    | `Epsilon -> after
    | `Elem (_,p) -> 
	if not after.compile then (after.compile <- true; 
				   to_compile := after :: !to_compile);
	{ new_node () with trans = `Elem (p, after)  }
    | `Seq(r1,r2) -> compile (compile after r2) r1
    | `Alt(r1,r2) ->
	let r1 = compile after r1 and r2 = compile after r2 in
	{ new_node () with trans = `Alt (r1,r2) }
    | `Star r ->
	let n  = new_node() in
	n.trans <- `Alt (compile n r, after);
	n
    | `WeakStar r ->
	let n  = new_node() in
	n.trans <- `Alt (after, compile n r);
	n

  let seens = ref []	
  let rec collect_aux accu n =
    if n.seen then accu 
    else ( seens := n :: !seens;
	   match n.trans with
	     | `Alt (n1,n2) -> collect_aux (collect_aux accu n2) n1
	     | _ -> n :: accu
	 )

  let collect fin n =
    let l = collect_aux [] n in
    List.iter (fun n -> n.seen <- false) !seens;
    let l = List.map (fun n ->
			match n.trans with
			  | `Final -> fin
			  | `Elem (p,a) -> 
			      mk !re_loc (Prod(p, mk !re_loc (PatVar a.name)))
			  | _ -> assert false
		     ) l in
    match l with
      | h::t ->
	  List.fold_left (fun accu p -> mk !re_loc (Or (accu,p))) h t
      | _ -> assert false
*)    
	
224

225
  let constant_nil t v = 
226
    mk !re_loc 
227
      (And (t, (mk !re_loc (Constant (v, Types.Atom Sequence.nil_atom)))))
228

229
230
  let compile loc regexp queue : ppat =
    re_loc := loc;
231
232
    let vars = seq_vars IdSet.empty regexp in
    let fin = IdSet.fold constant_nil queue vars in
233
234
    let re = propagate (fun p -> p) regexp in
    let n = guard_compile fin [re] in
235
    memo := Memo.empty; 
236
237
    let d = !defs in
    defs := [];
238
239
240
241
242
243
244
245

(*
    let after = new_node() in
    let n = collect queue (compile after re) in
    let d = List.map (fun n -> (n.name, collect queue n)) !to_compile in
    to_compile := [];
*)

246
    mk !re_loc (Recurs (n,d))
247
248
end

249
let compile_regexp = Regexp.compile noloc
250
251
252
253
254


let rec compile env { loc = loc; descr = d } : ti = 
  match (d : Ast.ppat') with
  | PatVar s -> 
255
      (try TypeEnv.find s env
256
       with Not_found -> 
257
	 raise_loc_generic loc ("Undefined type variable " ^ s)
258
      )
259
  | Recurs (t, b) -> compile (compile_many env b) t
260
  | Regexp (r,q) -> compile env (Regexp.compile loc r q)
261
262
263
264
265
266
267
  | 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))
268
  | Optional t -> cons loc (IOptional (compile env t))
269
  | Record (o,r) -> 
270
      cons loc (IRecord (o, List.map (fun (l,t) -> l,compile env t) r))
271
272
  | Constant (x,v) -> cons loc (IConstant (x,v))
  | Capture x -> cons loc (ICapture x)
273

274
275
276
and compile_many env b = 
  let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
  let env = 
277
    List.fold_left (fun env (v,t,x) -> TypeEnv.add v x env) env b in
278
  List.iter (fun (v,t,x) -> x.descr' <- IAlias (v, compile env t)) b;
279
280
  env

281
282
283
module IntSet = 
  Set.Make(struct type t = int let compare (x:int) y = compare x y end)

284
let comp_fv_seen = ref []
285
let comp_fv_res = ref IdSet.empty
286
let rec comp_fv s =
287
  match s.fv with
288
    | Some fv -> comp_fv_res := IdSet.cup fv !comp_fv_res
289
290
    | None ->
	(match s.descr' with
291
	   | IAlias (_,x) -> 
292
	       if x.seen then ()
293
	       else ( 
294
295
		 x.seen <- true;
		 comp_fv_seen := x :: !comp_fv_seen; 
296
297
		 comp_fv x
	       ) 
298
299
300
301
302
	   | IOr (s1,s2) 
	   | IAnd (s1,s2)
	   | IDiff (s1,s2)
	   | ITimes (s1,s2) | IXml (s1,s2)
	   | IArrow (s1,s2) -> comp_fv s1; comp_fv s2
303
304
	   | IOptional r -> comp_fv r
	   | IRecord (_,r) -> List.iter (fun (l,s) -> comp_fv s) r
305
306
	   | IType _ -> ()
	   | ICapture x
307
	   | IConstant (x,_) -> comp_fv_res := IdSet.add x !comp_fv_res
308
	)
309
310
311


let fv s =   
312
313
  match s.fv with
    | Some l -> l
314
315
    | None -> 
	comp_fv s;
316
	let l = !comp_fv_res in
317
	comp_fv_res := IdSet.empty;
318
319
	List.iter (fun n -> n.seen <- false) !comp_fv_seen;
	comp_fv_seen := [];
320
	s.fv <- Some l; 
321
322
323
324
	l

let rec typ seen s : Types.descr =
  match s.descr' with
325
    | IAlias (v,x) ->
326
	if IntSet.mem s.id seen then 
327
328
	  raise_loc_generic s.loc' 
	    ("Unguarded recursion on variable " ^ v ^ " in this type")
329
	else typ (IntSet.add s.id seen) x
330
331
332
333
334
335
336
    | 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)
337
    | IOptional s -> Types.Record.or_absent (typ seen s)
338
    | IRecord (o,r) -> 
339
	Types.record' 
340
	  (o,List.map (fun (l,s) -> (l,typ_node s)) r)
341
    | ICapture x | IConstant (x,_) -> assert false
342
343
344
345
346
347
348

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;
349
	let t = typ IntSet.empty s in
350
351
352
	Types.define x t;
	x

353
354
355
let type_node s = 
  let s = typ_node s in
  let s = Types.internalize s in
356
(*  Types.define s (Types.normalize (Types.descr s)); *)
357
  s
358
359

let rec pat seen s : Patterns.descr =
360
  if IdSet.is_empty (fv s) 
361
362
  then Patterns.constr (Types.descr (type_node s)) 
  else
363
364
365
366
367
368
    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
369
  | IAlias (v,x) ->
370
      if IntSet.mem s.id seen 
371
372
373
      then raise 
	(Patterns.Error
	   ("Unguarded recursion on variable " ^ v ^ " in this pattern"));
374
      pat (IntSet.add s.id seen) x
375
376
  | IOr (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
  | IAnd (s1,s2) -> Patterns.cap (pat seen s1) (pat seen s2)
377
  | IDiff (s1,s2) when IdSet.is_empty (fv s2) ->
378
379
      let s2 = Types.neg (Types.descr (type_node s2)) in
      Patterns.cap (pat seen s1) (Patterns.constr s2)
380
  | IDiff _ ->
381
      raise (Patterns.Error "Difference not allowed in patterns")
382
383
  | ITimes (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
  | IXml (s1,s2) -> Patterns.xml (pat_node s1) (pat_node s2)
384
385
386
387
  | IOptional _ -> 
      raise 
      (Patterns.Error 
	 "Optional field not allowed in record patterns")
388
  | IRecord (o,r) ->
389
      let pats = ref [] in
390
391
      let aux (l,s) = 
	if IdSet.is_empty (fv s) then (l,type_node s)
392
	else
393
	  ( 
394
	    pats := Patterns.record l (pat_node s) :: !pats;
395
	    (l,Types.any_node)
396
397
398
399
	  ) in
      let constr = Types.record' (o,List.map aux r) in
      List.fold_left Patterns.cap (Patterns.constr constr) !pats
(* TODO: can avoid constr when o=true, and all fields have fv *)
400
401
402
  | ICapture x ->  Patterns.capture x
  | IConstant (x,c) -> Patterns.constant x c
  | IArrow _ ->
403
      raise (Patterns.Error "Arrow not allowed in patterns")
404
  | IType _ -> assert false
405
406
407
408
409

and pat_node s : Patterns.node =
  match s.pat_node with
    | Some x -> x
    | None ->
410
	let x = Patterns.make (fv s) in
411
	s.pat_node <- Some x;
412
	let t = pat IntSet.empty s in
413
414
415
	Patterns.define x t;
	x

416
let mk_typ e =
417
  if IdSet.is_empty (fv e) then type_node e
418
  else raise_loc_generic e.loc' "Capture variables are not allowed in types"
419
420
    

421
422
423
424
425
let typ glb e =
  mk_typ (compile glb e)

let pat glb e =
  pat_node (compile glb e)
426

427
428
429
430
let register_global_types glb b =
  let env' = compile_many glb b in
  List.fold_left 
    (fun glb (v,{ loc = loc }) -> 
431
       let t = TypeEnv.find v env' in
432
433
434
       let d = Types.descr (mk_typ t) in
       (*	       let d = Types.normalize d in*)
       Types.Print.register_global v d;
435
       if TypeEnv.mem v glb
436
       then raise_loc_generic loc ("Multiple definition for type " ^ v);
437
       TypeEnv.add v t glb
438
    ) glb b
439
440
441



442
443
(* II. Build skeleton *)

444
module Fv = IdSet
445

446
447
448
449
450
(* IDEA: introduce a node Loc in the AST to override nolocs
   in sub-expressions *)
   
let rec expr loc' glb { loc = loc; descr = d } = 
  let loc =  if loc = noloc then loc' else loc in
451
  let (fv,td) = 
452
    match d with
453
      | Forget (e,t) ->
454
	  let (fv,e) = expr loc glb e and t = typ glb t in
455
	  (fv, Typed.Forget (e,t))
456
457
      | Var s -> (Fv.singleton s, Typed.Var s)
      | Apply (e1,e2) -> 
458
	  let (fv1,e1) = expr loc glb e1 and (fv2,e2) = expr loc glb e2 in
459
	  (Fv.cup fv1 fv2, Typed.Apply (e1,e2))
460
      | Abstraction a ->
461
462
	  let iface = List.map (fun (t1,t2) -> (typ glb t1, typ glb t2)) 
			a.fun_iface in
463
464
465
	  let t = List.fold_left 
		    (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2)) 
		    Types.any iface in
466
467
468
	  let iface = List.map 
			(fun (t1,t2) -> (Types.descr t1, Types.descr t2)) 
			iface in
469
	  let (fv0,body) = branches loc glb a.fun_body in
470
471
472
473
474
475
476
477
478
	  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;
479
	       Typed.fun_fv = fv
480
481
482
483
	     }
	  )
      | Cst c -> (Fv.empty, Typed.Cst c)
      | Pair (e1,e2) ->
484
	  let (fv1,e1) = expr loc glb e1 and (fv2,e2) = expr loc glb e2 in
485
	  (Fv.cup fv1 fv2, Typed.Pair (e1,e2))
486
      | Xml (e1,e2) ->
487
	  let (fv1,e1) = expr loc glb e1 and (fv2,e2) = expr loc glb e2 in
488
	  (Fv.cup fv1 fv2, Typed.Xml (e1,e2))
489
      | Dot (e,l) ->
490
	  let (fv,e) = expr loc glb e in
491
	  (fv,  Typed.Dot (e,l))
492
493
      | RecordLitt r -> 
	  let fv = ref Fv.empty in
494
	  let r  = List.sort (fun (l1,_) (l2,_) -> compare l1 l2) r in
495
496
	  let r = List.map 
		    (fun (l,e) -> 
497
		       let (fv2,e) = expr loc glb e 
498
		       in fv := Fv.cup !fv fv2; (l,e))
499
500
501
502
503
504
505
		    r in
	  let rec check = function
	    | (l1,_) :: (l2,_) :: _ when l1 = l2 -> 
		raise_loc loc (MultipleLabel l1)
	    | _ :: rem -> check rem
	    | _ -> () in
	  check r;
506
	  (!fv, Typed.RecordLitt r)
507
      | Op (op,le) ->
508
	  let (fvs,ltes) = List.split (List.map (expr loc glb) le) in
509
	  let fv = List.fold_left Fv.cup Fv.empty fvs in
510
	  (fv, Typed.Op (op,ltes))
511
      | Match (e,b) -> 
512
513
	  let (fv1,e) = expr loc glb e
	  and (fv2,b) = branches loc glb b in
514
	  (Fv.cup fv1 fv2, Typed.Match (e, b))
515
      | Map (e,b) ->
516
517
	  let (fv1,e) = expr loc glb e
	  and (fv2,b) = branches loc glb b in
518
	  (Fv.cup fv1 fv2, Typed.Map (e, b))
519
      | Try (e,b) ->
520
521
	  let (fv1,e) = expr loc glb e
	  and (fv2,b) = branches loc glb b in
522
	  (Fv.cup fv1 fv2, Typed.Try (e, b))
523
  in
524
525
  fv,
  { Typed.exp_loc = loc;
526
527
528
529
    Typed.exp_typ = Types.empty;
    Typed.exp_descr = td;
  }
	      
530
  and branches loc glb b = 
531
    let fv = ref Fv.empty in
532
    let accept = ref Types.empty in
533
534
    let b = List.map 
	      (fun (p,e) ->
535
		 let (fv2,e) = expr loc glb e in
536
		 let p = pat glb p in
537
538
		 let fv2 = Fv.diff fv2 (Patterns.fv p) in
		 fv := Fv.cup !fv fv2;
539
		 accept := Types.cup !accept (Types.descr (Patterns.accept p));
540
		 { Typed.br_used = false;
541
		   Typed.br_pat = p;
542
543
		   Typed.br_body = e }
	      ) b in
544
545
546
547
    (!fv, 
     { 
       Typed.br_typ = Types.empty; 
       Typed.br_branches = b; 
548
549
       Typed.br_accept = !accept;
       Typed.br_compiled = None;
550
551
     } 
    )
552

553
554
let expr = expr noloc

555
556
557
let let_decl glb p e =
  let (_,e) = expr glb e in
  { Typed.let_pat = pat glb p;
558
559
560
561
562
563
    Typed.let_body = e;
    Typed.let_compiled = None }

(* III. Type-checks *)

type env = Types.descr Env.t
564
565
566

open Typed

567
let warning loc msg =
568
569
570
571
  Format.fprintf !Location.warning_ppf "Warning %a:@\n%a%s@\n" 
    Location.print_loc loc
    Location.html_hilight loc
    msg
572
573
574
575

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

576
let rec type_check env e constr precise = 
577
(*  Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
578
579
    Types.Print.print_descr constr precise; 
*)
580
  let d = type_check' e.exp_loc env e.exp_descr constr precise in
581
582
583
  e.exp_typ <- Types.cup e.exp_typ d;
  d

584
and type_check' loc env e constr precise = match e with
585
586
587
588
  | Forget (e,t) ->
      let t = Types.descr t in
      ignore (type_check env e t false);
      t
589
  | Abstraction a ->
590
591
592
593
594
595
596
      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
597
598
599
      let env = match a.fun_name with
	| None -> env
	| Some f -> Env.add f a.fun_typ env in
600
601
      List.iter 
	(fun (t1,t2) ->
602
	   ignore (type_check_branches loc env t1 a.fun_body t2 false)
603
604
	) a.fun_iface;
      t
605

606
607
  | Match (e,b) ->
      let t = type_check env e b.br_accept true in
608
      type_check_branches loc env t b constr precise
609
610
611

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

615
616
617
618
  | 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
619
620

(*
621
622
623
624
625
  | RecordLitt r ->
      let rconstr = Types.Record.get constr in
      if Types.Record.is_empty rconstr then
	raise_loc loc (ShouldHave (constr,"but it is a record."));

626
627
628
629
(* Completely buggy !  Need to check at the end that all required labels 
   are present ...A better to do it without precise = true ? *)
      let precise = true in

630
631
632
633
634
635
636
637
638
      let (rconstr,res) = 
	List.fold_left 
	  (fun (rconstr,res) (l,e) ->
	     let rconstr = Types.Record.restrict_label_present rconstr l in
	     let pi = Types.Record.project_field rconstr l in
	     if Types.Record.is_empty rconstr then
	       raise_loc loc 
		 (ShouldHave (constr,(Printf.sprintf 
					"Field %s is not allowed here."
639
					(Types.LabelPool.value l)
640
641
642
643
644
645
646
647
648
649
650
651
				     )
			     ));
	     let t = type_check env e pi true in
	     let rconstr = Types.Record.restrict_field rconstr l t in
	     
	     let res = 
	       if precise 
	       then Types.cap res (Types.record l false (Types.cons t))
	       else res in
	     (rconstr,res)
	  ) (rconstr, if precise then Types.Record.any else constr) r
      in
652
(*      check loc res constr ""; *)
653
      res
654
*)
655

656
657
658
659
660
  | 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
661
662
663
664
665
666
667
      (* Note: 
	 - could be more precise by integrating the decomposition
	 of constr inside Sequence.map.
      *)
      let res = 
	Sequence.map 
	  (fun t -> 
668
	     type_check_branches loc env t b constr' (precise || (not exact)))
669
670
671
	  t in
      if not exact then check loc res constr "";
      if precise then res else constr
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  | 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
689
  | Apply (e1,e2) ->
690
(*
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
      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
717
*)
718
719
720
      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
721
722
723
724
725
726
727
728
729
      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
730
731
732
733
734
735
736
737
738
739
740
741
742
  | 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
743
744
745
746
747
  | _ -> 
      let t : Types.descr = compute_type' loc env e in
      check loc t constr "";
      t

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
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


768
769
770
771
and compute_type env e =
  type_check env e Types.any true

and compute_type' loc env = function
772
773
  | Var s -> 
      (try Env.find s env 
774
       with Not_found -> raise_loc loc (UnboundId (Id.value s))
775
      )
776
  | Cst c -> Types.constant c
777
778
779
780
  | 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)))
781
782
783
  | Op (op, el) ->
      let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
      type_op loc op args
784
785
  | Map (e,b) ->
      let t = compute_type env e in
786
      Sequence.map (fun t -> type_check_branches loc env t b Types.any true) t
787
788
789
790
791
792
793
794
795
796

(* We keep these cases here to allow comparison and benchmarking ...
   Just comment the corresponding cases in type_check' to
   activate these ones.
*)
  | 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 ->
797
798
      let r = 
	List.map
799
          (fun (l,e) -> (l,Types.cons (compute_type env e)))
800
801
	  r in
      Types.record' (false,r)
802
  | _ -> assert false
803

804
and type_check_branches loc env targ brs constr precise =
805
  if Types.is_empty targ then Types.empty 
806
807
  else (
    brs.br_typ <- Types.cup brs.br_typ targ;
808
    branches_aux loc env targ 
809
810
      (if precise then Types.empty else constr) 
      constr precise brs.br_branches
811
  )
812
    
813
814
and branches_aux loc env targ tres constr precise = function
  | [] -> raise_loc loc (NonExhaustive targ)
815
816
817
818
819
820
  | 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' 
821
      then branches_aux loc env targ tres constr precise rem
822
823
824
825
826
827
      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
828
829
	  let t = type_check env' b.br_body constr precise in
	  let tres = if precise then Types.cup t tres else tres in
830
831
	  let targ'' = Types.diff targ acc in
	  if (Types.non_empty targ'') then 
832
	    branches_aux loc env targ'' tres constr precise rem 
833
834
	  else
	    tres
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
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


863
864
and type_op loc op args =
  match (op,args) with
865
    | "+", [loc1,t1; loc2,t2] ->
866
	type_int_binop Intervals.add loc1 t1 loc2 t2
867
868
    | "-", [loc1,t1; loc2,t2] ->
	type_int_binop Intervals.sub loc1 t1 loc2 t2
869
    | ("*" | "/" | "mod"), [loc1,t1; loc2,t2] ->
870
	type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
871
    | "@", [loc1,t1; loc2,t2] ->
872
873
874
	check loc1 t1 Sequence.any
	  "The first argument of @ must be a sequence";
	Sequence.concat t1 t2
875
    | "flatten", [loc1,t1] ->
876
877
878
	check loc1 t1 Sequence.seqseq 
	  "The argument of flatten must be a sequence of sequences";
	Sequence.flatten t1
879
880
881
882
    | "load_xml", [loc1,t1] ->
	check loc1 t1 Sequence.string
	  "The argument of load_xml must be a string (filename)";
	Types.any
883
884
885
886
    | "load_html", [loc1,t1] ->
	check loc1 t1 Sequence.string
	  "The argument of load_html must be a string (filename)";
	Types.any
887
888
    | "raise", [loc1,t1] ->
	Types.empty
889
890
    | "print_xml", [loc1,t1] ->
	Sequence.string
891
892
    | "print", [loc1,t1] ->
	check loc1 t1 Sequence.string
893
894
895
896
897
898
899
900
	  "The argument of print must be a string";
	Sequence.nil_type
    | "dump_to_file", [loc1,t1; loc2,t2] ->
	check loc1 t1 Sequence.string
	  "The argument of dump_to_file must be a string (filename)";
	check loc2 t2 Sequence.string
	  "The argument of dump_to_file must be a string (value to dump)";
	Sequence.nil_type
901
902
    | "int_of", [loc1,t1] ->
	check loc1 t1 Sequence.string
903
	  "The argument of int_of must be a string";
904
905
906
	if not (Types.subtype t1 Builtin.intstr) then
	  warning loc "This application of int_of may fail";
	Types.interval Intervals.any
907
908
    | "string_of", [loc1,t1] ->
	Sequence.string
909
910
911
912
913
914
915
916
917
918
919
    | _ -> assert false

and type_int_binop f loc1 t1 loc2 t2 =
  if not (Types.Int.is_int t1) then
    raise_loc loc1 
      (Constraint 
	 (t1,Types.Int.any,
	  "The first argument must be an integer"));
  if not (Types.Int.is_int t2) then
    raise_loc loc2
      (Constraint 
920
	       (t2,Types.Int.any,
921
922
923
924
925
		"The second argument must be an integer"));
  Types.Int.put
    (f (Types.Int.get t1) (Types.Int.get t2));