SWI-Prolog: how to insert a new clause to a database


I'm sorry, this has probably been asked before but I can't find a good answer.

I'm writing a Prolog assignment, in which we must write a database with insert, delete, etc. I'm currently stuck on the insert part. I'm trying to use tell, listing and told for this, but the results are often unpredictable, deleting random parts of the file. Here's the full code of my database, banco.pl:

:- dynamic progenitor/2. progenitor(maria,joao). progenitor(jose,joao). progenitor(maria,ana). progenitor(jose,ana). insere(X,Y) :- dynamic progenitor/2, assert(progenitor(X,Y)). tell('banco.pl'), listing(progenitor), told.

I then run the following on SWI-Prolog:


And get the following result on banco.pl:

:- dynamic progenitor/2. progenitor(maria, joao). progenitor(jose, joao). progenitor(maria, ana). progenitor(jose, ana).

Note that the clause I tried to insert isn't even in the file, and the lines defining commit and insere are missing.

How would I do this correctly?


tell starts writing to the beginning of the file. so you're overwriting everything else that was in the file. you have these options:


put your progenitor predicate (and just that) in another file.

</li> <li>

use append/1 to write to the end of the file with portray_clause. this only helps for insert, but you stated that you want delete too.

</li> <li>

read the other clauses into a list and reprint them, then use listing/1 :

</li> </ol>

(text for formatting)

read_all_other_clauses(All_Other_Clauses):- see('yourfilename.pl'), read_all_other_clauses_(All_Other_Clauses,[]), seen. read_all_other_clauses_(Other,Acc0):- (read(Clause) -> (Clause = progenitor(_,_) -> % omit those clauses, because they'll be printed with listing/1 read_all_other_clauses_(Other,Acc0); read_all_other_clauses_(Other,[Clause|Acc0])); Other = Acc0). % read failed, we're done operation(Insert, X,Y):- (call,(Insert) -> assert(progenitor(X,y)); retract(progenitor(X,y))), read_all_other_clauses(Others), tell('yourfilename.pl'), % After the reading! maplist(portray_clause,Others), %Maplist is a SWI built-in, easy to rewrite, if you don't have it. listing(progenitor/2), told. insert(X,Y):- operation(true,X,Y). delete(X,Y):- operation(fail,X,Y).

Note that you could use the read_all_other_clauses for your delete only, if you change the line with the omit comment. Then you could use the solution proposed in #2 for your insere


  • How to schedule streaming with Wowza Streaming Engine
  • Connection MySQL Eclipse error
  • How to use Dapper.FluentMap.Dommel.Mapping for multiple Ids
  • SQL logic for getting records in a single row for a unique id
  • How to store results by advancing memory address in c
  • Can I add an XML declaration to an XSLT output?
  • Page content not showing after Jquery function
  • Oracle where exists clause not working on SQL Plus
  • Android Chronometer own implementation
  • entries get data from combobox selection using tkinter and python
  • How to use pandas to read a line from a csv, proceed a VLOOKUP action and save the results into anot
  • Writing to csv with for loops
  • How to parse XML using c#? [closed]
  • How String.count() works? [duplicate]
  • Laravel collection accentuation error
  • Continue Postgres transaction with exceptions in Java
  • How can I open a Windows CMD window for Perl and run a command?
  • Is there anything wrong with using requires after output starts printing?
  • Entity Framework Code Migrations: Exception has been thrown by the target of an invocation
  • jquery parse xml from single and multiple tags
  • Noise after changing volume in QAudioOutput
  • All shortest paths for weighted graphs with networkx?
  • Truncate a VARCHAR to specific length in Derby AUTOMATICALLY
  • numpy array divide column by vector
  • How to attach php documentation in eclipse
  • JQuery UI selectable plugin - Multiple mouse drag selection and unselect option
  • Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
  • chrome devtools inconsistency array length
  • Can I apply the Git-Flow workflow on GitHub
  • Aspect advising other aspects
  • Can't get LogCat (Alcatel OneTouch Evolve)
  • Defining variable by logical subseting on time interval in data.table
  • Paramiko SSHException Channel Closed
  • Doctrine2 inverse persistance not working in nested forms
  • Partial specialization of a class template in derived class affects base class
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Adding a button at the bottom of a table view
  • Perl system calls when running as another user using sudo
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?