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
else
clause of the definition offlatteno
. Theelse
clause handles the case in whichs
is a symbol (the symbola
, here). However, the clause also allowss
to 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 theelse
clause accepting non-symbol values fors
.In later versions of miniKanren we have added special constraints such as
symbolo
and=/=
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
symbolo
constraint inflatteno
to ensures
is 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