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 9060001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:06:46+00:00 2026-06-16T15:06:46+00:00

I have an infinite structure like the following (using the Stream type from the

  • 0

I have an infinite structure like the following (using the Stream type from the streams package):

data U x = U (Stream x) x (Stream x) deriving (Functor,Foldable)             

I want to provide an instance of Traversable for it, like this:

instance Traversable U where
    traverse f (U lstream focus rstream) = 
        let pairs =  liftA unzip 
                   . sequenceA . fmap (traversepair f) 
                   $ zip lstream rstream 
            traversepair f (a,b) = (,) <$> f a <*> f b
            rebuild c (u,v) = U u c v
        in rebuild <$> f focus <*> pairs

The documentation for Data.Traversable says that they represent the “class of data structures that can be traversed from left to right”. But my definition does not traverse from left to right, it traverses outwardly. I had to define it that way to be able to lazily extract values on both sides after a sequence operation involving the Rand monad.

Is it a valid definition nonetheless? I have noticed that the Typeclassopedia entry for Traversable doesn’t say anything about “left-to-right”, it only talks about “commuting two functors”.

  • 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-16T15:06:47+00:00Added an answer on June 16, 2026 at 3:06 pm

    According to this thread the laws should be:

    1. traverse Identity == Identity
    2. traverse (Compose . fmap g . f) == Compose . fmap (traverse g) . traverse f

    These 2 laws ensure that each element in the traversed structure is traversed exactly once. It does not matter in which order. You could even say that a Traversable instance defines what left-to-right means for that particular datatype.

    There are two other requirements in the documentation:

    • In the Functor instance, fmap should be equivalent to traversal with
      the identity applicative functor (fmapDefault).
    • In the Foldable instance, foldMap should be equivalent to traversal with a constant applicative functor (foldMapDefault).

    In your above code, you break the second requirement, because you’re deriving Foldable, which will visit the elements in a different order than your Traversable instance. Creating your own instance for Foldable with foldMapDefault fixes that.

    By the way, sequenceA . fmap (traversepair f) is traverse (traversepair f).

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

Sidebar

Related Questions

I am using EclipseLink MOXy and have a data structure that has child elements
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have a subclass of UIScrollView that I'm using for images slideshow, with infinite
While implementing a FIFO I have used the following structure: struct Node { T
I have XSD Schema with the following structure: ElementA has only one ElementB and
I have a folder structure like this /localhost/parent/ I have an .htaccess residing in
I have a single-table with the following (relative) structure: foo_id, parent_foo_id, foo_name I would
I have a table with the following structure uid sid eid Key value 1
I have to following XML document structure: <option_set id=1> <option>Yes</option> <option>No</option> <option>Maybe</option> </option_set> <question
I have a filesystem like XML structure, and now I want to get the

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.