-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
103 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 was deleted.
Oops, something went wrong.
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,67 @@ | ||
{-# LANGUAGE RoleAnnotations #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Minipat.Dirt.Strip where | ||
|
||
import Control.Applicative (Alternative (..)) | ||
import Control.Exception (Exception, SomeException (..)) | ||
import Data.Coerce (coerce) | ||
import Data.Kind (Type) | ||
import Data.Proxy (Proxy (..)) | ||
import Data.Semigroup (Semigroup (..)) | ||
import Data.String (IsString (..)) | ||
import Data.Text (Text) | ||
import Minipat.Eval (evalPat) | ||
import Minipat.Parser (P) | ||
import Minipat.Stream (Stream) | ||
import Minipat.Stream qualified as S | ||
|
||
-- Attempting to add a few things to Streams | ||
-- 1) Tracking errors for later logging | ||
-- 2) IsString instance for seamless parsing | ||
newtype Strip (k :: k1) (a :: Type) = Strip {unStrip :: Either SomeException (Stream a)} | ||
deriving stock (Functor) | ||
|
||
type role Strip phantom nominal | ||
|
||
instance Applicative (Strip k) where | ||
pure = Strip . Right . pure | ||
liftA2 f (Strip ca) (Strip cb) = Strip (liftA2 (liftA2 f) ca cb) | ||
|
||
instance Semigroup (Strip k a) where | ||
Strip es1 <> Strip es2 = Strip (liftA2 (<>) es1 es2) | ||
sconcat = Strip . fmap sconcat . traverse unStrip | ||
|
||
instance Monoid (Strip k a) where | ||
mempty = Strip (Right mempty) | ||
mconcat = Strip . fmap mconcat . traverse unStrip | ||
|
||
instance Alternative (Strip k) where | ||
empty = mempty | ||
(<|>) = (<>) | ||
|
||
stripCast :: Strip k a -> Strip j a | ||
stripCast = coerce | ||
|
||
stripMap :: (Stream a -> Stream b) -> Strip k a -> Strip j b | ||
stripMap f (Strip c) = Strip (fmap f c) | ||
|
||
stripBind :: Strip k a -> (Stream a -> Strip j b) -> Strip j b | ||
stripBind (Strip c) f = Strip (c >>= unStrip . f) | ||
|
||
stripThrow :: (Exception e) => e -> Strip k a | ||
stripThrow = Strip . Left . SomeException | ||
|
||
stripFilter :: (a -> Bool) -> Strip k a -> Strip j a | ||
stripFilter = stripMap . S.streamFilter | ||
|
||
class StripParse k a | k -> a where | ||
stripParse :: Proxy k -> Text -> Strip k a | ||
|
||
instance (StripParse k a) => IsString (Strip k a) where | ||
fromString = stripParse (Proxy :: Proxy k) . fromString | ||
|
||
stripEval :: P a -> Text -> Strip k a | ||
stripEval ee txt = Strip (evalPat ee txt) | ||
|
||
-- instance Pattern (Strip k) where |
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,32 @@ | ||
module Minipat.Flow where | ||
|
||
import Control.Applicative (Alternative) | ||
import Data.Sequence (Seq) | ||
import Minipat.Pattern (Pattern) | ||
import Minipat.Stream | ||
( Stream | ||
, streamEarly | ||
, streamEarlyBy | ||
, streamFilter | ||
, streamLate | ||
, streamLateBy | ||
, streamPieces | ||
, streamSwitch | ||
) | ||
import Minipat.Time (CycleDelta, CycleTime) | ||
|
||
class (Alternative f, Pattern f) => Flow f where | ||
flowFilter :: (a -> Bool) -> f a -> f a | ||
flowEarlyBy, flowLateBy :: CycleDelta -> f a -> f a | ||
flowEarly, flowLate :: f CycleDelta -> f a -> f a | ||
flowSwitch :: f a -> CycleTime -> f a -> f a | ||
flowPieces :: f a -> Seq (CycleTime, f a) -> f a | ||
|
||
instance Flow Stream where | ||
flowFilter = streamFilter | ||
flowEarlyBy = streamEarlyBy | ||
flowLateBy = streamLateBy | ||
flowEarly = streamEarly | ||
flowLate = streamLate | ||
flowSwitch = streamSwitch | ||
flowPieces = streamPieces |
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