-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalma_backsearch.c
190 lines (167 loc) · 7 KB
/
alma_backsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <string.h>
#include "alma_backsearch.h"
static void collect_variables(alma_term *term, binding_list *b, int quote_level, void *parent) {
if (term->type == VARIABLE) {
b->num_bindings++;
b->list = realloc(b->list, sizeof(*b->list) * b->num_bindings);
b->list[b->num_bindings-1].var = malloc(sizeof(alma_variable));
copy_alma_var(term->variable, b->list[b->num_bindings-1].var);
b->list[b->num_bindings-1].quote_level = quote_level;
b->list[b->num_bindings-1].term = malloc(sizeof(alma_term));
copy_alma_term(term, b->list[b->num_bindings-1].term);
}
else if (term->type == FUNCTION) {
for (int i = 0; i < term->function->term_count; i++)
collect_variables(term->function->terms+i, b, quote_level, parent);
}
else if (term->type == QUOTE && term->quote->type == CLAUSE) {
clause *c = term->quote->clause_quote;
for (int i = 0; i < c->pos_count; i++)
for (int j = 0; j < c->pos_lits[i]->term_count; j++)
collect_variables(c->pos_lits[i]->terms+j, b, quote_level+1, parent);
for (int i = 0; i < c->neg_count; i++)
for (int j = 0; j < c->neg_lits[i]->term_count; j++)
collect_variables(c->neg_lits[i]->terms+j, b, quote_level+1, parent);
}
else {
// TODO quasi-quote case
}
}
// Initializes backward search with negation of clause argument
void backsearch_from_clause(tommy_list *backsearch_tasks, clause *c) {
backsearch_task *bt = malloc(sizeof(*bt));
bt->clause_count = 0;
c->index = --bt->clause_count;
bt->target = c;
clause *negated = malloc(sizeof(*negated));
memcpy(negated, c, sizeof(*negated));
alma_function *negated_lit;
if (c->pos_count == 1) {
negated->neg_count = 1;
negated->neg_lits = malloc(sizeof(*negated->neg_lits));
negated_lit = negated->neg_lits[0] = malloc(sizeof(*negated->neg_lits[0]));
copy_alma_function(c->pos_lits[0], negated->neg_lits[0]);
negated->pos_count = 0;
negated->pos_lits = NULL;
}
else {
negated->pos_count = 1;
negated->pos_lits = malloc(sizeof(*negated->pos_lits));
negated_lit = negated->pos_lits[0] = malloc(sizeof(*negated->pos_lits[0]));
copy_alma_function(c->neg_lits[0], negated->pos_lits[0]);
negated->neg_count = 0;
negated->neg_lits = NULL;
}
tommy_array_init(&bt->clauses);
tommy_hashlin_init(&bt->clause_bindings);
tommy_array_init(&bt->new_clauses);
tommy_array_insert(&bt->new_clauses, negated);
tommy_array_init(&bt->new_clause_bindings);
binding_list *negated_bindings = malloc(sizeof(*negated_bindings));
init_bindings(negated_bindings);
// Create initial bindings where each variable in negated target is present, but each term is set to NULL
for (int i = 0; i < negated_lit->term_count; i++)
collect_variables(negated_lit->terms+i, negated_bindings, 0, negated_lit);
bt->target_vars = negated_bindings;
tommy_array_insert(&bt->new_clause_bindings, negated_bindings);
tommy_array_init(&bt->to_resolve);
tommy_list_insert_tail(backsearch_tasks, &bt->node, bt);
}
static clause* backsearch_duplicate_check(backsearch_task *task, clause *c) {
for (int i = 0; i < tommy_array_size(&task->clauses); i++) {
clause *compare = tommy_array_get(&task->clauses, i);
if (compare->tag == c->tag && !clauses_differ(c, compare, 0))
return compare;
}
return NULL;
}
// Given particular task, populate to_resolve based on each clause
void generate_backsearch_tasks(kb *collection, long time, backsearch_task *bt) {
for (int i = 0; i < tommy_array_size(&bt->new_clauses); i++) {
clause *c = tommy_array_get(&bt->new_clauses, i);
clause *dupe = duplicate_check(collection, time, c, 0);
int bs_dupe = 0;
if (dupe == NULL) {
dupe = backsearch_duplicate_check(bt, c);
bs_dupe = 1;
}
if (dupe == NULL) {
c->index = --bt->clause_count;
// Obtain tasks between new backward search clauses and KB items
make_res_tasks(c, c->pos_count, c->pos_lits, &collection->neg_map, &bt->to_resolve, 1, 0);
make_res_tasks(c, c->neg_count, c->neg_lits, &collection->pos_map, &bt->to_resolve, 1, 1);
// Obtain tasks between pairs of backsearch clauses
// Currently a linear scan, perhaps to adjust later
for (int j = 0; j < tommy_array_size(&bt->clauses); j++) {
clause *jth = tommy_array_get(&bt->clauses, j);
for (int k = 0; k < c->pos_count; k++)
make_single_task(c, c->pos_lits[k], jth, &bt->to_resolve, 1, 0);
for (int k = 0; k < c->neg_count; k++)
make_single_task(c, c->neg_lits[k], jth, &bt->to_resolve, 1, 1);
}
tommy_array_insert(&bt->clauses, c);
// Insert corresponding binding into hashmap
binding_mapping *entry = malloc(sizeof(*entry));
entry->key = c->index;
entry->bindings = tommy_array_get(&bt->new_clause_bindings, i);
tommy_hashlin_insert(&bt->clause_bindings, &entry->node, entry, tommy_hash_u64(0, &entry->key, sizeof(entry->key)));
}
else {
if (bs_dupe)
transfer_parent(dupe, c, 0);
cleanup_bindings(tommy_array_get(&bt->new_clause_bindings, i));
free_clause(c);
}
}
tommy_array_done(&bt->new_clauses);
tommy_array_init(&bt->new_clauses);
tommy_array_done(&bt->new_clause_bindings);
tommy_array_init(&bt->new_clause_bindings);
}
// Advances by resolving available tasks
void process_backsearch_tasks(kb *collection, long time, tommy_list *backsearch_tasks, kb_logger *logger) {
tommy_node *curr = tommy_list_head(backsearch_tasks);
while (curr) {
backsearch_task *t = curr->data;
process_res_tasks(collection, time, &t->to_resolve, &t->new_clauses, t, logger);
curr = curr->next;
}
}
static void free_binding_mapping(void *arg) {
binding_mapping *bm = arg;
cleanup_bindings(bm->bindings);
free(bm);
}
void backsearch_halt(backsearch_task *t) {
free_clause(t->target);
for (tommy_size_t i = 0; i < tommy_array_size(&t->clauses); i++)
free_clause(tommy_array_get(&t->clauses, i));
tommy_array_done(&t->clauses);
tommy_hashlin_foreach(&t->clause_bindings, free_binding_mapping);
tommy_hashlin_done(&t->clause_bindings);
for (tommy_size_t i = 0; i < tommy_array_size(&t->new_clauses); i++)
free_clause(tommy_array_get(&t->new_clauses, i));
tommy_array_done(&t->new_clauses);
for (tommy_size_t i = 0; i < tommy_array_size(&t->new_clause_bindings); i++)
cleanup_bindings(tommy_array_get(&t->new_clause_bindings, i));
tommy_array_done(&t->new_clause_bindings);
for (tommy_size_t i = 0; i < tommy_array_size(&t->to_resolve); i++)
free(tommy_array_get(&t->to_resolve, i));
tommy_array_done(&t->to_resolve);
free(t);
}
int idling_backsearch(tommy_list *backsearch_tasks) {
tommy_node *i = tommy_list_head(backsearch_tasks);
while (i) {
backsearch_task *bt = i->data;
if (tommy_array_size(&bt->to_resolve) > 0)
return 0;
i = i->next;
}
return 1;
}
// Compare function to be used by tommy_hashlin_search for binding_mapping
// Compares long arg to key of binding_mapping
int bm_compare(const void *arg, const void *obj) {
return *(const long*)arg - ((const binding_mapping*)obj)->key;
}