add primitive Maybe type
This commit is contained in:
parent
0806887889
commit
af671aaa65
4 changed files with 81 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ let
|
||||||
// import ./parse.nix input
|
// import ./parse.nix input
|
||||||
// import ./trapdoor.nix input
|
// import ./trapdoor.nix input
|
||||||
// import ./null.nix input
|
// import ./null.nix input
|
||||||
|
// import ./maybe.nix input
|
||||||
// import ./wrap.nix input
|
// import ./wrap.nix input
|
||||||
// import ./enforce.nix input
|
// import ./enforce.nix input
|
||||||
// import ./sig.nix input
|
// import ./sig.nix input
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ mix.newMixture inputs (mixture: {
|
||||||
./enforce.nix
|
./enforce.nix
|
||||||
./nt.nix
|
./nt.nix
|
||||||
./null.nix
|
./null.nix
|
||||||
|
./maybe.nix
|
||||||
./parse.nix
|
./parse.nix
|
||||||
./sig.nix
|
./sig.nix
|
||||||
./trapdoor.nix
|
./trapdoor.nix
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,11 @@
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
(this)
|
(this)
|
||||||
|
impls
|
||||||
isClassSig
|
isClassSig
|
||||||
isNT
|
isNT
|
||||||
isTypeSig
|
isTypeSig
|
||||||
|
toTypeSig
|
||||||
;
|
;
|
||||||
in rec {
|
in rec {
|
||||||
enfIsType = type: value: msg: let
|
enfIsType = type: value: msg: let
|
||||||
|
|
@ -33,4 +35,9 @@ in rec {
|
||||||
|
|
||||||
enfIsNT = T: msg:
|
enfIsNT = T: msg:
|
||||||
isNT T || throw "${msg}: expected nt compatible type but got \"${toString T}\" of primitive nix type \"${typeOf T}\"";
|
isNT T || throw "${msg}: expected nt compatible type but got \"${toString T}\" of primitive nix type \"${typeOf T}\"";
|
||||||
|
|
||||||
|
# assert enfImpls "nt::&Maybe" T "nt::&Maybe.unwrap";
|
||||||
|
# impls = type: T: assert enfIsNT T "nt.impls"; impls' type T;
|
||||||
|
enfImpls = type: T: msg:
|
||||||
|
impls type T || throw "${msg}: given type \"${toTypeSig T}\" does not implement typeclass \"${toTypeSig type}\"";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
72
nt/primitives/util/maybe.nix
Normal file
72
nt/primitives/util/maybe.nix
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
{this, ...}: let
|
||||||
|
inherit
|
||||||
|
(this)
|
||||||
|
enfImpls
|
||||||
|
mkTrapdoorFn
|
||||||
|
mkTrapdoorSet
|
||||||
|
ntTrapdoorKey
|
||||||
|
ntDynamicTrapdoorKey
|
||||||
|
openTrapdoor
|
||||||
|
;
|
||||||
|
in {
|
||||||
|
# NOTE: Maybe is used to simplify parsing Type/Class declarations
|
||||||
|
# NOTE: and therefore must be implemented manually
|
||||||
|
Maybe = let
|
||||||
|
meta = instance: {
|
||||||
|
sig = "nt::&Maybe";
|
||||||
|
derive = [];
|
||||||
|
ops = {};
|
||||||
|
req = {"nt::&Maybe" = ["unwrap"];};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
mkTrapdoorFn {
|
||||||
|
default = {
|
||||||
|
unwrap = T:
|
||||||
|
assert enfImpls "nt::&Maybe" T "nt::&Maybe.unwrap";
|
||||||
|
(T |> openTrapdoor ntTrapdoorKey).ops."nt::&Maybe".unwrap;
|
||||||
|
};
|
||||||
|
unlock.${ntTrapdoorKey} = meta false;
|
||||||
|
};
|
||||||
|
|
||||||
|
Some = let
|
||||||
|
meta = instance: {
|
||||||
|
inherit instance;
|
||||||
|
sig = "nt::Some";
|
||||||
|
derive = ["nt::&Maybe"];
|
||||||
|
ops = {
|
||||||
|
"nt::&Maybe".unwrap = f: self: f self.${ntDynamicTrapdoorKey}.value;
|
||||||
|
};
|
||||||
|
req = {};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
mkTrapdoorFn {
|
||||||
|
default = value:
|
||||||
|
mkTrapdoorSet {
|
||||||
|
unlock = {
|
||||||
|
${ntTrapdoorKey} = meta true;
|
||||||
|
${ntDynamicTrapdoorKey} = {
|
||||||
|
inherit value;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
unlock.${ntTrapdoorKey} = meta false;
|
||||||
|
};
|
||||||
|
|
||||||
|
None = let
|
||||||
|
meta = instance: {
|
||||||
|
inherit instance;
|
||||||
|
sig = "nt::None";
|
||||||
|
derive = ["nt::&Maybe"];
|
||||||
|
ops = {
|
||||||
|
"nt::&Maybe".map = f: self: self;
|
||||||
|
};
|
||||||
|
req = {};
|
||||||
|
};
|
||||||
|
in
|
||||||
|
mkTrapdoorFn {
|
||||||
|
default = mkTrapdoorSet ntTrapdoorKey {
|
||||||
|
unlock = meta true;
|
||||||
|
};
|
||||||
|
unlock.${ntTrapdoorKey} = meta false;
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue