3 ms·
When I tried haskell I wanted to use wreq library to do some simple post and get requests. I took a look at docs and found the function definition for a post r
by imtringued 9y ago
When I tried haskell I wanted to use wreq library to do some simple post and get requests.
I took a look at docs and found the function definition for a post request.
https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Network-Wreq.html#v:post https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Networ...
post :: Postable a => String -> a -> IO (Response ByteString)
We can see that the second parameter should be an instance of the Postable typeclass.
The examples mention using a JSON value as a parameter
>>> r <- post "http://httpbin.org/post" (toJSON [1,2,3])
By looking at the definition of toJSON definition we know that toJSON returns a value
https://hackage.haskell.org/package/aeson-1.2.1.0/docs/Data-Aeson-Types.html#v:toJSON https://hackage.haskell.org/package/aeson-1.2.1.0/docs/Data-...
Just seeing a single example isn't enough for me to generalise though.
I proceed to go to the definition of the Postable typeclass.
https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Network-Wreq-Types.html#t:Postable https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Networ...
Unfortunately it doesn't list the instances of the typeclass so I decide to use hoogle.
https://www.haskell.org/hoogle/?hoogle=Postable https://www.haskell.org/hoogle/?hoogle=Postable
No results found.
I then start looking at the source code of the library until I found this file which contains the Postable instance for json values.
https://hackage.haskell.org/package/wreq-0.5.1.0/docs/src/Network-Wreq-Types.html https://hackage.haskell.org/package/wreq-0.5.1.0/docs/src/Ne...
import Data.Aeson (Value, encode)
instance Postable Value where
postPayload = putPayload
Why is it so difficult to do a simple task like finding all instances of a typeclass? In a Java IDE finding the implementations of an interface is just a single button away and fully automated. Of course I'm assuming that you've already imported the library that contains the typeclass instance. In this case the instances are part of the wreq library themselves and not of aeson or some plugin that you use to connect wreq and aeson. If hoogle worked as I think it should it could even be superior to a local search that only considers libraries that are part of your project. I'm a big fan of self documentation and like the type-aware documentation but this is a case where although it took a short time I've wasted more time than I'm comfortable with for a simple program that could be written in python or whatever instead.
I don't care about category theory at all and you can have all the practical benefits of category theory without putting it on a pedestial. It's neat to write more generic functions and stuff but there is no need to glorify it.
From a practical standpoint monoid, monad and functor you can think of them as an interface. When your code accepts a specific implementation like ArrayList you can in some cases instead use the java interface List to make your code more generic. When your code accepts a specific implementation like Maybe or List then sometimes you can use the Monad typeclass to make your code more generic.
- tikhonj 9y agoYou can find all the instances of a typeclass by loading all your libraries into GHCi and then using the :i command: λ> :i Monoid class Monoid a where mempty :: a mappend :: a -> a -> a mconcat :: [a] -> a {-# MINIMAL mempty, mappend #-} -- Defined in ‘GHC.Base’ instance [safe] (Monoid a, Monoid b) => Monoid (a :<|> b) -- Defined in ‘Servant.API.Alternative’ instance [safe] Applicative f => Monoid (Traversed a f) -- Defined in ‘Control.Lens.Internal.Fold’ instance [safe] Monad m => Monoid (Sequenced a m) -- Defined in ‘Control.Lens.Internal.Fold’ ... etc ...
- tome 9y agoI don't think that will work in this case. After all, that's essentially what Haddock does to generate its pages. The problem is that the Postable instances are orphans, for some reason which eludes me.
- tome 9y ago> I don't care about category theory at all and you can have all the practical benefits of category theory without putting it on a pedestial. It's neat to write more generic functions and stuff but there is no need to glorify it. Agreed. Here's part of a recent email correspondence between me and a fairly prominent functional programmer who's written at least one celebrated paper: > > I didn't want anyone to get the impression that they had to understand "category theory" or "maths" in order to use Haskell. This is a misconception about Haskell that is sadly far too prevalent in my opinion. > I agree. That being said, I haven't seen anyone in this discussion glorifying category theory, unless I missed something?
- tome 9y agoDear fellow Haskellers who care about the usability of our language, imtringued has raised an important point here. His/her comment is quite long but the summary is: Postable is exported here https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Network-Wreq.html#v:post https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Networ... but defined in some internal module. Its instances are technically orphans because they are defined directly in the module that Postable is reexported from. That means that the documentation for them is detached from the documentation for the class. https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Network-Wreq-Types.html#v:-36-fFormValueDouble https://hackage.haskell.org/package/wreq-0.5.1.0/docs/Networ... This is confusing and Haddock should be improved to address this.