From 1c0b88e256de6b4ea1b0c34ad525dfdad471f5ee Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 25 Dec 2023 11:54:21 +0000 Subject: Implement type substitution for constructed types --- src/main/java/org/shedlang/compiler/types/TypeSubstitution.java | 9 ++++++--- src/main/java/org/shedlang/compiler/types/Types.java | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'src/main/java') 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 Type constructedType( + TypeConstructor constructor, + List args + ) { + return new ConstructedType<>(constructor, args); + } + public static Type commonSupertype(List subtypes) { var uniqueSubtypes = subtypes.stream() .filter(subtype -> subtype != Types.BOTTOM) -- cgit v1.2.3