-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |