-
Notifications
You must be signed in to change notification settings - Fork 3
/
alma_kb.c
1639 lines (1454 loc) · 64.5 KB
/
alma_kb.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "alma_kb.h"
#include "alma_backsearch.h"
#include "alma_fif.h"
#include "alma_proc.h"
void kb_init(kb* collection, char *name, int verbose, int nesting_depth) {
collection->name = malloc(strlen(name)+1);
strcpy(collection->name, name);
collection->variable_id_count = 0;
collection->next_index = 0;
collection->verbose = verbose;
collection->nesting_depth = nesting_depth;
tommy_list_init(&collection->clauses);
tommy_hashlin_init(&collection->index_map);
tommy_hashlin_init(&collection->fif_map);
tommy_hashlin_init(&collection->pos_map);
tommy_list_init(&collection->pos_list);
tommy_hashlin_init(&collection->neg_map);
tommy_list_init(&collection->neg_list);
tommy_array_init(&collection->new_clauses);
tommy_array_init(&collection->timestep_delay_clauses);
tommy_array_init(&collection->distrust_set);
tommy_array_init(&collection->distrust_parents);
tommy_array_init(&collection->handle_set);
tommy_array_init(&collection->handle_parents);
tommy_array_init(&collection->retire_set);
tommy_array_init(&collection->retire_parents);
tommy_array_init(&collection->pos_lit_reinstates);
tommy_array_init(&collection->neg_lit_reinstates);
tommy_array_init(&collection->trues);
tommy_array_init(&collection->res_tasks);
tommy_hashlin_init(&collection->fif_tasks);
collection->agent_count = 0;
collection->agents = NULL;
}
static void kb_print_rec(kb *collection, int indent, kb_logger *logger, int print_name) {
char *spacing = malloc(indent+1);
memset(spacing, ' ', indent);
spacing[indent] = '\0';
tommy_node *curr = tommy_list_head(&collection->clauses);
while (curr) {
index_mapping *data = curr->data;
if (collection->verbose || data->value->dirty_bit) {
if (print_name)
tee_alt("%s%s%ld: ", logger, spacing, collection->name, data->key);
else
tee_alt("%s%ld: ", logger, spacing, data->key);
clause_print(data->value, logger);
tee_alt("\n", logger);
}
curr = curr->next;
}
if (collection->agent_count > 0)
tee_alt("%sAgent belief models:\n", logger, spacing);
for (int i = 0; i < collection->agent_count; i++) {
tee_alt("%s %s positive:\n", logger, spacing, collection->agents[i].pos->name);
kb_print_rec(collection->agents[i].pos, indent+2, logger, 1);
tee_alt("%s %s negative:\n", logger, spacing, collection->agents[i].neg->name);
kb_print_rec(collection->agents[i].neg, indent+2, logger, 1);
}
free(spacing);
}
// Top-level doesn't print name; recursive calls do
void kb_print(kb *collection, int indent, kb_logger *logger) {
kb_print_rec(collection, indent, logger, 0);
}
static void free_predname_mapping(void *arg) {
predname_mapping *entry = arg;
free(entry->predname);
free(entry->clauses);
// Note: clauses are not freed because they alias the clause objects freed in kb_halt
free(entry);
}
void kb_halt(kb *collection) {
free(collection->name);
for (tommy_size_t i = 0; i < tommy_array_size(&collection->new_clauses); i++)
free_clause(tommy_array_get(&collection->new_clauses, i));
tommy_array_done(&collection->new_clauses);
for (tommy_size_t i = 0; i < tommy_array_size(&collection->timestep_delay_clauses); i++)
free_clause(tommy_array_get(&collection->timestep_delay_clauses, i));
tommy_array_done(&collection->timestep_delay_clauses);
tommy_array_done(&collection->distrust_set);
tommy_array_done(&collection->distrust_parents);
tommy_array_done(&collection->handle_set);
tommy_array_done(&collection->handle_parents);
tommy_array_done(&collection->retire_set);
tommy_array_done(&collection->retire_parents);
tommy_array_done(&collection->pos_lit_reinstates);
tommy_array_done(&collection->neg_lit_reinstates);
tommy_array_done(&collection->trues);
tommy_node *curr = tommy_list_head(&collection->clauses);
while (curr) {
index_mapping *data = curr->data;
curr = curr->next;
free_clause(data->value);
free(data);
}
tommy_hashlin_done(&collection->index_map);
tommy_list_foreach(&collection->pos_list, free_predname_mapping);
tommy_hashlin_done(&collection->pos_map);
tommy_list_foreach(&collection->neg_list, free_predname_mapping);
tommy_hashlin_done(&collection->neg_map);
tommy_hashlin_foreach(&collection->fif_map, free_fif_mapping);
tommy_hashlin_done(&collection->fif_map);
// Res task pointers are aliases to those freed from clauses, so only free overall task here
for (tommy_size_t i = 0; i < tommy_array_size(&collection->res_tasks); i++)
free(tommy_array_get(&collection->res_tasks, i));
tommy_array_done(&collection->res_tasks);
tommy_hashlin_foreach(&collection->fif_tasks, free_fif_task_mapping);
tommy_hashlin_done(&collection->fif_tasks);
for (int i = 0; i < collection->agent_count; i++) {
kb_halt(collection->agents[i].pos);
kb_halt(collection->agents[i].neg);
}
free(collection->agents);
free(collection);
}
// Given a literal's function, checks for form bel(const_name, "...")
static int is_bel_literal(alma_function *lit) {
return strcmp(lit->name, "bel") == 0 && lit->term_count == 2 && lit->terms[0].type == FUNCTION
&& lit->terms[0].function->term_count == 0 && lit->terms[1].type == QUOTE && lit->terms[1].quote->type == CLAUSE;
}
// Syncs beliefs downward into argument KB's own agent model KBs
static void new_beliefs_to_agents(kb *collection) {
// Recursively sync into own agent models
if (collection->nesting_depth >= 1) {
for (tommy_size_t i = 0; i < tommy_array_size(&collection->new_clauses); i++) {
clause *c = tommy_array_get(&collection->new_clauses, i);
if (c->pos_count + c->neg_count == 1) {
int positive = (c->pos_count == 1);
alma_function *lit = positive ? c->pos_lits[0] : c->neg_lits[0];
if (is_bel_literal(lit)) {
char *agent_name = lit->terms[0].function->name;
// Only model agent with name distinct from collection itself
if (strcmp(agent_name, collection->name) != 0) {
agent_kb *agent = NULL;
// Search for matching agent KB
for (int j = 0; j < collection->agent_count; j++) {
if (strcmp(collection->agents[j].pos->name, agent_name) == 0) {
agent = collection->agents + j;
break;
}
}
// If a matching agent KB doesn't exist, create it
if (agent == NULL) {
collection->agent_count++;
collection->agents = realloc(collection->agents, sizeof(*collection->agents) * collection->agent_count);
agent = collection->agents + (collection->agent_count-1);
agent->pos = malloc(sizeof(*agent->pos));
kb_init(agent->pos, agent_name, collection->verbose, collection->nesting_depth-1);
agent->neg = malloc(sizeof(*agent->neg));
kb_init(agent->neg, agent_name, collection->verbose, collection->nesting_depth-1);
}
// Create copy of quoted belief, add to agent's appropriate KB
clause *unquoted = malloc(sizeof(*unquoted));
copy_clause_structure(lit->terms[1].quote->clause_quote, unquoted);
adjust_clause_context(unquoted, 1, 0);
// Initialize equivalence links for pair
unquoted->equiv_bel_up = c;
c->equiv_bel_down = unquoted;
if (positive) {
set_variable_ids(unquoted, 1, 0, NULL, &agent->pos->variable_id_count);
tommy_array_insert(&agent->pos->new_clauses, unquoted);
}
else {
set_variable_ids(unquoted, 1, 0, NULL, &agent->neg->variable_id_count);
tommy_array_insert(&agent->neg->new_clauses, unquoted);
}
}
}
}
}
for (int i = 0; i < collection->agent_count; i++) {
new_beliefs_to_agents(collection->agents[i].pos);
new_beliefs_to_agents(collection->agents[i].neg);
}
}
}
static clause* make_meta_literal(kb *collection, char *predname, clause *c, long time);
// For each new formula in new_clauses of agent kb, makes equivalent bel(...) formula for core kb
static void belief_sync_upward(kb *agent, int positive, kb *core) {
for (tommy_size_t i = 0; i < tommy_array_size(&agent->new_clauses); i++) {
clause *c = tommy_array_get(&agent->new_clauses, i);
if (c->equiv_bel_up == NULL) {
clause *bel = make_meta_literal(agent, "bel", c, 0);
// Restructure with quote as second arg, and first as agent name
free_function(bel->pos_lits[0]->terms[1].function);
bel->pos_lits[0]->terms[1].type = QUOTE;
bel->pos_lits[0]->terms[1].quote = bel->pos_lits[0]->terms[0].quote;
bel->pos_lits[0]->terms[0].type = FUNCTION;
bel->pos_lits[0]->terms[0].function = malloc(sizeof(*bel->pos_lits[0]->terms[0].function));
bel->pos_lits[0]->terms[0].function->name = malloc(strlen(agent->name)+1);
strcpy(bel->pos_lits[0]->terms[0].function->name, agent->name);
bel->pos_lits[0]->terms[0].function->term_count = 0;
bel->pos_lits[0]->terms[0].function->terms = NULL;
// Make literal negated if necessary
if (!positive) {
bel->neg_count = bel->pos_count;
bel->pos_count = 0;
bel->neg_lits = bel->pos_lits;
bel->pos_lits = NULL;
}
// Initialize equivalence links for pair
bel->equiv_bel_down = c;
c->equiv_bel_up = bel;
set_variable_ids(bel, 1, 0, NULL, &core->variable_id_count);
tommy_array_insert(&core->new_clauses, bel);
}
}
}
// Syncs beliefs upward from argument KB's own agent model KBs
static void new_beliefs_from_agents(kb *collection) {
for (int i = 0; i < collection->agent_count; i++) {
new_beliefs_from_agents(collection->agents[i].pos);
new_beliefs_from_agents(collection->agents[i].neg);
}
for (int i = 0; i < collection->agent_count; i++) {
belief_sync_upward(collection->agents[i].pos, 1, collection);
belief_sync_upward(collection->agents[i].neg, 0, collection);
}
}
// Returns a mapping holding a set of clauses that contains those unifiable with clause c
// Return type is a fif_mapping or predname_mapping depending on c's tag
// For predname_mapping, looks up mapping for a predicate appearing as a literal in clause c;
// currently, simply picks the first positive or negative literal
void* clause_lookup(kb *collection, clause *c) {
if (c->tag == FIF) {
char *name = c->fif->indexing_conc->name;
return tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
}
else {
tommy_hashlin *map = &collection->pos_map;
alma_function *pred;
if (c->pos_count != 0) {
pred = c->pos_lits[0];
}
else {
pred = c->neg_lits[0];
map = &collection->neg_map;
}
char *name = name_with_arity(pred->name, pred->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
return result;
}
}
// Checks if x and y are ground literals that are identical
// Currently does not check within quotes; any quotation term found returns 0
static int ground_duplicate_literals(alma_function *x, alma_function *y) {
if (strcmp(x->name, y->name) == 0 && x->term_count == y->term_count) {
for (int i = 0; i < x->term_count; i++)
if (x->terms[i].type != y->terms[i].type || x->terms[i].type == VARIABLE || x->terms[i].type == QUOTE
|| (x->terms[i].type == FUNCTION && !ground_duplicate_literals(x->terms[i].function, y->terms[i].function)))
return 0;
return 1;
}
return 0;
}
static void remove_dupe_literals(int *count, alma_function ***triple) {
alma_function **lits = *triple;
int deleted = 0;
int new_count = *count;
for (int i = 0; i < *count-1; i++) {
if (ground_duplicate_literals(lits[i], lits[i+1])) {
new_count--;
deleted = 1;
free_function(lits[i]);
lits[i] = NULL;
}
}
if (deleted) {
// Collect non-null literals at start of set
int loc = 1;
for (int i = 0; i < *count-1; i++) {
if (lits[i] == NULL) {
loc = i+1;
while (loc < *count && lits[loc] == NULL)
loc++;
if (loc >= *count)
break;
lits[i] = lits[loc];
lits[loc] = NULL;
}
}
*triple = realloc(lits, sizeof(*lits) * new_count);
*count = new_count;
}
}
// If c is found to be a clause's duplicate, returns a pointer to that clause; null otherwise
// See comments on clauses_differ function for further detail
// Check_distrusted flag determines whether distrusted clauses are used for dupe comparison as well
clause* duplicate_check(kb *collection, long time, clause *c, int check_distrusted) {
if (c->tag == FIF) {
char *name = c->fif->indexing_conc->name;
fif_mapping *result = tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (c->tag == result->clauses[i]->tag && !clauses_differ(c, result->clauses[i], 0))
return result->clauses[i];
}
}
}
else {
// Check for ground literal duplicates, and alter clause to remove any found
// Necessary for dupe checking to work in such cases, even if side effects are wasted for a discarded duplicate
qsort(c->pos_lits, c->pos_count, sizeof(*c->pos_lits), function_compare);
qsort(c->neg_lits, c->neg_count, sizeof(*c->neg_lits), function_compare);
remove_dupe_literals(&c->pos_count, &c->pos_lits);
remove_dupe_literals(&c->neg_count, &c->neg_lits);
char *name;
tommy_hashlin *map;
// If clause has a positive literal, all duplicate candidates must have that same positive literal
// Arbitrarily pick first positive literal as one to use; may be able to do smarter literal choice later
if (c->pos_count > 0) {
map = &collection->pos_map;
name = name_with_arity(c->pos_lits[0]->name, c->pos_lits[0]->term_count);
}
else {
map = &collection->neg_map;
name = name_with_arity(c->neg_lits[0]->name, c->neg_lits[0]->term_count);
}
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
free(name);
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
// A clause distrusted this timestep is still checked against
if ((check_distrusted || flags_negative(result->clauses[i]) || flag_min(result->clauses[i]) == time) &&
c->tag == result->clauses[i]->tag && !clauses_differ(c, result->clauses[i], 0))
return result->clauses[i];
}
}
}
return NULL;
}
// Inserts clause into the hashmap (with key based on lit), as well as the linked list
// If the map entry already exists, append; otherwise create a new one
static void map_add_clause(tommy_hashlin *map, tommy_list *list, alma_function *lit, clause *c) {
char *name = name_with_arity(lit->name, lit->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
result->num_clauses++;
result->clauses = realloc(result->clauses, sizeof(*result->clauses) * result->num_clauses);
result->clauses[result->num_clauses-1] = c; // Aliases with pointer assignment
free(name); // Name not added into hashmap must be freed
}
else {
predname_mapping *entry = malloc(sizeof(*entry));
entry->predname = name;
entry->num_clauses = 1;
entry->clauses = malloc(sizeof(*entry->clauses));
entry->clauses[0] = c; // Aliases with pointer assignment
tommy_hashlin_insert(map, &entry->hash_node, entry, tommy_hash_u64(0, entry->predname, strlen(entry->predname)));
tommy_list_insert_tail(list, &entry->list_node, entry);
}
}
// Removes clause from hashmap and list if it exists
static void map_remove_clause(tommy_hashlin *map, tommy_list *list, alma_function *lit, clause *c) {
char *name = name_with_arity(lit->name, lit->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
for (int i = 0; i < result->num_clauses; i++) {
if (result->clauses[i] == c) {
if (i < result->num_clauses-1)
result->clauses[i] = result->clauses[result->num_clauses-1];
result->num_clauses--;
// Shrink size of clause list of result
if (result->num_clauses > 0) {
result->clauses = realloc(result->clauses, sizeof(*result->clauses) * result->num_clauses);
}
// Remove predname_mapping from hashmap and linked list
else {
tommy_hashlin_remove_existing(map, &result->hash_node);
tommy_list_remove_existing(list, &result->list_node);
free_predname_mapping(result);
free(name);
return;
}
}
}
}
free(name);
}
// Returns first alma_function pointer for literal with given name in clause
// Searching positive or negative literals is decided by pos boolean
static alma_function* literal_by_name(clause *c, char *name, int pos) {
if (c != NULL) {
int count = pos ? c->pos_count : c->neg_count;
alma_function **lits = pos ? c->pos_lits : c->neg_lits;
for (int i = 0; i < count; i++)
if (strcmp(name, lits[i]->name) == 0)
return lits[i];
}
return NULL;
}
void make_single_task(clause *c, alma_function *c_lit, clause *other, tommy_array *tasks, int use_bif, int pos) {
if (c != other && (other->tag != BIF || use_bif) && other->tag != FIF) {
alma_function *other_lit = literal_by_name(other, c_lit->name, pos);
if (other_lit != NULL && other_lit != c_lit) {
res_task *t = malloc(sizeof(*t));
if (!pos) {
t->x = c;
t->pos = c_lit;
t->y = other;
t->neg = other_lit;
}
else {
t->x = other;
t->pos = other_lit;
t->y = c;
t->neg = c_lit;
}
tommy_array_insert(tasks, t);
}
}
}
// Helper of res_tasks_from_clause
void make_res_tasks(clause *c, int count, alma_function **c_lits, tommy_hashlin *map, tommy_array *tasks, int use_bif, int pos) {
for (int i = 0; i < count; i++) {
char *name = name_with_arity(c_lits[i]->name, c_lits[i]->term_count);
predname_mapping *result = tommy_hashlin_search(map, pm_compare, name, tommy_hash_u64(0, name, strlen(name)));
// New tasks are from Cartesian product of result's clauses with clauses' ith
if (result != NULL)
for (int j = 0; j < result->num_clauses; j++)
if (flags_negative(result->clauses[j]))
make_single_task(c, c_lits[i], result->clauses[j], tasks, use_bif, pos);
free(name);
}
}
// Finds new res tasks based on matching pos/neg predicate pairs, where one is from the KB and the other from arg
// Tasks are added into the res_tasks of collection
// Used only for non-bif resolution tasks; hence checks tag of c
void res_tasks_from_clause(kb *collection, clause *c, int process_negatives) {
if (c->tag != BIF && c->tag != FIF) {
make_res_tasks(c, c->pos_count, c->pos_lits, &collection->neg_map, &collection->res_tasks, 0, 0);
// Only done if clauses differ from KB's clauses (i.e. after first task generation)
if (process_negatives)
make_res_tasks(c, c->neg_count, c->neg_lits, &collection->pos_map, &collection->res_tasks, 0, 1);
}
}
// Resolve helper
static void lits_copy(int count, alma_function **lits, alma_function **cmp, binding_list *mgu, alma_function **res_lits, int *res_count) {
for (int i = 0; i < count; i++) {
// In calls cmp can be assigned to resolvent to not copy it
if (cmp == NULL || lits[i] != *cmp) {
res_lits[*res_count] = malloc(sizeof(*res_lits[*res_count]));
copy_alma_function(lits[i], res_lits[*res_count]);
(*res_count)++;
}
}
}
// Given an MGU, make a single resulting clause without unified literal pair, then substitute
static void resolve(res_task *t, binding_list *mgu, clause *result) {
result->pos_count = 0;
if (t->x->pos_count + t->y->pos_count - 1 > 0) {
result->pos_lits = malloc(sizeof(*result->pos_lits) * (t->x->pos_count + t->y->pos_count - 1));
// Copy positive literals from t->x and t->y
lits_copy(t->x->pos_count, t->x->pos_lits, &t->pos, mgu, result->pos_lits, &result->pos_count);
lits_copy(t->y->pos_count, t->y->pos_lits, NULL, mgu, result->pos_lits, &result->pos_count);
}
else
result->pos_lits = NULL;
result->neg_count = 0;
if (t->x->neg_count + t->y->neg_count - 1 > 0) {
result->neg_lits = malloc(sizeof(*result->neg_lits) * (t->x->neg_count + t->y->neg_count - 1));
// Copy negative literals from t->x and t->y
lits_copy(t->y->neg_count, t->y->neg_lits, &t->neg, mgu, result->neg_lits, &result->neg_count);
lits_copy(t->x->neg_count, t->x->neg_lits, NULL, mgu, result->neg_lits, &result->neg_count);
}
else
result->neg_lits = NULL;
result->tag = NONE;
result->fif = NULL;
subst_clause(mgu, result, 0);
}
// Places clause into term as clause_quote type of quote, with copy of structure
static void quote_from_clause(alma_term *t, clause *c) {
t->type = QUOTE;
t->quote = malloc(sizeof(*t->quote));
t->quote->type = CLAUSE;
t->quote->clause_quote = malloc(sizeof(*t->quote->clause_quote));
copy_clause_structure(c, t->quote->clause_quote);
}
static int derivations_distrusted(clause *c) {
for (int i = 0; i < c->parent_set_count; i++) {
int parent_distrusted = 0;
for (int j = 0; j < c->parents[i].count; j++) {
if (c->parents[i].clauses[j]->distrusted >= 0) {
parent_distrusted = 1;
break;
}
}
if (!parent_distrusted)
return 0;
}
return 1;
}
// Commonly used for special clauses like true, distrust, contra, etc.
static void init_single_parent(clause *child, clause *parent) {
if (parent != NULL) {
child->parent_set_count = 1;
child->parents = malloc(sizeof(*child->parents));
child->parents[0].count = 1;
child->parents[0].clauses = malloc(sizeof(*child->parents[0].clauses));
child->parents[0].clauses[0] = parent;
}
}
// Creates a meta-formula literal with binary arguments of quotation term for c and the time
static clause* make_meta_literal(kb *collection, char *predname, clause *c, long time) {
clause *res = malloc(sizeof(*res));
res->pos_count = 1;
res->neg_count = 0;
res->pos_lits = malloc(sizeof(*res->pos_lits));
res->pos_lits[0] = malloc(sizeof(*res->pos_lits[0]));
res->pos_lits[0]->name = malloc(strlen(predname)+1);
strcpy(res->pos_lits[0]->name, predname);
res->pos_lits[0]->term_count = 2;
res->pos_lits[0]->terms = malloc(sizeof(*res->pos_lits[0]->terms) * 2);
quote_from_clause(res->pos_lits[0]->terms+0, c);
func_from_long(res->pos_lits[0]->terms+1, time);
res->neg_lits = NULL;
res->parent_set_count = res->children_count = 0;
res->parents = NULL;
res->children = NULL;
res->equiv_bel_up = res->equiv_bel_down = NULL;
res->tag = NONE;
res->fif = NULL;
set_variable_ids(res, 1, 0, NULL, &collection->variable_id_count);
return res;
}
// Recursively apply pause to formula, its descendants, and down equivalence links
static void pause_recursive(clause *c, long time) {
c->paused = time;
c->dirty_bit = 1;
if (c->children != NULL) {
for (int i = 0; i < c->children_count; i++) {
if (c->children[i]->paused < 0) {
pause_recursive(c->children[i], time);
}
}
}
if (c->equiv_bel_down != NULL) {
pause_recursive(c->equiv_bel_down, time);
}
}
static void distrust_recursive(kb *collection, clause *c, clause *contra, long time, tommy_array *clauses) {
// Formula becomes distrusted at current time
c->distrusted = time;
if (c->tag == FIF)
remove_fif_tasks(&collection->fif_tasks, c);
// Assert atomic distrusted() formula
clause *d = make_meta_literal(collection, "distrusted", c, time);
init_single_parent(d, contra);
tommy_array_insert(clauses, d);
// Recursively distrust children
if (c->children != NULL) {
for (int i = 0; i < c->children_count; i++) {
if (c->children[i]->distrusted < 0 && derivations_distrusted(c->children[i])) {
distrust_recursive(collection, c->children[i], contra, time, clauses);
}
}
}
if (c->equiv_bel_down != NULL) {
clause *bel_down = c->equiv_bel_down;
// Recursively distrust equiv_bel_up versions of the children of equiv_bel_down
for (int i = 0; i < bel_down->children_count; i++) {
distrust_recursive(collection, bel_down->children[i]->equiv_bel_up, contra, time, clauses);
}
// Recursively pause following down equivalence links
pause_recursive(c->equiv_bel_down, time);
}
}
static void retire_recursive(kb *collection, clause *c, clause *contra) {
// TODO: appropriate retired-handling code to recurse through clause and descendants as needed
}
static void binding_subst(binding_list *target, binding_list *theta) {
for (int i = 0; i < target->num_bindings; i++)
subst_term(theta, target->list[i].term, 0);
}
static void make_contra(kb *collection, clause *contradictand_pos, clause *contradictand_neg, long time, tommy_array *clauses) {
clause *contra = malloc(sizeof(*contra));
contra->pos_count = 1;
contra->neg_count = 0;
contra->pos_lits = malloc(sizeof(*contra->pos_lits));
contra->pos_lits[0] = malloc(sizeof(*contra->pos_lits[0]));
contra->pos_lits[0]->name = malloc(strlen("contra_event")+1);
strcpy(contra->pos_lits[0]->name, "contra_event");
contra->pos_lits[0]->term_count = 3;
contra->pos_lits[0]->terms = malloc(sizeof(*contra->pos_lits[0]->terms) * 3);
quote_from_clause(contra->pos_lits[0]->terms+0, contradictand_pos);
quote_from_clause(contra->pos_lits[0]->terms+1, contradictand_neg);
func_from_long(contra->pos_lits[0]->terms+2, time);
contra->neg_lits = NULL;
contra->parent_set_count = contra->children_count = 0;
contra->parents = NULL;
contra->children = NULL;
contra->equiv_bel_up = contra->equiv_bel_down = NULL;
contra->tag = NONE;
contra->fif = NULL;
set_variable_ids(contra, 1, 0, NULL, &collection->variable_id_count);
tommy_array_insert(clauses, contra);
clause *contradicting = malloc(sizeof(*contradicting));
copy_clause_structure(contra, contradicting);
contradicting->pos_lits[0]->name = realloc(contradicting->pos_lits[0]->name, strlen("contradicting")+1);
strcpy(contradicting->pos_lits[0]->name, "contradicting");
init_single_parent(contradicting, contra);
set_variable_ids(contradicting, 1, 0, NULL, &collection->variable_id_count);
tommy_array_insert(clauses, contradicting);
tommy_array_insert(&collection->distrust_set, contradictand_pos);
tommy_array_insert(&collection->distrust_parents, contra);
tommy_array_insert(&collection->distrust_set, contradictand_neg);
tommy_array_insert(&collection->distrust_parents, contra);
}
static binding_list* parent_binding_prepare(backsearch_task *bs, long parent_index, binding_list *theta) {
// Retrieve parent existing bindings, if any
binding_mapping *mapping = NULL;
if (parent_index < 0)
mapping = tommy_hashlin_search(&bs->clause_bindings, bm_compare, &parent_index, tommy_hash_u64(0, &parent_index, sizeof(parent_index)));
// Substitute based on MGU obtained when unifying
if (mapping != NULL) {
binding_list *copy = malloc(sizeof(*copy));
copy_bindings(copy, mapping->bindings);
binding_subst(copy, theta);
return copy;
}
else
return NULL;
}
// Process resolution tasks from argument and place results in new_arr
void process_res_tasks(kb *collection, long time, tommy_array *tasks, tommy_array *new_arr, backsearch_task *bs, kb_logger *logger) {
for (tommy_size_t i = 0; i < tommy_array_size(tasks); i++) {
res_task *current_task = tommy_array_get(tasks, i);
if (current_task != NULL) {
// Does not do resolution with a distrusted clause
if (flags_negative(current_task->x) && flags_negative(current_task->y)) {
binding_list *theta = malloc(sizeof(*theta));
init_bindings(theta);
if (collection->verbose)
print_unify(current_task->pos, current_task->x->index, current_task->neg, current_task->y->index, logger);
// Given a res_task, attempt unification
if (pred_unify(current_task->pos, current_task->neg, theta, collection->verbose)) {
// If successful, create clause for result of resolution and add to new_clauses
clause *res_result = malloc(sizeof(*res_result));
resolve(current_task, theta, res_result);
binding_list *x_bindings = NULL;
if (bs) {
x_bindings = parent_binding_prepare(bs, current_task->x->index, theta);
binding_list *y_bindings = parent_binding_prepare(bs, current_task->y->index, theta);
if (x_bindings != NULL && y_bindings != NULL) {
// Check that tracked bindings for unified pair are compatible/unifiable
binding_list *parent_theta = malloc(sizeof(*parent_theta));
init_bindings(parent_theta);
int unify_fail = 0;
for (int j = 0; j < x_bindings->num_bindings; j++) {
if (!term_unify(x_bindings->list[j].term, y_bindings->list[j].term, parent_theta)) {
unify_fail = 1;
break;
}
}
cleanup_bindings(y_bindings);
if (unify_fail) {
cleanup_bindings(parent_theta);
cleanup_bindings(x_bindings);
if (res_result->pos_count > 0) {
for (int j = 0; j < res_result->pos_count; j++)
free_function(res_result->pos_lits[j]);
free(res_result->pos_lits);
}
if (res_result->neg_count > 0) {
for (int j = 0; j < res_result->neg_count; j++)
free_function(res_result->neg_lits[j]);
free(res_result->neg_lits);
}
free(res_result);
goto cleanup;
}
else {
// Use x_bindings with collected info as binding stuff
binding_subst(x_bindings, parent_theta);
// Apply parent_theta to resolution result as well
subst_clause(parent_theta, res_result, 0);
cleanup_bindings(parent_theta);
}
}
else if (x_bindings == NULL) {
// Consolidate to only x_bindings
x_bindings = y_bindings;
}
}
// A resolution result must be empty to be a valid clause to add to KB
if (res_result->pos_count > 0 || res_result->neg_count > 0) {
// Initialize parents of result
res_result->parent_set_count = 1;
res_result->parents = malloc(sizeof(*res_result->parents));
res_result->parents[0].count = 2;
res_result->parents[0].clauses = malloc(sizeof(*res_result->parents[0].clauses) * 2);
res_result->parents[0].clauses[0] = current_task->x;
res_result->parents[0].clauses[1] = current_task->y;
res_result->children_count = 0;
res_result->children = NULL;
res_result->equiv_bel_up = res_result->equiv_bel_down = NULL;
set_variable_ids(res_result, 0, 0, x_bindings, &collection->variable_id_count);
tommy_array_insert(new_arr, res_result);
if (bs)
tommy_array_insert(&bs->new_clause_bindings, x_bindings);
}
else {
free(res_result);
if (bs) {
clause *answer = malloc(sizeof(*answer));
memcpy(answer, bs->target, sizeof(*answer));
if (bs->target->pos_count == 1) {
answer->pos_lits = malloc(sizeof(*answer->pos_lits));
answer->pos_lits[0] = malloc(sizeof(*answer->pos_lits[0]));
copy_alma_function(bs->target->pos_lits[0], answer->pos_lits[0]);
for (int j = 0; j < answer->pos_lits[0]->term_count; j++)
subst_term(x_bindings, answer->pos_lits[0]->terms+j, 0);
}
else {
answer->neg_lits = malloc(sizeof(*answer->neg_lits));
answer->neg_lits[0] = malloc(sizeof(*answer->neg_lits[0]));
copy_alma_function(bs->target->neg_lits[0], answer->neg_lits[0]);
for (int j = 0; j < answer->neg_lits[0]->term_count; j++)
subst_term(x_bindings, answer->neg_lits[0]->terms+j, 0);
}
set_variable_ids(answer, 0, 0, NULL, &collection->variable_id_count);
// TODO: parent setup for backsearch answer?
tommy_array_insert(&collection->new_clauses, answer);
cleanup_bindings(x_bindings);
}
// If not a backward search, empty resolution result indicates a contradiction between clauses
else
make_contra(collection, current_task->x, current_task->y, time, &collection->new_clauses);
}
}
if (collection->verbose)
print_bindings(theta, 1, 1, logger);
cleanup:
cleanup_bindings(theta);
}
free(current_task);
}
}
tommy_array_done(tasks);
tommy_array_init(tasks);
}
// Given a new clause, add to the KB and maps
static void add_clause(kb *collection, clause *c, long time) {
// Add clause to overall clause list and index map
index_mapping *ientry = malloc(sizeof(*ientry));
c->index = ientry->key = collection->next_index++;
c->acquired = time;
c->distrusted = -1;
c->paused = -1;
c->retired = -1;
c->handled = -1;
c->dirty_bit = 1;
c->pyobject_bit = 1;
ientry->value = c;
tommy_list_insert_tail(&collection->clauses, &ientry->list_node, ientry);
tommy_hashlin_insert(&collection->index_map, &ientry->hash_node, ientry, tommy_hash_u64(0, &ientry->key, sizeof(ientry->key)));
if (c->tag == FIF) {
char *name = c->fif->indexing_conc->name;
// Index into fif hashmap
fif_mapping *result = tommy_hashlin_search(&collection->fif_map, fifm_compare, name, tommy_hash_u64(0, name, strlen(name)));
if (result != NULL) {
result->num_clauses++;
result->clauses = realloc(result->clauses, sizeof(*result->clauses)*result->num_clauses);
result->clauses[result->num_clauses-1] = c;
}
else {
fif_mapping *entry = malloc(sizeof(*entry));
entry->indexing_conc_name = malloc(strlen(name)+1);
strcpy(entry->indexing_conc_name, name);
entry->num_clauses = 1;
entry->clauses = malloc(sizeof(*entry->clauses));
entry->clauses[0] = c;
tommy_hashlin_insert(&collection->fif_map, &entry->node, entry, tommy_hash_u64(0, entry->indexing_conc_name, strlen(entry->indexing_conc_name)));
}
}
else {
// If non-fif, indexes clause into pos/neg hashmaps/lists
for (int j = 0; j < c->pos_count; j++)
map_add_clause(&collection->pos_map, &collection->pos_list, c->pos_lits[j], c);
for (int j = 0; j < c->neg_count; j++)
map_add_clause(&collection->neg_map, &collection->neg_list, c->neg_lits[j], c);
}
}
// Special semantic operator: true
// Must be a singleton positive literal with unary quote arg
// Process quoted material into new formulas with truth as parent
static void handle_true(kb *collection, clause *truth, kb_logger *logger) {
if (truth->pos_lits[0]->term_count == 1 && truth->pos_lits[0]->terms[0].type == QUOTE) {
alma_quote *quote = truth->pos_lits[0]->terms[0].quote;
tommy_array unquoted;
tommy_array_init(&unquoted);
// Raw sentence must be converted into clauses
if (quote->type == SENTENCE) {
alma_node *sentence_copy = cnf_copy(quote->sentence);
nodes_to_clauses(sentence_copy, 1, &unquoted, &collection->variable_id_count);
}
// Quote clause can be extracted directly
else {
clause *u = malloc(sizeof(*u));
copy_clause_structure(quote->clause_quote, u);
// True formula has its outermost quotation withdrawn
adjust_clause_context(u, 1, 0);
// Adjust variable IDs for the new formula
set_variable_ids(u, 1, 0, NULL, &collection->variable_id_count);
tommy_array_insert(&unquoted, u);
}
for (int i = 0; i < tommy_array_size(&unquoted); i++) {
clause *curr = tommy_array_get(&unquoted, i);
init_single_parent(curr, truth);
tommy_array_insert(&collection->timestep_delay_clauses, curr);
}
tommy_array_done(&unquoted);
}
}
// Special semantic operator: distrust
// If argument unifies with a formula in the KB, derives distrusted
// Distrusted formulas cannot be more general than argument
// Hence, clauses found are placed in a quotation term with no added quasi-quotation
static void handle_distrust(kb *collection, clause *distrust) {
alma_function *lit = distrust->pos_lits[0];
if (lit->term_count == 1 && lit->terms[0].type == QUOTE && lit->terms[0].quote->type == CLAUSE) {
void *mapping = clause_lookup(collection, lit->terms[0].quote->clause_quote);
if (mapping != NULL) {
alma_quote *q = malloc(sizeof(*q));
q->type = CLAUSE;
for (int i = mapping_num_clauses(mapping, lit->terms[0].quote->clause_quote->tag)-1; i >= 0; i--) {
clause *ith = mapping_access(mapping, lit->terms[0].quote->clause_quote->tag, i);
if (flags_negative(ith) && counts_match(ith, lit->terms[0].quote->clause_quote)) {
q->clause_quote = ith;
binding_list *theta = malloc(sizeof(*theta));
init_bindings(theta);
// Distrust each match found
if (quote_term_unify(lit->terms[0].quote, q, theta)) {
tommy_array_insert(&collection->distrust_set, ith);
tommy_array_insert(&collection->distrust_parents, distrust);
}
cleanup_bindings(theta);
}
}
free(q);
}
}
}
static int all_digits(char *str) {
for (int i = 0; i < strlen(str); i++)
if (!isdigit(str[i]))
return 0;
return 1;
}
// Recursively removes pause from formula, its descendants, and down equivalence links
static void unpause_recursive(clause *c, tommy_array *unpaused) {
c->paused = -1;
c->dirty_bit = 1;
tommy_array_insert(unpaused, c);
if (c->children != NULL) {
for (int i = 0; i < c->children_count; i++) {
if (c->children[i]->paused > 0) {
unpause_recursive(c->children[i], unpaused);
}
}
}
if (c->equiv_bel_down != NULL) {
unpause_recursive(c->equiv_bel_down, unpaused);
}
}
// Special semantic operator: reinstate
// Reinstatement succeeds if given args of a quote matching distrusted formula(s) and a matching timestep for when distrusted
static void handle_reinstate(kb *collection, clause *reinstate, long time, tommy_array *unpaused) {
alma_term *arg1 = reinstate->pos_lits[0]->terms+0;
alma_term *arg2 = reinstate->pos_lits[0]->terms+1;
void *mapping = clause_lookup(collection, arg1->quote->clause_quote);
if (mapping != NULL) {
alma_quote *q = malloc(sizeof(*q));
q->type = CLAUSE;
for (int i = mapping_num_clauses(mapping, arg1->quote->clause_quote->tag)-1; i >= 0; i--) {
clause *ith = mapping_access(mapping, arg1->quote->clause_quote->tag, i);
if (ith->distrusted == atol(arg2->function->name) && counts_match(ith, arg1->quote->clause_quote)) {