Reputation: 63
I am trying to understand why the following product-pluso
function is returning unexpected results. Did I find a bug or am I misunderstanding how this works? I am using core.logic with CLP/FD. I am still learning, so I could just be doing it wrong. The function should take two factors, a number, and a sum. The sum should be the product of the factors plus the number. It works great, unless both of the factors are fresh. Then I get strange results. This is happening with core.logic v0.8.2.
(ns strang-result
(:refer-clojure :exclude [==])
(:use
clojure.test
[clojure.core.logic :exclude [is]])
(:require
[clojure.core.logic.fd :as fd]))
(defn product-pluso [factor1 factor2 number sum]
(fd/eq (= sum (+ number (* factor1 factor2)))))
(run* [x y]
(fd/in x y (fd/interval 1 38))
(product-pluso x y 2 40))
;=> ([1 38] [2 19] [3 13] [4 10] [5 8] [6 7] [7 6] [8 5] [9 5] [10 4] [11 4]
; [12 4] [13 3] [14 3] [15 3] [16 3] [17 3] [18 3] [19 2] [38 1])
Upvotes: 3
Views: 286
Reputation: 18556
This just appears to be a bug. Something strange is happening with fd/+
constraint that is not being checked. I'm one of the lead developers, I've filed a ticket for this:
http://dev.clojure.org/jira/browse/LOGIC-126
UPDATE: Please try 0.8.3, the issue has been resolved there and you should see only 4 results. Thanks for the report.
Upvotes: 2
Reputation: 33637
You can solve the issue using project
, without project
it seems to somehow ORing the constrains rather then ANDing them.
(defn product-pluso [factor1 factor2 number sum]
(fresh [product]
(fd/+ product number sum)
(project [product]
(fd/* factor1 factor2 product))))
Upvotes: 0