News aggregator

My first Haskell library is for diffing JSON value. Feedback and help much appreciated.

Haskell on Reddit - Fri, 11/13/2015 - 8:28am

Hello fellow Haskell programmers :)

I'm currently working on my first Haskell library. It's a port of a library that I've implemented in JavaScript. The library exports a diff function that calculates a difference/delta between any two values by using their JSON representation. The delta converts to very compact JSON. Later it can be applied to an object with patch.

This makes it possible to send changes to data between a client and a server much more compactly than re-sending entire JSON objects – which is what people often do in REST APIs. More information about the tricks used to achieve the compact format is available in the README for the JavaScript version linked above.

The source code for the Haskell port is available here.

I've had a few struggles with the port due to some approaches that made a lot of sense in JavaScript but turned out to be suboptimal in Haskell. I've learned quite a bit during the process about how to take advantage of types instead of being restricted by them.

I do have an issue left that I'd like to get some opinions on from more experienced Haskell developers.

In JavaScript Arrays and Objects are at some level the same data structure. They are both a bit like a HashMap String a. For Arrays the keys will be indexes in strings (ugh). I used this in my original JavaScript implementation to use the exact same diff algorithm for objects and arrays through duck typing.

I'd like to do something similar in my Haskell implementation but for good reasons that isn't as simple. I'm currently doing an inefficient conversion in order to reuse the same code for both Objects and Arrays.

Is there any way I can avoid the conversion without having to write the same code twice for Arrays and Objects? The only solution I can come up with is using type classes. But it seems nontrivial and a bit hacky?

Besides help on the above problem I'd really love comments and suggestions on the code overall :)

Thanks in advance!

submitted by paldepind
[link] [9 comments]
Categories: Incoming News

`cabal repl` causes GHC panic on simple project with C++ files

Haskell on Reddit - Fri, 11/13/2015 - 3:10am

I don't know if this is a bug or I'm doing something wrong but I haven't been able to get an answer and it's REALLY frustrating. I'm trying to use Haskell for a project and due to missing libraries I'm forced to wrap around a C++ one.

This really shouldn't be that difficult. There are no external dependencies except from stdc++ and the whole source for the library is available in 2 hpp/cpp files.

Here's the StackOverflow question with a bit more detail and a link to the project if you want to give it a shot.

Btw, cabal run works fine. Only GHCi is causing issues which leads me to believe it's a bug. I've also tried to manually invoke ghci with various combinations of .o files but I always either get missing references of GHC panic. It should also be noted that GHC was fine with me giving it -lstdc++ as an option but GHCi didn't accept that until I specified -lstdc++-6 which I suspect isn't even the same thing because then it complained it still couldn't find things like sqrt.

submitted by Darwin226
[link] [5 comments]
Categories: Incoming News


Haskell on Reddit - Thu, 11/12/2015 - 9:16pm

A keybinding found in the future (or did I just dream this after a very long refactor?)

(haskell-yank &optional ARG)

Reinsert ("paste") the last stretch of killed haskell code. More precisely, reinsert the most recent haskell-kill, which is the stretch of killed haskell code most recently killed OR yanked. Insert code and comments at point, insert PROPERTIES (language pragmas, ghc options and imports) in relevant sections, re-sort and eliminate duplicates. Add let and where bindings based on PROPERTY semantic-position-at-point, refactor code if semantics are different to haskell-kill PROPERTIES. Reindent between point and mark. With just M-m u as argument, call haskell-hoogle-get-code-extras to infer PROPERTIES that may be missing from the haskell-kill.

submitted by tonyday567
[link] [1 comment]
Categories: Incoming News

How to mix the base functor/recursion scheme stuff with AST of mutually recursive types?

Haskell on Reddit - Thu, 11/12/2015 - 8:02pm

I believe I partially understand the following code:

data TreeF f = Tip Int | Folk f f deriving (Eq, Show) newtype Fix f = In { out :: f (Fix f) }

Though still a bit confused with the Eq instance definition:

instance Eq (f (Fix f)) => Eq (Fix f) where In f == In g = f == g

Nevertheless, I am trying to apply this—design pattern—to my AST, which unfortunately is a bit more complicated than the ones presented in those blogs. After some simplification, it looks like:

