aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-29 11:22:55 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-29 11:22:55 +0000
commitbbc57bddb4dea75e3774673dbb173662e930c3dc (patch)
treec21931d110e5719249888efbc1fa721b030f777e /src/main/java
parent1fef4fd33b335d78bc91a174b970be05bf8a5b39 (diff)
Support subtyping for constructed sum types
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java25
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java10
2 files changed, 35 insertions, 0 deletions
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
index dcaa9226..ffd2ac01 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerContext.java
@@ -481,6 +481,31 @@ public class TypeCheckerContext {
.orElseThrow(() -> new MissingSumTypeInfoError(type, source));
}
+ public SumTypeInfo sumTypeInfo(ConstructedType<SumType> type, Source source) {
+ var typeInfo = sumTypeInfo(type.genericType(), source);
+
+ var typeSubstitutionMap = type.typeSubstitutionMap();
+
+ var variants = typeInfo.variants().stream()
+ .map(variant -> switch (variant) {
+ case SumVariant.ConstructedStruct constructedStruct ->
+ // TODO: overload to avoid cast?
+ SumVariant.constructedStruct((ConstructedType<StructType>) TypeSubstitution.substitute(
+ constructedStruct.type(),
+ typeSubstitutionMap
+ ));
+
+ case SumVariant.Struct struct ->
+ SumVariant.struct((StructType) TypeSubstitution.substitute(
+ struct.type(),
+ typeSubstitutionMap
+ ));
+ })
+ .collect(PCollectors.toOrderedSet());
+
+ return new SumTypeInfo(variants);
+ }
+
public TypeCheckerContext addUserDefinedEffectInfo(
UserDefinedEffect effect,
UserDefinedEffectInfo effectInfo
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
index 2c69ac14..4aab5370 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
@@ -29,6 +29,16 @@ public class TypeCheckerSubTyping {
}
if (
+ supertype instanceof ConstructedType<?> supertypeConstructedType &&
+ supertypeConstructedType.genericType() instanceof SumType
+ ) {
+ // TODO: merge with logic for plain sum types
+ var superTypeInfo = context.sumTypeInfo((ConstructedType<SumType>) supertypeConstructedType, Sources.NULL);
+ return superTypeInfo.variants().stream()
+ .anyMatch(supertypeVariant -> isSubType(subtype, supertypeVariant.toType(), context));
+ }
+
+ if (
supertype instanceof FunctionType supertypeFunction &&
subtype instanceof FunctionType subtypeFunction
) {