/[svn]/typing/typer.ml
ViewVC logotype

Contents of /typing/typer.ml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (hide annotations)
Tue Jul 10 16:57:42 2007 UTC (5 years, 10 months ago) by abate
File size: 13000 byte(s)
[r2002-10-16 16:18:48 by cvscast] Empty log message

Original author: cvscast
Date: 2002-10-16 16:18:48+00:00
1 abate 5 (* I. Transform the abstract syntax of types and patterns into
2     the internal form *)
3    
4     open Location
5     open Ast
6    
7 abate 9 exception Pattern of string
8     exception NonExhaustive of Types.descr
9     exception Constraint of Types.descr * Types.descr * string
10 abate 5
11 abate 9 let raise_loc loc exn = raise (Location (loc,exn))
12    
13 abate 5 (* Internal representation as a graph (desugar recursive types and regexp),
14     to compute freevars, etc... *)
15    
16 abate 9 type ti = {
17 abate 5 id : int;
18     mutable loc' : loc;
19     mutable fv : string SortedList.t option;
20     mutable descr': descr;
21     mutable type_node: Types.node option;
22     mutable pat_node: Patterns.node option
23     }
24     and descr =
25 abate 9 [ `Alias of string * ti
26 abate 5 | `Type of Types.descr
27     | `Or of ti * ti
28     | `And of ti * ti
29     | `Diff of ti * ti
30     | `Times of ti * ti
31     | `Arrow of ti * ti
32     | `Record of Types.label * bool * ti
33     | `Capture of Patterns.capture
34     | `Constant of Patterns.capture * Types.const
35     ]
36    
37    
38    
39     module S = struct type t = string let compare = compare end
40     module StringMap = Map.Make(S)
41     module StringSet = Set.Make(S)
42    
43     let mk' =
44     let counter = ref 0 in
45 abate 13 fun loc ->
46 abate 5 incr counter;
47 abate 9 let rec x = {
48     id = !counter;
49 abate 13 loc' = loc;
50 abate 9 fv = None;
51     descr' = `Alias ("__dummy__", x);
52     type_node = None;
53     pat_node = None
54     } in
55 abate 5 x
56    
57     let cons loc d =
58 abate 13 let x = mk' loc in
59 abate 5 x.descr' <- d;
60     x
61    
62     (* Note:
63     Compilation of Regexp is implemented as a ``rewriting'' of
64     the parsed syntax, in order to be able to print its result
65     (for debugging for instance)
66    
67     It would be possible (and a little more efficient) to produce
68     directly ti nodes.
69     *)
70    
71     module Regexp = struct
72     let memo = Hashtbl.create 51
73     let defs = ref []
74     let name =
75     let c = ref 0 in
76     fun () ->
77     incr c;
78     "#" ^ (string_of_int !c)
79    
80     let rec seq_vars accu = function
81     | Epsilon | Elem _ -> accu
82     | Seq (r1,r2) | Alt (r1,r2) -> seq_vars (seq_vars accu r1) r2
83     | Star r | WeakStar r -> seq_vars accu r
84     | SeqCapture (v,r) -> seq_vars (StringSet.add v accu) r
85    
86     let rec propagate vars = function
87     | Epsilon -> `Epsilon
88     | Elem x -> `Elem (vars,x)
89     | Seq (r1,r2) -> `Seq (propagate vars r1,propagate vars r2)
90     | Alt (r1,r2) -> `Alt (propagate vars r1, propagate vars r2)
91     | Star r -> `Star (propagate vars r)
92     | WeakStar r -> `WeakStar (propagate vars r)
93     | SeqCapture (v,x) -> propagate (StringSet.add v vars) x
94    
95     let cup r1 r2 =
96     match (r1,r2) with
97     | (_, `Empty) -> r1
98     | (`Empty, _) -> r2
99     | (`Res t1, `Res t2) -> `Res (mk noloc (Or (t1,t2)))
100    
101     let rec compile fin e seq : [`Res of Ast.ppat | `Empty] =
102     if List.mem seq e then `Empty
103     else
104     let e = seq :: e in
105     match seq with
106     | [] ->
107     `Res fin
108     | `Epsilon :: rest ->
109     compile fin e rest
110     | `Elem (vars,x) :: rest ->
111     let capt = StringSet.fold
112     (fun v t -> mk noloc (And (t, (mk noloc (Capture v)))))
113     vars x in
114     `Res (mk noloc (Prod (capt, guard_compile fin rest)))
115     | `Seq (r1,r2) :: rest ->
116     compile fin e (r1 :: r2 :: rest)
117     | `Alt (r1,r2) :: rest ->
118     cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
119     | `Star r :: rest -> cup (compile fin e (r::seq)) (compile fin e rest)
120     | `WeakStar r :: rest -> cup (compile fin e rest) (compile fin e (r::seq))
121    
122     and guard_compile fin seq =
123     try Hashtbl.find memo seq
124     with
125     Not_found ->
126     let n = name () in
127     let v = mk noloc (PatVar n) in
128     Hashtbl.add memo seq v;
129     let d = compile fin [] seq in
130     (match d with
131     | `Empty -> assert false
132     | `Res d -> defs := (n,d) :: !defs);
133     v
134    
135    
136     let atom_nil = Types.mk_atom "nil"
137     let constant_nil v t =
138     mk noloc (And (t, (mk noloc (Constant (v, Types.Atom atom_nil)))))
139    
140     let compile regexp queue : ppat =
141     let vars = seq_vars StringSet.empty regexp in
142     let fin = StringSet.fold constant_nil vars queue in
143     let n = guard_compile fin [propagate StringSet.empty regexp] in
144     Hashtbl.clear memo;
145     let d = !defs in
146     defs := [];
147     mk noloc (Recurs (n,d))
148     end
149    
150     let compile_regexp = Regexp.compile
151    
152    
153     let rec compile env { loc = loc; descr = d } : ti =
154     match (d : Ast.ppat') with
155     | PatVar s ->
156     (try StringMap.find s env
157 abate 9 with Not_found ->
158     raise_loc loc (Pattern ("Undefined type variable " ^ s))
159 abate 5 )
160 abate 13 | Recurs (t, b) -> compile (compile_many env b) t
161 abate 5 | Regexp (r,q) -> compile env (Regexp.compile r q)
162     | Internal t -> cons loc (`Type t)
163     | Or (t1,t2) -> cons loc (`Or (compile env t1, compile env t2))
164     | And (t1,t2) -> cons loc (`And (compile env t1, compile env t2))
165     | Diff (t1,t2) -> cons loc (`Diff (compile env t1, compile env t2))
166     | Prod (t1,t2) -> cons loc (`Times (compile env t1, compile env t2))
167     | Arrow (t1,t2) -> cons loc (`Arrow (compile env t1, compile env t2))
168     | Record (l,o,t) -> cons loc (`Record (l,o,compile env t))
169     | Constant (x,v) -> cons loc (`Constant (x,v))
170     | Capture x -> cons loc (`Capture x)
171    
172 abate 13 and compile_many env b =
173     let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
174     let env =
175     List.fold_left (fun env (v,t,x) -> StringMap.add v x env) env b in
176     List.iter (fun (v,t,x) -> x.descr' <- `Alias (v, compile env t)) b;
177     env
178    
179    
180 abate 5 let rec comp_fv seen s =
181     match s.fv with
182     | Some l -> l
183     | None ->
184     let l =
185     match s.descr' with
186 abate 9 | `Alias (_,x) -> if List.memq s seen then [] else comp_fv (s :: seen) x
187 abate 5 | `Or (s1,s2)
188     | `And (s1,s2)
189     | `Diff (s1,s2)
190     | `Times (s1,s2)
191     | `Arrow (s1,s2) -> SortedList.cup (comp_fv seen s1) (comp_fv seen s2)
192     | `Record (l,opt,s) -> comp_fv seen s
193     | `Type _ -> []
194     | `Capture x
195     | `Constant (x,_) -> [x]
196     in
197     if seen = [] then s.fv <- Some l;
198     l
199    
200    
201     let fv = comp_fv []
202    
203     let rec typ seen s : Types.descr =
204     match s.descr' with
205 abate 9 | `Alias (v,x) ->
206     if List.memq s seen then
207     raise_loc s.loc'
208     (Pattern
209     ("Unguarded recursion on variable " ^ v ^ " in this type"))
210 abate 5 else typ (s :: seen) x
211     | `Type t -> t
212     | `Or (s1,s2) -> Types.cup (typ seen s1) (typ seen s2)
213     | `And (s1,s2) -> Types.cap (typ seen s1) (typ seen s2)
214     | `Diff (s1,s2) -> Types.diff (typ seen s1) (typ seen s2)
215     | `Times (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
216     | `Arrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
217     | `Record (l,o,s) -> Types.record l o (typ_node s)
218 abate 9 | `Capture _ | `Constant _ -> assert false
219 abate 5
220     and typ_node s : Types.node =
221     match s.type_node with
222     | Some x -> x
223     | None ->
224     let x = Types.make () in
225     s.type_node <- Some x;
226     let t = typ [] s in
227     Types.define x t;
228     x
229    
230     let type_node s = Types.internalize (typ_node s)
231    
232     let rec pat seen s : Patterns.descr =
233     if fv s = [] then Patterns.constr (type_node s) else
234     match s.descr' with
235 abate 9 | `Alias (v,x) ->
236     if List.memq s seen then
237     raise_loc s.loc'
238     (Pattern
239     ("Unguarded recursion on variable " ^ v ^ " in this pattern"))
240 abate 5 else pat (s :: seen) x
241     | `Or (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
242     | `And (s1,s2) -> Patterns.cap (pat seen s1) (pat seen s2)
243     | `Diff (s1,s2) when fv s2 = [] ->
244     let s2 = Types.cons (Types.neg (Types.descr (type_node s2)))in
245     Patterns.cap (pat seen s1) (Patterns.constr s2)
246 abate 9 | `Diff _ ->
247     raise_loc s.loc' (Pattern "Difference not allowed in patterns")
248 abate 5 | `Times (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
249     | `Record (l,false,s) -> Patterns.record l (pat_node s)
250 abate 9 | `Record _ ->
251     raise_loc s.loc'
252     (Pattern "Optional field not allowed in record patterns")
253 abate 5 | `Capture x -> Patterns.capture x
254     | `Constant (x,c) -> Patterns.constant x c
255 abate 9 | `Arrow _ ->
256     raise_loc s.loc' (Pattern "Arrow not allowed in patterns")
257     | `Type _ -> assert false
258 abate 5
259     and pat_node s : Patterns.node =
260     match s.pat_node with
261     | Some x -> x
262     | None ->
263     let x = Patterns.make (fv s) in
264     s.pat_node <- Some x;
265     let t = pat [] s in
266     Patterns.define x t;
267     x
268    
269 abate 13 let global_types = ref StringMap.empty
270    
271     let mk_typ e =
272 abate 9 if fv e = [] then type_node e
273 abate 13 else raise_loc e.loc' (Pattern "Capture variables are not allowed in types")
274    
275 abate 5
276 abate 13 let typ e =
277     mk_typ (compile !global_types e)
278    
279 abate 5 let pat e =
280 abate 13 let e = compile !global_types e in
281 abate 5 pat_node e
282    
283 abate 13 let register_global_types b =
284     let env = compile_many !global_types b in
285     List.iter (fun (v,_) -> ignore (mk_typ (StringMap.find v env))) b;
286     global_types := env
287 abate 5
288    
289     (* II. Build skeleton *)
290    
291 abate 6 module Fv = StringSet
292    
293 abate 5 let rec expr { loc = loc; descr = d } =
294 abate 6 let (fv,td) =
295 abate 5 match d with
296 abate 6 | Var s -> (Fv.singleton s, Typed.Var s)
297     | Apply (e1,e2) ->
298     let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
299     (Fv.union fv1 fv2, Typed.Apply (e1,e2))
300 abate 5 | Abstraction a ->
301 abate 6 let iface = List.map (fun (t1,t2) -> (typ t1, typ t2)) a.fun_iface in
302     let t = List.fold_left
303     (fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2))
304     Types.any iface in
305 abate 9 let iface = List.map
306     (fun (t1,t2) -> (Types.descr t1, Types.descr t2))
307     iface in
308 abate 6 let (fv0,body) = branches a.fun_body in
309     let fv = match a.fun_name with
310     | None -> fv0
311     | Some f -> Fv.remove f fv0 in
312     (fv,
313     Typed.Abstraction
314     { Typed.fun_name = a.fun_name;
315     Typed.fun_iface = iface;
316     Typed.fun_body = body;
317     Typed.fun_typ = t;
318     Typed.fun_fv = Fv.elements fv0
319     }
320     )
321     | Cst c -> (Fv.empty, Typed.Cst c)
322     | Pair (e1,e2) ->
323     let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
324     (Fv.union fv1 fv2, Typed.Pair (e1,e2))
325     | RecordLitt r ->
326     (* XXX TODO: check that no label appears twice *)
327     let fv = ref Fv.empty in
328     let r = List.map
329     (fun (l,e) ->
330     let (fv2,e) = expr e in
331     fv := Fv.union !fv fv2;
332     (l,e)
333     ) r in
334     (!fv, Typed.RecordLitt r)
335     | Op (o,e) ->
336     let (fv,e) = expr e in (fv, Typed.Op (o,e))
337     | Match (e,b) ->
338     let (fv1,e) = expr e
339     and (fv2,b) = branches b in
340     (Fv.union fv1 fv2, Typed.Match (e, b))
341     | Map (e,b) ->
342     let (fv1,e) = expr e
343     and (fv2,b) = branches b in
344     (Fv.union fv1 fv2, Typed.Map (e, b))
345 abate 5 in
346 abate 6 fv,
347     { Typed.exp_loc = loc;
348 abate 5 Typed.exp_typ = Types.empty;
349     Typed.exp_descr = td;
350     }
351    
352 abate 6 and branches b =
353     let fv = ref Fv.empty in
354     let b = List.map
355     (fun (p,e) ->
356     let (fv2,e) = expr e in
357     fv := Fv.union !fv fv2;
358     { Typed.br_used = false;
359     Typed.br_pat = pat p;
360     Typed.br_body = e }
361     ) b in
362 abate 9 (!fv, { Typed.br_typ = Types.empty; Typed.br_branches = b } )
363 abate 5
364 abate 6 module Env = StringMap
365    
366     open Typed
367    
368     let rec compute_type env e =
369     let d = compute_type' e.exp_loc env e.exp_descr in
370     e.exp_typ <- Types.cup e.exp_typ d;
371     d
372    
373     and compute_type' loc env = function
374     | Var s -> Env.find s env
375     | Apply (e1,e2) ->
376     let t1 = compute_type env e1 and t2 = compute_type env e2 in
377 abate 11 if Types.is_empty t2
378     then Types.empty
379     else
380     if Types.subtype t1 Types.Arrow.any
381     then
382     let t1 = Types.Arrow.get t1 in
383     let dom = Types.Arrow.domain t1 in
384     if Types.subtype t2 dom
385     then Types.Arrow.apply t1 t2
386     else
387     raise_loc loc
388     (Constraint
389     (t2,dom,"The argument is not in the domain of the function"))
390     else
391     raise_loc loc
392     (Constraint
393     (t1,Types.Arrow.any,"The expression in function position is not necessarily a function"))
394 abate 6 | Abstraction a ->
395     let env = match a.fun_name with
396     | None -> env
397     | Some f -> Env.add f a.fun_typ env in
398     List.iter (fun (t1,t2) ->
399 abate 9 let t = type_branches loc env t1 a.fun_body in
400     if not (Types.subtype t t2) then
401     raise_loc loc (Constraint (t,t2,"Constraint not satisfied in interface"))
402 abate 6 ) a.fun_iface;
403     a.fun_typ
404     | Cst c -> Types.constant c
405     | Pair (e1,e2) ->
406     let t1 = compute_type env e1 and t2 = compute_type env e2 in
407     let t1 = Types.cons t1 and t2 = Types.cons t2 in
408     Types.times t1 t2
409     | RecordLitt r ->
410     List.fold_left
411     (fun accu (l,e) ->
412     let t = compute_type env e in
413     let t = Types.record l false (Types.cons t) in
414     Types.cap accu t
415     ) Types.Record.any r
416     | Op (op,e) -> assert false
417     | Match (e,b) ->
418     let t = compute_type env e in
419 abate 9 type_branches loc env t b
420 abate 6 | Map (e,b) -> assert false
421    
422 abate 9 and type_branches loc env targ brs =
423 abate 6 if Types.is_empty targ then Types.empty
424 abate 9 else (
425     brs.br_typ <- Types.cup brs.br_typ targ;
426     branches_aux loc env targ Types.empty brs.br_branches
427     )
428 abate 6
429 abate 9 and branches_aux loc env targ tres = function
430     | [] -> raise_loc loc (NonExhaustive targ)
431 abate 6 | b :: rem ->
432     let p = b.br_pat in
433     let acc = Types.descr (Patterns.accept p) in
434    
435     let targ' = Types.cap targ acc in
436     if Types.is_empty targ'
437 abate 9 then branches_aux loc env targ tres rem
438 abate 6 else
439     ( b.br_used <- true;
440     let res = Patterns.filter targ' p in
441     let env' = List.fold_left
442     (fun env (x,t) -> Env.add x (Types.descr t) env)
443     env res in
444     let t = compute_type env' b.br_body in
445 abate 9 let tres = Types.cup t tres in
446     let targ'' = Types.diff targ acc in
447     if (Types.non_empty targ'') then
448     branches_aux loc env targ'' (Types.cup t tres) rem
449     else
450     tres
451 abate 6 )

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.5