Example:
Taking the code for 0
:
?- code_type(0'0,X). X = alnum ; X = csym ; X = prolog_identifier_continue ; X = ascii ; X = digit ; X = graph ; X = to_lower(48) ; X = to_upper(48) ; X = digit(0) ; X = xdigit(0).
Taking the (unicode) code for the "forall" sign (see https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode)
?- code_type(0x2200,X). X = prolog_symbol ; X = graph ; X = punct ; X = period ; X = quote ; X = to_lower(8704) ; X = to_upper(8704) ; false.
code_type/2 works for both codes and chars as argument Code, which makes it easy to use in DCGs which may be fed codes or chars without obnoxious case decisions:
any_digits([C|Cs]) --> [C],{code_type(C,digit)},!,any_digits(Cs). any_digits([]) --> []. accept_any_digits(Atom,Acc,Rest,How) :- assertion(member(How,[chars,codes])), explode(How,Atom,List), phrase(any_digits(Acc),List,Rest). explode(chars,Atom,List) :- atom_chars(Atom,List). explode(codes,Atom,List) :- atom_codes(Atom,List).
The above works irrespective of whether we feed a list of chars or a list of codes to the DCG, and even though we only use code_type/2:
?- accept_any_digits('01276x',Acc,Rest,codes). Acc = [48,49,50,55,54], Rest = [120]. ?- accept_any_digits('01276x',Acc,Rest,chars). Acc = ['0','1','2','7','6'], Rest = [x].