create primitives/nt directory

This commit is contained in:
Emile Clark-Boman 2026-01-28 12:46:34 +10:00
parent d358ab0d2d
commit d3d16cda76
5 changed files with 62 additions and 35 deletions

View file

@ -2,7 +2,7 @@
mix.newMixture inputs (mixture: {
includes = {
public = [
./nt.nix
./nt
];
protected = [
./bootstrap

View file

@ -16,6 +16,7 @@
inherit
(this.std)
flip
recdef
removeAttrsRec
;
@ -34,11 +35,6 @@
Terminal
;
recdef = def: let
Self = def Self;
in
Self;
classDecl = {
derive = Terminal [];
ops = Terminal {};

View file

@ -0,0 +1,11 @@
{mix, ...} @ inputs:
mix.newMixture inputs (mixture: {
includes = {
public = [
./nt.nix
];
protected = [
./bootstrap
];
};
})

View file

@ -3,11 +3,14 @@
(builtins)
attrNames
elem
getAttr
isString
typeOf
;
inherit
(this)
toTypeSig
;
inherit
(this.std)
contains
@ -25,8 +28,14 @@
openTrapdoor
;
in rec {
ntTrapdoorKey = mkTrapdoorKey "nt";
ntDynamicTrapdoorKey = mkTrapdoorKey "ntDyn";
openNT = openTrapdoor ntTrapdoorKey;
# check if a value is NOT NixTypes compatible
isPrimitive = ! isNT;
# check if a value is NixTypes compatible
isNT = T:
openNT T
@ -54,8 +63,19 @@ in rec {
&& content.instance == true)
|> isSome;
# XXX: TODO: Implement isomorphisms between types especially
# XXX: TODO: implicit isomorphism from nix primitives to NT types.
enfIsPrimitive = type: value: msg: let
got = typeOf value;
in
got == type || throw "${msg}: expected primitive nix type \"${type}\" but got \"${got}\"";
enfIsNT = T: msg:
isNT T || throw "${msg}: expected nt compatible type but got \"${toString T}\" of primitive nix type \"${typeOf T}\"";
enfImpls = type: T: msg:
impls type T || throw "${msg}: given type \"${toTypeSig T}\" does not implement typeclass \"${toTypeSig type}\"";
# XXX: TODO: Implement isomorphisms between types, especially
# XXX: TODO: implicit isomorphisms from nix primitives to NT types.
impls = type: T:
assert enfIsNT T "nt.impls";
@ -68,29 +88,4 @@ in rec {
openNT T
|> bindMaybe (content: content.sig == toTypeSig type)
|> isSome;
typeSig = T:
assert enfIsNT T "nt.typeSig";
openNT T
|> bindMaybe (getAttr "sig")
|> isSome;
toTypeSig = x:
if isString x
then x
else typeSig x;
ntTrapdoorKey = mkTrapdoorKey "nt";
ntDynamicTrapdoorKey = mkTrapdoorKey "ntDyn";
enfIsPrimitive = type: value: msg: let
got = typeOf value;
in
got == type || throw "${msg}: expected primitive nix type \"${type}\" but got \"${got}\"";
enfIsNT = T: msg:
isNT T || throw "${msg}: expected nt compatible type but got \"${toString T}\" of primitive nix type \"${typeOf T}\"";
enfImpls = type: T: msg:
impls type T || throw "${msg}: given type \"${toTypeSig T}\" does not implement typeclass \"${toTypeSig type}\"";
}

View file

@ -1,11 +1,25 @@
{this, ...}: let
inherit
(builtins)
getAttr
isString
split
stringLength
typeOf
;
inherit
(this)
enfIsNT
openNT
;
inherit
(this.maybe)
bindMaybe
isSome
;
inherit
(this.std)
filterEven
@ -52,6 +66,17 @@ in rec {
then init result ++ (last result |> stringTail)
else null;
typeSig = T:
assert enfIsNT T "nt.typeSig";
openNT T
|> bindMaybe (getAttr "sig")
|> isSome;
toTypeSig = x:
if isString x
then x
else typeSig x;
# NOTE: we're testing how similar `list` is to `toTypeSig type` (non-commutative)
# NOTE: we measure similarity in the reverse order (ie end of signature is most important)
# sigSimilarity = type: list: let