-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This pr replaces the `Writer` effect with a more fitting `Output` effect in the Juvix Tree evaluator.
- Loading branch information
1 parent
da67611
commit d09f152
Showing
5 changed files
with
91 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Juvix.Prelude.Effects | ||
( module Juvix.Prelude.Effects.Output, | ||
module Juvix.Prelude.Effects.Base, | ||
) | ||
where | ||
|
||
import Juvix.Prelude.Effects.Base | ||
import Juvix.Prelude.Effects.Output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Juvix.Prelude.Effects.Accum where | ||
|
||
import Data.Kind qualified as GHC | ||
import Juvix.Prelude.Base hiding (Effect, Output, output, runOutputList) | ||
import Juvix.Prelude.Effects.Base | ||
|
||
data Accum (o :: GHC.Type) :: Effect | ||
|
||
type instance DispatchOf (Accum _) = 'Static 'NoSideEffects | ||
|
||
newtype instance StaticRep (Accum o) = Accum | ||
{ _unAccum :: [o] | ||
} | ||
|
||
runAccumList :: Eff (Accum o ': r) a -> Eff r ([o], a) | ||
runAccumList m = do | ||
(a, Accum s) <- runStaticRep (Accum mempty) m | ||
return (reverse s, a) | ||
|
||
ignoreAccum :: Eff (Accum o ': r) a -> Eff r a | ||
ignoreAccum m = snd <$> runAccumList m | ||
|
||
accum :: (Accum o :> r) => o -> Eff r () | ||
accum o = overStaticRep (\(Accum l) -> Accum (o : l)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Juvix.Prelude.Effects.Base | ||
( module Juvix.Prelude.Effects.Base, | ||
module Effectful, | ||
module Effectful.Reader.Static, | ||
module Effectful.State.Static.Local, | ||
module Effectful.Error.Static, | ||
module Effectful.TH, | ||
module Effectful.Dispatch.Static, | ||
) | ||
where | ||
|
||
import Effectful | ||
import Effectful.Dispatch.Static | ||
import Effectful.Error.Static | ||
import Effectful.Internal.Env (getEnv, putEnv) | ||
import Effectful.Reader.Static | ||
import Effectful.State.Static.Local | ||
import Effectful.TH | ||
import Juvix.Prelude.Base (($), (<$>), type (~)) | ||
|
||
overStaticRep :: (DispatchOf e ~ 'Static sideEffects, e :> r) => (StaticRep e -> StaticRep e) -> Eff r () | ||
overStaticRep f = unsafeEff $ \r -> do | ||
e' <- f <$> getEnv r | ||
putEnv r e' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{-# OPTIONS_GHC -Wno-unused-type-patterns #-} | ||
|
||
module Juvix.Prelude.Effects.Output where | ||
|
||
import Data.Kind qualified as GHC | ||
import Effectful.Dispatch.Dynamic | ||
import Juvix.Prelude.Base hiding (Effect, Output, interpret, output, reinterpret, runOutputList) | ||
import Juvix.Prelude.Effects.Accum | ||
import Juvix.Prelude.Effects.Base | ||
|
||
data Output (o :: GHC.Type) :: Effect where | ||
Output :: o -> Output o m () | ||
|
||
makeEffect ''Output | ||
|
||
runOutputEff :: (o -> Eff r ()) -> Eff (Output o ': r) a -> Eff r a | ||
runOutputEff handle = | ||
interpret $ \_ -> \case | ||
Output x -> handle x | ||
|
||
runOutputList :: Eff (Output o ': r) a -> Eff r ([o], a) | ||
runOutputList = reinterpret runAccumList $ \_ -> \case | ||
Output x -> accum x | ||
|
||
ignoreOutput :: Eff (Output o ': r) a -> Eff r a | ||
ignoreOutput = interpret $ \_ -> \case | ||
Output {} -> return () |