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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:13:40+00:00 2026-06-15T23:13:40+00:00

I have a bit of code that would be more cleanly written if I

  • 0

I have a bit of code that would be more cleanly written if I could treat Monads as Nums (where applicable, of course). Easily enough done:

{-# LANGUAGE FlexibleInstances #-}

import Control.Monad (liftM, liftM2)
import Data.Char (digitToInt)

instance (Monad m, Num a) => Num (m a) where
  (+) = liftM2 (+)
  (-) = liftM2 (-)
  (*) = liftM2 (*)
  abs = liftM abs
  signum = liftM signum
  fromInteger = return . fromInteger

square :: (Monad m, Num a) => m a -> m a
square x = x * x

-- Prints "Just 9", as expected
main = putStrLn $ show $ square $ Just 3

But when I add the following function to the file …

digitToNum :: (Num a) => Char -> a
digitToNum = fromIntegral . digitToInt

… I receive the following error:

monadNumTest.hs:15:14:
    Overlapping instances for Num (m a)
      arising from a use of `*'
    Matching instances:
      instance (Monad m, Num a) => Num (m a)
        -- Defined at monadNumTest.hs:6:10
      instance Integral a => Num (GHC.Real.Ratio a)
        -- Defined in `GHC.Real'
    (The choice depends on the instantiation of `m, a'
     To pick the first instance above, use -XIncoherentInstances
     when compiling the other instance declarations)
    In the expression: x * x
    In an equation for `square': square x = x * x

This doesn’t make sense to me, because (1) digitToNum is never called and (2) Ratio isn’t a Monad. So I’m unsure how or why that’s a problem. Any tips around this would be appreciated.

This is GHC 7.4.2, using the Haskell Platform 2012.4.0.0.

  • 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-15T23:13:42+00:00Added an answer on June 15, 2026 at 11:13 pm

    The key issue at play here is a principle in haskell that writing additional instances should not change the operation of existing code. This increases the robustness of haskell code, as code won’t break or have different behaviour if a module it depends on adds a new instance.

    For this reason, when choosing possible instances to use for a type, Haskell does not consider the context of the instances. For example, when matching checking if a type will match the instance instance (Monad m, Num a) => Num (m a) for the class Num, it will only check if it can match m a. This is because any type may later be made an instance of a class, and if instance choice used context information adding that instance would change the operation of existing programs.

    For example, there is nothing stopping the following code being added either in your module, or in a module you depend on:

    instance Monad Ratio where
       return = undefined
       (>>=) = undefined
    

    Sure, such an instance is useless, but haskell has no way of judging that. It also is possible that there is a useful definition of Monad for Ratio (I haven’t looked into that).

    In summary, what you are trying to do isn’t a good idea. You can stop these limitations using OverlappingInstances and IncoherentInstances, however for the reasons described above these flags are not recommended for mainstream use by most haskell programmers.

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

Sidebar

Related Questions

i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of code that fits theoretical prediction to experimental data, and
I have a bit of sample code that is throwing this warning: main.c: In
I have a bit of Javascript code that creates a save friendly version of
I have the following bit of code that worked as expected before we upgraded
I have the following bit of code that is timing out when I pass
I have a very simple bit of code that is supposed to capture the
How do I change the following bit of code so that I only have
I have some legacy C++ code that I am trying to understand a bit
I have the following bit of code, I expect that given cstdio is included

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.