Skip to content

Instantly share code, notes, and snippets.

@graninas
Created September 13, 2023 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graninas/d3f4e001f549020ac9fb60ca50482957 to your computer and use it in GitHub Desktop.
Save graninas/d3f4e001f549020ac9fb60ca50482957 to your computer and use it in GitHub Desktop.
Some thoughts on merging Church Free and State monad
-- Option 1: State as an eDSL
data StateMethod s next
= Put s (() -> next)
| Get (s -> next)
instance Functor (StateMethod s) where
fmap f (Put st next) = Put st (f . next)
fmap f (Get next) = Get (f . next)
type FreePoweredState st a = F (StateMethod st) a
put :: s -> FreePoweredState s ()
put st = liftF (Put st id)
get :: FreePoweredState s s
get = liftF (Get id)
-- Option 2: State as a part of Church Free (may be incorrect):
newtype StateMergedChurch s f a = MyChurch
{ runMyChurch
:: forall r
. (s -> a -> (s, r)) -- Pure case
-> (s -> f r -> (s, r)) -- Continuation case
-> s -- initial state
-> (s, r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment