I am a complete beginner in Mercury language, although I have learned Prolog before. One of the new aspects of Mercury is dererminism. The main function has to be deterministic. In order to make it so, I have to check if a variable is unified/bound to a value, but I cannot find how to do that. Particularly see the code:
main(!IO) :-
mother(X,"john"),
( if bound(X) <-----this is my failed try; how to check if X is unified?
then
io.format("%s\n", [s(X)], !IO)
else
io.write_string("Not available\n",!IO)
).
Such main could not fail, i.e. it would (I guess) satisfy the deterministic constraint. So the question is how to check if a variable is bound.
I've translated the family tree from a Prolog example found on this side for comparison. I have specified all facts (persons) and their relationship with each other and a few helper predicates. Note that this typed version did actually find the error that you see in the top answer:
female(jane).The main predicate does not have to be deterministic, it can also be
cc_multi, which means Mercury will commit (not try other) choices it has; you can verify this by replacingmotherwithparent.You also do not have to check for boundness of your variables, instead you just use any not deterministic term in the if clause, and on succeeding the then part will have guaranteed bound variables, or unbound in the else part.
If you want this example to be more dynamic, you will have to use the
lexerortermmodule to parse input into apersonatom.If you want all solutions you should check the
solutionmodule.