I am doing an exercise to write some logic code in a few different idioms... Prolog, miniKanren, ASP etc... to get a feel for each.
I have a question about something really basic:
In Prolog you can define:
human(bob).
then in the REPL you can check the truth of this fact and a non-fact:
?- human(bob).
true.
?- human(e_t).
false.
In the python miniKanren, LogPy, you can similarly define the fact:
from kanren import fact
fact(human, 'bob')
I cannot find a recipe for simply checking the truth of "bob is human".
The run
function seems to require a variable as the second arg, but I am an not trying to find the value of a variable.
All the kanren examples I found start by showing how to query a relation, like:
from kanren import Relation, fact, run, var
human = Relation()
fact(human, 'bob')
fact(human, 'jim')
x = var('x')
run(0, x, human(x))
# output:
('jim', 'bob')
That's more useful, but what I want to do is even simpler than that (just for the sake of comparing the same baby steps in Prolog).
So... while writing out this question I played around a bit more and found a recipe for what I'm trying to do.
This works in the LogPy miniKanren, I would love to know if a similar usage is applicable in other implementations such as Clojure core.logic etc (or if there's another way to do it!).
from kanren import Relation, fact, run, var
human = Relation()
fact(human, 'bob')
run(0, True, human('bob'))
>>> (True,)
run(0, True, human('E.T.'))
>>> ()
We can use anything in place of the True
arg: if the goal arg evaluates true then you will get your value back in the result tuple, otherwise an empty result.