State effect example - Polysemy

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Sridhar Ratnakumar

I'm trying to migrate my code from Control.Monad.Free to polysemy; does anyone have an example that shows how to use the State effect in my own effect?

Sridhar Ratnakumar

This compiles:

data ActivityLang m a where
  SetNote' :: Text -> ActivityLang m ()

makeSem ''ActivityLang

runActivityPure :: Sem (ActivityLang ': r) a -> Sem r a
runActivityPure = interpret $ \case
  SetNote' s -> pure ()  -- TODO: modify the reader env using `s`

runFoo :: Sem (ActivityLang : State (Entry' Covered Maybe) : r) a -> Sem r _
runFoo = runState initialEntry . runActivityPure

But how would you compose the two effects so I can use the reader in SetNote'?

Sridhar Ratnakumar

(Correction: State, not Reader)

Sridhar Ratnakumar

End goal: "ActivityLang" describes the DSL to ultimately build (piecemeal) a Haskell record . So it is pure, except for using a State to represent intermediate states of the record (represented using barbie HKD)


@Sridhar Ratnakumar try reinterpret:

runAL :: Sem (ActivityLang : r) a -> Sem (State (Entry' Covered Maybe) : r) a
runAL = reinterpret \case
  SetNote' s -> useStateWith s
Sridhar Ratnakumar

That worked, and I've successfully migrated to polysemy!