@@ -17,6 +17,7 @@ import Data.Dependent.Map (DMap, DSum (..), GCompare (..), GOrdering (..))
17import qualified Data.Dependent.Map as DMap
17import qualified Data.Dependent.Map as DMap
18import Data.Functor.Misc
18import Data.Functor.Misc
19import Data.Semigroup
19import Data.Semigroup
20+import Data.Traversable
21import Debug.Trace (trace)
22import Debug.Trace (trace)
@@ -82,12 +83,27 @@ ffor = flip fmap
82instance Reflex t => Functor (Behavior t) where
83instance Reflex t => Functor (Behavior t) where
83 fmap f = pull . liftM f . sample
84 fmap f = pull . liftM f . sample
85−--TODO: Rewrite rules for Applicative
86instance Reflex t => Applicative (Behavior t) where
86instance Reflex t => Applicative (Behavior t) where
87 pure = constant
87 pure = constant
88− f <*> x = pull $ sample f <*> sample x
88+ f <*> x = pull $ sample f `ap` sample x
89− a *> b = b
89+ _ *> b = b
90− a <* b = a
90+ a <* _ = a
92+instance Reflex t => Monad (Behavior t) where
93+ a >>= f = pull $ sample a >>= sample . f
94+ -- Note: it is tempting to write (_ >> b = b); however, this would result in (fail x >> return y) succeeding (returning y), which violates the law that (a >> b = a >>= \_ -> b), since the implementation of (>>=) above actually will fail. Since we can't examine Behaviors other than by using sample, I don't think it's possible to write (>>) to be more efficient than the (>>=) above.
96+ fail = error "Monad (Behavior t) does not support fail"
98+instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
99+ a <> b = pull $ liftM2 (<>) (sample a) (sample b)
100+ sconcat = pull . liftM sconcat . mapM sample
101+ times1p n = fmap $ times1p n
103+instance (Reflex t, Monoid a) => Monoid (Behavior t a) where
104+ mempty = constant mempty
105+ mappend a b = pull $ liftM2 mappend (sample a) (sample b)
106+ mconcat = pull . liftM mconcat . mapM sample
92--TODO: See if there's a better class in the standard libraries already
108--TODO: See if there's a better class in the standard libraries already
93-- | A class for values that combines filtering and mapping using 'Maybe'.
109-- | A class for values that combines filtering and mapping using 'Maybe'.