简体   繁体   中英

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 (ie 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?

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 bar s from the database, and it will declare bar/1 as dynamic .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM