Commit 9837c5b4fc6ab8d5cc1ec939c1ddfa07b8c38d27

Thomas de Grivel 2023-10-20T23:03:40

wip operator, segv

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
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 5ed4fd2..8edff5b 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -249,9 +249,12 @@ sw buf_inspect_call (s_buf *buf, const s_call *call)
     return buf_inspect_call_brackets(buf, call);
   if (call->ident.sym == sym_1("cast"))
     return buf_inspect_cast(buf, call);
-  if (operator_find(&call->ident, 1))
+  if (operator_find(&call->ident) &&
+      operator_arity(&call->ident) == 1)
     return buf_inspect_call_op_unary(buf, call);
-  if ((op_precedence = operator_precedence(&call->ident, 2)) > 0)
+  if (operator_find(&call->ident) &&
+      operator_arity(&call->ident) == 2 &&
+      (op_precedence = operator_precedence(&call->ident)) > 0)
     return buf_inspect_call_op(buf, call, op_precedence);
   if ((r = buf_inspect_ident(buf, &call->ident)) < 0)
     return r;
@@ -349,8 +352,10 @@ sw buf_inspect_call_op (s_buf *buf, const s_call *call, s8 op_precedence)
   s_tag *right;
   left = &call->arguments->tag;
   right = &list_next(call->arguments)->tag;
