Peteris
Peteris

Reputation: 3756

Why run* does not work with natural-number in core.logic?

I'm playing around with the Peano arithmetic application of core.logic at GitHub. When I try

(run* [q] (natural-number one))

Clojure spits out an error, however,

(run 1 [q] (natural-number one))

succeeds with (_.0) as intended. Is there some implementation detail here?

Edit:

I'm running the following code:

(ns myns.core
  (:refer-clojure :exclude [==])
  (:use [clojure.core.logic]))

(defn s [n]
  "Returns n's succeeding natural number"
  (llist n []))

(def zero 0)
(def one   (s zero))
(def two   (s one))
(def three (s two))
(def four  (s three))
(def five  (s four))
(def six   (s five))

(defn natural-number [x]
  "A relation where x is a natural number"
  (matche [x]
          ([zero])
          ([(s ?x)] (natural-number ?x))))

The stack trace is:

clojure.core.logic.LVar cannot be cast to clojure.lang.IFn
  [Thrown class java.lang.ClassCastException]

0: myns.core$natural_number$fn__177103$_inc__177104$fn__177113$fn__177114$_inc__177115$fn__177116.invoke(core.clj:194)
1: clojure.core.logic.Substitutions.bind(logic.clj:208)
2: myns.core$natural_number$fn__177103$_inc__177104$fn__177113$fn__177114$_inc__177115.invoke(core.clj:192)
3: clojure.core.logic$eval175682$fn__175691$_inc__175692.invoke(logic.clj:808)
4: clojure.core.logic$eval175682$fn__175683$fn__175684.invoke(logic.clj:813)
5: clojure.lang.LazySeq.sval(LazySeq.java:42)
6: clojure.lang.LazySeq.seq(LazySeq.java:67)
7: clojure.lang.Cons.next(Cons.java:39)
8: clojure.lang.LazySeq.next(LazySeq.java:92)
9: clojure.lang.RT.next(RT.java:580)

project.clj

(defproject myns "1.0.0-SNAPSHOT"
   :description "FIXME: write description"
   :dependencies [[org.clojure/clojure "1.3.0"]
                  [org.clojure/core.logic "0.6.7"]])

Upvotes: 3

Views: 546

Answers (1)

Ambrose
Ambrose

Reputation: 1220

This looks like a subtle regression between core.logic 0.6.5 and 0.6.7. It requires more investigation to get to the root of the issue.

In the meantime I have rewritten the successor function to use conso instead of llist.

I think it is more idiomatic minikanren/core.logic code.

s now takes 2 arguments: the number, and the next number.

(run* [q] (s zero one))
;=> (_.0)

The revised code fixes your issue.

Upvotes: 2

Related Questions