Atomic term processing

These predicates enable atomic terms to be processed as a sequence of characters and character codes. Facilities exist to split and join atoms, to convert a single character to and from the corresponding character code, and to convert a number to and from a list of characters.

1. atom_length/2

atom_length(Atom, Length) is true iff integer Length equals the number of characters of the name of the atom Atom.

Templates and modes for the predicate are as follows:

atom_length(+atom, ?integer)

1.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
atom_length('scarlet', 5). false

Now we run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query atom_length('enchanted evening', N).
check hasSolution true
enter variable N
check binding 17
enter query atom_length('enchanted\
evening', N).
check hasSolution true
enter variable N
check binding 17
enter query atom_length('', N).
check hasSolution true
enter variable N
check binding 0

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query atom_length(Atom, 4).
check hasSolution false
check exception instantiation_error
enter query atom_length(1.23, 4).
check hasSolution false
check exception type_error(atom, 1.23)
enter query atom_length(atom, '4').
check hasSolution false
check exception type_error(integer, '4')

2. atom_concat/3

atom_concat(Atom_1, Atom_2, Atom_12) is true iff characters of the name of the atom Atom_12 are the result of concatenating the characters of the name of the atom Atom_2 to the characters of the name of the atom Atom_1.

atom_concat(Atom_1, Atom_2, Atom_12) is re-executable.

Templates and modes for the predicate are as follows:

atom_concat(?atom, ?atom, +atom)
atom_concat(+atom, +atom, -atom)

2.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
atom_concat('hello', 'world', 'small world'). false

Now we run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query atom_concat('hello', 'world', S3).
check hasSolution true
enter variable S3
check binding 'helloworld'
enter query atom_concat('small', T, 'smallworld').
check hasSolution true
enter variable T
check binding 'world'
enter query atom_concat(T, 'world', 'smallworld').
check hasSolution true
enter variable T
check binding 'small'
enter query atom_concat(T1, T2, 'hello').
check hasSolution true
enter variable T1
check binding ''
enter variable T2
check binding 'hello'
check hasAnotherSolution true
enter variable T1
check binding 'h'
enter variable T2
check binding 'ello'
check hasAnotherSolution true
enter variable T1
check binding 'he'
enter variable T2
check binding 'llo'
check hasAnotherSolution true
enter variable T1
check binding 'hel'
enter variable T2
check binding 'lo'
check hasAnotherSolution true
enter variable T1
check binding 'hell'
enter variable T2
check binding 'o'
check hasAnotherSolution true
enter variable T1
check binding 'hello'
enter variable T2
check binding ''
check hasAnotherSolution false

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query atom_concat(small, V2, V4).
check hasSolution false
check exception instantiation_error

3. sub_atom/5

sub_atom(Atom, Before, Length, After, Sub_atom) is true iff atom Atom can be broken into three pieces, AtomL, Sub_atom and AtomR such that Before is the number of characters of the name of AtomL, Length is the number of characters of the name of Sub_atom and After is the number of characters of the name of AtomR.

Templates and modes for the predicate are as follows:

sub_atom(+atom, ?integer, ?integer, ?integer, ?atom)

sub_atom(Atom, Before, Length, After, Sub_atom) is re-executable.

3.1 Example tests

