Is it possible to insert a Clojure statement in the middle of a series of core.logic calls, "à la Prolog"?

137 views Asked by At

Prolog mixes logic (fallible) goals, and (infallible) processes like write/2 or assert/1. Is it possible to do so with Clojure's core.logic?

For example:

(pldb/with-db myFacts
   (l/run* [x y] 
       (person x)
       (println "we found a person!")
       (likes y x)
   )
)

It looks like this can't work because (println ...) is a Clojure function embedded in a core.logic run (which is supposed to contain only conditions/constraints I'm guessing).

More interestingly, I would like to be able to assert new facts in the midst of checking logical conditions like in Prolog:

liked(x,y):-
    person(x),
    assertz(likable(x)),
    likes(y,x).

Which would approximatively look like this in core.logic I'm guessing:

(pldb/with-db myFacts
   (l/run* [x y] 
       (person x)
       (def newFacts (-> newFacts (pldb/db-fact likeable x)))
       (likes y x)
   )
)

Those examples are silly but, in general, the question is about inserting procedural commands in the midst of constraint checking in core.logic like it is done in Prolog.

One concrete application of that, for instance, would be the capability to collect arcs in the recursive traversal of a graph by using assertion. But, unless I am mistaken, that's only possible if we have the possible to mix assertion with logical conditions.

Thank you very much for your help and guidance in this.

UPDATE: I think I might have figured it out: including a Clojure statement is fine but it must return true ; unlike in Prolog, those functions don't automatically "succeed". So all we need to add is: (l/== (println "hello") true)) Does it sound correct? FYI, this post was really helpful: https://ask.clojure.org/index.php/9546/can-core-logic-ask-questions

0

There are 0 answers