1

I have a Prolog file with the following structure:

% LIBRARY SECTION %

foo(X) :- bar(X);
          baz(X).

% USER DATA SECTION %

% e.g. bar(charlie).

The user data of the file is intended to be allowed to extended by the user but by default contains nothing. However this causes the query foo(X). to fails because bar/1 and baz/1 are not defined.

I have tried defining them with placeholder values (i.e. bar(none).) but then GNU Prolog complains about discontiguous predicates when user data is added to the bottom of the file.

Is there another way to define a dummy/placeholder version of bar/1 and baz/1 so that foo(X). does not fail and so that other lines containing bar and baz can be added to the bottom of the file?

DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • The `foo` predicate provides answered based on user information (lines of `bar`). Without any user information (no lines of `bar`) typing (`foo(X)`) currently throws an error. I'd like a placeholder so that it can not fail in this case. – DanielGibbs Jul 17 '15 at 07:33
  • I've updated the question in what is hopefully a more understandable way. – DanielGibbs Jul 17 '15 at 07:41

1 Answers1

3

If I understand the question, you want to have something along the lines of:

ask_bar :-
    % get user input
    assertz(bar(Input)).

foo(X) :-
    bar(X).

If this is indeed the problem, you have two options:

First one: Declare bar/1 as a dynamic predicate:

:- dynamic(bar/1).

(this is a directive, you just type the :- at the beginning of the line.)

Second one: in your program, before any reference to bar/1, call the predicate retractall/1, like this:

main :-
    retractall(bar(_)),
    %....

This will delete all bars from the database, and it will declare bar/1 as dynamic.

  • `dynamic/1` was exactly what I was looking for; thanks! – DanielGibbs Jul 17 '15 at 07:46
  • @DanielGibbs `retractall` can be useful, too, although even if you do use it it is probably cleaner to have `dynamic` there, it makes your intentions more explicit. –  Jul 17 '15 at 07:59