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.

naeg
  • 3,944
  • 3
  • 24
  • 29

1 Answers1

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.

naeg
  • 3,944
  • 3
  • 24
  • 29