public
Last active

Use case for Free Applicatives

  • Download Gist
gergoerdi.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
{-# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.