Functor <constant>
"Philip K.F. Hölzenspies"
p.k.f.holzenspies at utwente.nl
Wed Sep 9 13:15:06 EDT 2009
Dear Serge,
If you want the type checker to know what type is "inside" your
BNatural, no there isn't.
You use reverse in your example, which has type [a] -> [a], so the
type of what is in your BNatural doesn't change when you fmap reverse
onto it. In general, however, you can't guarantee this. What if you
fmap length?
fmap length (BNat [Bit0,Bit0]) -->> BNat 2
Obviously, this violates the declaration of BNat. In this case
specific case, you could maybe reason that the type checker should
just reduce that only [DBit] -> [DBit] functions are allowed. However,
the Functor class is set up to be more general; it allows a -> b
functions. When your data type can not be instantiated as Functor this
way, then it "isn't a functor" (at least, in the Haskell
interpretation of functor).
There is nothing to stop you from defining your own alternative map:
bmap :: ([DBit] -> [DBit]) -> BNatural -> BNatural
bmap f (BNat bs) = BNat $ f bs
I hope this makes some sense.
Regards,
Philip
On Sep 9, 2009, at 6:45 PM, Serge D. Mechveliani wrote:
> People,
>
> I have data DBit = Bit0 | Bit1 deriving (Eq, Ord, Enum)
> data BNatural = BNat [DBit] deriving (Eq)
>
> and want to apply things like fmap reverse (bn :: BNatural).
>
> GHC reports an error on this usage of fmap.
> It also does not allow
> instance Functor BNatural where fmap f (BNat ds) = BNat $ f
> ds,
>
> -- it reports that a type constructor for Functor must have kind * -
> > *.
> Probaly, GHC agrees with Haskell at this point (?).
>
> Now, I deceive the compiler by declaring instead
>
> newtype BNatAux a = BNat a deriving (Eq)
> type BNatural = BNatAux [DBit]
> instance Functor BNatAux where fmap f (BNat ds) = BNat $ f ds
>
> So, a parasitic BNatAux is introduced.
> I wonder: is there a simpler way to have fmap for BNatural ?
> Thank you in advance for advice,
>
> -----------------
> Serge Mechveliani
> mechvel at botik.ru
>
>
>
>
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
More information about the Glasgow-haskell-users
mailing list