Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expressing that a specific subset of X's have property Y in core.logic

I want to:

  1. Describe a fact about a subset of a class of objects.
  2. Declare that an object has a property that consists of other properties.

Take the following as an example:

Red robotic birds are only composed of buttons, cheese, and wire.

I want to express that a class of birds, birds that are red and robotic, have a property. This property is that they are composed only of buttons, cheese, and wire. There are no restrictions on the type of wire cheese or buttons. Also as a result, it should be deducible that there are no red robotic birds that are composed of paper. Also, these birds can be composed of a subset of the items buttons, cheese, and wire.

In clojure/core.logic.prelude, there are relations and facts using defrel and fact. However, I can't come up with a combination to explain this fact.

like image 716
bmillare Avatar asked Jul 15 '11 21:07

bmillare


1 Answers

In Prolog there is no distinction between facts and goals as there are in miniKanren. I might address this in the future.

BTW, I'm not sure that this completely answers your question - it would be helpful to hear what types of queries you wish to run.

This is tested code (for Clojure 1.3.0-beta1) since I'm using the ^:index trick, this code will run fine in 1.2.0 if you swap it for ^{:index true}:

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

Trying it out.

(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)
  )
like image 199
dnolen Avatar answered Oct 22 '22 10:10

dnolen