Skip to content

Commit

Permalink
Implement DB example/template
Browse files Browse the repository at this point in the history
Thanks to Florian Beeres @cideM
  • Loading branch information
tomjaguarpaw committed Jun 17, 2024
1 parent 98f9076 commit 8bbda7c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ including
For an introduction to Bluefin, see the docs in the
[`Bluefin`](bluefin/src/Bluefin.hs) module.

## Examples

There is an `bluefin-examples` package which you can see in this
repository at
[`bluefin-examples/src/Bluefin/Examples`](bluefin-examples/src/Bluefin/Examples).

## Acknowledgements

Tom Ellis would like to thank many individuals for their work related
Expand Down
3 changes: 3 additions & 0 deletions bluefin-examples/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.0.0

* Implement DB example/template. Thanks to Florian Beeres @cideM.
25 changes: 25 additions & 0 deletions bluefin-examples/bluefin-examples.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cabal-version: 3.0
name: bluefin-examples
version: 0.0.0.0
license: MIT
license-file: LICENSE
author: Tom Ellis
maintainer: Tom Ellis
build-type: Simple
extra-doc-files: CHANGELOG.md
description: The Bluefin effect system, examples
synopsis: The Bluefin effect system, examples
homepage: https://github.com/tomjaguarpaw/bluefin
bug-reports: https://github.com/tomjaguarpaw/bluefin/issues

common warnings
ghc-options: -Wall

library
import: warnings
exposed-modules:
Bluefin.Examples.DB
build-depends:
base, bluefin >= 0.0.6.0 && < 0.1
hs-source-dirs: src
default-language: Haskell2010
58 changes: 58 additions & 0 deletions bluefin-examples/src/Bluefin/Examples/DB.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}

module Bluefin.Examples.DB where

import Bluefin.Compound (useImpl, useImplIn)
import Bluefin.Eff (Eff, (:&), (:>))
import qualified Bluefin.Eff as BF
import Bluefin.Exception (Exception)
import qualified Bluefin.Exception as BF
import Bluefin.IO (IOE)
import qualified Bluefin.IO as BF

newtype DbHandle = DbHandle String deriving (Show)

newtype UserId = UserId String deriving (Show, Eq)

newtype User = User String deriving (Show)

data DbEff es = MkDbEff
{ queryImpl :: DbHandle -> UserId -> Eff es User
}

query :: (e :> es) => DbEff e -> DbHandle -> UserId -> Eff es User
query db dbHandle userId = useImpl $ queryImpl db dbHandle userId

runDbEffIo ::
forall exEff dbEff es r.
(exEff :> es, dbEff :> es) =>
Exception String exEff ->
IOE dbEff ->
(forall e. DbEff e -> Eff (e :& es) r) ->
Eff es r
runDbEffIo ex _ fn =
useImplIn
fn
( MkDbEff
{ queryImpl = \_ userId -> do
if userId == UserId "1"
then pure $ User "Alice"
else BF.throw ex "not found"
}
)

main :: IO ()
main = do
let dbHandle = DbHandle "db"

result <- BF.runEff $ \io -> BF.try $ \ex ->
runDbEffIo ex io $ \db -> do
u1 <- query db dbHandle (UserId "1")
BF.effIO io $ print u1
u2 <- query db dbHandle (UserId "2")
BF.effIO io $ print u2

case result of
Left err -> print err
Right _ -> print "success"
1 change: 1 addition & 0 deletions cabal.project
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
bluefin/bluefin.cabal
bluefin-internal/bluefin-internal.cabal
bluefin-examples/bluefin-examples.cabal

0 comments on commit 8bbda7c

Please sign in to comment.