Been reading Learn You A Haskell For a Great Good ! and have big trouble with understanding instance and kind.
Q1: So the type t in Tofu t acts as a function with the kind signature (* -> (* -> *)) -> * ? And the overall kind signature of tofu is * -> *, isnt it? since (* -> *) -> * results in * and so does (* -> (* -> *)) -> *
Q2: When we want to make Frank a b instance of the typeclass Tofu t, data type Frank a b must also have the same kind with t. That means kind of a is *, b is * -> *, and b a which will be (* -> *) -> * which results in *. Is that correct?
Q3: The x in tofu x represents j a since both have the kind of *. Frank with its kind (* -> (* -> *)) -> * is applied on x. But I'm not sure how presenting j a as x will distinguish the x in tofu x which is j a and the x in Frank x which is a j.
I'm kind of new to the idea of having a function inside data type or class (Ex: b in Frank a b or t in Tofu t) which is a bit confusing
I leave the link here since quoting would make the post look unnecessarily long. link
class Tofu t where
tofu :: j a -> t a j
data Frank a b = Frank {frankField :: b a}
instance Tofu Frank where
tofu x = Frank x
Q1:
t's kind is* -> (* -> *) -> *, or more explicitly* -> ((* -> *) -> *), not(* -> (* -> *)) -> *.tofudoesn't have a kind signature, only type constructors do; its type's kind is*. So are its argument's and result's types. And same for any function.Q2: You start with a wrong supposition:
instance Tofu Frankmakes theFranktype constructor an instance ofTofu, notFrank a b. So it'sFrankwhich must have the same kind ast, notFrank a b(which has kind*).No,
b ais an application ofbof kind* -> *toaof kind*, so the application has kind*. Exactly as ifbwas a function of typex -> y, andawas a value of typex,b awould have typey, not(x -> y) -> x: just replacexandyby*.Q3:
"Has type", not "represents".
xdoesn't have a kind, because it isn't a type.No, in
it's the
Frankdata constructor which is applied tox, not the type constructor. It's a function with signatureb a1 -> Frank a1 b(renamingaso you don't confuse it withtofu's). Sob ~ janda1 ~ a.