-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1502ee
commit 748fe29
Showing
2 changed files
with
54 additions
and
0 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
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,53 @@ | ||
{-# LANGUAGE DeriveDataTypeable #-} | ||
{-# LANGUAGE DeriveFoldable #-} | ||
{-# LANGUAGE DeriveFunctor #-} | ||
{-# LANGUAGE DeriveTraversable #-} | ||
{-# LANGUAGE MagicHash #-} | ||
{-# OPTIONS_GHC -fno-warn-unused-imports #-} | ||
|
||
module Data.Monoid.Set where | ||
|
||
import Data.Data | ||
import Data.Semigroup | ||
import Data.Foldable | ||
import Data.Traversable | ||
|
||
import GHC.Exts (isTrue#, dataToTag#) | ||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
---- | ||
|
||
-- | @Set@ is like @Maybe@, but the value can either be | ||
-- unspecified with @Unset@, or explicitly cleared with @Clear@ | ||
data Set a | ||
= Unset | ||
| Set a | ||
| Clear | ||
deriving (Data, Typeable, Show, Read, Functor, Foldable, Traversable) | ||
|
||
|
||
-- | The right-hand-side or "newer" value is prefered, unless it | ||
-- is Unset, in which case the old value is left unchanged | ||
instance Semigroup (Set a) where | ||
|
||
l <> Unset = l | ||
_ <> r = r | ||
|
||
stimes n s | ||
| n < 1 = Unset | ||
| let = s | ||
|
||
instance Monoid (Set a) where | ||
mempty = Unset | ||
|
||
|
||
isSet :: Set a -> Bool | ||
isSet s = isTrue# (dataToTag# s) | ||
|
||
maybeToSet :: Maybe a -> Set a | ||
maybeToSet = unsafeCoerce | ||
|
||
setToMaybe :: Set a -> Maybe a | ||
setToMaybe (Set a) = Just a | ||
setToMaybe _ = Nothing | ||
|