We run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query sub_atom(abracadabra, 0, 5, _, S2).
check hasSolution true
enter variable S2
check binding 'abrac'
enter query sub_atom(abracadabra, _, 5, 0, S2).
check hasSolution true
enter variable S2
check binding 'dabra'
enter query sub_atom(abracadabra, 3, L, 3, S2).
check hasSolution true
enter variable L
check binding 5
enter variable S2
check binding 'acada'
enter query sub_atom(abracadabra, B, 2, A, ab).
check hasSolution true
enter variable B
check binding 0
enter variable A
check binding 9
check hasAnotherSolution true
enter variable B
check binding 7
enter variable A
check binding 2
enter query sub_atom('Banana', 3, 2, _, S2).
check hasSolution true
enter variable S2
check binding 'an'
enter query sub_atom('charity', _, 3, _, S2).
check hasSolution true
enter variable S2
check binding 'cha'
check hasAnotherSolution true
enter variable S2
check binding 'har'
check hasAnotherSolution true
enter variable S2
check binding 'ari'
check hasAnotherSolution true
enter variable S2
check binding 'rit'
check hasAnotherSolution true
enter variable S2
check binding 'ity'
enter query sub_atom('ab', Start, Length, _, Sub_atom).
check hasSolution true
enter variable Start
check binding 0
enter variable Length
check binding 0
enter variable Sub_atom
check binding ''
check hasAnotherSolution true
enter variable Start
check binding 0
enter variable Length
check binding 1
enter variable Sub_atom
check binding 'a'
check hasAnotherSolution true
enter variable Start
check binding 0
enter variable Length
check binding 2
enter variable Sub_atom
check binding 'ab'
check hasAnotherSolution true
enter variable Start
check binding 1
enter variable Length
check binding 0
enter variable Sub_atom
check binding ''
check hasAnotherSolution true
enter variable Start
check binding 1
enter variable Length
check binding 1
enter variable Sub_atom
check binding 'b'
check hasAnotherSolution true
enter variable Start
check binding 2
enter variable Length
check binding 0
enter variable Sub_atom
check binding ''

Note that there are no tests covering the cases when an error or exception is thrown by the engine while solving a query using this predicate.

4. atom_chars/2

atom_chars(Atom, List) is true iff List is a list whose elements are the one-char atoms whose names are the successive characters of the name of atom Atom.

Templates and modes for the predicate are as follows:

atom_chars(+atom, ?character_list)
atom_chars(-atom, +character_list)

4.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
atom_chars('soap', ['s', 'o', 'p']). false

We now run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query atom_chars('', L).
check hasSolution true
enter variable L
check binding []
enter query atom_chars([], L).
check hasSolution true
enter variable L
check binding ['[', ']']
enter query atom_chars('''', L).
check hasSolution true
enter variable L
check binding ['''']
enter query atom_chars('ant', L).
check hasSolution true
enter variable L
check binding ['a', 'n', 't']
enter query atom_chars(Str, ['s', 'o', 'p']).
check hasSolution true
enter variable Str
check binding 'sop'
enter query atom_chars('North', ['N' | X]).
check hasSolution true
enter variable X
check binding ['o', 'r', 't', 'h']

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query atom_chars(X, Y).
check hasSolution false
check exception instantiation_error

5. atom_codes/2

atom_codes(Atom, List) is true iff List is a list whose elements correspond to the successive characters of the name Atom, and the value of each element is the character code for the corresponding character of the name.

Templates and modes for the predicate are as follows:

atom_codes(+atom, ?character_code_list)
atom_codes(-atom, +character_code_list)

5.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
atom_codes('soap', [0's, 0'o, 0'p]). false

We now run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query atom_codes('', L).
check hasSolution true
enter variable L
check binding []
enter query atom_codes([], L).
check hasSolution true
enter variable L
check binding [0'[, 0']]
enter query atom_codes('''', L).
check hasSolution true
enter variable L
check binding [0''']
enter query atom_codes('ant', L).
check hasSolution true
enter variable L
check binding [0'a, 0'n, 0't]
enter query atom_codes(Str, [0's, 0'o, 0'p]).
check hasSolution true
enter variable Str
check binding 'sop'
enter query atom_codes('North', [0'N | X]).
check hasSolution true
enter variable X
check binding [0'o, 0'r, 0't, 0'h]

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query atom_codes(X, Y).
check hasSolution false
check exception instantiation_error

6. char_code/2

char_code(Char, Code) is true iff the character code for the one-char atom Char is Code.

Templates and modes for the predicate are as follows:

char_code(+character, ?character_code)
char_code(-character, +character_code)

6.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
char_code('b', 84). false

We now run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query char_code('a', Code).
check hasSolution true
enter variable Code
check binding 97
enter query char_code(Str, 99).
check hasSolution true
enter variable Str
check binding c
enter query char_code(Str, 0'c).
check hasSolution true
enter variable Str
check binding c
enter query char_code(Str, 163).
check hasSolution true
enter variable Str
check binding '£'

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query char_code('ab', Int).
check hasSolution false
check exception type_error(character, ab)
enter query char_code(C, I).
check hasSolution false
check exception instantiation_error

7. number_chars/2

number_chars(Number, List) is true iff List is a list whose elements are the one-char atoms corresponding to a character sequence of Number which could be output.

Templates and modes for the predicate are as follows:

number_chars(+number, ?character_list)
number_chars(-number, +character_list)

7.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
number_chars(33, ['3', '3']). true
number_chars(3.3, ['3', '.', '3']). true

We now run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query number_chars(33, L).
check hasSolution true
enter variable L
check binding ['3', '3']
enter query number_chars(33.0, L).
check hasSolution true
enter variable L
check binding ['3', '3', '.', '0']
enter query number_chars(X, ['3', '.', '3', 'E', '+', '0']).
check hasSolution true
enter variable X
check binding 3.3
enter query number_chars(A, ['-', '2', '5']).
check hasSolution true
enter variable A
check binding -25
enter query number_chars(A, ['\t','\n', ' ', '3']).
check hasSolution true
enter variable A
check binding 3
enter query number_chars(A, ['0', x, f]).
check hasSolution true
enter variable A
check binding 15
enter query number_chars(A, ['0', '''''', a]).
check hasSolution true
enter variable A
check binding 97
enter query number_chars(A, ['4', '.', '2']).
check hasSolution true
enter variable A
check binding 4.2
enter query number_chars(A, ['4', '2', '.', '0', 'e', '-', '1']).
check hasSolution true
enter variable A
check binding 4.2

The remaining tests cover the cases when an error or exception is thrown by the engine while solving a query.

alice.tuprolog.PrologActionFixture
enter query number_chars(A, ['\t','3', 'g',' ']).
check hasSolution false
check exception syntax_error
enter query number_chars(A, ['\t','3', 'g','4']).
check hasSolution false
check exception syntax_error
enter query number_chars(A, ['\t','3', '4','g', '5']).
check hasSolution false
check exception syntax_error
enter query number_chars(A, ['\t', '3', '5', '-', '6']).
check hasSolution false
check exception syntax_error

8. number_codes/2

number_codes(Number, List) is true iff List is a list whose elements are the character codes corresponding to a character sequence of Number which could be output.

Templates and modes for the predicate are as follows:

number_codes(+number, ?character_code_list)
number_codes(-number, +character_code_list)

8.1 Example tests

Let's start with some simple tests verifying success or failure of single goals.

alice.tuprolog.SimpleGoalFixture
goalsuccess()
number_codes(33, [0'3, 0'3]). true
number_codes(3.3, [0'3, 0'., 0'3]). true

We now run some tests also verifying the unification for some of the variables in goals.

First of all, let's start an appropriate fixture containing an engine.

fit.ActionFixture
start alice.tuprolog.EngineFixture

Then, ask the engine to solve a query, and check variable bindings.

fit.ActionFixture
enter query number_codes(33, L).
check hasSolution true
enter variable L
check binding [0'3, 0'3]
enter query number_codes(33.0, L).
check hasSolution true
enter variable L
check binding [0'3, 0'3, 0'., 0'0]
enter query number_codes(A, [0'-, 0'2, 0'5]).
check hasSolution true
enter variable A
check binding -25
enter query number_codes(A, [0' , 0'3]).
check hasSolution true
enter variable A
check binding 3
enter query number_codes(A, [0'0, 0'x, 0'f]).
check hasSolution true
enter variable A
check binding 15
enter query number_codes(A, [0'0, 0''''', 0'a]).
check hasSolution true
enter variable A
check binding 97
enter query number_codes(A, [0'4, 0'., 0'2]).
check hasSolution true
enter variable A
check binding 4.2
enter query number_codes(A, [0'4, 0'2, 0'., 0'0, 0'e, 0'-, 0'1]).
check hasSolution true
enter variable A
check binding 4.2

Note that there are no tests covering the cases when an error or exception is thrown by the engine while solving a query using this predicate.

Run the tests!


The results of the tests for Atomic term processing are as follows:

fit.Summary