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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:22:33+00:00 2026-05-31T04:22:33+00:00

I want to define what seems to require an infinite type. Required : a

  • 0

I want to define what seems to require an infinite type.

Required : a function “eat” that eats all it’s arguments except “3” for which it returns 3

eat 3 = 3
eat x = eat

So basically an arbitrary expression like “eat (+) foldl (Just 5) 3” evaluates to 3. But the problem here is the type of eat. What should that be?

The closest i got to a running code was :

newtype Rec = MakeRec (Int -> Rec)

eat :: Int -> Rec
eat x = MakeRec eat


instance Show Rec where
     show _ = "EAT"

This works okay for “eat 6” but not for “eat 6 7” and it doesn’t work if i put (eat 3 = 3) in it’s definition.

I am not sure if this is even possible in Haskell. (What argument would one use to show it’s not possible ?)

UPDATE : As noted in solution below, the type-information is needed at compile time so that compiler can know if “eat foldl 3 foldl” is invalid or not. So, exact solution to this problem is not possible.

  • 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-31T04:22:34+00:00Added an answer on May 31, 2026 at 4:22 am

    It’s not possible. Infinite types (that aren’t data types) are explicitly forbidden in Haskell, and it’s easy to produce code which would require them, and thus produces an error (for example, try let foo = (42, foo) in foo).

    You can, of course, create them with newtype and data, like you did, but then you have to explicitly wrap and unwrap the values in and out of constructors.

    This is an explicit design decision: with infinite types, many obviously wrong expressions that we would like the compiler to reject would have to be allowed, and since many more programs would be allowed, a lot of previously unambiguously-typed programs would become ambiguously typed,1 requiring explicit type annotations. So a tradeoff is made: requiring you to be explicit about the fairly rare uses of infinite types in return for getting much more help from the type system than we otherwise would.

    That said, there is a way to define something similar to your eat function, using typeclasses, but it can’t stop only when you give it a 3: whether you’ve given it a 3 or not can only be determined at runtime, and types are decided at compile time. However, here’s an overloaded value that can be both an Integer, and a function that just eats up its argument:

    class Eat a where
        eat :: a
    
    instance Eat Integer where
        eat = 3
    
    instance (Eat r) => Eat (a -> r) where
        eat _ = eat
    

    The catch is that you need to specify the types precisely when you use it:

    *Main> eat 1 foldr ()
    
    <interactive>:6:1:
        Ambiguous type variable `t0' in the constraint:
          (Eat t0) arising from a use of `eat'
        Probable fix: add a type signature that fixes these type variable(s)
        In the expression: eat 1 foldr ()
        In an equation for `it': it = eat 1 foldr ()
    *Main> eat 1 foldr () :: Integer
    3
    

    This is because eat 1 foldr () could be an Integer, but it could also be another function, just as we used eat 1 and eat 1 foldr as functions in the same expression. Again, we get flexible typing, but have to explicitly specify the types we want in return.

    1 Think typeclass overloading, like the overloaded numeric literals (42 can be any type that’s an instance of Num).

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

Sidebar

Related Questions

I want to define a constant that should be available in all of the
I want to define a JavaScript class, Foo. Foo = function(value){ this.value = value;
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I want to define a class that supports __getitem__ , but does not allow
I want to define a constant in objective-c. Previously I had the following function:
I want to define a class method that has access to a local variable.
Basic PHP design questions: I want to define a couple of constants that are
I want to define something like this in php : $EL = \n<br />\n;
I want to define some member variable and some code just in Debug Mode,
I want to define some properties on a class using the [Indexable()] attribute in

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.