Polysemy: Higher-order effects introductionsGautier DI FOLCO January 08, 2023 [Haskell] #haskell #polysemy #design #effects systems
One thing that sets polysemy apart is that it allows defining Higher-order effects.
As higher-order functions, higher-order effects are effects defined in terms of other effects, let's try a simple one:
data When (m :: Type -> Type) a where WhenM :: m () -> When m () makeSem ''When
m () is the effect (it will be replaced by
Sem _ ()).
In order to properly interpret this effect, we can rely on tactics.
= interpretH $ \case WhenM act -> runTSimple act interpretWhenEmbed
Two elements have to be noticed:
interpretHgives a context to interpret higher-order effects
m ()to the previously mentionned context (
Sem rInitialand cannot be used directly)
finally, we can use it as any other interpreters:
runM $ interpretWhenEmbed $ whenM $ embed $ putStrLn "Hello,"
See the full the code here.