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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:58:10+00:00 2026-06-09T14:58:10+00:00

In many articles about Haskell they say it allows to make some checks during

  • 0

In many articles about Haskell they say it allows to make some checks during compile time instead of run time. So, I want to implement the simplest check possible – allow a function to be called only on integers greater than zero. How can I do it?

  • 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-09T14:58:11+00:00Added an answer on June 9, 2026 at 2:58 pm

    Haskell can perform some checks at compile time that other languages perform at runtime. Your question seems to imply you are hoping for arbitrary checks to be lifted to compile time, which isn’t possible without a large potential for proof obligations (which could mean you, the programmer, would need to prove the property is true for all uses).

    In the below, I don’t feel like I’m saying anything more than what pigworker touched on while mentioning the very cool sounding Inch tool. Hopefully the additional words on each topic will clarify some of the solution space for you.

    What People Mean (when speaking of Haskell’s static guarantees)

    Typically when I hear people talk about the static guarantees provided by Haskell they are talking about the Hindley Milner style static type checking. This means one type can not be confused for another – any such misuse is caught at compile time (ex: let x = "5" in x + 1 is invalid). Obviously, this only scratches the surface and we can discuss some more aspects of static checking in Haskell.

    Smart Constructors: Check once at runtime, ensure safety via types

    Gabriel’s solution is to have a type, Positive, that can only be positive. Building positive values still requires a check at runtime but once you have a positive there are no checks required by consuming functions – the static (compile time) type checking can be leveraged from here.

    This is a good solution for many many problems. I recommended the same thing when discussing golden numbers. Never-the-less, I don’t think this is what you are fishing for.

    Exact Representations

    dflemstr commented that you can use a type, Word, which is unable to represent negative numbers (a slightly different issue than representing positives). In this manner you really don’t need to use a guarded constructor (as above) because there is no inhabitant of the type that violates your invariant.

    A more common example of using proper representations is non-empty lists. If you want a type that can never be empty then you could just make a non-empty list type:

    data NonEmptyList a = Single a | Cons a (NonEmptyList a)
    

    This is in contrast to the traditional list definition using Nil instead of Single a.

    Going back to the positive example, you could use a form of Peano numbers:

    data NonNegative = One | S NonNegative
    

    Or user GADTs to build unsigned binary numbers (and you can add Num, and other instances, allowing functions like +):

    {-# LANGUAGE GADTs #-}
    
    data Zero
    data NonZero
    data Binary a where
      I :: Binary a -> Binary NonZero
      O :: Binary a -> Binary a
      Z :: Binary Zero
      N :: Binary NonZero
    
    instance Show (Binary a) where
            show (I x) = "1" ++ show x
            show (O x) = "0" ++ show x
            show (Z)   = "0" 
            show (N)   = "1"
    

    External Proofs

    While not part of the Haskell universe, it is possible to generate Haskell using alternate systems (such as Coq) that allow richer properties to be stated and proven. In this manner the Haskell code can simply omit checks like x > 0 but the fact that x will always be greater than 0 will be a static guarantee (again: the safety is not due to Haskell).

    From what pigworker said, I would classify Inch in this category. Haskell has not grown sufficiently to perform your desired tasks, but tools to generate Haskell (in this case, very thin layers over Haskell) continue to make progress.

    Research on More Descriptive Static Properties

    The research community that works with Haskell is wonderful. While too immature for general use, people have developed tools to do things like statically check function partiality and contracts. If you look around you’ll find it’s a rich field.

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

Sidebar

Related Questions

There are many articles about how to turn off monitors in c#, but they
I have found many questions and articles about this but i still have some
I have seen many articles about POCO. What is this?
My environment: Microsoft Visual Studio 2010 C# I've read many articles and comments about,
In many books or articles u may see such a definition about interface :
I read many articles about unsafe functions like strcpy, memcpy, etc. which may lead
I read quite some articles about fragments, but I am still confused about how
I've read many articles about known types and i belive my example should work.
Many articles I've read on the Net about Singleton design pattern mention that the
After reading many articles and some questions on here, I finally succeded in activating

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.