3
votes

I am very confused what is the difference between csymf and csym in prolog, when I am using

?-code_type(X,csym).

X = 48 ;
X = 49 ;
....

?-code_type(X,csymf).

X = 65;
X = 66;
....

Would you please someone tell me what X should be exactly, I read the manual of SWI-prolog in this part and it is mentioned that

csym :Char is a letter (upper- or lowercase), digit or the underscore (_). These are valid C and Prolog symbol characters.

csymf :Char is a letter (upper- or lowercase) or the underscore (_). These are valid first characters for C and Prolog symbols.

So would you please provide me with some examples ?

Thanks

1
Use char_type/2 and be aware that only Latin-1 characters are generated. Yet, char_type/2 is defined for all Unicode characters. This is very SWI-specific. - false
?-char_type(A,cymf). will give me all starting letters that can cymf type starts with, including letters capital and small,underscore, and also ?, and some latin letters. ?- char_type(A,csym). gives me all starting character for the csym including numbers, letters wether capital or small and also latin characters, so I think that char_type(A,cymf),is a subset of char_type(A,cymf). Thanks for providing me such predicate to use. - Yasmin

1 Answers

2
votes

You already have found the relevant descriptions, that state that code_type(Code, Type) holds a relation between a character code and his classification.

I think the manual is a bit misleading, because Prolog symbols are different than C (or Java, to say) symbols. These latter can be described with a regular expression like [_a-zA-Z][_a-zA-Z0-9]*, equivalent to the readable descriptions you cite.

Then c9 is a valid C symbol, while 9c is not (a digit cant start a symbol).

To inspect all 'properties' of a character (I assume you are aware of differences between character code - an integer - and the encoded character - localized), you could use

?- char_type(v,T).
T = alnum ;
T = alpha ;
T = csym ;
T = csymf ;
T = ascii ;
T = graph ;
T = lower ;
T = lower('V') ;
T = to_lower('V') ;
T = to_upper(v) ;
false.

Then v could start a C symbol.

?- char_type('7',T).
T = alnum ;
T = csym ;
T = ascii ;
T = digit ;
T = graph ;
T = to_lower('7') ;
T = to_upper('7') ;
T = digit(7) ;
T = xdigit(7).

We miss csymf here, then 7 cannot start a C symbol.

To get all the characters that can start a C symbol, you could use

?- forall(char_type(X,csymf),write(X)).
ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyzªµºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
true.

I think your result could be different than mine, depending on your locale.