3

I'm using the Seven Languages In Seven Weeks Prolog tutorial and trying to run through some examples using the Android Jekejeke Runtime. For example, if I add

likes(wallace, grommit).

from the tutorial, I get.

Error: Undefined, private or package local predicate likes/2

I tried using assert, as described in How to create a fact in SWI-Prolog?, but then it says that assert is undefined, instead of likes.

Presumably I'm missing something basic about how the runtime works, or its dialect of prolog.it.

matt freake
  • 4,877
  • 4
  • 27
  • 56
  • 3
    Jekejeke is right. See [this answer](http://stackoverflow.com/a/20027252/772868) why. – false Feb 10 '15 at 00:02

2 Answers2

4

assert/1 is not a standard predicate, although several implementations provide it. That doesn't seem to be the case for Jekejeke Prolog. Use instead either the asserta/1 or the assertz/1 standard predicates. The first asserts a clause as the first for the predicate. The latter asserts a clause as the last for the predicate.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • And I need to use them to assert a fact? The Seven Languages tutorial has skipped over that so far, if you do – matt freake Feb 09 '15 at 10:15
  • Depends on exactly what you mean "to assert a fact". Predicate definitions are usually defined in a source file that you compile and load. The assert predicates are used when adding clauses for *dynamic* predicates at runtime. – Paulo Moura Feb 09 '15 at 10:18
0

This is a common error. Namely that there is a certain assumption that facts can be entered in the top level directly by typing it.

The interpreter issues an error since he understands what is input as a query and the predicate in the query is yet undefined.

But the end-user has multiple options:

1) First option use assertz/1 or asserta/1:
The top level is for executing goals. You need a goal that instructs interpreter to perform an assert. Use asserta/1 or assertz/1:

Top-level:

?- assertz(likes(foo, bar)).

Please not that predicates that have already been used as a static predicate, i.e. have been added by method 2) or 3), cannot be anymore asserted. Use the dynamic/1 directive then.

The built-in assert/1 is not supported since it is not part of the ISO core standard and usually redundant to assertz/1.

2) Second option use a file and consult it:
Place the facts and rules into a file. And consult it via the consult/1 built-in.

File baz.p:

likes(foo, bar).

Top-level:

?- consult('baz.p').

Instead of consult/1 you can also use ensure_loaded/1 or use_module/1.

3) Third option directly consult from console:
Enter the facts and rules directly in the top-level. Finish your entering of facts and rules by the end-of-file key stroke.

Top-level:

?- [user].
likes(foo, bar).
^D

Bye

  • See also https://de.wikipedia.org/wiki/Prolog_%28Programmiersprache%29#Laden_von_Prolog-Texten –  Oct 06 '15 at 13:02