-
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.
Effects for finite and infinite supplies (#2780)
This pr refactors the `Input` effect. It is now meant to be used with finite input lists. It also introduces the `StreamOf` effect, which is meant to be used for infinite supplies. Both have static implementations so they should add negligible overhead when used.
- Loading branch information
1 parent
d59d02c
commit e9a7e94
Showing
7 changed files
with
90 additions
and
61 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
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
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 |
---|---|---|
@@ -1,37 +1,38 @@ | ||
{-# OPTIONS_GHC -Wno-unused-type-patterns #-} | ||
module Juvix.Prelude.Effects.Input | ||
( Input, | ||
input, | ||
inputJust, | ||
peekInput, | ||
runInputList, | ||
) | ||
where | ||
|
||
module Juvix.Prelude.Effects.Input where | ||
|
||
import Data.Stream qualified as Stream | ||
import Juvix.Prelude.Base.Foundation | ||
import Juvix.Prelude.Effects.Base | ||
import Juvix.Prelude.Stream | ||
|
||
-- TODO make static versions. Finite and infinite. | ||
data Input (i :: GHCType) :: Effect where | ||
Input :: Input i m i | ||
|
||
makeEffect ''Input | ||
|
||
runInputList :: forall i r a. [i] -> Sem (Input (Maybe i) ': r) a -> Sem r a | ||
runInputList s = reinterpret (evalState s) $ \case | ||
Input -> do | ||
x <- gets @[i] nonEmpty | ||
case x of | ||
Nothing -> return Nothing | ||
Just (a :| as) -> do | ||
put as | ||
return (Just a) | ||
|
||
runInputStream :: forall i r a. Stream i -> Sem (Input i ': r) a -> Sem r a | ||
runInputStream s = reinterpret (evalState s) $ \case | ||
Input -> do | ||
Stream.Cons a as <- get @(Stream i) | ||
put as | ||
return a | ||
|
||
runInputNaturals :: Sem (Input Natural ': r) a -> Sem r a | ||
runInputNaturals = runInputStream allNaturals | ||
|
||
inputJust :: (Members '[Input (Maybe i)] r) => Sem r i | ||
import Safe | ||
|
||
data Input (i :: GHCType) :: Effect | ||
|
||
type instance DispatchOf (Input _) = 'Static 'NoSideEffects | ||
|
||
newtype instance StaticRep (Input i) = Input | ||
{ _unInput :: [i] | ||
} | ||
|
||
input :: (Member (Input i) r) => Sem r (Maybe i) | ||
input = | ||
stateStaticRep $ | ||
\case | ||
Input [] -> (Nothing, Input []) | ||
Input (i : is) -> (Just i, Input is) | ||
|
||
peekInput :: (Member (Input i) r) => Sem r (Maybe i) | ||
peekInput = do | ||
Input l <- getStaticRep | ||
return (headMay l) | ||
|
||
runInputList :: [i] -> Sem (Input i ': r) a -> Sem r a | ||
runInputList = evalStaticRep . Input | ||
|
||
inputJust :: (Members '[Input i] r) => Sem r i | ||
inputJust = fromMaybe (error "inputJust") <$> input |
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,30 @@ | ||
module Juvix.Prelude.Effects.StreamOf | ||
( StreamOf, | ||
yield, | ||
runStreamOf, | ||
runStreamOfNaturals, | ||
) | ||
where | ||
|
||
import Data.Stream | ||
import Juvix.Prelude.Base.Foundation | ||
import Juvix.Prelude.Effects.Base | ||
import Juvix.Prelude.Stream | ||
|
||
data StreamOf (i :: GHCType) :: Effect | ||
|
||
type instance DispatchOf (StreamOf _) = 'Static 'NoSideEffects | ||
|
||
newtype instance StaticRep (StreamOf i) = StreamOf | ||
{ _unStreamOf :: Stream i | ||
} | ||
|
||
yield :: (Member (StreamOf i) r) => Sem r i | ||
yield = stateStaticRep $ \case | ||
StreamOf (Cons i is) -> (i, StreamOf is) | ||
|
||
runStreamOf :: Stream i -> Sem (StreamOf i ': r) a -> Sem r a | ||
runStreamOf = evalStaticRep . StreamOf | ||
|
||
runStreamOfNaturals :: Sem (StreamOf Natural ': r) a -> Sem r a | ||
runStreamOfNaturals = runStreamOf allNaturals |