Updated April 5, 2023
Introduction to Prolog assert
The prolog assert is a predicate to add a new argument in the application’s database using prolog language. It is a meta-predicate to insert facts and clauses in the database as per requirement. It is a function to insert a single clause or rule at the first or last of the database using a programming language. It is a different type of method to add required clauses and facts in the database using a prolog programming language. It is one of the predicates to add facts or rules before or after other arguments and clauses in the prolog database. The prolog database allows to adds of some rules, facts, and clauses at any position using predicate are called a prolog assert.
Syntax of Prolog assert
The following syntaxes show the prolog assert and its data.
assert(Data).
Explanation:
- It uses to add clauses or facts in the database.
- It inserts arguments before or after other arguments.
- This function uses a single argument with the fact or clause.
For example, the prolog assert inserts clause before other arguments syntax shows below.
asserta(Data).
Explanation:
- It uses to add clauses or facts in the database.
- It inserts arguments before other arguments.
- This function uses a single argument with the fact or clause.
It inserts clause after other arguments syntax shows below.
Assertz (Data).
Explanation:
- It uses to add clauses or facts in the database.
- It inserts argument after other arguments.
- This function uses a single argument with the fact or clause.
With fact and value before arguments, syntax shows below.
asserta(fact('value')).
Explanation:
- It uses to add facts and their value in the database.
- It inserts arguments before other arguments.
With fact and value after other arguments, syntax shows below.
Assertz (fact('value')).
Explanation:
- It uses to add facts and their value in the database.
- It inserts argument after other arguments.
How does assert Works in Prolog?
Given below shows how assert works in Prolog:
Method 1:
Prolog comment works in the “pl” file.
- Create a file with the “pl” extension.
Example: main.pl
- If you want the data, then refer to the prolog file.
Prolog comment works in the console.
- Open prolog console or interpreter.
- Set the directory path of the “pl” file.
- Use the given prolog file.
[main].
- You can use the prolog console directly for programming.
- Use the syntax in the prolog console.
| ?- asserta(teachers).
Or
| ?- assertz(teachers).
Method 2:
You can use the prolog console directly for inserts clauses in a database.
Prolog comment works in the console.
- Open prolog console or interpreter.
- You can use the prolog console directly for programming.
- Use the syntax in the prolog console.
| ?- asserta((teachers(student, teacher) :-
principle(student, teacher),
learner(student))).
Method 3:
You can use the prolog console directly for inserts clauses in a database.
Prolog comment works in the console.
- Open prolog console or interpreter.
- You can use the prolog console directly for programming.
- Uses the syntax in the prolog console.
| ?- asserta(st('a', 'b')).
Or
| ?- assertz(st('a', 'b')).
Method 4:
You can use the prolog console directly for inserts clauses in a database.
Prolog comment works in the console.
- Open prolog console or interpreter.
- You can use the prolog console directly for programming.
- Uses the syntax in the prolog console.
| ?- asserta(st('a')).
Or
| ?- assertz(st('a')).
Examples of Prolog assert
Different examples are mentioned below:
Example #1
The basic “prolog assert” with before argument example, and the output shows below.
Code:
| ?- asserta(teachers).
Output:
Explanation:
- It uses to add a single clause in the database.
- It inserts arguments before other arguments.
Example #2
The basic “prolog assert” with after argument example, and the output shows below.
Code:
| ?- assertz(teachers).
Output:
Explanation:
- It uses to add a single clause in the database.
- It inserts argument after other arguments.
Example #3
The basic “prolog assert” with before argument example, and the output shows below.
Code:
| ?- asserta(st('good')).
Output:
Explanation:
- This command uses fact and its single data in the database.
- It inserts arguments before other arguments.
Example #4
The basic “prolog assert” with after argument example, and the output shows below.
Code:
| ?- assertz(st('good')).
Output:
Explanation:
- This command uses fact and its single data in the database.
- It inserts argument after other arguments.
Example #5
The basic “prolog assert” with before argument example, and the output shows below.
Code:
| ?- asserta(st('a', 'b')).
Output:
Explanation:
- This command uses facts and multiple data in the database.
- It inserts arguments before other arguments.
Example #6
The basic “prolog assert” with after argument example, and the output shows below.
Code:
| ?- assertz(st('a', 'b')).
Output:
Explanation:
- This command uses facts and multiple data in the database.
- It inserts argument after other arguments.
Example #7
The basic “prolog assert” with before argument example, and the output shows below.
Code:
| ?- asserta((teachers(student, teacher) :-
principle(student, teacher),
learner(student))).
Output:
Explanation:
- This command uses multiple facts and its data in the database.
- It inserts arguments before other arguments.
Example #8
The basic “prolog assert” with after argument example, and the output shows below.
Code:
| ?- assertz((teachers(student, teacher) :-
principle(student, teacher),
learner(student))).
Output:
Explanation:
- This command uses multiple facts and its data in the database.
- It inserts argument after other arguments.
Conclusion
It inserts facts, rules, and clauses into the application database. The condition creates an elegant, user-friendly, and operative application. This function manages and operates programming data or values as per requirement.
Recommended Articles
We hope that this EDUCBA information on “Prolog assert” was beneficial to you. You can view EDUCBA’s recommended articles for more information.