Packages

The Phi Programming Language

Current section

Files

Jump to
phi lib Data Ordering.hm
Raw

lib/Data/Ordering.hm

-----------------------------------------------------------------------------
-- |
-- Module : Data.Ordering
-- Copyright : (c) 2020-2021 EMQ Technologies Co., Ltd.
-- License : BSD-style (see the LICENSE file)
--
-- Maintainer : Feng Lee, feng@emqx.io
-- Yang M, yangm@emqx.io
-- Stability : experimental
-- Portability : portable
--
-- The Ordering datatype
--
-----------------------------------------------------------------------------
module Data.Ordering
( Ordering(..)
, invert
) where
import Data.Eq (class Eq)
import Data.Semigroup (class Semigroup)
import Data.Monoid (class Monoid)
-- | The `Ordering` data type represents the three possible outcomes of
-- | comparing two values:
-- |
-- | `LT` - The first value is _less than_ the second.
-- | `GT` - The first value is _greater than_ the second.
-- | `EQ` - The first value is _equal to_ the second.
data Ordering = LT | GT | EQ
instance Eq Ordering where
eq LT LT = true
eq GT GT = true
eq EQ EQ = true
eq _ _ = false
instance Semigroup Ordering where
append LT _ = LT
append GT _ = GT
append EQ y = y
-- | Reverses an `Ordering` value, flipping greater than for less than while
-- | preserving equality.
invert :: Ordering -> Ordering
invert GT = LT
invert EQ = EQ
invert LT = GT
instance Monoid Ordering where
mempty = EQ