I am trying to implement multiplication for unary numbers in MiniKanren. So I have written the following code:
#lang iracket/lang
(require minikanren)
(require minikanren/matche)
(defrel (unaryo n) (conde
[(== 'z n)]
[(fresh (m)
(== `(s ,m) n) (unaryo m))]))
(defrel (addo a b c)
(conde
[(== 'z a) (== b c)]
[(fresh (d e)
(== `(s ,d) a)
(== `(s ,e) c)
(addo d b e))]))
(defrel (mulo x y z)
(conde
[(== x 'z) (== z 'z)]
[(fresh (x1 z1)
(== `(s ,x1) x)
(addo y z1 z)
(mulo x1 y z1))]))
(run 1 (x) (mulo '(s (s z)) '(s (s (s z))) x))
Then I tried to execute some tests:
(run 1 (x) (mulo '(s (s z)) '(s (s (s z))) x))
outputs: '((s (s (s (s (s (s z))))))), which is correct(run 1 (x) (mulo '(s (s (s z))) x '(s (s (s (s (s (s (z)))))))))
, but this test produces '(), which is wrong, the result should be '((s (s z)))
Could you find place where I am wrong? Thanks in advance!
Tried to test addo
it produces right results
Hint: what would happen if you ran
unaryo
on the numbers you are passing intomulo
?