[Challenge] implementing the ambiguous operator in D

Simen kjaeraas simen.kjaras at gmail.com
Fri Sep 3 16:11:04 PDT 2010


Philippe Sigaud <philippe.sigaud at gmail.com> wrote:

> Now, the 'real'/intriguing/mind-bending amb operator (from the 60's) does
> like the Haskell implementation linked in SO does: backtracking on the
> results to avoid some condition. If someone is up to the challenge of
> implementing it in D, great! Maybe with closures? I never really thought
> about it.
>
> I guess the D syntax would be
> auto x = amb([1,2,3]);
> auto y =amb([4,5,6]);
> x*y == 8; // forces desambiguation => the ambs explore the possibilities.

I believe this will only work with arrays as input. Either that, or I need
a way to make this work:

struct Foo( R ) if ( isForwardRange!R ) {
     bool delegate( ElementType!R ) bar;
     Filter!( bar, R ) range;
}

Or, well, something like it. I need a static type for a Filter that
delegates to a struct member, in this case bar.


> assert(x == amb([2]));
> assert(y == amb([4]));
>
> There is only one value left, no more ambiguity. Maybe in this case an
> 'alias possibilities[0] this' could do the trick:
>
> assert(x == 2);
> assert(y == 4);
>
> Can we do alias someArray[0] this; ?

For a simple assert like that, overloading opEquals ought to do the
trick, no?


-- 
Simen


More information about the Digitalmars-d mailing list