bleeding: typesystem

This commit is contained in:
Emile Clark-Boman 2025-12-18 11:26:47 +10:00
parent 452dcf99bb
commit 3d9ec28bfc
6 changed files with 88 additions and 4 deletions

View file

@ -4,9 +4,13 @@
mkMod = mkMod' {inherit systems nib;};
std = mkMod ./std;
types = mkMod ./types;
parse = mkMod ./parse;
panic = mkMod ./panic.nix;
parse = mkMod ./parse;
patterns = mkMod ./patterns.nix;
types = mkMod ./types;
typesystem = mkMod ./typesystem.nix;
sys = mkMod ./sys.nix;
nib = std.mergeAttrsList [
@ -15,6 +19,8 @@
{inherit std types panic parse;}
# submodule content accessible directly (ie self.myFunc)
patterns
typesystem
sys
];
in

14
nib/enforce.nix Normal file
View file

@ -0,0 +1,14 @@
{nib, ...}: rec {
enfType = type: T:
assert (nib.isType type T
|| nib.panic.badType (nib.typeName type)); true;
enfSameType = T1: T2: enfType (nib.typeOf T1) T2;
enfAttrs = enfType (nib.typeOf {});
enfList = enfType (nib.typeOf []);
enfListOf = type: L:
assert (enfList L
&& builtins.all (T: nib.isType type T) L
|| nib.panic.badType "List ${nib.typeName type}" L); true;
}

View file

@ -1,4 +1,4 @@
{...}: {
{nib, ...}: {
badType = expect: x:
throw "Expected type ${expect} but got ${builtins.typeOf x}.";
throw "Expected type ${expect} but got ${nib.typeOf x}.";
}

47
nib/patterns.nix Normal file
View file

@ -0,0 +1,47 @@
{nib, ...}: let
# Rust inspired pattern matching syntax:
# resultA = match [platform arch] [
# (Pattern ["darwin" Any] darwin_package)
# (Pattern ["openbsd" "x86_64"] openbsd_x86_64_package)
# (Pattern [(x: x == "linux") (y: y == "x86_64")] linux_x86_64_package)
# (Pattern (x: y: x == "linux" && y == "aarch64") linux_aarch64_package)
# (Pattern Any default_package)
# ];
# resultB = match [platform arch] [
# (["darwin" Any] |> case darwin_package)
# (["openbsd" "x86_64"] |> case openbsd_x86_64_package)
# ([(x: x == "linux") (y: y == "x86_64")] |> case linux_x86_64_package)
# ((x: y: x == "linux" && y == "aarch64") |> case linux_aarch64_package)
# (Any |> case default_package)
# ];
types = nib.types;
in rec {
Pattern = pattern: return: throw "not implemented";
case = return: pattern: Pattern pattern return;
matchesPattern' = pattern: subject: let
recurse = p: s:
nib.isSameType p s
&& (
if nib.isList p
then builtins.all (map (p: recurse p)) (nib.std.zipLists)
else if nib.isAttrs p
then builtins.all ()
else nib.eq p s
);
in
recurse pattern subject;
# maybe' :: TList a b -> TList [TPattern c d] -> TMaybe d
match' = subject: patterns:
nib.enfType (types.TList types.TPattern) patterns
&& builtins.foldl' (
fix: p:
if types.isNone fix
# maintain None as a fixed value
then fix
else matchesPattern' p
)
types.Some
patterns;
}

View file

@ -10,4 +10,12 @@ in
maybe
res
terminal
rec {
# TODO
isAlgebraic = T: false;
isList = T: !isAlgebraic T && builtins.isList T;
isAttrs = T: !isAlgebraic T && builtins.isAttrs T;
}
]

9
nib/typesystem.nix Normal file
View file

@ -0,0 +1,9 @@
{...}: rec {
isType = type: T: type == typeOf T;
isSameType = T1: T2: typeOf T1 == typeOf T2;
# TODO
typeOf = builtins.typeOf;
# TODO
typeName = typeOf;
}