data Expr = EInt Int | ELam Pat Expr data Pat = PAs String Expr

Namely, it is a set of mutually recursive types. I first tried something like:

data ExprF f = EInt Int | ELam (f Pat) (f Expr) data PatF f = PAs String (f Expr)

but it seems that I ran into some infinite kind issue—a bit like the infinite type thing in using Y-combiantor with functions directly, rather than adding a newtype wrapper.

data Expr w = EInt Int | ELam ((w (Pat w)) (w (Expr w))) data Pat w = Pat (w (Expr w))

But I couldn't even define a Eq instance for it, a dependency hell: Eq (xxx Expr) requires Eq (xxx Pat) and vice versa.

Is there any solution to this problem, without trying to putting both patterns and expressions under a single type? One of my new idea is to using type-level pattern matching with type families, so I can get (w Pat) from (w Expr). However, I couldn't find such a way to implement it.

Thank you.

submitted by minsheng
[link] [18 comments]
Categories: Incoming News

the last mile in haskell performance

haskell-cafe - Thu, 11/12/2015 - 7:49pm
Looking at this: It seems that it is impossible to manage data in Haskell within a core without L1 cache faults. Except for unboxed arrays of primitive types. Since it is impossible to have unboxed arrays of user-defined types. Am I right? This is definitively very bad for tasks that are inherently single threaded and in general for the image of Haskell as a practical language. I have more to say about that, but I would like to know first if I´m right and second If there is some idea to going on to permit user defined boxed datatypes. Or if there is some low level trick for having it using foreign call and unsafeCoerce in some way, I know that the language ATS has unboxing a la carte....
Categories: Offsite Discussion

Magnus Therning: How can I unit test failure cases?

Planet Haskell - Thu, 11/12/2015 - 6:00pm

When writing unit tests, especially when using mock objects, there’s always a risk of falling into the trap of writing tests for the implementation rather than for the API. In my experience the most obvious indication of that is that any refactoring of the code requires major rework of the tests.

Right now I’m sitting with the task of writing tests for a function that promises to return NULL on failure. The function in question allocates memory up to three times, opens two message queues, sends a message on one and expects a reponse on the other. If all goes well it returns the pointer to one of the memory areas allocated. And if it fails, for whatever reason, it returns NULL.

Is it even possible to write a set of tests for it without testing the specific implementation?

I know some of the calls can be re-arranged, e.g. moving all memory allocation to the start of the function, without affecting the caller of the function. However, if I use mock objects to simulate failure in the external libraries and system calls I’d be forced to modify the tests on each such re-arranging of calls. In other words I’m testing the implementation!

Should I think differently about testing failure cases?

Should the API be changed in some way to make unit tests a better fit for testing failures?

Categories: Offsite Blogs

Representation for a Rubics Cube

Haskell on Reddit - Thu, 11/12/2015 - 5:57pm

I'm looking to implement a cube solver. I want a representation for an n*n cube.

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

Can someone explain "<$>" and "<*>"?

Haskell on Reddit - Thu, 11/12/2015 - 3:12pm

For example, what is this doing?

(,) <$> [1..10] <*> [1..10]

I understand it creates 100 pairs, but I don't understand how. From what I understand, the <*> binds them together, and the <$> applies (,) to each pair, but I don't know HOW. I've tried reading up on monads and alternatives and what not but it still doesn't make sense.

submitted by yaBoyPanda
[link] [31 comments]
Categories: Incoming News

Dominic Steinitz: Floating Point: A Faustian Bargain?

Planet Haskell - Thu, 11/12/2015 - 2:46pm

Every so often, someone bitten by floating point arithmetic behaving in an unexpected way is tempted to suggest that a calculation should be done be precisely and rounding done at the end. With floating point rounding is done at every step.

Here’s an example of why floating point might really be the best option for numerical calculations.

Suppose you wish to find the roots of a quintic equation.

> import Numeric.AD > import Data.List > import Data.Ratio > p :: Num a => a -> a > p x = x^5 - 2*x^4 - 3*x^3 + 3*x^2 - 2*x - 1

We can do so using Newton-Raphson using automatic differentiation to calculate the derivative (even though for polynomials this is trivial).

