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
|
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <log.h>
#include <schedule.h>
#include <term.h>
#include <map.h>
static void term_destroy_parent(void *item)
{
struct term *term = *(struct term **)item;
term_destroy_head(term, 1);
}
// doesn't care about ref count
// this is needed to destroy possibly multiple parents
void term_destroy_head(struct term *term, char including_parents)
{
debug("destroying head of %lx\n", term->hash & HASH_MASK);
map_delete(map_all_terms(), term);
// recursively destroy own parents
if (including_parents) {
map_destroy(term->parents);
// remove head from child's parents
if (term->type == ABS) {
map_delete(term->u.abs.term->parents, term);
} else if (term->type == APP) {
map_delete(term->u.app.lhs->parents, term);
map_delete(term->u.app.rhs->parents, term);
}
}
schedule_remove(term);
free(term);
}
struct term *term_new(term_type_t type, hash_t hash, size_t depth)
{
struct term *term = malloc(sizeof(*term));
if (!term)
fatal("out of memory!\n");
term->type = type;
term->refs = 1;
term->hash = hash;
term->depth = depth;
term->parents =
hashmap_new(sizeof(struct term *), 0, term_destroy_parent);
return term;
}
char term_is_beta_redex(struct term *term)
{
return term->type == APP && term->u.app.lhs->type == ABS;
}
size_t term_print(struct term *term)
{
switch (term->type) {
case ABS:
fprintf(stderr, "[");
size_t t = term_print(term->u.abs.term);
fprintf(stderr, "]");
return 2 + t;
case APP:
fprintf(stderr, "(");
size_t l = term_print(term->u.app.lhs);
fprintf(stderr, " ");
size_t r = term_print(term->u.app.rhs);
fprintf(stderr, ")");
return 3 + l + r;
case VAR:
return fprintf(stderr, "%ld", term->u.var.index);
default:
fatal("invalid type %d\n", term->type);
}
}
struct term *term_rehash_abs(struct term *head, struct term *term,
int including_parents)
{
debug("rehashing abs %lx (%lx)\n", head->hash & HASH_MASK,
term->hash & HASH_MASK);
hash_t res =
hash((uint8_t *)&head->type, sizeof(head->type), term->hash);
if (res == head->hash)
return head;
struct term *match = map_get(map_all_terms(), res);
if (match) { // already exists
// TODO: something different if match->u.abs.term == term)?
/* term_refer_head(match, head->depth); */
/* term_deref_head(head, including_parents); */
return match;
} else { // create new
struct term *new = term_new(ABS, res, head->depth);
// TODO: Clone parents
new->u.abs.term = term;
map_set(map_all_terms(), new);
map_set(term->parents, new);
/* term_deref_head(head, including_parents); */
return new;
}
}
// TODO: main problem is reparenting substitution tickle-up
// TODO: investigate reparenting using gen.blc? ?!!?!
struct term *term_rehash_app(struct term *head, struct term *lhs,
struct term *rhs, int including_parents)
{
debug("rehashing app %lx (%lx, %lx)\n", head->hash & HASH_MASK,
lhs->hash & HASH_MASK, rhs->hash & HASH_MASK);
hash_t res =
hash((uint8_t *)&head->type, sizeof(head->type), lhs->hash);
res = hash((uint8_t *)&res, sizeof(res), rhs->hash);
if (res == head->hash)
return head;
struct term *match = map_get(map_all_terms(), res);
if (match) { // already exists
/* term_refer_head(match, head->depth); */
/* term_deref(head, including_parents); */
return match;
} else { // create new
struct term *new = term_new(APP, res, head->depth);
// TODO: Clone parents
new->u.app.lhs = lhs;
new->u.app.rhs = rhs;
map_set(map_all_terms(), new);
map_set(lhs->parents, new);
map_set(rhs->parents, new);
/* term_deref_head(head, including_parents); */
return new;
}
}
struct term *term_rehash_var(struct term *head, size_t index,
int including_parents)
{
debug("rehashing var %lx (%lu)\n", head->hash & HASH_MASK, index);
hash_t res = hash((uint8_t *)&head->type, sizeof(head->type), index);
if (res == head->hash)
return head;
struct term *match = map_get(map_all_terms(), res);
if (match) { // already exists
term_refer_head(match, head->depth);
/* term_deref(head, including_parents); */
return match;
} else { // create new
struct term *new = term_new(VAR, res, head->depth);
// TODO: Clone parents
new->u.var.index = index;
map_set(map_all_terms(), new);
/* term_deref_head(head, including_parents); */
return new;
}
}
struct term *term_rehash(struct term *term, int including_parents)
{
if (term->type == ABS)
return term_rehash_abs(term, term->u.abs.term,
including_parents);
if (term->type == APP)
return term_rehash_app(term, term->u.app.lhs, term->u.app.rhs,
including_parents);
if (term->type == VAR)
return term_rehash_var(term, term->u.var.index,
including_parents);
fatal("invalid type %d\n", term->type);
}
void term_rehash_parents(struct term *term)
{
if (!term->parents)
return;
debug("rehashing parents of %lx\n", term->hash & HASH_MASK);
// we need to convert the parent hashmap to a list
// so we can replace the rehashed elements while looping
// TODO: Abstract list lib?
struct parent_list {
struct term *term;
struct parent_list *next;
};
struct parent_list *parents = calloc(sizeof(*parents), 1);
size_t iter = 0;
void *iter_val;
while (hashmap_iter(term->parents, &iter, &iter_val)) {
struct parent_list *new = malloc(sizeof(*parents));
new->term = *(struct term **)iter_val;
new->next = parents;
parents = new;
}
struct parent_list *iterator = parents;
while (iterator && iterator->term) {
struct term *parent = iterator->term;
hash_t previous = parent->hash;
struct term *new = term_rehash(parent, 1);
if (previous == new->hash) {
struct parent_list *next = iterator->next;
free(iterator);
iterator = next;
continue;
}
map_delete(term->parents, parent);
map_set(term->parents, new);
term_rehash_parents(new);
struct parent_list *next = iterator->next;
free(iterator);
iterator = next;
}
if (iterator == parents)
free(parents);
}
void term_refer_head(struct term *term, size_t depth)
{
debug("referring head of %lx\n", term->hash & HASH_MASK);
term->refs++;
if (depth < term->depth) // lower depths are more important
term->depth = depth;
}
void term_refer(struct term *term, size_t depth)
{
if (term->type == ABS) {
term_refer(term->u.abs.term, depth + 1);
} else if (term->type == APP) {
term_refer(term->u.app.lhs, depth + 1);
term_refer(term->u.app.rhs, depth + 1);
}
term_refer_head(term, depth);
}
char term_deref_head(struct term *term, char destroy_parents)
{
debug("dereferring head of %lx\n", term->hash & HASH_MASK);
assert(term->refs > 0);
term->refs--;
if (!term->refs) {
term_destroy_head(term, destroy_parents);
return 1;
}
return 0;
}
// returns 1 if destroyed
char term_deref(struct term *term, char destroy_parents)
{
char a = 0, b = 0;
if (term->type == ABS) {
a = term_deref(term->u.abs.term, destroy_parents);
} else if (term->type == APP) {
a = term_deref(term->u.app.lhs, destroy_parents);
b = term_deref(term->u.app.rhs, destroy_parents);
}
if (a || b)
return 1;
return term_deref_head(term, destroy_parents);
}
|