Skip to content

Commit 7b2207a

Browse files
CopilotGarciat
andcommitted
Remove documentation and tests, focus on package structure only
Co-authored-by: Garciat <118277+Garciat@users.noreply.github.com>
1 parent f3e94ed commit 7b2207a

16 files changed

Lines changed: 7 additions & 469 deletions

File tree

README.md

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,3 @@
1-
# Java Type Classes
1+
Conventions:
22

3-
A type class system for Java, inspired by Haskell's type classes.
4-
5-
## Overview
6-
7-
This library provides a way to define and use type classes in Java, enabling ad-hoc polymorphism through automatic instance resolution. It includes a rich set of predefined type classes and data types with higher-kinded type support.
8-
9-
## Usage
10-
11-
### Basic Example
12-
13-
```java
14-
import static com.garciat.typeclasses.TypeClasses.witness;
15-
16-
// Automatically resolve a Show instance for List<Integer>
17-
Show<List<Integer>> showListInt = witness(new Ty<>() {});
18-
String result = showListInt.show(List.of(1, 2, 3));
19-
// result: "[1, 2, 3]"
20-
```
21-
22-
### Core Type Classes
23-
24-
The library provides several built-in type classes:
25-
26-
- **Show** - Convert values to strings
27-
- **Eq** - Equality testing
28-
- **Ord** - Ordering comparisons
29-
- **Monoid** - Associative binary operations with identity
30-
- **Functor** - Mappable type constructors
31-
- **Applicative** - Application of functions in a context
32-
- **Monad** - Sequential composition of computations
33-
- **Foldable** - Structures that can be folded
34-
- **Traversable** - Structures that can be traversed
35-
36-
### Data Types
37-
38-
The library includes functional data types with type class instances:
39-
40-
- **Maybe** - Optional values (`Just` or `Nothing`)
41-
- **Either** - Sum types (`Left` or `Right`)
42-
- **JavaList** - List with type class instances
43-
- **FwdList** - Functional forward list
44-
- **Parser** - Parser combinators
45-
- **State** - State monad
46-
47-
### Higher-Kinded Types
48-
49-
The library supports higher-kinded types through a defunctionalization encoding:
50-
51-
```java
52-
// Work with Functors abstractly
53-
Functor<Maybe.Tag> functorMaybe = witness(new Ty<>() {});
54-
TApp<Maybe.Tag, Integer> maybeInt = Maybe.just(42);
55-
TApp<Maybe.Tag, String> maybeStr = functorMaybe.map(Object::toString, maybeInt);
56-
```
57-
58-
## Examples
59-
60-
See the `Examples` class for comprehensive usage examples:
61-
62-
```bash
63-
mvn compile exec:java -Dexec.mainClass="com.garciat.typeclasses.Examples"
64-
```
65-
66-
## API Structure
67-
68-
### Public API
69-
70-
The main entry point is `TypeClasses.witness()` which resolves type class instances. All type classes (marked with `@TypeClass`) and data types are part of the public API.
71-
72-
Key public components:
73-
- `TypeClasses.witness()` - Resolve type class instances
74-
- `Ty<T>` - Type token for capturing types
75-
- `Ctx<T>` - Context token for explicit instances
76-
- `@TypeClass` - Annotation for defining type classes
77-
- `Kind`, `TApp`, `TPar`, `TagBase` - Higher-kinded type infrastructure
78-
79-
### Internal Implementation
80-
81-
Internal implementation details (parsing, unification, witness resolution algorithms) are package-private and should not be relied upon by library users.
82-
83-
## Building
84-
85-
```bash
86-
mvn clean compile test
87-
```
88-
89-
## Conventions
90-
91-
- Use google-java-format for code formatting
92-
- Java 21 is required
93-
94-
## License
95-
96-
See repository for license information.
3+
- Use google-java-format

src/main/java/com/garciat/typeclasses/Main.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@
3636
import java.util.stream.Collectors;
3737
import java.util.stream.Stream;
3838

