Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for the relationship between leq and compare #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/Algebra/Lattice/Dropped.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Prelude ()
import Prelude.Compat

import Algebra.Lattice
import Algebra.PartialOrd

import Control.DeepSeq
import Control.Monad
Expand All @@ -42,8 +43,8 @@ import GHC.Generics

-- | Graft a distinct top onto an otherwise unbounded lattice.
-- As a bonus, the top will be an absorbing element for the join.
data Dropped a = Top
| Drop a
data Dropped a = Drop a
| Top
deriving ( Eq, Ord, Show, Read, Data, Typeable, Generic, Functor, Foldable, Traversable
#if __GLASGOW_HASKELL__ >= 706
, Generic1
Expand All @@ -65,6 +66,14 @@ instance NFData a => NFData (Dropped a) where

instance Hashable a => Hashable (Dropped a)

instance PartialOrd a => PartialOrd (Dropped a) where
leq _ Top = True
leq Top _ = False
leq (Drop x) (Drop y) = leq x y
comparable Top _ = True
comparable _ Top = True
comparable (Drop x) (Drop y) = comparable x y

instance JoinSemiLattice a => JoinSemiLattice (Dropped a) where
Top \/ _ = Top
_ \/ Top = Top
Expand Down
17 changes: 15 additions & 2 deletions src/Algebra/Lattice/Levitated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Prelude ()
import Prelude.Compat

import Algebra.Lattice
import Algebra.PartialOrd

import Control.DeepSeq
import Control.Monad
Expand All @@ -43,9 +44,9 @@ import GHC.Generics
-- | Graft a distinct top and bottom onto an otherwise unbounded lattice.
-- The top is the absorbing element for the join, and the bottom is the absorbing
-- element for the meet.
data Levitated a = Top
data Levitated a = Bottom
| Levitate a
| Bottom
| Top
deriving ( Eq, Ord, Show, Read, Data, Typeable, Generic, Functor, Foldable, Traversable
#if __GLASGOW_HASKELL__ >= 706
, Generic1
Expand All @@ -69,6 +70,18 @@ instance NFData a => NFData (Levitated a) where

instance Hashable a => Hashable (Levitated a)

instance PartialOrd a => PartialOrd (Levitated a) where
leq _ Top = True
leq Top _ = False
leq Bottom _ = True
leq _ Bottom = False
leq (Levitate x) (Levitate y) = leq x y
comparable Top _ = True
comparable _ Top = True
comparable Bottom _ = True
comparable _ Bottom = True
comparable (Levitate x) (Levitate y) = comparable x y

instance JoinSemiLattice a => JoinSemiLattice (Levitated a) where
Top \/ _ = Top
_ \/ Top = Top
Expand Down
13 changes: 11 additions & 2 deletions src/Algebra/Lattice/Lifted.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Prelude ()
import Prelude.Compat

import Algebra.Lattice
import Algebra.PartialOrd

import Control.DeepSeq
import Control.Monad
Expand All @@ -42,8 +43,8 @@ import GHC.Generics

-- | Graft a distinct bottom onto an otherwise unbounded lattice.
-- As a bonus, the bottom will be an absorbing element for the meet.
data Lifted a = Lift a
| Bottom
data Lifted a = Bottom
| Lift a
deriving ( Eq, Ord, Show, Read, Data, Typeable, Generic, Functor, Foldable, Traversable
#if __GLASGOW_HASKELL__ >= 706
, Generic1
Expand All @@ -65,6 +66,14 @@ instance NFData a => NFData (Lifted a) where

instance Hashable a => Hashable (Lifted a)

instance PartialOrd a => PartialOrd (Lifted a) where
leq Bottom _ = True
leq _ Bottom = False
leq (Lift x) (Lift y) = leq x y
comparable Bottom _ = True
comparable _ Bottom = True
comparable (Lift x) (Lift y) = comparable x y

instance JoinSemiLattice a => JoinSemiLattice (Lifted a) where
Lift x \/ Lift y = Lift (x \/ y)
Bottom \/ lift_y = lift_y
Expand Down
14 changes: 13 additions & 1 deletion src/Algebra/Lattice/Op.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#else
{-# LANGUAGE Safe #-}
#endif
#if __GLASGOW_HASKELL__ >= 806
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DerivingVia #-}
#endif

----------------------------------------------------------------------------
-- |
-- Module : Algebra.Lattice.Op
Expand All @@ -34,6 +39,7 @@ import Control.DeepSeq
import Control.Monad
import Data.Data
import Data.Hashable
import Data.Ord
import GHC.Generics

--
Expand All @@ -43,11 +49,17 @@ import GHC.Generics
-- | The opposite lattice of a given lattice. That is, switch
-- meets and joins.
newtype Op a = Op { getOp :: a }
deriving ( Eq, Ord, Show, Read, Data, Typeable, Generic, Functor, Foldable, Traversable
deriving ( Eq, Show, Read, Data, Typeable, Generic, Functor, Foldable, Traversable
#if __GLASGOW_HASKELL__ >= 706
, Generic1
#endif
)
#if __GLASGOW_HASKELL__ >= 806
deriving Ord via (Down a)
#else
instance Ord a => Ord (Op a) where
compare (Op a) (Op b) = compare b a
#endif

instance Applicative Op where
pure = return
Expand Down