Skip to content

Commit

Permalink
Fix the build with -contravariant
Browse files Browse the repository at this point in the history
For the most part, this is a matter of guarding the relevant definitions behind
`defined(MIN_VERSION_contravariant)` CPP. I also altered some definitions to
avoid relying on the `contravariant` library where they didn't need to do so.

Fixes #136.
  • Loading branch information
RyanGlScott committed Apr 12, 2024
1 parent e2ec1ac commit 9f2b08f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 40 deletions.
26 changes: 18 additions & 8 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.15.20230312
# version: 0.16.6
#
# REGENDATA ("0.15.20230312",["github","--config=cabal.haskell-ci","cabal.project"])
# REGENDATA ("0.16.6",["github","--config=cabal.haskell-ci","cabal.project"])
#
name: Haskell-CI
on:
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5
if [ "${{ matrix.setup-method }}" = ghcup ]; then
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.18.0/x86_64-linux-ghcup-0.1.18.0 > "$HOME/.ghcup/bin/ghcup"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false)
"$HOME/.ghcup/bin/ghcup" install cabal 3.10.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
Expand All @@ -95,7 +95,7 @@ jobs:
apt-get update
apt-get install -y "$HCNAME"
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.18.0/x86_64-linux-ghcup-0.1.18.0 > "$HOME/.ghcup/bin/ghcup"
curl -sL https://downloads.haskell.org/ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
"$HOME/.ghcup/bin/ghcup" install cabal 3.10.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
fi
Expand Down Expand Up @@ -173,17 +173,17 @@ jobs:
- name: install cabal-plan
run: |
mkdir -p $HOME/.cabal/bin
curl -sL https://github.com/haskell-hvr/cabal-plan/releases/download/v0.6.2.0/cabal-plan-0.6.2.0-x86_64-linux.xz > cabal-plan.xz
echo 'de73600b1836d3f55e32d80385acc055fd97f60eaa0ab68a755302685f5d81bc cabal-plan.xz' | sha256sum -c -
curl -sL https://github.com/haskell-hvr/cabal-plan/releases/download/v0.7.3.0/cabal-plan-0.7.3.0-x86_64-linux.xz > cabal-plan.xz
echo 'f62ccb2971567a5f638f2005ad3173dba14693a45154c1508645c52289714cb2 cabal-plan.xz' | sha256sum -c -
xz -d < cabal-plan.xz > $HOME/.cabal/bin/cabal-plan
rm -f cabal-plan.xz
chmod a+x $HOME/.cabal/bin/cabal-plan
cabal-plan --version
- name: install cabal-docspec
run: |
mkdir -p $HOME/.cabal/bin
curl -sL https://github.com/phadej/cabal-extras/releases/download/cabal-docspec-0.0.0.20211114/cabal-docspec-0.0.0.20211114.xz > cabal-docspec.xz
echo 'e224700d9e8c9ec7ec6bc3f542ba433cd9925a5d356676c62a9bd1f2c8be8f8a cabal-docspec.xz' | sha256sum -c -
curl -sL https://github.com/phadej/cabal-extras/releases/download/cabal-docspec-0.0.0.20230517/cabal-docspec-0.0.0.20230517-x86_64-linux.xz > cabal-docspec.xz
echo '3b31bbe463ad4d671abbc103db49628562ec48a6604cab278207b5b6acd21ed7 cabal-docspec.xz' | sha256sum -c -
xz -d < cabal-docspec.xz > $HOME/.cabal/bin/cabal-docspec
rm -f cabal-docspec.xz
chmod a+x $HOME/.cabal/bin/cabal-docspec
Expand Down Expand Up @@ -251,12 +251,22 @@ jobs:
- name: prepare for constraint sets
run: |
rm -f cabal.project.local
- name: constraint set no-contravariant
run: |
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -contravariant' all --dry-run
cabal-plan topo | sort
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -contravariant' --dependencies-only -j2 all
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -contravariant' all
- name: constraint set no-containers
run: |
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -containers' all --dry-run
cabal-plan topo | sort
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -containers' --dependencies-only -j2 all
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -containers' all
- name: constraint set no-comonad
run: |
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -comonad' all --dry-run
cabal-plan topo | sort
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -comonad' --dependencies-only -j2 all
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --constraint='semigroupoids -comonad' all
- name: save cache
Expand Down
3 changes: 3 additions & 0 deletions cabal.haskell-ci
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ constraint-set no-comonad

