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 that I'm aware of other yield in vb.net questions here on SO. I'm
The Erlang documentation states the following about gen_servers: ... Note that for any other
Note: I am not asking how to use Google Code's SVN repo as a
Note: Not sure if this is the right stack, please tell if I should
Greetings, Is there any open source graph database available other than Neo4J?? NOTE: Why
I run all my integers through a (int)Integer to make them safe to use
Note: This other question seems relevant but it's not: When does an associated object
I have two tables one called fs_note the other called dumy_fs_note I created after
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code

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.