hhu-stups / prolog-jupyter-kernel

A Jupyter kernel for Prolog
MIT License
17 stars 3 forks source link

Facts do not work properly #17

Closed kazemihabib closed 11 months ago

kazemihabib commented 11 months ago

I'm learning prolog and I noticed something weird with this kernel considering facts: If I put more than one facts in a cell it works properly:

rains.
hats.

outputs:

% Asserting clauses for user:rains/0
% Asserting clauses for user:hats/0

but if I try only one fact:

sunny.

it outputs:

ERROR: call/1: Unknown procedure: sunny/0

but the following works:

sunny :- true.

% Asserting clauses for user:sunny/0
dgelessus commented 11 months ago

This is a limitation of the Prolog Jupyter kernel unfortunately. When you write something like sunny. in a cell, the kernel doesn't know if you want to define a new fact sunny, or if you want to run a query that checks whether sunny is true. In these ambiguous cases, the kernel assumes that it's meant as a query, because that's the more common case. As you already found out, you can work around this by adding :- true or putting multiple facts in the same cell.

In a normal Prolog REPL, this ambiguity doesn't exist - there, all inputs are queries, unless you explicitly run consult(user). to say that you are now inputting facts/clauses. Unfortunately, because of how Jupyter works, we cannot have multiple "input modes" like this in the Prolog Jupyter kernel, so we had to use this imperfect solution, where defining a single fact doesn't work as expected.

pmoura commented 11 months ago

This is one of the issues that I solved differently in the Logtalk kernel (which was forked from this Prolog kernel): code cell contents is always interpreted as a query (or multiple queries) unless the code cell start with a cell magic telling that the contents that follow (directives and clauses) are to be added to the database.