Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

kernel.nix

Source: /nix/store/85n2k2gkxxwwpmhwfp2asxfln07nbklv-source/nix/kernel.nix

Module Description

Core kernel primitives for the NFX effect system

Combinators

adapt

The fundamental combinator - everything else builds on this.

adapt is the core primitive that enables all effect transformations. It simultaneously handles:

  1. Contravariant mapping of context requirements (what the effect needs)
  2. Covariant mapping of continuations (what happens given the result)

Type Signature

Fx<S,V> -> (T -> S) -> (T -> S -> V -> Fx<T,U>) -> Fx<T,U>

Parameters

  • e: The effect to transform
  • cmap: Context mapper T -> S - extracts inner requirement from outer context
  • fmap: Continuation (T, S, V) -> Fx<T,U> - transforms the result

How It Works

Creates an adapt node { _tag="adapt", e, cmap, fmap } — a pure data structure. The interpreter (runFx) peels adapt nodes iteratively, eliminating the O(N) stack growth that a recursive closure would cause.

Example

# Double the result of an effect
adapt (pure 21) (s: s) (_t: _s: v: immediate {} (v * 2))

See Also

  • map - Simpler value transformation (built on adapt)
  • contraMap - Simpler context transformation (built on adapt)

immediate

Creates an immediate (already resolved) effect.

An immediate effect is the terminal case of the Fx type - it carries both a state and a value, meaning it needs no further context to produce its result. This is one of two fundamental constructors for the Fx algebraic data type.

Type Signature

state -> value -> Fx<state, value>

Parameters

  • state: The current state/context that was used to produce the value
  • value: The computed result

Example

immediate {} 42  # Effect holding value 42 with empty state
immediate { count = 1; } "done"  # Effect with state and string value

See Also

  • pending - The other Fx constructor for suspended computations
  • runFx - Evaluates an effect to extract its value

pending

Creates a pending (suspended) effect awaiting context.

A pending effect wraps an “ability” - a function that will be called given context to produce the next step of computation. This is the mechanism by which effects express their requirements: the ability requests something from the context.

Type Signature

(context -> Fx<S, V>) -> Fx<S, V>

Parameters

  • ability: A function context -> Fx<S, V> that consumes context and produces the next computation step

Example

# Effect that reads a number from context and doubles it
pending (n: immediate n (n * 2))

# Effect requesting an attribute from context
pending (ctx: immediate ctx ctx.username)

See Also

  • immediate - The terminal case, already has its value
  • adapt - The fundamental combinator for transforming effects

Generated from /nix/store/85n2k2gkxxwwpmhwfp2asxfln07nbklv-source/nix/kernel.nix