MiniKanren Racket multiplication for unary numbers

93 views Asked by At

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:

  1. (run 1 (x) (mulo '(s (s z)) '(s (s (s z))) x)) outputs: '((s (s (s (s (s (s z))))))), which is correct
  2. (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

2

There are 2 answers

0
William E. Byrd On

(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)))

Hint: what would happen if you ran unaryo on the numbers you are passing into mulo?

0
Will Ness On

You have a typo.

Instead of

(run 1 (x) (mulo '(s (s (s z))) x '(s (s (s (s (s (s (z)))))))))

it is supposed to be

(run 1 (x) (mulo '(s (s (s z))) x '(s (s (s (s (s (s  z ))))))))