aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-03-02 14:07:54 +0000
committerMichael Williamson <mike@zwobble.org>2024-03-02 14:08:37 +0000
commitdfced83cd4ba6e49c395ea4106fc6f9fd7250de0 (patch)
tree15e02918865223d3bb3e736f18972068758c1570 /src/main/java
parenta7108d4744d972886845690df7f66edb3b5d48ec (diff)
Introduce TypeLevelParam
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionSignatureNode.java4
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java29
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java8
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructedStructType.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructedSumType.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/ConstructedType.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/EffectParam.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/FunctionType.java10
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeConstructor.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeLevelParam.java5
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeLevelValue.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeParam.java2
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeSubstitution.java3
-rw-r--r--src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java15
-rw-r--r--src/main/java/org/shedlang/compiler/types/Types.java8
15 files changed, 58 insertions, 38 deletions
diff --git a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionSignatureNode.java b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionSignatureNode.java
index f5900768..4e66b819 100644
--- a/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionSignatureNode.java
+++ b/src/main/java/org/shedlang/compiler/ast/typed/TypedFunctionSignatureNode.java
@@ -6,13 +6,13 @@ import org.shedlang.compiler.sources.Source;
import org.shedlang.compiler.sources.Sources;
import org.shedlang.compiler.types.Effect;
import org.shedlang.compiler.types.Type;
-import org.shedlang.compiler.types.TypeParam;
+import org.shedlang.compiler.types.TypeLevelParam;
import java.util.List;
import java.util.Optional;
public record TypedFunctionSignatureNode(
- Optional<List<TypeParam>> typeLevelParams,
+ Optional<List<TypeLevelParam>> typeLevelParams,
TypedParamsNode params,
Optional<TypedTypeLevelExpressionNode<Effect>> effect,
Optional<TypedTypeLevelExpressionNode<Type>> returnType,
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
index 61e7eb9d..13319913 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeChecker.java
@@ -73,7 +73,8 @@ public class TypeChecker {
var typeLevelArgs = receiverType.typeLevelParams()
.map(typeLevelParams ->
typeLevelParams.stream()
- .map(param -> new TypeArg(param))
+ // TODO: handle effect params
+ .map(param -> new TypeArg((TypeParam) param))
.toList()
);
@@ -1077,7 +1078,7 @@ public class TypeChecker {
typeLevelParams.orElse(List.of()).stream()
.map(typeLevelParam -> Map.entry(
typeLevelParam.name(),
- Variable.defined(Types.metaType(typeLevelParam), VariableType.PARAM)
+ Variable.defined(Types.typeLevelValueType(typeLevelParam), VariableType.PARAM)
))
.collect(PCollectors.toMap())
);
@@ -2119,18 +2120,28 @@ public class TypeChecker {
return context.toResult(typed);
}
- static TypeParam typeCheckTypeLevelParam(
+ static TypeLevelParam typeCheckTypeLevelParam(
UntypedTypeLevelParamNode untyped,
Scope scope
) {
- return new TypeParam(
- scope,
- untyped.name()
- );
+ return switch (untyped) {
+ case UntypedEffectParamNode untypedEffectParam ->
+ new EffectParam(
+ scope,
+ untyped.name()
+ );
+
+ case UntypedTypeParamNode untypedTypeParam ->
+ new TypeParam(
+ scope,
+ untyped.name()
+ );
+ };
+
}
- private static List<TypeParam> typeCheckTypeLevelParams(
+ private static List<TypeLevelParam> typeCheckTypeLevelParams(
List<UntypedTypeLevelParamNode> typeLevelParamNodes,
Scope scope
) {
@@ -2350,7 +2361,7 @@ public class TypeChecker {
// TODO: proper source
environment = environment.add(
param.name(),
- Variable.defined(Types.metaType(param), VariableType.CONSTANT),
+ Variable.defined(Types.typeLevelValueType(param), VariableType.CONSTANT),
Sources.NULL
);
}
diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
index 3a7c026b..1a0c0509 100644
--- a/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
+++ b/src/main/java/org/shedlang/compiler/typechecker/TypeCheckerSubTyping.java
@@ -104,7 +104,7 @@ public class TypeCheckerSubTyping {
? upperBound
: lowerBound;
- return Map.entry(typeArg.param(), inferredType);
+ return Map.<TypeLevelParam, TypeLevelValue>entry(typeArg.param(), inferredType);
})
.collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
@@ -196,13 +196,15 @@ public class TypeCheckerSubTyping {
var subtypeArg = subtypeConstructed.args().get(i);
var supertypeArg = supertypeConstructed.args().get(i);
- var result1 = tryAddSubtypeConstraint(subtypeArg, supertypeArg, constraints);
+ // TODO: support effect params
+ var result1 = tryAddSubtypeConstraint((Type) subtypeArg, (Type) supertypeArg, constraints);
if (result1.isEmpty()) {
return Optional.empty();
}
constraints = result1.get();
- var result2 = tryAddSubtypeConstraint(supertypeArg, subtypeArg, constraints);
+ // TODO: support effect params
+ var result2 = tryAddSubtypeConstraint((Type) supertypeArg, (Type) subtypeArg, constraints);
if (result2.isEmpty()) {
return Optional.empty();
}
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedStructType.java b/src/main/java/org/shedlang/compiler/types/ConstructedStructType.java
index d0906f94..bf7cf4d5 100644
--- a/src/main/java/org/shedlang/compiler/types/ConstructedStructType.java
+++ b/src/main/java/org/shedlang/compiler/types/ConstructedStructType.java
@@ -4,7 +4,7 @@ import java.util.List;
public record ConstructedStructType(
TypeConstructor<SimpleStructType> constructor,
- List<? extends Type> args
+ List<? extends TypeLevelValue> args
) implements ConstructedType<SimpleStructType>, StructType {
@Override
public SimpleStructType rawType() {
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedSumType.java b/src/main/java/org/shedlang/compiler/types/ConstructedSumType.java
index b001e43b..c32389ff 100644
--- a/src/main/java/org/shedlang/compiler/types/ConstructedSumType.java
+++ b/src/main/java/org/shedlang/compiler/types/ConstructedSumType.java
@@ -4,6 +4,6 @@ import java.util.List;
public record ConstructedSumType(
TypeConstructor<SimpleSumType> constructor,
- List<? extends Type> args
+ List<? extends TypeLevelValue> args
) implements ConstructedType<SimpleSumType>, SumType {
}
diff --git a/src/main/java/org/shedlang/compiler/types/ConstructedType.java b/src/main/java/org/shedlang/compiler/types/ConstructedType.java
index 4a3d941e..e49b3520 100644
--- a/src/main/java/org/shedlang/compiler/types/ConstructedType.java
+++ b/src/main/java/org/shedlang/compiler/types/ConstructedType.java
@@ -5,7 +5,7 @@ import java.util.stream.Collectors;
public sealed interface ConstructedType<T extends ConstructibleType> extends Type permits ConstructedStructType, ConstructedSumType {
TypeConstructor<T> constructor();
- List<? extends Type> args();
+ List<? extends TypeLevelValue> args();
@Override
default String describe() {
diff --git a/src/main/java/org/shedlang/compiler/types/EffectParam.java b/src/main/java/org/shedlang/compiler/types/EffectParam.java
index e8ae6121..2331ed01 100644
--- a/src/main/java/org/shedlang/compiler/types/EffectParam.java
+++ b/src/main/java/org/shedlang/compiler/types/EffectParam.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.types;
-public record EffectParam(Scope scope, String name) implements Effect {
+public record EffectParam(Scope scope, String name) implements Effect, TypeLevelParam {
public static EffectParam forFunction(
NamespaceName namespaceName,
String functionName,
diff --git a/src/main/java/org/shedlang/compiler/types/FunctionType.java b/src/main/java/org/shedlang/compiler/types/FunctionType.java
index 621a2364..fbc17df7 100644
--- a/src/main/java/org/shedlang/compiler/types/FunctionType.java
+++ b/src/main/java/org/shedlang/compiler/types/FunctionType.java
@@ -9,7 +9,7 @@ import java.util.Optional;
import static org.shedlang.compiler.util.Iterables.forEachInterspersed;
public record FunctionType(
- Optional<List<TypeParam>> typeLevelParams,
+ Optional<List<TypeLevelParam>> typeLevelParams,
List<Type> positionalParams,
List<NamedParam> namedParams,
Effect effect,
@@ -54,11 +54,11 @@ public record FunctionType(
return description.toString();
}
- public FunctionType withTypeLevelParams(List<TypeParam> typeLevelParams) {
+ public FunctionType withTypeLevelParams(List<TypeLevelParam> typeLevelParams) {
return withTypeLevelParams(Optional.of(typeLevelParams));
}
- private FunctionType withTypeLevelParams(Optional<List<TypeParam>> typeLevelParams) {
+ private FunctionType withTypeLevelParams(Optional<List<TypeLevelParam>> typeLevelParams) {
return new FunctionType(
typeLevelParams,
this.positionalParams,
@@ -101,7 +101,7 @@ public record FunctionType(
}
public record Builder(
- Optional<List<TypeParam>> typeLevelParams,
+ Optional<List<TypeLevelParam>> typeLevelParams,
PVector<Type> positionalParams,
PVector<NamedParam> namedParams,
Effect effect,
@@ -117,7 +117,7 @@ public record FunctionType(
);
}
- public Builder withTypeLevelParams(List<TypeParam> typeLevelParams) {
+ public Builder withTypeLevelParams(List<TypeLevelParam> typeLevelParams) {
return new Builder(
Optional.of(typeLevelParams),
this.positionalParams,
diff --git a/src/main/java/org/shedlang/compiler/types/TypeConstructor.java b/src/main/java/org/shedlang/compiler/types/TypeConstructor.java
index ba4b44b9..9fb8296d 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeConstructor.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeConstructor.java
@@ -3,7 +3,7 @@ package org.shedlang.compiler.types;
import java.util.List;
public record TypeConstructor<T extends ConstructibleType>(
- List<TypeParam> params,
+ List<TypeLevelParam> params,
T genericType
) implements TypeLevelValue {
@Override
diff --git a/src/main/java/org/shedlang/compiler/types/TypeLevelParam.java b/src/main/java/org/shedlang/compiler/types/TypeLevelParam.java
new file mode 100644
index 00000000..615c5880
--- /dev/null
+++ b/src/main/java/org/shedlang/compiler/types/TypeLevelParam.java
@@ -0,0 +1,5 @@
+package org.shedlang.compiler.types;
+
+public sealed interface TypeLevelParam extends TypeLevelValue permits EffectParam, TypeParam {
+ String name();
+}
diff --git a/src/main/java/org/shedlang/compiler/types/TypeLevelValue.java b/src/main/java/org/shedlang/compiler/types/TypeLevelValue.java
index a002c88a..f250c05c 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeLevelValue.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeLevelValue.java
@@ -1,5 +1,5 @@
package org.shedlang.compiler.types;
-public sealed interface TypeLevelValue permits Effect, Type, TypeConstructor {
+public sealed interface TypeLevelValue permits Effect, Type, TypeConstructor, TypeLevelParam {
String describe();
}
diff --git a/src/main/java/org/shedlang/compiler/types/TypeParam.java b/src/main/java/org/shedlang/compiler/types/TypeParam.java
index c7d0c88d..45262cc4 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeParam.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeParam.java
@@ -1,6 +1,6 @@
package org.shedlang.compiler.types;
-public record TypeParam(Scope scope, String name) implements Type {
+public record TypeParam(Scope scope, String name) implements Type, TypeLevelParam {
public static TypeParam forFunction(
NamespaceName namespaceName,
String functionName,
diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
index 4b29895c..de055e1d 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitution.java
@@ -96,7 +96,8 @@ public class TypeSubstitution {
return createConstructedType.apply(
constructedType.constructor(),
constructedType.args().stream()
- .map(arg -> substituteType(arg, map))
+ // TODO: handle effects
+ .map(arg -> substituteType((Type) arg, map))
.toList()
);
}
diff --git a/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java b/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java
index 6e6bd03a..02c602af 100644
--- a/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java
+++ b/src/main/java/org/shedlang/compiler/types/TypeSubstitutionMap.java
@@ -7,15 +7,15 @@ import java.util.Objects;
public class TypeSubstitutionMap {
@SafeVarargs
- public static TypeSubstitutionMap of(Map.Entry<TypeParam, Type>... map) {
+ public static TypeSubstitutionMap of(Map.Entry<TypeLevelParam, TypeLevelValue>... map) {
return new TypeSubstitutionMap(Map.ofEntries(map));
}
public static TypeSubstitutionMap from(
- List<TypeParam> params,
- List<? extends Type> args
+ List<TypeLevelParam> params,
+ List<? extends TypeLevelValue> args
) {
- var map = new HashMap<TypeParam, Type>();
+ var map = new HashMap<TypeLevelParam, TypeLevelValue>();
for (var argIndex = 0; argIndex < args.size(); argIndex++) {
var param = params.get(argIndex);
@@ -26,14 +26,15 @@ public class TypeSubstitutionMap {
return new TypeSubstitutionMap(map);
}
- private final Map<TypeParam, Type> map;
+ private final Map<TypeLevelParam, TypeLevelValue> map;
- public TypeSubstitutionMap(Map<TypeParam, Type> map) {
+ public TypeSubstitutionMap(Map<TypeLevelParam, TypeLevelValue> map) {
this.map = map;
}
public Type substitute(TypeParam typeParam) {
- return this.map.getOrDefault(typeParam, typeParam);
+ // TODO: Validate map? Check here?
+ return (Type) this.map.getOrDefault(typeParam, typeParam);
}
@Override
diff --git a/src/main/java/org/shedlang/compiler/types/Types.java b/src/main/java/org/shedlang/compiler/types/Types.java
index 8c26d890..a8c3aef5 100644
--- a/src/main/java/org/shedlang/compiler/types/Types.java
+++ b/src/main/java/org/shedlang/compiler/types/Types.java
@@ -50,7 +50,7 @@ public class Types {
}
public static <T extends ConstructibleType> TypeConstructor<T> typeConstructor(
- List<TypeParam> params,
+ List<TypeLevelParam> params,
T genericType
) {
return new TypeConstructor<>(params, genericType);
@@ -59,7 +59,7 @@ public class Types {
@SuppressWarnings("unchecked")
public static ConstructedType<?> constructedType(
TypeConstructor<?> constructor,
- List<? extends Type> args
+ List<? extends TypeLevelValue> args
) {
return switch (constructor.genericType()) {
case SimpleStructType structType ->
@@ -71,14 +71,14 @@ public class Types {
public static ConstructedStructType constructedStructType(
TypeConstructor<SimpleStructType> constructor,
- List<? extends Type> args
+ List<? extends TypeLevelValue> args
) {
return new ConstructedStructType(constructor, args);
}
public static ConstructedSumType constructedSumType(
TypeConstructor<SimpleSumType> constructor,
- List<? extends Type> args
+ List<? extends TypeLevelValue> args
) {
return new ConstructedSumType(constructor, args);
}