Zulip Chat Archive
Stream: std4
Topic: Unboxed `Partial`?
Mario Carneiro (Jun 28 2023 at 05:24):
I don't really buy that we would save by factoring a.get!
into a.get?.get!
. Rust uses a[i]
instead of a.get(i).unwrap()
even though the latter functions are pretty much designed for the purpose and this is zero overhead
Mario Carneiro (Jun 28 2023 at 05:25):
These are just incredibly common functions, such that a shorthand is warranted
Mario Carneiro (Jun 28 2023 at 05:25):
(Even if we ignore the fact that a.get?.get!
is not zero cost in lean)
James Gallicchio (Jun 28 2023 at 05:25):
RE: get
/getD
/get!
/get?
all over the standard collections APIs -- do we think it's possible to get a zero-cost implementation of Partial
, where values are not boxed into a thunk? e.g. the runtime representation of Partial A h
is the same as A
if h
is true, and undefined if h
is not true
James Gallicchio (Jun 28 2023 at 05:26):
To me, (some operation).get.!
reads just as fine as (some operation).get!
, and cuts down on the size of the API that we need to maintain.
Mario Carneiro (Jun 28 2023 at 05:27):
The problem is that you would have to evaluate the expression to get something of type A
Mario Carneiro (Jun 28 2023 at 05:27):
but if it's undefined then this could cause UB
Mario Carneiro (Jun 28 2023 at 05:27):
so you have to leave it unevaluated, hence it's a thunk
Mario Carneiro (Jun 28 2023 at 05:27):
you can't evaluate it until you receive evidence that h is actually true
James Gallicchio (Jun 28 2023 at 05:30):
Yeah, I guess the compiler would have to inline whatever produces the Partial
James Gallicchio (Jun 28 2023 at 05:30):
in order to then optimize the box(0) application away
James Gallicchio (Jun 28 2023 at 05:31):
So I guess the question then is whether we could get compiler support for optimizing eliminators of Partial
, which seems substantially less likely
Mario Carneiro (Jun 28 2023 at 05:32):
that almost certainly already exists, assuming Partial
= Option
Mario Carneiro (Jun 28 2023 at 05:32):
you just have to inline enough stuff to make it possible to do
James Gallicchio (Jun 28 2023 at 05:33):
ah, hm
James Gallicchio (Jun 28 2023 at 05:34):
Let me see if I can get a Partial
version of Array.get
to produce identical bytecode
James Gallicchio (Jun 28 2023 at 05:40):
I guess it almost makes more sense to have an API for producing Fin n
s by panicking on out of bounds :stuck_out_tongue:
Mario Carneiro (Jun 28 2023 at 05:41):
that's fine, although you can't define it for n = 0
Mario Carneiro (Jun 28 2023 at 05:41):
which makes it a lot less ergonomic than alternatives
James Gallicchio (Jun 28 2023 at 05:44):
that's an ergonomic issue of panic! generally, where there's no easy way to say that you want to escape to a farther out context which is Inhabited
. the only solution I have to that ergonomics issue would be to start putting lots of stuff in an Except
monad instead of panicking.
James Gallicchio (Jun 28 2023 at 05:45):
get! : [Inhabited A] -> Except E A -> A
Mario Carneiro (Jun 28 2023 at 05:47):
is that supposed to be returning something of type A
?
James Gallicchio (Jun 28 2023 at 05:48):
maybe add a [ToString E]
parameter to put in the panic message
James Gallicchio (Jun 28 2023 at 05:50):
maybe we have a ifOk : [IfOk E A B] -> A -> Except E B
API for decidably failing coercions
James Gallicchio (Jun 28 2023 at 05:50):
get! <| do return A.get (←ifOk 0)
does start to look absurd though :)
Mario Carneiro (Jun 28 2023 at 05:51):
yeah, I'm not seeing the ergonomic gain
James Gallicchio (Jun 28 2023 at 05:53):
I've definitely written code that would be cleaner with these abstractions, and not even niche code. But for small use cases it doesn't make sense.
James Gallicchio (Jun 28 2023 at 05:54):
Idk. I will push through the repetitive API for now
James Gallicchio (Jun 28 2023 at 06:13):
Honestly, textbook rust just opens Except String
monad contexts everywhere and uses ?
as a synonym for .←
. I feel somewhat justified in doing the same in Lean.
Mario Carneiro (Jun 28 2023 at 06:19):
okay, but that's not the API in question
Mario Carneiro (Jun 28 2023 at 06:21):
Now is not a good time for redesigning Array
. The API exists, we are just making the dependent version
James Gallicchio (Jun 28 2023 at 06:22):
:thumbs_up: will revisit this at some point
François G. Dorais (Jun 28 2023 at 09:58):
Sounds to me that the general end goal here is a more seamless interface for "safe laziness"? I've been wondering about that for a while too.
James Gallicchio (Jun 30 2023 at 01:11):
Not laziness but just better interfaces for handling quotients and partials
Sebastian Ullrich (Jun 30 2023 at 07:30):
I believe I had a proposal once for unified ?!D but I wouldn't know where to even start looking for it...
Last updated: Dec 20 2023 at 11:08 UTC