Skip to content
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

What should if (maybe) do? #235

Open
FeldrinH opened this issue Jul 4, 2023 · 14 comments
Open

What should if (maybe) do? #235

FeldrinH opened this issue Jul 4, 2023 · 14 comments

Comments

@FeldrinH
Copy link

FeldrinH commented Jul 4, 2023

It feels like the section on maybe could really use an example of what trying to do if (maybe) ... does.

I see two possible possibilities:

  • The if statement gives up and produces and error
  • The if statement picks randomly between true and false
@Taureon
Copy link

Taureon commented Jul 4, 2023

it would do it either probably or half
probably = 50% chance to do it
half = counter = counter + 10 would just add 5

@conqp
Copy link

conqp commented Jul 5, 2023

I'd recommend that maybe infers its chance depending on how it has been generated:

b = maybe!  // 50% true
b = maybe & true!  // 66.666..% true
b = maybe & true & true!  // 75% true
...

@stohrendorf
Copy link
Contributor

As far as I know, there's some sort of "probabilistic boolean algebra", but I can't recall how it is named. IIRC, && boils down to a multiplication of probabilities, and a || b boils down to min(a+b,1), but I could be wrong.

@tetra-coder
Copy link
Contributor

maybe should roll a coin(pick 0 or 1) and do the operation at a 50% chance.
however, we will be also be able to use percentages because maybe && true is too much.

b = 50%! // same as maybe
c = 90% // likely
d = 10% // unlikely

and i think that "maybe" was a const const const variable made by some user

@darinsecurity
Copy link

The compiler will implement quantum superposition in the manner described: #271

@Snapstromegon
Copy link

On machines where quantum superpositions can't be used, the compiler should instead do some parts of both branches. For example like this:

if(maybe) {
   print("Hello World")
   print("1")
   print("a")
   print("b")
} else {
   print("else")
   print("x")
   print("2")
   print("3")
}

// Result:
Hello World
1
2
3

Which lines get selected could be up to runtime randomness, as long as 50% of lines of each branch get executed.

Another option would be to introduce definitely (or short def) as a response to maybe.

if(maybe) {
   print("true")
} else {
   print("false")
} def {
   print("Hi")
}

// Result:
Hi

@arontsang
Copy link

The obvious behaviour for

if (maybe)
     callMeMaybe()!

To never call. This is the expected behaviour as demonstrated by user Carly R Jepson.

@james-cnz
Copy link

As far as I know, there's some sort of "probabilistic boolean algebra", but I can't recall how it is named. IIRC, && boils down to a multiplication of probabilities, and a || b boils down to min(a+b,1), but I could be wrong.

I'd guess a || b would be 1 - (1-a)*(1-b)
i.e. to find the probability that either are true, invert the probability that both are false.

@james-cnz
Copy link

It seems that DreamBerd is using ternary logic, and I think the third value usually simply represents that the value is unknown, not specifying any particular probability. I guess one way to approach this, if only variable assignments were involved, would be to apply any assignments that were consistent between both branches, and set any variables that were assigned differently between branches to be undefined.

e.g. given

if (condition) {
  x = 1!
  y = 2!
} else {
  x = 1!
  y = 3!
}

x would be set to 1 and y would be set to undefined. This would be complicated to implement, though, and would only work for variable assignment, not output, so I don't think it's really practical.

I think Snapstromegon's second suggestion, of adding a third clause to the if statement is probably more practical. I'd possibly rename it, though. Perhaps:
if () {} indoubt {} else {}

@stohrendorf
Copy link
Contributor

Again, there's a formal specification for that "fuzzy boolean", I just can't remember the name, and I fail to cite from memory.

@arontsang
Copy link

arontsang commented Nov 16, 2023 via email

@stohrendorf
Copy link
Contributor

It's actually called Fuzzy Logic :/

@arontsang
Copy link

arontsang commented Nov 16, 2023 via email

@james-cnz
Copy link

OK. I was just going by what was written in your comment. You mentioned "multiplication of probabilities". Assuming we're working with probabilities, a && b would be a * b for independent variables. Under the same conditions, a || b would be 1 - (1-a)*(1-b), I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants