Skip to content

bitwise-and BitwiseAnd for Int and Word #419

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions linear-base.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/Data/Bits/Linear.hs
Original file line number Diff line number Diff line change
@@ -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