Created
December 1, 2012 18:08
-
-
Save sjoerdvisscher/4183639 to your computer and use it in GitHub Desktop.
Use case for Free Applicatives
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
{-# LANGUAGE RankNTypes #-} | |
-- See http://gergo.erdi.hu/blog/2012-12-01-static_analysis_with_applicatives/ | |
import Control.Applicative | |
import Data.Functor.HFree | |
data MarketData a | |
data MarketKey | |
request :: MarketData a -> MarketKey | |
request = undefined | |
data Market | |
getMarketData :: Market -> MarketData a -> a | |
getMarketData = undefined | |
type P a = HFree Applicative MarketData a | |
eval :: Applicative g => (forall b. f b -> g b) -> HFree Applicative f a -> g a | |
eval = rightAdjunct | |
get :: MarketData a -> P a | |
get = liftFree | |
dependencies :: P a -> [MarketKey] | |
dependencies = getConst . eval (Const . (:[]) . request) | |
runP :: (Applicative m, Monad m) => ([MarketKey] -> m Market) -> P a -> m a | |
runP loadMarket pricer = do | |
let deps = dependencies pricer | |
mkt <- loadMarket deps | |
return $ iter (getMarketData mkt) pricer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment