move nt specifics from trapdoor.nix/sig.nix -> nt.nix

This commit is contained in:
Emile Clark-Boman 2026-01-25 11:43:50 +10:00
parent 9804d2e95a
commit 25d00f7a2f
2 changed files with 28 additions and 4 deletions

View file

@ -5,14 +5,14 @@
attrNames attrNames
elem elem
isAttrs isAttrs
isString
typeOf
; ;
inherit inherit
(this) (this)
enfIsNT mkTrapdoorKey
ntTrapdoorKey
openTrapdoor openTrapdoor
toTypeSig
; ;
in rec { in rec {
# check if a value is an nt type/class # check if a value is an nt type/class
@ -56,4 +56,29 @@ in rec {
# NOTE safe variant, use `is'` if you can guarantee `isNT T` holds # NOTE safe variant, use `is'` if you can guarantee `isNT T` holds
is = type: T: assert enfIsNT T "nt.is"; is' type T; is = type: T: assert enfIsNT T "nt.is"; is' type T;
# 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;
# XXX: TODO: move ntTrapdoorKey to nt.nix
ntTrapdoorKey = mkTrapdoorKey "nt";
enfIsType = 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

@ -16,7 +16,6 @@ in rec {
masterkey = "_''traps''_"; masterkey = "_''traps''_";
defaultTrapdoorKey = "_'"; defaultTrapdoorKey = "_'";
mkTrapdoorKey = id: "${defaultTrapdoorKey}${id}"; mkTrapdoorKey = id: "${defaultTrapdoorKey}${id}";
ntTrapdoorKey = mkTrapdoorKey "nt";
mkTrapdoorFn = key: decl: mkTrapdoorFn = key: decl:
assert enfHasAttr "default" decl "mkTrapdoorFn"; assert enfHasAttr "default" decl "mkTrapdoorFn";