Recursive definition in core.logic

281 views
Skip to first unread message

Casper

unread,
Sep 21, 2014, 11:07:58 AM9/21/14
to clo...@googlegroups.com
I have been looking through core.logic tutorials and while I "get it" I haven't had the big epiphany yet. One thing that keeps nagging me is how to make a relation that isn't "fixed".

An example is https://github.com/swannodette/logic-tutorial in which there is defined some relations such as parent, child, son, daughter and granddaughter. As I see it these are all a fixed relationships in that they are a fixed distance from each other.

For me that leads to the question, how do we then define the relationship 'descendant' (which would be the generalisation of child, grandchild etc)? 

Seems to me that this would involve a recursive definition, but I don't know how to make one like that. Any hints?

This is the relevant code from the excellent tutorial made by swannodette, that I linked above:

(ns logic-tutorial.tut1
  (:refer-clojure :exclude [==])
  (:use [clojure.core.logic])
  (:use [clojure.core.logic.pldb]))

(def rels
         (db
           [male 'Bob]
           [female 'Cindy]
           [parent 'John 'Cindy]
           [parent 'Jane 'Cindy]
           [parent 'John 'Bob]
           [parent 'Jane 'Bob]))

(defn child [x y]
  (parent y x))

(defn son [x y]
  (all
   (child x y)
   (male x)))

(defn daughter [x y]
  (all
   (child x y)
   (female x)))

(defn grandparent [x y]
  (fresh [z]
    (parent x z)
    (parent z y)))

(defn granddaughter [x y]
  (fresh [z]
    (daughter x z)
    (child z y)))

;; Running it

(with-db rels
    (run* [q]
          (child 'Bob q)))

François Rey

unread,
Sep 21, 2014, 12:29:41 PM9/21/14
to clo...@googlegroups.com
I think this blog post should help:
https://kotka.de/blog/2011/10/A_field_trip_into_logic_programming.html
The site seems to have an invalid certificate so you may or may not want to proceed, but I just did and all is fine.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tassilo Horn

unread,
Sep 21, 2014, 2:40:51 PM9/21/14
to Casper, clo...@googlegroups.com
Casper <cas...@gmail.com> writes:

> For me that leads to the question, how do we then define the relationship
> 'descendant' (which would be the generalisation of child, grandchild etc)?
>
> (defn child [x y]
> (parent y x))

Ok, so you have a `child' relation already, so this should be easy (but
it's not tested):

(defn descendant [d a]
(conde
[(child d a)]
[(fresh [p]
(child d p)
(descendant p a))]))

So d is a descendant of a if

- d is a child of a, or
- there is some p who is the parent of d and a descendant of a.

I think instead of `conde' you can use `conda' here, because when the
first clause succeeds the second one cannot succeed and doesn't need to
be tested.

Bye,
Tassilo

Joshua Ballanco

unread,
Sep 22, 2014, 3:10:07 AM9/22/14
to clo...@googlegroups.com, Casper
On Sunday, September 21, 2014 at 21:40, Tassilo Horn wrote:
> I think instead of `conde' you can use `conda' here, because when the
> first clause succeeds the second one cannot succeed and doesn't need to
>


Careful with the use of `conde` vs `conda`, as `conda` is an early cut. In other words, if you substituted `conda` in Tassilo’s solution, and you had multiple generations, then `descendants` would only find the first generation of descendants. If you wanted to find *all* descendants, then you need `conde`.
Reply all
Reply to author
Forward
0 new messages