News aggregator

Silk: Announcing rest - A Haskell REST framework

Planet Haskell - Mon, 06/30/2014 - 8:22am

We are excited to officially announce the open source release of our REST framework rest!

rest is a set of packages used to write, document, and use RESTful applications. You write your API in Haskell using rest's DSL. This API can then be run in different web frameworks like happstack, snap, or wai. Additionally, you can automatically generate documentation from it, as well as client libraries for Haskell and Javascript. We have been using it in production for most of our services for a long time and we like it so much that we decided to share it with the public.

If you want to start using rest, check out the tutorial or the example application. You can also come to the Haskell Exchange 2014 where Erik will give a talk about rest. We’d also be happy to answer any questions you have, shoot us an e-mail!

The most important packages are:

  • rest-core: A DSL for defining versioned and web server agnostic REST resources. This is the workhorse of the framework
  • rest-gen: Automatically generates documentation, Haskell, JavaScript, and Ruby clients from a rest API
  • rest-snap, rest-happstack, rest-wai: Drivers for running resources using the web server of your choice

We have also released other packages that are either used by or can be used with rest:

  • rest-client: Used by haskell clients generated by rest-gen
  • rest-types: Types used by the other rest packages
  • json-schema: Define and derive schemas for JSON serializations
  • generic-aeson: Generically derives JSON serializations for data types minimalistically
  • regular-xmlpickler: Generically derives XML serializations for data types
  • aeson-utils: Utilities for working with Aeson.
  • hxt-pickle-utils: Utility functions for using HXT picklers
  • multipart: HTTP Multipart implementation forked from the cgi package
  • rest-stringmap: Maps with string-like keys with built-in serialization to XML and JSON (since JSON doesn’t allow arbitrary keys)
  • code-builder: String manipulation library for code generation

We had a great time working on rest at ZuriHac (thanks for organizing, Better!) and we are happy to see that a lot of people were interested in our work. We got a lot done, here are some highlights:

  • Erik wrote an introductory tutorial to rest
  • Håkan Thörngren rewrote the rest-gen Haskell code generator to use haskell-src-exts, it was released in rest-gen-0.14
  • Christian Berentsen did several things:
    • Cleaned up the interface of json-schema, it was released in json-schema-0.6
    • Added support for outputing Fay compatible json as a separate output type (we want to make it easier to extend rest with more output types so things like this can go in external packages)
    • Implemented a generic API discovery resource that you can hook into your API with no configuration
  • Tom Lokhorst helped out with some always appreciated bug fixing
  • Sebas wrote the rest-wai driver to make sure everyone can use the web server they prefer together with rest
  • Adam worked on the rest-example application and rewrote parts of rest-gen to make the code generator and the library interface simpler
  • wiz did performance benchmarks and created a script to generate haddocks for rest itself

I hope I didn’t forget anyone. A big thanks to everyone who participated!

All the mentioned projects are available on hackage and in public repositories on github. We also created a public mailing list for all our open source projects.

We use these packages to write our Haskell API code for Silk.
Join our team if you enjoy this stuff too, we’re hiring!
Categories: Offsite Blogs

Smallcheck Depth

haskell-cafe - Sun, 06/29/2014 - 11:44pm
The docs for smallcheck, and the original paper, say that the depth is the depth of nested constructor applications. For some reason this behavior has changed in newer versions of Smallcheck and maybe someone can shed some light on why. For instance, I would think the depth of True and False is 0, as there are no nested constructors. Sure enough, in Smallcheck 0.2.1, (series 0) :: [Bool] gives [True, False]. Yet in smallcheck 1.1.1, (list 0 series) :: [Bool] gives []. Thanks. Omari _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Is there a good way to work backwards on a list?

Haskell on Reddit - Sun, 06/29/2014 - 11:41pm

I have to perform an operation on every other element of the list starting from the end; I'm curious if there's a good way of doing that better than calculating the length of the list beforehand or reversing it, performing the operation, and reversing again.

submitted by iobender
[link] [17 comments]
Categories: Incoming News

Side Effects?

Haskell on Reddit - Sun, 06/29/2014 - 6:05pm

