// Copyright (c) 2023, Marvin Borner // SPDX-License-Identifier: MIT #include #include #include #include #include static struct term *substitute(struct term *term, struct term *substitution, size_t level) { if (term->type == VAR && term->u.var.index == level) { return substitution; } else if (term->type == ABS) { struct term *new = substitute(term->u.abs.term, substitution, level + 1); if (term->u.abs.term->hash == substitution->hash) return term; /* term_deref(term->u.abs.term); */ struct term *rehashed = term_rehash_abs(term, new); /* term_deref_head(term); */ return rehashed; } else if (term->type == APP) { struct term *lhs = substitute(term->u.app.lhs, substitution, level); struct term *rhs = substitute(term->u.app.rhs, substitution, level); if (term->u.app.lhs->hash == substitution->hash && term->u.app.rhs->hash == substitution->hash) return term; /* if (term->u.app.lhs->hash != substitution->hash) */ /* term_deref(term->u.app.lhs); */ /* if (term->u.app.rhs->hash != substitution->hash) */ /* term_deref(term->u.app.rhs); */ struct term *rehashed = term_rehash_app(term, lhs, rhs); /* term_deref_head(term); */ return rehashed; } fatal("invalid type %d\n", term->type); } // reduction of application // ([X],Y) -> X/Y void reduce(struct term *term) { assert(term->type == APP); assert(term->u.app.lhs->type == ABS); substitute(term->u.app.lhs->u.abs.term, term->u.app.rhs, 0); }