News aggregator

Haskell intuition

Haskell on Reddit - Fri, 05/10/2013 - 3:15pm

Hi All,

I'm writing my first greenfield project in Haskell having been tinkering for quite a while. It's become apparent that I don't have a good intuition for "haskell in the large" (and this is a feeling I've seen expressed before by people new to the language). If it's OK I'd like to ask some general big picture haskell application design questions here in the hope that they will help myself and others.

Here's what I'm writing for some context:

I want to write a client library for my excellent online small business accounting package, freeagent (which is an oauth2 web service). Eventually I'd like to make it into a bridging library between freeagent and hledger. I'll need the following:

  • Oauth2 library and http client. I've gone for hoauth2 and http-streams. I'm not set in stone here; any pointers?

  • A library to manage a ~/.hfreeagent config file. The config file will need to manage and store the oauth credentials (id, secret, hostname etc), the location of ledger files, and token management (oauth2 tokens need to be periodically refreshed). ConfigFile or bos' library look like good options here.

  • A rest client which marshals json objects to haskell and thence to however hledger represents things. Aeson looks good for this one.

So far so good!

So the questions then:

  • What should my monad stack look like? The presence of the config suggests a ReaderT somewhere. But I'll also need to write to the config file when I refresh a token, so do I need StateT? Or do I need two separate interfaces? I understand monads and transformers, but I now realise that I have no idea how to design them.

  • Any pointers on structuring the library (directory structure, module naming etc).

  • Is there anything that could generate my types from json for me? Something like F#'s type providers, template haskell or some clever lensing. Or do I have to go and manually define all of the types in the API that I want to use: https://dev.freeagent.com/docs?

  • How would you model the workflow of looking up the token, checking if it's still valid, refreshing it if not and then making it available to the rest client? (this goes back to the monad question I guess).

I think that will do for now. The library will be open source https://github.com/perurbis/hfreeagent, and I'd like for it to serve as a roadmap if possible for folks moving from playing with and liking Haskell to actually thinking about and building real world stuff with it. This is the reason I'm asking here, rather than SO so people can follow along - and it doesn't get closed as too general :-). I will attempt to roll all of the good stuff from the conversation here - if any into the documentation.

Thanks all, Ben

submitted by b00thead
[link] [23 comments]
Categories: Incoming News

Typeclass with an ‘or’ restriction.

