Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 52c2ccf

Browse files
Add section on kind inference via method signature
Add section about kind inference through method signature to show a middle step between having `class OfKindType a` being forced to kind `*` and having to add the `KindSignatures` pragma.
1 parent 9a132d5 commit 52c2ccf

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

‎03-KindSignatures/src/KindSignatures.hs‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,42 @@ instance OfKindType (Maybe (IO ())) -- to get /these/ to compile?
6161
In the instance declaration for ‘OfKindType Maybe’
6262
6363
The big problem here is that, in the absence of a better idea, GHC will
64-
assume that a typeclass parameter is of kind 'Type'. How do we tell it
65-
otherwise? We provide a kind signature! This is exactly what the extension
66-
exists to do:
64+
assume that a typeclass parameter is of kind 'Type'.
65+
66+
How do we tell it otherwise?
67+
68+
One way is to give GHC more information about our type by filling in method
69+
signatures for our class.
70+
71+
For example, let's take a look at a simplfied definition for the Functor class:
72+
-}
73+
74+
class Functor f where
75+
fmap :: (a -> b) -> f a -> f b
76+
77+
{-
78+
Although @f@ is on its own here in the class definition, if we take a closer
79+
look at the signature for @fmap@ we can see that @f@ never appears on its
80+
own, but it sits right in front of another type variable every time it shows
81+
up: @f a@ and @f b@.
82+
83+
This information alone is enough for GHC to infer that the type of @f@ is
84+
actually ’* -> *’.
85+
86+
Let's try another example:
87+
-}
88+
89+
class Stuff a where
90+
thing :: a b c d
91+
92+
{-
93+
Here, we can see @a@ has 3 more type variables right after it: @b@, @c@ and @d@.
94+
This means that @a@ will be given a kind of ’* -> * -> * -> *’.
95+
96+
Another way to let GHC know this is by explicitly providing it with a kind
97+
signature in the class definition itself.
98+
99+
This is exactly what the KindSignatures extension allows us to do:
67100
-}
68101

69102
class OfKindTypeToType (a :: Type -> Type)
@@ -92,6 +125,7 @@ class MyFavouriteBifunctor (element :: (Type -> Type -> Type))
92125
instance MyFavouriteBifunctor Either
93126
instance MyFavouriteBifunctor (,)
94127

128+
95129
{-
96130
So, right now, we can think of all our kinds as being 'Type' or @a -> b@ for
97131
some kinds @a@ and @b@, and that's it. However, there's one more that is

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /