I am currently learning miniKanren by learning The Reasoned Schemer.
And I am stuck in an exercise in Chapter 5 frame 62: (run* (x) (flatten_o (a) x)), why there are three lists in output?
I am currently learning miniKanren by learning The Reasoned Schemer.
And I am stuck in an exercise in Chapter 5 frame 62: (run* (x) (flatten_o (a) x)), why there are three lists in output?
Good question! Where do these extra lists come from???
The problem is in the
elseclause of the definition offlatteno. Theelseclause handles the case in whichsis a symbol (the symbola, here). However, the clause also allowssto be the empty list or a pair! This is why we see three lists instead of one---the extra two lists are produced by recursive calls that succeed due to theelseclause accepting non-symbol values fors.In later versions of miniKanren we have added special constraints such as
symboloand=/=to prevent such behavior. For example, here is the same query, andflatteno, written in faster-miniKanren (https://github.com/webyrd/faster-miniKanren):Note the use of the
symboloconstraint inflattenoto ensuresis a symbol.You can find a non-"Little Book" explanation of these constraints in this paper:
http://webyrd.net/quines/quines.pdf
We are trying to figure out how to include a description of these constraints in a Little Book format. The implementation of the constraints is a bit involved, which makes it hard to fit in a Little Book!
Hope this helps!
Cheers,
--Will