[Challenge] implementing the ambiguous operator in D

Nick Sabalausky a at a.a
Thu Sep 2 16:01:47 PDT 2010


"Nick Sabalausky" <a at a.a> wrote in message 
news:i5p68t$2pth$1 at digitalmars.com...
> "Philippe Sigaud" <philippe.sigaud at gmail.com> wrote in message 
> news:mailman.42.1283371696.858.digitalmars-d at puremagic.com...
>> Hey, this question on SO makes for a good challenge:
>>
>> http://stackoverflow.com/questions/3608834/is-it-possible-to-generically-implement-the-amb-operator-in-d
>>
>> The amb operator does this:
>>
>> amb([1, 2]) * amb([3, 4, 5]) == amb([3, 4, 5, 6, 8, 10])
>> amb(["hello", "world"]) ~ amb(["qwerty"]) == amb(["helloqwerty", 
>> "worldqwerty"])
>> amb(["hello", "world"]) ~ "qwerty" == amb(["helloqwerty", "worldqwerty"])
>> amb(["hello", "very long string"]).length = amb([5, 16])
>>
>
> Interesting thing, but devil's advocate: What would be the uses/benefits 
> of that versus just having "for each element" versions of the operators?
>
> Also, "ambiguous" seems like a rather odd name for what it does. I don't 
> see how ambiguity has anything to do with it. That disconnect made it 
> difficult at first for me to understand what it was. It's more like an 
> "elementwise" or something.
>

I've been starting at the Ruby page, and I think I'm starting to understand 
it a little more:

Suppose you have two "mostly" unknown values: x and y. Neither you, nor the 
computer at runtime, know what either of their values actually are. But, you 
*do* know a finite set of values that they *might* be:

auto x = amb([1, 2]); // x is either 1 or 2, but I don't know which
auto y = amb([3, 4, 5]); // y is either 3, 4 or 5, but I don't know which
assert(x != 2); // Not guaranteed to be == 2 at this point
assert(y != 4);

// x*y must be one of these values, but I don't know which, it could be any 
of them:
assert(x*y == amb([3, 4, 5, 6, 8, 10]);

// Here's the *real* magic (psuedo-syntax):
// For some bizarre reason, this means
// that x*y must be == 8 (despite the !=)
amb(if x*y != 8);

// Since x*y has been determined to be 8,
// the real values of x and y are now known and
// completely unambiguous:
assert(x == 2);
assert(y == 4);




More information about the Digitalmars-d mailing list