Skip to content

Move Unzip to the bottom of type-class hierarchy #208

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
12 changes: 6 additions & 6 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.19.20250104
# version: 0.19.20250506
#
# REGENDATA ("0.19.20250104",["github","cabal.project"])
# REGENDATA ("0.19.20250506",["github","cabal.project"])
#
name: Haskell-CI
on:
Expand All @@ -23,7 +23,7 @@ on:
jobs:
linux:
name: Haskell-CI - Linux - ${{ matrix.compiler }}
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
timeout-minutes:
60
container:
Expand Down Expand Up @@ -91,12 +91,12 @@ jobs:
- name: Install GHCup
run: |
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.30.0/x86_64-linux-ghcup-0.1.30.0 > "$HOME/.ghcup/bin/ghcup"
curl -sL https://downloads.haskell.org/ghcup/0.1.50.1/x86_64-linux-ghcup-0.1.50.1 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
- name: Install cabal-install
run: |
"$HOME/.ghcup/bin/ghcup" install cabal 3.14.1.1 || (cat "$HOME"/.ghcup/logs/*.* && false)
echo "CABAL=$HOME/.ghcup/bin/cabal-3.14.1.1 -vnormal+nowrap" >> "$GITHUB_ENV"
"$HOME/.ghcup/bin/ghcup" install cabal 3.14.2.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
echo "CABAL=$HOME/.ghcup/bin/cabal-3.14.2.0 -vnormal+nowrap" >> "$GITHUB_ENV"
- name: Install GHC (GHCup)
if: matrix.setup-method == 'ghcup'
run: |
Expand Down
9 changes: 9 additions & 0 deletions semialign/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 1.4

- Reorder class hierarchy. `Unzip` is now at the bottom, right above `Functor`.
There are two ways things may break:
- You may need to add `Unzip` instance if you haven't one already, for example:
`instance Unzip ((->) e) where unzip = unzipDefault` was added in this patch.
- `Unzip f` doesn't imply whole hierarchy, so you may need to change `Unzip f` to `Zip f`
in the constraints of some of your functions.

# 1.3.1

- Support GHC-8.6.5...GHC-9.10.1
Expand Down
3 changes: 1 addition & 2 deletions semialign/semialign.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cabal-version: >=1.10
name: semialign
version: 1.3.1
x-revision: 1
version: 1.4
synopsis:
Align and Zip type-classes from the common Semialign ancestor.

Expand Down
24 changes: 22 additions & 2 deletions semialign/src/Data/Semialign/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ oops = error . ("Data.Align: internal error: " ++)
-- ≡ mapMaybe justHere (toList (align x y))
-- @
--
class Functor f => Semialign f where
class Unzip f => Semialign f where
-- | Analogous to @'zip'@, combines two structures by taking the union of
-- their shapes and using @'These'@ to hold the elements.
align :: f a -> f b -> f (These a b)
Expand Down Expand Up @@ -295,7 +295,7 @@ class Zip f => Repeat f where
--
-- For sequence-like types this holds, but for Map-like it doesn't.
--
class Zip f => Unzip f where
class Functor f => Unzip f where
unzipWith :: (c -> (a, b)) -> f c -> (f a, f b)
unzipWith f = unzip . fmap f

Expand Down Expand Up @@ -337,6 +337,12 @@ class (ZipWithIndex i f, Repeat f) => RepeatWithIndex i f | f -> i where
-- base
-------------------------------------------------------------------------------

-- |
--
-- @since 1.4
instance Unzip ((->) e) where
unzip = unzipDefault

instance Semialign ((->) e) where
align f g x = These (f x) (g x)
alignWith h f g x = h (These (f x) (g x))
Expand Down Expand Up @@ -661,6 +667,13 @@ instance (RepeatWithIndex i f, RepeatWithIndex j g) => RepeatWithIndex (i, j) (C
-------------------------------------------------------------------------------

-- Based on the Data.Vector.Fusion.Stream.Monadic zipWith implementation

-- |
--
-- @since 1.4
instance Monad m => Unzip (Stream m) where
unzip = unzipDefault

instance Monad m => Align (Stream m) where
nil = Stream.empty

Expand Down Expand Up @@ -688,9 +701,16 @@ instance Monad m => Semialign (Stream m) where
_ -> Skip (sa, sb, Nothing, False)
#endif


instance Monad m => Zip (Stream m) where
zipWith = Stream.zipWith

-- |
--
-- @since 1.4
instance Monad m => Unzip (Bundle m v) where
unzip = unzipDefault

instance Monad m => Align (Bundle m v) where
nil = Bundle.empty

Expand Down
9 changes: 9 additions & 0 deletions these-tests/test/Tests/AlignWrong.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import Data.These

newtype WrongMap k v = WM (Map k v) deriving (Eq, Ord, Show, Functor, Foldable)

instance Unzip (WrongMap k) where
unzip = unzipDefault

instance (Arbitrary k, Arbitrary v, Ord k) => Arbitrary (WrongMap k v) where
arbitrary = WM <$> arbitrary
shrink (WM m) = WM <$> shrink m
Expand Down Expand Up @@ -46,6 +49,9 @@ instance Ord k => Zip (WrongMap k) where
newtype WeirdMap k v = WeirdMap (Map k v)
deriving (Eq, Ord, Show, Functor, Foldable)

instance Unzip (WeirdMap k) where
unzip = unzipDefault

instance (Arbitrary k, Arbitrary v, Ord k) => Arbitrary (WeirdMap k v) where
arbitrary = WeirdMap <$> arbitrary
shrink (WeirdMap m) = WeirdMap <$> shrink m
Expand Down Expand Up @@ -73,6 +79,9 @@ instance Ord k => Zip (WeirdMap k) where
newtype R a = Nest [[a]]
deriving (Show, Eq, Ord, Functor, Foldable)

instance Unzip R where
unzip = unzipDefault

instance Align R where
nil = Nest []

Expand Down
4 changes: 2 additions & 2 deletions these-tests/test/Tests/Semialign.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ data CSemialign f where

semialignLaws
:: forall (f :: * -> *).
( Semialign f, Unzip f, Foldable f, Typeable f
( Semialign f, Zip f, Foldable f, Typeable f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
Expand Down Expand Up @@ -357,7 +357,7 @@ unalignLaws' _ = testGroup "Unalign"


unzipLaws'
:: forall f proxy. (Unzip f
:: forall f proxy. (Zip f
, Eq (f A), Show (f A), Arbitrary (f A)
, Eq (f B), Show (f B), Arbitrary (f B)
, Eq (f C), Show (f C), Arbitrary (f C)
Expand Down