bmi*_*are 17 clojure clojure-core.logic
我想要:
以下面的例子为例:
Red robotic birds are only composed of buttons, cheese, and wire.
Run Code Online (Sandbox Code Playgroud)
我想表达一类鸟类,红色和机器人的鸟类,有一个属性.这个属性是它们只由按钮,奶酪和电线组成.电线奶酪或按钮的类型没有限制.因此,应该可以推断出没有由纸构成的红色机器鸟.此外,这些鸟可以由项目按钮,奶酪和电线的子集组成.
在clojure/core.logic.prelude中,有使用defrel和的关系和事实fact.但是,我无法想出一个组合来解释这个事实.
dno*_*len 23
在Prolog中,与miniKanren一样,事实和目标之间没有区别.我将来可能会解决这个问题.
顺便说一句,我不确定这完全回答了你的问题 - 听听你希望运行什么类型的查询会很有帮助.
这是经过测试的代码(对于Clojure 1.3.0-beta1),因为我使用了^:index技巧,如果你将它交换为^ {:index true},这段代码在1.2.0中运行正常:
(ns clojure.core.logic.so
(:refer-clojure :exclude [==])
(:use [clojure.core.logic]))
(defrel property* ^:index p ^:index t)
(fact property* :bird :red-robotic-bird)
(fact property* :red :red-robotic-bird)
(fact property* :robotic :red-robotic-bird)
(fact property* :tasty :cake)
(fact property* :red-robotic-bird :macaw)
(defn property [p t]
(conde
[(property* p t)]
[(fresh [ps]
(property* ps t)
(property p ps))]))
(defrel composition* ^:index m ^:index t)
(fact composition* :buttons :red-robotic-bird)
(fact composition* :cheese :red-robotic-bird)
(fact composition* :wire :red-robotic-bird)
(fact composition* :flour :cake)
(defn composition [m t]
(fresh [p]
(composition* m p)
(conde
[(== p t)]
[(property p t)])))
Run Code Online (Sandbox Code Playgroud)
尝试一下.
(comment
;; what is a macaw composed of?
(run* [q] (composition q :macaw))
;; (:wire :cheese :buttons)
;; what things include buttons in their composition?
(run* [q] (composition :buttons q))
;; (:red-robotic-bird :macaw)
;; does a macaw include flour in its composition?
(run* [q] (composition :flour :macaw))
;; ()
;; is a macaw a bird?
(run* [q] (property :bird :macaw))
;; (_.0)
;; is a cake a bird?
(run* [q] (property :bird :cake))
;; ()
;; what are the properties of a macaw?
(run* [q] (property q :macaw))
;; (:red-robotic-bird :robotic :bird :red)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3169 次 |
| 最近记录: |