How does GHCi pick names for type variables?

人走茶凉 提交于 2019-11-29 06:09:21

As I understand it, ghci chooses names in the same order that it infers the types. It uses the naming scheme as you mentioned to decide the type name of the result, which is [b] because that is the type name specified in the definition of map. It then decides that the function that is the first parameter to map should return something of type b also.

The remaining type variable to be named is thus the type variable for the second element in the argument tuple to fst, and again, it looks at the definition of fst to decide which name to use. The definition of fst :: (a, b) -> a, so b would be the preferred name here, but since b is already taken, it appends a 1 so that it becomes b1.

I think that this system has advantages in situations where you don't deal with arbitrary types as is the case here. If the resulting type looks something like this, for example:

castAdd :: (Num n, Num n1, Num n2) => n -> n1 -> n2

... it is arguably more readable than:

castAdd :: (Num a, Num b, Num c) => a -> b -> c

... because you can mostly rely on that n# signifies a numeric type, since the class definition for Num is class Num n where ....

EDIT: Yes, I know that castAdd is impossible to implement, but it's just a type example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!