Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Handing Atoms



Constructing Atoms

Constructing an atom means, we can make one atom from a list of characters, or from a list of ASCII values. To do this, we have to use atom_chars() and atom_codes() predicates. In both cases, the first argument will be one variable, and the second argument will be a list. So atom_chars() constructs atom from characters, but atom_codes() construct atoms from ASCII sequence.

Example

| ?- atom_chars(X, ['t','i','g','e','r']).

X = tiger

yes
| ?- atom_chars(A, ['t','o','m']).

A = tom

yes
| ?- atom_codes(X, [97,98,99,100,101]).

X = abcde

yes
| ?- atom_codes(A, [97,98,99]).

A = abc

yes
| ?- 

Decomposing Atoms

The atom decomposing means from an atom, we can get a sequence of characters, or a sequence ASCII codes. To do this, we have to use the same atom_chars() and atom_codes() predicates. But one difference is that, in both cases, the first argument will be one atom, and the second argument will be a variable. So atom_chars() decomposes atom to characters, but atom_codes() decomposes atoms to ASCII sequence.

Example

| ?- atom_chars(tiger,X).

X = [t,i,g,e,r]

yes
| ?- atom_chars(tom,A).

A = [t,o,m]

yes
| ?- atom_codes(tiger,X).

X = [116,105,103,101,114]

yes
| ?- atom_codes(tom,A).

A = [116,111,109]

yes
| ?- 

The consult in Prolog

The consulting is a technique, that is used to merge the predicates from different files. We can use the consult() predicate, and pass the filename to attach the predicates. Let us see one example program to understand this concept.

Suppose we have two files, namely, prog1.pl and prog2.pl.

Program (prog1.pl)

likes(mary,cat).
likes(joy,rabbit).
likes(tim,duck).

Program (prog2.pl)

likes(suman,mouse).
likes(angshu,deer).

Output

| ?- consult('D:/TP Prolog/Sample Codes/prog1.pl').
compiling D:/TP Prolog/Sample Codes/prog1.pl for byte code...
D:/TP Prolog/Sample Codes/prog1.pl compiled, 2 lines read - 443 bytes written, 4 ms

(16 ms) yes
| ?- likes(joy,rabbit).

yes
| ?- likes(suman,mouse).

no
| ?- consult('D:/TP Prolog/Sample Codes/prog2.pl').
compiling D:/TP Prolog/Sample Codes/prog2.pl for byte code...
D:/TP Prolog/Sample Codes/prog2.pl compiled, 1 lines read - 366 bytes written, 3 ms
warning: D:/TP Prolog/Sample Codes/prog2.pl:1: redefining procedure likes/2
         D:/TP Prolog/Sample Codes/prog1.pl:1: previous definition

yes
| ?- likes(suman,mouse).

yes
| ?- likes(joy,rabbit).

no
| ?- 

Now from this output we can understand that this is not as simple as it seems. If two files have completely different clauses, then it will work fine. But if there are same predicates, then while we try to consult the file, it will check the predicates from the second file, when it finds some match, it simply deletes all of the entry of the same predicates from the local database, then load them again from the second file.

prolog_inputs_and_outputs.htm
Advertisements