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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:27:39+00:00 2026-05-14T21:27:39+00:00

I have the following haskell code: fac n = product [1..n] taylor3s w0 f

  • 0

I have the following haskell code:

fac n = product [1..n]

taylor3s w0 f f' f'' t h = w1 : taylor3s w1 f f' f'' (t+h) h
  where hp i = h^i / fac i
        w1 = w0 + (hp 1) * f t w0 + (hp 2) * f' t w0 + (hp 3) * f'' t w0

taylor_results = take 4 $ taylor3s 1 f f' f'' 1 0.25
  where f   t x = t^4 - 4*x/t
        f'  t x = 4*t^3 - 4*(f t x)/t + 4*x/t^2
        f'' t x = 12*t^2 - 4*(f' t x)/t + 8*(f t x)/t^2 - 8*x/t^3

taylor_results is supposed to be a use case of taylor3s. However, there is something wrong with the number type inferencing. When I try to compile, this is the error I get:

practice.hs:93:26:
    Ambiguous type variable `a' in the constraints:
      `Integral a'
        arising from a use of `taylor3s' at practice.hs:93:26-51
      `Fractional a' arising from a use of `f' at practice.hs:93:37
    Possible cause: the monomorphism restriction applied to the following:
      taylor_results :: [a] (bound at practice.hs:93:0)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction

Can someone help me with understanding what the problem is?

  • 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-14T21:27:40+00:00Added an answer on May 14, 2026 at 9:27 pm

    Since you’re mixing operations that are only available on integrals and operations that are only available on fractionals (specifically you use ^ of which the second operand must be integral – use ** if you intend for both operands to have the same Floating type), haskell infers that all arguments and the result of taylor3s have the type Fractional a, Integral a => a. This is not a type error, since theoretically such a type could exist, but it’s most likely not what you want because in practice such a type does not exist.

    The reason that you get a type error anyway is that the inferred type of taylor_results is consequently also Fractional a, Integral a => a which is polymorphic and thus violates the monomorphism restriction.

    If you would explicitly declare taylor_results as taylor_results :: Fractional a, Integral a => a or disable the monomorphism restriction, the whole thing would compile, but be impossible to use (without defining a type that actually instantiates Integral and Fractional, which would be nonsense).

    Note that if you fix this (for example by replacing ^ with **) the type of taylor_results will still be polymorphic (it will be inferred as taylor_results :: (Floating a, Enum a) => [a], which is actually sensible), so you will still run into the monomorphism restriction. So you still need to either turn the restriction off, explicitly declare the type of taylor_results to be polymorphic or explicitly declare the type of taylor_results to be a specific type that instantiates Floating and Enum (e.g. Double). Note that unless you do the latter, taylor_results will be recalculated each time you use it (which is why the monomorphism restriction exists).

    Note that if you fix this (for example by replacing ^ with **) the most general type of taylor_results will be (Floating a, Enum a) => [a], however the type you get (unless you disable the monomorphism restriction) will be [Double]. If you don’t want doubles, you either have to explicitly declare taylor_results to be of another type (which instantiates Floating and Enum) or to be polymorphic. Note that if you declare it to be polymorphic, taylor_results will be recalculated each time you use it (which is why the monomorphism restriction exists).

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

Sidebar

Related Questions

I have the following Haskell code: -- Problem 69 import ProjectEuler phi :: Integer
I have the following haskell code to expand environment variables in strings (parsing code
I have the following Template Haskell code in my module, which is part of
I have written a script in haskell named testscript with the following code: #!/usr/bin/env
I have the following Haskell code import Data.Int import System.Environment type Coord = (Int16,
I have a problem with a Haskell code, I have the following: takeMeHere cs
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
I have some troubles with Haskell's type system. Situation: Following program is taking list
So, the idea is that I would take the following code used to run

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.