3

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])
1
  • 1
    Just to clarify, each [x y] pair should multiply to 38 (38 + 2 = 40), but many do not. Commented Mar 28, 2013 at 3:07

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@SteveSloan it's a good catch and I'm pretty surprised it hasn't shown up before. Will try to look into it over the weekend and push out 0.8.3 with the fix.
0

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)))) 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.