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

  • SEARCH
  • Home
  • 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 7409027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:00:44+00:00 2026-05-29T06:00:44+00:00

I’m doing a fun project in which I’m trying to redo some basic data

  • 0

I’m doing a fun project in which I’m trying to redo some basic data types and concepts from Java. Currently I’m tackling Iterators.

My approach is the following:
(1) Translate Interfaces to Typeclasses
(2) Declare custom data types and instances for actual implementations

So I created the following type classes:

class Iterator it where
    next :: it e -> (it e, e)
    hasNext :: it e -> Bool

class Iterable i where
    iterator :: Iterator it => i e -> it e

class Iterable c => Collection c where
    add :: c e -> e -> c e

Yes, I’m trying to translate the concept of Iterators (which in this case is simply a box around the actual list).

Here is my implementation of a simple List:

data LinkedList e = Element e (LinkedList e) | Nil
    deriving (Show, Eq)

instance Collection LinkedList where
    add Nil e = Element e Nil
    add (Element x xs) e = Element x $ add xs e

I’ve excluded other functions like remove, contains, addAll for simplification.

Here is the Iterator:

data LinkedListIterator e = It (LinkedList e)

instance Iterator LinkedListIterator where
    hasNext (It Nil) = False
    hasNext (It _) = True
    next (It (Element x xs)) = (It xs, x)

Finally, an instance for Iterable LinkedList is missing. This is what I do:

instance Iterable LinkedList where
    iterator list = It list

The iterator function wraps the list into a LinkedListIterator and returns that. Here GHC claims an error:

Could not deduce (it ~ LinkedListIterator)
from the context (Iterator it)
  bound by the type signature for
             iterator :: Iterator it => LinkedList e -> it e

  `it' is a rigid type variable bound by
       the type signature for
         iterator :: Iterator it => LinkedList e -> it e

Expected type: it e
  Actual type: LinkedListIterator e

which I do not quite understand. There is an instance of Iterator for LinkedListIterator, so why is the expected Type “it e” not compatible with the actual type “LinkedListIterator e” (which, as far as I understand, is an Iterator e). What does the tilde (~) mean anyway? What is a rigid type variable?

EDIT: I changed the title from Translating Java Types into Haskell types: type deduction fail due to rigid type variable to Returning something from another type class B in function of type class A in Haskell since I believe that my actual problem is related to the return-something-of-type-class-B-from-type-class-A-issue in the iterator-function.

SOLUTION: Thanks to the answers I now changed my code to the version below. However, I had a fun time reading the Typeclassopedia and can only recommend it. As said, one should learn haskell idioms.

data Iterator c e = Next (Iterator c e, e) | Empty
    deriving (Show, Eq)

next :: Iterator c e -> (Iterator c e, e)
next (Next (i, e)) = (i, e)

hasNext :: Iterator c e -> Bool
hasNext Empty = False
hasNext _ = True

class Iterable i where
    iterator :: i e -> Iterator (i e) e

instance Iterable LinkedList where
    iterator Nil = Empty
    iterator (Element x xs) = Next (iterator xs, x)
  • 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-05-29T06:00:45+00:00Added an answer on May 29, 2026 at 6:00 am
    iterator :: Iterator it => i e -> it e
    

    This means the caller can choose it to be whatever they want, subject to it implementing Iterator. Another way of looking at it is that it’s a promise by iterator to work for all types it that implement Iterator.

    Your implementation provides a LinkedListIterator, no matter what the caller asks for.

    The compiler cannot prove that they are the same thing (because the caller could demand a different Iterator implementation), so issues an error.

    This is different from Java, where the caller chooses the classes of the inputs, and the callee chooses the class of the output. In Haskell, the caller chooses the types of the input and the output.

    ~ means type equality.


    Some broader points. (And I appreciate that you are trying to translate Java idioms into Haskell, but imo you need to learn Haskell idioms.)

    1. Sometimes you don’t want to return a value that implements a typeclass, you just want to return a value.

      If instead of Iterator being a typeclass, it was a datatype…

      data Iterator e = Iterator {next    :: (Iterator e, e),
                                  hasNext :: Bool}
      

      …then you could just return a value of type Iterator and not have to worry about different typeclass implementations.

      Laziness means that successive values from the iterator won’t be generated (and exceptions won’t be thrown) until they’re asked for. As long as you don’t old on to an old iterator value, these values can be garbage collected as you iterate along, so we still use constant space.

    2. A better definition of Iterator would be

      data Iterator e = Iterator {next :: Maybe (Iterator e, e)}
      

      This is better because it makes it harder for you to ask for the next value from the iterator without first checking to see that there is a next value.

    3. My second definition of Iterator looks a bit like your definition of LinkedList, and also like the definition of standard Haskell lists (which are themselves linked lists). And indeed it is idiomatic to use Haskell lists as an intermediate data structure where necessary for iteration.

    4. Read about the Foldable and Traversable typeclasses in the Typeclassopedia. In fact, read the Typeclassopedia, it’s a good introduction to some of the more scary-sounding typeclasses.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.