1

I am interested in expressing the number of occurrences of an element in a list as a relational goal in Clojure's core.logic library.

(Note that this question is not a duplicate of clojure core.logic counting elements in a set because that question was accepting non-relational goals as an answer.)

In particular, I am adapting the following Prolog code to core.logic.

count(_, [], 0).
count(X, [X | T], N) :-
  !, count(X, T, N1),
  N is N1 + 1.
count(X, [_ | T], N) :-
  count(X, T, N).

Here is my attempt

(defn counto [item coll n]
  (l/conde
   ;; If the coll is empty, the count must be 0.
   [(l/emptyo coll)
    (l/== n 0)]
   ;; If the coll head matches the item, increment count and recur.
   [(l/fresh [tail n*]
      (l/conso item tail coll)
      (counto item tail n*)
      (fd/+ n* 1 n))]
   ;; If the coll head does not match the item, retain count and recur.
   [(l/fresh [head tail]
      (l/conso head tail coll)
      (l/!= head item)
      (counto item tail n))]))

This seems to work well for generating a tiny amount of inputs. e.g.

(l/run 3 [q]
  (l/fresh [x y z]
    (l/== q [x y z])
    (fd/in x y z (fd/interval 0 1))
    (counto 0 q 2)))

;; => ((1 0 0) (0 1 0) (0 0 1))

However, this code runs into an infinite loop when requesting for additional solutions. e.g. replacing 3 with 4 above, or even replacing l/run with l/run*, results in an infinite loop.

Is there a mistake in my implementation? Or am I hitting a limitation of core.logic?

3
  • The second code block teminates without a problem when running with l/run* instead of l/run. Could you provide exact and trided code example with your question?
    – erdos
    Nov 20 at 17:27
  • run 4 and run* also work for me both on clojure 1.8 and core.logic 0.8.11 and clojure 1.11.1 and core.logic 1.0.1 Nov 22 at 12:45
  • I am using Clojure 1.11.1 with the deps {org.clojure/core.logic {:mvn/version "RELEASE"}}. For some reason, I am unable to reproduce the original problem. May have been the results of a stale REPL image. If possible, please close this question due to the inability to reproduce the issue.
    – oper0
    Nov 23 at 1:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.