haskell-cafe - Fri, 05/10/2013 - 2:58pm
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Greetings, We can currently do something like This means that our `a' has to be an instance of Num and Eq. Apologies for a bit of an artificial example. Is there a way however to do something along the lines of: This would allow us to make an instance of Num be an instance of Foo or an instance of Eq to be an instance of Foo. The compiler currently complains about multiple declarations. Is there currently a way to achieve this? The main issue I can see with this is that given an instance of both, Num and Eq, it wouldn't be possible to pick the correct default implementation. Purely a theoretical question. - -- Mateusz K. -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) iQIcBAEBAgAGBQJRjP0hAAoJEM1mucMq2pqX30wP/0d0TQHs4S3G5TBw+T1baI6n g/5k/YlPmSgS3FaO8JMQsb2uqL8dGPZGUN7d2ohwcigtXS88KWH4u4rTjrs1+p8o ktS+vIE4kEEedTAX6wmP2Zn+rvK2zFGboCafaX/a/IxT5CbwYZ97RrWCjzz1jlPs S/VlhNHcTQ7Cf/0pa0xJ1kbao+vBHiWtWjxcdzCT/6zS86+jm9vz8qrYT3TWUd3y AJXPBjRuXeyz+
Categories: Offsite Discussion

www.glc.us.es

del.icio.us/haskell - Fri, 05/10/2013 - 11:32am
Categories: Offsite Blogs

A use case for *real* existential types

haskell-cafe - Fri, 05/10/2013 - 11:17am
I've been working on a new Haskell interface to the linux kernel's inotify system, which allows applications to subscribe to and be notified of filesystem events. An application first issues a system call that returns a file descriptor that notification events can be read from, and then issues further system calls to watch particular paths for events. These return a watch descriptor (which is just an integer) that can be used to unsubscribe from those events. Now, of course an application can open multiple inotify descriptors, and when removing watch descriptors, you want to remove it from the same inotify descriptor that contains it; otherwise you run the risk of deleting a completely different watch descriptor. So the natural question here is if we can employ the type system to enforce this correspondence. Phantom types immediately come to mind, as this problem is almost the same as ensuring that STRefs are only ever used in a single ST computation. The twist is that the inotify interface h
Categories: Offsite Discussion

Give me feedback on my first attempt at Template Haskell

Haskell on Reddit - Fri, 05/10/2013 - 9:50am

I'm trying to find my way around Template Haskell, so after discovering that all the liftA* functions can be built from just pure and <*> like this:

fp a b = a . <*> . b liftA = fp id pure liftA2 = (fp . fp) id pure liftA3 = (fp . fp . fp) id pure

et cetera, I decided to make a liftA* function generator in template haskell. The code is here - it defines functions named liftA', liftA2', etc.

I found it difficult to figure out when to use functions like varT, appT, etc., and when to use the data constructors VarT, AppT, etc. The version I have now almost exclusively uses the lowercase functions that operate inside the Q monad but my code went through several iterations to get there and I'm not sure that's the right choice.

I'd appreciate people with TH experience taking a look and commenting on my approach to the extent that it's possible to do so with such a small code sample.

submitted by fizbin
[link] [2 comments]
Categories: Incoming News

Control.Monad proposal: Add whenJust

libraries list - Fri, 05/10/2013 - 7:13am
I would like to propose the addition of whenJust :: Monad m => Maybe a -> (a -> m ()) -> m () whenJust (Just x) f = f x whenJust _ _ = return () to Control.Monad, in the section "Conditional execution of monadic expressions" next to guard :: MonadPlus m => Bool -> m () when :: Monad m => Bool -> m () -> m () unless :: Monad m => Bool -> m () -> m () Why? It would allow us to write more readable code and fit well into the group of similar functions of this style. Compare mUser <- lookupUser whenJust mUser email or whenJust mUser $ \user -> do putStrLn "Mailing!" email user with some currently available alternatives: case mUser of Just user -> do putStrLn "Mailing!" email user Nothing -> return () (Default base case clutter.) import Data.Foldable forM_ mUser $ \user -> do putStrLn "Mailing!" email user (Not too intuitive/well-named here and "Ambiguous occurrence forM_" clash with Control.Monad.)
Categories: Offsite Discussion

Reinventing a solution to configuration problem

haskell-cafe - Fri, 05/10/2013 - 6:45am
Hello Cafe, I came up with (actually reinvented [1]) a solution to configuration problem. For example configuration: data Config = Config { port :: Int, verbosity :: Int, logfile :: FilePath } deriving Show the basic idea is to use implicit parameters: runServer :: (?config :: Config) => IO () The problem is that it is tiresome to decorate all functions in this way. It is also non composable. For example if we switch to using two separate configuration entities like this: data LogConfig = LogConfig { verbosity :: Int, logfile :: FilePath } data NetConfig = NetConfig { port :: Int } we suddenly have to fix types all over the place. Yuck. The extended idea here (compared to [2]) is to use type synonym with all required parameters. This is simple (but requires at least Rank2Types): type ConfIO a = (?config :: Config) => IO a All the functions requiring config will now be simply in ConfIO monad which is really IO with a bonus. A great feature of this solution is that we don't need to use liftIO anywher
Categories: Offsite Discussion

Flip around type parameters?

haskell-cafe - Fri, 05/10/2013 - 4:39am
Hi. Does Haskell allow you to flip around type parameters somehow? I was playing around with toy code (still learning the fundamentals) and I came up with a class like this: code: -------- class Rotatable2D a where rotate :: (Num b) => (a b) -> b -> (a b) -------- It was easy to make an instance of a generic single-parameter type: code: -------- data Angle a = Angle a deriving (Show) instance Rotatable2D Angle where rotate (Angle a) b = Angle (a + b) -------- But let's say I have something a little more complicated: code: -------- data CircAppr a b = CircAppr a a b -- radius, rotation angle, number of points -------- I think I need something like so: -------- instance Rotatable2D (\x -> CircAppr x a) where rotate (CircAppr a b c) d = CircAppr a (b + d) c -------- But I tried that and it isn't valid syntax.
Categories: Offsite Discussion

Philip Wadler: Eschersketch

Planet Haskell - Fri, 05/10/2013 - 3:30am
Categories: Offsite Blogs

www.fpcomplete.com

del.icio.us/haskell - Thu, 05/09/2013 - 11:36pm
Categories: Offsite Blogs

How do you do concurrency in Haskell

Haskell on Reddit - Thu, 05/09/2013 - 10:45pm

I'm curious to know what the most common approach to concurrency is. Do you:

  1. Use threads with MVars for synchronizing and passing data?
  2. Use Software Transactional Memory (STM)?
  3. Use event-based concurrency?
  4. Use some library that implements Actors?
  5. Use Cloud Haskell?
  6. Something else entirely?
submitted by egonSchiele
[link] [29 comments]
Categories: Incoming News

Stream instance for Parsec + conduits

haskell-cafe - Thu, 05/09/2013 - 6:57pm
Hi all. I would like to have a Parsec Stream instance for Data.Text streams in yesod's ConduitM. So far, I have this: hpaste.org/87599 The idea is that because Yesod's conduits will be chunking values in Data.Text, I should have a wrapper StreamSource to cache chunked values. For some reason, the test parser fails, saying: [Left "Blah" (line 1, column 1): unexpected "g" expecting "h" or "g"] Any ideas? Cheers! Phil _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Categories: Offsite Discussion

PolymorphicDynamic – GHC

del.icio.us/haskell - Thu, 05/09/2013 - 4:56pm
Categories: Offsite Blogs

Interfacing Java/Haskell

haskell-cafe - Thu, 05/09/2013 - 4:04pm
Hi all, I am rather new to the café, so I just hope my question fits in. Does anyone have experience with integrating Haskell and Java? I have done some searching, finding a lot of pointers but hardly anything in terms of evaluation, successes, or caveats. From what I see Frege looks promising, arguably not haskell I suppose, but does it work? Other projects I have seen appear to have reached a stand-still for ages. The background for the question is that I will contribute some control algorithms based on machine learning or AI in a larger project. It would save me a lot of time if I could write in Haskell, but only assuming that interfacing with Java afterwards is trivial compared to writing everything in java in the first place. I am, perhaps, particularly worried that a Haskell-lookalike for JVM might be unable to optimise properly, like not being lazy. Any advice? TIA
Categories: Offsite Discussion

Help Finding a Recent Post on /r/haskell

Haskell on Reddit - Thu, 05/09/2013 - 2:19pm

I believe I read the post in this subreddit, and it was a recent post from the front page as well, but I cannot find either the post or the thread that contained it, and it was an incredibly useful comment.

I was going to show it to some more people today, but couldn't find the link.

The comment compared the metaprogramming facilities in lisp to that of haskell.

Template Haskell was mentioned as well as 4 or 5 other specifications about how haskell can accomplish already most of the use cases of lisp macros.

The post concluded that Haskell either already can or is headed in the direction of accomplishing every use case except for one, and the use case was one that was very generic to lisp.

The post was neutral in tone and written in a detailed manner.

It was about a page long iirc and used parentheses around every digit corresponding to each use case for the lisp facilities.

I think it had a score of around 20 or 30 at the time I read it, but I am more fuzzy on that point.

It was in exchange with a more lisp centric individual who was also neutral in tone in the responses.

Searching the subreddit for posts or threads that included template haskell and macros or lisp macros or metaprogramming did not turn up what I am looking for.

I would be really appreciative for any help locating this as it was the best info I've seen on the topic.

submitted by MoreHugs
[link] [3 comments]
Categories: Incoming News

Error compiling transformers-base-0.4.1 (debian 64bits, ghc-7.4.2)

haskell-cafe - Thu, 05/09/2013 - 1:47pm
Hello Café I am running into problems when installing transformers-base-0.4.1. Has anyone an idea about what is going on? REM : this is a "brand new" debian install, I have just install ghc, cabal install cabal-install etc. Thank you very much J-C cabal install transformers-base Resolving dependencies... Downloading transformers-base-0.4.1... Configuring transformers-base-0.4.1... Preprocessing library transformers-base-0.4.1... Building transformers-base-0.4.1... [1 of 1] Compiling Control.Monad.Base ( src/Control/Monad/Base.hs, dist/build/Control/Monad/Base.o ) src/Control/Monad/Base.hs:63:10: Duplicate instance declarations: instance Applicative (L.ST s) -- Defined at src/Control/Monad/Base.hs:63:10 instance Applicative (L.ST s) -- Defined in `Control.Applicative' src/Control/Monad/Base.hs:67:10: Duplicate instance declarations: instance Applicative (S.ST s) -- Defined at src/Control/Monad/Base.hs:67:10 instance Applicative (S.ST s) -- Defined in
Categories: Offsite Discussion