Commit 493d1f2b328ca221047f38903929270edf516cce

Thomas de Grivel 2023-02-10T17:53:26

wip fn

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
diff --git a/lib/c3.facts b/lib/c3.facts
index 8359316..71b747a 100644
--- a/lib/c3.facts
+++ b/lib/c3.facts
@@ -5,5 +5,5 @@
 {C3, :name, "C3"}
 {C3, :path, "c3.facts"}
 {C3, :function, C3.first}
-{C3.first, :fn, :fn}
-%{hash: 0x55B52EDF4CF3E808}
+{C3.first, :fn, fn ([a | _b]) { a }}
+%{hash: 0x08854B651E330862}
diff --git a/libc3/buf_inspect.c b/libc3/buf_inspect.c
index 4b3361f..9419a4c 100644
--- a/libc3/buf_inspect.c
+++ b/libc3/buf_inspect.c
@@ -223,6 +223,93 @@ sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec)
   return result;
 }
 
+sw buf_inspect_fn (s_buf *buf, const s_fn *fn)
+{
+  sw r;
+  sw result = 0;
+  if ((r = buf_write_1(buf, "fn ")) < 0)
+    return r;
+  result += r;
+  if ((r = buf_inspect_fn_pattern(buf, fn->pattern)) < 0)
+    return r;
+  result += r;
+  if ((r = buf_write_u8(buf, ' ')) < 0)
+    return r;
+  result += r;
+  if ((r = buf_inspect_fn_algo(buf, fn->algo)) < 0)
+    return r;
+  result += r;
+  return result;
+}
+
+sw buf_inspect_fn_algo (s_buf *buf, const s_list *algo)
+{
+  sw r;
+  sw result = 0;
+  assert(buf);
+  if (! algo) {
+    if ((r = buf_write_1(buf, "{}")) < 0)
+      return r;
+    result += r;
+    return result;
+  }
+  if (! list_next(algo)) {
+    if ((r = buf_write_1(buf, "{ ")) <= 0)
+      return r;
+    result += r;
+    if ((r = buf_inspect_tag(buf, &algo->tag)) < 0)
+      return r;
+    result += r;
+    if ((r = buf_write_1(buf, " }")) <= 0)
+      return r;
+    result += r;
+    return result;
+  }
+  if ((r = buf_write_1(buf, "{\n  ")) <= 0)
+    return r;
+  result += r;
+  while (algo) {
+    if ((r = buf_inspect_tag(buf, &algo->tag)) < 0)
+      return r;
+    result += r;
+    algo = list_next(algo);
+    if (algo) {
+      if ((r = buf_write_1(buf, ";\n  ")) < 0)
+        return r;
+      result += r;
+    }
+  }
+  if ((r = buf_write_1(buf, "\n}")) < 0)
+    return r;
+  result += r;
+  return result;
+}
+
+sw buf_inspect_fn_pattern (s_buf *buf, const s_list *pattern)
+{
+  sw r;
+  sw result = 0;
+  assert(buf);
+  if ((r = buf_write_u8(buf, '(')) <= 0)
+    return r;
+  result += r;
+  while (pattern) {
+    if ((r = buf_inspect_tag(buf, &pattern->tag)) < 0)
+      return r;
+    result += r;
+    pattern = list_next(pattern);
+    if (pattern) {
+      if ((r = buf_write_1(buf, ", ")) < 0)
+        return r;
+      result += r;
+    }
+  }
+  if ((r = buf_write_u8(buf, ')')) < 0)
+    return r;
+  result += r;
+  return result;
+}
+
 sw buf_inspect_ident (s_buf *buf, const s_ident *ident)
 {
   sw r;
@@ -797,8 +884,7 @@ sw buf_inspect_tag (s_buf *buf, const s_tag *tag)
     return buf_inspect_character(buf, tag->data.character);
   case TAG_F32:     return buf_inspect_f32(buf, tag->data.f32);
   case TAG_F64:     return buf_inspect_f64(buf, tag->data.f64);
-  case TAG_FN:
-    return buf_inspect_uw_hex(buf, (uw) &tag->data.fn);
+  case TAG_FN:      return buf_inspect_fn(buf, &tag->data.fn);
   case TAG_IDENT:   return buf_inspect_ident(buf, &tag->data.ident);
   case TAG_INTEGER: return buf_inspect_integer(buf, &tag->data.integer);
   case TAG_LIST:    return buf_inspect_list(buf, tag->data.list);
diff --git a/libc3/buf_inspect.h b/libc3/buf_inspect.h
index d2af0e2..a735cf4 100644
--- a/libc3/buf_inspect.h
+++ b/libc3/buf_inspect.h
@@ -42,6 +42,8 @@ sw buf_inspect_fact (s_buf *buf, const s_fact *fact);
 sw buf_inspect_fact_size (const s_fact *fact);
 sw buf_inspect_fact_spec (s_buf *buf, p_facts_spec spec);
 sw buf_inspect_fn (s_buf *buf, const s_fn *fn);
+sw buf_inspect_fn_algo (s_buf *buf, const s_list *algo);
+sw buf_inspect_fn_pattern (s_buf *buf, const s_list *pattern);
 sw buf_inspect_fn_size (const s_fn *fn);
 sw buf_inspect_ident (s_buf *buf, const s_ident *ident);
 sw buf_inspect_ident_size (const s_ident *ident);
diff --git a/libc3/buf_parse.c b/libc3/buf_parse.c
index 5e23b9e..75f01d2 100644
--- a/libc3/buf_parse.c
+++ b/libc3/buf_parse.c
@@ -431,21 +431,26 @@ sw buf_parse_fn (s_buf *buf, s_fn *dest)
   assert(dest);
   buf_save_init(buf, &save);
   if ((r = buf_read_1(buf, "fn")) <= 0)
-    return r;
+    goto clean;
   result += r;
   if ((r = buf_ignore_spaces(buf)) <= 0)
     goto restore;
   result += r;
   fn_init(dest);
-  if ((r = buf_parse_fn_pattern(buf, &dest->pattern)) < 0)
+  if ((r = buf_parse_fn_pattern(buf, &dest->pattern)) < 0) {
+    warnx("buf_parse_fn: invalid pattern");
     goto restore;
+  }
   dest->arity = list_length(dest->pattern);
   result += r;
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
   result += r;
-  if ((r = buf_parse_fn_algo(buf, &dest->algo)) <= 0)
+  if ((r = buf_parse_fn_algo(buf, &dest->algo)) <= 0) {
+    buf_inspect_fn(&g_c3_env.err, dest);
+    warnx("buf_parse_fn: invalid program");
     goto restore;
+  }
   r = result;
   goto clean;
  restore:
@@ -463,8 +468,9 @@ sw buf_parse_fn_algo (s_buf *buf, s_list **dest)
   s_tag tag;
   assert(buf);
   assert(dest);
+  buf_save_init(buf, &save);
   if ((r = buf_read_1(buf, "{")) <= 0)
-    return r;
+    goto clean;
   result += r;
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
@@ -477,10 +483,15 @@ sw buf_parse_fn_algo (s_buf *buf, s_list **dest)
     dest = &(*dest)->next.data.list;
     if ((r = buf_ignore_spaces(buf)) < 0)
       goto restore;
+    result += r;
     if ((r = buf_read_1(buf, ";")) < 0)
       goto restore;
+    if (! r)
+      break;
+    result += r;
     if ((r = buf_ignore_spaces(buf)) < 0)
       goto restore;
+    result += r;
   }
   if (r < 0)
     goto restore;
