Can I lift this interestingly typed function into a Maybe?

Hi all, My issue is generic Haskell use, but I should provide the specific background. I have some Snap code: eres <- eitherWithDB $ DB.findOne (DB.select [] "someCollection") let maybeVal = either (error "Mongoed") (maybe Nothing fromBson) eres and this works so that "maybeVal" will be of type Maybe MyType. "eres" is either a MongoDB Failure, or a Maybe Document (I find one document, or not). I'd like to convert this Maybe Document to a Maybe MyType without unpacking and repacking the Maybe. Is this possible? The fromBson function, that converts from a Document to an end user type, confuses me due to it's type: fromBson :: Monadhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Mo... m => Documenthttp://hackage.haskell.org/packages/archive/bson/0.1.7/doc/html/Data-Bson.ht... -> m a Thanks, Si

On Thu, Jan 24, 2013 at 9:56 PM, Simon Peter Nicholls
I'd like to convert this Maybe Document to a Maybe MyType without unpacking and repacking the Maybe. Is this possible?
That would be exactly fmap :: (Document -> MyType) -> (Maybe Document -> Maybe MyType) where I've instantiated the type variables for your use case.
The fromBson function, that converts from a Document to an end user type, confuses me due to it's type:
fromBson :: Monadhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Mo... m => Documenthttp://hackage.haskell.org/packages/archive/bson/0.1.7/doc/html/Data-Bson.ht... -> m a
All the small, single letters are type variables. Because they are implicitly universally quantified; you, the caller, gets to specify what you want them to be. Written out in full, it's actually fromBson :: forall (m :: * -> *), a. Monad m => Document -> m a Here, again specializing for your use case, fromBson probably needs to be of type fromBson :: Document -> Maybe MyType So if you have a rightEres :: Maybe Document then fmap fromBson rightEres :: Maybe (Maybe MyType) which you could then join $ fmap fromBson rightEres :: Maybe MyType What you're really after is the more idiomatic rightEres >>= fromBson :: Maybe MyType HTH, -- Kim-Ee
Thanks, Si
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Ahhh, thank you. The following works:
let meal = either (error "Mongoed") (>>= fromBson) eres
On Thu, Jan 24, 2013 at 4:24 PM, Kim-Ee Yeoh
On Thu, Jan 24, 2013 at 9:56 PM, Simon Peter Nicholls < simon@mintsource.org> wrote:
I'd like to convert this Maybe Document to a Maybe MyType without unpacking and repacking the Maybe. Is this possible?
That would be exactly
fmap :: (Document -> MyType) -> (Maybe Document -> Maybe MyType)
where I've instantiated the type variables for your use case.
The fromBson function, that converts from a Document to an end user type, confuses me due to it's type:
fromBson :: Monadhttp://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/Control-Mo... m => Documenthttp://hackage.haskell.org/packages/archive/bson/0.1.7/doc/html/Data-Bson.ht... -> m a
All the small, single letters are type variables. Because they are implicitly universally quantified; you, the caller, gets to specify what you want them to be. Written out in full, it's actually
fromBson :: forall (m :: * -> *), a. Monad m => Document -> m a
Here, again specializing for your use case, fromBson probably needs to be of type
fromBson :: Document -> Maybe MyType
So if you have a rightEres :: Maybe Document
then
fmap fromBson rightEres :: Maybe (Maybe MyType)
which you could then
join $ fmap fromBson rightEres :: Maybe MyType
What you're really after is the more idiomatic
rightEres >>= fromBson :: Maybe MyType
HTH, -- Kim-Ee
Thanks, Si
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (2)
-
Kim-Ee Yeoh
-
Simon Peter Nicholls