|
|
|
| Foreign type | Prolog type | C type | Description of the C type |
| integer | integer | long | value of the integer |
| positive | positive integer | long | value of the integer |
| float | floating point number | double | value of the floating point number |
| number | number | double | value of the number |
| atom | atom | int | internal key of the atom |
| boolean | boolean | int | value of the boolean (0=false, 1=true) |
| char | character | int | value of (the code of) the character |
| code | character code | int | value of the character-code |
| byte | byte | int | value of the byte |
| in_char | in-character | int | value of the character or -1 for end-of-file |
| in_code | in-character code | int | value of the character-code or -1 for end-of-file |
| in_byte | in-byte | int | value of the byte or -1 for the end-of-file |
| string | atom | char * | C string containing the name of the atom |
| chars | character list | char * | C string containing the characters of the list |
| codes | character-code list | char * | C string containing the characters of the list |
| term | Prolog term | PlTerm | generic Prolog term |
|
|
|
|
typedef struct
{
Bool is_var;
Bool unify;
union
{
long l;
char *s;
double d;
}value;
}FIOArg;
|
void Get_Choice_Counter(void) TYPE Get_Choice_Buffer (TYPE) void No_More_Choice (void)
|
#include <string.h>
#include "gprolog.h"
Bool
first_occurrence(char *str, long c, long *pos)
{
char *p;
p = strchr(str, c);
if (p == NULL) /* C does not appear in A */
return FALSE; /* fail */
*pos = p - str; /* set the output argument */
return TRUE; /* succeed */
}
| ?- first_occurrence(prolog, p, X).
X = 0
| ?- first_occurrence(prolog, k, X).
no
| ?- first_occurrence(prolog, A, X).
{exception: error(instantiation_error,first_occurrence/3)}
| ?- first_occurrence(prolog, 1 ,X).
{exception: error(type_error(character,1),first_occurrence/3)}
|
#include <string.h>
#include "gprolog.h"
Bool
occurrence(char *str, long c, long *pos)
{
char **info_pos;
char *p;
info_pos = Get_Choice_Buffer(char **); /* recover the buffer */
if (Get_Choice_Counter() == 0) /* first invocation ? */
*info_pos = str;
p = strchr(*info_pos, c);
if (p == NULL) /* C does not appear */
{
No_More_Choice(); /* remove choice-point */
return FALSE; /* fail */
}
*pos = p - str; /* set the output argument */
*info_pos = p + 1; /* update next starting pos */
return TRUE; /* succeed */
}
| | ?- occurrence(prolog, o, X). | ||
| X = 2 ? | (here the user presses ; to compute another solution) | |
| X = 4 ? | (here the user presses ; to compute another solution) | |
| no | (no more solution) | |
| | ?- occurrence(prolog, k, X). | ||
| no | ||
#include <string.h>
#include "gprolog.h"
Bool
occurrence2(char *str, long c, long *pos)
{
char **info_pos;
char *p;
info_pos = Get_Choice_Buffer(char **); /* recover the buffer */
if (Get_Choice_Counter() == 0) /* first invocation ? */
{
p = strchr(str, c);
if (p == NULL) /* C does not appear at all */
{
No_More_Choice(); /* remove choice-point */
return FALSE; /* fail */
}
*info_pos = p;
}
/* info_pos = an occurrence */
*pos = *info_pos - str; /* set the output argument */
p = strchr(*info_pos + 1, c);
if (p == NULL) /* no more occurrence */
No_More_Choice(); /* remove choice-point */
else
*info_pos = p; /* else update next solution */
return TRUE; /* succeed */
}
| | ?- occurrence2(prolog, l, X). | ||
| X = 3 | (here the user is not prompted since there is no more alternative) | |
| | ?- occurrence2(prolog, o, X). | ||
| X = 2 ? | (here the user presses ; to compute another solution) | |
| X = 4 | (here the user is not prompted since there is no more alternative) | |
|
#include "gprolog.h"
Bool
char_ascii(FIOArg *c, FIOArg *ascii)
{
if (!c->is_var) /* Char is not a variable */
{
ascii->unify = TRUE; /* enforce unif. of Code */
ascii->value.l = c->value.l; /* set Code */
return TRUE; /* succeed */
}
if (ascii->is_var) /* Code is also a variable */
Pl_Err_Instantiation(); /* emit instantiation_error */
c->value.l = ascii->value.l; /* set Char */
return TRUE; /* succeed */
}
| ?- char_ascii(a, X).
X = 97
| ?- char_ascii(X, 65).
X = 'A'
| ?- char_ascii(a, 12).
no
| ?- char_ascii(X, X).
{exception: error(instantiation_error,char_ascii/2)}
| ?- char_ascii(1, 12).
{exception: error(type_error(character,1),char_ascii/2)}