Consider functions f1 and f2 in for example Haskell or ML. Suppose f2 calls f1. Does type inference just use the information of what f1 can call, which amounts to just looking at the definition of f1. Or does it also look at how f2 calls f1 when doing type inference on f1. For example f2 might pass in literals when calling f1 which would restrict the argument types of f1.
Share
Type systems don’t use information about the caller to determine the types a function could handle — that would be both overly restrictive and impossible to achieve in general. E.g., suppose (Haskell)
would henceforth restrict the type of
headfrom[a] -> ato[Int] -> Int; afterwards,head ["hello", "world"]would be impossible and we’d have to redefineheadthe next time we want to use it on a different type. However, in the context of the definition ofone,headactually has the type[Int] -> Intas the variables in its type get instantiated. But that doesn’t change the global definition ofheador its type.(In practice, a compiler may specialize a function that it knows is going to be called in only a few situations and adapt the code to the specific types being passed in, as long as it doesn’t change program semantics.)