I implemented my own Promise framework in C # and wanted to test the concept in Haskell, so after some serious brain training (still very new to this) I produced
data Promise fa = PendingPromise f | ResolvedPromise a | BrokenPromise deriving( Show ) class Future p where later :: (b -> c) -> p (a -> b) b -> p (a -> c) c resolve :: p (a -> b) a -> a -> p (a -> b) b instance Future Promise where later g (PendingPromise f) = PendingPromise (g . f) later g (ResolvedPromise a) = ResolvedPromise (ga) resolve (PendingPromise f) a = ResolvedPromise (fa)
Finding out how to write this Promise fa data type was a real pain.
In any case, the later method seems to be a kind of applicative functor, and Promises - monads. Is it possible to make a Promise instance of some class and get this functionality instead of implementing my own Future class?
Edit Thanks to @bheklilr, the later function turned out to be fmap masked with a slight data type override
data Promise ab = PendingPromise (a -> b) | ResolvedPromise b | BrokenPromise instance Functor (Promise c) where fmap f (PendingPromise g) = PendingPromise (f . g) fmap f (ResolvedPromise a) = ResolvedPromise (fa) fmap f BrokenPromise = BrokenPromise
Knowing that (Promise a) is a Functor, it is easier to understand why Monad.
source share