Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify #51

Merged
merged 33 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4c97319
indent
billhails Mar 1, 2024
cbd9149
renamed flat type to user type
billhails Mar 1, 2024
2217b60
continued renaming flat to user
billhails Mar 1, 2024
eb014e2
renamed LamTypeConstructorInfo.vec to .needsVec
billhails Mar 2, 2024
35639b0
added yaml support for !include and factored out common primitives
billhails Mar 2, 2024
d34fdb5
renamed TcTypeDef to TcUserType
billhails Mar 2, 2024
fbde13a
formatting
billhails Mar 2, 2024
c54b2f5
gpl in primitives.yaml
billhails Mar 2, 2024
660280b
minor formatting changes
billhails Mar 2, 2024
7c7d4ef
simplify lambda conversion, fix indent
billhails Mar 3, 2024
df2cc12
generate count functions for all singly self-referential linked lists
billhails Mar 3, 2024
436fcad
slightly neater output
billhails Mar 3, 2024
32869e1
generate count functions for arrays
billhails Mar 3, 2024
4a7f6da
various tiny simplifications
billhails Mar 6, 2024
2cbc397
grabbed ENV.md from env-3
billhails Mar 6, 2024
b13464d
more thoughts on env
billhails Mar 8, 2024
5a29f46
minor clean-up
billhails Mar 9, 2024
b87b077
working listutils, changed indent
billhails Mar 10, 2024
87bad2a
useful shell stuff
billhails Mar 10, 2024
1644df9
listUtils.zipWith
billhails Mar 11, 2024
e88167b
"fixed" issue with forward references in letrec
billhails Mar 16, 2024
d0f096b
split main, simplified tc_analyze
billhails Mar 16, 2024
1d9b2a5
forgot to fix the tests
billhails Mar 16, 2024
6d3eeff
fixed parser bug
billhails Mar 16, 2024
a07bc78
various simplifications
billhails Mar 28, 2024
b7c98da
overlooked additions
billhails Mar 28, 2024
587ba17
cant_happen now reports file and line number
billhails Mar 29, 2024
376ab18
clean up and minor bug-fixes
billhails Mar 30, 2024
6ab799d
code generation of TypeName functions for debugging
billhails Mar 30, 2024
e68deda
TPMC now supports comparison
billhails Mar 31, 2024
776062b
print now appends newline
billhails Mar 31, 2024
472bf3b
tidy-up after print newline
billhails Mar 31, 2024
f2d59ed
renamed starship to spaceship (old typo propagated)
billhails Mar 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify lambda conversion, fix indent
  • Loading branch information
billhails committed Mar 3, 2024
commit 7c7d4ef74a8347fe13844c7ad49c4161d4dc9074
99 changes: 53 additions & 46 deletions src/lambda_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,48 +586,65 @@ static LamExp *makePrimApp(HashSymbol *symbol, LamList *args) {
return NULL;
}

