将两个向量限制在同一个域中但不是彼此的成员

Ada*_*ard 5 clojure clojure-core.logic

我已经使用 clojure 一段时间了,但刚开始使用 core.logic。

给定一个域,比如1 2 3 4我想得到一个包含两个向量的向量,比如([[1 2] [3 4]]).

注意:这只是我真正想做的事情的简化版本。:) 见:https : //github.com/adamhoward/lineup

我在网上找到了非会员的这个定义:

(defne not-membero [x l]
  ([_ []])
  ([_ [?y . ?r]]
    (!= x ?y)
    (not-membero x ?r)))
Run Code Online (Sandbox Code Playgroud)

我正在尝试像这样使用它:

(run 1 [q]
     (fresh [w x
             y z]
            (== q [[w x]
                   [y z]])
            (infd w x y z (domain 1 2 3 4))
            (everyg distinctfd [[w x] [y z]])
            (everyg #(not-membero % [y z]) [w x])))
Run Code Online (Sandbox Code Playgroud)

在 Emacs 中运行它会给我一条Evaluation aborted.消息。

当我尝试退出时memberonot-membero我又回来了([[1 2] [1 2]]),这对我来说很有意义。第一个向量中的每个元素[w x]都是第二个向量的成员[y z]

但是,当我打电话时,run 2我回来了([[1 2] [1 2]] [[1 2] [1 3]])。我不明白[[1 2] [1 3]]上面的规则怎么可能是正确的。我没有everyg正确理解吗?任何指导(包括 rtfmanual、rtfbook、rtfdissertation)将不胜感激。

谢谢。

编辑:可能已经解决了这个问题。

仍然不确定奇怪的结果,memberonot-membero我发现我可以做到这一点而不是目标:

(everyg #(distinctfd (conj [y z] %)) [w x])
Run Code Online (Sandbox Code Playgroud)

[w x]conj'd to 的每个元素都[y z]包含所有不同的值。这可能比非会员效率低,所以我仍然愿意接受任何帮助。

Pep*_*ijn 1

您的示例似乎与此相同:

(run* [q]
  (fresh [w x y z]
    (fd/in w x y z (fd/domain 1 2 3 4))
    (== q [[w x] [y z]])
    (fd/distinct [w x y z])))
Run Code Online (Sandbox Code Playgroud)

但看到足球问题......

(ns test
  (:refer-clojure :exclude [==])
  (:use clojure.core.logic)
  (require [clojure.core.logic.fd :as fd]))

(defn game [g] (fresh [a b c d e] (== g [a b c d e])))

(def players (range 1 11))

(defne not-membero [x l]
  ([_ []])
  ([_ [?y . ?r]]
    (!= x ?y)
    (not-membero x ?r)))

(defne plays [player games]
  ([_ []])
  ([_ [?f ?s . ?r]]
   (membero player ?f) ; if you play this game
   (not-membero player ?s) ; do not play next game
   (plays player ?r))
  ([_ [?f ?s . ?r]]
   (membero player ?s)
   (not-membero player ?f)
   (plays player ?r)))

(defne goalies [games]
  ([[[_ _ _ _ ?a]
     [_ _ _ _ ?b]
     [_ _ _ _ ?c]
     [_ _ _ _ ?d]]]
    (fd/in ?a ?b ?c ?d (apply fd/domain players))
    (not-membero 1 [?a ?b ?c ?d]) ; star player != goalie
    (fd/distinct [?a ?b ?c ?d]))) ; not goalie twice

(defn -main [& args]
  (run 1 [q]
    (fresh [a b c d]
      (game a)
      (game b)
      (game c)
      (game d)
      (== q [a b c d])
      (goalies q)
      (everyg #(plays % q) players))))
Run Code Online (Sandbox Code Playgroud)