From b6324b652d58e402b869d15e90e309879b953037 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Sun, 25 Jan 2026 11:42:16 +1000 Subject: [PATCH] segregate parsing utilities --- nt/primitives/{util => parse}/parse.nix | 11 +++++---- nt/primitives/{util => parse}/sig.nix | 23 +++++++----------- nt/primitives/util/README.md | 1 - nt/primitives/util/default.nix | 7 ------ nt/primitives/util/enforce.nix | 32 ------------------------- 5 files changed, 15 insertions(+), 59 deletions(-) rename nt/primitives/{util => parse}/parse.nix (91%) rename nt/primitives/{util => parse}/sig.nix (85%) delete mode 100644 nt/primitives/util/README.md delete mode 100644 nt/primitives/util/enforce.nix diff --git a/nt/primitives/util/parse.nix b/nt/primitives/parse/parse.nix similarity index 91% rename from nt/primitives/util/parse.nix rename to nt/primitives/parse/parse.nix index 1eacd3f..f63eada 100644 --- a/nt/primitives/util/parse.nix +++ b/nt/primitives/parse/parse.nix @@ -9,16 +9,19 @@ inherit (this) - enfIsType is - Wrap + ; + + inherit + (this.std) + enfIsAttrs ; in rec { # form: getAttrAt :: list string -> set -> null | Wrap Any # given path as a list of strings, return that value of an # attribute set at that path getAttrAt = path: xs: - assert enfIsType "set" xs "getAttrAt"; + assert enfIsAttrs xs "getAttrAt"; foldl' (left: right: if left != null && isAttrs left.value && hasAttr right left.value then Wrap left.value.${right} @@ -30,7 +33,7 @@ in rec { # given path as a list of strings, return that value of an # attribute set at that path hasAttrAt = path: xs: - assert enfIsType "set" xs "hasAttrAt"; + assert enfIsAttrs xs "hasAttrAt"; getAttrAt path xs != null; # NOTE: inefficient (im lazy) # Alternative to mapAttrsRecursiveCond diff --git a/nt/primitives/util/sig.nix b/nt/primitives/parse/sig.nix similarity index 85% rename from nt/primitives/util/sig.nix rename to nt/primitives/parse/sig.nix index dedc59c..ae89bb2 100644 --- a/nt/primitives/util/sig.nix +++ b/nt/primitives/parse/sig.nix @@ -1,18 +1,16 @@ {this, ...}: let inherit (builtins) - isString split stringLength + typeOf ; inherit - (this) - enfIsNT + (this.std) filterEven init last - ntTrapdoorKey nullOr stringHead stringTail @@ -34,6 +32,12 @@ in rec { isClassSig = sig: parseSig sig |> nullOr (result: last result |> isClassSig); + enfIsClassSig = sig: msg: + isClassSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Typeclass signature"; + + enfIsTypeSig = sig: msg: + isTypeSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Type signature"; + parseTypeSig = sig: let result = parseSig sig; in @@ -48,17 +52,6 @@ in rec { then init result ++ (last result |> stringTail) else null; - # NOTE: unsafe variant, use typeSig if you can't guarantee `isNT T` holds - typeSig' = T: T.${ntTrapdoorKey}.sig; - - # NOTE: safe variant, use typeSig' if you can guarantee `isNT T` holds - typeSig = T: assert enfIsNT T "nt.typeSig"; typeSig' T; - - 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 diff --git a/nt/primitives/util/README.md b/nt/primitives/util/README.md deleted file mode 100644 index 8b13789..0000000 --- a/nt/primitives/util/README.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/nt/primitives/util/default.nix b/nt/primitives/util/default.nix index 844594d..36814cf 100644 --- a/nt/primitives/util/default.nix +++ b/nt/primitives/util/default.nix @@ -1,14 +1,7 @@ {mix, ...} @ inputs: mix.newMixture inputs (mixture: { includes.public = [ - ./enforce.nix ./nt.nix - ./null.nix - ./maybe.nix - ./parse.nix - ./sig.nix ./trapdoor.nix - ./util.nix - ./wrap.nix ]; }) diff --git a/nt/primitives/util/enforce.nix b/nt/primitives/util/enforce.nix deleted file mode 100644 index 3957634..0000000 --- a/nt/primitives/util/enforce.nix +++ /dev/null @@ -1,32 +0,0 @@ -{this, ...}: let - inherit - (builtins) - typeOf - ; - - inherit - (this) - impls - isClassSig - isNT - isTypeSig - toTypeSig - ; -in { - enfIsType = type: value: msg: let - got = typeOf value; - in - got == type || throw "${msg}: expected primitive nix type \"${type}\" but got \"${got}\""; - - enfIsClassSig = sig: msg: - isClassSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Typeclass signature"; - - enfIsTypeSig = sig: msg: - isTypeSig sig || throw "${msg}: given value \"${toString sig}\" of primitive nix type \"${typeOf sig}\" is not a valid Type signature"; - - 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}\""; -}