format: lib/fixed-points

This commit is contained in:
Johannes Kirschbauer 2024-08-15 11:11:52 +02:00
parent b24de4052b
commit dea395bbad
No known key found for this signature in database

View File

@ -63,7 +63,6 @@ rec {
See [`extends`](#function-library-lib.fixedPoints.extends) for an example use case. See [`extends`](#function-library-lib.fixedPoints.extends) for an example use case.
There `self` is also often called `final`. There `self` is also often called `final`.
# Inputs # Inputs
`f` `f`
@ -90,7 +89,12 @@ rec {
::: :::
*/ */
fix = f: let x = f x; in x; fix =
f:
let
x = f x;
in
x;
/** /**
A variant of `fix` that records the original recursive attribute set in the A variant of `fix` that records the original recursive attribute set in the
@ -99,14 +103,20 @@ rec {
This is useful in combination with the `extends` function to This is useful in combination with the `extends` function to
implement deep overriding. implement deep overriding.
# Inputs # Inputs
`f` `f`
: 1\. Function argument : 1\. Function argument
*/ */
fix' = f: let x = f x // { __unfix__ = f; }; in x; fix' =
f:
let
x = f x // {
__unfix__ = f;
};
in
x;
/** /**
Return the fixpoint that `f` converges to when called iteratively, starting Return the fixpoint that `f` converges to when called iteratively, starting
@ -117,7 +127,6 @@ rec {
0 0
``` ```
# Inputs # Inputs
`f` `f`
@ -134,13 +143,12 @@ rec {
(a -> a) -> a -> a (a -> a) -> a -> a
``` ```
*/ */
converge = f: x: converge =
f: x:
let let
x' = f x; x' = f x;
in in
if x' == x if x' == x then x else converge f x';
then x
else converge f x';
/** /**
Extend a function using an overlay. Extend a function using an overlay.
@ -149,7 +157,6 @@ rec {
A fixed-point function is a function which is intended to be evaluated by passing the result of itself as the argument. A fixed-point function is a function which is intended to be evaluated by passing the result of itself as the argument.
This is possible due to Nix's lazy evaluation. This is possible due to Nix's lazy evaluation.
A fixed-point function returning an attribute set has the form A fixed-point function returning an attribute set has the form
```nix ```nix
@ -257,7 +264,6 @@ rec {
``` ```
::: :::
# Inputs # Inputs
`overlay` `overlay`
@ -299,8 +305,7 @@ rec {
::: :::
*/ */
extends = extends =
overlay: overlay: f:
f:
# The result should be thought of as a function, the argument of that function is not an argument to `extends` itself # The result should be thought of as a function, the argument of that function is not an argument to `extends` itself
( (
final: final:
@ -316,9 +321,11 @@ rec {
*/ */
composeExtensions = composeExtensions =
f: g: final: prev: f: g: final: prev:
let fApplied = f final prev; let
fApplied = f final prev;
prev' = prev // fApplied; prev' = prev // fApplied;
in fApplied // g final prev'; in
fApplied // g final prev';
/** /**
Composes a list of [`overlays`](#chap-overlays) and returns a single overlay function that combines them. Composes a list of [`overlays`](#chap-overlays) and returns a single overlay function that combines them.
@ -342,7 +349,6 @@ rec {
- `final` is the result of the fixed-point function, with all overlays applied. - `final` is the result of the fixed-point function, with all overlays applied.
- `prev` is the result of the previous overlay function. - `prev` is the result of the previous overlay function.
# Type # Type
``` ```
@ -385,8 +391,7 @@ rec {
``` ```
::: :::
*/ */
composeManyExtensions = composeManyExtensions = lib.foldr (x: y: composeExtensions x y) (final: prev: { });
lib.foldr (x: y: composeExtensions x y) (final: prev: {});
/** /**
Create an overridable, recursive attribute set. For example: Create an overridable, recursive attribute set. For example:
@ -414,7 +419,6 @@ rec {
Same as `makeExtensible` but the name of the extending attribute is Same as `makeExtensible` but the name of the extending attribute is
customized. customized.
# Inputs # Inputs
`extenderName` `extenderName`
@ -425,8 +429,13 @@ rec {
: 2\. Function argument : 2\. Function argument
*/ */
makeExtensibleWithCustomName = extenderName: rattrs: makeExtensibleWithCustomName =
fix' (self: (rattrs self) // { extenderName: rattrs:
fix' (
self:
(rattrs self)
// {
${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs); ${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs);
}); }
);
} }