From b979531c13cfb67ffe74e67b8dae782adc3925f5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 31 Dec 2023 13:11:51 +0000 Subject: Store TypeInfos instead of context for type constraints --- .../shedlang/compiler/typechecker/TypeChecker.java | 8 +++--- .../compiler/typechecker/TypeCheckerSubTyping.java | 31 ++++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java index b2cd1a7a..8d8111fb 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java @@ -32,7 +32,7 @@ public class TypeChecker { } private static void checkIsSubType(Type actualType, Type expectedType, Source source, TypeCheckerContext context) { - if (!isSubType(actualType, expectedType, context)) { + if (!isSubType(actualType, expectedType, context.toTypeInfos())) { throw new UnexpectedTypeError( TypeSets.type(expectedType), actualType, @@ -72,7 +72,7 @@ public class TypeChecker { ) { var constraints = TypeCheckerSubTyping.constraints( receiverType.typeLevelParams().orElse(List.of()), - context + context.toTypeInfos() ); if (args.positional().size() != receiverType.positionalParams().size()) { @@ -385,7 +385,7 @@ public class TypeChecker { throw new FunctionHasNoEffectsError(untyped.source()); } - if (!isSubEffect(receiverType.effect(), context.effect(), context)) { + if (!isSubEffect(receiverType.effect(), context.effect(), context.toTypeInfos())) { throw new UnexpectedEffectError( context.effect(), receiverType.effect(), @@ -2018,7 +2018,7 @@ public class TypeChecker { var effect = untyped.effect() .map(untypedEffectNode -> { var explicitEffect = typeCheckEffect(untypedEffectNode, initialContext).value(); - if (!isSubEffect(explicitEffect, expectedEffect, initialContext)) { + if (!isSubEffect(explicitEffect, expectedEffect, initialContext.toTypeInfos())) { throw new UnexpectedEffectError( expectedEffect, explicitEffect, diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java index d4659a4c..898cacf9 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java @@ -3,7 +3,6 @@ package org.shedlang.compiler.typechecker; import org.pcollections.PMap; import org.pcollections.PVector; import org.shedlang.compiler.sources.Source; -import org.shedlang.compiler.sources.Sources; import org.shedlang.compiler.typechecker.errors.UnexpectedTypeError; import org.shedlang.compiler.typechecker.typesets.TypeSets; import org.shedlang.compiler.types.*; @@ -17,8 +16,8 @@ import java.util.stream.Collectors; public class TypeCheckerSubTyping { private TypeCheckerSubTyping() {} - static boolean isSubType(Type subtype, Type supertype, TypeCheckerContext context) { - var constraints = new Constraints(List.of(), P.map(), context); + static boolean isSubType(Type subtype, Type supertype, TypeInfos typeInfos) { + var constraints = new Constraints(List.of(), P.map(), typeInfos); return tryAddSubtypeConstraint(subtype, supertype, constraints) .isPresent(); } @@ -34,16 +33,15 @@ public class TypeCheckerSubTyping { } } - static Constraints constraints(List typeLevelParams, TypeCheckerContext context) { - return new Constraints(typeLevelParams, P.map(), context); + static Constraints constraints(List typeLevelParams, TypeInfos typeInfos) { + return new Constraints(typeLevelParams, P.map(), typeInfos); } record Constraints( // TODO: typeLevelParams are redundant if we make typeBounds always have those params as keys? List typeLevelParams, PMap typeBounds, - // TODO: replace with type info - TypeCheckerContext context + TypeInfos typeInfos ) { public Constraints addLowerBound(TypeParam typeParam, Type lowerBound) { var bounds = this.typeBounds.getOrDefault(typeParam, TypeBounds.EMPTY) @@ -51,7 +49,7 @@ public class TypeCheckerSubTyping { return new Constraints( this.typeLevelParams, this.typeBounds.plus(typeParam, bounds), - this.context + this.typeInfos ); } @@ -123,7 +121,7 @@ public class TypeCheckerSubTyping { } if (supertype instanceof SumType supertypeSum) { - var superTypeInfo = constraints.context.sumTypeInfo(supertypeSum, Sources.NULL); + var superTypeInfo = constraints.typeInfos.sumTypeInfo(supertypeSum).orElseThrow(); return tryAddVariantSubtypeConstraint(subtype, superTypeInfo, constraints); } @@ -131,10 +129,9 @@ public class TypeCheckerSubTyping { supertype instanceof ConstructedType supertypeConstructedType && supertypeConstructedType.genericType() instanceof SumType ) { - var superTypeInfo = constraints.context.sumTypeInfo( - (ConstructedType) supertypeConstructedType, - Sources.NULL - ); + var superTypeInfo = constraints.typeInfos.sumTypeInfo( + (ConstructedType) supertypeConstructedType + ).orElseThrow(); return tryAddVariantSubtypeConstraint(subtype, superTypeInfo, constraints); } @@ -184,7 +181,7 @@ public class TypeCheckerSubTyping { } } - if (!isSubEffect(subtypeFunction.effect(), supertypeFunction.effect(), constraints.context)) { + if (!isSubEffect(subtypeFunction.effect(), supertypeFunction.effect(), constraints.typeInfos)) { return Optional.empty(); } @@ -222,7 +219,7 @@ public class TypeCheckerSubTyping { return Optional.empty(); } - static boolean isSubEffect(Effect subEffect, Effect superEffect, TypeCheckerContext context) { + static boolean isSubEffect(Effect subEffect, Effect superEffect, TypeInfos typeInfos) { if (subEffect == Effects.NONE) { return true; } @@ -232,8 +229,8 @@ public class TypeCheckerSubTyping { } if (superEffect instanceof EffectUnion superEffectUnion) { - return isSubEffect(subEffect, superEffectUnion.left(), context) || - isSubEffect(subEffect, superEffectUnion.right(), context); + return isSubEffect(subEffect, superEffectUnion.left(), typeInfos) || + isSubEffect(subEffect, superEffectUnion.right(), typeInfos); } return false; -- cgit v1.2.3