Search code examples
prologgnu-prolog

How to define a dummy/placeholder predicate in GNU Prolog


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?


Solution

  • 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.