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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:23:32+00:00 2026-05-31T08:23:32+00:00

I am learning Haskell and I want to do TDD. I am trying to

  • 0

I am learning Haskell and I want to do TDD.
I am trying to test if a function raise an expected exception.
I am using HUnit and testpack.

testpack provides an assertRaises function but I don’t manage to compile my code 🙁

Here is my source code :

module Main where
import Test.HUnit
import Test.HUnit.Tools
import Control.Exception

foo n   | n > 2 = throw ( IndexOutOfBounds ( "Index out of bounds : " ++ ( show n ) ) )
foo n | otherwise = n

testException = TestCase( assertRaises "throw exception" ( IndexOutOfBounds "Index out of bounds : 4" ) ( foo 4 ) )

main = runTestTT ( TestList [ testException ] )

When I compile it with ghc I get the following error message :

test_exceptions.hs:10:107:
    No instance for (Ord (IO a0))
      arising from a use of `foo'
    Possible fix: add an instance declaration for (Ord (IO a0))
    In the third argument of `assertRaises', namely `(foo 4)'
    In the first argument of `TestCase', namely
      `(assertRaises
          "throw exception"
          (IndexOutOfBounds "Index out of bounds : 4")
          (foo 4))'
    In the expression:
      TestCase
        (assertRaises
           "throw exception"
           (IndexOutOfBounds "Index out of bounds : 4")
           (foo 4))

test_exceptions.hs:10:111:
    No instance for (Num (IO a0))
      arising from the literal `4'
    Possible fix: add an instance declaration for (Num (IO a0))
    In the first argument of `foo', namely `4'
    In the third argument of `assertRaises', namely `(foo 4)'
    In the first argument of `TestCase', namely
      `(assertRaises
          "throw exception"
          (IndexOutOfBounds "Index out of bounds : 4")
          (foo 4))'

What’s wrong ?

  • 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-31T08:23:33+00:00Added an answer on May 31, 2026 at 8:23 am

    assertRaises expects its third argument to be an IO action (with type IO a), but the return type from foo is a number (with type (Num a, Ord a) => a), not an IO action.

    Try replacing (foo 4) with (evaluate (foo 4)).

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

Sidebar

Related Questions

I am currently learning Haskell. I am trying to write a function which given
Learning haskell and want a function to generate a 2D grid similar to how
I'm learning Haskell and I'm trying to write a function to return a list
I'm trying to broaden my mind by learning Haskell. My self-inflicted homework was to
I'm still learning Haskell and need help with the type inference please! Using packages
I'm learning Haskell, and these are my first steps in trying to understand the
I'm just learning Haskell, and trying to figure out the most idiomatic way to
I'm learning Haskell now and I'm facing the following problem: I want to rewrite
Hey, I'm learning Haskell and I'm interested in using it to make static libraries
I am learning Haskell. I have created function which returns multiplication table up to

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.