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

The Archive Base Latest Questions

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

Note to other potential contributors: Please don’t hesitate to use abstract or mathematical notations

  • 0

Note to other potential contributors: Please don’t hesitate to use abstract or mathematical notations to make your point. If I find your answer unclear, I will ask for elucidation, but otherwise feel free to express yourself in a comfortable fashion.

To be clear: I am not looking for a “safe” head, nor is the choice of head in particular exceptionally meaningful. The meat of the question follows the discussion of head and head', which serve to provide context.

I’ve been hacking away with Haskell for a few months now (to the point that it has become my main language), but I am admittedly not well-informed about some of the more advanced concepts nor the details of the language’s philosophy (though I am more than willing to learn). My question then is not so much a technical one (unless it is and I just don’t realize it) as it is one of philosophy.

For this example, I am speaking of head.

As I imagine you’ll know,

Prelude> head []    
*** Exception: Prelude.head: empty list

This follows from head :: [a] -> a. Fair enough. Obviously one cannot return an element of (hand-wavingly) no type. But at the same time, it is simple (if not trivial) to define

head' :: [a] -> Maybe a
head' []     = Nothing
head' (x:xs) = Just x

I’ve seen some little discussion of this here in the comment section of certain statements. Notably, one Alex Stangl says

‘There are good reasons not to make everything “safe” and to throw exceptions when preconditions are violated.’

I do not necessarily question this assertion, but I am curious as to what these “good reasons” are.

Additionally, a Paul Johnson says,

‘For instance you could define “safeHead :: [a] -> Maybe a”, but now instead of either handling an empty list or proving it can’t happen, you have to handle “Nothing” or prove it can’t happen.’

The tone that I read from that comment suggests that this is a notable increase in difficulty/complexity/something, but I am not sure that I grasp what he’s putting out there.

One Steven Pruzina says (in 2011, no less),

“There’s a deeper reason why e.g ‘head’ can’t be crash-proof. To be polymorphic yet handle an empty list, ‘head’ must always return a variable of the type which is absent from any particular empty list. It would be Delphic if Haskell could do that…”.

Is polymorphism lost by allowing empty list handling? If so, how so, and why? Are there particular cases which would make this obvious? This section amply answered by @Russell O’Connor. Any further thoughts are, of course, appreciated.

I’ll edit this as clarity and suggestion dictates. Any thoughts, papers, etc., you can provide will be most appreciated.

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

    Is polymorphism lost by allowing empty
    list handling? If so, how so, and why?
    Are there particular cases which would
    make this obvious?

    The free theorem for head states that

    f . head = head . $map f
    

    Applying this theorem to [] implies that

    f (head []) = head (map f []) = head []
    

    This theorem must hold for every f, so in particular it must hold for const True and const False. This implies

    True = const True (head []) = head [] = const False (head []) = False
    

    Thus if head is properly polymorphic and head [] were a total value, then True would equal False.

    PS. I have some other comments about the background to your question to the effect of if you have a precondition that your list is non-empty then you should enforce it by using a non-empty list type in your function signature instead of using a list.

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

Sidebar

Related Questions

Note - I have moved the original post to the bottom because I think
Say I have an online store wherein each product has a single category (and
In answer to this SQL question , I've encountered a statement that fixed-value IN()
I have binary A , which is a debug build with accompanying symbols --
The url_helpers do not seem to be working in my User model during an
I'm looking for the cleverest algorithm for determining the number of fortnightly occurring events
As a followup to Class containing auto_ptr stored in vector , I believe the
This is my first question here on stackoverflow, so I hope that I am
I am currently working with a large XML file which includes <Count>123</Count> for some
I've googled this, as well as checked the documentation , but not been able

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.