about summary refs log tree commit diff
path: root/users/sterni/nix/int
diff options
context:
space:
mode:
authorsterni <sternenseemann@systemli.org>2023-08-05T15·47+0200
committersterni <sternenseemann@systemli.org>2023-08-09T12·12+0000
commit81fc87e29ebee143aa27ff6167556db76a803d12 (patch)
tree79dc8e43c0c37392ce99cef8ce960dac163363f4 /users/sterni/nix/int
parent55a3b3eb81a1c37b0c5345e68c03520c85564e3d (diff)
refactor(nix/sterni/int): rename div & mod to quot & rem r/6479
This mirrors the terminology Haskell uses: quot and rem truncate towards
zero which is also the case for builtins.div. We can give us the option
to introduce Haskell-style int.div and int.mod (to complete the
confusion) which would truncate towards negative infinity.

Change-Id: Ibebb0a01a73c9718cd62121b2fc2a19c3a4be0de
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9009
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'users/sterni/nix/int')
-rw-r--r--users/sterni/nix/int/default.nix15
-rw-r--r--users/sterni/nix/int/tests/default.nix36
2 files changed, 28 insertions, 23 deletions
diff --git a/users/sterni/nix/int/default.nix b/users/sterni/nix/int/default.nix
index e1d7e1fe7e4f..870744522361 100644
--- a/users/sterni/nix/int/default.nix
+++ b/users/sterni/nix/int/default.nix
@@ -23,7 +23,7 @@ let
   bitShiftR = bit: count:
     if count == 0
     then bit
-    else div (bitShiftR bit (count - 1)) 2;
+    else (bitShiftR bit (count - 1)) / 2;
 
   bitShiftL = bit: count:
     if count == 0
@@ -85,8 +85,13 @@ let
   odd = x: bitAnd x 1 == 1;
   even = x: bitAnd x 1 == 0;
 
-  inherit (builtins) div;
-  mod = a: b: let res = a / b; in a - (res * b);
+  quot' = builtins.div; # no typecheck
+  rem = a: b:
+    assert builtins.isInt a && builtins.isInt b;
+    let res = quot' a b; in a - (res * b);
+  quot = a: b:
+    assert builtins.isInt a && builtins.isInt b;
+    quot' a b;
 
 in
 {
@@ -96,8 +101,8 @@ in
     exp
     odd
     even
-    div
-    mod
+    quot
+    rem
     bitShiftR
     bitShiftL
     bitOr
diff --git a/users/sterni/nix/int/tests/default.nix b/users/sterni/nix/int/tests/default.nix
index bc694ac90a02..80bd05b6b5eb 100644
--- a/users/sterni/nix/int/tests/default.nix
+++ b/users/sterni/nix/int/tests/default.nix
@@ -365,7 +365,7 @@ let
   checkShiftRDivExp = n:
     assertEq "${toString n} >> 5 == ${toString n} / 2 ^ 5"
       (int.bitShiftR n 5)
-      (int.div n (int.exp 2 5));
+      (n / (int.exp 2 5));
 
   checkShiftLMulExp = n:
     assertEq "${toString n} >> 6 == ${toString n} * 2 ^ 6"
@@ -406,40 +406,40 @@ let
   ]);
 
   divisions = [
-    { a = 2; b = 1; c = 2; mod = 0; }
-    { a = 2; b = 2; c = 1; mod = 0; }
-    { a = 20; b = 10; c = 2; mod = 0; }
-    { a = 12; b = 5; c = 2; mod = 2; }
-    { a = 23; b = 4; c = 5; mod = 3; }
+    { a = 2; b = 1; c = 2; rem = 0; }
+    { a = 2; b = 2; c = 1; rem = 0; }
+    { a = 20; b = 10; c = 2; rem = 0; }
+    { a = 12; b = 5; c = 2; rem = 2; }
+    { a = 23; b = 4; c = 5; rem = 3; }
   ];
 
-  checkDiv = n: { a, b, c, mod }: [
-    (assertEq "${n}: div result" (int.div a b) c)
-    (assertEq "${n}: mod result" (int.mod a b) mod)
-    (assertEq "${n}: divMod law" ((int.div a b) * b + (int.mod a b)) a)
+  checkQuot = n: { a, b, c, rem }: [
+    (assertEq "${n}: quot result" (int.quot a b) c)
+    (assertEq "${n}: rem result" (int.rem a b) rem)
+    (assertEq "${n}: quotRem law" ((int.quot a b) * b + (int.rem a b)) a)
   ];
 
-  testDivMod = it "checks integer division and modulo"
+  testQuotRem = it "checks integer quotient and remainder"
     (lib.flatten [
-      (builtins.map (checkDiv "+a / +b") divisions)
+      (builtins.map (checkQuot "+a / +b") divisions)
       (builtins.map
-        (fun.rl (checkDiv "-a / +b") (x: x // {
+        (fun.rl (checkQuot "-a / +b") (x: x // {
           a = -x.a;
           c = -x.c;
-          mod = -x.mod;
+          rem = -x.rem;
         }))
         divisions)
       (builtins.map
-        (fun.rl (checkDiv "+a / -b") (x: x // {
+        (fun.rl (checkQuot "+a / -b") (x: x // {
           b = -x.b;
           c = -x.c;
         }))
         divisions)
       (builtins.map
-        (fun.rl (checkDiv "-a / -b") (x: x // {
+        (fun.rl (checkQuot "-a / -b") (x: x // {
           a = -x.a;
           b = -x.b;
-          mod = -x.mod;
+          rem = -x.rem;
         }))
         divisions)
     ]);
@@ -451,5 +451,5 @@ runTestsuite "nix.int" [
   testBasic
   testExp
   testBit
-  testDivMod
+  testQuotRem
 ]