{-# LANGUAGE FlexibleInstances, Safe #-} {- This module is part of Chatty. Copyleft (c) 2014 Marvin Cohrs All wrongs reversed. Sharing is an act of love, not crime. Please share Chatty with everyone you like. Chatty is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Chatty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Chatty. If not, see <http://www.gnu.org/licenses/>. -} -- | Provides a typeclass for everything that may carry a void value module Data.Chatty.None where import Data.Maybe import Data.Text (pack, Text) -- | Typeclass for everything that may carry a void value class None n where none :: n instance None [a] where none :: [a] none = [] instance None (Maybe a) where none :: Maybe a none = Maybe a forall a. Maybe a Nothing instance None () where none :: () none = () instance None Text where none :: Text none = String -> Text pack String "" instance None (a -> a) where none :: a -> a none = a -> a forall a. a -> a id instance Monad m => None (a -> m a) where none :: a -> m a none = a -> m a forall (m :: * -> *) a. Monad m => a -> m a return instance None Int where none :: Int none = Int 0 instance None Integer where none :: Integer none = Integer 0 instance None Bool where none :: Bool none = Bool False -- | Wrap the void into a monad. noneM :: (Monad m,None n) => m n noneM :: m n noneM = n -> m n forall (m :: * -> *) a. Monad m => a -> m a return n forall n. None n => n none -- | Join a maybe into the underlying type. 'Nothing' becomes 'none'. joinMaybe :: None n => Maybe n -> n joinMaybe :: Maybe n -> n joinMaybe (Just n j) = n j joinMaybe Maybe n Nothing = n forall n. None n => n none -- | Wrap the value into a maybe. 'none' becomes 'Nothing'. expandMaybe :: (Eq n,None n) => n -> Maybe n expandMaybe :: n -> Maybe n expandMaybe n n | n n n -> n -> Bool forall a. Eq a => a -> a -> Bool == n forall n. None n => n none = Maybe n forall a. Maybe a Nothing | Bool otherwise = n -> Maybe n forall a. a -> Maybe a Just n n -- | Clean the maybe by pulling wrapped 'none's to the outside (as a 'Nothing'). cleanMaybe :: (Eq n,None n) => Maybe n -> Maybe n cleanMaybe :: Maybe n -> Maybe n cleanMaybe = n -> Maybe n forall n. (Eq n, None n) => n -> Maybe n expandMaybe (n -> Maybe n) -> (Maybe n -> n) -> Maybe n -> Maybe n forall b c a. (b -> c) -> (a -> b) -> a -> c . Maybe n -> n forall n. None n => Maybe n -> n joinMaybe -- | Eliminate all void elements from the list. reduce :: (Eq n,None n) => [n] -> [n] reduce :: [n] -> [n] reduce = (n -> Bool) -> [n] -> [n] forall a. (a -> Bool) -> [a] -> [a] filter (n -> n -> Bool forall a. Eq a => a -> a -> Bool /=n forall n. None n => n none) -- | Eliminate all 'Nothing's from the list and unjust all remaining values. reduceMaybe :: [Maybe a] -> [a] reduceMaybe :: [Maybe a] -> [a] reduceMaybe = (Maybe a -> a) -> [Maybe a] -> [a] forall a b. (a -> b) -> [a] -> [b] map Maybe a -> a forall a. Maybe a -> a unjust ([Maybe a] -> [a]) -> ([Maybe a] -> [Maybe a]) -> [Maybe a] -> [a] forall b c a. (b -> c) -> (a -> b) -> a -> c . (Maybe a -> Bool) -> [Maybe a] -> [Maybe a] forall a. (a -> Bool) -> [a] -> [a] filter Maybe a -> Bool forall a. Maybe a -> Bool isJust where unjust :: Maybe a -> a unjust (Just a j) = a j