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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:15:08+00:00 2026-06-08T14:15:08+00:00

The monad instanc of Data.Stream is defined that way: instance Monad Stream where return

  • 0

The monad instanc of Data.Stream is defined that way:

instance Monad Stream where
  return = repeat
  xs >>= f = join (fmap f xs)
    where
      join :: Stream (Stream a) -> Stream a
      join ~(Cons xs xss) = Cons (head xs) (join (map tail xss))

That means join takes the first element of the first stream, the second element of the second stream etc, so the resulting stream can be seen as “main diagonal”, discarding all other elements.

Now there is a way to go through an infinite two-dimensional table, discovered by Georg Cantor for his proof that there are as many rational numbers as natural numbers: http://www.jcu.edu/math/vignettes/infinity.htm

Now my question is if a join using a path along all secondary diagonals (visiting every element of every stream) would be a valid implementation as well. Or would this violate one of the monad laws?

  • 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-08T14:15:11+00:00Added an answer on June 8, 2026 at 2:15 pm

    It would violate

    return x >>= f === f x
    

    Consider

    f k = Cons k (f (k+1))
    

    Now fmap f (return 1) is repeat (f 1) and if join went through all elements, in the resulting Stream, elements would repeat.

    As a two-dimensional table, fmap f (return 1) looks like

    1 2 3 4 ...
    1 2 3 4 ...
    1 2 3 4 ...
    

    and if you traverse that following the secondary diagonals, you get

    1 1 2 1 2 3 1 2 3 4 ...
    

    and not 1 2 3 4 5 ... as with f 1.

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

Sidebar

Related Questions

instance Monad (Either a) where return = Left fail = Right Left x >>=
In Haskell, is there a way to restrict a monad M a so that
I am defining an instance of a monad as follows: data Something = Something
I want to make my monad transformer to be an instance of MonadError if
I've noticed that the word monad seems to be used in a somewhat inconsistent
In chapter 15 of Real World Haskell, a type class is defined: class (Monad
Say I have two type classes defined as follows that are identical in function
How does one fold over a monad strictly? Data.Foldable has the strict foldl' and
I want to create my own monad. This is what i wrote: data LeafConType
Looking at the docs for Control.Applicative , I notice that they have instance declarations

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.