Skip to content

Commit 0b59ab1

Browse files
committed
add Tagged instance for weakened "through"
1 parent 4d8cf90 commit 0b59ab1

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dependencies:
6666
- rerefined ^>= 0.8.0
6767
- vector-sized >= 1.5.0 && < 1.7
6868
- vector >= 0.12.3.1 && < 0.14
69+
- tagged ^>= 0.8.8
6970

7071
library:
7172
source-dirs: src

src/Strongweak/Strengthen.hs

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import Data.Bits ( FiniteBits )
4141

4242
import Data.Typeable ( Typeable, TypeRep, typeRep, Proxy(Proxy) )
4343

44+
import Data.Tagged ( Tagged(..) )
45+
4446
{- | Attempt to strengthen some @'Weakened' a@, asserting certain invariants.
4547
4648
We take 'Weaken' as a superclass in order to maintain strong/weak type pair
@@ -244,3 +246,8 @@ f .> g = g . f
244246

245247
typeRep' :: forall a. Typeable a => TypeRep
246248
typeRep' = typeRep (Proxy @a)
249+
250+
-- | SPECIAL: Strengthen through a 'Tagged'. That is, strengthen @a@ then tag it
251+
-- with @x@.
252+
instance Strengthen a => Strengthen (Tagged x a) where
253+
strengthen = fmap Tagged <$> strengthen

src/Strongweak/Weaken.hs

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Data.Functor.Const
2323
import Data.List.NonEmpty qualified as NonEmpty
2424
import Data.List.NonEmpty ( NonEmpty )
2525
import GHC.TypeNats
26+
import Data.Tagged ( Tagged(..) )
2627

2728
{- | Weaken some @a@, relaxing certain invariants.
2829
@@ -149,3 +150,14 @@ instance (Weaken a, Weaken b) => Weaken (Either a b) where
149150
type Weakened (Either a b) = Either (Weakened a) (Weakened b)
150151
weaken = \case Left a -> Left $ weaken a
151152
Right b -> Right $ weaken b
153+
154+
-- | SPECIAL: Weaken through a 'Tagged'. That is, strip the 'Tagged' and weaken
155+
-- the inner @a@.
156+
--
157+
-- This appears to contribute a useful role: we want to plug some newtype into
158+
-- the strongweak ecosystem, but it would result in orphan instances. With this,
159+
-- we can go through 'Tagged', and the phantom type helps us handle
160+
-- parameterized newtypes (like @newtype 'ByteOrdered' (end :: 'ByteOrder') a@).
161+
instance Weaken a => Weaken (Tagged x a) where
162+
type Weakened (Tagged x a) = Weakened a
163+
weaken = weaken . unTagged

strongweak.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ library
5656
build-depends:
5757
base >=4.18 && <5
5858
, rerefined >=0.8.0 && <0.9
59+
, tagged >=0.8.8 && <0.9
5960
, text >=2.0 && <2.2
6061
, text-builder-linear >=0.1.3 && <0.2
6162
, vector >=0.12.3.1 && <0.14
@@ -93,6 +94,7 @@ test-suite spec
9394
, quickcheck-instances >=0.3.26 && <0.4
9495
, rerefined >=0.8.0 && <0.9
9596
, strongweak
97+
, tagged >=0.8.8 && <0.9
9698
, text >=2.0 && <2.2
9799
, text-builder-linear >=0.1.3 && <0.2
98100
, vector >=0.12.3.1 && <0.14

0 commit comments

Comments
 (0)