I'm trying to implement language with type inference based on Hindley-Milner algorithm. But I don't know how to infer types for recursive functions:
f = g
g = f
Here I must generate and solve constraints for f
and g
together because if I will do it for one function earlier it will not know the type of the other function. But I can't do it for all functions in module at the same time:
f = h 0
g = h "str"
h _ = 0
Here in f
I have h :: Int -> Int
and in g
I have h :: String -> Int
, that will produce error if I will solve it at the same time but will not if I will infer the type of h
before types for f
and g
(h :: forall a. a -> Int
and can be used in f
and g
with different substitution for a
).
How can I infer these types?