How to define a predicate in a prolog

I am new to Prolog, and so far I have learned how to define a predicate in a file and run an interpreter to use it. But I would like to know if there is a way to define a predicate in an invitation? - so I don’t need to switch back and forth.

the way i do it now looks like this

defs.pl file:

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

in the prologue 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.

+5
source share
2 answers

You can use the predicate assert/1:

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

EDIT: , , , . /2 , assert .

assert, , :

% file contents
:- dynamic(adjacent/2).
adjacent(1,2).
adjacent(1,3).
+9

?- consult(user).

?- [user].

, (Ctrl-D Linux, Ctrl-Z MS-Windows). , . consult/1.

assert/1 retract/1 , (.. ), .

+4

All Articles