static LamExp *convertFunCall(AstFunCall *funCall, LamContext *env) {
AstExpression *function = funCall->function;
LamList *args = convertExpressions(funCall->arguments, env);
int actualNargs = countAstExpressions(funCall->arguments);
int save = PROTECT(args);
// see if it's a type constructor we can inline FIXME - or a primitive
if (function->type == AST_EXPRESSION_TYPE_SYMBOL) {
HashSymbol *symbol = function->val.symbol;
LamExp *primApp = makePrimApp(symbol, args);
if (primApp != NULL) {
return primApp;
static LamExp *inlineConstructor(HashSymbol *symbol, LamList *args,
LamContext *env) {
LamTypeConstructorInfo *info = lookupInLamContext(env, symbol);
if (info != NULL) {
int actualNargs = countLamList(args);
if (info->needsVec) {
if (actualNargs == info->arity) {
return makeConstruct(symbol, info->index, args);
} else {
cant_happen("wrong number of arguments to constructor %s",
symbol->name);
}
} else {
LamTypeConstructorInfo *info = lookupInLamContext(env, symbol);
if (info != NULL) {
if (info->needsVec) {
if (actualNargs == info->arity) {
LamExp *inLine =
makeConstruct(symbol, info->index, args);
UNPROTECT(save);
return inLine;
} else {
cant_happen
("wrong number of arguments to constructor %s",
symbol->name);
}
} else {
cant_happen("arguments to empty constructor %s",
symbol->name);
}
if (actualNargs > 0) {
cant_happen("arguments to constant constructor %s",
symbol->name);
}
return makeConstant(symbol, info->index);
}
}
// otherwise we convert as normal
LamExp *fun = convertExpression(function, env);
(void) PROTECT(fun);
return NULL;
}

static LamExp *convertApplication(AstFunCall *funCall, LamList *args,
LamContext *env) {
int actualNargs = countAstExpressions(funCall->arguments);
LamExp *fun = convertExpression(funCall->function, env);
int save = PROTECT(fun);
LamApply *apply = newLamApply(fun, actualNargs, args);
(void) PROTECT(apply);
PROTECT(apply);
LamExp *result = newLamExp(LAMEXP_TYPE_APPLY, LAMEXP_VAL_APPLY(apply));
UNPROTECT(save);
return result;
}

static LamExp *convertFunCall(AstFunCall *funCall, LamContext *env) {
LamList *args = convertExpressions(funCall->arguments, env);
int save = PROTECT(args);
LamExp *result = NULL;
if (funCall->function->type == AST_EXPRESSION_TYPE_SYMBOL) {
HashSymbol *symbol = funCall->function->val.symbol;
result = makePrimApp(symbol, args);
if (result != NULL) {
UNPROTECT(save);
return result;
}
// see if it's a type constructor we can inline FIXME - or a primitive
result = inlineConstructor(symbol, args, env);
if (result != NULL) {
UNPROTECT(save);
return result;
}
}
// otherwise we convert as normal
result = convertApplication(funCall, args, env);
UNPROTECT(save);
return result;
}

static LamLam *convertCompositeBodies(int nargs, AstCompositeFunction *fun,
LamContext *env) {
ENTER(convertCompositeBodies);
Expand Down Expand Up @@ -671,22 +688,12 @@ static LamExp *convertCompositeFun(AstCompositeFunction *fun, LamContext *env) {
}

static LamExp *convertSymbol(HashSymbol *symbol, LamContext *env) {
LamTypeConstructorInfo *info = lookupInLamContext(env, symbol);
if (info == NULL) {
DEBUG("convertSymbol %s is not a constructor", symbol->name);
LamExp *result = inlineConstructor(symbol, NULL, env);
if (result == NULL) {
symbol = dollarSubstitute(symbol);
LamExp *res = newLamExp(LAMEXP_TYPE_VAR, LAMEXP_VAL_VAR(symbol));
return res;
}
DEBUG("convertSymbol %s is a constructor", symbol->name);
if (info->needsVec) {
if (info->arity > 0) {
cant_happen("too few arguments to constructor %s", symbol->name);
}
return makeConstruct(symbol, info->index, NULL);
} else {
return makeConstant(symbol, info->index);
result = newLamExp(LAMEXP_TYPE_VAR, LAMEXP_VAL_VAR(symbol));
}
return result;
}

static LamExp *convertExpression(AstExpression *expression, LamContext *env) {
Expand Down
2 changes: 2 additions & 0 deletions src/tc_analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,9 @@ static bool unify(TcType *a, TcType *b) {
a = prune(a);
b = prune(b);
DEBUG("UNIFY");
// *INDENT-OFF*
IFDEBUG(ppTcType(a); eprintf(" WITH "); ppTcType(b));
// *INDENT-ON*
if (a == b)
return true;
if (a->type == TCTYPE_TYPE_VAR) {
Expand Down
8 changes: 4 additions & 4 deletions src/tpmc_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ static TpmcPattern *collectAssignmentSubstitutions(TpmcPattern *pattern, TpmcSub
pattern->pattern->val.assignment->name,
pattern->path);
// we no longer need to remember this is an assignment now we have the substitution
return collectPatternSubstitutions(pattern->pattern->val.
assignment->value, substitutions);
TpmcPattern *value = pattern->pattern->val.assignment->value;
return collectPatternSubstitutions(value, substitutions);
}

static TpmcPattern *collectConstructorSubstitutions(TpmcPattern *pattern, TpmcSubstitutionTable
Expand All @@ -423,9 +423,9 @@ static TpmcPattern *collectConstructorSubstitutions(TpmcPattern *pattern, TpmcSu

static TpmcPattern *collectComparisonSubstitutions(TpmcPattern *pattern, TpmcSubstitutionTable
*substitutions) {
TpmcPattern *previous = pattern->pattern->val.comparison->previous;
pattern->pattern->val.comparison->previous =
collectPatternSubstitutions(pattern->pattern->val.
comparison->previous, substitutions);
collectPatternSubstitutions(previous, substitutions);
pattern->pattern->val.comparison->current =
collectPatternSubstitutions(pattern->pattern->val.comparison->current,
substitutions);
Expand Down
15 changes: 8 additions & 7 deletions src/tpmc_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ static void populateSubPatternMatrixRowWithComponents(TpmcMatrix *matrix,
arity, pattern->pattern->val.constructor->components->size);
}
for (int i = 0; i < arity; i++) {
setTpmcMatrixIndex(matrix, i, y,
pattern->pattern->val.constructor->
components->entries[i]);
TpmcPattern *entry =
pattern->pattern->val.constructor->components->entries[i];
setTpmcMatrixIndex(matrix, i, y, entry);
}
LEAVE(populateSubPatternMatrixRowWithComponents);
}
Expand Down Expand Up @@ -510,10 +510,11 @@ static void collectPathsBoundByPattern(TpmcPattern *pattern,
break;
case TPMCPATTERNVALUE_TYPE_BIGINTEGER:
break;
case TPMCPATTERNVALUE_TYPE_CONSTRUCTOR:
collectPathsBoundByConstructor(pattern->pattern->val.
constructor->components,
boundVariables);
case TPMCPATTERNVALUE_TYPE_CONSTRUCTOR:{
TpmcPatternArray *components =
pattern->pattern->val.constructor->components;
collectPathsBoundByConstructor(components, boundVariables);
}
break;
default:
cant_happen("unrecognised type %d in collectPathsBoundByPattern",
Expand Down