+++ /dev/null
-#!/bin/bash
-usage() {
- echo "Usage: $0 CSOURCE [-o OFILE]" >&2
- exit 1
-}
-if [ $# -lt 1 ]
-then
- usage
-fi
-if [ $# -eq 3 ]
-then
- if [ $2 != "-o" ]
- then
- usage
- fi
- OFILE=$3
-else
- OFILE=a.out
-fi
-CFLAGS=${CFLAGS:-}
-LDLIBS=${LDLIBS:-}
-LDFLAGS=${LDFLAGS:-}
-CC=${CC:-gcc}
-set -xe
-"$CC" $CFLAGS "$1" $LDFLAGS rts.c $LDLIBS -o "$OFILE"
ARRAY_ITER(struct stmt *, st, i, &fundecl->body) {
struct subst *s1 = infer_stmt(gamma, st, at);
s = subst_union(s1, s);
+ subst_apply_g(s, gamma);
} AIEND
// Remove arguments from gamma
return res;
}
+bool gamma_free_in(struct gamma *gamma, struct ident ident)
+{
+ bool free_in = false;
+ for (int i = 0; i<gamma->nentries; i++) {
+ if (gamma->entries[i].scope != 0)
+ continue;
+ if (scheme_free_in(gamma->entries[i].scheme, ident))
+ return true;
+ }
+ return free_in;
+}
+
struct type *gamma_fresh(struct gamma *gamma)
{
char buf[10] = {0};
void gamma_iter(struct gamma *gamma, void *st, void (*iter)(struct ident, struct scheme *, void *));
struct scheme *gamma_lookup(struct gamma *gamma, struct ident ident);
+bool gamma_free_in(struct gamma *gamma, struct ident ident);
struct type *gamma_fresh(struct gamma *gamma);
void gamma_print(struct gamma *gamma, FILE *out);
s->nvar = 0;
s->var = xalloc(nftv, struct ident);
for (int i = 0; i<nftv; i++) {
- if (gamma_lookup(gamma, ftv[i]) != NULL)
+ if (gamma_free_in(gamma, ftv[i]))
continue;
s->nvar++;
s->var[i] = ident_dup(ftv[i]);
return s;
}
+bool scheme_free_in(struct scheme *scheme, struct ident ident)
+{
+ for (int i = 0; i<scheme->nvar; i++)
+ if (ident_cmp(scheme->var[i], ident) == 0)
+ return false;
+ return type_free_in(scheme->type, ident);
+}
+
void scheme_print(struct scheme *scheme, FILE *out)
{
if (scheme == NULL) {
for (int i = 0; i<scheme->nvar; i++) {
if (i > 0)
safe_fprintf(out, " ");
- ident_print(scheme->var[i], stderr);
+ ident_print(scheme->var[i], out);
}
safe_fprintf(out, ": ");
}
struct type *scheme_instantiate(struct gamma *gamma, struct scheme *s);
struct scheme *scheme_create(struct type *t);
struct scheme *scheme_generalise(struct gamma *gamma, struct type *t);
+bool scheme_free_in(struct scheme *scheme, struct ident ident);
void scheme_print(struct scheme *scheme, FILE *out);
void scheme_free(struct scheme *scheme);
break;
}
}
+ gamma_print(gamma, stdout);
gamma_free(gamma);
}
}
}
+bool type_free_in(struct type *r, struct ident ident)
+{
+ switch (r->type) {
+ case tarrow:
+ return type_free_in(r->data.ttuple.l, ident)
+ || type_free_in(r->data.ttuple.r, ident);
+ case tbasic:
+ break;
+ case tlist:
+ return type_free_in(r->data.tlist, ident);
+ case ttuple:
+ return type_free_in(r->data.ttuple.l, ident)
+ || type_free_in(r->data.ttuple.r, ident);
+ case tvar:
+ return ident_cmp(r->data.tvar, ident) == 0;
+ default:
+ die("Unsupported type node: %d\n", r->type);
+ }
+ return false;
+}
+
int type_cmp(struct type *l, struct type *r)
{
if (l == NULL)
struct type *type_dup(struct type *t);
void type_ftv(struct type *r, int *nftv, struct ident **ftv);
+bool type_free_in(struct type *type, struct ident ident);
#endif