|
| 1 | +package com.garciat.typeclasses; |
| 2 | + |
| 3 | +import static com.garciat.typeclasses.api.TypeClass.Witness.Overlap.OVERLAPPABLE; |
| 4 | +import static com.garciat.typeclasses.api.TypeClass.Witness.Overlap.OVERLAPPING; |
| 5 | +import static java.lang.reflect.AccessFlag.PUBLIC; |
| 6 | +import static java.lang.reflect.AccessFlag.STATIC; |
| 7 | + |
| 8 | +import com.garciat.typeclasses.api.Ctx; |
| 9 | +import com.garciat.typeclasses.api.Ty; |
| 10 | +import com.garciat.typeclasses.api.TypeClass; |
| 11 | +import com.garciat.typeclasses.impl.FuncType; |
| 12 | +import com.garciat.typeclasses.impl.ParsedType; |
| 13 | +import com.garciat.typeclasses.impl.Unification; |
| 14 | +import com.garciat.typeclasses.impl.utils.Lists; |
| 15 | +import com.garciat.typeclasses.impl.utils.ZeroOneMore; |
| 16 | +import com.garciat.typeclasses.types.Either; |
| 17 | +import com.garciat.typeclasses.types.Maybe; |
| 18 | +import java.lang.reflect.Method; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +public class TypeClasses { |
| 25 | + public static <T> T witness(Ty<T> ty, Ctx<?>... context) { |
| 26 | + return switch (summon(ParsedType.parse(ty.type()), parseContext(context))) { |
| 27 | + case Either.Left<SummonError, Object>(SummonError error) -> |
| 28 | + throw new WitnessResolutionException(error); |
| 29 | + case Either.Right<SummonError, Object>(Object instance) -> { |
| 30 | + @SuppressWarnings("unchecked") |
| 31 | + T typedInstance = (T) instance; |
| 32 | + yield typedInstance; |
| 33 | + } |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + private static List<ContextInstance> parseContext(Ctx<?>[] context) { |
| 38 | + return Arrays.stream(context) |
| 39 | + .map(ctx -> new ContextInstance(ctx.instance(), ParsedType.parse(ctx.type()))) |
| 40 | + .toList(); |
| 41 | + } |
| 42 | + |
| 43 | + public static class WitnessResolutionException extends RuntimeException { |
| 44 | + private WitnessResolutionException(SummonError error) { |
| 45 | + super(error.format()); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private sealed interface SummonError { |
| 50 | + record NotFound(ParsedType target) implements SummonError {} |
| 51 | + |
| 52 | + record Ambiguous(ParsedType target, List<Candidate> candidates) implements SummonError {} |
| 53 | + |
| 54 | + record Nested(ParsedType target, SummonError cause) implements SummonError {} |
| 55 | + |
| 56 | + default String format() { |
| 57 | + return switch (this) { |
| 58 | + case NotFound(ParsedType target) -> "No witness found for type: " + target.format(); |
| 59 | + case Ambiguous(ParsedType target, List<Candidate> candidates) -> |
| 60 | + "Ambiguous witnesses found for type: " |
| 61 | + + target.format() |
| 62 | + + "\nCandidates:\n" |
| 63 | + + candidates.stream() |
| 64 | + .map(c -> c.rule().toString()) |
| 65 | + .collect(Collectors.joining("\n")) |
| 66 | + .indent(2); |
| 67 | + case Nested(ParsedType target, SummonError cause) -> |
| 68 | + "While summoning witness for type: " |
| 69 | + + target.format() |
| 70 | + + "\nCaused by: " |
| 71 | + + cause.format().indent(2); |
| 72 | + }; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static Either<SummonError, Object> summon( |
| 77 | + ParsedType target, List<ContextInstance> context) { |
| 78 | + return switch (ZeroOneMore.of(findCandidates(target, context))) { |
| 79 | + case ZeroOneMore.One<Candidate>(Candidate(var rule, var requirements)) -> |
| 80 | + summonAll(requirements, context) |
| 81 | + .map(rule::instantiate) |
| 82 | + .mapLeft(error -> new SummonError.Nested(target, error)); |
| 83 | + case ZeroOneMore.Zero<Candidate>() -> Either.left(new SummonError.NotFound(target)); |
| 84 | + case ZeroOneMore.More<Candidate>(var candidates) -> |
| 85 | + Either.left(new SummonError.Ambiguous(target, candidates)); |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + private static Either<SummonError, List<Object>> summonAll( |
| 90 | + List<ParsedType> targets, List<ContextInstance> context) { |
| 91 | + return Either.traverse(targets, target -> summon(target, context)); |
| 92 | + } |
| 93 | + |
| 94 | + private record Candidate(WitnessRule rule, List<ParsedType> requirements) {} |
| 95 | + |
| 96 | + private static List<Candidate> findCandidates(ParsedType target, List<ContextInstance> context) { |
| 97 | + return Stream.<WitnessRule>concat( |
| 98 | + context.stream(), reduceOverlapping(findRules(target)).stream()) |
| 99 | + .flatMap( |
| 100 | + rule -> |
| 101 | + rule |
| 102 | + .tryMatch(target) |
| 103 | + .map(requirements -> new Candidate(rule, requirements)) |
| 104 | + .stream()) |
| 105 | + .toList(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @implSpec <a href= |
| 110 | + * "https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/instances.html#overlapping-instances">6.8.8.5. |
| 111 | + * Overlapping instances</a> |
| 112 | + */ |
| 113 | + private static List<InstanceConstructor> reduceOverlapping(List<InstanceConstructor> candidates) { |
| 114 | + return candidates.stream() |
| 115 | + .filter( |
| 116 | + iX -> |
| 117 | + candidates.stream().filter(iY -> iX != iY).noneMatch(iY -> isOverlappedBy(iX, iY))) |
| 118 | + .toList(); |
| 119 | + } |
| 120 | + |
| 121 | + private static boolean isOverlappedBy(InstanceConstructor iX, InstanceConstructor iY) { |
| 122 | + return (iX.overlap() == OVERLAPPABLE || iY.overlap() == OVERLAPPING) |
| 123 | + && isSubstitutionInstance(iX, iY) |
| 124 | + && !isSubstitutionInstance(iY, iX); |
| 125 | + } |
| 126 | + |
| 127 | + private static boolean isSubstitutionInstance( |
| 128 | + InstanceConstructor base, InstanceConstructor reference) { |
| 129 | + return Unification.unify(base.func().returnType(), reference.func().returnType()) |
| 130 | + .fold(() -> false, map -> !map.isEmpty()); |
| 131 | + } |
| 132 | + |
| 133 | + private static List<InstanceConstructor> findRules(ParsedType target) { |
| 134 | + return switch (target) { |
| 135 | + case ParsedType.App(var fun, var arg) -> Lists.concat(findRules(fun), findRules(arg)); |
| 136 | + case ParsedType.Const(var java) -> rulesOf(java); |
| 137 | + case ParsedType.Var(var java) -> List.of(); |
| 138 | + case ParsedType.ArrayOf(var elem) -> List.of(); |
| 139 | + case ParsedType.Primitive(var java) -> List.of(); |
| 140 | + }; |
| 141 | + } |
| 142 | + |
| 143 | + private static List<InstanceConstructor> rulesOf(Class<?> cls) { |
| 144 | + return Arrays.stream(cls.getDeclaredMethods()) |
| 145 | + .filter(TypeClasses::isWitnessMethod) |
| 146 | + .map(FuncType::parse) |
| 147 | + .map(InstanceConstructor::new) |
| 148 | + .toList(); |
| 149 | + } |
| 150 | + |
| 151 | + private static boolean isWitnessMethod(Method m) { |
| 152 | + return m.accessFlags().contains(PUBLIC) |
| 153 | + && m.accessFlags().contains(STATIC) |
| 154 | + && m.isAnnotationPresent(TypeClass.Witness.class); |
| 155 | + } |
| 156 | + |
| 157 | + private sealed interface WitnessRule { |
| 158 | + Maybe<List<ParsedType>> tryMatch(ParsedType target); |
| 159 | + |
| 160 | + Object instantiate(List<Object> dependencies); |
| 161 | + } |
| 162 | + |
| 163 | + private record ContextInstance(Object instance, ParsedType type) implements WitnessRule { |
| 164 | + @Override |
| 165 | + public Maybe<List<ParsedType>> tryMatch(ParsedType target) { |
| 166 | + return target.equals(type) ? Maybe.just(List.of()) : Maybe.nothing(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Object instantiate(List<Object> dependencies) { |
| 171 | + return instance; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public String toString() { |
| 176 | + return "context instance: " + type.format(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private record InstanceConstructor(FuncType func) implements WitnessRule { |
| 181 | + public TypeClass.Witness.Overlap overlap() { |
| 182 | + return func.java().getAnnotation(TypeClass.Witness.class).overlap(); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public Maybe<List<ParsedType>> tryMatch(ParsedType target) { |
| 187 | + return Unification.unify(func.returnType(), target) |
| 188 | + .map(map -> Unification.substituteAll(map, func.paramTypes())); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public Object instantiate(List<Object> dependencies) { |
| 193 | + try { |
| 194 | + return func.java().invoke(null, dependencies.toArray()); |
| 195 | + } catch (Exception e) { |
| 196 | + throw new RuntimeException(e); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public String toString() { |
| 202 | + return func.format(); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments