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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:28:33+00:00 2026-06-13T03:28:33+00:00

I am having trouble defining the return over a custom defined recursive data type.

  • 0

I am having trouble defining the return over a custom defined recursive data type.

The data type is as follows:

data A a = B a | C (A a) (A a)

However, I don’t know how to define the return statement since I can’t figure out when to return B value and when to recursively return C.

Any help is appreciated!

  • 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-13T03:28:34+00:00Added an answer on June 13, 2026 at 3:28 am

    One way to define a Monad instance for this type is to treat it as a free monad. In effect, this takes A a to be a little syntax with one binary operator C, and variables represented by values of type a embedded by the B constructor. That makes return the B constructor, embedding variables, and >>= the operator which performs subsitution.

    instance Monad A where
      return = B
      B x   >>= f = f x
      C l r >>= f = C (l >>= f) (r >>= f)
    

    It’s not hard to see that (>>= B) performs the identity substitution, and that composition of substitutions is associative.

    Another, more “imperative” way to see this monad is that it captures the idea of computations that can flip coins (or read a bitstream or otherwise have some access to a sequence of binary choices).

    data Coin = Heads | Tails
    

    Any computation which can flip coins must either stop flipping and be a value (with B), or flip a coin and carry on (with C) in one way if the coin comes up Heads and another if Tails. The monadic operation which flips a coin and tells you what came up is

    coin :: A Coin
    coin = C (B Heads) (B Tails)
    

    The >>= of A can now be seen as sequencing coin-flipping computations, allowing the choice of a subsequent computation to depend on the value delivered by an earlier computation.

    If you have an infinite stream of coins, then (apart from your extraordinary good fortune) you’re also lucky enough to be able to run any A-computation to its value, as follows

    data Stream x = x :> Stream x   -- actually, I mean "codata"
    
    flipping :: Stream Coin -> A v -> v
    flipping _             (B v)    = v
    flipping (Heads :> cs) (C h t)  = flipping cs h
    flipping (Tails :> cs) (C h t)  = flipping cs t
    

    The general pattern in this sort of monad is to have one constructor for returning a value (B here) and a bunch of others which represent the choice of possible operations and the different ways computations can continue given the result of an operation. Here C has no non-recursive parameters and two subtrees, so I could tell that there must be just one operation and that it must have just two possible outcomes, hence flipping a coin.

    So, it’s substitution for a syntax with variables and one binary operator, or it’s a way of sequencing computations that flip coins. Which view is better? Well… they’re two sides of the same coin.

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

Sidebar

Related Questions

I'm having trouble defining a custom property in a QML item: Item { property
I am having trouble with defining a CanCan ability over a HABTM relationship such
I am having trouble defining the content-type of an element (phonenumber) whilst at the
I'm having trouble defining a foreign key relationship in entity framework. I've boiled the
I am having trouble defining and automating my build process despite simple requirements: Every
I'm having a little trouble defining my question. Here's what I have. A mailto
Having trouble determining if a file name conforms to a specific convention as follows.
I'm having trouble defining the CSS styles necessary in order to achieve the following
I have successfully created a task, although I am having trouble defining when it
I'm having trouble with this regular expression: Construct a regular expression defining the following

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.