0
$\begingroup$

In the paper Practical type inference for arbitrary-rank types, the covariance and contravariance rules are essential when dealing with function types. Covariance applies to the return types of functions, while contravariance applies to the argument types.

In (∀b. [b] → [b]) → Bool < (∀a. a → a) → Bool, the types are compared in a contravariant manner for the input types, which means the more general type must be on the right side. Here, the type a → a is more general than [b] → [b], making the inequality hold. However, in the case of [b] → Int < a → Int, the argument types are compared contravariantly. For contravariance to hold, the left-hand side argument must be more specific than the right-hand side. Since a can be any type and [b] is a specific type (a list of b), the inequality holds but it should not, for example you can not type annotate length function with a -> Int.

In my opinion, the correct relationship would be a → Int < [b] → Int, and this happens by matching a with [b].

I've been reading some papers on type inference, particularly around co- and contravariant rules for functions. I understand where the contravariant rule comes from, but I don't understand why it doesn't work for examples like [b] → Int < a → Int.

Could someone explain why the contravariant rule doesn't apply here, or if I'm misunderstanding something fundamental?

asked Oct 10, 2024 at 5:18
$\endgroup$
3
  • $\begingroup$ Where did you get ([b] -> Int) < (a -> Int) from? You are saying it is wrong but it is not clear where the claim that it is right comes from. $\endgroup$ Commented Oct 10, 2024 at 6:26
  • $\begingroup$ Cross-posted from stackoverflow.com/questions/79072356/… $\endgroup$ Commented Oct 10, 2024 at 7:48
  • $\begingroup$ @Li-yaoXia I was not careful with the scope of foralls. ((∀b.[b]) -> Int) < ((∀a.a) -> Int) and (∀a.a -> Int) < (∀b.[b] -> Int). $\endgroup$ Commented Oct 10, 2024 at 21:51

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.