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: { mix.newMixture inputs (mixture: {
includes = { includes = {
public = [ public = [
./nt.nix ./nt
]; ];
protected = [ protected = [
./bootstrap ./bootstrap

View file

@ -16,6 +16,7 @@
inherit inherit
(this.std) (this.std)
flip flip
recdef
removeAttrsRec removeAttrsRec
; ;
@ -34,11 +35,6 @@
Terminal Terminal
; ;
recdef = def: let
Self = def Self;
in
Self;
classDecl = { classDecl = {
derive = Terminal []; derive = Terminal [];
ops = 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) (builtins)
attrNames attrNames
elem elem
getAttr
isString
typeOf typeOf
; ;
inherit
(this)
toTypeSig
;
inherit inherit
(this.std) (this.std)
contains contains
@ -25,8 +28,14 @@
openTrapdoor openTrapdoor
; ;
in rec { in rec {
ntTrapdoorKey = mkTrapdoorKey "nt";
ntDynamicTrapdoorKey = mkTrapdoorKey "ntDyn";
openNT = openTrapdoor ntTrapdoorKey; openNT = openTrapdoor ntTrapdoorKey;
# check if a value is NOT NixTypes compatible
isPrimitive = ! isNT;
# check if a value is NixTypes compatible # check if a value is NixTypes compatible
isNT = T: isNT = T:
openNT T openNT T
@ -54,8 +63,19 @@ in rec {
&& content.instance == true) && content.instance == true)
|> isSome; |> isSome;
# XXX: TODO: Implement isomorphisms between types especially enfIsPrimitive = type: value: msg: let
# XXX: TODO: implicit isomorphism from nix primitives to NT types. 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: impls = type: T:
assert enfIsNT T "nt.impls"; assert enfIsNT T "nt.impls";
@ -68,29 +88,4 @@ in rec {
openNT T openNT T
|> bindMaybe (content: content.sig == toTypeSig type) |> bindMaybe (content: content.sig == toTypeSig type)
|> isSome; |> 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 {this, ...}: let
inherit inherit
(builtins) (builtins)
getAttr
isString
split split
stringLength stringLength
typeOf typeOf
; ;
inherit
(this)
enfIsNT
openNT
;
inherit
(this.maybe)
bindMaybe
isSome
;
inherit inherit
(this.std) (this.std)
filterEven filterEven
@ -52,6 +66,17 @@ in rec {
then init result ++ (last result |> stringTail) then init result ++ (last result |> stringTail)
else null; 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'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) # NOTE: we measure similarity in the reverse order (ie end of signature is most important)
# sigSimilarity = type: list: let # sigSimilarity = type: list: let