| 1 |
(* I. Transform the abstract syntax of types and patterns into
|
| 2 |
the internal form *)
|
| 3 |
|
| 4 |
open Location
|
| 5 |
open Ast
|
| 6 |
|
| 7 |
exception Pattern of string
|
| 8 |
exception NonExhaustive of Types.descr
|
| 9 |
exception MultipleLabel of Types.label
|
| 10 |
exception Constraint of Types.descr * Types.descr * string
|
| 11 |
exception ShouldHave of Types.descr * string
|
| 12 |
exception WrongLabel of Types.descr * Types.label
|
| 13 |
exception UnboundId of string
|
| 14 |
|
| 15 |
let raise_loc loc exn = raise (Location (loc,exn))
|
| 16 |
|
| 17 |
(* Internal representation as a graph (desugar recursive types and regexp),
|
| 18 |
to compute freevars, etc... *)
|
| 19 |
|
| 20 |
type ti = {
|
| 21 |
id : int;
|
| 22 |
mutable loc' : loc;
|
| 23 |
mutable fv : string SortedList.t option;
|
| 24 |
mutable descr': descr;
|
| 25 |
mutable type_node: Types.node option;
|
| 26 |
mutable pat_node: Patterns.node option
|
| 27 |
}
|
| 28 |
and descr =
|
| 29 |
[ `Alias of string * ti
|
| 30 |
| `Type of Types.descr
|
| 31 |
| `Or of ti * ti
|
| 32 |
| `And of ti * ti * bool
|
| 33 |
| `Diff of ti * ti
|
| 34 |
| `Times of ti * ti
|
| 35 |
| `Arrow of ti * ti
|
| 36 |
| `Record of Types.label * bool * ti
|
| 37 |
| `Capture of Patterns.capture
|
| 38 |
| `Constant of Patterns.capture * Types.const
|
| 39 |
]
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
module S = struct type t = string let compare = compare end
|
| 44 |
module StringMap = Map.Make(S)
|
| 45 |
module StringSet = Set.Make(S)
|
| 46 |
|
| 47 |
let mk' =
|
| 48 |
let counter = ref 0 in
|
| 49 |
fun loc ->
|
| 50 |
incr counter;
|
| 51 |
let rec x = {
|
| 52 |
id = !counter;
|
| 53 |
loc' = loc;
|
| 54 |
fv = None;
|
| 55 |
descr' = `Alias ("__dummy__", x);
|
| 56 |
type_node = None;
|
| 57 |
pat_node = None
|
| 58 |
} in
|
| 59 |
x
|
| 60 |
|
| 61 |
let cons loc d =
|
| 62 |
let x = mk' loc in
|
| 63 |
x.descr' <- d;
|
| 64 |
x
|
| 65 |
|
| 66 |
(* Note:
|
| 67 |
Compilation of Regexp is implemented as a ``rewriting'' of
|
| 68 |
the parsed syntax, in order to be able to print its result
|
| 69 |
(for debugging for instance)
|
| 70 |
|
| 71 |
It would be possible (and a little more efficient) to produce
|
| 72 |
directly ti nodes.
|
| 73 |
*)
|
| 74 |
|
| 75 |
module Regexp = struct
|
| 76 |
let defs = ref []
|
| 77 |
let name =
|
| 78 |
let c = ref 0 in
|
| 79 |
fun () ->
|
| 80 |
incr c;
|
| 81 |
"#" ^ (string_of_int !c)
|
| 82 |
|
| 83 |
let rec seq_vars accu = function
|
| 84 |
| Epsilon | Elem _ -> accu
|
| 85 |
| Seq (r1,r2) | Alt (r1,r2) -> seq_vars (seq_vars accu r1) r2
|
| 86 |
| Star r | WeakStar r -> seq_vars accu r
|
| 87 |
| SeqCapture (v,r) -> seq_vars (StringSet.add v accu) r
|
| 88 |
|
| 89 |
let uniq_id = let r = ref 0 in fun () -> incr r; !r
|
| 90 |
|
| 91 |
type flat = [ `Epsilon
|
| 92 |
| `Elem of int * Ast.ppat (* the int arg is used to
|
| 93 |
to stop generic comparison *)
|
| 94 |
| `Seq of flat * flat
|
| 95 |
| `Alt of flat * flat
|
| 96 |
| `Star of flat
|
| 97 |
| `WeakStar of flat ]
|
| 98 |
|
| 99 |
let rec propagate vars : regexp -> flat = function
|
| 100 |
| Epsilon -> `Epsilon
|
| 101 |
| Elem x -> let p = vars x in `Elem (uniq_id (),p)
|
| 102 |
| Seq (r1,r2) -> `Seq (propagate vars r1,propagate vars r2)
|
| 103 |
| Alt (r1,r2) -> `Alt (propagate vars r1, propagate vars r2)
|
| 104 |
| Star r -> `Star (propagate vars r)
|
| 105 |
| WeakStar r -> `WeakStar (propagate vars r)
|
| 106 |
| SeqCapture (v,x) ->
|
| 107 |
let v= mk noloc (Capture v) in
|
| 108 |
propagate (fun p -> mk noloc (And (vars p,v,true))) x
|
| 109 |
|
| 110 |
let cup r1 r2 =
|
| 111 |
match (r1,r2) with
|
| 112 |
| (_, `Empty) -> r1
|
| 113 |
| (`Empty, _) -> r2
|
| 114 |
| (`Res t1, `Res t2) -> `Res (mk noloc (Or (t1,t2)))
|
| 115 |
|
| 116 |
(*TODO: review this compilation schema to avoid explosion when
|
| 117 |
coding (Optional x) by (Or(Epsilon,x)); memoization ... *)
|
| 118 |
|
| 119 |
module Memo = Map.Make(struct type t = flat list let compare = compare end)
|
| 120 |
module Coind = Set.Make(struct type t = flat list let compare = compare end)
|
| 121 |
let memo = ref Memo.empty
|
| 122 |
|
| 123 |
let rec compile fin e seq : [`Res of Ast.ppat | `Empty] =
|
| 124 |
if Coind.mem seq !e then `Empty
|
| 125 |
else (
|
| 126 |
e := Coind.add seq !e;
|
| 127 |
match seq with
|
| 128 |
| [] ->
|
| 129 |
`Res fin
|
| 130 |
| `Epsilon :: rest ->
|
| 131 |
compile fin e rest
|
| 132 |
| `Elem (_,p) :: rest ->
|
| 133 |
`Res (mk noloc (Prod (p, guard_compile fin rest)))
|
| 134 |
| `Seq (r1,r2) :: rest ->
|
| 135 |
compile fin e (r1 :: r2 :: rest)
|
| 136 |
| `Alt (r1,r2) :: rest ->
|
| 137 |
cup (compile fin e (r1::rest)) (compile fin e (r2::rest))
|
| 138 |
| `Star r :: rest ->
|
| 139 |
cup (compile fin e (r::seq)) (compile fin e rest)
|
| 140 |
| `WeakStar r :: rest ->
|
| 141 |
cup (compile fin e rest) (compile fin e (r::seq))
|
| 142 |
)
|
| 143 |
and guard_compile fin seq =
|
| 144 |
try Memo.find seq !memo
|
| 145 |
with
|
| 146 |
Not_found ->
|
| 147 |
let n = name () in
|
| 148 |
let v = mk noloc (PatVar n) in
|
| 149 |
memo := Memo.add seq v !memo;
|
| 150 |
let d = compile fin (ref Coind.empty) seq in
|
| 151 |
(match d with
|
| 152 |
| `Empty -> assert false
|
| 153 |
| `Res d -> defs := (n,d) :: !defs);
|
| 154 |
v
|
| 155 |
|
| 156 |
|
| 157 |
let atom_nil = Types.mk_atom "nil"
|
| 158 |
let constant_nil v t =
|
| 159 |
mk noloc (And (t, (mk noloc (Constant (v, Types.Atom atom_nil))), true))
|
| 160 |
|
| 161 |
let compile regexp queue : ppat =
|
| 162 |
let vars = seq_vars StringSet.empty regexp in
|
| 163 |
let fin = StringSet.fold constant_nil vars queue in
|
| 164 |
let n = guard_compile fin [propagate (fun p -> p) regexp] in
|
| 165 |
memo := Memo.empty;
|
| 166 |
let d = !defs in
|
| 167 |
defs := [];
|
| 168 |
mk noloc (Recurs (n,d))
|
| 169 |
end
|
| 170 |
|
| 171 |
let compile_regexp = Regexp.compile
|
| 172 |
|
| 173 |
|
| 174 |
let rec compile env { loc = loc; descr = d } : ti =
|
| 175 |
match (d : Ast.ppat') with
|
| 176 |
| PatVar s ->
|
| 177 |
(try StringMap.find s env
|
| 178 |
with Not_found ->
|
| 179 |
raise_loc loc (Pattern ("Undefined type variable " ^ s))
|
| 180 |
)
|
| 181 |
| Recurs (t, b) -> compile (compile_many env b) t
|
| 182 |
| Regexp (r,q) -> compile env (Regexp.compile r q)
|
| 183 |
| Internal t -> cons loc (`Type t)
|
| 184 |
| Or (t1,t2) -> cons loc (`Or (compile env t1, compile env t2))
|
| 185 |
| And (t1,t2,e) -> cons loc (`And (compile env t1, compile env t2,e))
|
| 186 |
| Diff (t1,t2) -> cons loc (`Diff (compile env t1, compile env t2))
|
| 187 |
| Prod (t1,t2) -> cons loc (`Times (compile env t1, compile env t2))
|
| 188 |
| Arrow (t1,t2) -> cons loc (`Arrow (compile env t1, compile env t2))
|
| 189 |
| Record (l,o,t) -> cons loc (`Record (l,o,compile env t))
|
| 190 |
| Constant (x,v) -> cons loc (`Constant (x,v))
|
| 191 |
| Capture x -> cons loc (`Capture x)
|
| 192 |
|
| 193 |
and compile_many env b =
|
| 194 |
let b = List.map (fun (v,t) -> (v,t,mk' t.loc)) b in
|
| 195 |
let env =
|
| 196 |
List.fold_left (fun env (v,t,x) -> StringMap.add v x env) env b in
|
| 197 |
List.iter (fun (v,t,x) -> x.descr' <- `Alias (v, compile env t)) b;
|
| 198 |
env
|
| 199 |
|
| 200 |
|
| 201 |
let comp_fv_seen = ref []
|
| 202 |
let comp_fv_res = ref []
|
| 203 |
let rec comp_fv s =
|
| 204 |
if List.memq s !comp_fv_seen then ()
|
| 205 |
else (
|
| 206 |
comp_fv_seen := s :: !comp_fv_seen;
|
| 207 |
(match s.descr' with
|
| 208 |
| `Alias (_,x) -> comp_fv x
|
| 209 |
| `Or (s1,s2)
|
| 210 |
| `And (s1,s2,_)
|
| 211 |
| `Diff (s1,s2)
|
| 212 |
| `Times (s1,s2)
|
| 213 |
| `Arrow (s1,s2) -> comp_fv s1; comp_fv s2
|
| 214 |
| `Record (l,opt,s) -> comp_fv s
|
| 215 |
| `Type _ -> ()
|
| 216 |
| `Capture x
|
| 217 |
| `Constant (x,_) -> comp_fv_res := x :: !comp_fv_res);
|
| 218 |
if (!comp_fv_res = []) then s.fv <- Some [];
|
| 219 |
(* TODO: check that the above line is correct *)
|
| 220 |
)
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
let fv s =
|
| 225 |
match s.fv with
|
| 226 |
| Some l -> l
|
| 227 |
| None ->
|
| 228 |
comp_fv s;
|
| 229 |
let l = SortedList.from_list !comp_fv_res in
|
| 230 |
comp_fv_res := [];
|
| 231 |
comp_fv_seen := [];
|
| 232 |
s.fv <- Some l;
|
| 233 |
l
|
| 234 |
|
| 235 |
let rec typ seen s : Types.descr =
|
| 236 |
match s.descr' with
|
| 237 |
| `Alias (v,x) ->
|
| 238 |
if List.memq s seen then
|
| 239 |
raise_loc s.loc'
|
| 240 |
(Pattern
|
| 241 |
("Unguarded recursion on variable " ^ v ^ " in this type"))
|
| 242 |
else typ (s :: seen) x
|
| 243 |
| `Type t -> t
|
| 244 |
| `Or (s1,s2) -> Types.cup (typ seen s1) (typ seen s2)
|
| 245 |
| `And (s1,s2,_) -> Types.cap (typ seen s1) (typ seen s2)
|
| 246 |
| `Diff (s1,s2) -> Types.diff (typ seen s1) (typ seen s2)
|
| 247 |
| `Times (s1,s2) -> Types.times (typ_node s1) (typ_node s2)
|
| 248 |
| `Arrow (s1,s2) -> Types.arrow (typ_node s1) (typ_node s2)
|
| 249 |
| `Record (l,o,s) -> Types.record l o (typ_node s)
|
| 250 |
| `Capture _ | `Constant _ -> assert false
|
| 251 |
|
| 252 |
and typ_node s : Types.node =
|
| 253 |
match s.type_node with
|
| 254 |
| Some x -> x
|
| 255 |
| None ->
|
| 256 |
let x = Types.make () in
|
| 257 |
s.type_node <- Some x;
|
| 258 |
let t = typ [] s in
|
| 259 |
Types.define x t;
|
| 260 |
x
|
| 261 |
|
| 262 |
let type_node s =
|
| 263 |
let s = typ_node s in
|
| 264 |
let s = Types.internalize s in
|
| 265 |
(* Types.define s (Types.normalize (Types.descr s)); *)
|
| 266 |
s
|
| 267 |
|
| 268 |
let rec pat seen s : Patterns.descr =
|
| 269 |
if fv s = [] then Patterns.constr (type_node s) else
|
| 270 |
match s.descr' with
|
| 271 |
| `Alias (v,x) ->
|
| 272 |
if List.memq s seen then
|
| 273 |
raise_loc s.loc'
|
| 274 |
(Pattern
|
| 275 |
("Unguarded recursion on variable " ^ v ^ " in this pattern"))
|
| 276 |
else pat (s :: seen) x
|
| 277 |
| `Or (s1,s2) -> Patterns.cup (pat seen s1) (pat seen s2)
|
| 278 |
| `And (s1,s2,e) -> Patterns.cap (pat seen s1) (pat seen s2) e
|
| 279 |
| `Diff (s1,s2) when fv s2 = [] ->
|
| 280 |
let s2 = Types.cons (Types.neg (Types.descr (type_node s2)))in
|
| 281 |
Patterns.cap (pat seen s1) (Patterns.constr s2) true
|
| 282 |
| `Diff _ ->
|
| 283 |
raise_loc s.loc' (Pattern "Difference not allowed in patterns")
|
| 284 |
| `Times (s1,s2) -> Patterns.times (pat_node s1) (pat_node s2)
|
| 285 |
| `Record (l,false,s) -> Patterns.record l (pat_node s)
|
| 286 |
| `Record _ ->
|
| 287 |
raise_loc s.loc'
|
| 288 |
(Pattern "Optional field not allowed in record patterns")
|
| 289 |
| `Capture x -> Patterns.capture x
|
| 290 |
| `Constant (x,c) -> Patterns.constant x c
|
| 291 |
| `Arrow _ ->
|
| 292 |
raise_loc s.loc' (Pattern "Arrow not allowed in patterns")
|
| 293 |
| `Type _ -> assert false
|
| 294 |
|
| 295 |
and pat_node s : Patterns.node =
|
| 296 |
match s.pat_node with
|
| 297 |
| Some x -> x
|
| 298 |
| None ->
|
| 299 |
let x = Patterns.make (fv s) in
|
| 300 |
s.pat_node <- Some x;
|
| 301 |
let t = pat [] s in
|
| 302 |
Patterns.define x t;
|
| 303 |
x
|
| 304 |
|
| 305 |
let global_types = ref StringMap.empty
|
| 306 |
|
| 307 |
let mk_typ e =
|
| 308 |
if fv e = [] then type_node e
|
| 309 |
else raise_loc e.loc' (Pattern "Capture variables are not allowed in types")
|
| 310 |
|
| 311 |
|
| 312 |
let typ e =
|
| 313 |
mk_typ (compile !global_types e)
|
| 314 |
|
| 315 |
let pat e =
|
| 316 |
let e = compile !global_types e in
|
| 317 |
pat_node e
|
| 318 |
|
| 319 |
let register_global_types b =
|
| 320 |
let env = compile_many !global_types b in
|
| 321 |
List.iter (fun (v,_) ->
|
| 322 |
let d = Types.descr (mk_typ (StringMap.find v env)) in
|
| 323 |
(* let d = Types.normalize d in*)
|
| 324 |
Types.Print.register_global v d;
|
| 325 |
()
|
| 326 |
) b;
|
| 327 |
global_types := env
|
| 328 |
|
| 329 |
|
| 330 |
(* II. Build skeleton *)
|
| 331 |
|
| 332 |
module Fv = StringSet
|
| 333 |
|
| 334 |
let rec expr { loc = loc; descr = d } =
|
| 335 |
let (fv,td) =
|
| 336 |
match d with
|
| 337 |
| DebugTyper t -> (Fv.empty, Typed.DebugTyper (typ t))
|
| 338 |
| Forget (e,t) ->
|
| 339 |
let (fv,e) = expr e and t = typ t in
|
| 340 |
(fv, Typed.Forget (e,t))
|
| 341 |
| Var s -> (Fv.singleton s, Typed.Var s)
|
| 342 |
| Apply (e1,e2) ->
|
| 343 |
let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
|
| 344 |
(Fv.union fv1 fv2, Typed.Apply (e1,e2))
|
| 345 |
| Abstraction a ->
|
| 346 |
let iface = List.map (fun (t1,t2) -> (typ t1, typ t2)) a.fun_iface in
|
| 347 |
let t = List.fold_left
|
| 348 |
(fun accu (t1,t2) -> Types.cap accu (Types.arrow t1 t2))
|
| 349 |
Types.any iface in
|
| 350 |
let iface = List.map
|
| 351 |
(fun (t1,t2) -> (Types.descr t1, Types.descr t2))
|
| 352 |
iface in
|
| 353 |
let (fv0,body) = branches a.fun_body in
|
| 354 |
let fv = match a.fun_name with
|
| 355 |
| None -> fv0
|
| 356 |
| Some f -> Fv.remove f fv0 in
|
| 357 |
(fv,
|
| 358 |
Typed.Abstraction
|
| 359 |
{ Typed.fun_name = a.fun_name;
|
| 360 |
Typed.fun_iface = iface;
|
| 361 |
Typed.fun_body = body;
|
| 362 |
Typed.fun_typ = t;
|
| 363 |
Typed.fun_fv = Fv.elements fv
|
| 364 |
}
|
| 365 |
)
|
| 366 |
| Cst c -> (Fv.empty, Typed.Cst c)
|
| 367 |
| Pair (e1,e2) ->
|
| 368 |
let (fv1,e1) = expr e1 and (fv2,e2) = expr e2 in
|
| 369 |
(Fv.union fv1 fv2, Typed.Pair (e1,e2))
|
| 370 |
| Dot (e,l) ->
|
| 371 |
let (fv,e) = expr e in
|
| 372 |
(fv, Typed.Dot (e,l))
|
| 373 |
| RecordLitt r ->
|
| 374 |
let fv = ref Fv.empty in
|
| 375 |
let r = List.sort (fun (l1,_) (l2,_) -> compare l1 l2) r in
|
| 376 |
let r = List.map
|
| 377 |
(fun (l,e) ->
|
| 378 |
let (fv2,e) = expr e in fv := Fv.union !fv fv2; (l,e))
|
| 379 |
r in
|
| 380 |
let rec check = function
|
| 381 |
| (l1,_) :: (l2,_) :: _ when l1 = l2 ->
|
| 382 |
raise_loc loc (MultipleLabel l1)
|
| 383 |
| _ :: rem -> check rem
|
| 384 |
| _ -> () in
|
| 385 |
check r;
|
| 386 |
(!fv, Typed.RecordLitt r)
|
| 387 |
| Op (op,le) ->
|
| 388 |
let (fvs,ltes) = List.split (List.map expr le) in
|
| 389 |
let fv = List.fold_left Fv.union Fv.empty fvs in
|
| 390 |
(fv, Typed.Op (op,ltes))
|
| 391 |
| Match (e,b) ->
|
| 392 |
let (fv1,e) = expr e
|
| 393 |
and (fv2,b) = branches b in
|
| 394 |
(Fv.union fv1 fv2, Typed.Match (e, b))
|
| 395 |
| Map (e,b) ->
|
| 396 |
let (fv1,e) = expr e
|
| 397 |
and (fv2,b) = branches b in
|
| 398 |
(Fv.union fv1 fv2, Typed.Map (e, b))
|
| 399 |
| Try (e,b) ->
|
| 400 |
let (fv1,e) = expr e
|
| 401 |
and (fv2,b) = branches b in
|
| 402 |
(Fv.union fv1 fv2, Typed.Try (e, b))
|
| 403 |
in
|
| 404 |
fv,
|
| 405 |
{ Typed.exp_loc = loc;
|
| 406 |
Typed.exp_typ = Types.empty;
|
| 407 |
Typed.exp_descr = td;
|
| 408 |
}
|
| 409 |
|
| 410 |
and branches b =
|
| 411 |
let fv = ref Fv.empty in
|
| 412 |
let accept = ref Types.empty in
|
| 413 |
let b = List.map
|
| 414 |
(fun (p,e) ->
|
| 415 |
let (fv2,e) = expr e in
|
| 416 |
let p = pat p in
|
| 417 |
let fv2 = List.fold_right Fv.remove (Patterns.fv p) fv2 in
|
| 418 |
fv := Fv.union !fv fv2;
|
| 419 |
accept := Types.cup !accept (Types.descr (Patterns.accept p));
|
| 420 |
{ Typed.br_used = false;
|
| 421 |
Typed.br_pat = p;
|
| 422 |
Typed.br_body = e }
|
| 423 |
) b in
|
| 424 |
(!fv,
|
| 425 |
{
|
| 426 |
Typed.br_typ = Types.empty;
|
| 427 |
Typed.br_branches = b;
|
| 428 |
Typed.br_accept = !accept;
|
| 429 |
Typed.br_compiled = None;
|
| 430 |
}
|
| 431 |
)
|
| 432 |
|
| 433 |
let let_decl p e =
|
| 434 |
let (_,e) = expr e in
|
| 435 |
{ Typed.let_pat = pat p;
|
| 436 |
Typed.let_body = e;
|
| 437 |
Typed.let_compiled = None }
|
| 438 |
|
| 439 |
(* III. Type-checks *)
|
| 440 |
|
| 441 |
module Env = StringMap
|
| 442 |
type env = Types.descr Env.t
|
| 443 |
|
| 444 |
open Typed
|
| 445 |
|
| 446 |
let warning loc msg =
|
| 447 |
Format.fprintf Format.std_formatter
|
| 448 |
"Warning %a:@\n%s@\n" Location.print_loc loc msg
|
| 449 |
|
| 450 |
let check loc t s msg =
|
| 451 |
if not (Types.subtype t s) then raise_loc loc (Constraint (t, s, msg))
|
| 452 |
|
| 453 |
let rec type_check env e constr precise =
|
| 454 |
(* Format.fprintf Format.std_formatter "constr=%a precise=%b@\n"
|
| 455 |
Types.Print.print_descr constr precise;
|
| 456 |
*)
|
| 457 |
let d = type_check' e.exp_loc env e.exp_descr constr precise in
|
| 458 |
e.exp_typ <- Types.cup e.exp_typ d;
|
| 459 |
d
|
| 460 |
|
| 461 |
and type_check' loc env e constr precise = match e with
|
| 462 |
| Forget (e,t) ->
|
| 463 |
let t = Types.descr t in
|
| 464 |
ignore (type_check env e t false);
|
| 465 |
t
|
| 466 |
| Abstraction a ->
|
| 467 |
let t =
|
| 468 |
try Types.Arrow.check_strenghten a.fun_typ constr
|
| 469 |
with Not_found ->
|
| 470 |
raise_loc loc
|
| 471 |
(ShouldHave
|
| 472 |
(constr, "but the interface of the abstraction is not compatible"))
|
| 473 |
in
|
| 474 |
let env = match a.fun_name with
|
| 475 |
| None -> env
|
| 476 |
| Some f -> Env.add f a.fun_typ env in
|
| 477 |
List.iter
|
| 478 |
(fun (t1,t2) ->
|
| 479 |
ignore (type_check_branches loc env t1 a.fun_body t2 false)
|
| 480 |
) a.fun_iface;
|
| 481 |
t
|
| 482 |
|
| 483 |
| Match (e,b) ->
|
| 484 |
let t = type_check env e b.br_accept true in
|
| 485 |
type_check_branches loc env t b constr precise
|
| 486 |
|
| 487 |
| Try (e,b) ->
|
| 488 |
let te = type_check env e constr precise in
|
| 489 |
let tb = type_check_branches loc env Types.any b constr precise in
|
| 490 |
Types.cup te tb
|
| 491 |
|
| 492 |
| Pair (e1,e2) ->
|
| 493 |
let rects = Types.Product.get constr in
|
| 494 |
if Types.Product.is_empty rects then
|
| 495 |
raise_loc loc (ShouldHave (constr,"but it is a pair."));
|
| 496 |
let pi1 = Types.Product.pi1 rects in
|
| 497 |
|
| 498 |
let t1 = type_check env e1 (Types.Product.pi1 rects)
|
| 499 |
(precise || (Types.Product.need_second rects))in
|
| 500 |
let rects = Types.Product.restrict_1 rects t1 in
|
| 501 |
let t2 = type_check env e2 (Types.Product.pi2 rects) precise in
|
| 502 |
if precise then
|
| 503 |
Types.times (Types.cons t1) (Types.cons t2)
|
| 504 |
else
|
| 505 |
constr
|
| 506 |
|
| 507 |
| RecordLitt r ->
|
| 508 |
let rconstr = Types.Record.get constr in
|
| 509 |
if Types.Record.is_empty rconstr then
|
| 510 |
raise_loc loc (ShouldHave (constr,"but it is a record."));
|
| 511 |
|
| 512 |
let (rconstr,res) =
|
| 513 |
List.fold_left
|
| 514 |
(fun (rconstr,res) (l,e) ->
|
| 515 |
let rconstr = Types.Record.restrict_label_present rconstr l in
|
| 516 |
let pi = Types.Record.project_field rconstr l in
|
| 517 |
if Types.Record.is_empty rconstr then
|
| 518 |
raise_loc loc
|
| 519 |
(ShouldHave (constr,(Printf.sprintf
|
| 520 |
"Field %s is not allowed here."
|
| 521 |
(Types.label_name l)
|
| 522 |
)
|
| 523 |
));
|
| 524 |
let t = type_check env e pi true in
|
| 525 |
let rconstr = Types.Record.restrict_field rconstr l t in
|
| 526 |
|
| 527 |
let res =
|
| 528 |
if precise
|
| 529 |
then Types.cap res (Types.record l false (Types.cons t))
|
| 530 |
else res in
|
| 531 |
(rconstr,res)
|
| 532 |
) (rconstr, if precise then Types.Record.any else constr) r
|
| 533 |
in
|
| 534 |
res
|
| 535 |
|
| 536 |
| Map (e,b) ->
|
| 537 |
let t = type_check env e (Sequence.star b.br_accept) true in
|
| 538 |
|
| 539 |
let constr' = Sequence.approx (Types.cap Sequence.any constr) in
|
| 540 |
let exact = Types.subtype (Sequence.star constr') constr in
|
| 541 |
(* Note:
|
| 542 |
- could be more precise by integrating the decomposition
|
| 543 |
of constr inside Sequence.map.
|
| 544 |
*)
|
| 545 |
let res =
|
| 546 |
Sequence.map
|
| 547 |
(fun t ->
|
| 548 |
type_check_branches loc env t b constr' (precise || (not exact)))
|
| 549 |
t in
|
| 550 |
if not exact then check loc res constr "";
|
| 551 |
if precise then res else constr
|
| 552 |
| Op ("@", [e1;e2]) ->
|
| 553 |
let constr' = Sequence.star
|
| 554 |
(Sequence.approx (Types.cap Sequence.any constr)) in
|
| 555 |
let exact = Types.subtype constr' constr in
|
| 556 |
if exact then
|
| 557 |
let t1 = type_check env e1 constr' precise
|
| 558 |
and t2 = type_check env e2 constr' precise in
|
| 559 |
if precise then Sequence.concat t1 t2 else constr
|
| 560 |
else
|
| 561 |
(* Note:
|
| 562 |
the knownledge of t1 may makes it useless to
|
| 563 |
check t2 with 'precise' ... *)
|
| 564 |
let t1 = type_check env e1 constr' true
|
| 565 |
and t2 = type_check env e2 constr' true in
|
| 566 |
let res = Sequence.concat t1 t2 in
|
| 567 |
check loc res constr "";
|
| 568 |
if precise then res else constr
|
| 569 |
| Op ("flatten", [e]) ->
|
| 570 |
let constr' = Sequence.star
|
| 571 |
(Sequence.approx (Types.cap Sequence.any constr)) in
|
| 572 |
let sconstr' = Sequence.star constr' in
|
| 573 |
let exact = Types.subtype constr' constr in
|
| 574 |
if exact then
|
| 575 |
let t = type_check env e sconstr' precise in
|
| 576 |
if precise then Sequence.flatten t else constr
|
| 577 |
else
|
| 578 |
let t = type_check env e sconstr' true in
|
| 579 |
let res = Sequence.flatten t in
|
| 580 |
check loc res constr "";
|
| 581 |
if precise then res else constr
|
| 582 |
| _ ->
|
| 583 |
let t : Types.descr = compute_type' loc env e in
|
| 584 |
check loc t constr "";
|
| 585 |
t
|
| 586 |
|
| 587 |
and compute_type env e =
|
| 588 |
type_check env e Types.any true
|
| 589 |
|
| 590 |
and compute_type' loc env = function
|
| 591 |
| DebugTyper t -> Types.descr t
|
| 592 |
| Var s ->
|
| 593 |
(try Env.find s env
|
| 594 |
with Not_found -> raise_loc loc (UnboundId s)
|
| 595 |
)
|
| 596 |
| Apply (e1,e2) ->
|
| 597 |
let t1 = type_check env e1 Types.Arrow.any true in
|
| 598 |
let t1 = Types.Arrow.get t1 in
|
| 599 |
let dom = Types.Arrow.domain t1 in
|
| 600 |
if Types.Arrow.need_arg t1 then
|
| 601 |
let t2 = type_check env e2 dom true in
|
| 602 |
Types.Arrow.apply t1 t2
|
| 603 |
else
|
| 604 |
(ignore (type_check env e2 dom false); Types.Arrow.apply_noarg t1)
|
| 605 |
| Cst c -> Types.constant c
|
| 606 |
| Dot (e,l) ->
|
| 607 |
let t = type_check env e Types.Record.any true in
|
| 608 |
(try (Types.Record.project t l)
|
| 609 |
with Not_found -> raise_loc loc (WrongLabel(t,l)))
|
| 610 |
| Op (op, el) ->
|
| 611 |
let args = List.map (fun e -> (e.exp_loc, compute_type env e)) el in
|
| 612 |
type_op loc op args
|
| 613 |
| Map (e,b) ->
|
| 614 |
let t = compute_type env e in
|
| 615 |
Sequence.map (fun t -> type_check_branches loc env t b Types.any true) t
|
| 616 |
|
| 617 |
(* We keep these cases here to allow comparison and benchmarking ...
|
| 618 |
Just comment the corresponding cases in type_check' to
|
| 619 |
activate these ones.
|
| 620 |
*)
|
| 621 |
| Pair (e1,e2) ->
|
| 622 |
let t1 = compute_type env e1
|
| 623 |
and t2 = compute_type env e2 in
|
| 624 |
Types.times (Types.cons t1) (Types.cons t2)
|
| 625 |
| RecordLitt r ->
|
| 626 |
List.fold_left
|
| 627 |
(fun accu (l,e) ->
|
| 628 |
let t = compute_type env e in
|
| 629 |
let t = Types.record l false (Types.cons t) in
|
| 630 |
Types.cap accu t
|
| 631 |
) Types.Record.any r
|
| 632 |
|
| 633 |
|
| 634 |
| _ -> assert false
|
| 635 |
|
| 636 |
and type_check_branches loc env targ brs constr precise =
|
| 637 |
if Types.is_empty targ then Types.empty
|
| 638 |
else (
|
| 639 |
brs.br_typ <- Types.cup brs.br_typ targ;
|
| 640 |
branches_aux loc env targ
|
| 641 |
(if precise then Types.empty else constr)
|
| 642 |
constr precise brs.br_branches
|
| 643 |
)
|
| 644 |
|
| 645 |
and branches_aux loc env targ tres constr precise = function
|
| 646 |
| [] -> raise_loc loc (NonExhaustive targ)
|
| 647 |
| b :: rem ->
|
| 648 |
let p = b.br_pat in
|
| 649 |
let acc = Types.descr (Patterns.accept p) in
|
| 650 |
|
| 651 |
let targ' = Types.cap targ acc in
|
| 652 |
if Types.is_empty targ'
|
| 653 |
then branches_aux loc env targ tres constr precise rem
|
| 654 |
else
|
| 655 |
( b.br_used <- true;
|
| 656 |
let res = Patterns.filter targ' p in
|
| 657 |
let env' = List.fold_left
|
| 658 |
(fun env (x,t) -> Env.add x (Types.descr t) env)
|
| 659 |
env res in
|
| 660 |
let t = type_check env' b.br_body constr precise in
|
| 661 |
let tres = if precise then Types.cup t tres else tres in
|
| 662 |
let targ'' = Types.diff targ acc in
|
| 663 |
if (Types.non_empty targ'') then
|
| 664 |
branches_aux loc env targ'' tres constr precise rem
|
| 665 |
else
|
| 666 |
tres
|
| 667 |
)
|
| 668 |
|
| 669 |
and type_let_decl env l =
|
| 670 |
let acc = Types.descr (Patterns.accept l.let_pat) in
|
| 671 |
let t = type_check env l.let_body acc true in
|
| 672 |
let res = Patterns.filter t l.let_pat in
|
| 673 |
List.map (fun (x,t) -> (x, Types.descr t)) res
|
| 674 |
|
| 675 |
and type_rec_funs env l =
|
| 676 |
let types =
|
| 677 |
List.fold_left
|
| 678 |
(fun accu -> function {let_body={exp_descr=Abstraction a}} as l ->
|
| 679 |
let t = a.fun_typ in
|
| 680 |
let acc = Types.descr (Patterns.accept l.let_pat) in
|
| 681 |
if not (Types.subtype t acc) then
|
| 682 |
raise_loc l.let_body.exp_loc (NonExhaustive (Types.diff t acc));
|
| 683 |
let res = Patterns.filter t l.let_pat in
|
| 684 |
List.fold_left (fun accu (x,t) -> (x, Types.descr t)::accu) accu res
|
| 685 |
| _ -> assert false) [] l
|
| 686 |
in
|
| 687 |
let env' = List.fold_left (fun env (x,t) -> Env.add x t env) env types in
|
| 688 |
List.iter
|
| 689 |
(function { let_body = { exp_descr = Abstraction a } } as l ->
|
| 690 |
ignore (type_check env' l.let_body Types.any false)
|
| 691 |
| _ -> assert false) l;
|
| 692 |
types
|
| 693 |
|
| 694 |
|
| 695 |
and type_op loc op args =
|
| 696 |
match (op,args) with
|
| 697 |
| "+", [loc1,t1; loc2,t2] ->
|
| 698 |
type_int_binop Intervals.add loc1 t1 loc2 t2
|
| 699 |
| "-", [loc1,t1; loc2,t2] ->
|
| 700 |
type_int_binop Intervals.sub loc1 t1 loc2 t2
|
| 701 |
| ("*" | "/"), [loc1,t1; loc2,t2] ->
|
| 702 |
type_int_binop (fun i1 i2 -> Intervals.any) loc1 t1 loc2 t2
|
| 703 |
| "@", [loc1,t1; loc2,t2] ->
|
| 704 |
check loc1 t1 Sequence.any
|
| 705 |
"The first argument of @ must be a sequence";
|
| 706 |
Sequence.concat t1 t2
|
| 707 |
| "flatten", [loc1,t1] ->
|
| 708 |
check loc1 t1 Sequence.seqseq
|
| 709 |
"The argument of flatten must be a sequence of sequences";
|
| 710 |
Sequence.flatten t1
|
| 711 |
| "load_xml", [loc1,t1] ->
|
| 712 |
check loc1 t1 Sequence.string
|
| 713 |
"The argument of load_xml must be a string (filename)";
|
| 714 |
Types.any
|
| 715 |
| "raise", [loc1,t1] ->
|
| 716 |
Types.empty
|
| 717 |
| "print_xml", [loc1,t1] ->
|
| 718 |
Sequence.string
|
| 719 |
| "int_of", [loc1,t1] ->
|
| 720 |
check loc1 t1 Sequence.string
|
| 721 |
"The argument of int_of must a string";
|
| 722 |
if not (Types.subtype t1 Builtin.intstr) then
|
| 723 |
warning loc "This application of int_of may fail";
|
| 724 |
Types.interval Intervals.any
|
| 725 |
| _ -> assert false
|
| 726 |
|
| 727 |
and type_int_binop f loc1 t1 loc2 t2 =
|
| 728 |
if not (Types.Int.is_int t1) then
|
| 729 |
raise_loc loc1
|
| 730 |
(Constraint
|
| 731 |
(t1,Types.Int.any,
|
| 732 |
"The first argument must be an integer"));
|
| 733 |
if not (Types.Int.is_int t2) then
|
| 734 |
raise_loc loc2
|
| 735 |
(Constraint
|
| 736 |
(t2,Types.Int.any,
|
| 737 |
"The second argument must be an integer"));
|
| 738 |
Types.Int.put
|
| 739 |
(f (Types.Int.get t1) (Types.Int.get t2));
|
| 740 |
|
| 741 |
|