How to make instance of MonoFunctor

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Baa
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to make instance of MonoFunctor

Baa
Hello, Dear List!

I want to make functor of my type like

  newtype UserName = UserName { unName :: Text }

sure, it's impossible, so I will make MonoFunctor instead of (from
library mono-traversable). So, I try:

  instance MonoFunctor UserName where
    omap fn (UserName n) = UserName $ fn $ n

but I get error

        • Couldn't match expected type ‘Element UserName’
                      with actual type ‘Text’
        • In the second argument of ‘($)’, namely ‘unName an’
          In the second argument of ‘($)’, namely ‘fn $ unName an’
          In the expression: UserName $ fn $ unName an

interesting is that Text "type Element Text" of "type family Element
mono" (instance?). So, how to make mono-functor for such `UserName`
structure?


--
Best regards, Paul
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to make instance of MonoFunctor

Michael Snoyman
What do you want the type of `omap` to be for `UserName`? There are two reasonable definitions:

omap :: (Text -> Text) -> (UserName -> UserName)
omap :: (Char -> Char) -> (UserName -> UserName)

The first one is saying that a UserName is a container of a single Text value. The second is that a UserName is a container of a sequence of Char values. Once you figure out what the answer to this question is, you'll need to use an associated type called Element to specify what you intend, e.g.:

type Element UserName = Char

On Thu, Aug 10, 2017 at 11:35 AM, Baa <[hidden email]> wrote:
Hello, Dear List!

I want to make functor of my type like

  newtype UserName = UserName { unName :: Text }

sure, it's impossible, so I will make MonoFunctor instead of (from
library mono-traversable). So, I try:

  instance MonoFunctor UserName where
    omap fn (UserName n) = UserName $ fn $ n

but I get error

        • Couldn't match expected type ‘Element UserName’
                      with actual type ‘Text’
        • In the second argument of ‘($)’, namely ‘unName an’
          In the second argument of ‘($)’, namely ‘fn $ unName an’
          In the expression: UserName $ fn $ unName an

interesting is that Text "type Element Text" of "type family Element
mono" (instance?). So, how to make mono-functor for such `UserName`
structure?


--
Best regards, Paul
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Baa
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to make instance of MonoFunctor

Baa
Aha, so I missed:

  type instance Element UserName = Text

and now this is right:

  instance MonoFunctor UserName where
    omap fn n = UserName $ fn $ unName n

Yes, this will be omap :: (Text -> Text) -> (UserName -> UserName).

Many thanks!!


> What do you want the type of `omap` to be for `UserName`? There are
> two reasonable definitions:
>
> omap :: (Text -> Text) -> (UserName -> UserName)
> omap :: (Char -> Char) -> (UserName -> UserName)
>
> The first one is saying that a UserName is a container of a single
> Text value. The second is that a UserName is a container of a
> sequence of Char values. Once you figure out what the answer to this
> question is, you'll need to use an associated type called Element to
> specify what you intend, e.g.:
>
> type Element UserName = Char
>
> On Thu, Aug 10, 2017 at 11:35 AM, Baa <[hidden email]> wrote:
>
> > Hello, Dear List!
> >
> > I want to make functor of my type like
> >
> >   newtype UserName = UserName { unName :: Text }
> >
> > sure, it's impossible, so I will make MonoFunctor instead of (from
> > library mono-traversable). So, I try:
> >
> >   instance MonoFunctor UserName where
> >     omap fn (UserName n) = UserName $ fn $ n
> >
> > but I get error
> >
> >         • Couldn't match expected type ‘Element UserName’
> >                       with actual type ‘Text’
> >         • In the second argument of ‘($)’, namely ‘unName an’
> >           In the second argument of ‘($)’, namely ‘fn $ unName an’
> >           In the expression: UserName $ fn $ unName an
> >
> > interesting is that Text "type Element Text" of "type family Element
> > mono" (instance?). So, how to make mono-functor for such `UserName`
> > structure?
> >
> >
> > --
> > Best regards, Paul
> > _______________________________________________
> > Beginners mailing list
> > [hidden email]
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> >  

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Loading...