Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shadowed name warnings #14

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 23 additions & 21 deletions src/Data/Machine/Mealy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,12 @@ drop n m = if n <= 0 then m
in stepMealy s m >>= f

loop :: forall f s a. (Monad f) => MealyT f s a -> MealyT f s a
loop m =
let m0 = m
loop' m = mealy $ \s -> let f Halt = stepMealy s (loop m0)
f (Emit a m) = pure $ Emit a (loop' m)
in stepMealy s m >>= f
in loop' m

loop m0 = loop' m0
where
loop' m = mealy $ \s ->
stepMealy s m >>= case _ of
Halt -> stepMealy s (loop m0)
Emit a m' -> pure $ Emit a (loop' m')

zipWith :: forall f s a b c. (Monad f) => (a -> b -> c) -> MealyT f s a -> MealyT f s b -> MealyT f s c
zipWith f a b = f <$> a <*> b
Expand Down Expand Up @@ -134,21 +133,23 @@ msplit m = mealy $ \s -> f <$> stepMealy s m
f (Emit a m') = Emit (Just $ Tuple a m') (msplit m')

interleave :: forall f s a. (Monad f) => MealyT f s a -> MealyT f s a -> MealyT f s a
interleave m1 m2 = mealy $ \s -> let f Halt = stepMealy s m2
f (Emit a m1) = pure $ Emit a (interleave m2 m1)
in stepMealy s m1 >>= f
interleave m1 m2 = mealy $ \s ->
stepMealy s m1 >>= case _ of
Halt -> stepMealy s m2
Emit a m1' -> pure $ Emit a (interleave m2 m1')

once :: forall f s a. (Monad f) => MealyT f s a -> MealyT f s a
once = take 1

ifte :: forall f s a b. (Monad f) => MealyT f s a -> (a -> MealyT f s b) -> MealyT f s b -> MealyT f s b
ifte ma f mb = mealy $ \s -> let g Halt = stepMealy s mb
g (Emit a ma) = h ma <$> stepMealy s (f a)

h ma Halt = Halt
h ma (Emit b fb) = Emit b (fb <> ifte ma f mb)

in stepMealy s ma >>= g
ifte ma f mb = mealy $ \s ->
stepMealy s ma >>= case _ of
Halt -> stepMealy s mb
Emit a ma' -> go ma' <$> stepMealy s (f a)
where
go ma' = case _ of
Halt -> Halt
Emit b fb -> Emit b (fb <> ifte ma' f mb)

when :: forall f s a b. (Monad f) => MealyT f s a -> (a -> MealyT f s b) -> MealyT f s b
when ma f = ifte ma f halt
Expand All @@ -159,10 +160,11 @@ instance functorMealy :: (Monad f) => Functor (MealyT f s) where
g Halt = Halt

instance applyMealy :: (Monad f) => Apply (MealyT f s) where
apply f x = mealy $ \s -> let ap Halt _ = Halt
ap _ Halt = Halt
ap (Emit f f') (Emit x x') = Emit (f x) (f' <*> x')
in ap <$> stepMealy s f <*> stepMealy s x
apply f x = mealy $ \s -> ap <$> stepMealy s f <*> stepMealy s x
where
ap Halt _ = Halt
ap _ Halt = Halt
ap (Emit f' g) (Emit x' y) = Emit (f' x') (g <*> y)

instance applicativeMealy :: (Monad f) => Applicative (MealyT f s) where
pure t = pureMealy $ \s -> Emit t halt
Expand Down