Add InferContext::add()

This commit is contained in:
Sam Vervaeck 2023-06-05 14:45:13 +02:00
parent ebe1612112
commit 1853612284
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
2 changed files with 24 additions and 9 deletions

View file

@ -17,6 +17,8 @@
namespace bolt { namespace bolt {
std::string describe(const Type* Ty); // For debugging only
class DiagnosticEngine; class DiagnosticEngine;
class Constraint; class Constraint;
@ -145,6 +147,16 @@ namespace bolt {
TypeEnv Env; TypeEnv Env;
void add(ByteString Name, Scheme* Scm) {
// auto F = static_cast<Forall*>(Scm);
// std::cerr << Name << " : forall ";
// for (auto TV: *F->TVs) {
// std::cerr << describe(TV) << " ";
// }
// std::cerr << ". " << describe(F->Type) << "\n";
Env.emplace(Name, Scm);
}
Type* ReturnType = nullptr; Type* ReturnType = nullptr;
InferContext* Parent = nullptr; InferContext* Parent = nullptr;

View file

@ -18,8 +18,6 @@
namespace bolt { namespace bolt {
std::string describe(const Type* Ty);
Constraint* Constraint::substitute(const TVSub &Sub) { Constraint* Constraint::substitute(const TVSub &Sub) {
switch (Kind) { switch (Kind) {
case ConstraintKind::Equal: case ConstraintKind::Equal:
@ -102,7 +100,7 @@ namespace bolt {
} }
void Checker::addBinding(ByteString Name, Scheme* Scm) { void Checker::addBinding(ByteString Name, Scheme* Scm) {
getContext().Env.emplace(Name, Scm); getContext().add(Name, Scm);
} }
Type* Checker::getReturnType() { Type* Checker::getReturnType() {
@ -307,7 +305,7 @@ namespace bolt {
Type* Ty = createConType(Decl->Name->getCanonicalText()); Type* Ty = createConType(Decl->Name->getCanonicalText());
// Must be added early so we can create recursive types // Must be added early so we can create recursive types
Decl->Ctx->Parent->Env.emplace(Decl->Name->getCanonicalText(), new Forall(Ty)); Decl->Ctx->Parent->add(Decl->Name->getCanonicalText(), new Forall(Ty));
for (auto Member: Decl->Members) { for (auto Member: Decl->Members) {
switch (Member->getKind()) { switch (Member->getKind()) {
@ -322,7 +320,7 @@ namespace bolt {
for (auto Element: TupleMember->Elements) { for (auto Element: TupleMember->Elements) {
ParamTypes.push_back(inferTypeExpression(Element)); ParamTypes.push_back(inferTypeExpression(Element));
} }
Decl->Ctx->Parent->Env.emplace(TupleMember->Name->getCanonicalText(), new Forall(Decl->Ctx->TVs, Decl->Ctx->Constraints, TArrow::build(ParamTypes, RetTy))); Decl->Ctx->Parent->add(TupleMember->Name->getCanonicalText(), new Forall(Decl->Ctx->TVs, Decl->Ctx->Constraints, TArrow::build(ParamTypes, RetTy)));
break; break;
} }
case NodeKind::RecordVariantDeclarationMember: case NodeKind::RecordVariantDeclarationMember:
@ -356,7 +354,7 @@ namespace bolt {
auto Ty = createConType(Name); auto Ty = createConType(Name);
// Must be added early so we can create recursive types // Must be added early so we can create recursive types
Decl->Ctx->Parent->Env.emplace(Name, new Forall(Ty)); Decl->Ctx->Parent->add(Name, new Forall(Ty));
// Corresponds to the logic of one branch of a VariantDeclarationMember // Corresponds to the logic of one branch of a VariantDeclarationMember
Type* FieldsTy = new TNil(); Type* FieldsTy = new TNil();
@ -367,7 +365,7 @@ namespace bolt {
for (auto TV: Vars) { for (auto TV: Vars) {
RetTy = new TApp(RetTy, TV); RetTy = new TApp(RetTy, TV);
} }
Decl->Ctx->Parent->Env.emplace(Name, new Forall(Decl->Ctx->TVs, Decl->Ctx->Constraints, new TArrow(FieldsTy, RetTy))); Decl->Ctx->Parent->add(Name, new Forall(Decl->Ctx->TVs, Decl->Ctx->Constraints, new TArrow(FieldsTy, RetTy)));
popContext(); popContext();
break; break;
@ -441,6 +439,8 @@ namespace bolt {
return; return;
} }
// std::cerr << "declare " << Let->getNameAsString() << std::endl;
setContext(Let->Ctx); setContext(Let->Ctx);
auto addClassVars = [&](ClassDeclaration* Class, bool IsRigid) { auto addClassVars = [&](ClassDeclaration* Class, bool IsRigid) {
@ -451,7 +451,7 @@ namespace bolt {
auto Name = TE->Name->getCanonicalText(); auto Name = TE->Name->getCanonicalText();
auto TV = IsRigid ? createRigidVar(Name) : createTypeVar(); auto TV = IsRigid ? createRigidVar(Name) : createTypeVar();
TV->Contexts.emplace(Id); TV->Contexts.emplace(Id);
Ctx->Env.emplace(Name, new Forall(TV)); Ctx->add(Name, new Forall(TV));
Out.push_back(TV); Out.push_back(TV);
} }
return Out; return Out;
@ -541,7 +541,7 @@ namespace bolt {
} }
if (!Let->isInstance()) { if (!Let->isInstance()) {
Let->Ctx->Parent->Env.emplace(Let->getNameAsString(), new Forall(Let->Ctx->TVs, Let->Ctx->Constraints, Ty)); Let->Ctx->Parent->add(Let->getNameAsString(), new Forall(Let->Ctx->TVs, Let->Ctx->Constraints, Ty));
} }
} }
@ -552,6 +552,8 @@ namespace bolt {
return; return;
} }
// std::cerr << "infer " << Decl->getNameAsString() << std::endl;
setContext(Decl->Ctx); setContext(Decl->Ctx);
std::vector<Type*> ParamTypes; std::vector<Type*> ParamTypes;
@ -720,6 +722,7 @@ namespace bolt {
TVSub Sub; TVSub Sub;
for (auto TV: *F->TVs) { for (auto TV: *F->TVs) {
auto Fresh = createTypeVar(); auto Fresh = createTypeVar();
// std::cerr << describe(TV) << " => " << describe(Fresh) << std::endl;
Fresh->Contexts = TV->Contexts; Fresh->Contexts = TV->Contexts;
Sub[TV] = Fresh; Sub[TV] = Fresh;
} }