> nr :: Fractional a => [a] > nr = unfoldr g 0 > where > g z = let u = z - (p z) / (h z) in Just (u, u) > h z = let [y] = grad (\[x] -> p x) [z] in y

After 7 iterations we see the size of the denominator is quite large (33308 digits) and the calculation takes many seconds.

ghci> length $ show $ denominator (nr!!7) 33308

On the other hand if we use floating point we get an answer accurate to 1 in after 7 iterations very quickly.

ghci> mapM_ putStrLn $ map show $ take 7 nr -0.5 -0.3368421052631579 -0.31572844839628944 -0.31530116270327685 -0.31530098645936266 -0.3153009864593327 -0.3153009864593327

The example is taken from here who refers the reader to Nick Higham’s book: Accuracy and Stability of Numerical Algorithms.

Of course we should check we found a right answer.

ghci> p $ nr!!6 0.0
Categories: Offsite Blogs

My First Haskell Project--Please give feedback!

Haskell on Reddit - Thu, 11/12/2015 - 10:30am

tl;dr: trying to write a blackjack game and it's a mess

This is my first Haskell program bigger than a Project Euler problem and it's not going very well.

I can see places where a monadic solution would be best (mainly passing around various pieces of game state like the deck) but actually implementing one is incredibly unintuitive. The error messages are worse than gcc barfing on C++ templates and it's a choice between a suboptimal solution using lots of temporary names or reading error messages for 2 hours. I think part of the reason the state monad always seems very brittle is that I need to keep packing and unpacking tuples holding each piece of state I need. If I'm not passing just one thing it becomes very verbose very fast. This ends up being more error prone than using mutable state because I have to remember which temporary names go where and typos might not get caught. So I really don't want this to become a bad habit.

The most frustrating part is I'm sure there's a better way to do nearly all of the solutions I came up with but can never seem to achieve the elegance that brought me to Haskell in the first place.

For now the Visibility type isn't actually used for anything, but in the future an AI might operate differently based e.g. on opponents' face up cards.

It's not meant to be a blackjack game that you play but rather to see how different strategies turn out over a lot of games. Right now there's only one (hit below 16, stay above 16) but the idea is I'll write more.

I didn't refactor playGame because even though it should definitely be a lot of smaller functions I think I'm fundamentally messing up big chunks of it (which could be better expressed using a state monad) and would rather learn the proper way to do it.

I really want to like Haskell, it's just that even the most trivial project is excruciating.

I'd appreciate any help and feedback on the code, particularly places I marked with XXX in a comment. I'd especially appreciate help with monads and applicative functors.

Also, does Visibility seem like a monad/applicative functor? It seemed to me like one because of the wrap-unwrap behavior and I think it obeys the appropriate laws, but ?

(Definitely going to break this up into modules but I left it as a single file so it'd be easier to follow for people on phones)

Help and feedback is much appreciated!

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

Designing data structures that are "dependent" on references/pointers. What to do?

Haskell on Reddit - Thu, 11/12/2015 - 7:56am

Is there a general approach for this? Should I just not use Haskell?


I want to create a program for managing and simulating tournaments/leagues in e-sports (and normal sports too, of course).

But I can't figure out a nice way of building the data structure for it. I had so much trouble with it that I resorted to python (which worked out great).

My main problem seems to be that both the winner and loser of a match have to be linked to new matches, and thus every match has two inputs. Here's an example of what is usually called a double elimination group.

player1 --> match -- w --------> winners -- w ---> 1st player2 --> 1 -- l -+ +----> match -- l -+ +-|-+ +-> decider --> 2nd | | +-> match --> 3rd player3 --> match -- w ---+ +--> losers -- w ---+ player4 --> 2 -- l --------> match -- l -----> 4th

This doesn't work well with a typical haskell structure of just holding the data and not referencing other structures. I've had some ideas where you use a builder to create a valid structure and then use an unsafe one underneath, maybe creating a set of arrows and a set of matches or something similar. But that feels like cheating because I'd like the type system to help me construct only valid structures from the start.

Does anyone have any suggestions?

EDIT: Thanks for all the input. I'm gonna try this again with your ideas in mind.

submitted by MaybeJustNothing
[link] [43 comments]
Categories: Incoming News

If Haskell is such a terrible language, what do they program in?

Haskell on Reddit - Thu, 11/12/2015 - 6:41am

I'm an experienced imperative programmer who's been trying to get into Haskell by light studying every now and then for almost 2 years now, and some of the concepts are sinking in, and I love Haskell. I read and try to understand the mathematical foundations as well, and realize that Haskell is not the perfect language (undefined, unsafeCoerce etc.) from that angle, but to my imperative eyes, it seems like a safe haven.

I understand that the language is not for everyone; both for those who don't want to bother with purity and a strong type system and those who desire a language with even stronger mathematical foundations than Haskell. I know what the first group of programmers are thinking, and what languages and tools they're using for their real-life programming problems, but I have no idea about the second group.

I see many people, (type-theorists, category theorists?) bashing Haskell to the ground for its flaws. So, the question is; If Haskell is such a terrible language, what do they program in? I'm sincerely curious about the answer; what do they use to solve real-life programming problems. Do they:

1) Say, "why pretend to be pure and safe while we aren't", then go ahead and use a conventional imperative language?