39-
/**
40-
* Core type class infrastructure for Java.
41-
*
42-
* <p>This class has been retained for backward compatibility but is no longer the main entry point.
43-
* The library should be accessed through the public type classes and data types.
44-
*
45-
* @deprecated Use {@link Examples} for demonstration code, or the individual type classes directly.
46-
*/
47-
@Deprecated
4839
public final class Main {
4940
private Main() {}
5041
}

src/main/java/com/garciat/typeclasses/TypeClasses.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,7 @@
1616
import java.util.stream.Collectors;
1717
import java.util.stream.Stream;
1818

19-
/**
20-
* Central facility for type class witness resolution.
21-
*
22-
* <p><b>PUBLIC API</b>: The {@link #witness} method is the main entry point for the library.
23-
*/
2419
public class TypeClasses {
25-
/**
26-
* Resolves and returns a witness (instance) of a type class for the given type.
27-
*
28-
* <p>This is the main entry point for using the type class system. It automatically finds and
29-
* instantiates the appropriate type class instance based on the provided type token.
30-
*
31-
* <p>Example:
32-
*
33-
* <pre>{@code
34-
* Show<List<Integer>> showListInt = TypeClasses.witness(new Ty<>() {});
35-
* String result = showListInt.show(List.of(1, 2, 3));
36-
* }</pre>
37-
*
38-
* @param <T> the type class instance type
39-
* @param ty the type token capturing the desired type class instance
40-
* @param context optional context instances to use in resolution
41-
* @return the resolved type class instance
42-
* @throws WitnessResolutionException if no suitable instance can be found
43-
*/
4420
public static <T> T witness(Ty<T> ty, Ctx<?>... context) {
4521
return switch (summon(ParsedType.parse(ty.type()), parseContext(context))) {
4622
case Either.Left<SummonError, Object>(SummonError error) ->
@@ -59,7 +35,6 @@ private static List<ContextInstance> parseContext(Ctx<?>[] context) {
5935
.toList();
6036
}
6137

62-
/** Exception thrown when a type class witness cannot be resolved. PUBLIC API. */
6338
public static class WitnessResolutionException extends RuntimeException {
6439
private WitnessResolutionException(SummonError error) {
6540
super(error.format());

src/main/java/com/garciat/typeclasses/api/Ctx.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,17 @@
44
import java.lang.reflect.Type;
55
import java.util.Objects;
66

7-
/**
8-
* Context token for capturing type class instances at runtime.
9-
*
10-
* <p>Usage:
11-
*
12-
* <pre>{@code
13-
* Show<String> showString = ...;
14-
* Ctx<Show<String>> ctx = new Ctx<>(showString) {};
15-
* }</pre>
16-
*
17-
* <p><b>PUBLIC API</b>: Used for passing explicit type class instances to witness resolution.
18-
*
19-
* @param <T> the type class instance type
20-
*/
217
public abstract class Ctx<T> {
228
private final T instance;
239

24-
/**
25-
* Constructs a context with the given instance.
26-
*
27-
* @param instance the type class instance
28-
*/
2910
public Ctx(T instance) {
3011
this.instance = instance;
3112
}
3213

33-
/**
34-
* Returns the instance.
35-
*
36-
* @return the type class instance
37-
*/
3814
public T instance() {
3915
return instance;
4016
}
4117

42-
/**
43-
* Returns the captured type.
44-
*
45-
* @return the Type object representing T
46-
*/
4718
public Type type() {
4819
return Objects.requireNonNull(
4920
((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
package com.garciat.typeclasses.api;
22

3-
/**
4-
* Base interface for kind-level types, providing basic kind checking in Java.
5-
*
6-
* <p>This interface is used to represent type-level kinds, similar to kinds in Haskell's type
7-
* system.
8-
*
9-
* <p><b>PUBLIC API</b>: This is part of the library's public API. Users implementing custom data
10-
* types will need to use this interface.
11-
*/
123
public interface Kind<K extends Kind.Base> {
13-
/** Base interface for all kinds. */
144
sealed interface Base permits KStar, KArr {}
155

16-
/** KStar represents the kind * (star) - the kind of proper types. */
176
final class KStar implements Base {}
187

19-
/** KArr k represents the kind * -> k - the kind of type constructors. */
208
final class KArr<K extends Base> implements Base {}
219
}
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
11
package com.garciat.typeclasses.api;
22

3-
/**
4-
* Full application of a unary type constructor.
5-
*
6-
* <p>TApp :: (* -> *) -> * -> *
7-
*
8-
* <p><b>PUBLIC API</b>: This is part of the library's public API. Users will use this in type
9-
* signatures.
10-
*
11-
* @param <Tag> the type constructor tag
12-
* @param <A> the applied type argument
13-
*/
143
public interface TApp<Tag extends Kind<Kind.KArr<Kind.KStar>>, A> extends Kind<Kind.KStar> {}
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
package com.garciat.typeclasses.api;
22

3-
/**
4-
* Partial application of a binary type constructor.
5-
*
6-
* <p>TPar :: (* -> * -> *) -> * -> (* -> *)
7-
*
8-
* <p><b>PUBLIC API</b>: This is part of the library's public API. Users will use this in type
9-
* signatures.
10-
*
11-
* @param <Tag> the type constructor tag
12-
* @param <A> the first applied type argument
13-
*/
143
public interface TPar<Tag extends Kind<Kind.KArr<Kind.KArr<Kind.KStar>>>, A>
154
extends Kind<Kind.KArr<Kind.KStar>> {}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
package com.garciat.typeclasses.api;
22

3-
/**
4-
* Base class for type-level tags. Subclasses of this class represent type constructor tags used in
5-
* higher-kinded type encoding.
6-
*
7-
* <p><b>PUBLIC API</b>: This is part of the library's public API. Users implementing custom data
8-
* types will need to extend this class.
9-
*
10-
* @param <K> the kind of this tag
11-
*/
123
public abstract class TagBase<K extends Kind.Base> implements Kind<K> {}

src/main/java/com/garciat/typeclasses/api/Ty.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,7 @@
44
import java.lang.reflect.Type;
55
import java.util.Objects;
66

7-
/**
8-
* Type token for capturing type information at runtime.
9-
*
10-
* <p>Usage:
11-
*
12-
* <pre>{@code
13-
* Show<String> showString = TypeClasses.witness(new Ty<Show<String>>() {});
14-
* }</pre>
15-
*
16-
* <p><b>PUBLIC API</b>: This is the main interface users interact with to summon type class
17-
* instances.
18-
*
19-
* @param <T> the type being captured
20-
*/
217
public interface Ty<T> {
22-
/**
23-
* Returns the captured type.
24-
*
25-
* @return the Type object representing T
26-
*/
278
default Type type() {
289
return Objects.requireNonNull(
2910
((ParameterizedType) getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]);

src/main/java/com/garciat/typeclasses/api/TypeClass.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,15 @@
33
import java.lang.annotation.Retention;
44
import java.lang.annotation.RetentionPolicy;
55

6-
/**
7-
* Marks an interface as a type class.
8-
*
9-
* <p>Type classes are interfaces that define a set of operations that can be implemented for
10-
* various types. The type class system uses compile-time and runtime reflection to automatically
11-
* resolve instances.
12-
*
13-
* <p><b>PUBLIC API</b>: This is part of the library's public API. Users define and implement type
14-
* classes using this annotation.
15-
*/
166
@Retention(RetentionPolicy.RUNTIME)
177
public @interface TypeClass {
18-
/** Marks a method as a witness (instance) of a type class. */
198
@Retention(RetentionPolicy.RUNTIME)
209
@interface Witness {
21-
/**
22-
* Specifies the overlap behavior for this witness.
23-
*
24-
* @return the overlap behavior
25-
*/
2610
Overlap overlap() default Overlap.NONE;
2711

28-
/** Defines how instances can overlap with other instances. */
2912
enum Overlap {
30-
/** No overlap allowed (default). */
3113
NONE,
32-
/** This instance can overlap and take precedence over others. */
3314
OVERLAPPING,
34-
/** This instance can be overlapped by others. */
3515
OVERLAPPABLE
3616
}
3717
}

0 commit comments

Comments
 (0)