1:- module(pchar, [
    2    pchar_upper/2,
    3    pchar_lower/2,
    4    pchar_type/2,
    5    pchar_code/2,
    6    pchar_hex2/2
    7]).    8
    9:- use_module(purity).   10:- use_module(phex).   11
   12:- multifile(purity:pcompare/4).   13:- multifile(purity:ptype/2).   14
   15purity:pcompare(pchar, A, B, C) :-
   16    pchar_hex2(A, Ah),
   17    pchar_hex2(B, Bh),
   18    purity:pcompare(Ah,Bh,C).
   19 
   20 % pchar_upper(Char, UpperChar).
   21 pchar_upper(C, U) :- ch_map(C, _, U).
   22 
   23 % pchar_lower(Char, LowerChar).
   24 pchar_lower(C, L) :- ch_map(C, L, _).
   25 
   26purity:ptype( a, pchar).
   27purity:ptype( b, pchar).
   28purity:ptype( c, pchar).
   29purity:ptype( d, pchar).
   30purity:ptype( e, pchar).
   31purity:ptype( f, pchar).
   32purity:ptype( g, pchar).
   33purity:ptype( h, pchar).
   34purity:ptype( i, pchar).
   35purity:ptype( j, pchar).
   36purity:ptype( k, pchar).
   37purity:ptype( l, pchar).
   38purity:ptype( m, pchar).
   39purity:ptype( n, pchar).
   40purity:ptype( o, pchar).
   41purity:ptype( p, pchar).
   42purity:ptype( q, pchar).
   43purity:ptype( r, pchar).
   44purity:ptype( s, pchar).
   45purity:ptype( t, pchar).
   46purity:ptype( u, pchar).
   47purity:ptype( v, pchar).
   48purity:ptype( w, pchar).
   49purity:ptype( x, pchar).
   50purity:ptype( y, pchar).
   51purity:ptype( z, pchar).
   52purity:ptype( 'A', pchar).
   53purity:ptype( 'B', pchar).
   54purity:ptype( 'C', pchar).
   55purity:ptype( 'D', pchar).
   56purity:ptype( 'E', pchar).
   57purity:ptype( 'F', pchar).
   58purity:ptype( 'G', pchar).
   59purity:ptype( 'H', pchar).
   60purity:ptype( 'I', pchar).
   61purity:ptype( 'J', pchar).
   62purity:ptype( 'K', pchar).
   63purity:ptype( 'L', pchar).
   64purity:ptype( 'M', pchar).
   65purity:ptype( 'N', pchar).
   66purity:ptype( 'O', pchar).
   67purity:ptype( 'P', pchar).
   68purity:ptype( 'Q', pchar).
   69purity:ptype( 'R', pchar).
   70purity:ptype( 'S', pchar).
   71purity:ptype( 'T', pchar).
   72purity:ptype( 'U', pchar).
   73purity:ptype( 'V', pchar).
   74purity:ptype( 'W', pchar).
   75purity:ptype( 'X', pchar).
   76purity:ptype( 'Y', pchar).
   77purity:ptype( 'Z', pchar).
   78purity:ptype( ';', pchar).
   79purity:ptype( ':', pchar).
   80purity:ptype( '"', pchar).
   81purity:ptype( '\'', pchar).
   82purity:ptype( '\'', pchar).
   83purity:ptype( '/', pchar).
   84purity:ptype( '\\', pchar).
   85purity:ptype( '<', pchar).
   86purity:ptype( '>', pchar).
   87purity:ptype( ',', pchar).
   88purity:ptype( '.', pchar).
   89purity:ptype( '!', pchar).
   90purity:ptype( '@', pchar).
   91purity:ptype( '#', pchar).
   92purity:ptype( '$', pchar).
   93purity:ptype( '%', pchar).
   94purity:ptype( '^', pchar).
   95purity:ptype( '&', pchar).
   96purity:ptype( '*', pchar).
   97purity:ptype( '(', pchar).
   98purity:ptype( ')', pchar).
   99purity:ptype( '[', pchar).
  100purity:ptype( ']', pchar).
  101purity:ptype( '{', pchar).
  102purity:ptype( '}', pchar).
  103purity:ptype( '|', pchar).
  104purity:ptype( '-', pchar).
  105purity:ptype( '_', pchar).
  106purity:ptype( '+', pchar).
  107purity:ptype( '=', pchar).
  108purity:ptype( '0', pchar).
  109purity:ptype( '1', pchar).
  110purity:ptype( '2', pchar).
  111purity:ptype( '3', pchar).
  112purity:ptype( '4', pchar).
  113purity:ptype( '5', pchar).
  114purity:ptype( '6', pchar).
  115purity:ptype( '7', pchar).
  116purity:ptype( '8', pchar).
  117purity:ptype( '9', pchar).
  118purity:ptype( ' ', pchar).
  119purity:ptype( '\n', pchar).
  120purity:ptype( '\r', pchar).
  121purity:ptype( '\t', pchar).
  122 
  123 % ch_map(Char, Lower, Upper).
  124 ch_map( a, a, 'A').
  125 ch_map( b, b, 'B').
  126 ch_map( c, c, 'C').
  127 ch_map( d, d, 'D').
  128 ch_map( e, e, 'E').
  129 ch_map( f, f, 'F').
  130 ch_map( g, g, 'G').
  131 ch_map( h, h, 'H').
  132 ch_map( i, i, 'I').
  133 ch_map( j, j, 'J').
  134 ch_map( k, k, 'K').
  135 ch_map( l, l, 'L').
  136 ch_map( m, m, 'M').
  137 ch_map( n, n, 'N').
  138 ch_map( o, o, 'O').
  139 ch_map( p, p, 'P').
  140 ch_map( q, q, 'Q').
  141 ch_map( r, r, 'R').
  142 ch_map( s, s, 'S').
  143 ch_map( t, t, 'T').
  144 ch_map( u, u, 'U').
  145 ch_map( v, v, 'V').
  146 ch_map( w, w, 'W').
  147 ch_map( x, x, 'X').
  148 ch_map( y, y, 'Y').
  149 ch_map( z, z, 'Z').
  150 ch_map( 'A', a, 'A').
  151 ch_map( 'B', b, 'B').
  152 ch_map( 'C', c, 'C').
  153 ch_map( 'D', d, 'D').
  154 ch_map( 'E', e, 'E').
  155 ch_map( 'F', f, 'F').
  156 ch_map( 'G', g, 'G').
  157 ch_map( 'H', h, 'H').
  158 ch_map( 'I', i, 'I').
  159 ch_map( 'J', j, 'J').
  160 ch_map( 'K', k, 'K').
  161 ch_map( 'L', l, 'L').
  162 ch_map( 'M', m, 'M').
  163 ch_map( 'N', n, 'N').
  164 ch_map( 'O', o, 'O').
  165 ch_map( 'P', p, 'P').
  166 ch_map( 'Q', q, 'Q').
  167 ch_map( 'R', r, 'R').
  168 ch_map( 'S', s, 'S').
  169 ch_map( 'T', t, 'T').
  170 ch_map( 'U', u, 'U').
  171 ch_map( 'V', v, 'V').
  172 ch_map( 'W', w, 'W').
  173 ch_map( 'X', x, 'X').
  174 ch_map( 'Y', y, 'Y').
  175 ch_map( 'Z', z, 'Z').
  176 ch_map( ';', ';', ';').
  177 ch_map( ':', ':', ':').
  178 ch_map( '"', '"', '"').
  179 ch_map( '\'','\'','\'').
  180 ch_map( '\'','\'','\'').
  181 ch_map( '/','/','/').
  182 ch_map( '\\','\\','\\').
  183 ch_map( '<','<','<').
  184 ch_map( '>','>','>').
  185 ch_map( ',',',',',').
  186 ch_map( '.','.','.').
  187 ch_map( '!','!','!').
  188 ch_map( '@','@','@').
  189 ch_map( '#','#','#').
  190 ch_map( '$','$','$').
  191 ch_map( '%','%','%').
  192 ch_map( '^','^','^').
  193 ch_map( '&','&','&').
  194 ch_map( '*','*','*').
  195 ch_map( '(','(','(').
  196 ch_map( ')',')',')').
  197 ch_map( '[','[','[').
  198 ch_map( ']',']',']').
  199 ch_map( '{','{','{').
  200 ch_map( '}','}','}').
  201 ch_map( '|','|','|').
  202 ch_map( '-','-','-').
  203 ch_map( '_','_','_').
  204 ch_map( '+','+','+').
  205 ch_map( '=','=','=').
  206 ch_map( '0','0','0').
  207 ch_map( '1','1','1').
  208 ch_map( '2','2','2').
  209 ch_map( '3','3','3').
  210 ch_map( '4','4','4').
  211 ch_map( '5','5','5').
  212 ch_map( '6','6','6').
  213 ch_map( '7','7','7').
  214 ch_map( '8','8','8').
  215 ch_map( '9','9','9').
  216 ch_map( ' ',' ',' ').
  217 ch_map( '\n','\n','\n').
  218 ch_map( '\r','\r','\r').
  219 ch_map( '\t','\t','\t').
  220 
  221 % pchar_type(Char, Type).
  222 %
  223 % Type is one of alpha, digit, symbol, or whitespace
  224 % 
  225 pchar_type( a, alpha).
  226 pchar_type( b, alpha).
  227 pchar_type( c, alpha).
  228 pchar_type( d, alpha).
  229 pchar_type( e, alpha).
  230 pchar_type( f, alpha).
  231 pchar_type( g, alpha).
  232 pchar_type( h, alpha).
  233 pchar_type( i, alpha).
  234 pchar_type( j, alpha).
  235 pchar_type( k, alpha).
  236 pchar_type( l, alpha).
  237 pchar_type( m, alpha).
  238 pchar_type( n, alpha).
  239 pchar_type( o, alpha).
  240 pchar_type( p, alpha).
  241 pchar_type( q, alpha).
  242 pchar_type( r, alpha).
  243 pchar_type( s, alpha).
  244 pchar_type( t, alpha).
  245 pchar_type( u, alpha).
  246 pchar_type( v, alpha).
  247 pchar_type( w, alpha).
  248 pchar_type( x, alpha).
  249 pchar_type( y, alpha).
  250 pchar_type( z, alpha).
  251 pchar_type( 'A', alpha).
  252 pchar_type( 'B', alpha).
  253 pchar_type( 'C', alpha).
  254 pchar_type( 'D', alpha).
  255 pchar_type( 'E', alpha).
  256 pchar_type( 'F', alpha).
  257 pchar_type( 'G', alpha).
  258 pchar_type( 'H', alpha).
  259 pchar_type( 'I', alpha).
  260 pchar_type( 'J', alpha).
  261 pchar_type( 'K', alpha).
  262 pchar_type( 'L', alpha).
  263 pchar_type( 'M', alpha).
  264 pchar_type( 'N', alpha).
  265 pchar_type( 'O', alpha).
  266 pchar_type( 'P', alpha).
  267 pchar_type( 'Q', alpha).
  268 pchar_type( 'R', alpha).
  269 pchar_type( 'S', alpha).
  270 pchar_type( 'T', alpha).
  271 pchar_type( 'U', alpha).
  272 pchar_type( 'V', alpha).
  273 pchar_type( 'W', alpha).
  274 pchar_type( 'X', alpha).
  275 pchar_type( 'Y', alpha).
  276 pchar_type( 'Z', alpha).
  277 pchar_type( ';', symbol).
  278 pchar_type( ':', symbol).
  279 pchar_type( '"', symbol).
  280 pchar_type( '\'', symbol).
  281 pchar_type( '\'', symbol).
  282 pchar_type( '/', symbol).
  283 pchar_type( '\\', symbol).
  284 pchar_type( '<', symbol).
  285 pchar_type( '>', symbol).
  286 pchar_type( ',', symbol).
  287 pchar_type( '.', symbol).
  288 pchar_type( '!', symbol).
  289 pchar_type( '@', symbol).
  290 pchar_type( '#', symbol).
  291 pchar_type( '$', symbol).
  292 pchar_type( '%', symbol).
  293 pchar_type( '^', symbol).
  294 pchar_type( '&', symbol).
  295 pchar_type( '*', symbol).
  296 pchar_type( '(', symbol).
  297 pchar_type( ')', symbol).
  298 pchar_type( '[', symbol).
  299 pchar_type( ']', symbol).
  300 pchar_type( '{', symbol).
  301 pchar_type( '}', symbol).
  302 pchar_type( '|', symbol).
  303 pchar_type( '-', symbol).
  304 pchar_type( '_', symbol).
  305 pchar_type( '+', symbol).
  306 pchar_type( '=', symbol).
  307 pchar_type( '0', digit).
  308 pchar_type( '1', digit).
  309 pchar_type( '2', digit).
  310 pchar_type( '3', digit).
  311 pchar_type( '4', digit).
  312 pchar_type( '5', digit).
  313 pchar_type( '6', digit).
  314 pchar_type( '7', digit).
  315 pchar_type( '8', digit).
  316 pchar_type( '9', digit).
  317 pchar_type( ' ', whitespace).
  318 pchar_type( '\n', whitespace).
  319 pchar_type( '\r', whitespace).
  320 pchar_type( '\t', whitespace).
  321 
  322% phar_hex2(Char,Hex2). 
  323 pchar_hex2(a,hex2(6,1)).
  324 pchar_hex2(b,hex2(6,2)).
  325 pchar_hex2(c,hex2(6,3)).
  326 pchar_hex2(d,hex2(6,4)).
  327 pchar_hex2(e,hex2(6,5)).
  328 pchar_hex2(f,hex2(6,6)).
  329 pchar_hex2(g,hex2(6,7)).
  330 pchar_hex2(h,hex2(6,8)).
  331 pchar_hex2(i,hex2(6,9)).
  332 pchar_hex2(j,hex2(6,a)).
  333 pchar_hex2(k,hex2(6,b)).
  334 pchar_hex2(l,hex2(6,c)).
  335 pchar_hex2(m,hex2(6,d)).
  336 pchar_hex2(n,hex2(6,e)).
  337 pchar_hex2(o,hex2(6,f)).
  338 pchar_hex2(p,hex2(7,0)).
  339 pchar_hex2(q,hex2(7,1)).
  340 pchar_hex2(r,hex2(7,2)).
  341 pchar_hex2(s,hex2(7,3)).
  342 pchar_hex2(t,hex2(7,4)).
  343 pchar_hex2(u,hex2(7,5)).
  344 pchar_hex2(v,hex2(7,6)).
  345 pchar_hex2(w,hex2(7,7)).
  346 pchar_hex2(x,hex2(7,8)).
  347 pchar_hex2(y,hex2(7,9)).
  348 pchar_hex2(z,hex2(7,a)).
  349 pchar_hex2('A',hex2(4,1)).
  350 pchar_hex2('B',hex2(4,2)).
  351 pchar_hex2('C',hex2(4,3)).
  352 pchar_hex2('D',hex2(4,4)).
  353 pchar_hex2('E',hex2(4,5)).
  354 pchar_hex2('F',hex2(4,6)).
  355 pchar_hex2('G',hex2(4,7)).
  356 pchar_hex2('H',hex2(4,8)).
  357 pchar_hex2('I',hex2(4,9)).
  358 pchar_hex2('J',hex2(4,a)).
  359 pchar_hex2('K',hex2(4,b)).
  360 pchar_hex2('L',hex2(4,c)).
  361 pchar_hex2('M',hex2(4,d)).
  362 pchar_hex2('N',hex2(4,e)).
  363 pchar_hex2('O',hex2(4,f)).
  364 pchar_hex2('P',hex2(5,0)).
  365 pchar_hex2('Q',hex2(5,1)).
  366 pchar_hex2('R',hex2(5,2)).
  367 pchar_hex2('S',hex2(5,3)).
  368 pchar_hex2('T',hex2(5,4)).
  369 pchar_hex2('U',hex2(5,5)).
  370 pchar_hex2('V',hex2(5,6)).
  371 pchar_hex2('W',hex2(5,7)).
  372 pchar_hex2('X',hex2(5,8)).
  373 pchar_hex2('Y',hex2(5,9)).
  374 pchar_hex2('Z',hex2(5,a)).
  375 pchar_hex2(;,hex2(3,b)).
  376 pchar_hex2(:,hex2(3,a)).
  377 pchar_hex2('"',hex2(2,2)).
  378 pchar_hex2('\'',hex2(2,7)).
  379 pchar_hex2(?,hex2(3,f)).
  380 pchar_hex2(/,hex2(2,f)).
  381 pchar_hex2('\\',hex2(5,c)).
  382 pchar_hex2(<,hex2(3,c)).
  383 pchar_hex2(>,hex2(3,e)).
  384 pchar_hex2(',',hex2(2,c)).
  385 pchar_hex2('.',hex2(2,e)).
  386 pchar_hex2('!',hex2(2,1)).
  387 pchar_hex2(@,hex2(4,0)).
  388 pchar_hex2(#,hex2(2,3)).
  389 pchar_hex2($,hex2(2,4)).
  390 pchar_hex2('%',hex2(2,5)).
  391 pchar_hex2(^,hex2(5,e)).
  392 pchar_hex2(&,hex2(2,6)).
  393 pchar_hex2(*,hex2(2,a)).
  394 pchar_hex2('(',hex2(2,8)).
  395 pchar_hex2(')',hex2(2,9)).
  396 pchar_hex2('[',hex2(5,b)).
  397 pchar_hex2(']',hex2(5,d)).
  398 pchar_hex2('{',hex2(7,b)).
  399 pchar_hex2('}',hex2(7,d)).
  400 pchar_hex2('|',hex2(7,c)).
  401 pchar_hex2('-',hex2(2,d)).
  402 pchar_hex2('_',hex2(5,f)).
  403 pchar_hex2(+,hex2(2,b)).
  404 pchar_hex2(=,hex2(3,d)).
  405 pchar_hex2('0',hex2(3,0)).
  406 pchar_hex2('1',hex2(3,1)).
  407 pchar_hex2('2',hex2(3,2)).
  408 pchar_hex2('3',hex2(3,3)).
  409 pchar_hex2('4',hex2(3,4)).
  410 pchar_hex2('5',hex2(3,5)).
  411 pchar_hex2('6',hex2(3,6)).
  412 pchar_hex2('7',hex2(3,7)).
  413 pchar_hex2('8',hex2(3,8)).
  414 pchar_hex2('9',hex2(3,9)).
  415 pchar_hex2(' ',hex2(2,0)).
  416 pchar_hex2('\n',hex2(0,a)).
  417 pchar_hex2('\r',hex2(0,d)).
  418 pchar_hex2('\t',hex2(0,9)).
  419 
  420% pchar_code(Char,Code).
  421 pchar_code(a,97).
  422 pchar_code(b,98).
  423 pchar_code(c,99).
  424 pchar_code(d,100).
  425 pchar_code(e,101).
  426 pchar_code(f,102).
  427 pchar_code(g,103).
  428 pchar_code(h,104).
  429 pchar_code(i,105).
  430 pchar_code(j,106).
  431 pchar_code(k,107).
  432 pchar_code(l,108).
  433 pchar_code(m,109).
  434 pchar_code(n,110).
  435 pchar_code(o,111).
  436 pchar_code(p,112).
  437 pchar_code(q,113).
  438 pchar_code(r,114).
  439 pchar_code(s,115).
  440 pchar_code(t,116).
  441 pchar_code(u,117).
  442 pchar_code(v,118).
  443 pchar_code(w,119).
  444 pchar_code(x,120).
  445 pchar_code(y,121).
  446 pchar_code(z,122).
  447 pchar_code('A',65).
  448 pchar_code('B',66).
  449 pchar_code('C',67).
  450 pchar_code('D',68).
  451 pchar_code('E',69).
  452 pchar_code('F',70).
  453 pchar_code('G',71).
  454 pchar_code('H',72).
  455 pchar_code('I',73).
  456 pchar_code('J',74).
  457 pchar_code('K',75).
  458 pchar_code('L',76).
  459 pchar_code('M',77).
  460 pchar_code('N',78).
  461 pchar_code('O',79).
  462 pchar_code('P',80).
  463 pchar_code('Q',81).
  464 pchar_code('R',82).
  465 pchar_code('S',83).
  466 pchar_code('T',84).
  467 pchar_code('U',85).
  468 pchar_code('V',86).
  469 pchar_code('W',87).
  470 pchar_code('X',88).
  471 pchar_code('Y',89).
  472 pchar_code('Z',90).
  473 pchar_code(;,59).
  474 pchar_code(:,58).
  475 pchar_code('"',34).
  476 pchar_code('\'',39).
  477 pchar_code(?,63).
  478 pchar_code(/,47).
  479 pchar_code(\,92).
  480 pchar_code(<,60).
  481 pchar_code(>,62).
  482 pchar_code(',',44).
  483 pchar_code('.',46).
  484 pchar_code(!,33).
  485 pchar_code(@,64).
  486 pchar_code(#,35).
  487 pchar_code($,36).
  488 pchar_code('%',37).
  489 pchar_code(^,94).
  490 pchar_code(&,38).
  491 pchar_code(*,42).
  492 pchar_code('(',40).
  493 pchar_code(')',41).
  494 pchar_code('[',91).
  495 pchar_code(']',93).
  496 pchar_code('{',123).
  497 pchar_code('}',125).
  498 pchar_code('|',124).
  499 pchar_code(-,45).
  500 pchar_code('_',95).
  501 pchar_code(+,43).
  502 pchar_code(=,61).
  503 pchar_code('0',48).
  504 pchar_code('1',49).
  505 pchar_code('2',50).
  506 pchar_code('3',51).
  507 pchar_code('4',52).
  508 pchar_code('5',53).
  509 pchar_code('6',54).
  510 pchar_code('7',55).
  511 pchar_code('8',56).
  512 pchar_code('9',57).
  513 pchar_code(' ',32).
  514 pchar_code('\n',10).
  515 pchar_code('\r',13).
  516 pchar_code('\t',9)