static term* elim_over_intro(typing_context* Delta, term* t) { check(t && t->args && t->args[t->num_args - 1], "ill formed term"); check(t->tag == ELIM && t->args[t->num_args - 1]->tag == INTRO, "elim_over_intro must be called on an eliminator applied to a constructor"); term* last = t->args[t->num_args - 1]; datatype* T = elim_to_datatype(t->var, Delta); int index = datatype_intro_index(last->var, T); check(index != -1, "bad intro index while evaluating %W", t, print_term); term *app = term_dup(t->args[index + 1]); int i; for (i = 0; i < last->num_args; i++) { app = make_app(app, term_dup(last->args[i])); if (constructor_arg_is_inductive(T, last->var, i)) { term *inductive = term_dup(t); free_term(inductive->args[inductive->num_args - 1]); inductive->args[inductive->num_args - 1] = term_dup(last->args[i]); app = make_app(app, inductive); } } free_term(t); t = NULL; return app; error: return NULL; }
term* normalize_no_unfold(typing_context* Delta, term* t) { if (t == NULL) return NULL; switch (t->tag) { case VAR: { return term_dup(t); } case APP: { term* l = normalize_no_unfold(Delta, t->left); term* r = normalize_no_unfold(Delta, t->right); if (l->tag == LAM) { term* subs = substitute(l->var, r, l->right); free_term(l); free_term(r); return normalize_no_unfold_and_free(Delta, subs); } return make_app(l, r); } case IMPLICIT: case HOLE: case TYPE: case LAM: case PI: return term_dup(t); default: sentinel("unexpected tag %d"); } error: return NULL; }
static term* elim_over_intro(typing_context* Delta, term* t) { check(t && t->args && t->args[t->num_args - 1], "ill formed term"); term* last = t->args[t->num_args - 1]; datatype* T = elim_to_datatype(t->var, Delta); int index = datatype_intro_index(last->var, T); term *app = term_dup(t->args[index + 1]); int i; for (i = 0; i < last->num_args; i++) { app = make_app(app, term_dup(last->args[i])); if (constructor_arg_is_inductive(T, last->var, i)) { term *inductive = term_dup(t); free_term(inductive->args[inductive->num_args - 1]); inductive->args[inductive->num_args - 1] = term_dup(last->args[i]); app = make_app(app, inductive); } } free_term(t); t = NULL; return app; error: return NULL; }
term* whnf(context *Sigma, typing_context* Delta, term* t) { if (t == NULL) return NULL; switch (t->tag) { case VAR: { term* defn = context_lookup(t->var, Sigma); if (defn == NULL) { return term_dup(t); } return whnf(Sigma, Delta, defn); } case APP: { term* l = whnf(Sigma, Delta, t->left); if (l->tag == LAM) { term* subs = substitute(l->var, t->right, l->right); free_term(l); return whnf_and_free(Sigma, Delta, subs); } return make_app(l, term_dup(t->right)); } case ELIM: { term* last = t->args[t->num_args - 1]; term* nlast = whnf(Sigma, Delta, last); term* c = term_dup(t); free_term(c->args[c->num_args - 1]); c->args[c->num_args - 1] = nlast; if (nlast->tag == INTRO) { return whnf_and_free(Sigma, Delta, elim_over_intro(Delta, c)); } else { return c; } } case HOLE: case DATATYPE: case TYPE: case LAM: case INTRO: case PI: case IMPLICIT: return term_dup(t); } }
static term* normalize_fuel_app(context *Sigma, typing_context* Delta, term* t, int fuel) { term *f = normalize_fuel(Sigma, Delta, t->left, fuel-1); term *x = normalize_fuel(Sigma, Delta, t->right, fuel-1); if (!f || !x) goto error; if (f->tag == LAM) { term* subs = substitute(f->var, x, f->right); free_term(f); free_term(x); term* ans = normalize_fuel(Sigma, Delta, subs, fuel-1); free_term(subs); return ans; } return make_app(f, x); error: free_term(f); free_term(x); return NULL; }
/* invariant: no sharing between returned term and *any* arguments. the caller must free the result. */ term* substitute(variable* from, term* to, term* haystack) { if (haystack == NULL) return NULL; check(from != NULL && to != NULL, "substitute requires non-NULL arguments"); check(term_locally_well_formed(to), "substitute requires %W to be locally well-formed", to, print_term); check(term_locally_well_formed(haystack),"substitute requires %W to be locally well-formed", haystack, print_term); switch(haystack->tag) { case VAR: if (variable_equal(from, haystack->var)) { return term_dup(to); } else { return term_dup(haystack); } case HOLE: return term_dup(haystack); case LAM: if (variable_equal(from, haystack->var)) { return make_lambda(variable_dup(haystack->var), substitute(from, to, haystack->left), term_dup(haystack->right)); } else { if (is_free(haystack->var, to)) { variable *g = gensym(haystack->var->name); term *tg = make_var(g); term* new_haystack = make_lambda(variable_dup(g), term_dup(haystack->left), substitute(haystack->var, tg, haystack->right)); free_term(tg); term* ans = substitute(from, to, new_haystack); free_term(new_haystack); return ans; } return make_lambda(variable_dup(haystack->var), substitute(from, to, haystack->left), substitute(from, to, haystack->right)); } case PI: if (variable_equal(from, haystack->var)) { return make_pi(variable_dup(haystack->var), substitute(from, to, haystack->left), term_dup(haystack->right)); } else { if (is_free(haystack->var, to)) { variable *g = gensym(haystack->var->name); term *tg = make_var(g); term* new_haystack = make_pi(variable_dup(g), term_dup(haystack->left), substitute(haystack->var, tg, haystack->right)); free_term(tg); term* ans = substitute(from, to, new_haystack); free_term(new_haystack); return ans; } return make_pi(variable_dup(haystack->var), substitute(from, to, haystack->left), substitute(from, to, haystack->right)); } case APP: return make_app(substitute(from, to, haystack->left), substitute(from, to, haystack->right)); case TYPE: return term_dup(haystack); case DATATYPE: { term* ans = make_datatype_term(variable_dup(haystack->var), haystack->num_params, haystack->num_indices); #define SUB_VEC(dst, src, n) do { \ int __i; \ for (__i = 0; __i < n; __i++) { \ dst[__i] = substitute(from, to, src[__i]); \ } \ } while(0) SUB_VEC(ans->params, haystack->params, haystack->num_params); SUB_VEC(ans->indices, haystack->indices, haystack->num_indices); return ans; } case INTRO: { term* ans = make_intro(variable_dup(haystack->var), substitute(from, to, haystack->left), haystack->num_args, haystack->num_params, haystack->num_indices); SUB_VEC(ans->args, haystack->args, haystack->num_args); SUB_VEC(ans->params, haystack->params, haystack->num_params); SUB_VEC(ans->indices, haystack->indices, haystack->num_indices); return ans; } case ELIM: { term* ans = make_elim(variable_dup(haystack->var), haystack->num_args, haystack->num_params, haystack->num_indices); SUB_VEC(ans->args, haystack->args, haystack->num_args); SUB_VEC(ans->params, haystack->params, haystack->num_params); SUB_VEC(ans->indices, haystack->indices, haystack->num_indices); return ans; } case IMPLICIT: return term_dup(haystack); default: sentinel("malformed term with tag %d", haystack->tag); } error: return NULL; }
int CompilationEnvironment::make_exe( const String &exe, const String &cpp ) { return make_app( exe, cpp, false, true ); }
int CompilationEnvironment::make_lib( const String &lib, const String &cpp, bool dyn, bool want_libs ) { return make_app( lib, cpp, true, dyn, want_libs ); }