A software engineer website

Polysemy: Introduction to Effect definition

Gautier DI FOLCO December 07, 2022 [Haskell] #haskell #polysemy #design #effects systems

In the first log we explained that in the expression:

displayFile :: FilePath -> Sem '[Trace, Embed IO] Int
displayFile path = do
  trace $ "Displaying " <> path
  content <- embed $ readFile path
  embed $ putStr content
  return $ length content

trace is emiting an effect of Trace.

trace has the following type:

trace :: forall r. Member Trace r => String -> Sem r ()

and is generated via:

{-# LANGUAGE TemplateHaskell #-}

module EffectDefinitionIntro
  ( Trace (..),

import Data.Kind
import Polysemy

data Trace (m :: Type -> Type) a where
  Trace :: String -> Trace m ()

makeSem ''Trace

makeSem will generate something like:

trace :: forall r_a44O. Member Trace r_a44O => String -> Sem r_a44O ()
trace a = emit $ Trace a

So, whenever trace is called, the value is sent to the interpreter.

Note: to keep it short m will be replaced by Sem r during interpretation

See the full the code here.