-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small improvements for Prelude.Natural (#719)
* Add Natural.subtract * Small improvements for the Natural comparison functions - Add property assertions - Only lessThanEqual uses the Natural/subtract primitive, everything else is ultimately implemented in terms of lessThanEqual. - greaterThanEqual now requires only 2 normalization steps instead of 3 * Update nixops/dhall-haskell.json Command used: nix-prefetch-git --fetch-submodules https://github.com/dhall-lang/dhall-haskell
- Loading branch information
Showing
9 changed files
with
69 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
{- | ||
`equal` checks if two Naturals are equal. | ||
-} | ||
let lessThanEqual = | ||
./lessThanEqual sha256:1a5caa2b80a42b9f58fff58e47ac0d9a9946d0b2d36c54034b8ddfe3cb0f3c99 | ||
? ./lessThanEqual | ||
|
||
let equal | ||
: Natural → Natural → Bool | ||
= λ(a : Natural) | ||
→ λ(b : Natural) | ||
→ Natural/isZero (Natural/subtract a b) | ||
&& Natural/isZero (Natural/subtract b a) | ||
= λ(a : Natural) → λ(b : Natural) → lessThanEqual a b && lessThanEqual b a | ||
|
||
let example0 = assert : equal 5 5 ≡ True | ||
|
||
let example1 = assert : equal 5 6 ≡ False | ||
|
||
let property0 = λ(n : Natural) → assert : equal n n ≡ True | ||
|
||
in equal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
{- | ||
`greaterThan` checks if one Natural is strictly greater than another. | ||
-} | ||
|
||
let Bool/not = | ||
../Bool/not sha256:723df402df24377d8a853afed08d9d69a0a6d86e2e5b2bac8960b0d4756c7dc4 | ||
? ../Bool/not | ||
|
||
let lessThanEqual = | ||
./lessThanEqual sha256:1a5caa2b80a42b9f58fff58e47ac0d9a9946d0b2d36c54034b8ddfe3cb0f3c99 | ||
? ./lessThanEqual | ||
let lessThan = | ||
./lessThan sha256:3381b66749290769badf8855d8a3f4af62e8de52d1364d838a9d1e20c94fa70c | ||
? ./lessThan | ||
|
||
let greaterThan | ||
: Natural → Natural → Bool | ||
= λ(x : Natural) → λ(y : Natural) → Bool/not (lessThanEqual x y) | ||
= λ(x : Natural) → λ(y : Natural) → lessThan y x | ||
|
||
let example0 = assert : greaterThan 5 6 ≡ False | ||
|
||
let example1 = assert : greaterThan 5 5 ≡ False | ||
|
||
let example2 = assert : greaterThan 5 4 ≡ True | ||
|
||
let property0 = λ(n : Natural) → assert : greaterThan 0 n ≡ False | ||
|
||
let property1 = λ(n : Natural) → assert : greaterThan n n ≡ False | ||
|
||
in greaterThan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
{- | ||
`greaterThanEqual` checks if one Natural is greater than or equal to another. | ||
-} | ||
let Bool/not = | ||
../Bool/not sha256:723df402df24377d8a853afed08d9d69a0a6d86e2e5b2bac8960b0d4756c7dc4 | ||
? ../Bool/not | ||
|
||
let lessThan = | ||
./lessThan sha256:d8a37951fd3f88dcc3927c20b73e6c53499f5275d949eed59bb77ea748492bf0 | ||
? ./lessThan | ||
let lessThanEqual = | ||
./lessThanEqual sha256:1a5caa2b80a42b9f58fff58e47ac0d9a9946d0b2d36c54034b8ddfe3cb0f3c99 | ||
? ./lessThanEqual | ||
|
||
let greaterThanEqual | ||
: Natural → Natural → Bool | ||
= λ(x : Natural) → λ(y : Natural) → Bool/not (lessThan x y) | ||
= λ(x : Natural) → λ(y : Natural) → lessThanEqual y x | ||
|
||
let example0 = assert : greaterThanEqual 5 6 ≡ False | ||
|
||
let example1 = assert : greaterThanEqual 5 5 ≡ True | ||
|
||
let example1 = assert : greaterThanEqual 5 4 ≡ True | ||
let example2 = assert : greaterThanEqual 5 4 ≡ True | ||
|
||
let property0 = λ(n : Natural) → assert : greaterThanEqual n 0 ≡ True | ||
|
||
let property1 = λ(n : Natural) → assert : greaterThanEqual n n ≡ True | ||
|
||
in greaterThanEqual |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,26 @@ | ||
{- | ||
`lessThan` checks if one Natural is strictly less than another. | ||
Examples: | ||
``` | ||
``` | ||
-} | ||
let lessThanEqual = | ||
./lessThanEqual sha256:1a5caa2b80a42b9f58fff58e47ac0d9a9946d0b2d36c54034b8ddfe3cb0f3c99 | ||
? ./lessThanEqual | ||
let greaterThanEqual = | ||
./greaterThanEqual sha256:30ebfab0febd7aa0ccccfdf3dc36ee6d50f0117f35dd4a9b034750b7e885a1a4 | ||
? ./greaterThanEqual | ||
|
||
let Bool/not = | ||
../Bool/not sha256:723df402df24377d8a853afed08d9d69a0a6d86e2e5b2bac8960b0d4756c7dc4 | ||
? ../Bool/not | ||
|
||
let lessThan | ||
: Natural → Natural → Bool | ||
= λ(x : Natural) → λ(y : Natural) → lessThanEqual (x + 1) y | ||
= λ(x : Natural) → λ(y : Natural) → Bool/not (greaterThanEqual x y) | ||
|
||
let example0 = assert : lessThan 5 6 ≡ True | ||
|
||
let example1 = assert : lessThan 5 5 ≡ False | ||
|
||
let example2 = assert : lessThan 5 4 ≡ False | ||
|
||
let property0 = λ(n : Natural) → assert : lessThan n 0 ≡ False | ||
|
||
let property1 = λ(n : Natural) → assert : lessThan n n ≡ False | ||
|
||
in lessThan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{- | ||
`subtract m n` computes `n - m`, truncating to `0` if `m > n` | ||
-} | ||
let subtract : Natural → Natural → Natural = Natural/subtract | ||
|
||
let example0 = assert : subtract 1 2 ≡ 1 | ||
|
||
let example1 = assert : subtract 1 1 ≡ 0 | ||
|
||
let example2 = assert : subtract 2 1 ≡ 0 | ||
|
||
let property0 = λ(n : Natural) → assert : subtract 0 n ≡ n | ||
|
||
let property1 = λ(n : Natural) → assert : subtract n 0 ≡ 0 | ||
|
||
let property2 = λ(n : Natural) → assert : subtract n n ≡ 0 | ||
|
||
in subtract |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"url": "https://github.com/dhall-lang/dhall-haskell.git", | ||
"rev": "2cf6a1ba74e02a3e16a8060cacba026c66e2ff23", | ||
"date": "2019-08-03T21:38:01-07:00", | ||
"sha256": "0ilp0dhfvbjzx8wh27lpijn53ghzjrq8f5cqpfgxiww3n8zliyc9", | ||
"url": "https://github.com/dhall-lang/dhall-haskell", | ||
"rev": "350b54c43ed0914200722e9ab78dfed854771a72", | ||
"date": "2019-09-01T18:09:28+00:00", | ||
"sha256": "0lr0q5jfqsx0j184s9xydzibcp370hikbim0m6dvj50xvf22a4gp", | ||
"fetchSubmodules": true | ||
} |