-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nullable and boolean operations #13207
Comments
No, this is not reasonably possible. However this makes another good argument that Nullable should just be |
If that design could be made comparably efficient, then it seems like that would essentially obviate the whole issue of lifting as well. The biggest downside of the current situation I have seen is that a method defined for, say, an |
@JeffBezanson Do you think |
I won't rehash the whole debate here and don't have a strong opinion, but I'll point out that some people like |
Also One worry with making |
I think we should follow C#'s lead: only auto-lift core math operations. That solves the infinite space of possible unwrappings very readily. |
"core math operations" is a far less well-defined thing in Julia than in C#, unfortunately. |
Well, in C# comparison operators for |
This is too far afield for now, but a more flexible |
@malmaud Can you detail this use case? (Though I suspect this is exactly what Jeff is worried about... :-) |
What is the rational for not having comparisons between nullables return booleans? I can kind of see the purity argument, but is there a concrete problem it would cause? |
@malmaud what do you do if one of the arguments is null? If you choose to throw an error, you lose the ability to propagate missingness. If you return an empty |
Return |
|
Here's my comment above phrased as a reason for why auto-lifting On the other hand, I can see how automatically lifting |
I suppose a case could be made that it could return |
@davidagold That is exactly the same situation that probabilistic programming faces, so it's not unique to Nullable. |
@malmaud I'd still be very interested in hearing more about that use case. How would it work with |
@nalimilan
This is all elegant because of
Then we are out of luck. The ideal situation would be something like, for
would evaluate to something like If @zenna might have thoughts on this. |
@malmaud Looks like applying the principle of multiple dispatch everywhere. That's an interesting investigation, but one with far-reaching consequences that I don't even dare imagine... Maybe you can get Jeff interested by this new challenge, after all. :-p I guess we can close this issue, let's see how far we can get without this special support for |
I'm not sure about Nullables but in general the limitations on My use case is in probabilistic programming. We have a random variable type And you can, for the most part, with the glaring exception of using It's not so much of an issue if all of your code is your own, then you can just use cancer_base_rate = 0.01
breast_cancer = flip(cancer_base_rate)
positive_mammogram = ifelse(breast_cancer,flip(0.8),flip(0.096))
prob(breast_cancer, positive_mammogram) The real problem is that when I want to use libraries written by other people where they use a normal if. It's frustrating because many functions almost work (if they've written their code sufficiently generally) but not quite, usually because of short-circuiting functions. I have no idea where these limitations are fundamental; but if they're not, there are real use cases. |
@nalimilan I quickly wrote a simple package to demo how easy in principle it would be for Julia to allow customizing the behavior of control structures based on the type of the predicate. |
I don't think this approach handles side effects correctly ? You would at least need to pass lambdas to be able to control execution. You might for example want to run a loop until "convergence", i.e., |
This may be all wet, but could this be handled by having the compiler simply lower things to something like: handle_cond(cond::Bool) = cond
if cond -> if handle_cond(cond) ...
while cond -> while handle_cond(cond) ... and then overriding handle_cond for Nullables? If the compiler can return typeof(b) without evaluating b, could it do something like the following, where _ is just a temp variable? a || b -> (handle_cond(_ = (a), typeof(b)) ? _ : (b))
a && b -> (handle_cond(_ = (a), typeof(b)) ? (b) : _) |
It's certainly easy to insert, say, Of course that feature is also totally inadequate for @zenna's case. There you need to evaluate both branches of the |
I agree with Jeff 100% here. This would solve only a little bit of the whole problem in a very patchwork sort of way and just leave you wanting more. Abstract interpretation is the correct approach. @carnaval, how about doing some kind of announcement and open sourcing of the green fairy? |
https://github.com/carnaval/green-fairy but it could use a license and some "what this code do" docs |
@carnaval I'm not sure I see the problem when evaluating the conditional causes side effects-Wouldn't simply translating It's true though if both branches of the |
@davidagold raised at JuliaStats/NullableArrays.jl#62 the issue that
Nullable{Bool}
currently doesn't work well with boolean operations, includingif
,&&
and||
, which has consequences on many functions, likefind
.Nullable
:NullableArrays.jl improves this by comparing the contents of the
Nullable
s and returning aNullable{Bool}
, raising an error if one of them isNULL
.Nullable
s isn't convenient, as it cannot be used withif
,&&
and||
:This means that
NullableArray
needs to come with its own implementation for many functions to save the user from typingget
all over the place. For example:Would it be possible to allow
Nullable{Bool}
inif
,&&
and||
? The semantics are well-defined: they would be strictly equivalent to callingget
on theNullable
:Nullable(true)
is equivalent totrue
,Nullable(false)
is equivalent tofalse
, andNullable()
raises an error. Special-casingNullable
would have the advantage that no notion of "truthiness" is introduced in Julia, with the drawback that it wouldn't be generic -- but maybeNullable
is the only type for which this definition makes sense.Cc: @davidagold @johnmyleswhite
The text was updated successfully, but these errors were encountered: