Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use SWI-Prolog ./2 function?

Need example using SWI-Prolog ./2.

The signature is

.(+Int,[])

Also if there is a name for this operator it would be nice to know. Searching for '.' is senseless.

The closest to a name is from SWI documentation section F.3 Arithmetic Functions

List of one character: character code

What I tried

?- X is .(97,[]).

results in

ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR:   [11] throw(error(type_error(dict,97),_5812))
ERROR:    [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR:    [7] <user>
ERROR: 
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

Also tried

?- X is .(97,['a']).

with same result.

like image 260
Guy Coder Avatar asked Jan 02 '23 16:01

Guy Coder


1 Answers

In standard Prolog, the '.' functor is the actual list term functor. Specifically, [H|T] is equivalent to '.'(H, T). If you run GNU Prolog, for example, you will get this:

| ?- write_canonical([H|T]).
'.'(_23,_24)

yes
| ?- X = .(H,T).

X = [H|T]

yes
| ?-

The is/2 operator will allow you to read the character code directly from a list consisting of a single character code. So the following works in Prolog:

| ?- X is [97].

X = 97

yes
| ?- X is .(97,[]).

X = 97

yes

Where it starts to get interesting is that, in SWI Prolog, they've introduced dictionaries that rely on the '.' as a means of designating dictionary keys. This creates a conflict with the '.' functor being used as a list functor. This is explained in the link on dictionaries. Thus, when you attempt to use the '.' functor for a list, you get something like this:

1 ?- X is [97].
X = 97.

2 ?- X is .(97, []).
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR:   [11] throw(error(type_error(dict,97),_5184))
ERROR:    [9] '$dicts':'.'(97,[],_5224) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR:    [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR:    [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

3 ?- X = .(H,T).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:   [11] throw(error(instantiation_error,_6914))
ERROR:    [9] '$dicts':'.'(_6944,_6946,_6948) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR:    [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR:    [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

SWI has, therefore, defined another functor, [|], to serve the purpose that '.' traditionally served, which is the list functor.

4 ?- X is '[|]'(97, []).
X = 97.

5 ?- X = '[|]'(H, T).
X = [H|T].

6 ?-

Oddly, you might think then that this would happen:

7 ?- write_canonical([H|T]).
'[|]'(_, _)

However, what happens is this:

7 ?- write_canonical([H|T]).
[_|_]
true.

So to SWI Prolog, the list representation [H|T] is already the canonical representation of the list.

like image 126
lurker Avatar answered Jan 12 '23 20:01

lurker