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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:19:07+00:00 2026-05-28T06:19:07+00:00

This is more or less a simplified version of the issue I’m trying to

  • 0

This is more or less a simplified version of the issue I’m trying to understand in my own code. I’m working with functions that are polymorphic in the Failure class from this package.

{-# LANGUAGE FlexibleContexts #-}
import Data.Maybe
import Data.Either
import Control.Failure

data FookError = FookError deriving Show

fook :: (Failure FookError m)=> Int -> m Int
fook = undefined

fooks :: (Failure FookError m)=> Int -> m Int
-- DOES NOT TYPE-CHECK:
--fooks n = return $ head $ rights $ map fook [1..]
-- OKAY:
fooks n   = return $ head $ catMaybes $ map fook [1..]

You can see in the above code, that when I treat the return type of fook as Maybe Int the module compiles fine, however treating it as Either Fook Int fails.

What is going on here?

  • 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-28T06:19:08+00:00Added an answer on May 28, 2026 at 6:19 am

    This is because, in the non-working definition of fooks, fook‘s type is ambiguous. When you use catMaybes, it disambiguates as Maybe Int, but when you use rights, it can be Either e Int for any e, and the compiler doesn’t necessarily know which. Sure, by default the only Failure instance for Either is instance Failure e (Either e), but there’s nothing stopping you defining, e.g. instance Failure String (Either Int).

    If you explicitly specify the type by defining fooks as

    fooks n =
      return $ head $ rights $ map (fook :: Int -> Either FookError Int) [1..]
    

    then it works fine.

    However, I suspect that you’re not doing what you really want here; fooks never actually uses the failure capability of the underlying monad; indeed, even if there are no non-failing results, the monadic action still succeeds and returns a value. The value just so happens to be an error, but that’s still probably not what you want 🙂

    If you want fooks to try a bunch of individual fooks in turn, and return the first one that succeeds, then something like:

    fooks :: (Failure FookError m, MonadPlus m) => Int -> m Int
    fooks n = foldr mplus (failure FookError) $ map fook [1..]
    

    should do the trick. The plain Failure class by itself offers no way to recover from errors, so you need to require MonadPlus too. Note that the failure FookError here will never actually be used, since [1..] is infinite, but presumably you’re planning to change the definition; say to one that actually uses n 🙂

    Unfortunately, that isn’t all! Either e doesn’t have a MonadPlus instance, presumably because there’s no reasonable value of mzero (although another potential problem is that mplus (Left e) (Left e') could be either Left e or Left e').

    Thankfully, it’s easy to define an instance for our specific type:

    instance MonadPlus (Either FookError) where
      mzero = failure FookError
      mplus a@(Right _) _ = a
      mplus (Left _) a = a
    

    You’ll need {-# LANGUAGE FlexibleInstances #-} at the top of your file to do this.

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

Sidebar

Related Questions

This is more or less a best-practice question: I'm working on a site that
I use C# code more-or-less like this to serialize an object to XML: XmlSerializer
This is more or less an academia question to help me better understand this
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
This is more of a syntax question I'm trying to write a store procedure
This more or less complex phrase to translate in my Django template is: This
Here's a simplified version of what I'm trying to do : Before any other
I have this old jquery script for progressive disclosure: (notice the $(this).text('more...') code changes
I want to know this more in detail. Is it a realtime scheduler? An
I am wondering if there is a way to handle this more elegantly. After

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.