r/functionalprogramming • u/EntryNo8040 • May 29 '26
Python Bringing rigorous Type Classes (Functor, Applicative, Monad) to Python: Introducing Katharos
If you come from Haskell or Rust and have to write Python for ML/AI work, you know the pain: if x is None everywhere, exceptions that silently swallow errors, no ? operator, no HKTs, no sealed types. I got tired of it and built a library to close that gap.
Katharos is a zero-dependency Python library that gives you Maybe, Either/Result, IO, the list monad, Semigroup, Monoid, Functor, Applicative, and Monad — all fully typed and passing pyright strict mode.
https://github.com/kamalfarahani/katharos
The Engineering Challenge
The hard part is that Python has no HKTs and no sealed keyword (as of 3.13). There's no way to say Functor f or write :: f a -> (a -> b) -> f b generically. The workaround is structural gymnastics: a two-parameter generic class hierarchy (Functor[F, A], Applicative[App, A], Monad[M, A]) plus @final on concrete types to prevent unsafe subclassing. It's not pretty internally, but the external API stays clean.
Operator Mapping
If you already think in Haskell or Rust, here's the translation table:
| Katharos | Haskell | Rust |
|---|---|---|
m | f | m >>= f | m.and_then(f) |
v ** wrapped_f | wrapped_f <*> v | — |
a >> b | a >> b | — |
a @ b | a <> b | — |
@do(M) decorator | do { ... } | — |
Examples
1. Maybe[A] — Haskell's Maybe a / Rust's Option<T>
No more if x is None chains. Short-circuits automatically on Nothing.
return
return
# | is >>=
| | # Just(1.5811...)
| | # Nothing() — short-circuits at safe_div
| | # Nothing() — short-circuits at safe_sqrt
# fmap for pure transformations
# Just(10)
# Nothing()
2. Result[E, A] — Haskell's Either e a / Rust's Result<T, E>
Errors as values. The | chain (>>=) stops at the first Failure, exactly like Rust's ?.
return
return
return
return
| # Success(42)
| # Failure(ValueError("invalid literal..."))
| # Failure(ValueError("-5 is not positive"))
# fmap only runs on the success path
# Success(84)
3. do-notation — Python do blocks, exactly like Haskell
The @do(M) decorator desugars yield into >>= chains. Each yield unwraps the value; short-circuits on Nothing/Failure. The final return is lifted via M.pure(...).
# Maybe — like Haskell:
# userScore uid = do
# name <- lookupUser uid
# score <- lookupScore name
# return (name ++ ": " ++ show score)
=
return
=
return
: = yield
: = yield
return f
# Just(alice: 95)
# Nothing() — short-circuits at lookup_user
# Result — equivalent of Rust's ? in a pipeline
return
: = yield
: = yield
return +
# Success(8)
4. ImmutableList[T] — the list monad, non-determinism included
ImmutableList is a full Monad + Monoid. Bind (|) is concatMap. The do-notation gives you Haskell list comprehensions.
# concatMap / flatMap
|
# ImmutableList([1, -1, 2, -2, 3, -3])
# do-notation = list comprehension
# In Haskell: [(color, size) | color <- ["red","blue"], size <- ["S","M","L"]]
: = yield
: = yield
return
# ImmutableList([
# ('red','S'), ('red','M'), ('red','L'),
# ('blue','S'), ('blue','M'), ('blue','L')
# ])
# Monoid: @ is <>
@ # ImmutableList([1, 2, 3, 4])
# ImmutableList([]) — mempty
5. Semigroup / Monoid — @ is <>
Sum, Product, and NonEmptyList are all Semigroup/Monoid instances. F.sigma is fold1 / sconcat over a NonEmptyList.
# @ is <>
@ @ # Sum(12)
@ @ # Product(24)
# identity() is mempty
# Sum(0)
# Product(1)
# F.sigma is fold1 / sconcat — requires NonEmptyList (no empty-list footgun)
=
# Sum(10)
# NonEmptyList itself is a Semigroup (no Monoid — no empty case)
=
=
@ # NonEmptyList([1, 2, 3, 4, 5, 6])
Docs
Full docs at https://katharos.readthedocs.io. If this scratches an itch for you, a star on the repo goes a long way.
https://github.com/kamalfarahani/katharos