Clingo
Loading...
Searching...
No Matches
symbol.c

The example shows how to create and inspect symbols.

The example shows how to create and inspect symbols.

Output

$ ./symbol 0
the hash of 42 is 281474976710698
the hash of x is 562949963481760
the hash of x(42,x) is 1407374893613792
42 is equal to 42
42 is not equal to x
42 is less than x

Code

// NOLINTNEXTLINE(STDC_FORMAT_MACROS)
#define STDC_FORMAT_MACROS
#include <clingo/symbol.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
enum constants {
message_limit = 25,
example_number = 42,
};
bool handle_result(clingo_result_t res) {
if (res != clingo_result_success) {
printf("%.*s\n", (int)str.size, str.data);
return false;
}
return true;
}
// clear the string builder
// retrieve the symbol's string
ret = clingo_symbol_to_string(symbol, builder);
if (ret != clingo_result_success) {
return ret;
}
// obtain the string stored in the builder
ret = clingo_string_builder_string(builder, &str);
if (ret != clingo_result_success) {
return ret;
}
// print the string
printf("%.*s", (int)str.size, str.data);
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
int ret = 0;
clingo_lib_t *lib = NULL;
clingo_symbol_t symbols[] = {0, 0, 0};
clingo_string_builder_t *builder = NULL;
clingo_symbol_t const *args = NULL;
size_t size = 0;
res = clingo_string_builder_new(&builder);
if (res != clingo_result_success) {
handle_result(res);
goto out;
}
res = clingo_lib_new(0, clingo_log_level_info, NULL, NULL, message_limit, &lib);
if (res != clingo_result_success) {
handle_result(res);
goto out;
}
// create a number, identifier (function without arguments), and a function symbol
symbols[0] = clingo_symbol_create_number(example_number);
res = clingo_symbol_create_id(lib, "x", 1, true, &symbols[1]);
if (!handle_result(res)) {
goto out;
}
res = clingo_symbol_create_function(lib, "x", 1, symbols, 2, true, &symbols[2]);
if (!handle_result(res)) {
goto out;
}
// print the symbols along with their hash values
for (size_t i = 0; i < sizeof(symbols) / sizeof(*symbols); ++i) {
printf("the hash of ");
res = print_symbol(symbols[i], builder);
if (!handle_result(res)) {
goto out;
}
printf(" is %zu\n", clingo_symbol_hash(symbols[i]));
}
// compare symbols
res = clingo_symbol_arguments(symbols[2], &args, &size);
if (!handle_result(res)) {
goto out;
}
assert(size == 2);
// equal to comparison
for (size_t i = 0; i < size; ++i) {
res = print_symbol(symbols[0], builder);
if (!handle_result(res)) {
goto out;
}
printf(" %s ", clingo_symbol_equal(symbols[0], args[i]) ? "is equal to" : "is not equal to");
res = print_symbol(args[i], builder);
if (!handle_result(res)) {
goto out;
}
printf("\n");
}
// less than comparison
res = print_symbol(symbols[0], builder);
if (!handle_result(res)) {
goto out;
}
printf(" %s ", clingo_symbol_compare(symbols[0], symbols[1]) < 0 ? "is less than" : "is not less than");
res = print_symbol(symbols[1], builder);
if (!handle_result(res)) {
goto out;
}
printf("\n");
out:
return ret;
}
CLINGO_VISIBILITY_DEFAULT void clingo_lib_release(clingo_lib_t *lib)
Release a library object created with clingo_lib_new().
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_free(clingo_string_builder_t const *bld)
Free the string builder.
CLINGO_VISIBILITY_DEFAULT void clingo_string_builder_clear(clingo_string_builder_t *bld)
Clear the string in the builder.
CLINGO_VISIBILITY_DEFAULT bool clingo_lib_new(clingo_lib_flags_t flags, clingo_log_level_t level, clingo_logger_t const *logger, void *data, size_t limit, clingo_lib_t **lib)
Create a library object.
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:176
struct clingo_string_builder clingo_string_builder_t
A builder for strings.
Definition core.h:257
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_string(clingo_string_builder_t const *bld, clingo_string_t *value)
Get the (zero-terminated) string in the builder.
CLINGO_VISIBILITY_DEFAULT void clingo_result_string(clingo_result_t code, clingo_string_t *value)
Convert the given result code into a string.
int clingo_result_t
Corresponding type to clingo_result_e.
Definition core.h:111
CLINGO_VISIBILITY_DEFAULT bool clingo_string_builder_new(clingo_string_builder_t **bld)
Create a new string builder.
@ clingo_log_level_info
the info level
Definition core.h:149
@ clingo_result_success
successful API calls
Definition core.h:103
CLINGO_VISIBILITY_DEFAULT int clingo_symbol_compare(clingo_symbol_t a, clingo_symbol_t b)
Check if a symbol is less than another symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, clingo_string_builder_t *builder)
Get the string representation of a symbol.
CLINGO_VISIBILITY_DEFAULT size_t clingo_symbol_hash(clingo_symbol_t symbol)
Calculate a hash code of a symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_create_id(clingo_lib_t *lib, char const *name, size_t size, bool is_positive, clingo_symbol_t *symbol)
Construct a symbol representing an id.
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_create_function(clingo_lib_t *lib, char const *name, size_t name_size, clingo_symbol_t const *arguments, size_t arguments_size, bool is_positive, clingo_symbol_t *symbol)
Construct a symbol representing a function.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_equal(clingo_symbol_t a, clingo_symbol_t b)
Check if two symbols are equal.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_number(int32_t number)
Construct a symbol representing a number.
auto main(Library &lib, std::span< std::string_view const > arguments={}, App *app=nullptr, bool raise_errors=false) -> int
Run a clingo based application with the given arguments.
Definition app.hh:219
@ symbols
Whether to write symbols in a structured format.
Struct to capture strings that are not null-terminated.
Definition core.h:91
size_t size
the length of the string
Definition core.h:93
char const * data
pointer to the beginning of the string
Definition core.h:92