Skip to content

Commit

Permalink
Use dynamic dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed May 20, 2024
1 parent 6ffe292 commit 072cc85
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 70 deletions.
108 changes: 57 additions & 51 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,55 @@
# This GitHub workflow config has been generated by a script via
#
# haskell-ci 'github' 'hedis-effectful.cabal'
# haskell-ci 'github' '--config=cabal.haskell-ci' 'cabal.project'
#
# To regenerate the script (for example after adjusting tested-with) run
#
# haskell-ci regenerate
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.16.6
# version: 0.19.20240514
#
# REGENDATA ("0.16.6",["github","hedis-effectful.cabal"])
# REGENDATA ("0.19.20240514",["github","--config=cabal.haskell-ci","cabal.project"])
#
name: Haskell-CI
on:
- push
- pull_request
push:
branches:
- master
pull_request:
branches:
- master
jobs:
linux:
name: Haskell-CI - Linux - ${{ matrix.compiler }}
runs-on: ubuntu-22.04
runs-on: ubuntu-20.04
timeout-minutes:
60
container:
image: buildpack-deps:bionic
image: buildpack-deps:jammy
continue-on-error: ${{ matrix.allow-failure }}
strategy:
matrix:
include:
- compiler: ghc-9.6.1
- compiler: ghc-9.8.2
compilerKind: ghc
compilerVersion: 9.6.1
compilerVersion: 9.8.2
setup-method: ghcup
allow-failure: false
- compiler: ghc-9.4.4
- compiler: ghc-9.6.5
compilerKind: ghc
compilerVersion: 9.4.4
compilerVersion: 9.6.5
setup-method: ghcup
allow-failure: false
- compiler: ghc-9.2.7
- compiler: ghc-9.4.8
compilerKind: ghc
compilerVersion: 9.2.7
compilerVersion: 9.4.8
setup-method: ghcup
allow-failure: false
- compiler: ghc-9.2.8
compilerKind: ghc
compilerVersion: 9.2.8
setup-method: ghcup
allow-failure: false
- compiler: ghc-9.0.2
Expand All @@ -53,32 +62,17 @@ jobs:
compilerVersion: 8.10.7
setup-method: ghcup
allow-failure: false
- compiler: ghc-8.8.4
compilerKind: ghc
compilerVersion: 8.8.4
setup-method: hvr-ppa
allow-failure: false
fail-fast: false
steps:
- name: apt
run: |
apt-get update
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.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)
else
apt-add-repository -y 'ppa:hvr/ghc'
apt-get update
apt-get install -y "$HCNAME"
mkdir -p "$HOME/.ghcup/bin"
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
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.20.0/x86_64-linux-ghcup-0.1.20.0 > "$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.2.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
env:
HCKIND: ${{ matrix.compilerKind }}
HCNAME: ${{ matrix.compiler }}
Expand All @@ -90,20 +84,13 @@ jobs:
echo "CABAL_DIR=$HOME/.cabal" >> "$GITHUB_ENV"
echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV"
HCDIR=/opt/$HCKIND/$HCVER
if [ "${{ matrix.setup-method }}" = ghcup ]; then
HC=$HOME/.ghcup/bin/$HCKIND-$HCVER
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HOME/.ghcup/bin/$HCKIND-pkg-$HCVER" >> "$GITHUB_ENV"
echo "HADDOCK=$HOME/.ghcup/bin/haddock-$HCVER" >> "$GITHUB_ENV"
echo "CABAL=$HOME/.ghcup/bin/cabal-3.10.1.0 -vnormal+nowrap" >> "$GITHUB_ENV"
else
HC=$HCDIR/bin/$HCKIND
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HCDIR/bin/$HCKIND-pkg" >> "$GITHUB_ENV"
echo "HADDOCK=$HCDIR/bin/haddock" >> "$GITHUB_ENV"
echo "CABAL=$HOME/.ghcup/bin/cabal-3.10.1.0 -vnormal+nowrap" >> "$GITHUB_ENV"
fi
HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER")
HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#')
HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#')
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HCPKG" >> "$GITHUB_ENV"
echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV"
echo "CABAL=$HOME/.ghcup/bin/cabal-3.10.2.0 -vnormal+nowrap" >> "$GITHUB_ENV"
HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')
echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV"
echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV"
Expand Down Expand Up @@ -150,6 +137,11 @@ jobs:
- name: update cabal index
run: |
$CABAL v2-update -v
- name: cache (tools)
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ matrix.compiler }}-tools-655fd156
path: ~/.haskell-ci-tools
- name: install cabal-plan
run: |
mkdir -p $HOME/.cabal/bin
Expand All @@ -159,8 +151,18 @@ jobs:
rm -f cabal-plan.xz
chmod a+x $HOME/.cabal/bin/cabal-plan
cabal-plan --version
- name: install doctest
run: |
$CABAL --store-dir=$HOME/.haskell-ci-tools/store v2-install $ARG_COMPILER --ignore-project -j2 doctest --constraint='doctest ^>=0.22.0'
doctest --version
- name: save cache (tools)
uses: actions/cache/save@v4
if: always()
with:
key: ${{ runner.os }}-${{ matrix.compiler }}-tools-655fd156
path: ~/.haskell-ci-tools
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
path: source
- name: initial cabal.project for sdist
Expand Down Expand Up @@ -188,15 +190,15 @@ jobs:
echo " ghc-options: -Werror=missing-methods" >> cabal.project
cat >> cabal.project <<EOF
EOF
$HCPKG list --simple-output --names-only | perl -ne 'for (split /\s+/) { print "constraints: $_ installed\n" unless /^(hedis-effectful)$/; }' >> cabal.project.local
$HCPKG list --simple-output --names-only | perl -ne 'for (split /\s+/) { print "constraints: any.$_ installed\n" unless /^(hedis-effectful)$/; }' >> cabal.project.local
cat cabal.project
cat cabal.project.local
- name: dump install plan
run: |
$CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dry-run all
cabal-plan
- name: restore cache
uses: actions/cache/restore@v3
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }}
path: ~/.cabal/store
Expand All @@ -211,6 +213,10 @@ jobs:
- name: build
run: |
$CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --write-ghc-environment-files=always
- name: doctest
run: |
cd ${PKGDIR_hedis_effectful} || false
doctest -XHaskell2010 -XNoStarIsType -XBangPatterns -XConstraintKinds -XDataKinds -XFlexibleContexts -XImportQualifiedPost -XGADTs -XLambdaCase -XTypeFamilies -XTypeOperators src
- name: cabal check
run: |
cd ${PKGDIR_hedis_effectful} || false
Expand All @@ -223,7 +229,7 @@ jobs:
rm -f cabal.project.local
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all
- name: save cache
uses: actions/cache/save@v3
uses: actions/cache/save@v4
if: always()
with:
key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# hedis-effectful

