core.logic unification matches value in map but not key

258 views Asked by At

I don't understand why from the following two examples, the first one returns the correct value (1), while the second returns (). I'd have expected the result (:key) or an exception that it can't find the solution, but not an empty list.

(l/run* [q]
  (l/== {:key 1} {:key q}))

;; BUT IT DOESNT WORK

(l/run* [q]
  (l/== {:key 1} {q 1}))
1

There are 1 answers

2
Beyamor On BEST ANSWER

I haven't found it documented as intended behaviour or not, but looking at the source code, from my (limited) understanding, it looks like unification of maps only happens with values, not keys.

As implemented in unify-with-map*, when core.logic finds the key :key in the first map, it looks up its value in second, doesn't find it, and so bails on the unification. Since there are no solutions, like @Arthur Ulfeldt said, you end up with ().