Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a predicate in prolog

Tags:

prolog

I am new to Prolog and I have so far learned how to define a predicate in a file and the run the interpreter to use it. But I would like to know if there is a way to define the predicate at the ?- prompt so that I don't have to switch back and forth.

the way I am doing it now is like this

file defs.pl:

adjacent(1,2).
adjacent(1,3).

in the prolog interpreter:

?- consult('defs.pl').
% defs.pl compiled 0.00 sec, 122 bytes
true.
?- adjacent(1,2).
true.

EDIT maybe I meant how to define 'facts' I'm not sure.

like image 351
Nathan Avatar asked Dec 03 '09 23:12

Nathan


People also ask

What do you mean by predicate in Prolog?

Prolog predicate is the method to contain the argument and return the boolean values such as true or false. It is a function to operate and return given values, variables, or arguments using a prolog programming language.

How does Prolog try to satisfy a predicate?

Goals relating to user-defined predicates are evaluated by examining the database of rules and facts loaded by the user. Prolog attempts to satisfy a goal by matching it with the heads of clauses in the database, working from top to bottom.

Why not predicate is used in Prolog?

Prolog in Artificial Intelligence The not predicate is used to negate some statement, which means, when a statement is true, then not(statement) will be false, otherwise if the statement is false, then not(statement) will be true. If X and Y match, then different(X,Y) fails, Otherwise different(X,Y) succeeds.


1 Answers

You can use the assert/1 predicate:

?- assert(adjacent(1,4)).
true

EDIT: By the way, this will not work if you try to combine it with predicates defined in a file. So either define all adjacent/2 predicates in your file, are define them all with assert in the command line.

If you do want to define some of the predicates in the file, and others with assert, then declare in your file that the predicate is dynamic:

% file contents
:- dynamic(adjacent/2).
adjacent(1,2).
adjacent(1,3).
like image 116
catchmeifyoutry Avatar answered Sep 26 '22 07:09

catchmeifyoutry