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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:01:52+00:00 2026-05-12T23:01:52+00:00

I want to create my own monad. This is what i wrote: data LeafConType

  • 0

I want to create my own monad. This is what i wrote:

data LeafConType a = LeafCon (a,Int,Int)

instance Monad (LeafConType ) where
return = LeafCon 
lc@(LeafCon (t,i,n)) >>= f = if i>=n
                                then lc
                                else f (t,i,n)

But this dont work. Ghc says:

leafcon.hs:26:1:
    Occurs check: cannot construct the infinite type: a = (a, Int, Int)
    When generalising the type(s) for `return'
    In the instance declaration for `Monad LeafConType'

leafcon.hs:27:1:
    Occurs check: cannot construct the infinite type: a = (a, Int, Int)
    When generalising the type(s) for `>>='
    In the instance declaration for `Monad LeafConType'

Whats wrong with that?


I want to do calculations while i is lower than n. n should be constants by I don’t know yet how to do this correct. It should be some mix of State and Maybe. If you have some advices feel free to share it with me:P

  • 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-12T23:01:53+00:00Added an answer on May 12, 2026 at 11:01 pm

    About return:

    Prelude> :t return
    return :: (Monad m) => a -> m a 
    

    So return takes an argument of type a, and returns something of type m a. In this case m is LeafConType, so LeafConType a is returned.

    Now suppose that we pass True. Then a = Bool, so the return type must be LeafConType Bool. However, you define:

    return = LeafCon
    

    So, return True becomes LeafCon True. But that is not allowed, because the type definition of LeafConType states that

    data LeafConType a = LeafCon (a, Int, Int)
    

    So for LeafConType Bool the argument to LeafCon must have type (Bool, Int, Int), not just Bool. And that is what the compile error means: a cannot be the same as (a, Int, Int). You state:

    I want to do calculations while i is lower than n.

    This means that you will need some default values for i and n, for otherwise it will be impossible to define return. If both of them are zero by default, then you could define:

    return a = LeafCon (a, 0, 0)
    

    About (>>=):

    Prelude> :t (>>=)
    (>>=) :: (Monad m) => m a -> (a -> m b) -> m b
    

    Now look at your implementation (slightly different notation, same idea):

    lc@(LeafCon (t, i, n)) >>= f | i >= n    = lc 
                                 | otherwise = f t
    

    What we see here, is that lc is returned when i >= n. But lc is of type LeafConType a, while f is a function which may return a value of type LeafConType b, for any b. As a result it could be that b is not equal to a and hence these types don’t match. In conclusion, you seriously have to ask yourself one question:

      Can this type of computation be expressed as a monad anyway?

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

Sidebar

Related Questions

I want to create my own RSS/XML feed. I fetch data from the database
I want to create my own routing system with .htaccess , but I have
I want to create my own time stamp data structure in C. DAY (
I want to create my own tag, but it doesn't work Component: package com;
I want to create my own WebSocket server, but it will only include a
I wrote a DB class and I want to create my own model class
I want to create my own DSL but working with DSL Definition is confusing.
I want to create own filetype to save objects in my app. Basically, I
I want to create my own IM and I'm searching an open-source IM APIs.
I want to create my own event - OnPositionChange . Event implementation of course

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.