示例#1
0
文件: normalize.c 项目: jroesch/arvo
term* normalize_fuel_intro(context *Sigma, typing_context* Delta, term* t, int fuel) {
  term* ans = make_intro(variable_dup(t->var), term_dup(t->left), t->num_args, t->num_params);
  int i;
  for (i = 0; i < t->num_params; i++) {
    ans->params[i] = normalize_fuel(Sigma, Delta, t->params[i], fuel-1);
  }

  for (i = 0; i < t->num_args; i++) {
    // FIXME: this leaks on error --jrw
    ans->args[i] = normalize_fuel(Sigma, Delta, t->args[i], fuel-1);
    if (!ans->args[i]) goto error;
  }
  return ans;
 error:
  free_term(ans);
  return NULL;
}
示例#2
0
文件: term.c 项目: kleopatra999/arvo
/*
  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;
}
示例#3
0
void make_configs (FILE *configfile, FILE *sourcefile)
{
	make_intro (sourcefile);
	make_lines (configfile, sourcefile);
	make_ending (sourcefile);
}