2) Program in Agda (or the like), even if the "Hello World" program compiles to 20 MBs? (Note that my intention isn't to say anything negative about Agda, I know very little about it, and AFAIK it's not intended to be a "programming" language)

3) Use Agda(or the like) to generate source code for other languages.

4) Prefer one of Haskell's categorical cousins such as OCaml or Erlang because it/they don't have its flaws.

5) (Probably something else I have no idea about.)...

As I said, I'm sincerely curious. Thank you

submitted by enobayram
[link] [84 comments]
Categories: Incoming News

Get a Brain

Haskell on Reddit - Thu, 11/12/2015 - 5:57am
Categories: Incoming News

checking whether the list is active

haskell-cafe - Thu, 11/12/2015 - 5:34am
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe< at >
Categories: Offsite Discussion

Image processing

Haskell on Reddit - Thu, 11/12/2015 - 2:59am

I wrote a small proof-of-concept program in Python a while ago which works on jpeg files. I was thinking of working on this again in Haskell.

This program works to select particular photos from a directory of photos. Ideally, I'd like to work with raw files, i.e., .nef files from Nikon cameras etc., since most photographers work with raw files. This by might be a naive question, but is this possible or am I limited to working with jpeg, tiff and png images?

submitted by MyFunc
[link] [6 comments]
Categories: Incoming News

Excuse me, I think this is my stop now...

Haskell on Reddit - Thu, 11/12/2015 - 2:00am

I had been disappointed in the past with other languages such as C++ and Python treading water and stagnating. So this gave me the incentive to look around and discover Haskell.

I only joined the community shortly after Haskell 2010 came to be, and I've learned a lot of mind-blowing things about programming. And for that I'm very grateful to you all!

I was looking forward to a community open to move the language forward given its mantra to "avoid success at all costs" and do the right thing even if it hurts a little.

Haskell's future was looking bright when AMP and FTP were being finally implemented. But this seemed too good to be true. And in fact, people are still complaining about those changes and I'm sure many would rather they were rolled back again.

Also, we have an either absent or contested committee which bows to public pressure by high profile Haskellers who resort to dismissal and hyperbole just to sabotage what's technically the proper thing to do. To be honest, I've started losing faith in the committee myself for becoming conservative (e.g. this 3-year no-warning/cpp policy... wtf).

Given the recent developments I'm afraid Haskell is past its prime (pun intended) anyway. Other languages are moving forward, break things, literature needs updating, people moan, but popularity continues to rise nonetheless. Later generations (often without being aware) then just benefit from the improvements their forefathers fought for rather than blaming them for lost opportunities.

But for some reason, Haskell is held to a higher standard for a much smaller user-base or rather for the prospect of a yet to materialise large user-base. Is this a sign of delusion?

I'm sad about this, but I've been told it's important to walk away when you realise that something isn't working out anymore. So now, I guess, it's my time to vote with my feet (even if I my vote doesn't carry as much weight as Mark's) and seek for greener pastures.

I wish you all the best, and I hope Haskell won't go the way of the Dodo.

submitted by [deleted]
[link] [40 comments]
Categories: Incoming News