[Haskell-beginners] Long time no Haskell....simple typeclass question

Nicholls, Mark nicholls.mark at vimn.com
Wed Dec 31 13:26:03 UTC 2014


Actually I can fix it by following the Haskell advice and reading http://blog.omega-prime.co.uk/?p=127 and turning on some extensions...

Basically....
You DO follow Haskell's advice by amending the class declaration with a class content on the exe function, but you can let the instance declaration declare the constraint by....
turning the constraint into a type (ConstraintKinds)
You then use type families  (TypeFamilies) to calculate the constraint in the instance declaration.
 
> {-# LANGUAGE TypeFamilies, ConstraintKinds #-}

> import GHC.Prim

> data Func a b = Func (a->b) 

> exeFunc :: (Ord a) => Func a b -> a -> b
> exeFunc (Func g) = g

> class IFunc f where
>   type ExeConstraint f a b :: Constraint
>   exe :: (ExeConstraint f a b) => f a b -> a -> b 

> instance IFunc Func where
>   type ExeConstraint Func a b = Ord a
>   exe = exeFunc

As opposed to the instance declaration for the function type, which has no constraint 

> instance IFunc (->) where
>   type ExeConstraint (->) a b = ()
>   exe f = f


In some sense it still feels clumsy....with my F bounded OO type head on...which I'm told I must ignore....in something like C# this would be something like...

    interface IOrd {}

    interface IFunc<A,B>
    {
        B Exe(A a);
    }

    class Fun<A,B> : IFunc<A,B>
        where A : IOrd
    {
        readonly Func<A,B> f;

        public Fun(Func<A,B> f) { this.f = f; }

        public B Exe(A a)
        {
            return this.f(a);
        }
    }

i.e. here the IFunc declaration is closed...and blissfully unaware of any horrible constraints that need to be applied in order for the compiler to make the data type inhabit the type.

If I want to make my typeclass definitions as closed in Haskell, it would seem that I would always have to declare them with indexed types as constraints?
 
Or am I missing something? (I expect there is a reason, and I'm aware I'm comparing apple Haskell type(classe)s with orange C# types).

CONFIDENTIALITY NOTICE

This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited.

While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data.

Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us.

MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe.  MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc.  Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT.



More information about the Beginners mailing list