Hello, I am quite new to Haskell and functional programming in general. My question has to do with side effects. I'm working through Yet Another Haskell Tutorial and am at the section on user input. Specifically my question has to do with this quote on page 32 of the PDF:

"After all, suppose you have a function that reads a string from the keyboard. If you call this function twice, and the user types something the first time and something else the second time, then you no longer have a function, since it would return two different values."

What if the user types the same name twice? In other words given the same input: "Kelly", the function always outputs the same message: "Hello Kelly" I don't understand how this is producing a side effect since given the same input, the function produces the same output. This seems to me to be the same as calling a factorial function with the input of 5 and getting output 120. Or calling the function with 6 and getting 720.

I hope this question isn't too elementary for the subreddit, but I appreciate any help for a n00b like me!

submitted by bacaholic
[link] [20 comments]
Categories: Incoming News

Requesting maintainership: bzlib-conduit andprocess-conduit

haskell-cafe - Sun, 06/29/2014 - 5:02pm
I'd like to request to be granted maintainership of two conduit-related packages: bzlib-conduit and process-conduit. Both packages are maintained by the same author. I opened up a pull request[1] on bzlib-conduit back in January, and haven't heard back yet. There's also an issue with process-conduit[2] which I'd like to address, but given the lack of response of bzlib-conduit, haven't sent a pull request yet. I also emailed the author directly five days ago regarding the bzlib-conduit pull request, and have not received a response. [1] [2] _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Neil Mitchell: Optimisation with Continuations

Planet Haskell - Sun, 06/29/2014 - 3:56pm

Summary: Continuations are confusing. Here we solve a simple problem (that is at the heart of the Shake build system) using continuations.

