Recently, I've been studying Haskell for the first time and I'm just reading the library, but I have doubts about the use of the type operator.
For example, in the following example, there is no right side and I don't know what synonym it is.
https://github.com/RaphaelJ/friday/blob/master/src/Vision/Image/Class.hs#L93
class Storable (ImagePixeli) =>MaskedImage i where
type ImagePixeli
Also, in the following example, the left side ImagePixel (Manifest p) is more complicated than the right side p.
I don't really understand why you use Sinonim.
https://github.com/RaphaelJ/friday/blob/master/src/Vision/Image/Type.hs#L52
instanceStorable p=>MaskedImage (Manifest p)where
type ImagePixel(Manifest p) =p
Is there any other way to use the type operator other than the type synonym?
haskell
The type
and data
that appear during class
and instance
declarations are GHC extensions called Type Families that provide a slightly different function than the type
that appears at the top.
To enable the type family, specify TypeFamilies
as the pragma (declared at the beginning of the linked source code):
Cf.https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/type-family.html
The class
and instance
declarations correspond to the usual class.
Normally, the class
declaration will give you a method, its type, and default implementation.If you use the type family, you will be able to declare the "type-level version"That is the declaration of the type function using type
.
In other words, you can define a type function, kind, and default type.
Here is a trial example to verify:
class Hoge i where
type Fugai::* -- Species (kind) specified
type Fugai=() -- Default type
using piyo::Fugai->Fugai -- `Fugai`
piyo=id -- Default Implementation
instance Hoge Inthere
type Fuga Int=Maybe Int
piyon=fmap succmn
instance Hoge Double where
type Fuga Double = [Double ]
piyols=ls++ls
piyo
is the method and Fuga
is the type function.You'll find that it can be declared in a similar way.You can also verify that Fugai
is used as the type.
Let's look at the class
declaration first:
class Storable (ImagePixeli) =>MaskedImage i where
type ImagePixeli
This declaration shows that
MaskedImage
i
is a type variable representing an instance of MaskedImage
ImagePixeli
must be an instance of Storable
ImagePixeli
is a synonym defined in the instance declarationThat's about it.
Now look at the instance
declaration:
instanceStorable p=>MaskedImage (Manifest p)where
type ImagePixel(Manifest p) =p
This instance
declaration shows that
Manifestp
is an instance of MaskedImage
(the instance also happens to contain the type variable p
)ImagePixel(Manifestp)
is a synonym of type p
p
must be an instance of Storable
This instance
declaration is valid if the type check passes.
However, Storable p
may need a little more explanation.
The following is a brief confirmation that this instance
definition is valid.
To make Manifestp
an instance of MaskedImage
,
instance
i
is mechanically replaced with Manifestp
provides:
instanceStorable(ImagePixel(Manifestp))=>MaskedImage(Manifestp)where
Type ImagePixel (Manifest p)
The developer defined ImagePixel(Manifest p)
as a synonym of type p
:
instanceStorable(ImagePixel(Manifestp))=>MaskedImage(Manifestp)where
type ImagePixel(Manifest p) =p
As defined by this type of synonym, ImagePixel(Manifest p)
is replaced by p
, so you get:
instanceStorable p=>MaskedImage (Manifest p)where
type ImagePixel(Manifest p) =p
611 GDB gets version error when attempting to debug with the Presense SDK (IDE)
578 Understanding How to Configure Google API Key
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
581 PHP ssh2_scp_send fails to send files as intended
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.