packages feed

hgeometry-combinatorial-0.12.0.1 / src / Data / Ext.hs

{-# LANGUAGE DeriveAnyClass  #-}{-# LANGUAGE OverloadedStrings  #-}---------------------------------------------------------------------------------- |-- Module      :  Data.Ext-- Copyright   :  (C) Frank Staals-- License     :  see the LICENSE file-- Maintainer  :  Frank Staals---- A pair-like data type to represent a 'core' type that has extra information-- as well.----------------------------------------------------------------------------------module Data.Ext whereimport Control.DeepSeqimport Control.Lens hiding ((.=))import Data.Aesonimport Data.Aeson.Types (typeMismatch)import Data.Biapplicativeimport Data.Bifoldableimport Data.Bifunctor.Applyimport Data.Bitraversableimport Data.Functor.Apply (liftF2)import Data.Semigroup.Bifoldableimport Data.Semigroup.Bitraversableimport GHC.Generics (Generic)import Test.QuickCheck---------------------------------------------------------------------------------- | Our Ext type that represents the core datatype core extended with extra-- information of type 'extra'.data core :+ extra = core :+ extra deriving (Show,Read,Eq,Ord,Bounded,Generic,NFData)infixr 1 :+instance Bifunctor (:+) where  bimap f g (c :+ e) = f c :+ g einstance Biapply (:+) where  (f :+ g) <<.>> (c :+ e) = f c :+ g einstance Biapplicative (:+) where  bipure = (:+)  (f :+ g) <<*>> (c :+ e) = f c :+ g einstance Bifoldable (:+) where  bifoldMap f g (c :+ e) = f c `mappend` g einstance Bitraversable (:+) where  bitraverse f g (c :+ e) = (:+) <$> f c <*> g einstance Bifoldable1 (:+)instance Bitraversable1 (:+) where  bitraverse1 f g (c :+ e) = liftF2 (:+) (f c) (g e)instance (Semigroup core, Semigroup extra) => Semigroup (core :+ extra) where  (c :+ e) <> (c' :+ e') = c <> c' :+ e <> e'instance (ToJSON core, ToJSON extra) => ToJSON (core :+ extra) where  -- toJSON     (c :+ e) = toJSON     (c,e)  -- toEncoding (c :+ e) = toEncoding (c,e)  toJSON     (c :+ e) = object ["core" .= c, "extra" .= e]  toEncoding (c :+ e) = pairs  ("core" .= c <> "extra" .= e)instance (FromJSON core, FromJSON extra) => FromJSON (core :+ extra) where  -- parseJSON = fmap (\(c,e) -> c :+ e) . parseJSON  parseJSON (Object v) = (:+) <$> v .: "core" <*> v .: "extra"  parseJSON invalid    = typeMismatch "Ext (:+)" invalidinstance (Arbitrary c, Arbitrary e) => Arbitrary (c :+ e) where  arbitrary = (:+) <$> arbitrary <*> arbitrary-- | Access the core of an extended value._core :: (core :+ extra) -> core_core (c :+ _) = c{-# INLINABLE _core #-}-- | Access the extra part of an extended value._extra :: (core :+ extra) -> extra_extra (_ :+ e) = e{-# INLINABLE _extra #-}-- | Lens access to the core of an extended value.core :: Lens (core :+ extra) (core' :+ extra) core core'core = lens _core (\(_ :+ e) c -> c :+ e){-# INLINABLE core #-}-- | Lens access to the extra part of an extended value.extra :: Lens (core :+ extra) (core :+ extra') extra extra'extra = lens _extra (\(c :+ _) e -> c :+ e){-# INLINABLE extra #-}-- | Tag a value with the unit type.ext   :: a -> a :+ ()ext x = x :+ (){-# INLINABLE ext #-}