Imagine we are given two IO a computations, and want to run them both to completion, returning the first a value as soon as it is produced (let's ignore exceptions). Writing that in Haskell isn't too hard:

parallel :: IO a -> IO a -> IO a
parallel t1 t2 = do
once <- newOnce
var <- newEmptyMVar
forkIO $ t1 >>= once . putMVar var
forkIO $ t2 >>= once . putMVar var
readMVar var

We create an empty variable var with newEmptyMVar, fire off two threads with forkIO to run the computations which write their results to var, and finish by reading as soon as a value is available with readMVar. We use a utility newOnce to ensure that only one of the threads calls putMVar, defined as:

newOnce :: IO (IO () -> IO ())
newOnce = do
run <- newMVar True
return $ \act -> do
b <- modifyMVar run $ \b -> return (False, b)
when b act

Calling newOnce produces a function that given an action will either run it (the first time) or ignore it (every time after). Using newOnce we only call putMVar for the first thread to complete.

This solution works, and Shake does something roughly equivalent (but much more complex) in it's main scheduler. However, this solution has a drawback - it uses two additional threads. Can we use only one additional thread?

For the problem above, running the computations to completion without retrying, you can't avoid two additional threads. To use only one additional thread and run in parallel you must run one of the operations on the calling thread - but if whatever you run on the additional thread finishes first, there's no way to move the other computation off the the calling thread and return immediately. However, we can define:

type C a = (a -> IO ()) -> IO ()

Comparing IO a to C a, instead of returning an a, we get given a function to pass the a to (known as a continuation). We still "give back" the a, but not as a return value, instead we pass it onwards to a function. We assume that the continuation is called exactly once. We can define parallel on C:

parallel :: C a -> C a -> C a
parallel t1 t2 k = do
once <- newOnce
forkIO $ t1 (once . k)
t2 (once . k)

This definition takes the two computations to run (t1 and t2), plus the continuation k. We fork a separate thread to run t1, but run t2 on the calling thread, using only one additional thread. While the parallel function won't return until after t2 completes, subsequent processing using the a value will continue as soon as either finishes.

Looking at the transformers package, we see Control.Monad.Trans.Cont contains ContT, which is defined as:

newtype ContT r m a = ContT {runContT :: (a -> m r) -> m r}

If we use r for () and IO for m then we get the same type as C. We can redefine C as:

type C a = ContT () IO a

The changes to parallel just involve wrapping with ContT and unwrapping with runContT:

parallel :: C a -> C a -> C a
parallel t1 t2 = ContT $ \k -> do
once <- newOnce
forkIO $ runContT t1 (once . k)
runContT t2 (once . k)

Now we've defined our parallel function in terms of C, it is useful to convert between C and IO:

toC :: IO a -> C a
toC = liftIO

fromC :: C a -> IO a
fromC c = do
var <- newEmptyMVar
forkIO $ runContT c $ putMVar var
readMVar var

The toC function is already defined by ContT as liftIO. The fromC function needs to change from calling a callback on any thread, to returning a value on this thread, which we can do with a forkIO and MVar. Given parallel on IO takes two additional threads, and parallel on C takes only one, it's not too surprising that converting IO to C requires an additional thread.

Aren't threads cheap?

Threads in Haskell are very cheap, and many people won't care about one additional thread. However, each thread comes with a stack, which takes memory. The stack starts off small (1Kb) and grows/shrinks in 32Kb chunks, but if it ever exceeds 1Kb, it never goes below 32Kb. For certain tasks (e.g. Shake build rules) often some operation will take a little over 1Kb in stack. Since each active rule (started but not finished) needs to maintain a stack, and for huge build systems there can be 30K active rules, you can get over 1Gb of stack memory. While stacks and threads are cheap, they aren't free.

The plan for Shake

Shake currently has one thread per active rule, and blocks that thread until all dependencies have rebuilt. The plan is to switch to continuations and only have one thread per rule executing in parallel. This change will not require any code changes to Shake-based build systems, hopefully just reduce memory usage. Until then, huge build systems may wish to pass +RTS -kc8K, which can save several 100Mb of memory.

Categories: Offsite Blogs

Declarative database migrations

haskell-cafe - Sun, 06/29/2014 - 2:39pm
Hello! Looking for tools to use discovered  that I miss something like Rails database migrations in haskell. Is there a package for it (describe the whole schema and/or particular migrations)? --  Timur Amirov Berlin, Germany_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Any surviving copies of Simon Peyton Jones' XMonad talk videos on OSCON 2007?

Haskell on Reddit - Sun, 06/29/2014 - 2:12pm

Due to criminal negligence of that they themselves confess to, the XMonad talks in OSCON 2007 were deleted.

Are there any surviving copies of this talk?

Sad reminders of this infamous decision can be witnessed in:

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

PrimMonad, Vector and records (stupid question)

Haskell on Reddit - Sun, 06/29/2014 - 1:23pm

So, I know that I can use PrimMonad to write a function that deals with mutable vectors in ST and IO like this:

modifyVector :: PrimMonad m => VUM.MVector (PrimState m) Int -> m () modifyVector vec = do VUM.write vec 0 100 newVec <- VUM.replicate 100 (0 :: Int) return ()

But how would I handle having a record with a vector inside in such a situation?

Say I got

data PrimMonad m => ContainsVector m = ContainsVector { cvVector :: VUM.MVector (PrimState m) Int }

and then adding

let cvVec = ContainsVector newVec

to modifyVector does not work. In Haskell you can't reference the types from your function type declaration inside the function itself (unfortunately?), so I wouldn't know how to write an explicit type signature for my ContainsVector creation, and the compiler can't seem to deduce by itself that I want the record's monad type to be the same as my function. Any messing around with RankNTypes / ExistentialQuantification was also unsuccessful. Not that I'd consider myself an expert in that, every time I mess with the ST monad or ExistentialQuantification, I get rather confused ;-)

What's the correct approach here?


submitted by SirRockALot1
[link] [8 comments]
Categories: Incoming News

QuickTypeSearch: Upgraded and thanks for the feedback!

Haskell on Reddit - Sun, 06/29/2014 - 12:04pm

Hi! Last week I launched QuickTypeSearch, a type searcher based on type normalization. I want to thanks you for all the feedback I received, and to inform that I have updated the site implementing some of those ideas (now it also includes class methods). I also indexed more pacakges so hopefully, now it can be a bit more usefull :).

Some query examples:

submitted by sofosure
[link] [17 comments]
Categories: Incoming News

Basic program ideas for learning about Monads?

Haskell on Reddit - Sun, 06/29/2014 - 11:18am

edit: Thanks a lot for the suggestions!

