From 57758098c6563e7bb0372657b74c71028de022e1 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Wed, 28 Jan 2026 18:42:33 +1000 Subject: [PATCH] add Maybe README example --- README.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e53fd5a..d5f9473 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # ❄ NixTypes (nt) ❄ -

Because Nix never held your hand. It shot off your fingers off and spat out God's longest stack trace

+

Because Nix never held your hand. It shot off your fingers and spat out God's longest stack trace

>[!WARNING] > ✨ **Under Construction** ✨ @@ -24,6 +24,66 @@ Some of the sweet sweet batteries included: 4. **A Module System** (say goodbye to managing all your `imports`) 5. **Types, Types, & More Types** (Maybe/Some/None, Monads, Tree, Rose, etc) +## Types For Humans + + +## Types (Not) For Humans +Let's define a Maybe type in two diferent ways: +1. As a polymorphic type `PolyMaybe := Some | None` for monads `Some` and `None` +2. And as an lax-idempotent (Kock–Zöberlein) monad `KZMaybe` with states `KZSome` and `KZNone` +```nix +let + inherit + (nt) + mk + enfNotNull + Type + Sum + Monad' + KZMonad' # apostrophe implies typeclass + ; +in rec { + # === METHOD 1: + Some = Type { + ops = { + Monad'.result = value: mk { inherit value; }; + Monad'.unwrap = self: self.value; + Monad'.bind = self: f: f self.value; + }; + impls = [ Monad' ]; + }; + None = Type { + ops.mk = mk { + Monad'.result = mk {}; + Monad'.unwrap = _: null; + Monad'.bind = _: _: null; + }; + impls = [ Monad' ]; + }; + PolyMaybe = Sum [Some None]; + + # === METHOD 2: + KZSome = value: + assert enfNotNull value "KZSome value"; + KZMaybe; + KZNone = KZMaybe null; + KZMaybe = Type { + ops = { + # Lift a value INTO the monadic context + # NOTE: because KZMaybe implements KZMonad' lax-idempotence + # NOTE: is automatically added to the result operation + Monad'.result = value: mk { inherit value; }; + # Lift a value OUT OF the monadic context + Monad'.unwrap = self: self.value; + # Alter a value inside the monadic context + Monad'.bind = f: x: f x; + }; + + impls = [ KZMonad' ]; + }; +} +``` + ### ❄🎁 Parse the Parcel *Close your eyes with me ok? MMmmmmmmmmm yes just like that...* **NOW** imagine