Supported Types
Higher-Kinded-J provides Higher-Kinded Type (HKT) simulation capabilities, allowing various Java types and custom types to be used with generic functional type classes like Functor
, Applicative
, Monad
, and MonadError
.
This is achieved by representing the application of a type constructor F
to a type A
as Kind<F_WITNESS, A>
, where F_WITNESS
is a special "witness" or phantom type unique to the type constructor F
.
Key for Understanding Entries:
- Type: The Java type or custom type being simulated.
XxxKind<A>
Interface: The specificKind
interface for this type (e.g.,OptionalKind<A>
). It extendsKind<XxxKind.Witness, A>
and usually contains the nestedfinal class Witness {}
.- Witness Type
F_WITNESS
: The phantom type used as the first parameter toKind
(e.g.,OptionalKind.Witness
). This is what parameterizes the type classes (e.g.,Monad<OptionalKind.Witness>
). XxxKindHelper
Class: Provideswiden
andnarrow
methods.- For external types (like
java.util.List
,java.util.Optional
),widen
typically creates an internalXxxHolder
record which implementsXxxKind<A>
, andnarrow
extracts the Java type from this holder. - For library-defined types (
Id
,Maybe
,IO
,Try
, monad transformers), if the type itself directly implementsXxxKind<A>
, thenwiden
often performs a (checked) cast, andnarrow
checksinstanceof
the actual type and casts.
- For external types (like
- Type Class Instances: Concrete implementations of
Functor<F_WITNESS>
,Monad<F_WITNESS>
, etc.
1. Id<A>
(Identity)
- Type Definition: A custom record (
Id
) that directly wraps a valueA
. It's the simplest monad. IdKind<A>
Interface:Id<A>
itself implementsIdKind<A>
, andIdKind<A> extends Kind<Id.Witness, A>
.- Witness Type
F_WITNESS
:Id.Witness
IdKindHelper
:IdKindHelper
(wrap
castsId
toKind
,unwrap
castsKind
toId
;narrow
is a convenience for unwrap).- Type Class Instances:
IdentityMonad
(Monad<Id.Witness>
)
- Notes:
Id.of(a)
createsId(a)
.map
andflatMap
operate directly. Useful as a base for monad transformers and generic programming with no extra effects.Id<A>
directly implementsIdKind<A>
. - Usage: How to use the Identity Monad
2. java.util.List<A>
- Type Definition: Standard Java
java.util.List<A>
. ListKind<A>
Interface:ListKind<A>
extendsKind<ListKind.Witness, A>
.- Witness Type
F_WITNESS
:ListKind.Witness
ListKindHelper
: Uses an internalListHolder<A>
record that implementsListKind<A>
to wrapjava.util.List<A>
.- Type Class Instances:
ListFunctor
(Functor<ListKind.Witness>
)ListMonad
(Monad<ListKind.Witness>
)
- Notes: Standard list monad behavior.
of(a)
creates a singleton listList.of(a)
;of(null)
results in an empty list. - Usage: How to use the List Monad
3. java.util.Optional<A>
- Type Definition: Standard Java
java.util.Optional<A>
. OptionalKind<A>
Interface:OptionalKind<A>
extendsKind<OptionalKind.Witness, A>
.- Witness Type
F_WITNESS
:OptionalKind.Witness
OptionalKindHelper
: Uses an internalOptionalHolder<A>
record that implementsOptionalKind<A>
to wrapjava.util.Optional<A>
.- Type Class Instances:
OptionalFunctor
(Functor<OptionalKind.Witness>
)OptionalMonad
(MonadError<OptionalKind.Witness, Unit>
)
- Notes:
Optional.empty()
is the error state.raiseError(Unit.INSTANCE)
createsOptional.empty()
.of(value)
usesOptional.ofNullable(value)
. - Usage: How to use the Optional Monad
4. Maybe<A>
- Type Definition: Custom sealed interface (
Maybe
) withJust<A>
(non-null) andNothing<A>
implementations. MaybeKind<A>
Interface:Maybe<A>
itself implementsMaybeKind<A>
, andMaybeKind<A> extends Kind<MaybeKind.Witness, A>
.- Witness Type
F_WITNESS
:MaybeKind.Witness
MaybeKindHelper
:widen
castsMaybe
toKind
;unwrap
castsKind
toMaybe
. Providesjust(value)
,nothing()
,fromNullable(value)
.- Type Class Instances:
MaybeFunctor
(Functor<MaybeKind.Witness>
)MaybeMonad
(MonadError<MaybeKind.Witness, Unit>
)
- Notes:
Nothing
is the error state;raiseError(Unit.INSTANCE
) createsNothing
.Maybe.just(value)
requires non-null.MaybeMonad.of(value)
usesMaybe.fromNullable()
. - Usage: How to use the Maybe Monad
5. Either<L, R>
- Type Definition: Custom sealed interface (
Either
) withLeft<L,R>
andRight<L,R>
records. EitherKind<L, R>
Interface:Either<L,R>
itself implementsEitherKind<L,R>
, andEitherKind<L,R> extends Kind<EitherKind.Witness<L>, R>
.- Witness Type
F_WITNESS
:EitherKind.Witness<L>
(Error typeL
is fixed for the witness). EitherKindHelper
:wrap
castsEither
toKind
;unwrap
castsKind
toEither
. Providesleft(l)
,right(r)
.- Type Class Instances:
EitherFunctor<L>
(Functor<EitherKind.Witness<L>>
)EitherMonad<L>
(MonadError<EitherKind.Witness<L>, L>
)
- Notes: Right-biased.
Left(l)
is the error state.of(r)
createsRight(r)
. - Usage: How to use the Either Monad
6. Try<A>
- Type Definition: Custom sealed interface (
Try
) withSuccess<A>
andFailure<A>
(wrappingThrowable
). TryKind<A>
Interface:Try<A>
itself implementsTryKind<A>
, andTryKind<A> extends Kind<TryKind.Witness, A>
.- Witness Type
F_WITNESS
:TryKind.Witness
TryKindHelper
:wrap
castsTry
toKind
;unwrap
castsKind
toTry
. Providessuccess(value)
,failure(throwable)
,tryOf(supplier)
.- Type Class Instances:
TryFunctor
(Functor<TryKind.Witness>
)TryApplicative
(Applicative<TryKind.Witness>
)TryMonad
(MonadError<TryKind.Witness, Throwable>
)
- Notes:
Failure(t)
is the error state.of(v)
createsSuccess(v)
. - Usage: How to use the Try Monad
7. java.util.concurrent.CompletableFuture<A>
- Type Definition: Standard Java
java.util.concurrent.CompletableFuture<A>
. CompletableFutureKind<A>
Interface:CompletableFutureKind<A>
extendsKind<CompletableFutureKind.Witness, A>
.- Witness Type
F_WITNESS
:CompletableFutureKind.Witness
CompletableFutureKindHelper
: Uses an internalCompletableFutureHolder<A>
record. Provideswrap
,unwrap
,join
.- Type Class Instances:
CompletableFutureFunctor
(Functor<CompletableFutureKind.Witness>
)CompletableFutureApplicative
(Applicative<CompletableFutureKind.Witness>
)CompletableFutureMonad
(Monad<CompletableFutureKind.Witness>
)CompletableFutureMonad
(MonadError<CompletableFutureKind.Witness, Throwable>
)
- Notes: Represents asynchronous computations. A failed future is the error state.
of(v)
createsCompletableFuture.completedFuture(v)
. - Usage: How to use the CompletableFuture Monad
8. IO<A>
- Type Definition: Custom interface (
IO
) representing a deferred, potentially side-effecting computation. IOKind<A>
Interface:IO<A>
itself implementsIOKind<A>
, andIOKind<A> extends Kind<IOKind.Witness, A>
.- Witness Type
F_WITNESS
:IOKind.Witness
IOKindHelper
:wrap
castsIO
toKind
;unwrap
castsKind
toIO
. Providesdelay(supplier)
,unsafeRunSync(kind)
.- Type Class Instances:
IOFunctor
(Functor<IOKind.Witness>
)IOApplicative
(Applicative<IOKind.Witness>
)IOMonad
(Monad<IOKind.Witness>
)
- Notes: Evaluation is deferred until
unsafeRunSync
. Exceptions during execution are generally unhandled byIOMonad
itself unless caught within the IO's definition. - Usage: How to use the IO Monad
9. Lazy<A>
- Type Definition: Custom class (
Lazy
) for deferred computation with memoization. LazyKind<A>
Interface:Lazy<A>
itself implementsLazyKind<A>
, andLazyKind<A> extends Kind<LazyKind.Witness, A>
.- Witness Type
F_WITNESS
:LazyKind.Witness
LazyKindHelper
:wrap
castsLazy
toKind
;unwrap
castsKind
toLazy
. Providesdefer(supplier)
,now(value)
,force(kind)
.- Type Class Instances:
LazyMonad
(Monad<LazyKind.Witness>
)
- Notes: Result or exception is memoized.
of(a)
creates an already evaluatedLazy.now(a)
. - Usage: How to use the Lazy Monad
10. Reader<R_ENV, A>
- Type Definition: Custom functional interface (
Reader
) wrappingFunction<R_ENV, A>
. ReaderKind<R_ENV, A>
Interface:Reader<R_ENV,A>
itself implementsReaderKind<R_ENV,A>
, andReaderKind<R_ENV,A> extends Kind<ReaderKind.Witness<R_ENV>, A>
.- Witness Type
F_WITNESS
:ReaderKind.Witness<R_ENV>
(Environment typeR_ENV
is fixed). ReaderKindHelper
:wrap
castsReader
toKind
;unwrap
castsKind
toReader
. Providesreader(func)
,ask()
,constant(value)
,runReader(kind, env)
.- Type Class Instances:
ReaderFunctor<R_ENV>
(Functor<ReaderKind.Witness<R_ENV>>
)ReaderApplicative<R_ENV>
(Applicative<ReaderKind.Witness<R_ENV>>
)ReaderMonad<R_ENV>
(Monad<ReaderKind.Witness<R_ENV>>
)
- Notes:
of(a)
creates aReader
that ignores the environment and returnsa
. - Usage: How to use the Reader Monad
11. State<S, A>
- Type Definition: Custom functional interface (
State
) wrappingFunction<S, StateTuple<S, A>>
. StateKind<S,A>
Interface:State<S,A>
itself implementsStateKind<S,A>
, andStateKind<S,A> extends Kind<StateKind.Witness<S>, A>
.- Witness Type
F_WITNESS
:StateKind.Witness<S>
(State typeS
is fixed). StateKindHelper
:wrap
castsState
toKind
;unwrap
castsKind
toState
. Providespure(value)
,get()
,set(state)
,modify(func)
,inspect(func)
,runState(kind, initialState)
, etc.- Type Class Instances:
StateFunctor<S>
(Functor<StateKind.Witness<S>>
)StateApplicative<S>
(Applicative<StateKind.Witness<S>>
)StateMonad<S>
(Monad<StateKind.Witness<S>>
)
- Notes:
of(a)
(pure
) returnsa
without changing state. - Usage: How to use the State Monad
12. Writer<W, A>
- Type Definition: Custom record (
Writer
) holding(W log, A value)
. RequiresMonoid<W>
. WriterKind<W, A>
Interface:Writer<W,A>
itself implementsWriterKind<W,A>
, andWriterKind<W,A> extends Kind<WriterKind.Witness<W>, A>
.- Witness Type
F_WITNESS
:WriterKind.Witness<W>
(Log typeW
and itsMonoid
are fixed). WriterKindHelper
:wrap
castsWriter
toKind
;unwrap
castsKind
toWriter
. Providesvalue(monoid, val)
,tell(monoid, log)
,runWriter(kind)
, etc.- Type Class Instances: (Requires
Monoid<W>
for Applicative/Monad)WriterFunctor<W>
(Functor<WriterKind.Witness<W>>
)WriterApplicative<W>
(Applicative<WriterKind.Witness<W>>
)WriterMonad<W>
(Monad<WriterKind.Witness<W>>
)
- Notes:
of(a)
(value
) producesa
with an empty log (fromMonoid.empty()
). - Usage: How to use the Writer Monad
13. Validated<E, A>
- Type Definition: Custom sealed interface (
Validated
) withValid<E, A>
(holdingA
) andInvalid<E, A>
(holdingE
) implementations. ValidatedKind<E, A>
Interface: Defines the HKT structure (ValidatedKind
) forValidated<E,A>
. It extendsKind<ValidatedKind.Witness<E>, A>
. ConcreteValid<E,A>
andInvalid<E,A>
instances are cast to this kind byValidatedKindHelper
.- Witness Type
F_WITNESS
:ValidatedKind.Witness<E>
(Error typeE
is fixed for the HKT witness). ValidatedKindHelper
Class: (ValidatedKindHelper
).widen
castsValidated<E,A>
(specificallyValid
orInvalid
instances) toKind<ValidatedKind.Witness<E>, A>
.narrow
castsKind
back toValidated<E,A>
. Provides static factory methodsvalid(value)
andinvalid(error)
that return the Kind-wrapped type.- Type Class Instances: (Error type
E
is fixed for the monad instance)ValidatedMonad<E>
(MonadError<ValidatedKind.Witness<E>, E>
). This also providesMonad
,Functor
, andApplicative
behavior.
- Notes:
Validated
is right-biased, meaning operations likemap
andflatMap
apply to theValid
case and propagateInvalid
untouched.ValidatedMonad.of(a)
creates aValid(a)
. As aMonadError
,ValidatedMonad
providesraiseError(error)
to create anInvalid(error)
andhandleErrorWith(kind, handler)
for standardized error recovery. Theap
method is also right-biased and does not accumulate errors from multipleInvalid
s in the typical applicative sense; it propagates the firstInvalid
encountered or anInvalid
function. - Usage: How to use the Validated Monad