-  if (left->type == TAG_CALL && 
-      (precedence = operator_precedence(&left->data.call.ident, 2))
+  if (left->type == TAG_CALL &&
+      operator_find(&left->data.call.ident) &&
+      operator_arity(&left->data.call.ident) == 2 &&
+      (precedence = operator_precedence(&left->data.call.ident))
       < op_precedence) {
     paren = true;
     if ((r = buf_write_1(buf, "(")) < 0)
@@ -376,8 +381,10 @@ sw buf_inspect_call_op (s_buf *buf, const s_call *call, s8 op_precedence)
   if ((r = buf_write_1(buf, " ")) < 0)
     return r;
   result += r;
-  if (right->type == TAG_CALL && 
-      (precedence = operator_precedence(&right->data.call.ident, 2))
+  if (right->type == TAG_CALL &&
+      operator_find(&right->data.call.ident) &&
+      operator_arity(&right->data.call.ident) == 2 &&
+      (precedence = operator_precedence(&right->data.call.ident))
       < op_precedence) {
     paren = true;
     if ((r = buf_write_1(buf, "(")) < 0)
@@ -451,9 +458,12 @@ sw buf_inspect_call_size (const s_call *call)
   s8 op_precedence;
   sw r;
   sw result = 0;
-  if (operator_find(&call->ident, 1))
+  if (operator_find(&call->ident) &&
+      operator_arity(&call->ident) == 1)
     return buf_inspect_call_op_unary_size(call);
-  if ((op_precedence = operator_precedence(&call->ident, 2)) > 0)
+  if (operator_find(&call->ident) &&
+      operator_arity(&call->ident) == 2 &&
+      (op_precedence = operator_precedence(&call->ident)) > 0)
     return buf_inspect_call_op_size(call, op_precedence);
   if ((r = buf_inspect_ident_size(&call->ident)) < 0)
     return r;
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 10878e8..c6cc8ee 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -595,8 +595,8 @@ sw buf_parse_call_op (s_buf *buf, s_call *dest)
   result += r;
   if ((r = buf_parse_ident_peek(buf, &next_op)) <= 0)
     goto restore;
-  if (! operator_find(&next_op, 2) ||
-      operator_precedence(&next_op, 2) < 0) {
+  if (! operator_resolve(&next_op, 2, &next_op) ||
+      operator_precedence(&next_op) < 0) {
     r = 0;
     goto restore;
   }
@@ -641,8 +641,8 @@ sw buf_parse_call_op_rec (s_buf *buf, s_call *dest, u8 min_precedence)
   tag_copy(&dest->arguments->tag, left);
   if ((r = buf_parse_ident_peek(buf, &next_op)) <= 0)
     goto restore;
-  if (! operator_find(&next_op, 2) ||
-      (op_precedence = operator_precedence(&next_op, 2)) < 0) {
+  if (! operator_resolve(&next_op, 2, &next_op) ||
+      (op_precedence = operator_precedence(&next_op)) < 0) {
     r = 0;
     goto restore;
   }
@@ -650,8 +650,9 @@ sw buf_parse_call_op_rec (s_buf *buf, s_call *dest, u8 min_precedence)
     if ((r = buf_parse_ident(buf, &next_op)) <= 0)
       goto restore;
     result += r;
+    if (! operator_resolve(&next_op, 2, &next_op))
+      goto restore;
     op = next_op;
-    operator_call_ident(&op, 2, &tmp.ident);
     if ((r = buf_ignore_spaces(buf)) < 0)
       goto restore;
     result += r;
@@ -668,14 +669,19 @@ sw buf_parse_call_op_rec (s_buf *buf, s_call *dest, u8 min_precedence)
     r = buf_parse_ident_peek(buf, &next_op);
     if (r <= 0)
       break;
-    next_op_precedence = operator_precedence(&next_op, 2);
-    while (r > 0 && operator_find(&next_op, 2) &&
+    if (! operator_resolve(&next_op, 2, &next_op))
+      break;
+    next_op_precedence = operator_precedence(&next_op);
+    while (r > 0 && operator_arity(&next_op) == 2 &&
            (next_op_precedence >= op_precedence ||
-            (operator_is_right_associative(&next_op, 2) &&
+            (operator_is_right_associative(&next_op) &&
              next_op_precedence == op_precedence))) {
       call_init_op(&tmp2);
       tmp2.arguments->tag = *right;
-      if ((r = buf_parse_call_op_rec(buf, &tmp2, (next_op_precedence > op_precedence) ? op_precedence + 1 : op_precedence)) <= 0) {
+      if ((r = buf_parse_call_op_rec(buf, &tmp2,
+                                     (next_op_precedence > op_precedence) ?
+                                     op_precedence + 1 :
+                                     op_precedence)) <= 0) {
         tmp2.arguments->tag.type = TAG_VOID;
         call_clean(&tmp2);
         break;
@@ -692,13 +698,17 @@ sw buf_parse_call_op_rec (s_buf *buf, s_call *dest, u8 min_precedence)
         break;
       }
       r = buf_parse_ident_peek(buf, &next_op);
-      if (r > 0)
-        next_op_precedence = operator_precedence(&next_op, 2);
+      if (r > 0) {
+        if (! operator_resolve(&next_op, 2, &next_op))
+          break;
+        if ((next_op_precedence = operator_precedence(&next_op)) < 0)
+          break;
+      }
     }
     if (r <= 0 || (op_precedence = next_op_precedence) < min_precedence)
       break;
     call_init_op(&tmp3);
-    operator_call_ident(&op, 2, &tmp3.ident);
+    tmp3.ident = op;
     tmp3.arguments->tag = *left;
     list_next(tmp3.arguments)->tag = *right;
     tag_init_call(left, &tmp3);
@@ -728,7 +738,7 @@ sw buf_parse_call_op_unary (s_buf *buf, s_call *dest)
   if ((r = buf_parse_ident(buf, &tmp.ident)) <= 0)
     goto restore;
   result += r;
-  if (! operator_find(&tmp.ident, 1))
+  if (! operator_resolve(&tmp.ident, 1, &tmp.ident))
     goto restore;
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
@@ -737,7 +747,6 @@ sw buf_parse_call_op_unary (s_buf *buf, s_call *dest)
     goto restore;
   result += r;
   *dest = tmp;
-  operator_call_ident(&tmp.ident, 1, &dest->ident);
   r = result;
   goto clean;
  restore:
diff --git a/libc3/env.c b/libc3/env.c
index 5ca70d0..3a1d493 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -30,6 +30,7 @@
 #include "error.h"
 #include "error_handler.h"
 #include "facts.h"
+#include "facts_cursor.h"
 #include "facts_with.h"
 #include "facts_with_cursor.h"
 #include "file.h"
@@ -750,173 +751,101 @@ bool env_module_maybe_reload (const s_sym *module, s_env *env,
   return r;
 }
 
-s_ident * env_operator_call_ident (s_env *env, const s_ident *op,
-                                   u8 arity, s_ident *dest)
+s8 env_operator_arity (s_env *env, const s_ident *op)
 {
-  s_facts_with_cursor cursor;
+  s_facts_cursor cursor;
+  s8 r = -1;
+  s_tag tag_op;
   s_tag tag_arity;
-  s_tag tag_arity_u8;
-  s_tag tag_is_a;
-  s_tag tag_module;
-  s_tag tag_module_name;
-  s_tag tag_operator;
-  s_tag tag_operator_var;
-  s_tag tag_sym;
-  s_tag tag_symbol;
-  s_ident tmp;
-  tmp = *op;
-  ident_resolve_module(&tmp, env);
-  tag_init_1(  &tag_arity, ":arity");
-  tag_init_u8( &tag_arity_u8, arity);
-  tag_init_1(  &tag_is_a, ":is_a");
-  tag_init_1(  &tag_module, ":module");
-  tag_init_sym(&tag_module_name, tmp.module);
-  tag_init_1(  &tag_operator, ":operator");
-  tag_init_var(&tag_operator_var);
-  tag_init_sym(&tag_sym, tmp.sym);
-  tag_init_1(  &tag_symbol, ":symbol");
-  facts_with(&env->facts, &cursor, (t_facts_spec) {
-      &tag_module_name, &tag_is_a, &tag_module,
-      &tag_operator, &tag_operator_var, NULL,   /* module exports operator */
-      &tag_operator_var, &tag_symbol, &tag_sym,
-      &tag_arity, &tag_arity_u8,
-      NULL, NULL });
-  if (facts_with_cursor_next(&cursor)) {
-    if (tag_operator_var.type == TAG_IDENT) {
-      *dest = tag_operator_var.data.ident;
-      facts_with_cursor_clean(&cursor);
-      return dest;
-    }
+  s_tag tag_var;
+  assert(env);
+  assert(op);
+  tag_init_ident(&tag_op, op);
+  tag_init_1(    &tag_arity, ":arity");
+  tag_init_var(  &tag_var);
+  facts_with_tags(&env->facts, &cursor, &tag_op, &tag_arity, &tag_var);
+  if (facts_cursor_next(&cursor) &&
+      tag_var.type == TAG_U8) {
+    r = tag_var.data.u8;
   }
-  warnx("env_operator_call_ident: operator %s/%d not found in module %s",
-        tmp.sym->str.ptr.ps8,
-        arity,
-        tmp.module->str.ptr.ps8);
-  facts_with_cursor_clean(&cursor);
-  return NULL;
+  else
+    warnx("env_operator_arity: "
+          "arity for operator %s not found in module %s",
+          op->sym->str.ptr.ps8,
+          op->module->str.ptr.ps8);
+  facts_cursor_clean(&cursor);
+  return r;
 }
 
-bool env_operator_find (s_env *env, const s_ident *op, u8 arity)
+bool env_operator_find (s_env *env, const s_ident *op)
 {
-  s_facts_with_cursor cursor;
-  p_facts_spec spec;
-  s_tag tag_arity;
-  s_tag tag_arity_u8;
   s_tag tag_is_a;
-  s_tag tag_module;
-  s_tag tag_module_name;
+  s_tag tag_op;
   s_tag tag_operator;
-  s_tag tag_operator_var;
-  s_tag tag_symbol;
-  s_tag tag_sym;
-  s_ident tmp;
   assert(env);
   assert(op);
-  tmp = *op;
-  ident_resolve_module(&tmp, env);
-  tag_init_1(  &tag_arity, ":arity");
-  tag_init_u8( &tag_arity_u8, arity);
-  tag_init_1(  &tag_is_a, ":is_a");
-  tag_init_1(  &tag_module, ":module");
-  tag_init_sym(&tag_module_name, tmp.module);
-  tag_init_1(  &tag_operator, ":operator");
-  tag_init_var(&tag_operator_var);
-  tag_init_1(  &tag_symbol, ":symbol");
-  tag_init_sym(&tag_sym, tmp.sym);
-  spec = (p_facts_spec) & (t_facts_spec) {
-    &tag_module_name, &tag_is_a, &tag_module,
-    &tag_operator, &tag_operator_var, NULL,
-    &tag_operator_var, &tag_is_a, &tag_operator,
-    &tag_symbol, &tag_sym,
-    &tag_arity, &tag_arity_u8,
-    NULL, NULL };
-  facts_with(&env->facts, &cursor, spec);
-  if (facts_with_cursor_next(&cursor)) {
-    facts_with_cursor_clean(&cursor);
-    return true;
-  }
-#ifdef DEBUG_OPERATOR_FIND
-  warnx("env_operator_find: operator %s not found in module %s",
-        tmp.sym->str.ptr.ps8,
-        tmp.module->str.ptr.ps8);
-#endif
-  return false;
+  tag_init_1(    &tag_is_a, ":is_a");
+  tag_init_ident(&tag_op, op);
+  tag_init_1(    &tag_operator, ":operator");
+  return facts_find_fact_by_tags(&env->facts, &tag_op, &tag_is_a,
+                                 &tag_operator) ? 1 : 0;
 }
 
-bool env_operator_is_right_associative (s_env *env, const s_ident *op,
-                                        u8 arity)
+bool env_operator_is_right_associative (s_env *env, const s_ident *op)
 {
-  s_facts_with_cursor cursor;
-  s_tag tag_arity;
-  s_tag tag_arity_u8;
-  s_tag tag_is_a;
-  s_tag tag_module;
-  s_tag tag_module_name;
-  s_tag tag_operator;
-  s_tag tag_operator_assoc;
-  s_tag tag_operator_assoc_var;
-  s_tag tag_operator_var;
-  s_tag tag_symbol;
-  s_tag tag_sym;
-  s_ident tmp;
+  s_tag tag_assoc;
+  s_tag tag_op;
+  s_tag tag_right;
   assert(env);
   assert(op);
-  tmp = *op;
-  ident_resolve_module(&tmp, env);
-  tag_init_1(  &tag_arity, ":arity");
-  tag_init_u8( &tag_arity_u8, arity);
-  tag_init_1(  &tag_is_a, ":is_a");
-  tag_init_1(  &tag_module, ":module");
-  tag_init_sym(&tag_module_name, tmp.module);
-  tag_init_1(  &tag_operator, ":operator");
-  tag_init_1(  &tag_operator_assoc, ":operator_associativity");
-  tag_init_var(&tag_operator_assoc_var);
-  tag_init_var(&tag_operator_var);
-  tag_init_1(  &tag_symbol, ":symbol");
-  tag_init_sym(&tag_sym, tmp.sym);
-  facts_with(&env->facts, &cursor, (t_facts_spec) {
-      &tag_module_name, &tag_is_a, &tag_module,
-      &tag_operator, &tag_operator_var, NULL,
-      &tag_operator_var, &tag_is_a, &tag_operator,
-      &tag_symbol, &tag_sym,
-      &tag_arity, &tag_arity_u8,
-      &tag_operator_assoc, &tag_operator_assoc_var,
-      NULL, NULL });
-  if (facts_with_cursor_next(&cursor)) {
-    if (tag_operator_assoc_var.type == TAG_SYM &&
-        tag_operator_assoc_var.data.sym == sym_1("right")) {
-      facts_with_cursor_clean(&cursor);
-      return true;
-    }
-    facts_with_cursor_clean(&cursor);
-    return false;
+  tag_init_1(    &tag_assoc, ":operator_associativity");
+  tag_init_ident(&tag_op, op);
+  tag_init_1(    &tag_right, ":right");
+  return facts_find_fact_by_tags(&env->facts, &tag_op, &tag_assoc,
+                                 &tag_right) ? 1 : 0;
+}
+
+s8 env_operator_precedence (s_env *env, const s_ident *op)
+{
+  s_facts_cursor cursor;
+  s8 r = -1;
+  s_tag tag_op;
+  s_tag tag_precedence;
+  s_tag tag_var;
+  assert(env);
+  assert(op);
+  tag_init_ident(&tag_op, op);
+  tag_init_1(    &tag_precedence, ":operator_precedence");
+  tag_init_var(  &tag_var);
+  facts_with_tags(&env->facts, &cursor, &tag_op, &tag_precedence,
+                  &tag_var);
+  if (facts_cursor_next(&cursor) &&
+      tag_var.type == TAG_U8) {
+    r = tag_var.data.u8;
   }
-  warnx("env_operator_is_right_associative: "
-        "operator %s not found in module %s",
-        tmp.sym->str.ptr.ps8,
-        tmp.module->str.ptr.ps8);
-  facts_with_cursor_clean(&cursor);
-  return false;
+  else
+    warnx("env_operator_precedence: "
+          "precedence for operator %s not found in module %s",
+          op->sym->str.ptr.ps8,
+          op->module->str.ptr.ps8);
+  facts_cursor_clean(&cursor);
+  return r;
 }
 
-s8 env_operator_precedence (s_env *env, const s_ident *op, u8 arity)
+s_ident * env_operator_resolve (s_env *env, const s_ident *op,
+                                u8 arity, s_ident *dest)
 {
   s_facts_with_cursor cursor;
-  s8 r = -1;
   s_tag tag_arity;
   s_tag tag_arity_u8;
   s_tag tag_is_a;
   s_tag tag_module;
   s_tag tag_module_name;
   s_tag tag_operator;
-  s_tag tag_operator_precedence;
-  s_tag tag_operator_precedence_var;
-  s_tag tag_operator_var;
-  s_tag tag_symbol;
+  s_tag tag_var;
   s_tag tag_sym;
+  s_tag tag_symbol;
   s_ident tmp;
-  assert(env);
-  assert(op);
   tmp = *op;
   ident_resolve_module(&tmp, env);
   tag_init_1(  &tag_arity, ":arity");
@@ -925,30 +854,29 @@ s8 env_operator_precedence (s_env *env, const s_ident *op, u8 arity)
   tag_init_1(  &tag_module, ":module");
   tag_init_sym(&tag_module_name, tmp.module);
   tag_init_1(  &tag_operator, ":operator");
-  tag_init_1(  &tag_operator_precedence, ":operator_precedence");
-  tag_init_var(&tag_operator_precedence_var);
-  tag_init_var(&tag_operator_var);
-  tag_init_1(  &tag_symbol, ":symbol");
+  tag_init_var(&tag_var);
   tag_init_sym(&tag_sym, tmp.sym);
+  tag_init_1(  &tag_symbol, ":symbol");
   facts_with(&env->facts, &cursor, (t_facts_spec) {
       &tag_module_name, &tag_is_a, &tag_module,
-      &tag_operator, &tag_operator_var, NULL,
-      &tag_operator_var, &tag_is_a, &tag_operator,
-      &tag_symbol, &tag_sym,
-      &tag_operator_precedence, &tag_operator_precedence_var,
+      &tag_operator, &tag_var, NULL,   /* module exports operator */
+      &tag_var, &tag_symbol, &tag_sym,
+      &tag_arity, &tag_arity_u8,
       NULL, NULL });
-  if (facts_with_cursor_next(&cursor) &&
-      tag_operator_precedence_var.type == TAG_U8) {
-    r = tag_operator_precedence_var.data.u8;
+  if (facts_with_cursor_next(&cursor)) {
+    if (tag_var.type == TAG_IDENT) {
+      *dest = tag_var.data.ident;
+      facts_with_cursor_clean(&cursor);
+      return dest;
+    }
   }
-  else
-    warnx("env_operator_precedence: "
-          "operator %s/%d not found in module %s",
+  if (false)
+    warnx("env_operator_resolve: operator %s/%d not found in module %s",
           tmp.sym->str.ptr.ps8,
           arity,
           tmp.module->str.ptr.ps8);
   facts_with_cursor_clean(&cursor);
-  return r;
+  return NULL;
 }
 
 void env_pop_error_handler (s_env *env)
diff --git a/libc3/env.h b/libc3/env.h
index ac51116..5f6b753 100644
--- a/libc3/env.h
+++ b/libc3/env.h
@@ -57,14 +57,13 @@ bool       env_module_load (const s_sym *module, s_env *env,
                             s_facts *facts);
 bool       env_module_maybe_reload (const s_sym *module, s_env *env,
                                     s_facts *facts);
-s_ident *  env_operator_call_ident (s_env *env, const s_ident *op,
-                                    u8 arity, s_ident *dest);
-bool       env_operator_find (s_env *env, const s_ident *op, u8 arity);
+s8         env_operator_arity (s_env *env, const s_ident *op);
+bool       env_operator_find (s_env *env, const s_ident *op);
 bool       env_operator_is_right_associative (s_env *env,
-                                              const s_ident *op,
-                                              u8 arity);
-s8         env_operator_precedence (s_env *env, const s_ident *op,
-                                    u8 arity);
+                                              const s_ident *op);
+s8         env_operator_precedence (s_env *env, const s_ident *op);
+s_ident *  env_operator_resolve (s_env *env, const s_ident *op,
+                                 u8 arity, s_ident *dest);
 bool       env_tag_ident_is_bound (const s_env *env, const s_tag *tag,
                                    s_facts *facts);
 
diff --git a/libc3/facts.c b/libc3/facts.c
index 1916662..381125d 100644
--- a/libc3/facts.c
+++ b/libc3/facts.c
@@ -177,6 +177,14 @@ s_fact * facts_find_fact (s_facts *facts, const s_fact *fact)
   return result;
 }
 
+s_fact * facts_find_fact_by_tags (s_facts *facts, const s_tag *subject,
+                                  const s_tag *predicate,
+                                  const s_tag *object)
+{
+  s_fact f = {subject, predicate, object, 0};
+  return facts_find_fact(facts, &f);
+}
+
 s_tag * facts_find_tag (s_facts *facts, const s_tag *tag)
 {
   s_set_item__tag *item;
diff --git a/libc3/facts.h b/libc3/facts.h
index d741c7e..3d45038 100644
--- a/libc3/facts.h
+++ b/libc3/facts.h
@@ -57,6 +57,9 @@ bool     facts_unref_tag (s_facts *facts, const s_tag *tag);
 sw       facts_dump (s_facts *facts, s_buf *buf);
 sw       facts_dump_file (s_facts *facts, const s8 *path);
 s_fact * facts_find_fact (s_facts *facts, const s_fact *fact);
+s_fact * facts_find_fact_by_tags (s_facts *facts, const s_tag *subject,
+                                  const s_tag *predicate,
+                                  const s_tag *object);
 s_tag *  facts_find_tag (s_facts *facts, const s_tag *tag);
 sw       facts_log_add (s_log *log, const s_fact *fact);
 sw       facts_log_remove (s_log *log, const s_fact *fact);
diff --git a/libc3/operator.c b/libc3/operator.c
index 890bfb7..27a95f7 100644
--- a/libc3/operator.c
+++ b/libc3/operator.c
@@ -13,23 +13,28 @@
 #include <assert.h>
 #include "c3.h"
 
-s_ident * operator_call_ident (const s_ident *op, u8 arity,
-                               s_ident *dest)
+s8 operator_arity (const s_ident *op)
 {
-  return env_operator_call_ident(&g_c3_env, op, arity, dest);
+  return env_operator_arity(&g_c3_env, op);
 }
 
-bool operator_find (const s_ident *op, u8 arity)
+bool operator_find (const s_ident *op)
 {
-  return env_operator_find(&g_c3_env, op, arity);
+  return env_operator_find(&g_c3_env, op);
 }
 
-bool operator_is_right_associative (const s_ident *op, u8 arity)
+bool operator_is_right_associative (const s_ident *op)
 {
-  return env_operator_is_right_associative(&g_c3_env, op, arity);
+  return env_operator_is_right_associative(&g_c3_env, op);
 }
 
-s8 operator_precedence (const s_ident *op, u8 arity)
+s8 operator_precedence (const s_ident *op)
 {
-  return env_operator_precedence(&g_c3_env, op, arity);
+  return env_operator_precedence(&g_c3_env, op);
+}
+
+s_ident * operator_resolve (const s_ident *op, u8 arity,
+                            s_ident *dest)
+{
+  return env_operator_resolve(&g_c3_env, op, arity, dest);
 }
diff --git a/libc3/operator.h b/libc3/operator.h
index 3bf2e37..548ebfe 100644
--- a/libc3/operator.h
+++ b/libc3/operator.h
@@ -16,10 +16,11 @@
 #include "types.h"
 
 /* Observers */
-s_ident * operator_call_ident (const s_ident *op, u8 arity,
-                               s_ident *dest);
-bool      operator_find (const s_ident *op, u8 arity);
-bool      operator_is_right_associative (const s_ident *op, u8 arity);
-s8        operator_precedence (const s_ident *op, u8 arity);
+s8        operator_arity (const s_ident *op);
+bool      operator_find (const s_ident *op);
+bool      operator_is_right_associative (const s_ident *op);
+s8        operator_precedence (const s_ident *op);
+s_ident * operator_resolve (const s_ident *ident, u8 arity,
+                            s_ident *dest);
 
 #endif /* OPERATOR_H */
diff --git a/test/facts_cursor_test.c b/test/facts_cursor_test.c
index 0e70809..64e3e92 100644
--- a/test/facts_cursor_test.c
+++ b/test/facts_cursor_test.c
@@ -114,7 +114,6 @@ TEST_CASE(facts_cursor_next)
   s_facts_cursor cursor;
   uw i = 0;
   s8 *p[24] = {
-    "a",
     "-0x10000000000000000",
     "-0x100000000",
     "-0x10000",
@@ -133,10 +132,11 @@ TEST_CASE(facts_cursor_next)
     "\"a\"",
     "A",
     ":a",
-    "{a, b}",
     "{:a, :b}",
-    "{{a, b}, {c, d}}",
     "{{:a, :b}, {:c, :d}}",
+    "{{a, b}, {c, d}}",
+    "{a, b}",
+    "a",
     NULL
   };
   s_fact fact[24];
diff --git a/test/facts_test_dump_file.expected.facts b/test/facts_test_dump_file.expected.facts
index 2eb5553..4d3a9e9 100644
--- a/test/facts_test_dump_file.expected.facts
+++ b/test/facts_test_dump_file.expected.facts
@@ -1,6 +1,5 @@
 %{module: C3.Facts.Dump,
   version: 1}
-add {a, a, a}
 add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
 add {-4294967296, -4294967296, -4294967296}
 add {-65536, -65536, -65536}
@@ -19,7 +18,8 @@ add {((), ()), ((), ()), ((), ())}
 add {"a", "a", "a"}
 add {A, A, A}
 add {:a, :a, :a}
-add {{a, b}, {a, b}, {a, b}}
 add {{:a, :b}, {:a, :b}, {:a, :b}}
-add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
 add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+add {{a, b}, {a, b}, {a, b}}
+add {a, a, a}
diff --git a/test/facts_test_save.expected.facts b/test/facts_test_save.expected.facts
index 2eb5553..4d3a9e9 100644
--- a/test/facts_test_save.expected.facts
+++ b/test/facts_test_save.expected.facts
@@ -1,6 +1,5 @@
 %{module: C3.Facts.Dump,
   version: 1}
-add {a, a, a}
 add {-18446744073709551616, -18446744073709551616, -18446744073709551616}
 add {-4294967296, -4294967296, -4294967296}
 add {-65536, -65536, -65536}
@@ -19,7 +18,8 @@ add {((), ()), ((), ()), ((), ())}
 add {"a", "a", "a"}
 add {A, A, A}
 add {:a, :a, :a}
-add {{a, b}, {a, b}, {a, b}}
 add {{:a, :b}, {:a, :b}, {:a, :b}}
-add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
 add {{{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}, {{:a, :b}, {:c, :d}}}
+add {{{a, b}, {c, d}}, {{a, b}, {c, d}}, {{a, b}, {c, d}}}
+add {{a, b}, {a, b}, {a, b}}
+add {a, a, a}