The example shows how to inspect and use theory atoms.
This is a very simple example that uses the backend to let theory atoms affect answer sets. In general, the backend can be used to implement a custom theory by translating it to a logic program. On the other hand, a propagator can be used to implement a custom theory without adding any constraints in advance. Or both approaches can be combined.
Output
./theory-atoms 0
number of grounded theory atoms: 2
theory atom b/1 has a guard: true
Model: y
Model: x y
Code
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
bool ret = true;
size_t atoms_n;
char *str = NULL;
size_t str_n = 0;
goto error;
}
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
if (str_n < n) {
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
goto error;
}
str = str_new;
str_n = n;
}
printf(" %s", str);
}
printf("\n");
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
bool ret = true;
while (true) {
if (model) { print_model(model); }
else { break; }
}
goto out;
error:
ret = false;
out:
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
size_t size;
"#theory t {"
" term { + : 1, binary, left };"
" &a/0 : term, any;"
" &b/1 : term, {=}, term, any"
"}."
"x :- &a { 1+2 }."
"y :- &b(3) { } = 17."
)) { goto error; }
printf("number of grounded theory atoms: %zu\n", size);
char const *name;
if (strcmp(name, "b") == 0) {
bool guard;
printf("theory atom b/1 has a guard: %s\n", guard ? "true" : "false");
break;
}
}
if (!solve(ctl, &solve_ret, lit)) { goto error; }
goto out;
error:
printf("%s\n", error_message);
out:
return ret;
}