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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:24:39+00:00 2026-06-13T16:24:39+00:00

Possible Duplicate: Can GHC warn if class instance is a loop? Consider a type

  • 0

Possible Duplicate:
Can GHC warn if class instance is a loop?

Consider a type class with two methods which are implementable in terms of each other:

class Num a => Foo a where

  foo :: a

  bar :: a -> a
  bar x = baz x + 1

  baz :: a -> a
  baz x = bar x - 1

Depending on the type, it might be easier to implement bar or baz, or you might want to give implementations of both of them for efficiency reasons.

Now I go somewhere else and make an instance of this class

instance Foo Integer where
  foo = 1

Oops, I forgot to implement either of bar or baz! Never mind, the type system will pick that up for me, won’t it?

C:\path\to\file> ghci Foo.hs
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( Foo.hs, interpreted )
Ok, modules loaded: Main.

Hmm, apparently not. Now if I try to use my class

*Main> bar 1
<interactive>: out of memory

Uh oh. Cue hours of painful debugging.

Is there a way to let GHC know that each instance needs to specify at least one of bar or baz?

  • 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-13T16:24:45+00:00Added an answer on June 13, 2026 at 4:24 pm

    Unfortunately not. Most libraries that define type classes with defaults will specify a “minimal complete definition”, but they don’t specify that to GHC in a checkable way right now. There’s been some vague talk of implementing a pragma for this, but nothing serious as far as I know.

    Note that just checking for mutual recursion isn’t sufficient; mutually recursive default methods might be perfectly valid, e.g. some and many in Alternative.

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

Sidebar

Related Questions

Possible Duplicate: Can I get all methods of a class? Is there a way
Possible Duplicate: Can one do a for each loop in java in reverse order?
Possible Duplicate: Can Super deal with multiple inheritance? Python inheritance? I have a class
Possible Duplicate: Can a Java class add a method to itself at runtime? Is
Possible Duplicate: Can I watch an NSNotification from another class? I am currently using
Possible Duplicate: Can you overload controller methods in ASP.Net MVC? I need to 2
Possible Duplicate: Can a JavaScript function return itself? Consider the following javascript function: var
Possible Duplicate: Can you make an Extension Method Static/Shared? Extension methods are great! Pardon
Possible Duplicate: Can’t convert value type array to params object[] Why is object h
Possible Duplicate: Can main function call itself in C++? I found this problem very

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.