On Thu, Dec 6, 2012 at 9:24 AM, Xiao Jia <me@xiao-jia.com> wrote:
Hi, I'm trying to model a round robin scheduler in Haskell.

> class Schedulable s where
>   isFinal :: s -> Bool
>
> class Scheduler s where
>   add :: (Schedulable a) => a -> s -> s
>   next :: (Schedulable a) => s -> (a, s)
>   empty :: s -> Bool
>
> data Schedulable a => RoundRobin = RoundRobin [a] [a]
>
> instance Scheduler RoundRobin where
>   add p (RoundRobin ps qs) = RoundRobin (ps ++ [p]) qs
>
>   next (RoundRobin []     qs) = next (RoundRobin qs [])
>   next (RoundRobin (p:ps) qs) = (p, RoundRobin ps (qs ++ [p]))
>
>   empty (RoundRobin [] _) = True
>   empty _                 = False

However, GHC complains that

> main.hs:9:6:
>     Illegal datatype context (use -XDatatypeContexts): Schedulable a =>

After some searches I realized that datatype contexts are deprecated.
So how can I model the scheduler without using datatype contexts?

Thanks.

--
Regards,
Xiao Jia

 
Hello Xiao,

From what I've read from this page ( http://hackage.haskell.org/trac/haskell-prime/wiki/NoDatatypeContexts ), you simply need to remove « Schedulable a » from 9th line.
It would there fore read like so :


data Schedulable a => RoundRobin = RoundRobin [a] [a]

Putting « Schedulable a) => » in your « add » and « next » functions should be sufficient.

Hope this helps,

Jon

(Sorry for the double post, but I think I did something wrong on the first one...)