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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:49:17+00:00 2026-05-27T10:49:17+00:00

The Maybe/Either monad slows things down significantly. Does the use of some continuation monad

  • 0

The Maybe/Either monad slows things down significantly. Does the use of some continuation monad for handling errors speeds things up? Is there such a thing as a “builtin continuation monad” or a “buitin error monad”? By builtin I mean something like ST.

Benchmark:

import Criterion.Main                                          

unsafeDiv x 0 = error "division by zero"                       
unsafeDiv x y = x `div` y                                      

safeDiv x 0 = Nothing                                          
safeDiv x y = Just (x `div` y)                                 

test1 :: Int -> [Int]                                          
test1 n = map (n `unsafeDiv`) [n,n-1..1]                       

test2 :: Int -> Maybe [Int]                                    
test2 n = mapM (n `safeDiv`) [n-1,n-2..0]                      

test3 :: Int -> Maybe [Int]                                    
test3 n = test3' Just [n-1,n-2..0]                             
  where test3' k []     = k []                                 
        test3' k (0:ns) = Nothing                              
        test3' k (n:ns) = test3' (k . (n:)) ns                 

main = defaultMain                                             
  [ bench "test1" (nf test1 100000)                            
  , bench "test2" (nf test2 100000)                            
  , bench "test3" (nf test3 100000)                            
  ] 
  • 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-27T10:49:17+00:00Added an answer on May 27, 2026 at 10:49 am

    I’ve had some success with a rather horrible hand written monad that uses something like

    newtype M r a = M { runM :: r -> (# Bool, a #) }
    

    where I treat the Bool like the Maybe constructor and in the Nothing case put an error in for ‘a’. I usually use this when I have more structure (an environment e, state, logs, etc.), so I’m not sure how well it would pay off when it is this simple but the monad looks something like:

    instance Monad (M r) where
      return a = M (\_ -> (# True, a #))
      M f >>= k = M (\r -> case f r of
        (# True, a #) -> runM (k a) r
        (# False, _ #) -> (# False, undefined #))
      fail _ = M (\_ -> (# False, undefined #))
    

    This has the benefit that we don’t construct any thing on the heap, just the stack.

    However, you need to be careful to be strict in all the right places. It is easy to accidentally build a thunk in your state which can kill your performance.

    If you are feeling daring you can smuggle an unsafeCoerced error through in the ‘a’ slot on failure as well and extract it at the end, or you can just translate the Bool into a Maybe e but you need to be careful, because you don’t want to build up a tower of unsafeCoerces defeating all the work you went through to get this far.

    The effectiveness of this approach depends on how much the overhead of building and tearing down Maybe frames is vs. the mental and execution time costs of distributing the code for dealing about failure across a lot of different places in the code. Note how >>= has to effectively unwind the Failure case manually.

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

Sidebar

Related Questions

Maybe there is no difference, but is either way better than the other (or
Somewhere in some of my projects or maybe in eclipse, there are some jar
Maybe I am just over thinking this and need to write some more prototype
maybe it's dumb but is there a difference between new Something().method(); and Something tmpSomething
Maybe its a very dumb question but I hope you can give me some
I'm a newbie using jquery and js either, so maybe thats the reason why
Maybe, it is a stupid question but I can't use python eggs with IronPython.
One of the things mentioned recently maybe in the SO podcast or Joel was
Maybe there is something better than NSURLConnection for this? I want to: Fetch XML
I have some buttons which are fixed either side of my page, they work

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.