From d3d16cda76d2d0ba5d0dfda1cf339bfb1cf7a794 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Wed, 28 Jan 2026 12:46:34 +1000 Subject: [PATCH] create primitives/nt directory --- nt/primitives/default.nix | 2 +- nt/primitives/{nt.nix => nt/class.nix} | 6 +-- nt/primitives/nt/default.nix | 11 ++++ nt/primitives/{bootstrap => nt}/nt.nix | 53 +++++++++---------- nt/primitives/{bootstrap/parse => nt}/sig.nix | 25 +++++++++ 5 files changed, 62 insertions(+), 35 deletions(-) rename nt/primitives/{nt.nix => nt/class.nix} (97%) create mode 100644 nt/primitives/nt/default.nix rename nt/primitives/{bootstrap => nt}/nt.nix (84%) rename nt/primitives/{bootstrap/parse => nt}/sig.nix (88%) diff --git a/nt/primitives/default.nix b/nt/primitives/default.nix index 43fcce7..b8f921d 100644 --- a/nt/primitives/default.nix +++ b/nt/primitives/default.nix @@ -2,7 +2,7 @@ mix.newMixture inputs (mixture: { includes = { public = [ - ./nt.nix + ./nt ]; protected = [ ./bootstrap diff --git a/nt/primitives/nt.nix b/nt/primitives/nt/class.nix similarity index 97% rename from nt/primitives/nt.nix rename to nt/primitives/nt/class.nix index 7f21268..ed8fab7 100644 --- a/nt/primitives/nt.nix +++ b/nt/primitives/nt/class.nix @@ -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 {}; diff --git a/nt/primitives/nt/default.nix b/nt/primitives/nt/default.nix new file mode 100644 index 0000000..43fcce7 --- /dev/null +++ b/nt/primitives/nt/default.nix @@ -0,0 +1,11 @@ +{mix, ...} @ inputs: +mix.newMixture inputs (mixture: { + includes = { + public = [ + ./nt.nix + ]; + protected = [ + ./bootstrap + ]; + }; +}) diff --git a/nt/primitives/bootstrap/nt.nix b/nt/primitives/nt/nt.nix similarity index 84% rename from nt/primitives/bootstrap/nt.nix rename to nt/primitives/nt/nt.nix index ed795c5..9340aae 100644 --- a/nt/primitives/bootstrap/nt.nix +++ b/nt/primitives/nt/nt.nix @@ -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}\""; } diff --git a/nt/primitives/bootstrap/parse/sig.nix b/nt/primitives/nt/sig.nix similarity index 88% rename from nt/primitives/bootstrap/parse/sig.nix rename to nt/primitives/nt/sig.nix index ffbfa17..084ada7 100644 --- a/nt/primitives/bootstrap/parse/sig.nix +++ b/nt/primitives/nt/sig.nix @@ -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