public
Last active

Algebraic Effects in Haskell

  • Download Gist
gistfile1.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 33 34 35 36 37 38 39 40 41 42 43 44 45
 
{-# OPTIONS -Wall #-}
 
{-# LANGUAGE GADTs, DataKinds, TemplateHaskell, KindSignatures,
MultiParamTypeClasses, RankNTypes, PolyKinds, FlexibleContexts,
TypeFamilies, UndecidableInstances, FlexibleInstances #-}
 
module Effects where
 
import Data.Singletons
 
-- kind Effect = * > * -> * -> *
$(singletons [d|
data Effect res res' t = Effect res res' t
 
|])
 
class Handler (e :: * -> * -> * -> *) (m :: * -> *) where
handle :: res -> e res res' t -> (res' -> t -> m a) -> m a
 
data EFFECT :: * where
MkEff :: (res :: *) -> SEffect e -> EFFECT
 
{- Effect -}
data State :: * -> * -> * -> * where
Get :: State a a a
Put :: b -> State a b ()
 
instance Handler State m where
handle n Get k = k n n
handle _ (Put n') k = k n' ()
 
-- everything get's a bit funky when i try to figure out EFFECT
type STATE t = MkEff t State
-- Effects.hs:35:16:
-- `MkEff' of type `forall (k :: BOX)
-- (k :: BOX)
-- (k :: BOX)
-- res
-- (e :: Effect k k k).
-- res -> SEffect k k k e -> EFFECT' is not promotable
-- In the type `MkEff t State'
-- In the type declaration for `STATE'
--

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.