3 ms·
> it is possible to write an amb macro that "chooses" the right argument to make a subsequent assert statement be true. I'm not too familiar with Scheme's macr
by quelltext 6y ago
> it is possible to write an amb macro that "chooses" the right argument to make a subsequent assert statement be true.
I'm not too familiar with Scheme's macro feature but how would one go about that? I was under the impression with macros you get to define what ever syntax is part if the call, but here in order to achieve what the author mentions the macro would need to be able to look at surrounding code, to look ahead for the assert. How is this possible? Is he maybe talking of a compiler plugin or a macro (not shown) that encloses the entire expression, e.g. `(make-assert-true-macro (let ...{the code snippet}... ))`?
- Xophmeister 6y agoI'm just thinking out aloud, so I'm probably -- if not completely wrong -- at best sketchy on details! That being said, `amb` could be a function/macro that uses a continuation in such a way that, when a failure is encountered (however that may be defined), it can backtrace and try with the next value. By using continuations with some encapsulation of the call stack, it doesn't need to envelope the surrounding code and can just "rewind".
- rscho 6y agoSicp, the ultimate. https://mitpress.mit.edu/sites/default/files/sicp/full-text/sicp/book/node91.html https://mitpress.mit.edu/sites/default/files/sicp/full-text/...
- ekidd 6y agoYou can implement 'amb' using either continuations or monads. Back in 2005, I wrote a very short Ruby version of 'amb' using continuations: http://www.randomhacks.net/2005/10/11/amb-operator/ http://www.randomhacks.net/2005/10/11/amb-operator/ This approach works if your code contains no side effects, because it involves backtracking and restarting the computation as needed. But continuations are a heavyweight feature that place significant constraints on language implementation, so few languages support them. You can also implement 'amb' using monads: http://www.randomhacks.net/2007/03/12/monads-in-15-minutes/ http://www.randomhacks.net/2007/03/12/monads-in-15-minutes/ (Yes, this is yet another monad tutorial. Sorry.) This benefits from a "do notation" syntax, which could be implemented using macros.
- nromiun 6y agoIt is possible to implement it in a lot of languages. https://www.rosettacode.org/wiki/Amb https://www.rosettacode.org/wiki/Amb