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

Brent Yorgey byorgey at seas.upenn.edu
Thu Dec 6 16:07:11 CET 2012


On Thu, Dec 06, 2012 at 09:47:31AM -0500, Jonathan Rioux wrote:
> On Thu, Dec 6, 2012 at 9:24 AM, Xiao Jia <me at 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]

I think you meant

  data RoundRobin a = RoundRobin [a] [a]

-Brent



More information about the Beginners mailing list