Re: Four Easy Question

From: Martin Sondergaard (nobody_at_nowhere.com)
Date: 03/23/04


Date: Tue, 23 Mar 2004 21:34:02 -0000

Linux Man wrote :

> Hi
>
> I'm new in prolog and have some easy question:
>
> 1- How can I create a file in prolog?
>
> 2- How can I read from a file or check that, say "john is father of
> bob" (father(john, bob)), from that file?
>
> 3- How can I write a new predicate in the created file
> (e.g. father(jim, alice).)?
>
>
> Many Thanks
> Steve

Another way of doing it, instead of reading each line from the file,
one line at a time,
is to read in the whole file by using "consult( 'myfile' )".

Also, you can write all your Prolog facts out to a file by using "listing",
instead of using "write" statements.

E.g.

write_to_file :-
    tell( 'myfile.pl' ), % Open the file.
    listing(father), % Write facts.
    listing(mother),
    listing(sibling),
    told. % Close the file.

read_from_file :-
    consult( 'myfile.pl' ).

    To understand the above, learn about "listing/1",
and try it out.
    In the code above, I used "tell" and "told" instead of "open" and
"close".
But you can use "open" and "close" if you prefer.

> 3- How can I write a new predicate in the created file
> (e.g. father(jim, alice).)?

First read in the old file, using "consult( 'myfile.pl' )".
Then assert the new fact, with "assert( father(jim, alice))".
Assert any new facts you want, and retract any facts you want to delete.
Then delete the old file, and write all the facts out to a new file.

?- consult( 'myfile.pl' ).

yes

?- assert(father(jim, alice)).

yes

?- assert( ... ). Etc.

?- delete_file( 'myfile.pl' ), write_to_file.

--
Martin Sondergaard,
London.


Relevant Pages

  • Re: Complete noob question.
    ... > Emacs or Prolog. ... I have purchased two books ... prolog facts and prolog questions. ... ?- assert(greek(socrates)). ...
    (comp.lang.prolog)
  • Re: Election 2008 - FYI
    ... Bob W, are you so filled with overbearing personal hubris that you can't ... Don't you know better than to argue with Ed Jay. ... Ed is going to respond with truth and facts to ... And God forbid you should disagree with him. ...
    (rec.arts.dance)
  • Re: OO versus RDB
    ... There is a very simple mapping from RM to PROLOG. ... a representation based on predicates, ... 'connection' comes from the fact that PROLOG stores rules about facts ...
    (comp.object)
  • Re: The good old AR7 survival rifle is back !!
    ... Now tell us again....do you pack all that junk in your bob or in your ... attempt to define "BOB" v. Survival Kit as "the last thing you grab as ... explained facts. ... wallets, money, pocketknife, Tums and dental floss, you've declined to ...
    (misc.survivalism)
  • Re: Representing data structures in Prolog
    ... > the representation of knowledge with Prolog. ... I have several facts of the form. ... > object in object oriented languages. ... And create rules with multiple arguments to express more succinctly ...
    (comp.lang.prolog)