2
votes

I'm new to Prolog, please just don't blast me.

Whenever i had to consult a prolog database i used the predicate consult/1 :

consult(:File)

Now, i noticed that there exist also the predicate open

open(+SrcDest, +Mode, --Stream, +Options)

that allows to read a database. Apart the possibility to modify the database, not allowed by consult, which are the differences between consult and open (maybe in the extensions of the files that each predicate can open, or maybe because consult reads fact and rules, while with open we can read terms) ?

1
Note that facts and rules (and directives) are terms. - Paulo Moura
consult asserts the contents of the file. open does not. - lurker
However, why facts and rules are also used as predicates ? - Koinos
I'm not sure I understand your question. And it's different than what you're asking in your post. A predicate in Prolog is a set of facts and rules with the same name. - lurker

1 Answers

2
votes

Using consult/1 seems to be quite similar to use ?- [filename]. Using consult/1 in your program, you can have access to all the facts and predicates written into the file. So for instance if you have a file data.pl like this:

fact(a).
fact(b).
fact(c).

hello:-
    writeln('hello').

You can create a file, test.pl and use all the facts and predicate of data.pl:

run:-
    consult(prova),
    findall(A,fact(A),L),
    hello,
    writeln(L).

?- run.
hello
[a,b,c]
true.

Moreover, consult seems to accept only .pl file with a dictionary sructure. On the other hand, with open/3, you can access each type of file and read also char by char, but you cannot access the predicates and facts written into the file:

run:-
    open('prova.pl',read,Str),
    findall(A,fact(A),L),
    hello,
    writeln(L).

?- run.
ERROR: Undefined procedure: fact/1

Obviously, with open/3 or open/4 you can create, write into files and so on.