[![Build Status](https://github.com/scrive/hedis-effectful/workflows/Haskell-CI/badge.svg?branch=master)](https://github.com/scrive/hedis-effectful/actions?query=branch%3Amaster)
[![Build Status](https://github.com/scrive/hedis-effectful/actions/workflows/haskell-ci.yml/badge.svg?branch=master)](https://github.com/scrive/hedis-effectful/actions?query=branch%3Amaster)
[![Hackage](https://img.shields.io/hackage/v/hedis-effectful.svg)](https://hackage.haskell.org/package/hedis-effectful)
[![Dependencies](https://img.shields.io/hackage-deps/v/hedis-effectful.svg)](https://packdeps.haskellers.com/[email protected])
[![Stackage LTS](https://www.stackage.org/package/hedis-effectful/badge/lts)](https://www.stackage.org/lts/package/hedis-effectful)
Expand Down
5 changes: 5 additions & 0 deletions cabal.haskell-ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
branches: master

doctest: <9.9
tests: True
benchmarks: True
15 changes: 9 additions & 6 deletions hedis-effectful.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,37 @@ extra-source-files:
CHANGELOG.md
README.md

tested-with:
GHC ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.7 || ==9.6.3
tested-with: GHC == { 8.10.7, 9.0.2, 9.2.8, 9.4.8, 9.6.5, 9.8.2 }

bug-reports: https://github.com/scrive/hedis-effectful/issues
bug-reports: https://github.com/scrive/hedis-effectful/issues

source-repository head
type: git
location: https://github.com/scrive/hedis-effectful.git

common language
ghc-options: -Wall -Wcompat -Wno-unticked-promoted-constructors
ghc-options: -Wall -Wcompat -Wno-unticked-promoted-constructors
-Wprepositive-qualified-module

default-language: Haskell2010
default-extensions:
NoStarIsType
BangPatterns
ConstraintKinds
DataKinds
FlexibleContexts
ImportQualifiedPost
GADTs
LambdaCase
TypeFamilies
TypeOperators

library
import: language
build-depends:
, base >=4.13 && <5
, base <5
, effectful-core >=1.0.0.0 && <3.0.0.0
, hedis ^>=0.15
, hedis >=0.15

hs-source-dirs: src
exposed-modules: Effectful.Redis
22 changes: 10 additions & 12 deletions src/Effectful/Redis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,30 @@
-- | Access to a Redis database via 'MonadRedis'.
module Effectful.Redis
( -- * Effect
Redis
Redis (..)

-- * Handler
, runRedis
)
where

import qualified Database.Redis as R
import Database.Redis qualified as R
import Effectful
import Effectful.Dispatch.Static
import Effectful.Dispatch.Dynamic

-- | Provide the ability to use the 'R.MonadRedis' instance of 'Eff'.
data Redis :: Effect
data Redis :: Effect where
LiftRedis :: R.Redis a -> Redis m a

type instance DispatchOf Redis = Static WithSideEffects

newtype instance StaticRep Redis = Redis R.Connection
type instance DispatchOf Redis = Dynamic

-- | Run the redis effect.
runRedis :: (IOE :> es) => R.Connection -> Eff (Redis : es) a -> Eff es a
runRedis = evalStaticRep . Redis
runRedis conn = interpret $ \_ -> \case
LiftRedis action -> liftIO $ R.runRedis conn action

----------------------------------------
-- Orphan instance

instance (IOE :> es, Redis :> es) => R.MonadRedis (Eff es) where
liftRedis redis = do
Redis connection <- getStaticRep
liftIO $ R.runRedis connection redis
instance (Redis :> es) => R.MonadRedis (Eff es) where
liftRedis = send . LiftRedis

0 comments on commit 072cc85

Please sign in to comment.