summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorqfpl <>2018-12-02 03:20:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-12-02 03:20:00 (GMT)
commitf63b5b0e48c7acef4089b29b12f6c0b745279246 (patch)
tree80f24c631d007765107add0949738dcdc8e52390
parentc3da37c8dbfbde12e7696057efb50456fbca63e2 (diff)
version 0.3.0.30.3.0.3
-rw-r--r--changelog.md4
-rw-r--r--natural.cabal10
-rw-r--r--src/Natural.hs82
3 files changed, 89 insertions, 7 deletions
diff --git a/changelog.md b/changelog.md
index ff167ed..617cc1a 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,3 +1,7 @@
+0.3.0.3
+
+* added some more functions.
+
0.3.0.2
* fix bug in `zero` and `one` prisms.
diff --git a/natural.cabal b/natural.cabal
index 89c318b..e877218 100644
--- a/natural.cabal
+++ b/natural.cabal
@@ -1,7 +1,7 @@
-- documentation, see http://haskell.org/cabal/users-guide/
name: natural
-version: 0.3.0.2
+version: 0.3.0.3
synopsis: Natural number
description:
<<http://i.imgur.com/uZnp9ke.png>>
@@ -18,7 +18,7 @@ extra-source-files: changelog.md
cabal-version: >=1.10
homepage: https://github.com/qfpl/natural
bug-reports: https://github.com/qfpl/natural/issues
-tested-with: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.3
+tested-with: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.3, GHC == 8.6.1
source-repository head
type: git
@@ -26,7 +26,7 @@ source-repository head
library
exposed-modules: Natural
- build-depends: base >=4.8 && <4.12
+ build-depends: base >=4.8 && <4.13
, lens >=4.15 && < 4.18
, semigroupoids >= 5 && < 6
hs-source-dirs: src
@@ -36,8 +36,8 @@ library
build-depends: semigroups >= 0.9 && < 0.19
test-suite tests
- build-depends: QuickCheck >=2.9.2 && <2.12
- , base >=4.8 && <4.12
+ build-depends: QuickCheck >=2.9.2 && <2.13
+ , base >=4.8 && <4.13
, checkers >=0.4.6 && <0.5
, natural
, hedgehog >=0.5 && <0.7
diff --git a/src/Natural.hs b/src/Natural.hs
index 6ba12a8..1337199 100644
--- a/src/Natural.hs
+++ b/src/Natural.hs
@@ -14,6 +14,10 @@ module Natural (
, zero'
, successor
, successor'
+, plus
+, multiply
+, square
+, zeroOr
, length
, replicate
, take
@@ -37,6 +41,10 @@ module Natural (
, successor1
, successor1'
, successorW
+, plus1
+, multiply1
+, square1
+, oneOr
, notZero
, length1
, replicate1
@@ -50,6 +58,8 @@ module Natural (
, elemIndex1
, minus1
, list1
+, plusone
+, minusone
) where
import Control.Applicative(Const)
@@ -65,14 +75,14 @@ import Data.Int(Int)
import Data.List(iterate, zip, filter, map, repeat)
import Data.List.NonEmpty(NonEmpty((:|)))
import qualified Data.List.NonEmpty as NonEmpty(iterate, zip, filter)
-import Data.Maybe(listToMaybe, Maybe(Just, Nothing))
+import Data.Maybe(listToMaybe, Maybe(Just, Nothing), fromMaybe)
import Data.Monoid(Monoid(mappend, mempty))
import Data.Ord(Ord((<)), min, max)
import Data.Semigroup(Semigroup((<>)))
import Data.Semigroup.Foldable(Foldable1(foldMap1))
import Data.Tuple(fst, snd)
import Data.Word(Word)
-import Prelude(Show, Integral, Integer, (-), (+), (*), fromIntegral)
+import Prelude(Show, Integral, Integer, (-), (+), (*), (^), fromIntegral)
newtype Natural =
Natural
@@ -255,6 +265,34 @@ successor' ::
successor' =
(successor #)
+plus ::
+ Natural
+ -> Natural
+ -> Natural
+plus =
+ (<>)
+
+multiply ::
+ Natural
+ -> Natural
+ -> Natural
+multiply x y =
+ (_Wrapped # x <> (_Wrapped # y :: ProductNatural)) ^. _Wrapped
+
+square ::
+ Natural
+ -> Natural
+ -> Natural
+square (Natural x) (Natural y) =
+ Natural (x ^ y)
+
+zeroOr ::
+ AsNatural a =>
+ a
+ -> Natural
+zeroOr n =
+ fromMaybe zero' (n ^? _Natural)
+
length ::
Foldable f =>
f a
@@ -543,6 +581,34 @@ successorW =
(\(Natural n) -> Positive (n + 1))
(\(Positive n) -> Natural (n - 1))
+plus1 ::
+ Positive
+ -> Positive
+ -> Positive
+plus1 x y =
+ (_Wrapped # x <> (_Wrapped # y :: SumPositive)) ^. _Wrapped
+
+multiply1 ::
+ Positive
+ -> Positive
+ -> Positive
+multiply1 =
+ (<>)
+
+square1 ::
+ Positive
+ -> Positive
+ -> Positive
+square1 (Positive x) (Positive y) =
+ Positive (x ^ y)
+
+oneOr ::
+ AsPositive a =>
+ a
+ -> Positive
+oneOr n =
+ fromMaybe one' (n ^? _Positive)
+
notZero ::
Prism'
Natural
@@ -639,3 +705,15 @@ list1 =
iso
(\n -> replicate1 n ())
length1
+
+plusone ::
+ Natural
+ -> Positive
+plusone =
+ (^. successorW)
+
+minusone ::
+ Positive
+ -> Natural
+minusone =
+ (successorW #)