Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:39:53+00:00 2026-06-15T22:39:53+00:00

I’m modelling a round robin scheduler in Haskell. class Schedulable s where isFinal ::

  • 0

I’m modelling 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 =>

How can I solve this issue?

I also saw a proposal about removing datatype contexts, so how can I model the scheduler without using datatype contexts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:39:54+00:00Added an answer on June 15, 2026 at 10:39 pm

    Your Scheduler class

    class Scheduler s where
      add :: (Schedulable a) => a -> s -> s
      next :: (Schedulable a) => s -> (a, s)
      empty :: s -> Bool
    

    is not going to work.

    add promises that values of any Schedulable type can be added to the scheduler. That is possible, but it requires an extension, ExistentialQuantification or GADTs would allow to define a type wrapping any Schedulable value.

    next, however, promises to deliver a value of any Schedulable type, whatever the caller desires, and that’s not going to work. If I only ever add Int values to the scheduler, and then ask for a String, how would it construct one out of thin air?

    One way to get your code to work is

    {-# LANGUAGE GADTs #-}
    module Schedules where
    
    class Schedulable s where
      isFinal :: s -> Bool
    
    class Scheduler s where
      add :: (Schedulable a) => a -> s -> s
      next :: s -> (Schedule, s) -- returns a Schedulable item of unknown type, wrapped in a Schedule
      empty :: s -> Bool
    
    -- Wrapper type for any Schedulable
    data Schedule where
        Schedule :: Schedulable a => a -> Schedule
    
    -- Equivalent alternative using existential quantification instead of GADT syntax
    -- data Schedule = forall a. Schedulable a => Schedule a
    
    -- make Schedules Schedulable, maybe not necessary
    instance Schedulable Schedule where
        isFinal (Schedule s) = isFinal s
    
    -- RoundRobin queues schedulable items, wrapped as Schedules, since lists are homogeneous
    data RoundRobin = RoundRobin [Schedule] [Schedule]
    
    -- How RoundRobin works
    instance Scheduler RoundRobin where
      -- enqueue item after wrapping it
      add p (RoundRobin ps qs) = RoundRobin (ps ++ [Schedule p]) qs
    
      -- deliver next item to process
      -- the first equation suggests that (Maybe Schedule, s) would be the better return type
      next (RoundRobin []     []) = error "Nothing to schedule"
      next (RoundRobin []     qs) = next (RoundRobin qs [])
      next (RoundRobin (p:ps) qs) = (p, RoundRobin ps (qs ++ [p]))
    
      empty (RoundRobin [] _) = True
      empty _                 = False
    

    Using GADT syntax or existential quantification makes the constraints imposed on the constructor available via pattern matching, in contrast to the old DatatypeContexts that despite the constraints on the type, required the context put on the functions using the type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I want to construct a data frame in an Rcpp function, but when I
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.