Skip to content
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