28

I'd like to write a predicate, descendo, which declares that the first given coordinate [y, x] is descending to the second given coordinate (imagine the board with [0, 0] at the left upper corner).

A very simple implementation in Prolog could look like this:

descending(B, A) :-
  B = [B1,B2],
  A = [A1,A2],
  B1 is A1 + 1,
  B2 is A2 + 1.

I fail to implement this in core.logic though. I've tried a lot of different things already (==/=fd/conso/appendo and +fd/+). One of the things I tried:

(defn descendo
  [b a]
  (l/fresh [b1 b2 a1 a2]
           (l/== b [b1 b2])
           (l/== a [a1 a2])
           (l/+fd b1 1 a1)
           (l/+fd b2 1 a2)))

Most of them just return nothing when running them like this:

(l/run* [q]
  (l/fresh [a]
    (l/infd a (l/domain [0 0] [1 0] [0 1] [1 1]))
    (descendo a [0 0])
    (l/== q a)))

=> () ; expected output: ([1 1])

I have the feeling that thinking too much in Prolog is not good when using core.logic...any hint appreciated. Thanks in advance.

EDIT: Found a workaround, where descendo stays the same, but when running it we don't use a domain:

(l/run* [q]
  (l/fresh [a]
    (l/membero a [[0 0] [1 0] [0 1] [1 1]])
    (l/membero q [[0 0] [1 0] [0 1] [1 1]])
    (descendo a q)))

=> ([1 1])

I'm not sure whether domain is meant to be used on vectors anyway, so this might not be a workaround but the actual solution.

2
  • 1
    FD in core.logic are for natural numbers only and not for vectors etc
    – Ankur
    Commented Nov 6, 2012 at 13:53
  • 7
    please post your solution as a solution :-) Commented Feb 7, 2013 at 15:56

1 Answer 1

0

Found a workaround, where descendo stays the same, but when running it we don't use a domain:

(l/run* [q]
  (l/fresh [a]
    (l/membero a [[0 0] [1 0] [0 1] [1 1]])
    (l/membero q [[0 0] [1 0] [0 1] [1 1]])
    (descendo a q)))

=> ([1 1])

I'm not sure whether domain is meant to be used on vectors anyway, so this might not be a workaround but the actual solution.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.