|
1 | | -# Java Type Classes |
| 1 | +Conventions: |
2 | 2 |
|
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 |
0 commit comments