···11{ lib, ... }:
22rec {
33- # Compute the fixed point of the given function `f`, which is usually an
44- # attribute set that expects its final, non-recursive representation as an
55- # argument:
66- #
77- # f = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
88- #
99- # Nix evaluates this recursion until all references to `self` have been
1010- # resolved. At that point, the final result is returned and `f x = x` holds:
1111- #
1212- # nix-repl> fix f
1313- # { bar = "bar"; foo = "foo"; foobar = "foobar"; }
1414- #
1515- # Type: fix :: (a -> a) -> a
1616- #
1717- # See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
1818- # details.
33+ /*
44+ Compute the fixed point of the given function `f`, which is usually an
55+ attribute set that expects its final, non-recursive representation as an
66+ argument:
77+88+ ```
99+ f = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
1010+ ```
1111+1212+ Nix evaluates this recursion until all references to `self` have been
1313+ resolved. At that point, the final result is returned and `f x = x` holds:
1414+1515+ ```
1616+ nix-repl> fix f
1717+ { bar = "bar"; foo = "foo"; foobar = "foobar"; }
1818+ ```
1919+2020+ Type: fix :: (a -> a) -> a
2121+2222+ See https://en.wikipedia.org/wiki/Fixed-point_combinator for further
2323+ details.
2424+ */
1925 fix = f: let x = f x; in x;
20262121- # A variant of `fix` that records the original recursive attribute set in the
2222- # result. This is useful in combination with the `extends` function to
2323- # implement deep overriding. See pkgs/development/haskell-modules/default.nix
2424- # for a concrete example.
2727+ /*
2828+ A variant of `fix` that records the original recursive attribute set in the
2929+ result, in an attribute named `__unfix__`.
3030+3131+ This is useful in combination with the `extends` function to
3232+ implement deep overriding.
3333+ */
2534 fix' = f: let x = f x // { __unfix__ = f; }; in x;
26352727- # Return the fixpoint that `f` converges to when called recursively, starting
2828- # with the input `x`.
2929- #
3030- # nix-repl> converge (x: x / 2) 16
3131- # 0
3636+ /*
3737+ Return the fixpoint that `f` converges to when called iteratively, starting
3838+ with the input `x`.
3939+4040+ ```
4141+ nix-repl> converge (x: x / 2) 16
4242+ 0
4343+ ```
4444+4545+ Type: (a -> a) -> a -> a
4646+ */
3247 converge = f: x:
3348 let
3449 x' = f x;
···3752 then x
3853 else converge f x';
39544040- # Modify the contents of an explicitly recursive attribute set in a way that
4141- # honors `self`-references. This is accomplished with a function
4242- #
4343- # g = self: super: { foo = super.foo + " + "; }
4444- #
4545- # that has access to the unmodified input (`super`) as well as the final
4646- # non-recursive representation of the attribute set (`self`). `extends`
4747- # differs from the native `//` operator insofar as that it's applied *before*
4848- # references to `self` are resolved:
4949- #
5050- # nix-repl> fix (extends g f)
5151- # { bar = "bar"; foo = "foo + "; foobar = "foo + bar"; }
5252- #
5353- # The name of the function is inspired by object-oriented inheritance, i.e.
5454- # think of it as an infix operator `g extends f` that mimics the syntax from
5555- # Java. It may seem counter-intuitive to have the "base class" as the second
5656- # argument, but it's nice this way if several uses of `extends` are cascaded.
5757- #
5858- # To get a better understanding how `extends` turns a function with a fix
5959- # point (the package set we start with) into a new function with a different fix
6060- # point (the desired packages set) lets just see, how `extends g f`
6161- # unfolds with `g` and `f` defined above:
6262- #
6363- # extends g f = self: let super = f self; in super // g self super;
6464- # = self: let super = { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }; in super // g self super
6565- # = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // g self { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
6666- # = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // { foo = "foo" + " + "; }
6767- # = self: { foo = "foo + "; bar = "bar"; foobar = self.foo + self.bar; }
6868- #
5555+ /*
5656+ Modify the contents of an explicitly recursive attribute set in a way that
5757+ honors `self`-references. This is accomplished with a function
5858+5959+ ```nix
6060+ g = self: super: { foo = super.foo + " + "; }
6161+ ```
6262+6363+ that has access to the unmodified input (`super`) as well as the final
6464+ non-recursive representation of the attribute set (`self`). `extends`
6565+ differs from the native `//` operator insofar as that it's applied *before*
6666+ references to `self` are resolved:
6767+6868+ ```
6969+ nix-repl> fix (extends g f)
7070+ { bar = "bar"; foo = "foo + "; foobar = "foo + bar"; }
7171+ ```
7272+7373+ The name of the function is inspired by object-oriented inheritance, i.e.
7474+ think of it as an infix operator `g extends f` that mimics the syntax from
7575+ Java. It may seem counter-intuitive to have the "base class" as the second
7676+ argument, but it's nice this way if several uses of `extends` are cascaded.
7777+7878+ To get a better understanding how `extends` turns a function with a fix
7979+ point (the package set we start with) into a new function with a different fix
8080+ point (the desired packages set) lets just see, how `extends g f`
8181+ unfolds with `g` and `f` defined above:
8282+8383+ ```
8484+ extends g f = self: let super = f self; in super // g self super;
8585+ = self: let super = { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }; in super // g self super
8686+ = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // g self { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; }
8787+ = self: { foo = "foo"; bar = "bar"; foobar = self.foo + self.bar; } // { foo = "foo" + " + "; }
8888+ = self: { foo = "foo + "; bar = "bar"; foobar = self.foo + self.bar; }
8989+ ```
9090+ */
6991 extends = f: rattrs: self: let super = rattrs self; in super // f self super;
70927171- # Compose two extending functions of the type expected by 'extends'
7272- # into one where changes made in the first are available in the
7373- # 'super' of the second
9393+ /*
9494+ Compose two extending functions of the type expected by 'extends'
9595+ into one where changes made in the first are available in the
9696+ 'super' of the second
9797+ */
7498 composeExtensions =
7599 f: g: final: prev:
76100 let fApplied = f final prev;
77101 prev' = prev // fApplied;
78102 in fApplied // g final prev';
791038080- # Compose several extending functions of the type expected by 'extends' into
8181- # one where changes made in preceding functions are made available to
8282- # subsequent ones.
8383- #
8484- # composeManyExtensions : [packageSet -> packageSet -> packageSet] -> packageSet -> packageSet -> packageSet
8585- # ^final ^prev ^overrides ^final ^prev ^overrides
104104+ /*
105105+ Compose several extending functions of the type expected by 'extends' into
106106+ one where changes made in preceding functions are made available to
107107+ subsequent ones.
108108+109109+ ```
110110+ composeManyExtensions : [packageSet -> packageSet -> packageSet] -> packageSet -> packageSet -> packageSet
111111+ ^final ^prev ^overrides ^final ^prev ^overrides
112112+ ```
113113+ */
86114 composeManyExtensions =
87115 lib.foldr (x: y: composeExtensions x y) (final: prev: {});
881168989- # Create an overridable, recursive attribute set. For example:
9090- #
9191- # nix-repl> obj = makeExtensible (self: { })
9292- #
9393- # nix-repl> obj
9494- # { __unfix__ = «lambda»; extend = «lambda»; }
9595- #
9696- # nix-repl> obj = obj.extend (self: super: { foo = "foo"; })
9797- #
9898- # nix-repl> obj
9999- # { __unfix__ = «lambda»; extend = «lambda»; foo = "foo"; }
100100- #
101101- # nix-repl> obj = obj.extend (self: super: { foo = super.foo + " + "; bar = "bar"; foobar = self.foo + self.bar; })
102102- #
103103- # nix-repl> obj
104104- # { __unfix__ = «lambda»; bar = "bar"; extend = «lambda»; foo = "foo + "; foobar = "foo + bar"; }
117117+ /*
118118+ Create an overridable, recursive attribute set. For example:
119119+120120+ ```
121121+ nix-repl> obj = makeExtensible (self: { })
122122+123123+ nix-repl> obj
124124+ { __unfix__ = «lambda»; extend = «lambda»; }
125125+126126+ nix-repl> obj = obj.extend (self: super: { foo = "foo"; })
127127+128128+ nix-repl> obj
129129+ { __unfix__ = «lambda»; extend = «lambda»; foo = "foo"; }
130130+131131+ nix-repl> obj = obj.extend (self: super: { foo = super.foo + " + "; bar = "bar"; foobar = self.foo + self.bar; })
132132+133133+ nix-repl> obj
134134+ { __unfix__ = «lambda»; bar = "bar"; extend = «lambda»; foo = "foo + "; foobar = "foo + bar"; }
135135+ ```
136136+ */
105137 makeExtensible = makeExtensibleWithCustomName "extend";
106138107107- # Same as `makeExtensible` but the name of the extending attribute is
108108- # customized.
139139+ /*
140140+ Same as `makeExtensible` but the name of the extending attribute is
141141+ customized.
142142+ */
109143 makeExtensibleWithCustomName = extenderName: rattrs:
110144 fix' (self: (rattrs self) // {
111145 ${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs);