Is there any other way to use the Haskell type operator other than the type synonym?

Asked 2 years ago, Updated 2 years ago, 93 views

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

2022-09-30 19:56

1 Answers

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

  • Defining class 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 declaration
  • kind is omitted
  • No default type defined

That'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,

  • Rewrite class to 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


2022-09-30 19:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.