-
Notifications
You must be signed in to change notification settings - Fork 4
/
datalog.h
13011 lines (12289 loc) · 534 KB
/
datalog.h
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
/**
* datalog.h
*
* Created on: Jul 1, 2016
* Author: asaparov
*/
#ifndef DATALOG_H_
#define DATALOG_H_
#include <core/lex.h>
#include <grammar/morphology.h>
#include <limits.h>
#include <type_traits>
#include <atomic>
#include <cctype>
using namespace core;
constexpr unsigned int DATALOG_LABEL_WILDCARD = UINT_MAX;
constexpr unsigned int DATALOG_LABEL_EMPTY = UINT_MAX - 1;
constexpr unsigned int DATALOG_LABEL_NOT = UINT_MAX - 2;
constexpr char DATALOG_STRING_LABEL_WILDCARD = (char) -1;
constexpr unsigned int PREDICATE_ANSWER = 1;
constexpr unsigned int PREDICATE_COUNT = 2;
constexpr unsigned int PREDICATE_SUM = 3;
constexpr unsigned int PREDICATE_HIGHEST = 4;
constexpr unsigned int PREDICATE_LOWEST = 5;
constexpr unsigned int PREDICATE_HEIGHT = 6;
constexpr unsigned int PREDICATE_LONGEST = 7;
constexpr unsigned int PREDICATE_SHORTEST = 8;
constexpr unsigned int PREDICATE_LENGTH = 9;
constexpr unsigned int PREDICATE_LARGEST = 10;
constexpr unsigned int PREDICATE_SMALLEST = 11;
constexpr unsigned int PREDICATE_MOST = 12;
constexpr unsigned int PREDICATE_FEWEST = 13;
constexpr unsigned int PREDICATE_NOT = 14;
constexpr unsigned int NUM_PREDICATES = 14;
constexpr unsigned int PREDICATE_PARSE = 15;
constexpr unsigned int DIRECTION_FORWARD = 16;
constexpr unsigned int DIRECTION_BACKWARD = 17;
constexpr unsigned int DIRECTION_BOTH = 18;
constexpr unsigned int DIRECTION_SELF = 19;
constexpr unsigned int DIRECTION_NONE = 20;
constexpr unsigned int DIRECTION_PREDICATE = 21;
constexpr unsigned int DIRECTION_CONSTANT = 22;
constexpr unsigned int ARITY_ZERO = 23;
constexpr unsigned int ARITY_ONE = 24;
constexpr unsigned int ARITY_TWO = 25;
constexpr unsigned int ARITY_THREE = 26;
constexpr unsigned int ARITY_NULL = 27;
constexpr unsigned int ARG_OTHER = 28;
constexpr unsigned int DATALOG_TRUE = 29;
constexpr unsigned int PREDICATE_LOC = 30;
constexpr unsigned int PREDICATE_CONST = 31;
constexpr unsigned int PREDICATE_ATTR = 32;
constexpr unsigned int PREDICATE_OBJECT = 33;
constexpr unsigned int NUMBER_OFFSET = 34;
constexpr unsigned int INFLECTION_OFFSET = 34 + grammatical_number_count - 1;
enum datalog_expression_type {
DATALOG_PREDICATE = INFLECTION_OFFSET + 1,
DATALOG_FUNCTION = INFLECTION_OFFSET + 2,
DATALOG_TUPLE = INFLECTION_OFFSET + 3,
DATALOG_LIST = INFLECTION_OFFSET + 4,
DATALOG_VARIABLE = INFLECTION_OFFSET + 5,
DATALOG_CONSTANT = INFLECTION_OFFSET + 6,
DATALOG_INTEGER = INFLECTION_OFFSET + 7,
DATALOG_STRING = INFLECTION_OFFSET + 8,
DATALOG_EMPTY = INFLECTION_OFFSET + 9,
DATALOG_ANY = INFLECTION_OFFSET + 10,
DATALOG_NON_EMPTY = INFLECTION_OFFSET + 11
};
struct datalog_token_scribe { };
template<typename Stream>
inline bool print(const string& str, Stream& out, const datalog_token_scribe& printer) {
for (unsigned int i = 0; i < str.length; i++) {
if (std::isspace(str[i]) || str[i] == '.') {
return print('\'', out) && print(str, out) && print('\'', out);
}
}
return print(str, out);
}
/**
* Code for tokenizing/lexing Datalog data.
*/
bool populate_name_map(hash_map<string, unsigned int>& names) {
bool success = true;
success &= names.put("parse", PREDICATE_PARSE);
success &= names.put("answer", PREDICATE_ANSWER);
success &= names.put("count", PREDICATE_COUNT);
success &= names.put("sum", PREDICATE_SUM);
success &= names.put("highest", PREDICATE_HIGHEST);
success &= names.put("lowest", PREDICATE_LOWEST);
success &= names.put("height", PREDICATE_HEIGHT);
success &= names.put("longest", PREDICATE_LONGEST);
success &= names.put("shortest", PREDICATE_SHORTEST);
success &= names.put("length", PREDICATE_LENGTH);
success &= names.put("largest", PREDICATE_LARGEST);
success &= names.put("smallest", PREDICATE_SMALLEST);
success &= names.put("most", PREDICATE_MOST);
success &= names.put("fewest", PREDICATE_FEWEST);
success &= names.put("not", PREDICATE_NOT);
success &= names.put("direction_forward", DIRECTION_FORWARD);
success &= names.put("direction_backward", DIRECTION_BACKWARD);
success &= names.put("direction_both", DIRECTION_BOTH);
success &= names.put("direction_self", DIRECTION_SELF);
success &= names.put("direction_none", DIRECTION_NONE);
success &= names.put("direction_predicate", DIRECTION_PREDICATE);
success &= names.put("direction_constant", DIRECTION_CONSTANT);
success &= names.put("arity_zero", ARITY_ZERO);
success &= names.put("arity_unary", ARITY_ONE);
success &= names.put("arity_binary", ARITY_TWO);
success &= names.put("arity_three", ARITY_THREE);
success &= names.put("arity_null", ARITY_NULL);
success &= names.put("arg_other", ARG_OTHER);
success &= names.put("true", DATALOG_TRUE);
success &= names.put("loc", PREDICATE_LOC);
success &= names.put("const", PREDICATE_CONST);
success &= names.put("attr", PREDICATE_ATTR);
success &= names.put("object", PREDICATE_OBJECT);
success &= names.put("sg", NUMBER_OFFSET + NUMBER_SINGULAR - 1);
success &= names.put("pl", NUMBER_OFFSET + NUMBER_PLURAL - 1);
success &= names.put("uncountable", NUMBER_OFFSET + NUMBER_UNCOUNTABLE - 1);
success &= names.put("non_sg", NUMBER_OFFSET + NUMBER_NON_SINGULAR - 1);
success &= names.put("non_pl", NUMBER_OFFSET + NUMBER_NON_PLURAL - 1);
success &= names.put("all_numbers", NUMBER_OFFSET + NUMBER_ALL - 1);
success &= names.put("no_number", NUMBER_OFFSET + NUMBER_NONE - 1);
success &= names.put("past_participle", INFLECTION_OFFSET + INFLECTION_PAST_PARTICIPLE - 1);
success &= names.put("present_participle", INFLECTION_OFFSET + INFLECTION_PRESENT_PARTICIPLE - 1);
success &= names.put("infinitive", INFLECTION_OFFSET + INFLECTION_INFINITIVE - 1);
success &= names.put("default_tense", INFLECTION_OFFSET + INFLECTION_OTHER_VERB - 1);
success &= names.put("noun", INFLECTION_OFFSET + INFLECTION_NOUN - 1);
success &= names.put("adjective", INFLECTION_OFFSET + INFLECTION_ADJECTIVE - 1);
success &= names.put("comparative", INFLECTION_OFFSET + INFLECTION_COMPARATIVE - 1);
success &= names.put("superlative", INFLECTION_OFFSET + INFLECTION_SUPERLATIVE - 1);
success &= names.put("no_inflection", INFLECTION_OFFSET + INFLECTION_NONE - 1);
success &= names.put("type_predicate", DATALOG_PREDICATE);
success &= names.put("type_function", DATALOG_FUNCTION);
success &= names.put("type_tuple", DATALOG_TUPLE);
success &= names.put("type_list", DATALOG_LIST);
success &= names.put("type_variable", DATALOG_VARIABLE);
success &= names.put("type_constant", DATALOG_CONSTANT);
success &= names.put("type_integer", DATALOG_INTEGER);
success &= names.put("type_string", DATALOG_STRING);
success &= names.put("type_empty", DATALOG_EMPTY);
success &= names.put("type_any", DATALOG_ANY);
success &= names.put("type_non_empty", DATALOG_NON_EMPTY);
return success;
}
enum datalog_token_type {
DATALOG_TOKEN_LBRACKET,
DATALOG_TOKEN_RBRACKET,
DATALOG_TOKEN_LPAREN,
DATALOG_TOKEN_RPAREN,
DATALOG_TOKEN_COMMA,
DATALOG_TOKEN_PERIOD,
DATALOG_TOKEN_IDENTIFIER,
DATALOG_TOKEN_STRING,
DATALOG_TOKEN_SLASH_PLUS,
DATALOG_TOKEN_TAB
};
typedef lexical_token<datalog_token_type> datalog_token;
template<typename Stream>
inline bool print(datalog_token_type type, Stream& stream) {
switch (type) {
case DATALOG_TOKEN_LBRACKET:
return print('[', stream);
case DATALOG_TOKEN_RBRACKET:
return print(']', stream);
case DATALOG_TOKEN_LPAREN:
return print('(', stream);
case DATALOG_TOKEN_RPAREN:
return print(')', stream);
case DATALOG_TOKEN_COMMA:
return fprintf(stream, "COMMA") > 0;
case DATALOG_TOKEN_PERIOD:
return print('.', stream);
case DATALOG_TOKEN_IDENTIFIER:
return fprintf(stream, "IDENTIFIER") > 0;
case DATALOG_TOKEN_STRING:
return fprintf(stream, "STRING") > 0;
case DATALOG_TOKEN_SLASH_PLUS:
return fprintf(stream, "\\+") > 0;
case DATALOG_TOKEN_TAB:
return print('\t', stream);
}
fprintf(stderr, "print ERROR: Unknown datalog_token_type.\n");
return false;
}
enum datalog_lexer_state {
DATALOG_LEXER_START,
DATALOG_LEXER_IDENTIFIER,
DATALOG_LEXER_QUOTE,
DATALOG_LEXER_COMMENT
};
bool datalog_emit_symbol(array<datalog_token>& tokens, const position& start, char symbol) {
switch (symbol) {
case ',':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_COMMA);
case '.':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_PERIOD);
case '(':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_LPAREN);
case ')':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_RPAREN);
case '[':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_LBRACKET);
case ']':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_RBRACKET);
case '\t':
return emit_token(tokens, start, start + 1, DATALOG_TOKEN_TAB);
default:
fprintf(stderr, "datalog_emit_symbol ERROR: Unexpected symbol.\n");
return false;
}
}
template<bool TokenizeTabs = true>
bool datalog_lex(array<datalog_token>& tokens, FILE* input) {
position start = position(1, 1);
position current = position(1, 1);
datalog_lexer_state state = DATALOG_LEXER_START;
array<char> token = array<char>(1024);
int prev = 0, next = fgetc(input);
bool new_line = false;
bool found_tab = TokenizeTabs;
while (next != -1) {
switch (state) {
case DATALOG_LEXER_QUOTE:
if (next == '\\') {
/* escape character */
next = fgetc(input); current.column++;
if (next == -1) {
read_error("Unexpected end of stream", current);
return false;
}
if (!token.add(next)) return false;
} else if (next == '\'') {
if (!emit_token(tokens, token, start, current, DATALOG_TOKEN_STRING)) return false;
state = DATALOG_LEXER_START;
token.clear();
} else {
if (!token.add(next)) return false;
}
break;
case DATALOG_LEXER_IDENTIFIER:
if (next == ',' || next == '.' || next == '(' || next == ')'
|| next == '[' || next == ']' || (TokenizeTabs && next == '\t'))
{
if (TokenizeTabs && next == '\t')
found_tab = true;
if (!emit_token(tokens, token, start, current, DATALOG_TOKEN_IDENTIFIER)
|| !datalog_emit_symbol(tokens, current, next))
return false;
state = DATALOG_LEXER_START;
token.clear();
} else if (next == '\'') {
read_error("Unexpected quote after identifier", current);
return false;
} else if (next == '\\') {
read_error("Unexpected backslash after identifier", current);
return false;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
if (!emit_token(tokens, token, start, current, DATALOG_TOKEN_IDENTIFIER))
return false;
state = DATALOG_LEXER_START;
token.clear();
new_line = (next == '\n');
} else {
if (!token.add(next)) return false;
}
break;
case DATALOG_LEXER_START:
if (next == '\'' && found_tab) {
state = DATALOG_LEXER_QUOTE;
start = current;
} else if (next == ',' || next == '.' || next == '(' || next == ')'
|| next == '[' || next == ']' || (TokenizeTabs && next == '\t'))
{
if (!datalog_emit_symbol(tokens, current, next))
return false;
} else if (next == '\\') {
next = fgetc(input);
if (next != '+') {
read_error("Expected '+'", current);
return false;
} if (!emit_token(tokens, current, current + 1, DATALOG_TOKEN_SLASH_PLUS))
return false;
current.column++;
} else if (next == ' ' || next == '\t' || next == '\n' || next == '\r') {
new_line = (next == '\n');
} else if (next == '/') {
next = fgetc(input);
if (next != '*') {
read_error("Expected '*'", current);
return false;
}
state = DATALOG_LEXER_COMMENT;
} else {
if (!token.add(next)) return false;
state = DATALOG_LEXER_IDENTIFIER;
start = current;
}
break;
case DATALOG_LEXER_COMMENT:
if (prev == '*' && next == '/') {
state = DATALOG_LEXER_START;
} else if (next == '\n') {
new_line = true;
}
break;
}
if (new_line) {
current.line++;
current.column = 1;
found_tab = TokenizeTabs;
new_line = false;
} else current.column++;
prev = next;
next = fgetc(input);
}
if (state == DATALOG_LEXER_QUOTE) {
read_error("Expected closing quote", current);
return false;
} else if (state == DATALOG_LEXER_IDENTIFIER) {
return emit_token(tokens, token, start, current, DATALOG_TOKEN_IDENTIFIER);
}
return true;
}
/**
* Recursive-descent parser for tokenized Datalog data.
*/
struct datalog_expression;
bool datalog_interpret_expression(
const array<datalog_token>& tokens,
unsigned int& index,
datalog_expression& exp,
hash_map<string, unsigned int>& names,
hash_map<string, unsigned int>& variables);
inline bool exclude(unsigned int*& excluded, unsigned int& excluded_count,
const unsigned int* to_exclude, unsigned int to_exclude_count)
{
unsigned int* excluded_union = (unsigned int*) malloc(
sizeof(unsigned int) * (excluded_count + to_exclude_count));
if (excluded_union == NULL) {
fprintf(stderr, "exclude ERROR: Out of memory.\n");
return false;
}
unsigned int excluded_union_count = 0;
set_union(excluded_union, excluded_union_count, excluded, excluded_count, to_exclude, to_exclude_count);
if (excluded_union_count == 0) {
core::free(excluded_union);
return true;
} else if (excluded_count > 0)
core::free(excluded);
excluded = excluded_union;
excluded_count = excluded_union_count;
return true;
}
struct datalog_predicate {
static constexpr unsigned int ARG_COUNT = 3;
unsigned int function;
unsigned int* excluded;
unsigned int excluded_count;
datalog_expression* args[ARG_COUNT];
inline bool is_excluded(unsigned int value) const {
return index_of(value, excluded, excluded_count) < excluded_count;
}
inline bool ensure_excluded_capacity(unsigned int capacity) {
if (excluded_count == 0) excluded = NULL;
if (!resize(excluded, capacity)) {
fprintf(stderr, "datalog_predicate.ensure_excluded_capacity ERROR: Unable to expand excluded array.\n");
return false;
}
return true;
}
bool exclude(const unsigned int* items, unsigned int count) {
return ::exclude(excluded, excluded_count, items, count);
}
static inline void free(datalog_predicate& pred);
};
struct datalog_function {
unsigned int function;
unsigned int* excluded;
unsigned int excluded_count;
unsigned int vars[2];
datalog_expression* arg;
inline bool is_excluded(unsigned int value) const {
return index_of(value, excluded, excluded_count) < excluded_count;
}
inline bool ensure_excluded_capacity(unsigned int capacity) {
excluded = NULL;
if (!resize(excluded, capacity)) {
fprintf(stderr, "datalog_function.ensure_excluded_capacity ERROR: Unable to expand excluded array.\n");
return false;
}
return true;
}
bool exclude(const unsigned int* items, unsigned int count) {
return ::exclude(excluded, excluded_count, items, count);
}
static inline void free(datalog_function& func);
};
enum tuple_position {
POSITION_LEFT,
POSITION_RIGHT,
POSITION_EXACT
};
struct datalog_tuple {
array<datalog_expression*> elements;
tuple_position position;
static inline void free(datalog_tuple& tuple);
};
struct datalog_list {
array<datalog_expression*> elements;
static inline void free(datalog_list& list);
};
struct datalog_literal {
unsigned int label;
unsigned int* excluded;
unsigned int excluded_count;
inline bool is_excluded(unsigned int value) const {
return index_of(value, excluded, excluded_count) < excluded_count;
}
inline bool ensure_excluded_capacity(unsigned int capacity) {
excluded = NULL;
if (!resize(excluded, capacity)) {
fprintf(stderr, "datalog_function.ensure_excluded_capacity ERROR: Unable to expand excluded array.\n");
return false;
}
return true;
}
bool exclude(const unsigned int* items, unsigned int count) {
return ::exclude(excluded, excluded_count, items, count);
}
static inline void move(const datalog_literal& src, datalog_literal& dst) {
dst.label = src.label;
dst.excluded = src.excluded;
dst.excluded_count = src.excluded_count;
}
static inline unsigned int hash(const datalog_literal& key) {
unsigned int hash_value = default_hash(key.label);
if (key.excluded_count > 0)
hash_value ^= default_hash(key.excluded, key.excluded_count);
return hash_value;
}
static inline bool is_empty(const datalog_literal& key) {
return key.label == 0;
}
static inline void free(datalog_literal& literal);
};
inline bool operator < (const datalog_literal& first, const datalog_literal& second) {
if (first.label < second.label) return true;
else if (first.label > second.label) return false;
else if (first.excluded_count < second.excluded_count) return true;
else if (first.excluded_count > second.excluded_count) return false;
for (unsigned int i = 0; i < first.excluded_count; i++) {
if (first.excluded[i] < second.excluded[i]) return true;
else if (first.excluded[i] > second.excluded[i]) return false;
}
/* the literals are identical */
return false;
}
struct datalog_expression {
datalog_expression_type type;
unsigned int reference_count;
union {
datalog_predicate pred;
datalog_function func;
datalog_tuple tuple;
datalog_list list;
datalog_literal constant;
unsigned int variable;
int integer;
string str;
};
datalog_expression() : type(DATALOG_ANY), reference_count(1) { }
datalog_expression(datalog_expression_type type) : type(type), reference_count(1) { }
datalog_expression(const datalog_expression& src) : reference_count(1) {
if (!initialize(src))
exit(EXIT_FAILURE);
}
~datalog_expression() { free(); }
inline void operator = (const datalog_expression& src) {
reference_count = 1;
if (!initialize(src))
exit(EXIT_FAILURE);
}
inline void recompute_hash() const { }
static inline void move(const datalog_expression& src, datalog_expression& dst) {
dst.type = src.type;
dst.reference_count = src.reference_count;
switch (src.type) {
case DATALOG_PREDICATE:
dst.pred.function = src.pred.function;
dst.pred.excluded = src.pred.excluded;
dst.pred.excluded_count = src.pred.excluded_count;
for (unsigned int i = 0; i < array_length(src.pred.args); i++)
dst.pred.args[i] = src.pred.args[i];
return;
case DATALOG_FUNCTION:
dst.func.function = src.func.function;
dst.func.excluded = src.func.excluded;
dst.func.excluded_count = src.func.excluded_count;
dst.func.arg = src.func.arg;
for (unsigned int i = 0; i < array_length(src.func.vars); i++)
dst.func.vars[i] = src.func.vars[i];
return;
case DATALOG_TUPLE:
core::move(src.tuple.elements, dst.tuple.elements);
dst.tuple.position = src.tuple.position;
return;
case DATALOG_LIST:
core::move(src.list.elements, dst.list.elements); return;
case DATALOG_VARIABLE:
dst.variable = src.variable; return;
case DATALOG_CONSTANT:
dst.constant.label = src.constant.label;
dst.constant.excluded = src.constant.excluded;
dst.constant.excluded_count = src.constant.excluded_count;
return;
case DATALOG_INTEGER:
dst.integer = src.integer;
return;
case DATALOG_STRING:
string::move(src.str, dst.str);
return;
case DATALOG_EMPTY:
case DATALOG_ANY:
case DATALOG_NON_EMPTY:
return;
}
fprintf(stderr, "datalog_expression.move ERROR: Unrecognized expression type.\n");
exit(EXIT_FAILURE);
}
static inline void swap(datalog_expression& first, datalog_expression& second) {
char* first_data = (char*) &first;
char* second_data = (char*) &second;
for (unsigned int i = 0; i < sizeof(datalog_expression); i++)
core::swap(first_data[i], second_data[i]);
}
static inline bool is_empty(const datalog_expression& key) {
return key.reference_count == 0;
}
static inline unsigned int hash(const datalog_expression& key) {
unsigned int hash_value;
switch (key.type) {
case DATALOG_PREDICATE:
hash_value = default_hash(key.pred.function);
if (key.pred.excluded_count > 0)
hash_value ^= default_hash(key.pred.excluded, key.pred.excluded_count);
for (unsigned int i = 0; i < array_length(key.pred.args); i++) {
if (key.pred.args[i] == NULL) continue;
hash_value ^= hash(*key.pred.args[i]);
}
return 0 + 11 * hash_value;
case DATALOG_FUNCTION:
hash_value = default_hash(key.func.function);
if (key.func.excluded_count > 0)
hash_value ^= default_hash(key.func.excluded, key.func.excluded_count);
if (key.func.arg != NULL)
hash_value ^= hash(*key.func.arg);
for (unsigned int i = 0; i < array_length(key.func.vars); i++) {
if (key.func.vars[i] == 0) break;
hash_value ^= default_hash(key.func.vars[i]);
}
return 1 + 11 * hash_value;
case DATALOG_TUPLE:
hash_value = default_hash(key.tuple.position);
for (unsigned int i = 0; i < key.tuple.elements.length; i++)
hash_value ^= hash(*key.tuple.elements[i]);
return 2 + 11 * hash_value;
case DATALOG_LIST:
hash_value = 0;
for (unsigned int i = 0; i < key.list.elements.length; i++)
hash_value ^= hash(*key.list.elements[i]);
return 3 + 11 * hash_value;
case DATALOG_VARIABLE:
return 4 + 11 * default_hash(key.variable);
case DATALOG_CONSTANT:
return 5 + 11 * datalog_literal::hash(key.constant);
case DATALOG_INTEGER:
return 6 + 11 * default_hash(key.integer);
case DATALOG_STRING:
return 7 + 11 * string::hash(key.str);
case DATALOG_EMPTY:
return 8;
case DATALOG_ANY:
return 9;
case DATALOG_NON_EMPTY:
return 10;
}
fprintf(stderr, "datalog_expression.hash ERROR: Unrecognized expression type.\n");
exit(EXIT_FAILURE);
}
static inline void free(datalog_expression& exp) { exp.free(); }
private:
template<typename... Args>
inline bool initialize(const datalog_expression& src, Args&&... args);
inline void free() {
reference_count--;
if (reference_count == 0) {
switch (type) {
case DATALOG_PREDICATE:
core::free(pred);
break;
case DATALOG_FUNCTION:
core::free(func);
break;
case DATALOG_TUPLE:
core::free(tuple);
break;
case DATALOG_LIST:
core::free(list);
break;
case DATALOG_VARIABLE:
core::free(variable);
break;
case DATALOG_CONSTANT:
core::free(constant);
break;
case DATALOG_INTEGER:
core::free(integer);
break;
case DATALOG_STRING:
core::free(str);
break;
case DATALOG_EMPTY:
case DATALOG_ANY:
case DATALOG_NON_EMPTY:
break;
}
}
}
template<typename... Args>
friend bool init(datalog_expression&, const datalog_expression&, Args&&...);
};
struct datalog_expression_root {
/* semantic information */
datalog_expression root;
/* syntactic information */
grammatical_number index;
grammatical_number concord;
inflection inf;
datalog_expression_root() : index(NUMBER_ANY), concord(NUMBER_ANY), inf(INFLECTION_ANY) { }
datalog_expression_root(const datalog_expression_root& src) :
root(src.root), index(src.index), concord(src.concord), inf(src.inf) { }
inline void operator = (const datalog_expression_root& src) {
root = src.root;
index = src.index;
concord = src.concord;
inf = src.inf;
}
inline void recompute_hash() const {
root.recompute_hash();
}
static inline void move(const datalog_expression_root& src, datalog_expression_root& dst) {
datalog_expression::move(src.root, dst.root);
dst.index = src.index;
dst.concord = src.concord;
dst.inf = src.inf;
}
static inline void swap(datalog_expression_root& first, datalog_expression_root& second) {
datalog_expression::swap(first.root, second.root);
core::swap(first.index, second.index);
core::swap(first.concord, second.concord);
core::swap(first.inf, second.inf);
}
static inline bool is_empty(const datalog_expression_root& key) {
return datalog_expression::is_empty(key.root);
}
static inline unsigned int hash(const datalog_expression_root& key) {
return datalog_expression::hash(key.root) ^ default_hash(key.index) ^ default_hash(key.concord) ^ default_hash(key.inf);
}
static inline void free(datalog_expression_root& exp) {
datalog_expression::free(exp.root);
}
enum feature {
FEATURE_NULL = 0,
FEATURE_FUNCTION = 1,
FEATURE_FUNCTION_ONLY = 2,
FEATURE_FUNCTION_ANSWER = 3,
FEATURE_HAS_FUNCTION = 4,
FEATURE_HAS_FUNCTION_NOT = 5,
FEATURE_HAS_FUNCTION_COUNT_NOT = 6,
FEATURE_HAS_FUNCTION_ANSWER = 7,
FEATURE_PREDICATE = 8,
FEATURE_PREDICATE_ONLY = 9,
FEATURE_FIRST_PREDICATE = 10,
FEATURE_SECOND_PREDICATE = 11,
FEATURE_THIRD_PREDICATE = 12,
FEATURE_LAST_PREDICATE = 13,
FEATURE_DIRECTION = 14,
FEATURE_DIRECTION_ROOT = 15,
FEATURE_CONSTANT = 16,
FEATURE_PREDICATE_ARITY = 17,
FEATURE_ARG1 = 18,
FEATURE_ARG2 = 19,
FEATURE_ARG3 = 20,
FEATURE_ARG1_ONLY = 21,
FEATURE_ARG2_ONLY = 22,
FEATURE_ARG3_ONLY = 23,
FEATURE_ARG2_ARITY = 24,
FEATURE_NUMBER = 25,
FEATURE_INFLECTION = 26
};
enum function_type {
FUNCTION_EMPTY = 0,
FUNCTION_IDENTITY = 1,
FUNCTION_IDENTITY_COORD = 2,
FUNCTION_NULL = 3,
FUNCTION_SELECT_LEFT = 4,
FUNCTION_SELECT_LEFT_COORD = 5,
FUNCTION_SELECT_LEFT_DELETE_FEATURES = 6,
FUNCTION_SELECT_LEFT_CONCORD_SINGULAR = 7,
FUNCTION_SELECT_LEFT_DISJOINT = 8,
FUNCTION_SELECT_LEFT_DISJOINT_COORD = 9,
FUNCTION_SELECT_LEFT2 = 10,
FUNCTION_SELECT_LEFT2_DELETE_FEATURES = 11,
FUNCTION_SELECT_LEFT2_DISJOINT = 12,
FUNCTION_SELECT_LEFT2_DISJOINT_CONCORD_SINGULAR = 13,
FUNCTION_SELECT_LEFT2_DISJOINT_COORD = 14,
FUNCTION_SELECT_LEFT3_DISJOINT = 15,
FUNCTION_SELECT_LEFT4_DISJOINT = 16,
FUNCTION_SELECT_LEFT5_DISJOINT = 17,
FUNCTION_SELECT_LEFT6_DISJOINT = 18,
FUNCTION_SELECT_LEFT7_DISJOINT = 19,
FUNCTION_SELECT_LEFT_KEEP_FUNCTION = 20,
FUNCTION_SELECT_LEFT_KEEP_FUNCTION_DISJOINT = 21,
FUNCTION_SELECT_LEFT2_KEEP_FUNCTION = 22,
FUNCTION_SELECT_LEFT2_KEEP_FUNCTION_DISJOINT = 23,
FUNCTION_SELECT_LEFT3_KEEP_FUNCTION = 24,
FUNCTION_SELECT_LEFT_DELETE_HEAD = 25,
FUNCTION_SELECT_LEFT_DELETE_HEAD_CONCORD_SINGULAR = 26,
FUNCTION_SELECT_LEFT_DELETE_HEAD_DISJOINT = 27,
FUNCTION_SELECT_LEFT_DELETE_HEAD_DISJOINT_COORD = 28,
FUNCTION_SELECT_LEFT_DELETE_FUNCTION = 29,
FUNCTION_SELECT_LEFT_DELETE_FUNCTION_FEATURES = 30,
FUNCTION_SELECT_LEFT_DELETE_ANSWER = 31,
FUNCTION_SELECT_LEFT2_DELETE_ANSWER = 32,
FUNCTION_SELECT_LEFT3_DELETE_ANSWER = 33,
FUNCTION_SELECT_LEFT3_DELETE_ANSWER_DISJOINT = 34,
FUNCTION_SELECT_LEFT5_DELETE_ANSWER_DISJOINT = 35,
FUNCTION_SELECT_LEFT_DELETE_ANSWER_HEAD = 36,
FUNCTION_SELECT_LEFT_DELETE_COUNT_ANSWER = 37,
FUNCTION_SELECT_LEFT2_DELETE_COUNT_ANSWER = 38,
FUNCTION_SELECT_LEFT3_DELETE_COUNT_ANSWER = 39,
FUNCTION_SELECT_LEFT_DELETE_FUNCTION_ANSWER = 40,
FUNCTION_SELECT_LEFT2_DELETE_FUNCTION_ANSWER = 41,
FUNCTION_SELECT_LEFT3_DELETE_FUNCTION_ANSWER = 42,
FUNCTION_SELECT_LEFT_DELETE_NOT = 43,
FUNCTION_SELECT_LEFT_DELETE_NOT_FEATURES = 44,
FUNCTION_SELECT_RIGHT = 45,
FUNCTION_SELECT_RIGHT2 = 46,
FUNCTION_SELECT_RIGHT2_SINGULAR = 47,
FUNCTION_SELECT_RIGHT2_DISJOINT = 48,
FUNCTION_SELECT_RIGHT3_DISJOINT = 49,
FUNCTION_SELECT_RIGHT4_DISJOINT = 50,
FUNCTION_SELECT_RIGHT5_DISJOINT = 51,
FUNCTION_SELECT_RIGHT6_DISJOINT = 52,
FUNCTION_SELECT_RIGHT7_DISJOINT = 53,
FUNCTION_SELECT_RIGHT_DELETE_HEAD = 54,
FUNCTION_SELECT_RIGHT_DELETE_FUNCTION = 55,
FUNCTION_SELECT_RIGHT2_DELETE_ANSWER = 56,
FUNCTION_SELECT_FUNCTION = 57,
FUNCTION_SELECT_FUNCTION_DELETE_FEATURES = 58,
FUNCTION_SELECT_FUNCTION_DELETE_HEAD = 59,
FUNCTION_SELECT_FUNCTION_DELETE_HEAD_FEATURES = 60,
FUNCTION_DELETE_LEFT = 61,
FUNCTION_DELETE_LEFT_COORD = 62,
FUNCTION_DELETE_LEFT_FEATURES = 63,
FUNCTION_DELETE_LEFT_DISJOINT = 64,
FUNCTION_DELETE_LEFT_DISJOINT_COORD = 65,
FUNCTION_DELETE_LEFT2 = 66,
FUNCTION_DELETE_LEFT2_DISJOINT = 67,
FUNCTION_DELETE_LEFT2_DISJOINT_COORD = 68,
FUNCTION_DELETE_LEFT2_DISJOINT_FEATURES = 201,
FUNCTION_DELETE_LEFT3_DISJOINT = 69,
FUNCTION_DELETE_LEFT3_DISJOINT_FEATURES = 202,
FUNCTION_DELETE_LEFT4_DISJOINT = 70,
FUNCTION_DELETE_LEFT5_DISJOINT = 71,
FUNCTION_DELETE_LEFT6_DISJOINT = 72,
FUNCTION_DELETE_LEFT7_DISJOINT = 73,
FUNCTION_DELETE_LEFT_HEAD = 74,
FUNCTION_DELETE_LEFT_HEAD_FEATURES = 75,
FUNCTION_DELETE_LEFT2_HEAD = 76,
FUNCTION_DELETE_LEFT2_HEAD_FEATURES = 77,
FUNCTION_DELETE_LEFT_FUNCTION = 78,
FUNCTION_DELETE_LEFT_FUNCTION_DISJOINT = 79,
FUNCTION_DELETE_LEFT2_FUNCTION = 80,
FUNCTION_DELETE_LEFT2_FUNCTION_DISJOINT = 81,
FUNCTION_DELETE_LEFT_FUNCTION_HEAD = 82,
FUNCTION_DELETE_LEFT3_FUNCTION_HEAD = 83,
FUNCTION_DELETE_LEFT_KEEP_ANSWER = 84,
FUNCTION_DELETE_LEFT_KEEP_FUNCTION = 85,
FUNCTION_DELETE_LEFT_FEATURES_KEEP_FUNCTION = 86,
FUNCTION_DELETE_LEFT_HEAD_KEEP_FUNCTION = 87,
FUNCTION_DELETE_LEFT_HEAD_FEATURES_KEEP_FUNCTION = 88,
FUNCTION_DELETE_LEFT_HEAD_KEEP_NOT = 89,
FUNCTION_DELETE_LEFT_HEAD_FEATURES_KEEP_NOT = 90,
FUNCTION_DELETE_LEFT_ANSWER = 91,
FUNCTION_DELETE_LEFT2_ANSWER = 92,
FUNCTION_DELETE_LEFT3_ANSWER = 93,
FUNCTION_DELETE_LEFT3_ANSWER_DISJOINT = 94,
FUNCTION_DELETE_LEFT5_ANSWER_DISJOINT = 95,
FUNCTION_DELETE_LEFT_ANSWER_HEAD = 96,
FUNCTION_DELETE_LEFT_COUNT_ANSWER = 97,
FUNCTION_DELETE_LEFT2_COUNT_ANSWER = 98,
FUNCTION_DELETE_LEFT3_COUNT_ANSWER = 99,
FUNCTION_DELETE_LEFT_FUNCTION_ANSWER = 100,
FUNCTION_DELETE_LEFT2_FUNCTION_ANSWER = 101,
FUNCTION_DELETE_LEFT3_FUNCTION_ANSWER = 102,
FUNCTION_DELETE_LEFT_ANSWER_KEEP_FUNCTION = 103,
FUNCTION_DELETE_LEFT_ANSWER_HEAD_KEEP_FUNCTION = 104,
FUNCTION_DELETE_LEFT2_ANSWER_KEEP_FUNCTION = 105,
FUNCTION_DELETE_LEFT2_ANSWER_HEAD_KEEP_FUNCTION = 106,
FUNCTION_DELETE_LEFT3_ANSWER_KEEP_FUNCTION = 107,
FUNCTION_DELETE_LEFT3_ANSWER_HEAD_KEEP_FUNCTION = 108,
FUNCTION_DELETE_RIGHT = 109,
FUNCTION_DELETE_RIGHT2 = 110,
FUNCTION_DELETE_RIGHT2_DISJOINT = 111,
FUNCTION_DELETE_RIGHT3_DISJOINT = 112,
FUNCTION_DELETE_RIGHT4_DISJOINT = 113,
FUNCTION_DELETE_RIGHT5_DISJOINT = 114,
FUNCTION_DELETE_RIGHT6_DISJOINT = 115,
FUNCTION_DELETE_RIGHT7_DISJOINT = 116,
FUNCTION_DELETE_RIGHT_HEAD = 117,
FUNCTION_DELETE_RIGHT_HEAD_KEEP_FUNCTION = 118,
FUNCTION_DELETE_RIGHT2_ANSWER = 119,
FUNCTION_DELETE_COUNT = 120,
FUNCTION_DELETE_COUNT_HEAD = 121,
FUNCTION_DELETE_COUNT_HEAD_CONCORD_PLURAL = 122,
FUNCTION_DELETE_COUNT_ANSWER = 123,
FUNCTION_DELETE_NOT = 124,
FUNCTION_DELETE_NOT_FEATURES = 125,
FUNCTION_DELETE_NOT_INFINITIVE = 126,
FUNCTION_DELETE_LEFT_NOT_HEAD = 127,
FUNCTION_DELETE_LEFT_NOT_HEAD_FEATURES = 128,
FUNCTION_DELETE_FUNCTION = 129,
FUNCTION_DELETE_FUNCTION_HEAD = 130,
FUNCTION_DELETE_FUNCTION_FEATURES = 131,
FUNCTION_DELETE_ANSWER = 132,
FUNCTION_DELETE_ANSWER_HAS_LOC = 133,
FUNCTION_SELECT_ARG1 = 134,
FUNCTION_SELECT_ARG1_SINGULAR = 235,
FUNCTION_SELECT_ARG1_PLURAL = 236,
FUNCTION_SELECT_ARG1_ONLY = 234,
FUNCTION_SELECT_ARG1_DELETE_FEATURES = 237,
FUNCTION_SELECT_ARG1_ONLY_DELETE_FEATURES = 243,
FUNCTION_SELECT_ARG2 = 136,
FUNCTION_SELECT_ARG2_ONLY = 244,
FUNCTION_SELECT_ARG2_DELETE_FEATURES = 238,
FUNCTION_SELECT_ARG2_ONLY_DELETE_FEATURES = 245,
FUNCTION_SELECT_ARG3 = 137,
FUNCTION_SELECT_ARG3_ONLY = 246,
FUNCTION_SELECT_ARG3_DELETE_FEATURES = 138,
FUNCTION_SELECT_ARG3_ONLY_DELETE_FEATURES = 247,
FUNCTION_DELETE_ARG1 = 139,
FUNCTION_DELETE_ARG1_SINGULAR = 239,
FUNCTION_DELETE_ARG1_PLURAL = 240,
FUNCTION_DELETE_ARG1_FEATURES = 241,
FUNCTION_DELETE_ARG2 = 140,
FUNCTION_DELETE_ARG2_FEATURES = 242,
FUNCTION_DELETE_ARG3 = 141,
FUNCTION_DELETE_ARGS = 142,
FUNCTION_DELETE_ARGS_CONCORD_SINGULAR = 143,
FUNCTION_DELETE_ARGS_KEEP_PLURAL = 144,
FUNCTION_HEAD_ARG1_SELECT_ARG2 = 145,
FUNCTION_HEAD_ARG1_SELECT_ARG2_ONLY = 146,
FUNCTION_EMPTY_TUPLE = 147,
FUNCTION_EMPTY_TUPLE_ONLY = 148,
FUNCTION_EMPTY_TUPLE_ONLY_KEEP_CONCORD_SINGULAR = 149,
FUNCTION_KEEP_NULL = 249,
FUNCTION_EMPTY_ARG2 = 150,
FUNCTION_EMPTY_ARGS = 248,
FUNCTION_ARG2_ZERO_ARITY = 250,
FUNCTION_LOC = 151,
FUNCTION_TWO_PREDICATES = 152,
/* functions that relate to syntactic information */
FUNCTION_SINGULAR = 153,
FUNCTION_PLURAL = 154,
FUNCTION_UNCOUNTABLE = 155,
FUNCTION_CONCORD_SINGULAR = 156,
FUNCTION_CONCORD_PLURAL = 157,
FUNCTION_CONCORD_UNCOUNTABLE = 158,
FUNCTION_CONCORD_NON_SINGULAR = 159,
FUNCTION_CONCORD_NON_PLURAL = 160,
FUNCTION_CONCORD_NON_PLURAL_KEEP_SINGULAR = 252,
FUNCTION_CONCORD_ALL = 161,
FUNCTION_KEEP_CONCORD_SINGULAR = 251,
FUNCTION_KEEP_CONCORD_PLURAL = 253,
FUNCTION_KEEP_CONCORD_UNCOUNTABLE = 254,
FUNCTION_DELETE_NOT_CONCORD_SINGULAR = 162,
FUNCTION_DELETE_NOT_CONCORD_PLURAL = 163,
FUNCTION_KEEP_SINGULAR = 164,
FUNCTION_KEEP_PLURAL = 165,
FUNCTION_KEEP_UNCOUNTABLE = 166,
FUNCTION_INFINITIVE = 167,
FUNCTION_PRESENT_PARTICIPLE = 168,
FUNCTION_PAST_PARTICIPLE = 169,
FUNCTION_KEEP_PRESENT_PARTICIPLE = 170,
FUNCTION_KEEP_PAST_PARTICIPLE = 171,
FUNCTION_FLIP_PREDICATE = 172,
FUNCTION_FLIP_PREDICATE_PAST_PARTICIPLE = 173,
FUNCTION_FLIP_PREDICATE_KEEP_PAST_PARTICIPLE = 174,
FUNCTION_KEEP_FEATURES = 175,
FUNCTION_DELETE_FEATURES = 176
};
struct function {
function_type type;
constexpr function(const function_type& type) : type(type) { }
static inline unsigned int hash(const function& f) {