DreamBerd icon indicating copy to clipboard operation
DreamBerd copied to clipboard

What should `if (maybe)` do?

Open FeldrinH opened this issue 2 years ago • 14 comments

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

FeldrinH avatar Jul 04 '23 22:07 FeldrinH

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

Taureon avatar Jul 04 '23 22:07 Taureon

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

conqp avatar Jul 05 '23 11:07 conqp

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.

stohrendorf avatar Jul 05 '23 12:07 stohrendorf

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

tetra-coder avatar Jul 13 '23 02:07 tetra-coder

The compiler will implement quantum superposition in the manner described: https://github.com/TodePond/DreamBerd/discussions/271

darinsecurity avatar Jul 21 '23 03:07 darinsecurity

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

Snapstromegon avatar Jul 21 '23 08:07 Snapstromegon

The obvious behaviour for

if (maybe)
     callMeMaybe()!

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

arontsang avatar Jul 26 '23 05:07 arontsang

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 avatar Nov 15 '23 08:11 james-cnz

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 {}

james-cnz avatar Nov 15 '23 08:11 james-cnz

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

stohrendorf avatar Nov 15 '23 22:11 stohrendorf

Fuzzy memory?

On Thu, 16 Nov 2023, 06:21 Steffen Ohrendorf, @.***> wrote:

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

— Reply to this email directly, view it on GitHub https://github.com/TodePond/DreamBerd/issues/235#issuecomment-1813354219, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA46MLPBHRUM6X6KQL2753LYEU55HAVCNFSM6AAAAAAZ6GGUYOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJTGM2TIMRRHE . You are receiving this because you commented.Message ID: @.***>

arontsang avatar Nov 16 '23 03:11 arontsang

It's actually called Fuzzy Logic :/

stohrendorf avatar Nov 16 '23 05:11 stohrendorf

No, I meant, you "fail(ed) to cite from [your] (fuzzy) memory".

On Thu, 16 Nov 2023, 13:24 Steffen Ohrendorf, @.***> wrote:

It's actually called Fuzzy Logic https://en.wikipedia.org/wiki/Fuzzy_logic :/

— Reply to this email directly, view it on GitHub https://github.com/TodePond/DreamBerd/issues/235#issuecomment-1813809738, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA46MLJALCUFS44RUZQLYJ3YEWPRVAVCNFSM6AAAAAAZ6GGUYOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJTHAYDSNZTHA . You are receiving this because you commented.Message ID: @.***>

arontsang avatar Nov 16 '23 05:11 arontsang

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.

james-cnz avatar Nov 16 '23 08:11 james-cnz