Skip to content

Commit f51ba34

Browse files
committed
More instances
1 parent b16556b commit f51ba34

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

docs/Data.Maybe.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@ instance extendMaybe :: Extend Maybe
2323
instance invariantFirst :: Invariant Maybe
2424
instance semigroupMaybe :: (Semigroup a) => Semigroup (Maybe a)
2525
instance monoidMaybe :: (Semigroup a) => Monoid (Maybe a)
26-
instance showMaybe :: (Show a) => Show (Maybe a)
26+
instance semiringMaybe :: (Semiring a) => Semiring (Maybe a)
27+
instance moduloSemiringMaybe :: (ModuloSemiring a) => ModuloSemiring (Maybe a)
28+
instance ringMaybe :: (Ring a) => Ring (Maybe a)
29+
instance divisionRingMaybe :: (DivisionRing a) => DivisionRing (Maybe a)
30+
instance numMaybe :: (Num a) => Num (Maybe a)
2731
instance eqMaybe :: (Eq a) => Eq (Maybe a)
2832
instance ordMaybe :: (Ord a) => Ord (Maybe a)
2933
instance boundedMaybe :: (Bounded a) => Bounded (Maybe a)
34+
instance boundedOrdMaybe :: (BoundedOrd a) => BoundedOrd (Maybe a)
35+
instance booleanAlgebraMaybe :: (BooleanAlgebra a) => BooleanAlgebra (Maybe a)
36+
instance showMaybe :: (Show a) => Show (Maybe a)
3037
```
3138

3239
The `Maybe` type is used to represent optional values and can be seen as

src/Data/Maybe.purs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,27 @@ instance invariantFirst :: Invariant Maybe where
204204
-- | Nothing <> Nothing = Nothing
205205
-- | ```
206206
instance semigroupMaybe :: (Semigroup a) => Semigroup (Maybe a) where
207-
append Nothing x = x
208-
append x Nothing = x
209-
append (Just x) (Just y) = Just (x <> y)
207+
append x y = append <$> x <*> y
210208

211209
instance monoidMaybe :: (Semigroup a) => Monoid (Maybe a) where
212210
mempty = Nothing
213211

214-
-- | The `Show` instance allows `Maybe` values to be rendered as a string with
215-
-- | `show` whenever there is an `Show` instance for the type the `Maybe`
216-
-- | contains.
217-
instance showMaybe :: (Show a) => Show (Maybe a) where
218-
show (Just x) = "Just (" ++ show x ++ ")"
219-
show Nothing = "Nothing"
212+
instance semiringMaybe :: (Semiring a) => Semiring (Maybe a) where
213+
add x y = add <$> x <*> y
214+
one = Just one
215+
mul x y = mul <$> x <*> y
216+
zero = Just zero
217+
218+
instance moduloSemiringMaybe :: (ModuloSemiring a) => ModuloSemiring (Maybe a) where
219+
mod x y = mod <$> x <*> y
220+
div x y = div <$> x <*> y
221+
222+
instance ringMaybe :: (Ring a) => Ring (Maybe a) where
223+
sub x y = sub <$> x <*> y
224+
225+
instance divisionRingMaybe :: (DivisionRing a) => DivisionRing (Maybe a)
226+
227+
instance numMaybe :: (Num a) => Num (Maybe a)
220228

221229
-- | The `Eq` instance allows `Maybe` values to be checked for equality with
222230
-- | `==` and inequality with `/=` whenever there is an `Eq` instance for the
@@ -240,3 +248,17 @@ instance ordMaybe :: (Ord a) => Ord (Maybe a) where
240248
instance boundedMaybe :: (Bounded a) => Bounded (Maybe a) where
241249
top = Just top
242250
bottom = Nothing
251+
252+
instance boundedOrdMaybe :: (BoundedOrd a) => BoundedOrd (Maybe a)
253+
254+
instance booleanAlgebraMaybe :: (BooleanAlgebra a) => BooleanAlgebra (Maybe a) where
255+
conj x y = conj <$> x <*> y
256+
disj x y = disj <$> x <*> y
257+
not = map not
258+
259+
-- | The `Show` instance allows `Maybe` values to be rendered as a string with
260+
-- | `show` whenever there is an `Show` instance for the type the `Maybe`
261+
-- | contains.
262+
instance showMaybe :: (Show a) => Show (Maybe a) where
263+
show (Just x) = "Just (" ++ show x ++ ")"
264+
show Nothing = "Nothing"

0 commit comments

Comments
 (0)