(original) (raw)

{-# LANGUAGE Trustworthy #-} {-# LANGUAGE NoImplicitPrelude #-}

module Data.Functor.Utils where

import Data.Coerce (Coercible, coerce) import GHC.Base ( Applicative(..), Functor(..), Maybe(..), Monoid(..), Ord(..) , Semigroup(..), ($), otherwise )

newtype Max a = Max {getMax :: Maybe a} newtype Min a = Min {getMin :: Maybe a}

instance Ord a => Semigroup (Max a) where {-# INLINE (<>) #-} m <> Max Nothing = m Max Nothing <> n = n (Max m@(Just x)) <> (Max n@(Just y)) | x >= y = Max m | otherwise = Max n

instance Ord a => Monoid (Max a) where mempty = Max Nothing

instance Ord a => Semigroup (Min a) where {-# INLINE (<>) #-} m <> Min Nothing = m Min Nothing <> n = n (Min m@(Just x)) <> (Min n@(Just y)) | x <= y = Min m | otherwise = Min n

instance Ord a => Monoid (Min a) where mempty = Min Nothing

newtype StateL s a = StateL { runStateL :: s -> (s, a) }

instance Functor (StateL s) where fmap f (StateL k) = StateL $ \ s -> let (s', v) = k s in (s', f v)

instance Applicative (StateL s) where pure x = StateL (\ s -> (s, x)) StateL kf <*> StateL kv = StateL $ \ s -> let (s', f) = kf s (s'', v) = kv s' in (s'', f v) liftA2 f (StateL kx) (StateL ky) = StateL $ [s](#local-6989586621679048931) -> let (s', x) = kx s (s'', y) = ky s' in (s'', f x y)

newtype StateR s a = StateR { runStateR :: s -> (s, a) }

instance Functor (StateR s) where fmap f (StateR k) = StateR $ \ s -> let (s', v) = k s in (s', f v)

instance Applicative (StateR s) where pure x = StateR (\ s -> (s, x)) StateR kf <*> StateR kv = StateR $ \ s -> let (s', v) = kv s (s'', f) = kf s' in (s'', f v) liftA2 f (StateR kx) (StateR ky) = StateR $ \ s -> let (s', y) = ky s (s'', x) = kx s' in (s'', f x y)

(#.) :: Coercible b c => (b -> c) -> (a -> b) -> (a -> c) (#.) _f = coerce {-# INLINE (#.) #-}