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

Applicative-Monad Proposal fixes. #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
17 changes: 16 additions & 1 deletion Flite/Fresh.hs
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
module Flite.Fresh where

import Control.Monad (replicateM)
import Control.Applicative (Applicative(..))
import Control.Monad (ap, liftM, replicateM)

data Fresh a = Fresh { runFresh :: String -> Int -> (Int, a) }

data FreshW a b = FreshW { runFreshW :: String -> Int -> ([a], Int, b) }

instance Functor (FreshW a) where
fmap = liftM

instance Applicative (FreshW a) where
pure = return
(<*>) = ap

instance Monad (FreshW a) where
return a = FreshW (\s i -> ([], i, a))
m >>= f = FreshW (\s i -> case runFreshW m s i of
(l, j, a) -> let (l', j', a') = runFreshW (f a) s j
in (l ++ l', j', a'))

instance Functor Fresh where
fmap = liftM

instance Applicative Fresh where
pure = return
(<*>) = ap

instance Monad Fresh where
return a = Fresh (\s i -> (i, a))
m >>= f = Fresh (\s i -> case runFresh m s i of
Expand Down
10 changes: 10 additions & 0 deletions Flite/Identity.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
module Flite.Identity where

import Control.Applicative (Applicative(..))

newtype Identity a = I { runIdentity :: a }

instance Functor Identity where
fmap f (I a) = I $ f a

instance Applicative Identity where
pure a = I a

(I f) <*> (I a) = I $ f a

instance Monad Identity where
return a = I a
I a >>= f = f a
8 changes: 8 additions & 0 deletions Flite/Predex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Flite.Predex where

import Data.List
import Flite.Syntax
import Control.Applicative (Applicative(..))
import Control.Monad
import Flite.Traversals
import qualified Flite.RedSyntax as R
Expand Down Expand Up @@ -61,6 +62,13 @@ checkArg scope e = False
-- transformations that are applied during a computation.
data Count a = Count { runCount :: Int -> (Int, a) }

instance Functor Count where
fmap = liftM

instance Applicative Count where
pure = return
(<*>) = ap

instance Monad Count where
return a = Count $ \n -> (n, a)
x >>= f = Count $ \n -> case runCount x n of (m, y) -> runCount (f y) m
Expand Down
10 changes: 10 additions & 0 deletions Flite/State.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
module Flite.State where

import Control.Applicative (Applicative(..))
import Control.Monad (ap, liftM)

newtype State s a = S { runState :: s -> (s, a) }

instance Functor (State s) where
fmap = liftM

instance Applicative (State s) where
pure = return
(<*>) = ap

instance Monad (State s) where
return a = S (\s -> (s, a))
m >>= f = S (\s -> case runState m s of
Expand Down
5 changes: 5 additions & 0 deletions Flite/WriterState.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Flite.WriterState where

import Control.Applicative (Applicative(..))
import Control.Monad

newtype WriterState w s a = WS { runWS :: s -> (s, [w], a) }
Expand All @@ -14,6 +15,10 @@ instance Monad (WriterState w s) where
instance Functor (WriterState w s) where
fmap = liftM

instance Applicative (WriterState w s) where
pure = return
(<*>) = ap

write :: w -> WriterState w s ()
write w = WS $ \s -> (s, [w], ())

Expand Down