feat: Implement support for typing functions
The defun helper takes a type signature and a function and makes a typed version. Because we can.
This commit is contained in:
parent
1da22249bd
commit
5949663fcd
3 changed files with 20 additions and 1 deletions
|
@ -45,4 +45,10 @@ deepSeq rec {
|
|||
blue = throw "It should not be blue!";
|
||||
green = throw "It should not be green!";
|
||||
};
|
||||
|
||||
# Test curried function definitions
|
||||
func = defun [ string int string ]
|
||||
(name: age: "${name} is ${toString age} years old");
|
||||
|
||||
testFunc = func "Brynhjulf" 42;
|
||||
} "All tests passed!\n"
|
||||
|
|
15
yants.nix
15
yants.nix
|
@ -74,6 +74,19 @@ with builtins; let
|
|||
else actions."${__functor { inherit name check; } x}";
|
||||
};
|
||||
|
||||
mkFunc = sig: f: {
|
||||
inherit sig;
|
||||
__toString = self: foldl' (s: t: "${s} -> ${t.name}")
|
||||
"λ :: ${(head self.sig).name}" (tail self.sig);
|
||||
__functor = _: f;
|
||||
};
|
||||
defun' = sig: func: if length sig > 2
|
||||
then mkFunc sig (x: defun' (tail sig) (func ((head sig) x)))
|
||||
else mkFunc sig (x: ((head (tail sig)) (func ((head sig) x))));
|
||||
|
||||
defun = sig: func: if length sig < 2
|
||||
then (throw "Signature must at least have two types (a -> b)")
|
||||
else defun' sig func;
|
||||
in (typeSet [
|
||||
# Primitive types
|
||||
(typedef "any" (_: true))
|
||||
|
@ -98,4 +111,4 @@ in (typeSet [
|
|||
)) true (attrValues v))))
|
||||
|
||||
(poly2 "either" (t1: t2: v: t1.check v || t2.check v))
|
||||
]) // { inherit struct enum; }
|
||||
]) // { inherit struct enum defun; }
|
||||
|
|
BIN
z-functions.png
Normal file
BIN
z-functions.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 32 KiB |
Loading…
Reference in a new issue