prologclojure-core.logicminikanren

Simple Prolog to Clojure core.logic


I've been playing with Prolog recently and getting my head around how to represent some tasks I want to do with it, which are largely about having a database of facts and doing simple queries on it, joining multiple facts together.

But I want to use this is in a context where I'm writing Clojure. And it seems like core.logic should do what I want.

But I'm naively finding it difficult to see how to put basic Prolog predicates into core.logic.

For example, how should I represent something as simple as this in core.logic :

person(phil).
person(mike).
food(cheese).
food(apple).
likes(phil,apple).
likes(phil,cheese).

And a query like

food(F),person(P),likes(P,F)

Most introductions I can find are heavy on the logic programming but not the data representation.


Solution

  • As Guy Coder said, the PLDB package under core.logic solves exactly this kind of problems:

    (db-rel person p)
    (db-rel food f)
    (db-rel likes p f)
    
    (def facts (db
      [person 'phil]
      [person 'mike]
      [food 'cheese]
      [food 'apple]
      [likes 'phil 'apple]
      [likes 'phil 'cheese]))
    
    (with-db facts (run* [p f] (food f) (person p) (likes p f)))
    
    => ([phil cheese] [phil apple])    p=phil,f=cheese   or   p=phil,f=apple