I'm reading the Reasoned Schemer.
I have some intuition about how conde
works.
However, I can't find a formal definition of what conde
/conda
/condu
/condi
do.
I'm aware of https://www.cs.indiana.edu/~webyrd/ but that seems to have examples rather than definitions.
Is there a formal definition of conde
, conda
, condi
, condu
somewhere?
The Reasoned Schemer covers conda (soft cut) and condu (committed choice). You'll also find explanations of their behavior in William Byrd's excellent dissertation on miniKanren. You've tagged this post as being about core.logic. To be clear core.logic is based on a more recent version of miniKanren than the one presented in The Reasoned Schemer. miniKanren is always interleaves disjunctive goals - condi and the interleaving variants no longer exist. conde is condi now.
By Example, using core.logic:
conde will run every group, succeed if at least one group succeeds, and return all results from all successful groups.
conda and condu: both will stop after the first successful group(top to bottom)
conda returns all results from only the first successful group.
condu returns only one result from only the first successful group.
No idea what condi does though.
In Prolog's terms,
condA
is "soft cut",*->
, andcondU
is "committed choice" – a combination ofonce
and a soft cut, so that(once(A) *-> B ; false)
expresses the cut in(A, !, B)
:In
condA
, if the goalA
succeeds, all the solutions are passed through to the first clauseB
and no alternative clausesC
are tried.once/1
allows its argument goal to succeed only once (keeps only one solution, if any).condE
is a simple disjunction, andcondI
is a disjunction which alternates between the solutions of its constituents.Here's an attempt at faithfully translating the book's code, w/out logical variables and unification, into 18 lines of Haskell (where juxtaposition is curried function application, and
:
means cons). See if this clarifies things:mplus
" of the book):mplusI
"):bind
"):bindI
"):OR
" goal combination ("condE
"):OR
" goal combination ("condI
"):AND
" goal combination ("all
"):AND
" goal combination ("allI
" of the book):Goals produce streams (possibly empty) of (possibly updated) solutions, given a (possibly partial) solution to a problem.
Re-write rules for
all
are:Re-write rules for
condX
are:To arrive at the final
condE
andcondI
's translation, there's no need to implement the book'sifE
andifI
, since they reduce further to simple operator combinations, with all the operators considered to be right-associative:So there's no need for any special "syntax" in Haskell, plain operators suffice. Any combination can be used, with
&&/
instead of&&:
if needed. But OTOHcondI
could also be implemented as a function to accept a collection (list, tree etc.) of goals to be fulfilled, that would use some smart strategy to pick of them one most likely or most needed etc, and not just simple binary alternation as in||/
operator (orifI
of the book).Next, the book's
condA
can be modeled by two new operators,~~>
and||~
, working together. We can use them in a natural way as in e.g.which can intuitively be read as "
IF g1 THEN g2 AND ... OR-ELSE IF h1 THEN ... OR-ELSE gelse
".IF-THEN
" goal combination is to produce a "try" goal which must be called with a failure-continuation goal:OR-ELSE
" goal combination of a "try" goal and a simple goal simply calls its "try" goal with a second, on-failure goal, so it's nothing more than a convenience syntax for automatic grouping of operands:If the
||~
"OR-ELSE
" operator is given less binding power than the~~>
"IF-THEN
" operator and made right-associative too, and~~>
operator has still less binding power than&&:
and the like, sensible grouping of the above example is automatically produced asLast goal in an
||~
chain must thus be a simple goal. That's no limitation really, since last clause ofcondA
form is equivalent anyway to simple "AND
"-combination of its goals (or simplefalse
can be used just as well).That's all. We can even have more types of try-goals, represented by different kinds of "
IF
" operators, if we want:condAI
, if there were one in the book):condU
:So that, finally, the re-write rules for
condA
andcondU
of the book are simply: