Re: C interface confusion about char codes, lists and atoms...
From:
Sean Charles
Subject:
Re: C interface confusion about char codes, lists and atoms...
Date:
Sun, 14 Jul 2013 08:41:20 +0100
I found it….
Section 6.2 Types and Modes, in the table…
character
a single character atom
and from the preceding table entry:
TYPE_list
a list whose the type of each element is
TYPE
So I guess it was there after all I just didn't see it. What a surprise.
On 4 Jul 2013, at 09:23, Daniel Diaz <address@hidden> wrote:
Le 03/07/2013 18:30, emacstheviking a
écrit :
Can somebody point me to to the docs where it says
"single character atoms are characters" please....
You are right, from the Prolog programmer point of view, a character
is an atom of length 1.
Unfortunately the gprolog doc does not define the syntax of Prolog
terms since gprolog conforms to the ISO standard for Prolog (ok I
should add it).
Lower case letters can be written unquoted (e.g. a, b, ...) orther
chars need to be quoted (e.g. '#', 'A', ...).
Daniel
I seem to have missed something basic. I will also
read LPN, C&M and Art of Prolog tonight and make sure I take
it in that atoms are also characters.
Dammit.
:(
On 3 July 2013 16:26, Daniel Diaz <address@hidden>
wrote:
Le 03/07/2013 16:44, emacstheviking a écrit :
Hi,
I have been experimenting with the foreign
interface and managed to confuse myself.
Given this foreign declaration:
%% :- foreign( foo( +string ), [fct_name( foo
)]). %% +string => atom at call site
%% :- foreign( foo( +codes ), [fct_name( foo )]).
:- foreign( foo( +codes ), [fct_name( foo )]).
and this implementation:
// +string :: atom ==> char*,
works
// +codes :: character-code list ==> char*,
works as foo("Hello").
// +chars :: character list ==>
char*, fails on call foo("HI"), foo([64,65]).
Why?
PlBool
foo(char* s)
{
printf("FOO: ==> %s\n", s);
return PL_TRUE;
}
My problem is that when I use "+chars" I can't then
say:
foo("Hello")
as I get a type_error so please can anybody explain
to me what "character list" is and how it looks in a
command line session? The flag that controls the
interpretation of double-quoted strings is at its
default value as I have not altered it but I am aware
of its existence and the fact that "" is syntactic
sugaring of lists of codes.
Hi,
+atom means you pass an atom (e.g. abc) and you get the
internal reference of the atom in C (which is an integer)
+chars means you pass a list of characters (e.g. [a,b,c])
and you get a NULL terminated string in C, ie. a char *
(e.g. "abc")
+code means you pass a list of char codes (e.g. [97,98,99]
which is the same a "abc" in Prolog) an dyou get the same
char * as above.