core-logic

Nico 2020-01-27T20:20:07.001600Z

hi, I'm having some trouble with core.logic and function arities. I'm going through the reasoned schemer to learn and I've been trying to write some of my own relations as exercises. I wrote a simple implementation of concat which worked fine, and I tried to convert it to a relation:

(defn dumb-concat [x y]
  (cond
    (empty? y) x
    (empty? x) y
    :else (recur (conj x (first y)) (rest y))))

(defn dumb-concato [x y out]
  (l/conde
    [(l/emptyo y) (l/== out x)]
    [(l/emptyo x) (l/== out y)]
    [l/succeed (l/fresh [f r c]
                   (l/conso f r y)
                   (l/conjo x f c)
                   (dumb-concato c r)
                   )]))

Nico 2020-01-27T20:21:23.002400Z

however, when I try and test this with (l/run 1 [q] (dumb-concato [0 1 2 3] [5 4 3] q)) it just says Wrong number of args (2) passed to: logic-practice.core/dumb-concato

Nico 2020-01-27T20:21:29.002700Z

what am I doing wrong? Or is this a bug?

Nico 2020-01-27T20:30:17.003100Z

because I'm certainly passing 3 arguments to it (the two vectors and the lvar)

jimmy 2020-01-27T20:33:09.003500Z

(dumb-concato c r)

jimmy 2020-01-27T20:33:55.003900Z

^ That line looks like the error to me.

2020-01-27T20:47:54.004900Z

Dum-concato is just a regular function, not a core.logic relation, so you can't use it like that

2020-01-27T20:48:12.005100Z

Err

2020-01-27T20:48:22.005400Z

Dumb-concat

2020-01-27T20:51:07.009Z

What exactly a core.logic relation is kind of complicated, but a recursive definition would be something like, a core.logic relation is a function whose body is composed of core.logic relations

2020-01-27T20:52:53.012Z

So dumb-concato would be a relation if it didn't call dumb-concat, but the body of dumb-concat is all non-relations (it is all functions from clojure.core)

2020-01-27T20:54:53.013900Z

The kind of problem you get with calling clojure.core functions in a logic program is they don't know how to deal with logic variables

1☝️
2020-01-27T20:55:30.014800Z

And relations don't return values, but functions do

2020-01-27T20:56:11.015300Z

Oh

2020-01-27T20:56:23.015700Z

I misread your code though

2020-01-27T20:57:27.017600Z

What I read as a call to dumb-concat is actually a call to dumb-concato with the wrong number if arguments which is exactly what the error you got says

1🙂
Nico 2020-01-27T21:41:01.018100Z

...how did I miss the obvious mistake

Nico 2020-01-27T21:41:03.018300Z

thanks guys

Nico 2020-01-27T21:41:08.018500Z

somehow managed to make that mistake almost every time