Skip to content

Instantly share code, notes, and snippets.

@gelisam gelisam/Main.hs
Last active Oct 5, 2017

Embed
What would you like to do?
Demonstrating how to use the Haxl library.
{-# LANGUAGE DeriveDataTypeable, GADTs, MultiParamTypeClasses, OverloadedStrings, StandaloneDeriving, TypeFamilies #-}
import Control.Monad
import Data.Hashable
import Data.Typeable
import Haxl.Core
data DeepThought a where
AnswerToLifeTheUniverseAndEverything :: DeepThought Int
deriving Typeable
deriving instance Show (DeepThought a)
deriving instance Eq (DeepThought a)
runDeepThought :: DeepThought a -> ResultVar a -> IO ()
runDeepThought AnswerToLifeTheUniverseAndEverything var = putSuccess var 42
instance DataSourceName DeepThought where
dataSourceName _ = "DeepThought"
instance Show1 DeepThought where
show1 AnswerToLifeTheUniverseAndEverything = "AnswerToLifeTheUniverseAndEverything"
instance Hashable (DeepThought a) where
hashWithSalt salt AnswerToLifeTheUniverseAndEverything = hashWithSalt salt ()
instance StateKey DeepThought where
data State DeepThought = NoState
instance DataSource () DeepThought where
fetch _ _ _ reqs = SyncFetch $ do
forM_ reqs $ \(BlockedFetch req var) -> runDeepThought req var
initialState :: StateStore
initialState = stateSet NoState stateEmpty
-- |
-- >>> main
-- 42
main :: IO ()
main = do
myEnv <- initEnv initialState ()
r <- runHaxl myEnv (dataFetch AnswerToLifeTheUniverseAndEverything)
print r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.