What is the assumption made in “Learn You a Haskell” when deducing the kind?

一笑奈何 提交于 2021-02-18 09:05:21

问题


This question is not subjective. A very specific verb is used in the referenced book, and I'd like to understand what the implication of that phrasing is, because I'm afraid I'm misunderstanding something.

From Learn You a Haskell, the following paragraph is the third and last one containing "we assume *".

data Barry t k p = Barry { yabba :: p, dabba :: t k }  

And now we want to make it an instance of Functor. Functor wants types of kind * -> * but Barry doesn't look like it has that kind. What is the kind of Barry? Well, we see it takes three type parameters, so it's going to be something -> something -> something -> *. It's safe to say that p is a concrete type and thus has a kind of *. For k, we assume * and so by extension, t has a kind of * -> *. Now let's just replace those kinds with the somethings that we used as placeholders and we see it has a kind of (* -> *) -> * -> * -> *.

Why are we assuming anything at all? Upon reading "we assume X (i.e. we assume that X is true)" it is natural for me to think that we should also consider the case that X is false. In the specific case of the example, couldn't t be of kind (* -> *) -> * and k of kind (* -> *)? If this was the case, whatever t and k actually were, t k would still be a concrete type, no?

I see that the whole line of reasoning is then checked against the compiler, but I don't think the compiler assumes. If it does, I'd like to know what, if it doesn't then again I'm afraid I'm missing the meaning of the paragraph.


回答1:


In fact, the compiler does assume! But you can ask it not to with the PolyKinds extension. You can read about it in more detail here. With that extension turned on, the kind of Barry will be forall k. (k -> *) -> k -> * -> *.




回答2:


Good point. The author makes a needless assumtion. Perhaps just to make it easier to understand in his Type Foo chapter but people like yourself may rightfully question this.

Both t, k and p are type variables. As we see from yabba :: p it can live alone so it's like a constant function, as if it was a value instead of a type, it's type signature would say Int or Char, whatever... you name it. But since it is a type then it's kind signature is *.

However t type here takes a type variable k to construct a type (dabba :: t k) so we are sure that (no assumtion here) t has a kind signature like * -> * and k has *.

Once we know this... the type Barry t k p's kind signature is (* -> *) -> * -> * -> * which means it takes t then k and then p and give us Barry type.

Edit Make sure to read @luqui's comment below.



来源:https://stackoverflow.com/questions/59230320/what-is-the-assumption-made-in-learn-you-a-haskell-when-deducing-the-kind

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