@@ -506,7 +517,7 @@ sw buf_parse_fn_pattern (s_buf *buf, s_list **dest)
   assert(dest);
   buf_save_init(buf, &save);
   if ((r = buf_read_1(buf, "(")) < 0)
-    goto restore;
+    goto clean;
   result += r;
   if ((r = buf_ignore_spaces(buf)) < 0)
     goto restore;
@@ -1271,6 +1282,7 @@ sw buf_parse_tag (s_buf *buf, s_tag *dest)
       (r = buf_parse_tag_str(buf, dest)) != 0 ||
       (r = buf_parse_tag_tuple(buf, dest)) != 0 ||
       (r = buf_parse_tag_quote(buf, dest)) != 0 ||
+      (r = buf_parse_tag_fn(buf, dest)) != 0 ||
       (r = buf_parse_tag_call(buf, dest)) != 0 ||
       (r = buf_parse_tag_ident(buf, dest)) != 0 ||
       (r = buf_parse_tag_sym(buf, dest)) != 0)
@@ -1317,6 +1329,14 @@ sw buf_parse_tag_character (s_buf *buf, s_tag *dest)
   return r;
 }
 
+sw buf_parse_tag_fn (s_buf *buf, s_tag *dest)
+{
+  sw r;
+  if ((r = buf_parse_fn(buf, &dest->data.fn)) > 0)
+    dest->type.type = TAG_FN;
+  return r;
+}
+
 sw buf_parse_tag_ident (s_buf *buf, s_tag *dest)
 {
   sw r;
diff --git a/libc3/buf_parse.h b/libc3/buf_parse.h
index b659357..35288ba 100644
--- a/libc3/buf_parse.h
+++ b/libc3/buf_parse.h
@@ -65,6 +65,7 @@ sw buf_parse_tag (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_bool (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_call (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_character (s_buf *buf, s_tag *dest);
+sw buf_parse_tag_fn (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_ident (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_integer (s_buf *buf, s_tag *dest);
 sw buf_parse_tag_list (s_buf *buf, s_tag *dest);
diff --git a/libc3/env.c b/libc3/env.c
index 6f61e11..d4e9191 100644
--- a/libc3/env.c
+++ b/libc3/env.c
@@ -17,6 +17,7 @@
 #include <stdlib.h>
 #include "binding.h"
 #include "c3.h"
+#include "error.h"
 #include "error_handler.h"
 #include "frame.h"
 
@@ -124,43 +125,39 @@ s_tag * env_eval_call (s_env *env, const s_call *call, s_tag *dest)
   return result;
 }
 
+s_list * env_eval_call_arguments (s_env *env, s_list *args)
+{
+  s_list **dest;
+  s_list *result;
+  dest = &result;
+  while (args) {
+    *dest = list_new();
+    env_eval_tag(env, &args->tag, &(*dest)->tag);
+    tag_init_list(&(*dest)->next, NULL);
+    dest = &(*dest)->next.data.list;
+    args = list_next(args);
+  }
+  return result;
+}
+
 s_tag * env_eval_call_fn (s_env *env, const s_call *call, s_tag *dest)
 {
-  s_arg *args;
-  s_list *call_args;
+  s_list *args;
   s_frame frame;
   s_fn *fn;
-  s_tag tmp;
+  s_list *tmp;
   assert(env);
   assert(call);
   assert(dest);
   fn = call->fn;
   assert(fn);
   frame_init(&frame, env->frame);
-  frame.bindings = fn->bindings;
-  args = fn->args;
-  call_args = call->arguments;
-  while (args) {
-    if (! call_args) {
-      assert(! "env_eval_call_fn: missing argument");
-      errx(1, "env_eval_call_fn: missing argument");
-      return NULL;
-    }
-    /* TODO: check type */
-    env_eval_tag(env, &call_args->tag, &tmp);
-    frame.bindings = binding_new(args->name, &call_args->tag,
-                                 frame.bindings);
-    args = args->next;
-    call_args = list_next(call_args);
-  }
-  if (call_args) {
-    assert(! "env_eval_call_fn: too many arguments");
-    errx(1, "env_eval_call_fn: too many arguments");
-    return NULL;
-  }
   env->frame = &frame;
+  args = env_eval_call_arguments(env, call->arguments);
+  env_eval_equal_list(env, fn->pattern, args, &tmp);
   env_eval_progn(env, fn->algo, dest);
   env->frame = frame_clean(&frame);
+  list_delete_all(tmp);
   return dest;
 }
 
@@ -176,6 +173,130 @@ s_tag * env_eval_call_macro (s_env *env, const s_call *call, s_tag *dest)
   return dest;
 }
 
+bool env_eval_equal_list (s_env *env, const s_list *a, const s_list *b,
+                          s_list **dest)
+{
+  s_list *tmp;
+  s_list **t;
+  t = &tmp;
+  while (1) {
+    if (! a && ! b) {
+      *t = NULL;
+      goto ok;
+    }
+    if (! a)
+      goto ko;
+    if (! b)
+      goto ko;
+    *t = list_new();
+    if (! env_eval_equal_tag(env, &a->tag, &b->tag, &(*t)->tag))
+      goto ko;
+    a = list_next(a);
+    b = list_next(b);
+    t = &(*t)->next.data.list;
+  }
+ ok:
+  *dest = tmp;
+  return true;
+ ko:
+  list_delete_all(tmp);
+  return false;
+}
+
+bool env_eval_equal_tag (s_env *env, const s_tag *a, const s_tag *b,
+                         s_tag *dest)
+{
+  assert(env);
+  assert(a);
+  assert(b);
+  assert(dest);
+  if (a->type.type == TAG_IDENT) {
+    if (b->type.type == TAG_IDENT)
+      warnx("TAG_IDENT = TAG_IDENT");
+    tag_copy(b, dest);
+    frame_binding_new(env->frame, a->data.ident.sym, dest);
+    return true;
+  }
+  if (b->type.type == TAG_IDENT) {
+    tag_copy(a, dest);
+    frame_binding_new(env->frame, b->data.ident.sym, dest);
+    return true;
+  }
+  if (a->type.type != b->type.type) {
+    warnx("env_eval_equal_tag: type mismatch");
+    return false;
+  }
+  switch (a->type.type) {
+  case TAG_VOID:
+    tag_init_void(dest);
+    return true;
+  case TAG_IDENT:
+    error("env_eval_equal_tag: TAG_IDENT");
+  case TAG_LIST:
+    tag_init_list(dest, NULL);
+    return env_eval_equal_list(env, a->data.list, b->data.list,
+                               &dest->data.list);
+  case TAG_TUPLE:
+    dest->type.type = TAG_TUPLE;
+    return env_eval_equal_tuple(env, &a->data.tuple, &b->data.tuple,
+                                &dest->data.tuple);
+  case TAG_BOOL:
+  case TAG_CALL:
+  case TAG_CALL_FN:
+  case TAG_CALL_MACRO:
+  case TAG_CHARACTER:
+  case TAG_F32:
+  case TAG_F64:
+  case TAG_FN:
+  case TAG_INTEGER:
+  case TAG_PTAG:
+  case TAG_QUOTE:
+  case TAG_S16:
+  case TAG_S32:
+  case TAG_S64:
+  case TAG_S8:
+  case TAG_STR:
+  case TAG_SYM:
+  case TAG_U16:
+  case TAG_U32:
+  case TAG_U64:
+  case TAG_U8:
+  case TAG_VAR:
+    if (compare_tag(a, b)) {
+      warnx("env_eval_compare_tag: value mismatch");
+      return false;
+    }
+    tag_copy(a, dest);
+    return true;
+  }
+  error("env_eval_equal_tag: invalid tag");
+  return false;
+}
+
+bool env_eval_equal_tuple (s_env *env, const s_tuple *a,
+                           const s_tuple *b, s_tuple *dest)
+{
+  uw i;
+  s_tuple tmp;
+  assert(env);
+  assert(a);
+  assert(b);
+  assert(dest);
+  if (a->count != b->count)
+    return false;
+  tuple_init(&tmp, a->count);
+  i = 0;
+  while (i < a->count) {
+    if (! env_eval_equal_tag(env, a->tag + i, b->tag + i, tmp.tag + i)) {
+      tuple_clean(&tmp);
+      return false;
+    }
+    i++;
+  }
+  *dest = tmp;
+  return true;
+}
+
 const s_tag * env_eval_ident (s_env *env, const s_ident *ident)
 {
   const s_tag *tag;
diff --git a/libc3/env.h b/libc3/env.h
index 5371f8c..aa368b0 100644
--- a/libc3/env.h
+++ b/libc3/env.h
@@ -23,12 +23,20 @@ void    env_clean (s_env *env);
 s_env * env_init (s_env *env);
 
 /* modifiers */
-s_tag *       env_eval_call_fn (s_env *env, const s_call *call, s_tag *dest);
+s_tag *       env_eval_call_fn (s_env *env, const s_call *call,
+                                s_tag *dest);
 s_tag *       env_eval_call_macro (s_env *env, const s_call *call,
                                    s_tag *dest);
+bool          env_eval_equal_list (s_env *env, const s_list *a,
+                                   const s_list *b, s_list **dest);
+bool          env_eval_equal_tag (s_env *env, const s_tag *a,
+                                  const s_tag *b, s_tag *dest);
+bool          env_eval_equal_tuple (s_env *env, const s_tuple *a,
+                                    const s_tuple *b, s_tuple *dest);
 s_tag *       env_eval_fn (s_env *env, const s_fn *fn, s_tag *dest);
 const s_tag * env_eval_ident (s_env *env, const s_ident *ident);
-s_tag *       env_eval_progn (s_env *env, const s_list *program, s_tag *dest);
+s_tag *       env_eval_progn (s_env *env, const s_list *program,
+                              s_tag *dest);
 s_tag *       env_eval_tag (s_env *env, const s_tag *tag, s_tag *dest);
 s_module *    env_module_load (s_env *env, s_module *module,
                                const s_sym *name, s_facts *facts);
diff --git a/libc3/error.h b/libc3/error.h
index 080ee07..9f5f607 100644
--- a/libc3/error.h
+++ b/libc3/error.h
@@ -16,6 +16,12 @@
 
 #include "types.h"
 
+#define error(message)                                                 \
+  do {                                                                 \
+    assert(! message);                                                 \
+    errx(1, "%s: %s", __FUNCTION__, message);                          \
+  } while (0)
+
 void error_print (s_buf *buf, const s_error_handler *error_handler);
 void error_print_backtrace (s_buf *buf, const s_list *backtrace);
 
diff --git a/libc3/facts_cursor.c b/libc3/facts_cursor.c
index 595674b..d41c23d 100644
--- a/libc3/facts_cursor.c
+++ b/libc3/facts_cursor.c
@@ -64,11 +64,11 @@ s_fact * facts_cursor_next (s_facts_cursor *cursor)
   if (cursor->node) {
     s_fact *fact = cursor->node->fact;
     if (cursor->var_subject)
-      cursor->var_subject->data.var = fact->subject;
+      *cursor->var_subject = *fact->subject;
     if (cursor->var_predicate)
-      cursor->var_predicate->data.var = fact->predicate;
+      *cursor->var_predicate = *fact->predicate;
     if (cursor->var_object)
-      cursor->var_object->data.var = fact->object;
+      *cursor->var_object = *fact->object;
     return fact;
   }
   if (cursor->var_subject)
diff --git a/libc3/frame.c b/libc3/frame.c
index ca2bac3..3283e49 100644
--- a/libc3/frame.c
+++ b/libc3/frame.c
@@ -18,6 +18,11 @@
 #include "frame.h"
 #include "list.h"
 
+void frame_binding_new (s_frame *frame, const s_sym *name, s_tag *value)
+{
+  frame->bindings = binding_new(name, value, frame->bindings);
+}
+
 s_frame * frame_clean (s_frame *frame)
 {
   s_frame *next;
diff --git a/libc3/frame.h b/libc3/frame.h
index 6cd6122..3ee0890 100644
--- a/libc3/frame.h
+++ b/libc3/frame.h
@@ -27,6 +27,9 @@ s_frame * frame_new ();
 void frame_delete (s_frame *frame);
 void frame_delete_all (s_frame *frame);
 
+/* modifiers */
+void frame_binding_new(s_frame *frame, const s_sym *name, s_tag *value);
+
 /* observers */
 const s_tag * frame_get (s_frame *frame, const s_sym *sym);
 
diff --git a/libc3/hash.c b/libc3/hash.c
index d44d6f2..b72e98f 100644
--- a/libc3/hash.c
+++ b/libc3/hash.c
@@ -94,6 +94,14 @@ void hash_update_fact (t_hash *hash, const s_fact *fact)
   hash_update_tag(hash, fact->object);
 }
 
+void hash_update_fn (t_hash *hash, const s_fn *fn)
+{
+  const s8 type[] = "fn";
+  hash_update(hash, type, sizeof(type));
+  hash_update_list(hash, fn->pattern);
+  hash_update_list(hash, fn->algo);
+}
+
 void hash_update_ident (t_hash *hash, const s_ident *ident)
 {
   assert(hash);
@@ -124,7 +132,7 @@ void hash_update_list (t_hash *hash, const s_list *list)
 {
   const s_list *last;
   const s8 type[] = "list";
-  hash_update(hash, &type, sizeof(type));
+  hash_update(hash, type, sizeof(type));
   if (list) {
     while (list) {
       hash_update_tag(hash, &list->tag);
@@ -191,7 +199,7 @@ void hash_update_tag (t_hash *hash, const s_tag *tag)
     hash_update_character(hash, tag->data.character);          break;
   case TAG_F32: hash_update_f32(hash, tag->data.f32);          break;
   case TAG_F64: hash_update_f64(hash, tag->data.f64);          break;
-  case TAG_FN: hash_update_u64(hash, (u64) tag);               break;
+  case TAG_FN: hash_update_fn(hash, &tag->data.fn);            break;
   case TAG_IDENT: hash_update_ident(hash, &tag->data.ident);   break;
   case TAG_INTEGER:
     hash_update_integer(hash, &tag->data.integer);             break;
diff --git a/libc3/hash.h b/libc3/hash.h
index 37aa23e..50cd37c 100644
--- a/libc3/hash.h
+++ b/libc3/hash.h
@@ -27,9 +27,10 @@ void hash_update (t_hash *hash, const void *data, uw size);
 void hash_update_1 (t_hash *hash, const s8 *p);
 void hash_update_bool (t_hash *hash, e_bool b);
 void hash_update_call (t_hash *hash, const s_call *call);
-void hash_update_fact (t_hash *hash, const s_fact *fact);
 HASH_UPDATE_PROTOTYPE(f32);
 HASH_UPDATE_PROTOTYPE(f64);
+void hash_update_fact (t_hash *hash, const s_fact *fact);
+void hash_update_fn (t_hash *hash, const s_fn *fn);
 void hash_update_ident (t_hash *hash, const s_ident *ident);
 void hash_update_integer (t_hash *hash, const s_integer *i);
 void hash_update_list (t_hash *hash, const s_list *list);
diff --git a/libc3/str.c b/libc3/str.c
index a81de3c..c4ad27a 100644
--- a/libc3/str.c
+++ b/libc3/str.c
@@ -85,7 +85,7 @@ s_str * str_init_1 (s_str *str, s8 *free, const s8 *p)
 s_str * str_init_alloc (s_str *str, uw size, const s8 *p)
 {
   assert(str);
-  str->free.p = malloc(size);
+  str->free.p = calloc(size + 1, 1);
   str->size = size;
   str->ptr.p = str->free.p;
   if (! str->ptr.p)
@@ -98,7 +98,7 @@ s_str * str_init_dup (s_str *str, const s_str *src)
 {
   assert(str);
   assert(src);
-  str->free.p = malloc(src->size);
+  str->free.p = calloc(src->size + 1, 1);
   str->size = src->size;
   str->ptr.p = str->free.p;
   memcpy(str->free.p, src->ptr.p, str->size);
@@ -111,7 +111,7 @@ s_str * str_init_dup_1 (s_str *str, const s8 *src)
   assert(str);
   assert(src);
   len = strlen(src);
-  str->free.p = malloc(len + 1);
+  str->free.p = calloc(len + 1, 1);
   str->size = len;
   str->ptr.p = str->free.p;
   memcpy(str->free.p, src, len + 1);
diff --git a/test/facts_with_test.c b/test/facts_with_test.c
index abcdcf3..1de9286 100644
--- a/test/facts_with_test.c
+++ b/test/facts_with_test.c
@@ -116,21 +116,21 @@ void facts_with_test_ ()
                                             &object,
                                             NULL, NULL });
   TEST_ASSERT(facts_with_cursor_next(&cursor));
-  TAG_TEST_EQ(subject.data.var, tag);
-  TAG_TEST_EQ(predicate.data.var, tag + 1);
-  TAG_TEST_EQ(object.data.var, tag + 2);
+  TAG_TEST_EQ(&subject, tag);
+  TAG_TEST_EQ(&predicate, tag + 1);
+  TAG_TEST_EQ(&object, tag + 2);
   TEST_ASSERT(facts_with_cursor_next(&cursor));
-  TAG_TEST_EQ(subject.data.var, tag);
-  TAG_TEST_EQ(predicate.data.var, tag + 1);
-  TAG_TEST_EQ(object.data.var, tag + 3);
+  TAG_TEST_EQ(&subject, tag);
+  TAG_TEST_EQ(&predicate, tag + 1);
+  TAG_TEST_EQ(&object, tag + 3);
   TEST_ASSERT(facts_with_cursor_next(&cursor));
-  TAG_TEST_EQ(subject.data.var, tag);
-  TAG_TEST_EQ(predicate.data.var, tag + 4);
-  TAG_TEST_EQ(object.data.var, tag + 3);
+  TAG_TEST_EQ(&subject, tag);
+  TAG_TEST_EQ(&predicate, tag + 4);
+  TAG_TEST_EQ(&object, tag + 3);
   TEST_ASSERT(facts_with_cursor_next(&cursor));
-  TAG_TEST_EQ(subject.data.var, tag + 5);
-  TAG_TEST_EQ(predicate.data.var, tag + 1);
-  TAG_TEST_EQ(object.data.var, tag + 2);
+  TAG_TEST_EQ(&subject, tag + 5);
+  TAG_TEST_EQ(&predicate, tag + 1);
+  TAG_TEST_EQ(&object, tag + 2);
   TEST_ASSERT(! facts_with_cursor_next(&cursor));
   TEST_ASSERT(! facts_with_cursor_next(&cursor));
   facts_with_cursor_clean(&cursor);
diff --git a/test/ic3/fn.out.expected b/test/ic3/fn.out.expected
index 3f20c4b..501a0ae 100644
--- a/test/ic3/fn.out.expected
+++ b/test/ic3/fn.out.expected
@@ -1,3 +1,6 @@
-fn ...
-fn ...
-fn ...
+fn (x) { x }
+
+fn (x, _y) { x }
+
+fn ([x | _y]) { x }
+