constraint-set no-containers
constraints: semigroupoids -containers

constraint-set no-contravariant
constraints: semigroupoids -contravariant
9 changes: 6 additions & 3 deletions semigroupoids.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ library
Data.Functor.Bind
Data.Functor.Bind.Class
Data.Functor.Bind.Trans
Data.Functor.Contravariant.Conclude
Data.Functor.Contravariant.Decide
Data.Functor.Contravariant.Divise
Data.Functor.Extend
Data.Functor.Plus
Data.Groupoid
Expand All @@ -193,6 +190,12 @@ library
other-modules:
Semigroupoids.Internal

if impl(ghc >= 8.6) || flag(contravariant)
exposed-modules:
Data.Functor.Contravariant.Conclude
Data.Functor.Contravariant.Decide
Data.Functor.Contravariant.Divise

ghc-options: -Wall -Wno-warnings-deprecations -Wno-trustworthy-safe

if impl(ghc >= 9.0)
Expand Down
53 changes: 38 additions & 15 deletions src/Data/Functor/Contravariant/Conclude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
-- Stability : provisional
-- Portability : portable
--
-- This module is only available if building with GHC 8.6 or later, or if the
-- @+contravariant@ @cabal@ build flag is available.
----------------------------------------------------------------------------
module Data.Functor.Contravariant.Conclude (
Conclude(..)
Expand All @@ -22,7 +24,6 @@ module Data.Functor.Contravariant.Conclude (

import Control.Applicative.Backwards
import Control.Monad.Trans.Identity
import Control.Monad.Trans.Maybe
import qualified Control.Monad.Trans.RWS.Lazy as Lazy
import qualified Control.Monad.Trans.RWS.Strict as Strict
import Control.Monad.Trans.Reader
Expand All @@ -35,17 +36,20 @@ import Data.Functor.Apply
import Data.Functor.Compose
import Data.Functor.Contravariant
import Data.Functor.Contravariant.Decide
import Data.Functor.Contravariant.Divise
import Data.Functor.Contravariant.Divisible
import Data.Functor.Product
import Data.Functor.Reverse
import Data.Monoid (Alt(..))
import Data.Proxy
import Data.Void
import GHC.Generics

#if !(MIN_VERSION_transformers(0,6,0))
#if defined(MIN_VERSION_contravariant)
# if !(MIN_VERSION_transformers(0,6,0))
import Control.Monad.Trans.List
# endif
import Control.Monad.Trans.Maybe
import Data.Functor.Contravariant.Divise
import Data.Functor.Contravariant.Divisible
#endif

#ifdef MIN_VERSION_StateVar
Expand Down Expand Up @@ -110,37 +114,48 @@ concluded = conclude id
gconcluded :: (Generic1 f, Conclude (Rep1 f)) => f Void
gconcluded = to1 concluded

-- | @since 5.3.6
#if defined(MIN_VERSION_contravariant)
-- | This instance is only available if the @+contravariant@ @cabal@ flag is
-- enabled.
--
-- @since 5.3.6
instance Decidable f => Conclude (WrappedDivisible f) where
conclude f = WrapDivisible (lose f)
#endif

-- | @since 5.3.6
instance Conclude Comparison where conclude = lose
instance Conclude Comparison where
conclude f = Comparison $ \a _ -> absurd (f a)

-- | @since 5.3.6
instance Conclude Equivalence where conclude = lose
instance Conclude Equivalence where
conclude f = Equivalence $ absurd . f

-- | @since 5.3.6
instance Conclude Predicate where conclude = lose
instance Conclude Predicate where
conclude f = Predicate $ absurd . f

-- | @since 5.3.6
instance Conclude (Op r) where
conclude f = Op $ absurd . f

-- | @since 5.3.6
instance Conclude Proxy where conclude = lose
instance Conclude Proxy where
conclude _ = Proxy

#ifdef MIN_VERSION_StateVar
-- | @since 5.3.6
instance Conclude SettableStateVar where conclude = lose
instance Conclude SettableStateVar where
conclude k = SettableStateVar (absurd . k)
#endif

-- | @since 5.3.6
instance Conclude f => Conclude (Alt f) where
conclude = Alt . conclude

-- | @since 5.3.6
instance Conclude U1 where conclude = lose
instance Conclude U1 where
conclude _ = U1

-- | @since 5.3.6
instance Conclude f => Conclude (Rec1 f) where
Expand Down Expand Up @@ -178,15 +193,23 @@ instance Conclude m => Conclude (Lazy.RWST r w s m) where
instance Conclude m => Conclude (Strict.RWST r w s m) where
conclude f = Strict.RWST $ \_ _ -> contramap (\(a, _, _) -> a) (conclude f)

#if !(MIN_VERSION_transformers(0,6,0))
-- | @since 5.3.6
#if defined(MIN_VERSION_contravariant)
# if !(MIN_VERSION_transformers(0,6,0))
-- | This instance is only available if the @+contravariant@ @cabal@ flag is
-- enabled.
--
-- @since 5.3.6
instance (Divisible m, Divise m) => Conclude (ListT m) where
conclude _ = ListT conquer
#endif
# endif

-- | @since 5.3.6
-- | This instance is only available if the @+contravariant@ @cabal@ flag is
-- enabled.
--
-- @since 5.3.6
instance (Divisible m, Divise m) => Conclude (MaybeT m) where
conclude _ = MaybeT conquer
#endif

-- | @since 5.3.6
instance Conclude m => Conclude (Lazy.StateT s m) where
Expand Down
38 changes: 32 additions & 6 deletions src/Data/Functor/Contravariant/Decide.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
-- Stability : provisional
-- Portability : portable
--
-- This module is only available if building with GHC 8.6 or later, or if the
-- @+contravariant@ @cabal@ build flag is available.
----------------------------------------------------------------------------
module Data.Functor.Contravariant.Decide (
Decide(..)
Expand All @@ -37,7 +39,6 @@ import Data.Functor.Apply
import Data.Functor.Compose
import Data.Functor.Contravariant
import Data.Functor.Contravariant.Divise
import Data.Functor.Contravariant.Divisible
import Data.Functor.Product
import Data.Functor.Reverse
import Data.Monoid (Alt(..))
Expand All @@ -50,6 +51,10 @@ import Control.Monad.Trans.List
import Data.Either
#endif

#if defined(MIN_VERSION_contravariant)
import Data.Functor.Contravariant.Divisible
#endif

#ifdef MIN_VERSION_StateVar
import Data.StateVar
#endif
Expand Down Expand Up @@ -99,18 +104,38 @@ decided = decide id
gdecided :: (Generic1 f, Decide (Rep1 f)) => f b -> f c -> f (Either b c)
gdecided fb fc = gdecide id fb fc

-- | @since 5.3.6
#if defined(MIN_VERSION_contravariant)
-- | This instance is only available if the @+contravariant@ @cabal@ flag is
-- enabled.
--
-- @since 5.3.6
instance Decidable f => Decide (WrappedDivisible f) where
decide f (WrapDivisible x) (WrapDivisible y) = WrapDivisible (choose f x y)
#endif

-- | @since 5.3.6
instance Decide Comparison where decide = choose
instance Decide Comparison where
decide f (Comparison g) (Comparison h) = Comparison $ \a b -> case f a of
Left c -> case f b of
Left d -> g c d
Right{} -> LT
Right c -> case f b of
Left{} -> GT
Right d -> h c d

-- | @since 5.3.6
instance Decide Equivalence where decide = choose
instance Decide Equivalence where
decide f (Equivalence g) (Equivalence h) = Equivalence $ \a b -> case f a of
Left c -> case f b of
Left d -> g c d
Right{} -> False
Right c -> case f b of
Left{} -> False
Right d -> h c d

-- | @since 5.3.6
instance Decide Predicate where decide = choose
instance Decide Predicate where
decide f (Predicate g) (Predicate h) = Predicate $ either g h . f

-- | Unlike 'Decidable', requires no constraint on @r@.
--
Expand All @@ -123,7 +148,8 @@ instance Decide f => Decide (Alt f) where
decide f (Alt l) (Alt r) = Alt $ decide f l r

-- | @since 5.3.6
instance Decide U1 where decide = choose
instance Decide U1 where
decide _ U1 U1 = U1

-- | Has no 'Decidable' or 'Conclude' instance.
--
Expand Down
Loading

0 comments on commit 9f2b08f

Please sign in to comment.