package merlin-lib
Merlin's libraries
Install
Dune Dependency
Authors
Maintainers
Sources
merlin-5.5-503.tbz
sha256=67da3b34f2fea07678267309f61da4a2c6f08298de0dc59655b8d30fd8269af1
sha512=1fb3b5180d36aa82b82a319e15b743b802b6888f0dc67645baafdb4e18dfc23a7b90064ec9bc42f7424061cf8cde7f8839178d8a8537bf4596759f3ff4891873
doc/src/merlin-lib.ocaml_typing/typedecl_variance.ml.html
Source file typedecl_variance.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 251 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
(**************************************************************************) (* *) (* OCaml *) (* *) (* Gabriel Scherer, projet Parsifal, INRIA Saclay *) (* Rodolphe Lepigre, projet Deducteam, INRIA Saclay *) (* *) (* Copyright 2018 Institut National de Recherche en Informatique et *) (* en Automatique. *) (* *) (* All rights reserved. This file is distributed under the terms of *) (* the GNU Lesser General Public License version 2.1, with the *) (* special exception on linking described in the file LICENSE. *) (* *) (**************************************************************************) open Asttypes open Types module TypeSet = Btype.TypeSet module TypeMap = Btype.TypeMap type surface_variance = bool * bool * bool type variance_variable_context = | Type_declaration of Ident.t * type_declaration | Gadt_constructor of constructor_declaration | Extension_constructor of Ident.t * extension_constructor type variance_variable_error = | No_variable | Variance_not_reflected | Variance_not_deducible type variance_error = | Variance_not_satisfied of int | Variance_variable_error of { error : variance_variable_error; context : variance_variable_context; variable : type_expr } type error = | Bad_variance of variance_error * surface_variance * surface_variance | Varying_anonymous exception Error of Location.t * error (* Compute variance *) let get_variance ty visited = try TypeMap.find ty !visited with Not_found -> Variance.null let compute_variance env visited vari ty = let rec compute_variance_rec vari ty = (* Format.eprintf "%a: %x@." Printtyp.type_expr ty (Obj.magic vari); *) let vari' = get_variance ty visited in if Variance.subset vari vari' then () else let vari = Variance.union vari vari' in visited := TypeMap.add ty vari !visited; let compute_same = compute_variance_rec vari in match get_desc ty with Tarrow (_, ty1, ty2, _) -> compute_variance_rec (Variance.conjugate vari) ty1; compute_same ty2 | Ttuple tl -> List.iter compute_same tl | Tconstr (path, tl, _) -> let open Variance in if tl = [] then () else begin try let decl = Env.find_type path env in List.iter2 (fun ty v -> compute_variance_rec (compose vari v) ty) tl decl.type_variance with Not_found -> List.iter (compute_variance_rec unknown) tl end | Tobject (ty, _) -> compute_same ty | Tfield (_, _, ty1, ty2) -> compute_same ty1; compute_same ty2 | Tsubst _ -> assert false | Tvariant row -> List.iter (fun (_,f) -> match row_field_repr f with Rpresent (Some ty) -> compute_same ty | Reither (_, tyl, _) -> let v = Variance.(inter vari unknown) in (* cf PR#7269 *) List.iter (compute_variance_rec v) tyl | _ -> ()) (row_fields row); compute_same (row_more row) | Tpoly (ty, _) -> compute_same ty | Tvar _ | Tnil | Tlink _ | Tunivar _ -> () | Tpackage (_, fl) -> let v = Variance.(compose vari full) in List.iter (fun (_, ty) -> compute_variance_rec v ty) fl in compute_variance_rec vari ty let make p n i = let open Variance in set_if p May_pos (set_if n May_neg (set_if i Inj null)) let injective = Variance.(set Inj null) let compute_variance_type env ~check (required, loc) decl tyl = (* Requirements *) let check_injectivity = Btype.type_kind_is_abstract decl in let required = List.map (fun (c,n,i) -> let i = if check_injectivity then i else false in if c || n then (c,n,i) else (true,true,i)) required in (* Prepare *) let params = decl.type_params in let tvl = ref TypeMap.empty in (* Compute occurrences in the body *) let open Variance in List.iter (fun (cn,ty) -> compute_variance env tvl (if cn then full else covariant) ty) tyl; (* Infer injectivity of constrained parameters *) if check_injectivity then List.iter (fun ty -> if Btype.is_Tvar ty || mem Inj (get_variance ty tvl) then () else let visited = ref TypeSet.empty in let rec check ty = if TypeSet.mem ty !visited then () else begin visited := TypeSet.add ty !visited; if mem Inj (get_variance ty tvl) then () else match get_desc ty with | Tvar _ -> raise Exit | Tconstr _ -> let old = !visited in begin try Btype.iter_type_expr check ty with Exit -> visited := old; let ty' = Ctype.expand_head_opt env ty in if eq_type ty ty' then raise Exit else check ty' end | _ -> Btype.iter_type_expr check ty end in try check ty; compute_variance env tvl injective ty with Exit -> ()) params; begin match check with | None -> () | Some context -> (* Check variance of parameters *) let pos = ref 0 in List.iter2 (fun ty (c, n, i) -> incr pos; let var = get_variance ty tvl in let (co,cn) = get_upper var and ij = mem Inj var in if Btype.is_Tvar ty && (co && not c || cn && not n) || not ij && i then raise (Error(loc, Bad_variance (Variance_not_satisfied !pos, (co,cn,ij), (c,n,i))))) params required; (* Check propagation from constrained parameters *) let args = Btype.newgenty (Ttuple params) in let fvl = Ctype.free_variables args in let fvl = List.filter (fun v -> not (List.exists (eq_type v) params)) fvl in (* If there are no extra variables there is nothing to do *) if fvl = [] then () else let tvl2 = ref TypeMap.empty in List.iter2 (fun ty (p,n,_) -> if Btype.is_Tvar ty then () else let v = if p then if n then full else covariant else conjugate covariant in compute_variance env tvl2 v ty) params required; let visited = ref TypeSet.empty in let rec check ty = if TypeSet.mem ty !visited then () else let visited' = TypeSet.add ty !visited in visited := visited'; let v1 = get_variance ty tvl in let snap = Btype.snapshot () in let v2 = TypeMap.fold (fun t vt v -> if Ctype.is_equal env false [ty] [t] then union vt v else v) !tvl2 null in Btype.backtrack snap; let (c1,n1) = get_upper v1 and (c2,n2,i2) = get_lower v2 in if c1 && not c2 || n1 && not n2 then begin match List.find_opt (eq_type ty) fvl with | Some variable -> let error = if not i2 then No_variable else if c2 || n2 then Variance_not_reflected else Variance_not_deducible in let variance_error = Variance_variable_error { error; context; variable } in raise (Error (loc , Bad_variance ( variance_error , (c1,n1,false) , (c2,n2,false)))) | None -> Btype.iter_type_expr check ty end in List.iter (fun (_,ty) -> check ty) tyl; end; List.map2 (fun ty (p, n, _i) -> let v = get_variance ty tvl in let tr = decl.type_private in (* Use required variance where relevant *) let concr = not (Btype.type_kind_is_abstract decl) in let (p, n) = if tr = Private || not (Btype.is_Tvar ty) then (p, n) (* set *) else (false, false) (* only check *) and i = concr in let v = union v (make p n i) in if not concr || Btype.is_Tvar ty then v else union v (if p then if n then full else covariant else conjugate covariant)) params required let add_false = List.map (fun ty -> false, ty) (* A parameter is constrained if it is either instantiated, or it is a variable appearing in another parameter *) let constrained vars ty = match get_desc ty with | Tvar _ -> List.exists (List.exists (eq_type ty)) vars | _ -> true let for_constr = function | Types.Cstr_tuple l -> add_false l | Types.Cstr_record l -> List.map (fun {Types.ld_mutable; ld_type} -> (ld_mutable = Mutable, ld_type)) l let compute_variance_gadt env ~check (required, loc as rloc) decl (tl, ret_type_opt) = match ret_type_opt with | None -> compute_variance_type env ~check rloc {decl with type_private = Private} (for_constr tl) | Some ret_type -> match get_desc ret_type with | Tconstr (_, tyl, _) -> (* let tyl = List.map (Ctype.expand_head env) tyl in *) let fvl = List.map (Ctype.free_variables ?env:None) tyl in let _ = List.fold_left2 (fun (fv1,fv2) ty (c,n,_) -> match fv2 with [] -> assert false | fv :: fv2 -> (* fv1 @ fv2 = free_variables of other parameters *) if (c||n) && constrained (fv1 @ fv2) ty then raise (Error(loc, Varying_anonymous)); (fv :: fv1, fv2)) ([], fvl) tyl required in compute_variance_type env ~check rloc {decl with type_params = tyl; type_private = Private} (for_constr tl) | _ -> assert false let compute_variance_extension env decl ext rloc = let check = Some (Extension_constructor (ext.Typedtree.ext_id, ext.Typedtree.ext_type)) in let ext = ext.Typedtree.ext_type in compute_variance_gadt env ~check rloc {decl with type_params = ext.ext_type_params} (ext.ext_args, ext.ext_ret_type) let compute_variance_gadt_constructor env ~check rloc decl tl = let check = match check with | Some _ -> Some (Gadt_constructor tl) | None -> None in compute_variance_gadt env ~check rloc decl (tl.Types.cd_args, tl.Types.cd_res) let compute_variance_decl env ~check decl (required, _ as rloc) = let check = Option.map (fun id -> Type_declaration (id, decl)) check in let abstract = Btype.type_kind_is_abstract decl in if (abstract || decl.type_kind = Type_open) && decl.type_manifest = None then List.map (fun (c, n, i) -> make (not n) (not c) (not abstract || i)) required else begin let mn = match decl.type_manifest with None -> [] | Some ty -> [ false, ty ] in let vari = match decl.type_kind with Type_abstract _ | Type_open -> compute_variance_type env ~check rloc decl mn | Type_variant (tll,_rep) -> if List.for_all (fun c -> c.Types.cd_res = None) tll then compute_variance_type env ~check rloc decl (mn @ List.flatten (List.map (fun c -> for_constr c.Types.cd_args) tll)) else begin let vari = List.map (fun ty -> compute_variance_type env ~check rloc {decl with type_private = Private} (add_false [ ty ]) ) (Option.to_list decl.type_manifest) in let constructor_variance = List.map (compute_variance_gadt_constructor env ~check rloc decl) tll in match List.append vari constructor_variance with | vari :: rem -> List.fold_left (List.map2 Variance.union) vari rem | _ -> assert false end | Type_record (ftl, _) -> compute_variance_type env ~check rloc decl (mn @ List.map (fun {Types.ld_mutable; ld_type} -> (ld_mutable = Mutable, ld_type)) ftl) in if mn = [] || not abstract then List.map Variance.strengthen vari else vari end let is_hash id = let s = Ident.name id in String.length s > 0 && s.[0] = '#' let check_variance_extension env decl ext rloc = (* TODO: refactorize compute_variance_extension *) ignore (compute_variance_extension env decl ext rloc) let compute_decl env ~check decl req = compute_variance_decl env ~check decl (req, decl.type_loc) let check_decl env id decl req = ignore (compute_variance_decl env ~check:(Some id) decl (req, decl.type_loc)) type prop = Variance.t list type req = surface_variance list let property : (prop, req) Typedecl_properties.property = let open Typedecl_properties in let eq li1 li2 = try List.for_all2 Variance.eq li1 li2 with _ -> false in let merge ~prop ~new_prop = List.map2 Variance.union prop new_prop in let default decl = List.map (fun _ -> Variance.null) decl.type_params in let compute env decl req = compute_decl env ~check:None decl req in let update_decl decl variance = { decl with type_variance = variance } in let check env id decl req = if is_hash id then () else check_decl env id decl req in { eq; merge; default; compute; update_decl; check; } let transl_variance (v, i) = let co, cn = match v with | Covariant -> (true, false) | Contravariant -> (false, true) | NoVariance -> (false, false) in (co, cn, match i with Injective -> true | NoInjectivity -> false) let variance_of_params ptype_params = List.map transl_variance (List.map snd ptype_params) let variance_of_sdecl sdecl = variance_of_params sdecl.Parsetree.ptype_params let update_decls env sdecls decls = let required = List.map variance_of_sdecl sdecls in Typedecl_properties.compute_property property env decls required let update_class_decls env cldecls = let decls, required = List.fold_right (fun (obj_id, obj_abbr, _clty, _cltydef, ci) (decls, req) -> (obj_id, obj_abbr) :: decls, variance_of_params ci.Typedtree.ci_params :: req) cldecls ([],[]) in let decls = Typedecl_properties.compute_property property env decls required in List.map2 (fun (_,decl) (_, _, clty, cltydef, _) -> let variance = decl.type_variance in (decl, {clty with cty_variance = variance}, {cltydef with clty_variance = variance; clty_hash_type = {cltydef.clty_hash_type with type_variance = variance} })) decls cldecls
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>