[Haskell-beginners] Illegal datatype context when modelling round robin scheduler

Xiao Jia me at xiao-jia.com
Thu Dec 6 15:24:35 CET 2012


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



More information about the Beginners mailing list