Re: HasCallStack constraint for maximum in Prelude?

Andrew Lelechenko <[email protected]> Mon, 23 Mar 2026 00:40:32 +0000
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <[email protected]>
--===============7260208665706651074==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_95E176B8-4CBA-43C0-881B-D77EF71555C4"


--Apple-Mail=_95E176B8-4CBA-43C0-881B-D77EF71555C4
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=utf-8

Hi Todd,

You can find some discussion at =
https://mail.haskell.org/pipermail/libraries/2021-May/031246.html.

It=E2=80=99s tricky: a monomorphic GHC.List.maximum :: [a] -> a got =
HasCallStack, while a polymorphic one Data.Foldable.maximum :: Foldable =
t =3D> t a -> a didn=E2=80=99t. 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-Foldab=
le1.html#t:Foldable1.

Best regards,
Andrew

> On 22 Mar 2026, at 23:48, Todd Wilson via Haskell-Cafe =
<[email protected]> wrote:
>=20
> 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? :-)
>=20
> Todd Wilson
> Department of Computer Science
> California State University, Fresno
>=20
> On Sat, Jan 24, 2026 at 1:22=E2=80=AFPM Todd Wilson =
<[email protected] <mailto:[email protected]>> wrote:
>> Should maximum and minimum have HasCallStack constraints like head =
and tail do?
>>=20
>> ghci> :type head
>> head :: GHC.Stack.Types.HasCallStack =3D> [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) =3D> 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
>>=20
>> 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.


--Apple-Mail=_95E176B8-4CBA-43C0-881B-D77EF71555C4
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=utf-8

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; =
charset=3Dutf-8"></head><body style=3D"overflow-wrap: break-word; =
-webkit-nbsp-mode: space; line-break: after-white-space;">Hi =
Todd,<div><br></div><div>You can find some discussion at&nbsp;<a =
href=3D"https://mail.haskell.org/pipermail/libraries/2021-May/031246.html"=
>https://mail.haskell.org/pipermail/libraries/2021-May/031246.html</a>.</d=
iv><div><br></div><div>It=E2=80=99s tricky: a monomorphic =
GHC.List.maximum :: [a] -&gt; a got HasCallStack, while a polymorphic =
one Data.Foldable.maximum :: Foldable t =3D&gt; t a -&gt; a didn=E2=80=99t=
. 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&nbsp;<a =
href=3D"https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Dat=
a-Foldable1.html#t:Foldable1">https://hackage-content.haskell.org/package/=
base-4.22.0.0/docs/Data-Foldable1.html#t:Foldable1</a>.</div><div><br></di=
v><div>Best regards,</div><div>Andrew<br =
id=3D"lineBreakAtBeginningOfMessage"><div><br><blockquote =
type=3D"cite"><div>On 22 Mar 2026, at 23:48, Todd Wilson via =
Haskell-Cafe &lt;[email protected]&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div><div dir=3D"ltr"><div =
dir=3D"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=3D"all"></div><div><div dir=3D"ltr" class=3D"gmail_signature"><div =
dir=3D"ltr">Todd Wilson<div>Department of Computer =
Science</div><div>California State University, =
Fresno</div></div></div></div></div></div><br><div class=3D"gmail_quote =
gmail_quote_container"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Jan =
24, 2026 at 1:22=E2=80=AFPM Todd Wilson &lt;<a =
href=3D"mailto:[email protected]">[email protected]</a>&gt; =
wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px =
0px 0px 0.8ex;border-left:1px solid =
rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div>Should maximum =
and minimum have HasCallStack constraints like head and tail =
do?</div><div><br></div>ghci&gt; :type head<br>head :: =
GHC.Stack.Types.HasCallStack =3D&gt; [a] -&gt; a<br>ghci&gt; head =
[]<br>*** Exception: Prelude.head: empty list<br>CallStack (from =
HasCallStack):<br>&nbsp; error, called at =
libraries/base/GHC/List.hs:1646:3 in base:GHC.List<br>&nbsp; =
errorEmptyList, called at libraries/base/GHC/List.hs:85:11 in =
base:GHC.List<br>&nbsp; badHead, called at =
libraries/base/GHC/List.hs:81:28 in base:GHC.List<br>&nbsp; head, called =
at &lt;interactive&gt;:8:1 in interactive:Ghci1<br>ghci&gt; :type =
maximum<br>maximum :: (Foldable t, Ord a) =3D&gt; t a -&gt; =
a<br>ghci&gt; maximum []<br>*** Exception: Prelude.maximum: empty =
list<br>CallStack (from HasCallStack):<br>&nbsp; error, called at =
libraries/base/GHC/List.hs:1646:3 in base:GHC.List<br>&nbsp; =
errorEmptyList, called at libraries/base/GHC/List.hs:717:28 in =
base:GHC.List<br>&nbsp; maximum, called at =
libraries/base/Data/Foldable.hs:745:15 in =
base:Data.Foldable<br><div><br></div><div>Todd Wilson</div></div>
</blockquote></div></div>
_______________________________________________<br>Haskell-Cafe mailing =
list -- [email protected]<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></body></html>=

--Apple-Mail=_95E176B8-4CBA-43C0-881B-D77EF71555C4--

--===============7260208665706651074==
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.
--===============7260208665706651074==--