diff --git a/linear-base.cabal b/linear-base.cabal index e56309e3..3d56af6d 100644 --- a/linear-base.cabal +++ b/linear-base.cabal @@ -57,6 +57,7 @@ library Data.Bifunctor.Linear Data.Bifunctor.Linear.Internal.Bifunctor Data.Bifunctor.Linear.Internal.SymmetricMonoidal + Data.Bits.Linear Data.Bool.Linear Data.Either.Linear Data.Functor.Linear diff --git a/src/Data/Bits/Linear.hs b/src/Data/Bits/Linear.hs new file mode 100644 index 00000000..1baf615b --- /dev/null +++ b/src/Data/Bits/Linear.hs @@ -0,0 +1,37 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE DerivingVia #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE LinearTypes #-} +{-# LANGUAGE StandaloneDeriving #-} +{-# LANGUAGE NoImplicitPrelude #-} + +module Data.Bits.Linear + ( -- * Bits and sub-classes + BitwiseAnd (..) + ) +where + +import qualified Data.Bits as Bits +import Data.Unrestricted.Linear +import qualified Prelude + + +liftU2 :: (Movable a, Movable b) => (a -> b -> c) %1 -> (a %1 -> b %1 -> c) +liftU2 f x y = lifted f (move x) (move y) + where + lifted :: (a -> b -> c) %1 -> (Ur a %1 -> Ur b %1 -> c) + lifted g (Ur a) (Ur b) = g a b + +newtype MovableBits a = MovableBits a + deriving (Consumable, Dupable, Movable, Prelude.Eq, Bits.Bits) + +class BitwiseAnd a where + (.&.) :: a %1 -> a %1 -> a + infixl 7 .&. + +instance (Movable a, Bits.Bits a) => BitwiseAnd (MovableBits a) where + (.&.) = liftU2 (Bits..&.) + +deriving via MovableBits Prelude.Int instance BitwiseAnd Prelude.Int + +deriving via MovableBits Prelude.Word instance BitwiseAnd Prelude.Word