aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-25 11:54:21 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-25 12:07:37 +0000
commit1c0b88e256de6b4ea1b0c34ad525dfdad471f5ee (patch)
treea3678272ef3be9d1e09c9e286dcb1957e7e2edd1 /src
parent002aab32f5f68d5399a679100df40ffd8627c217 (diff)
Implement type substitution for constructed types
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeSubstitution.java9
-rw-r--r--src/main/java/org/shedlang/compiler/types/Types.java7
-rw-r--r--src/test/java/org/shedlang/compiler/types/TypeSubstitutionTests.java28
3 files changed, 41 insertions, 3 deletions
diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
index 27111fd5..a6719d17 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
@@ -9,9 +9,12 @@ public class TypeSubstitution {
case BottomType bottomType -> type;
- case ConstructedType constructedType -> {
- throw new UnsupportedOperationException("TODO");
- }
+ case ConstructedType<?> constructedType -> new ConstructedType<>(
+ constructedType.constructor(),
+ constructedType.args().stream()
+ .map(arg -> substitute(arg, map))
+ .toList()
+ );
case FunctionType functionType -> new FunctionType(
functionType.positionalParams().stream()
diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java
index c3c25853..0e502249 100644
--- a/src/main/java/org/shedlang/compiler/types/Types.java
+++ b/src/main/java/org/shedlang/compiler/types/Types.java
@@ -49,6 +49,13 @@ public class Types {
return new TypeConstructor<>(params, genericType);
}
+ public static <T extends StructuredType> Type constructedType(
+ TypeConstructor<T> constructor,
+ List<Type> args
+ ) {
+ return new ConstructedType<>(constructor, args);
+ }
+
public static Type commonSupertype(List<Type> subtypes) {
var uniqueSubtypes = subtypes.stream()
.filter(subtype -> subtype != Types.BOTTOM)
diff --git a/src/test/java/org/shedlang/compiler/types/TypeSubstitutionTests.java b/src/test/java/org/shedlang/compiler/types/TypeSubstitutionTests.java
index cd676d71..e9b04a95 100644
--- a/src/test/java/org/shedlang/compiler/types/TypeSubstitutionTests.java
+++ b/src/test/java/org/shedlang/compiler/types/TypeSubstitutionTests.java
@@ -36,6 +36,34 @@ public class TypeSubstitutionTests {
}
@Test
+ public void substitutingConstructedTypeSubstitutesArgs() {
+ var typeSubstitutionMap = TypeSubstitutionMap.of(
+ Map.entry(TypeParam.forStructType(STRUCT_TYPE, "T"), Types.INT)
+ );
+
+ var result = TypeSubstitution.substitute(
+ Types.constructedType(
+ Types.typeConstructor(
+ List.of(TypeParam.forStructType(STRUCT_TYPE_2, "T")),
+ STRUCT_TYPE_2
+ ),
+ List.of(TypeParam.forStructType(STRUCT_TYPE, "T"))
+ ),
+ typeSubstitutionMap
+ );
+
+ assertThat(result, equalTo(
+ Types.constructedType(
+ Types.typeConstructor(
+ List.of(TypeParam.forStructType(STRUCT_TYPE_2, "T")),
+ STRUCT_TYPE_2
+ ),
+ List.of(Types.INT)
+ )
+ ));
+ }
+
+ @Test
public void substitutingFunctionSubstitutesPositionalArgs() {
var typeSubstitutionMap = TypeSubstitutionMap.of(
Map.entry(TypeParam.forStructType(STRUCT_TYPE, "T"), Types.INT)