submitted by Apterygiformes
[link] [12 comments]
Categories: Incoming News

Implementing parental using blood type

haskell-cafe - Sun, 06/29/2014 - 3:16am
Hello, I'm trying to practice my Haskell. So I had the idea of making a program which, given a mother's and a child blood type, it can determine whether a certain father is possible or not. Please take a look on this gist with my implementation: Would you care to comment on it? It looks a bit cumbersome to my eyes. I'm trying to find the most intuitive and elegant way of do it. I'm not so much worried with performance. []'s Rafael _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Tracking object modification

Haskell on Reddit - Sun, 06/29/2014 - 3:04am


let's say I have a data structure like this:

data MyData = MyData { a :: Int, b :: String }

and a collection, e.g.

type Key = Int type MyMap = Map Key MyData

What I'd like to do is to track whether an object has been changed or not.
So for example:

-- modify an object, somehow signal or track that it has been modified -- bonus points for tracking what has been modified withMyData :: (MyData -> MyData) -> Key -> MyMap -> MyMap withMyData = adjust

What would be a good way to track changes to MyMap?
Or more generally, to a data structure?

I had two ideas of how to do that:

  • add a dirty flag to MyData: data MyData = MyData { dirty :: Bool, ..
  • use a dirty flag in my collection: Map Key (Bool, MyData)
  • keep a copy of the data structure before/after and compare.. (I guess that would be extremely bad for performance)

The background is that I have a model (think mvc) in a program and when I update the view I want to avoid unnecessary updates for objects which weren't modified.

Side-note: In e.g. C++ I would solve this by using flags or a bitmask for my private object members and provide setters/getters. The setter would then set the modification flags.

Is there an idiomatic or functional way to do this in Haskell?

Edit: fixed list

submitted by jrk-
[link] [20 comments]
Categories: Incoming News

Conceptualizing Functor vs Performance

haskell-cafe - Sun, 06/29/2014 - 1:37am
I am having trouble consolidating a performance-based data representation decision I would like to make against the concept of a "functor". The question can be reduced to the following: Conceptually I think that Data.Vector /should/ be a functor. More specifically, I see no CONCEPTUAL reason that it shouldn't be possible to define some "efficient" container-like object (for example for fast Matrix operations for unboxable numbers, but something that still works for a symbolic variable type) that chooses to represent its data as a Data.Vector.Unboxed if possible, but chooses some more generic structure (perhaps a strict list or a generic Data.Vector) if that is not the case. I haven't found such a functor, and can't seem to implement it on my own. The point is that in theory there is no true constraint on the type of the object being contained so it SHOULD be a Functor/Traversable/etc., the representation simply changes to something more efficient only if possible, and decays to a more generic type gracefull
Categories: Offsite Discussion

Which type of Ref should I use for this kind of processing?

Haskell on Reddit - Sun, 06/29/2014 - 12:52am

I'm a Haskell newbie trying to code something that plans laying out of coordinates in 3d space. A given objects position will depend on nearby objects and connections and other such details. It seems to me that I will need to repeatedly update x,y,z values of many objects.

I've read about MVars and TVars and I'm wondering if anybody could give me any general advice on which approach (or any other approach) might be the best to try out.

submitted by kanzenryu
[link] [8 comments]
Categories: Incoming News

Moderators, please block zhangliu529< at >

haskell-cafe - Sat, 06/28/2014 - 9:14pm
Every time I send a message to Cafe I get an autoreply from this user with the following text: 您发的邮件我已收到,我将尽快给您答复。 It's extremely annoying. Please, exclude this user from the mailing list. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

handling non-string command line arguments

haskell-cafe - Sat, 06/28/2014 - 8:28pm
I've been looking at choices for parsing command line arguments, including getOpt. The examples I can find focus on string arguments, whereas I am interested in numbers. In the application at hand, it is particularly important to issue clear error messages when arguments don't parse as numbers or are out of range. I could use getOpt as a first pass with string arguments and then turn the strings into validated numbers in a second pass, but that's a bit awkward. Alternately I could use the options records with Options -> IO Options functions. But both of these solutions treat type mismatches differently from other options errors. Has anyone found a cleaner solution?
Categories: Offsite Discussion