Skip to content

Instantly share code, notes, and snippets.

@gelisam
Last active October 5, 2017 10:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gelisam/0549eb2a292f86ca2574 to your computer and use it in GitHub Desktop.
Save gelisam/0549eb2a292f86ca2574 to your computer and use it in GitHub Desktop.
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