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 specificKindinterface 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>). XxxKindHelperClass: Provideswidenandnarrowmethods.- For external types (like
java.util.List,java.util.Optional),widentypically creates an internalXxxHolderrecord which implementsXxxKind<A>, andnarrowextracts the Java type from this holder. - For library-defined types (
Id,IO,Maybe,Either,Validated,Try, monad transformers), the type itself directly implementsXxxKind<A>. This meanswidenperforms a null check and direct cast (zero overhead), andnarrowchecksinstanceofthe 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<IdKind.Witness, A>.- Witness Type
F_WITNESS:IdKind.Witness IdKindHelper:IdKindHelper(wrapcastsIdtoKind,unwrapcastsKindtoId;narrowis a convenience for unwrap).- Type Class Instances:
IdMonad(Monad<IdKind.Witness>)
- Notes:
Id.of(a)createsId(a).mapandflatMapoperate 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 behaviour.
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.stream.Stream<A>
- Type Definition: Standard Java
java.util.stream.Stream<A>. StreamKind<A>Interface:StreamKind<A>extendsKind<StreamKind.Witness, A>.- Witness Type
F_WITNESS:StreamKind.Witness StreamKindHelper: Uses an internalStreamHolder<A>record that implementsStreamKind<A>to wrapjava.util.stream.Stream<A>. Provideswiden,narrow.- Type Class Instances:
StreamFunctor(Functor<StreamKind.Witness>)StreamApplicative(Applicative<StreamKind.Witness>)StreamMonad(MonadZero<StreamKind.Witness>)StreamTraverse(Traverse<StreamKind.Witness>,Foldable<StreamKind.Witness>)
- Notes: Lazy, potentially infinite sequences with single-use semantics - each Stream can only be consumed once. Attempting to reuse a consumed stream throws
IllegalStateException.of(a)creates singleton stream;of(null)creates empty stream.zero()returns empty stream. UseStreamOpsfor additional utility operations. - Usage: How to use the Stream Monad
4. Trampoline<A>
- Type Definition: Custom sealed interface (
Trampoline) implementing stack-safe recursion through trampolining. Provides three constructors:Done<A>(completed computation),More<A>(deferred computation), andFlatMap<A, B>(monadic sequencing). TrampolineKind<A>Interface:Trampoline<A>itself implementsTrampolineKind<A>, andTrampolineKind<A> extends Kind<TrampolineKind.Witness, A>.- Witness Type
F_WITNESS:TrampolineKind.Witness TrampolineKindHelper:widencastsTrampolinetoKind;narrowcastsKindtoTrampoline. Providesdone(value)for completed computations anddefer(supplier)for deferred evaluation.- Type Class Instances:
TrampolineFunctor(Functor<TrampolineKind.Witness>)TrampolineMonad(Monad<TrampolineKind.Witness>)
- Notes: Enables stack-safe tail recursion by converting recursive calls into iterative data structure processing, preventing
StackOverflowErrorin deeply recursive computations (verified with 100,000+ iterations).done(value)creates an already evaluated result;defer(supplier)defers computation. Therun()method executes the trampoline iteratively using an explicit stack. Essential for recursive algorithms (factorial, Fibonacci, tree traversals) and providesTrampolineUtilsfor guaranteed stack-safe applicative operations. - Usage: How to use the Trampoline Monad
5. Free<F, A>
- Type Definition: Custom sealed interface (
Free) representing programmes as data structures that can be interpreted in different ways. Provides three constructors:Pure<F,A>(terminal value),Suspend<F,A>(suspended computation), andFlatMapped<F,X,A>(monadic sequencing). FreeKind<F, A>Interface:Free<F,A>itself implementsFreeKind<F,A>, andFreeKind<F,A> extends Kind<FreeKind.Witness<F>, A>.- Witness Type
F_WITNESS:FreeKind.Witness<F>(whereFis the instruction set functor) FreeKindHelper:widencastsFreetoKind;narrowcastsKindtoFree. Providespure(value),suspend(computation),liftF(fa, functor).- Type Class Instances:
FreeFunctor<F>(Functor<FreeKind.Witness<F>>)FreeMonad<F>(Monad<FreeKind.Witness<F>>)
- Notes: Enables building domain-specific languages (DSLs) as composable data structures. Programmes are interpreted via
foldMapwith natural transformations, allowing multiple interpreters (IO, Test, Optimisation, etc.). Stack-safe execution using Higher-Kinded-J'sTrampolinemonad internally, demonstrating library composability (verified with 10,000+ operations). Essential for separating programme description from execution, enabling testability and alternative interpretations. ProvidesliftFto lift functor values into Free,mapandflatMapfor composition, andfoldMapfor interpretation. Useful for building testable workflows, query languages, and effect systems where the same programme needs different execution strategies. - Usage: How to use the Free Monad
6. 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
7. Maybe<A>
- Type Definition: Custom sealed interface (
Maybe) withJust<A>(non-null) andNothing<A>implementations. MaybeKind<A>Interface:Just<T>andNothing<T>directly implementMaybeKind<T>, which extendsKind<MaybeKind.Witness, T>.- Witness Type
F_WITNESS:MaybeKind.Witness MaybeKindHelper:widenperforms null check and castsMaybetoKind(zero overhead);narrowchecksinstanceof Maybeand casts. Providesjust(value),nothing(),fromNullable(value).- Type Class Instances:
MaybeFunctor(Functor<MaybeKind.Witness>)MaybeMonad(MonadError<MaybeKind.Witness, Unit>)
- Notes:
Nothingis 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
8. Either<L, R>
- Type Definition: Custom sealed interface (
Either) withLeft<L,R>andRight<L,R>records. EitherKind<L, R>Interface:Either.Left<L,R>andEither.Right<L,R>directly implementEitherKind<L,R>(andEitherKind2<L,R>for bifunctor operations), which extendsKind<EitherKind.Witness<L>, R>.- Witness Type
F_WITNESS:EitherKind.Witness<L>(Error typeLis fixed for the witness). EitherKindHelper:widenperforms null check and castsEithertoKind(zero overhead);narrowchecksinstanceof Eitherand casts. 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
9. 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:wrapcastsTrytoKind;unwrapcastsKindtoTry. 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
10. 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
11. IO<A>
- Type Definition: Custom interface (
IO) representing a deferred, potentially side-effecting computation. IOKind<A>Interface:IO<A>directly extendsIOKind<A>, which extendsKind<IOKind.Witness, A>.- Witness Type
F_WITNESS:IOKind.Witness IOKindHelper:widenperforms null check and returns theIOdirectly asKind(zero overhead);narrowchecksinstanceof IOand casts. 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 byIOMonaditself unless caught within the IO's definition. - Usage: How to use the IO Monad
12. 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:wrapcastsLazytoKind;unwrapcastsKindtoLazy. 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
13. 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_ENVis fixed). ReaderKindHelper:wrapcastsReadertoKind;unwrapcastsKindtoReader. 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 aReaderthat ignores the environment and returnsa. - Usage: How to use the Reader Monad
14. 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 typeSis fixed). StateKindHelper:wrapcastsStatetoKind;unwrapcastsKindtoState. 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) returnsawithout changing state. - Usage: How to use the State Monad
15. 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 typeWand itsMonoidare fixed). WriterKindHelper:wrapcastsWritertoKind;unwrapcastsKindtoWriter. 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) producesawith an empty log (fromMonoid.empty()). - Usage: How to use the Writer Monad
16. 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 typeEis fixed for the HKT witness). ValidatedKindHelperClass: (ValidatedKindHelper).widencastsValidated<E,A>(specificallyValidorInvalidinstances) toKind<ValidatedKind.Witness<E>, A>.narrowcastsKindback toValidated<E,A>. Provides static factory methodsvalid(value)andinvalid(error)that return the Kind-wrapped type.- Type Class Instances: (Error type
Eis fixed for the monad instance)ValidatedMonad<E>(MonadError<ValidatedKind.Witness<E>, E>). This also providesMonad,Functor, andApplicativebehaviour.
- Notes:
Validatedis right-biased, meaning operations likemapandflatMapapply to theValidcase and propagateInvaliduntouched.ValidatedMonad.of(a)creates aValid(a). As aMonadError,ValidatedMonadprovidesraiseError(error)to create anInvalid(error)andhandleErrorWith(kind, handler)for standardised error recovery. Theapmethod is also right-biased and does not accumulate errors from multipleInvalids in the typical applicative sense; it propagates the firstInvalidencountered or anInvalidfunction. - Usage: How to use the Validated Monad
17. Const<C, A>
- Type Definition: Custom record (
Const) holding a constant value of typeCwhilst treatingAas a phantom type parameter (present in the type signature but not stored). ConstKind2<C, A>Interface: (ConstKind2<C, A>) extendsKind2<ConstKind2.Witness, C, A>. This interface allowsConstto be used with bifunctor operations.- Witness Type
F_WITNESS:ConstKind2.Witness(used for bifunctor type class instances). ConstKindHelperClass: (ConstKindHelper). Provideswiden2to castConst<C, A>toKind2<ConstKind2.Witness, C, A>andnarrow2to cast back. Uses an internalConstKind2Holder<C, A>record that implementsConstKind2<C, A>.- Type Class Instances: (Only bifunctor, no monad instance as mapping the phantom type has no computational effect)
ConstBifunctor(Bifunctor<ConstKind2.Witness>). This instance providesfirst(transforms the constant value),second(changes only the phantom type), andbimap(combines both, though onlyfirstaffects the constant value).
- Notes: The second type parameter
Ais phantom—it exists only in the type signature and has no runtime representation. CallingmapSecondorsecondpreserves the constant value whilst changing the phantom type in the signature. This makesConstparticularly useful for fold implementations (accumulating a single value), getter patterns in lens libraries (van Laarhoven lenses), and data extraction from structures without transformation. The mapper function insecondis applied tonullfor exception propagation, so use null-safe mappers. Similar toConstin Scala's Cats and Scalaz libraries. - Usage: How to use the Const Type