Re: HasCallStack constraint for maximum in Prelude?
Oleg Grenrus <[email protected]> Tue, 24 Mar 2026 14:38:55 +0200
| Newsgroups | gmane.comp.lang.haskell.cafe |
|---|---|
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format. --===============1168480840022550145== Content-Type: multipart/alternative; boundary="------------R4dE7RL8Q73uVXlfFy1dQMI1" Content-Language: en-US This is a multi-part message in MIME format. --------------R4dE7RL8Q73uVXlfFy1dQMI1 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit While we are on it. Add `x-partial` to `List.maximum/minimum` and `Foldable.maximum/minimum` and point to Foldable1 variants. Adding that warning won't affect performance, but it would guide people to never use them (Especially Foldable variant; its type is wrong - it would make some sense with Bounded - if Bounded were related to Ord). - Oleg On 3/23/26 02:40, Andrew Lelechenko wrote: > Hi Todd, > > You can find some discussion at > https://mail.haskell.org/pipermail/libraries/2021-May/031246.html. > > It’s tricky: a monomorphic GHC.List.maximum :: [a] -> a got > HasCallStack, while a polymorphic one Data.Foldable.maximum :: > Foldable t => t a -> a didn’t. The reason for this was a concern that > adding HasCallStack to class methods might have adverse performance > consequences. So out of abundance of caution and to get at least > something done, the proposal targeted only monomorphic partial > functions. I think an enthusiastic proposer, willing to benchmark it, > could add HasCallStack to polymorphic partial functions as well (I > would personally be in favour). > > Also, there is a total Data.Foldable1.maximum at > https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Data-Foldable1.html#t:Foldable1. > > Best regards, > Andrew > >> On 22 Mar 2026, at 23:48, Todd Wilson via Haskell-Cafe >> <[email protected]> wrote: >> >> I sent this two months ago but received no response. One of my >> students asked me this, and I didn't know how to answer, so I asked >> here. Did it slip in under the radar? Did I ask it in the wrong >> forum? Or was everyone waiting for someone else to answer? :-) >> >> Todd Wilson >> Department of Computer Science >> California State University, Fresno >> >> On Sat, Jan 24, 2026 at 1:22 PM Todd Wilson <[email protected]> >> wrote: >> >> Should maximum and minimum have HasCallStack constraints like >> head and tail do? >> >> ghci> :type head >> head :: GHC.Stack.Types.HasCallStack => [a] -> a >> ghci> head [] >> *** Exception: Prelude.head: empty list >> CallStack (from HasCallStack): >> error, called at libraries/base/GHC/List.hs:1646:3 in base:GHC.List >> errorEmptyList, called at libraries/base/GHC/List.hs:85:11 in >> base:GHC.List >> badHead, called at libraries/base/GHC/List.hs:81:28 in >> base:GHC.List >> head, called at <interactive>:8:1 in interactive:Ghci1 >> ghci> :type maximum >> maximum :: (Foldable t, Ord a) => t a -> a >> ghci> maximum [] >> *** Exception: Prelude.maximum: empty list >> CallStack (from HasCallStack): >> error, called at libraries/base/GHC/List.hs:1646:3 in base:GHC.List >> errorEmptyList, called at libraries/base/GHC/List.hs:717:28 in >> base:GHC.List >> maximum, called at libraries/base/Data/Foldable.hs:745:15 in >> base:Data.Foldable >> >> Todd Wilson >> >> _______________________________________________ >> Haskell-Cafe mailing list -- [email protected] >> To (un)subscribe, modify options or view archives go to: >> Only members subscribed via the mailman list are allowed to post. > > > _______________________________________________ > Haskell-Cafe mailing list [email protected] > To (un)subscribe, modify options or view archives go to: > Only members subscribed via the mailman list are allowed to post. --------------R4dE7RL8Q73uVXlfFy1dQMI1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <p>While we are on it. Add `x-partial` to `List.maximum/minimum` and `Foldable.maximum/minimum` and point to Foldable1 variants.<br> <br> Adding that warning won't affect performance, but it would guide people to never use them (Especially Foldable variant; its type is wrong - it would make some sense with Bounded - if Bounded were related to Ord).<br> <br> - Oleg</p> <div class="moz-cite-prefix">On 3/23/26 02:40, Andrew Lelechenko wrote:<br> </div> <blockquote type="cite" cite="mid:[email protected]"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> Hi Todd, <div><br> </div> <div>You can find some discussion at <a href="https://mail.haskell.org/pipermail/libraries/2021-May/031246.html" moz-do-not-send="true" class="moz-txt-link-freetext">https://mail.haskell.org/pipermail/libraries/2021-May/031246.html</a>.</div> <div><br> </div> <div>It’s tricky: a monomorphic GHC.List.maximum :: [a] -> a got HasCallStack, while a polymorphic one Data.Foldable.maximum :: Foldable t => t a -> a didn’t. The reason for this was a concern that adding HasCallStack to class methods might have adverse performance consequences. So out of abundance of caution and to get at least something done, the proposal targeted only monomorphic partial functions. I think an enthusiastic proposer, willing to benchmark it, could add HasCallStack to polymorphic partial functions as well (I would personally be in favour).</div> <div><br> </div> <div>Also, there is a total Data.Foldable1.maximum at <a href="https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Data-Foldable1.html#t:Foldable1" moz-do-not-send="true" class="moz-txt-link-freetext">https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Data-Foldable1.html#t:Foldable1</a>.</div> <div><br> </div> <div>Best regards,</div> <div>Andrew<br id="lineBreakAtBeginningOfMessage"> <div><br> <blockquote type="cite"> <div>On 22 Mar 2026, at 23:48, Todd Wilson via Haskell-Cafe <a class="moz-txt-link-rfc2396E" href="mailto:[email protected]"><[email protected]></a> wrote:</div> <br class="Apple-interchange-newline"> <div> <div dir="ltr"> <div dir="ltr">I sent this two months ago but received no response. One of my students asked me this, and I didn't know how to answer, so I asked here. Did it slip in under the radar? Did I ask it in the wrong forum? Or was everyone waiting for someone else to answer? :-) <div> <div><br clear="all"> </div> <div> <div dir="ltr" class="gmail_signature"> <div dir="ltr">Todd Wilson <div>Department of Computer Science</div> <div>California State University, Fresno</div> </div> </div> </div> </div> </div> <br> <div class="gmail_quote gmail_quote_container"> <div dir="ltr" class="gmail_attr">On Sat, Jan 24, 2026 at 1:22 PM Todd Wilson <<a href="mailto:[email protected]" moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a>> wrote:<br> </div> <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <div dir="ltr"> <div>Should maximum and minimum have HasCallStack constraints like head and tail do?</div> <div><br> </div> ghci> :type head<br> head :: GHC.Stack.Types.HasCallStack => [a] -> a<br> ghci> head []<br> *** Exception: Prelude.head: empty list<br> CallStack (from HasCallStack):<br> error, called at libraries/base/GHC/<a class="moz-txt-link-freetext" href="List.hs:1646:3">List.hs:1646:3</a> in <a class="moz-txt-link-freetext" href="base:GHC.List">base:GHC.List</a><br> errorEmptyList, called at libraries/base/GHC/<a class="moz-txt-link-freetext" href="List.hs:85:11">List.hs:85:11</a> in <a class="moz-txt-link-freetext" href="base:GHC.List">base:GHC.List</a><br> badHead, called at libraries/base/GHC/<a class="moz-txt-link-freetext" href="List.hs:81:28">List.hs:81:28</a> in <a class="moz-txt-link-freetext" href="base:GHC.List">base:GHC.List</a><br> head, called at <interactive>:8:1 in <a class="moz-txt-link-freetext" href="interactive:Ghci1">interactive:Ghci1</a><br> ghci> :type maximum<br> maximum :: (Foldable t, Ord a) => t a -> a<br> ghci> maximum []<br> *** Exception: Prelude.maximum: empty list<br> CallStack (from HasCallStack):<br> error, called at libraries/base/GHC/<a class="moz-txt-link-freetext" href="List.hs:1646:3">List.hs:1646:3</a> in <a class="moz-txt-link-freetext" href="base:GHC.List">base:GHC.List</a><br> errorEmptyList, called at libraries/base/GHC/<a class="moz-txt-link-freetext" href="List.hs:717:28">List.hs:717:28</a> in <a class="moz-txt-link-freetext" href="base:GHC.List">base:GHC.List</a><br> maximum, called at libraries/base/Data/<a class="moz-txt-link-freetext" href="Foldable.hs:745:15">Foldable.hs:745:15</a> in <a class="moz-txt-link-freetext" href="base:Data.Foldable">base:Data.Foldable</a><br> <div><br> </div> <div>Todd Wilson</div> </div> </blockquote> </div> </div> _______________________________________________<br> Haskell-Cafe mailing list -- <a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a><br> To (un)subscribe, modify options or view archives go to:<br> Only members subscribed via the mailman list are allowed to post.</div> </blockquote> </div> <br> </div> <br> <fieldset class="moz-mime-attachment-header"></fieldset> <pre wrap="" class="moz-quote-pre">_______________________________________________ Haskell-Cafe mailing list -- <a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a> To (un)subscribe, modify options or view archives go to: Only members subscribed via the mailman list are allowed to post.</pre> </blockquote> </body> </html> --------------R4dE7RL8Q73uVXlfFy1dQMI1-- --===============1168480840022550145== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Haskell-Cafe mailing list -- [email protected] To (un)subscribe, modify options or view archives go to: Only members subscribed via the mailman list are allowed to post. --===============1168480840022550145==--