-
Notifications
You must be signed in to change notification settings - Fork 3
/
alma_fif.c
628 lines (562 loc) · 25.4 KB
/
alma_fif.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#include <string.h>
#include "alma_fif.h"
#include "alma_proc.h"
#include "alma_unify.h"
// Given a new fif clause, initializes fif task mappings held by fif_tasks for each premise of c
// Also places single fif_task into fif_task_mapping for first premise
void fif_task_map_init(kb *collection, alma_proc *procs, tommy_hashlin *fif_tasks, clause *c, int init_to_unify) {
if (c->tag == FIF) {
for (int i = 0; i < c->fif->premise_count; i++) {
alma_function *f = fif_access(c, i);
// Don't make task mappings for middle proc premises
if (i > 0 && is_proc(f, procs))
continue;
char *name = name_with_arity(f->name, f->term_count);
fif_task_mapping *result = tommy_hashlin_search(fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
// If a task map entry doesn't exist for name of a literal, create one
if (result == NULL) {
result = malloc(sizeof(*result));
result->predname = malloc(strlen(name)+1);
strcpy(result->predname, name);
tommy_list_init(&result->tasks);
tommy_hashlin_insert(fif_tasks, &result->node, result, tommy_hash_u64(0, result->predname, strlen(result->predname)));
}
// For first premise, initialze fif_task root for c
if (i == 0) {
fif_task *task = malloc(sizeof(*task));
task->fif = c;
task->bindings = malloc(sizeof(*task->bindings));
init_bindings(task->bindings);
task->premises_done = task->num_unified = 0;
task->unified_clauses = NULL;
task->num_to_unify = 0;
task->to_unify = NULL;
if (init_to_unify) {
// Searches for existing singletons to set to_unify
tommy_hashlin *map = c->fif->ordering[0] < 0 ? &collection->pos_map : &collection->neg_map; // Sign flip for clauses to unify with first fif premise
predname_mapping *pm_result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (pm_result != NULL) {
for (int j = 0; j < pm_result->num_clauses; j++) {
if (flags_negative(pm_result->clauses[j]) && pm_result->clauses[j]->pos_count + pm_result->clauses[j]->neg_count == 1) {
task->num_to_unify++;
task->to_unify = realloc(task->to_unify, sizeof(*task->to_unify) * task->num_to_unify);
task->to_unify[task->num_to_unify-1] = pm_result->clauses[j];
}
}
}
}
task->proc_next = is_proc(fif_access(c, 0), procs);
tommy_list_insert_tail(&result->tasks, &task->node, task);
}
free(name);
}
}
}
// Sets to_unify entries for fif tasks
void fif_tasks_from_clause(tommy_hashlin *fif_tasks, clause *c) {
// If a non-fif singleton clause, sets to_unify for tasks with a matching next clause to process
if (c->tag == NONE && c->pos_count + c->neg_count == 1) {
char *name;
int pos = 1;
if (c->pos_count == 1)
name = name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
else {
name = name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count);
pos = 0;
}
fif_task_mapping *result = tommy_hashlin_search(fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
if (result != NULL) {
int len = tommy_list_count(&result->tasks);
tommy_node *curr = tommy_list_head(&result->tasks);
// Process original list contents
int i = 0;
while (curr && i < len) {
fif_task *data = curr->data;
// Only modify add entry to to_unify if next for fif isn't a procedure predicate
if (flags_negative(data->fif) && !data->proc_next) {
int sign_match = (pos && data->fif->fif->ordering[data->premises_done] < 0) || (!pos && data->fif->fif->ordering[data->premises_done] >= 0);
if (sign_match) {
data->num_to_unify++;
data->to_unify = realloc(data->to_unify, sizeof(*data->to_unify) * data->num_to_unify);
data->to_unify[data->num_to_unify-1] = c;
}
}
curr = curr->next;
i++;
}
}
}
}
// Returns 1 if any of of task's unified_clauses is flagged as distrusted/retired/handled
static int fif_unified_flagged(fif_task *task) {
for (int i = 0; i < task->num_unified; i++) {
if (!flags_negative(task->unified_clauses[i]))
return 1;
}
return 0;
}
// Called when each premise of a fif is satisfied
static void fif_conclude(fif_task *task, binding_list *bindings, tommy_array *new_clauses, long long *id_count) {
for (int i = 0; i < task->fif->fif->num_conclusions; i++) {
clause *conclusion = malloc(sizeof(*conclusion));
copy_clause_structure(task->fif->fif->conclusions[i], conclusion);
conclusion->parent_set_count = 1;
conclusion->parents = malloc(sizeof(*conclusion->parents));
conclusion->parents[0].count = task->num_unified + 1;
conclusion->parents[0].clauses = malloc(sizeof(*conclusion->parents[0].clauses) * conclusion->parents[0].count);
for (int j = 0; j < conclusion->parents[0].count-1; j++) {
conclusion->parents[0].clauses[j] = task->unified_clauses[j];
}
conclusion->parents[0].clauses[conclusion->parents[0].count-1] = task->fif;
// Using task's overall bindings, obtain proper conclusion clause
subst_clause(bindings, conclusion, 0);
set_variable_ids(conclusion, 1, 0, NULL, id_count);
tommy_array_insert(new_clauses, conclusion);
}
}
// Continues attempting to unify from tasks set
// Task instances for which cannot be further progressed with current KB are added to stopped
static void fif_task_unify_loop(kb *collection, tommy_list *tasks, tommy_list *stopped, alma_proc *procs, kb_logger *logger) {
tommy_node *curr = tommy_list_head(tasks);
int len = tommy_list_count(tasks);
// Process original list contents
while (curr && len > 0) {
fif_task *next_task = curr->data;
curr = curr->next;
len--;
// Withdraw current task from set
tommy_list_remove_existing(tasks, &next_task->node);
alma_function *next_func = fif_access(next_task->fif, next_task->premises_done);
// Proc case
if (next_task->proc_next) {
if (collection->verbose)
print_bindings(next_task->bindings, 1, 0, logger);
if (proc_bound_check(next_func, next_task->bindings, procs) && proc_run(next_func, next_task->bindings, next_task, collection, logger)) {
if (collection->verbose) {
tee_alt("Proc succeeded!\n", logger);
print_bindings(next_task->bindings, 1, 1, logger);
}
next_task->premises_done++;
if (next_task->premises_done == next_task->fif->fif->premise_count) {
if (!fif_unified_flagged(next_task))
fif_conclude(next_task, next_task->bindings, &collection->new_clauses, &collection->variable_id_count);
free_fif_task(next_task);
}
// If incomplete, use current task to continue processing
else {
next_task->proc_next = is_proc(fif_access(next_task->fif, next_task->premises_done), procs);
tommy_list_insert_tail(tasks, &next_task->node, next_task);
}
}
else {
if (collection->verbose)
print_bindings(next_task->bindings, 1, 1, logger);
free_fif_task(next_task);
}
}
// Unify case
else {
char *name = name_with_arity(next_func->name, next_func->term_count);
// Important: because of conversion to CNF, a negative in ordering means fif premise is positive
int search_pos = (next_task->fif->fif->ordering[next_task->premises_done] < 0);
predname_mapping *m = tommy_hashlin_search(search_pos ? &collection->pos_map : &collection->neg_map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
int task_erased = 0;
if (m != NULL) {
for (int j = 0; j < m->num_clauses; j++) {
clause *jth = m->clauses[j];
if (flags_negative(jth) && jth->pos_count + jth->neg_count == 1) {
alma_function *to_unify = search_pos ? jth->pos_lits[0] : jth->neg_lits[0];
binding_list *copy = malloc(sizeof(*copy));
copy_bindings(copy, next_task->bindings);
if (collection->verbose) {
print_bindings(copy, 1, 0, logger);
print_unify(next_func, next_task->fif->index, to_unify, jth->index, logger);
}
if (pred_unify(next_func, to_unify, copy, collection->verbose)) {
if (collection->verbose) {
tee_alt("Unification succeeded!\n", logger);
print_bindings(copy, 1, 1, logger);
}
// If task is now completed, obtain resulting clause and insert to new_clauses
if (next_task->premises_done + 1 == next_task->fif->fif->premise_count) {
if (fif_unified_flagged(next_task)) {
// If any unified clauses became flagged, delete task
free_fif_task(next_task);
task_erased = 1;
break;
}
else {
next_task->num_unified++;
next_task->premises_done++;
next_task->unified_clauses = realloc(next_task->unified_clauses, sizeof(*next_task->unified_clauses)*next_task->num_unified);
next_task->unified_clauses[next_task->num_unified-1] = jth;
fif_conclude(next_task, copy, &collection->new_clauses, &collection->variable_id_count);
cleanup_bindings(copy);
next_task->num_unified--;
next_task->premises_done--;
}
}
// Otherwise, to continue processing, branch off second task for results and place in tasks
else {
fif_task *latest = malloc(sizeof(*latest));
latest->fif = next_task->fif;
latest->bindings = copy;
latest->premises_done = next_task->premises_done + 1;
latest->num_unified = next_task->num_unified + 1;
latest->unified_clauses = malloc(sizeof(*latest->unified_clauses)*latest->num_unified);
memcpy(latest->unified_clauses, next_task->unified_clauses, sizeof(*next_task->unified_clauses) * next_task->num_unified);
latest->unified_clauses[latest->num_unified-1] = jth;
latest->num_to_unify = 0;
latest->to_unify = NULL;
latest->proc_next = is_proc(fif_access(latest->fif, latest->premises_done), procs);
tommy_list_insert_tail(tasks, &latest->node, latest);
}
}
else {
if (collection->verbose) {
tee_alt("Unification failed\n", logger);
print_bindings(copy, 1, 1, logger);
}
// Unification failure
cleanup_bindings(copy);
}
}
}
}
if (!task_erased)
tommy_list_insert_tail(stopped, &next_task->node, next_task);
}
}
// If tasks list is nonempty, have newly tasks to recurse on and attempt to progress
if (!tommy_list_empty(tasks))
fif_task_unify_loop(collection, tasks, stopped, procs, logger);
}
// Process fif tasks
// For each task in fif_task_mapping:
// - Attempt to progress with either proc predicate or unification on to_unify
// - If progression succeeds, progresses further on next literals as able
// Above process repeats per task until unification fails or fif task is fully satisfied (and then asserts conclusion)
static void process_fif_task_mapping(kb *collection, fif_task_mapping *entry, tommy_list *to_progress, alma_proc *procs, kb_logger *logger) {
tommy_node *curr = tommy_list_head(&entry->tasks);
while (curr) {
fif_task *f = curr->data;
curr = curr->next;
if (f->to_unify != NULL || f->proc_next) {
binding_list *copy = malloc(sizeof(*copy));
copy_bindings(copy, f->bindings);
if (f->to_unify != NULL) {
if (fif_unified_flagged(f)) {
// If any unified clauses became flagged, delete task
tommy_list_remove_existing(&entry->tasks, &f->node);
free_fif_task(f);
}
else {
for (int i = 0; i < f->num_to_unify; i++) {
clause *unify_target = f->to_unify[i];
// Clause to unify must not have become flagged since it was connected to the fif task
if (flags_negative(unify_target)) {
alma_function *to_unify_func = (unify_target->pos_count > 0) ? unify_target->pos_lits[0] : unify_target->neg_lits[0];
if (collection->verbose) {
print_bindings(f->bindings, 1, 0, logger);
print_unify(to_unify_func, unify_target->index, fif_access(f->fif, f->premises_done), f->fif->index, logger);
}
if (pred_unify(fif_access(f->fif, f->premises_done), to_unify_func, f->bindings, collection->verbose)) {
if (collection->verbose) {
tee_alt("Unification succeeded!\n", logger);
print_bindings(f->bindings, 1, 1, logger);
}
// If task is now completed, obtain resulting clause and insert to new_clauses
if (f->premises_done + 1 == f->fif->fif->premise_count) {
f->premises_done++;
f->num_unified++;
f->unified_clauses = realloc(f->unified_clauses, sizeof(*f->unified_clauses)*f->num_unified);
f->unified_clauses[f->num_unified-1] = unify_target;
fif_conclude(f, f->bindings, &collection->new_clauses, &collection->variable_id_count);
cleanup_bindings(f->bindings);
f->premises_done--;
f->num_unified--;
f->unified_clauses = realloc(f->unified_clauses, sizeof(*f->unified_clauses)*f->num_unified);
}
// Otherwise, branch off copy of task and do unify loop call
else {
fif_task *advanced = malloc(sizeof(*advanced));
advanced->fif = f->fif;
advanced->bindings = f->bindings;
advanced->premises_done = f->premises_done + 1;
advanced->num_unified = f->num_unified + 1;
advanced->unified_clauses = malloc(sizeof(*advanced->unified_clauses)*advanced->num_unified);
memcpy(advanced->unified_clauses, f->unified_clauses, sizeof(*f->unified_clauses)*f->num_unified);
advanced->unified_clauses[advanced->num_unified-1] = unify_target;
advanced->num_to_unify = 0;
advanced->to_unify = NULL;
advanced->proc_next = is_proc(fif_access(advanced->fif, advanced->premises_done), procs);
tommy_list_insert_tail(to_progress, &advanced->node, advanced);
}
f->bindings = malloc(sizeof(*f->bindings));
copy_bindings(f->bindings, copy);
}
else {
if (collection->verbose) {
tee_alt("Unification failed\n", logger);
print_bindings(f->bindings, 1, 1, logger);
}
cleanup_bindings(f->bindings);
f->bindings = malloc(sizeof(*f->bindings));
copy_bindings(f->bindings, copy);
}
}
}
free(f->to_unify);
f->to_unify = NULL;
f->num_to_unify = 0;
f->proc_next = is_proc(fif_access(f->fif, f->premises_done), procs);
}
cleanup_bindings(copy);
}
else {
alma_function *proc = fif_access(f->fif, f->premises_done);
if (proc_bound_check(proc, f->bindings, procs) && proc_run(proc, f->bindings, f, collection, logger)) {
if (collection->verbose) {
tee_alt("Proc succeeded!\n", logger);
print_bindings(f->bindings, 1, 1, logger);
}
// Regardless of outcome, remove case with proc from tasks
tommy_list_remove_existing(&entry->tasks, &f->node);
f->premises_done++;
// If task is now completed, obtain resulting clause and insert to new_clauses
if (f->premises_done == f->fif->fif->premise_count) {
if (!fif_unified_flagged(f))
fif_conclude(f, f->bindings, &collection->new_clauses, &collection->variable_id_count);
// Delete task that held case ending with proc
free_fif_task(f);
cleanup_bindings(copy);
}
// If incomplete modify current task for results to continue processing
else {
f->proc_next = is_proc(fif_access(f->fif, f->premises_done), procs);
tommy_list_insert_tail(to_progress, &f->node, f);
}
}
else {
cleanup_bindings(copy);
if (collection->verbose)
print_bindings(f->bindings, 1, 1, logger);
}
}
}
}
}
// Process fif tasks and place results in new_clauses
void process_fif_tasks(kb *collection, alma_proc *procs, kb_logger *logger) {
tommy_list to_progress;
tommy_list_init(&to_progress);
// Loop over hashlin contents of collection's fif tasks; based on tommy_hashlin_foreach
// TODO: may want double indexing if this loops too many times
tommy_size_t bucket_max = collection->fif_tasks.low_max + collection->fif_tasks.split;
for (tommy_size_t pos = 0; pos < bucket_max; ++pos) {
tommy_hashlin_node *node = *tommy_hashlin_pos(&collection->fif_tasks, pos);
while (node) {
fif_task_mapping *data = node->data;
node = node->next;
process_fif_task_mapping(collection, data, &to_progress, procs, logger);
}
}
tommy_list progressed;
tommy_list_init(&progressed);
// Progress further on tasks; as far as able
fif_task_unify_loop(collection, &to_progress, &progressed, procs, logger);
// Incorporates tasks from progressed list back into task mapping
tommy_node *curr = tommy_list_head(&progressed);
while (curr) {
fif_task *data = curr->data;
curr = curr->next;
alma_function *f = fif_access(data->fif, data->premises_done);
char *name = name_with_arity(f->name, f->term_count);
fif_task_mapping *result = tommy_hashlin_search(&collection->fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL)
tommy_list_insert_tail(&result->tasks, &data->node, data);
free(name);
}
}
// Reorders clause array to begin with fifs; for correctness of task generation
void fif_to_front(tommy_array *clauses) {
tommy_size_t loc = 0;
for (tommy_size_t i = 0; i < tommy_array_size(clauses); i++) {
clause *c = tommy_array_get(clauses, i);
if (c->tag == FIF) {
if (loc != i) {
clause *tmp = tommy_array_get(clauses, loc);
tommy_array_set(clauses, loc, c);
tommy_array_set(clauses, i, tmp);
}
loc++;
}
}
}
void free_fif_mapping(void *arg) {
fif_mapping *entry = arg;
free(entry->indexing_conc_name);
free(entry->clauses);
// Note: clause entries ARE NOT FREED because they alias the clause objects freed in kb_halt
free(entry);
}
void free_fif_task(fif_task *task) {
// Note: clause entries ARE NOT FREED because they alias the clause objects freed in kb_halt
cleanup_bindings(task->bindings);
free(task->unified_clauses);
free(task->to_unify);
free(task);
}
void free_fif_task_mapping(void *arg) {
fif_task_mapping *entry = arg;
free(entry->predname);
tommy_node *curr = tommy_list_head(&entry->tasks);
while (curr) {
fif_task *data = curr->data;
curr = curr->next;
free_fif_task(data);
}
free(entry);
}
// Accesses the literal in position i of fif clause c
alma_function* fif_access(clause *c, int i) {
int next = c->fif->ordering[i];
if (next < 0)
return c->neg_lits[-next - 1];
else
return c->pos_lits[next];
}
// Deletes all fif tasks using c
void remove_fif_tasks(tommy_hashlin *fif_tasks, clause *c) {
for (int i = 0; i < c->fif->premise_count; i++) {
alma_function *f = fif_access(c, i);
char *name = name_with_arity(f->name, f->term_count);
fif_task_mapping *tm = tommy_hashlin_search(fif_tasks, fif_taskm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
if (currdata->fif->index == c->index) {
tommy_list_remove_existing(&tm->tasks, &currdata->node);
free_fif_task(currdata);
}
}
}
free(name);
}
}
// Used in removing a singleton clause -- remove partial fif tasks involving the clause
void remove_fif_singleton_tasks(tommy_hashlin *fif_tasks, tommy_hashlin *fif_map, clause *c) {
int compare_pos = c->neg_count == 1;
char *cname = compare_pos ? c->neg_lits[0]->name : c->pos_lits[0]->name;
int cterms = compare_pos ? c->neg_lits[0]->term_count : c->pos_lits[0]->term_count;
int count = 1;
char **names = malloc(sizeof(*names));
int prev_count = 0;
char **prev_names = NULL;
// Always process mapping for singleton's clause
names[0] = compare_pos ? name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count) : name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
// Check if fif tasks exist singleton
if (tommy_hashlin_search(fif_tasks, fif_taskm_compare, names[0], tommy_hash_u64(0, names[0], strlen(names[0]))) != NULL) {
// Check all fif clauses for containing premise matching c
tommy_size_t bucket_max = fif_map->low_max + fif_map->split;
for (tommy_size_t pos = 0; pos < bucket_max; ++pos) {
tommy_hashlin_node *node = *tommy_hashlin_pos(fif_map, pos);
while (node) {
fif_mapping *data = node->data;
node = node->next;
for (int i = 0; i < data->num_clauses; i++) {
for (int j = 0; j < data->clauses[i]->fif->premise_count; j++) {
alma_function *premise = fif_access(data->clauses[i], j);
// For each match, collect premises after singleton's location
if (strcmp(premise->name, cname) == 0 && premise->term_count == cterms) {
if (j > 0) {
prev_count++;
prev_names = realloc(prev_names, sizeof(*prev_names) * prev_count);
alma_function *pf = fif_access(data->clauses[i], j-1);
prev_names[prev_count-1] = name_with_arity(pf->name, pf->term_count);
}
names = realloc(names, sizeof(*names) * (count + data->clauses[i]->fif->premise_count - j- 1));
for (int k = j+1; k < data->clauses[i]->fif->premise_count; k++) {
premise = fif_access(data->clauses[i], k);
names[count] = name_with_arity(premise->name, premise->term_count);
count++;
}
break;
}
}
}
}
}
for (int i = 0; i < count; i++) {
fif_task_mapping *tm = tommy_hashlin_search(fif_tasks, fif_taskm_compare, names[i], tommy_hash_u64(0, names[i], strlen(names[i])));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
// Remove to_unify list entries of current clause, which is first in names array
if (i == 0) {
for (int j = 0; j < currdata->num_to_unify; j++) {
if (currdata->to_unify[j]->index == c->index) {
currdata->num_to_unify--;
currdata->to_unify[j] = currdata->to_unify[currdata->num_to_unify];
currdata->to_unify = realloc(currdata->to_unify, sizeof(*currdata->to_unify)*currdata->num_to_unify);
break;
}
}
}
// Otherwise, delete partial fif tasks that have unfied with current clause
else {
for (int j = 0; j < currdata->num_unified; j++) {
if (currdata->unified_clauses[j]->index == c->index) {
tommy_list_remove_existing(&tm->tasks, &currdata->node);
free_fif_task(currdata);
break;
}
}
}
}
}
free(names[i]);
}
free(names);
// fif_task_mappings have tasks deleted if they have unified with c
for (int i = 0; i < prev_count; i++) {
fif_task_mapping *tm = tommy_hashlin_search(fif_tasks, fif_taskm_compare, prev_names[i], tommy_hash_u64(0, prev_names[i], strlen(prev_names[i])));
if (tm != NULL) {
tommy_node *curr = tommy_list_head(&tm->tasks);
while (curr) {
fif_task *currdata = curr->data;
curr = curr->next;
for (int j = 0; j < currdata->num_unified; j++) {
if (currdata->unified_clauses[j]->index == c->index) {
tommy_list_remove_existing(&tm->tasks, &currdata->node);
free_fif_task(currdata);
}
}
}
}
free(prev_names[i]);
}
free(prev_names);
}
else {
// Doesn't appear in fif tasks, no need to check
free(names[0]);
free(names);
}
}
// Compare function to be used by tommy_hashlin_search for fif_mapping
// compares string arg to indexing_conc_name of fif_mapping
int fifm_compare(const void *arg, const void *obj) {
return strcmp((const char*)arg, ((const fif_mapping*)obj)->indexing_conc_name);
}
// Compare function to be used by tommy_hashlin_search for fif_task_mapping
// Compares string arg to predname of fif_task_mapping
int fif_taskm_compare(const void *arg, const void *obj) {
return strcmp((const char*)arg, ((const fif_task_mapping*)obj)->predname);
}