ANN: The Herbie GHC Plugin

80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable. You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin

This is *so cool*. One question - why did you go with something that
modifies the Core instead of rewriting the original source a la Alan
Zimmerman's work? Is it because the stable forms are sometimes gnarly
looking?
Thanks for making and sharing this!
On Tue, Sep 22, 2015 at 10:17 AM, Mike Izbicki
80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Allen Currently working on http://haskellbook.com

I'm not familiar with Alan Zimmerman's work. AFAIK, the only way to
get GHC to modify Haskell code without any annotations is via the
plugin mechanism, which manipulates core. It would have been possible
to do a template haskell quasiquoter, but that would require the
programmer to have to manually annotate their math expressions. But I
didn't want the programmer to have to do anything at all.
On Tue, Sep 22, 2015 at 8:41 AM, Christopher Allen
This is *so cool*. One question - why did you go with something that modifies the Core instead of rewriting the original source a la Alan Zimmerman's work? Is it because the stable forms are sometimes gnarly looking?
Thanks for making and sharing this!
On Tue, Sep 22, 2015 at 10:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Allen Currently working on http://haskellbook.com

Oh, I'm thinking of stuff like:
https://hackage.haskell.org/package/ghc-exactprint
Where the idea is the tool would refactor and rewrite your code, putting it
back where it was. Kinda like go fmt, but you can do anything you want.
https://github.com/mpickering/apply-refact also comes to mind.
I guess I'm thinking of this because I think it could be useful for
programmers to preserve the fixed versions in their source code, maybe with
before & after comments, for educational purposes.
On Tue, Sep 22, 2015 at 10:46 AM, Mike Izbicki
I'm not familiar with Alan Zimmerman's work. AFAIK, the only way to get GHC to modify Haskell code without any annotations is via the plugin mechanism, which manipulates core. It would have been possible to do a template haskell quasiquoter, but that would require the programmer to have to manually annotate their math expressions. But I didn't want the programmer to have to do anything at all.
On Tue, Sep 22, 2015 at 8:41 AM, Christopher Allen
wrote: This is *so cool*. One question - why did you go with something that modifies the Core instead of rewriting the original source a la Alan Zimmerman's work? Is it because the stable forms are sometimes gnarly looking?
Thanks for making and sharing this!
On Tue, Sep 22, 2015 at 10:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Allen Currently working on http://haskellbook.com
-- Chris Allen Currently working on http://haskellbook.com

On Tue, Sep 22, 2015, at 08:48, Christopher Allen wrote:
I guess I'm thinking of this because I think it could be useful for programmers to preserve the fixed versions in their source code, maybe with before & after comments, for educational purposes.
My thoughts exactly, I think this would be *really* cool as an HLint-style tool.

On Tue, Sep 22, 2015 at 08:57:26AM -0700, Eric Seidel wrote:
On Tue, Sep 22, 2015, at 08:48, Christopher Allen wrote:
I guess I'm thinking of this because I think it could be useful for programmers to preserve the fixed versions in their source code, maybe with before & after comments, for educational purposes.
My thoughts exactly, I think this would be *really* cool as an HLint-style tool.
I foresee -fwarn-numeric-instability! This goes beyond nice syntax suggestions to actually fixing potential bugs (when shared and compiled by other versions/compilers). Very cool. :)

Bryan Richter wrote:
I foresee -fwarn-numeric-instability!
Oh, yes, please!!!! Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

My thoughts exactly, I think this would be *really* cool as an HLint-style tool.
The main problem with this is that numerically stable expressions are often very opaque. If you look at the first example in the README, I would definitely rather have: w far near = -(2 * far * near) / (far - near) in my code than w near far = if near < -1.7210442634149447e+81 then (2.0 * near / (near - far)) * far else if near < 8.364504563556443e+16 then (2.0 * near) / ((near - far) / far) else ((2.0 * near) / (near - far)) * far One of the reasons I use Haskell is because the code is "at the level of thought" and GHC translates this into awesome speed without the programmer needing to worry about the gory details in most cases. I want that same experience for numerical stability.

That's what I suggested might be the case and this makes sense. Thank you :)
On Tue, Sep 22, 2015 at 12:17 PM, Mike Izbicki
My thoughts exactly, I think this would be *really* cool as an HLint-style tool.
The main problem with this is that numerically stable expressions are often very opaque. If you look at the first example in the README, I would definitely rather have:
w far near = -(2 * far * near) / (far - near)
in my code than
w near far = if near < -1.7210442634149447e+81 then (2.0 * near / (near - far)) * far else if near < 8.364504563556443e+16 then (2.0 * near) / ((near - far) / far) else ((2.0 * near) / (near - far)) * far
One of the reasons I use Haskell is because the code is "at the level of thought" and GHC translates this into awesome speed without the programmer needing to worry about the gory details in most cases. I want that same experience for numerical stability. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Allen Currently working on http://haskellbook.com

On 22-09-15 19:19, Christopher Allen wrote:
That's what I suggested might be the case and this makes sense. Thank you :)
On Tue, Sep 22, 2015 at 12:17 PM, Mike Izbicki
mailto:mike@izbicki.me> wrote: > My thoughts exactly, I think this would be *really* cool as an HLint-style tool.
The main problem with this is that numerically stable expressions are often very opaque. If you look at the first example in the README, I would definitely rather have:
w far near = -(2 * far * near) / (far - near)
in my code than
w near far = if near < -1.7210442634149447e+81 then (2.0 * near / (near - far)) * far else if near < 8.364504563556443e+16 then (2.0 * near) / ((near - far) / far) else ((2.0 * near) / (near - far)) * far
One of the reasons I use Haskell is because the code is "at the level of thought" and GHC translates this into awesome speed without the programmer needing to worry about the gory details in most cases. I want that same experience for numerical stability. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org mailto:Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Allen Currently working on http://haskellbook.com
I'd love it if there was a way to (semi-)automatically add rewrite rules to the Haskell program source modules. That way, it is both automatically applied on subsequent compiles, consistent between builds, and transparent what is happening, which expressions are unstable and how to fix them manually if desired. (Although I understand that rewrite rules are not always triggered?) Thanks for pointing out this problem in (my) programs and automating a solution! kind regards, Arjen

Very nice!
Now I just need to figure out how to extract the results of these analyses
and see if I can understand and apply them manually to what libraries of
mine are affected, so that folks who won't or can't run this plugin can
derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Does GHC HQ consider the possibility of embedding/shipping plugins like this with normal releases?
________________________________
From: Haskell-Cafe

In this case the primary concern would be that Herbie really needs to have an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install. -Edward On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron < elliot.cameron@covenanteyes.com> wrote:
Does GHC HQ consider the possibility of embedding/shipping plugins like this with normal releases? ------------------------------ *From:* Haskell-Cafe
on behalf of Edward Kmett *Sent:* Tuesday, September 22, 2015 1:39 PM *To:* mike@izbicki.me *Cc:* Haskell Cafe *Subject:* Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Okay. Having gone through and chewed on a bunch of the optimizations it has
found, I have to say I officially absolutely adore this plugin!
It would be nice if it didn't report 'improved' expressions that are the
same as the original, but everything else about it is amazing.
-Edward
On Tue, Sep 22, 2015 at 3:08 PM, Edward Kmett
In this case the primary concern would be that Herbie really needs to have an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install.
-Edward
On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron < elliot.cameron@covenanteyes.com> wrote:
Does GHC HQ consider the possibility of embedding/shipping plugins like this with normal releases? ------------------------------ *From:* Haskell-Cafe
on behalf of Edward Kmett *Sent:* Tuesday, September 22, 2015 1:39 PM *To:* mike@izbicki.me *Cc:* Haskell Cafe *Subject:* Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Thanks :)
That's a good suggestion about not calling expressions improved when
they're not. I've just pushed a new patch to the repo that fixes this
issue.
On Tue, Sep 22, 2015 at 12:48 PM, Edward Kmett
Okay. Having gone through and chewed on a bunch of the optimizations it has found, I have to say I officially absolutely adore this plugin!
It would be nice if it didn't report 'improved' expressions that are the same as the original, but everything else about it is amazing.
-Edward
On Tue, Sep 22, 2015 at 3:08 PM, Edward Kmett
wrote: In this case the primary concern would be that Herbie really needs to have an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install.
-Edward
On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron
wrote: Does GHC HQ consider the possibility of embedding/shipping plugins like this with normal releases?
________________________________ From: Haskell-Cafe
on behalf of Edward Kmett Sent: Tuesday, September 22, 2015 1:39 PM To: mike@izbicki.me Cc: Haskell Cafe Subject: Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Another minor complications arises from applying these optimizations to the
original source code. When I do so, they often involve the original
expression as a sub-expression, which it then suggests the original
optimization for, despite the ranges where it is inapplicable not being
available at that call site.
I don't see a good way to handle that except to find a way to tell herbie
the actual range the sub-expression can be applied to through some kind of
source annotation.
-Edward
On Tue, Sep 22, 2015 at 4:24 PM, Mike Izbicki
Thanks :)
That's a good suggestion about not calling expressions improved when they're not. I've just pushed a new patch to the repo that fixes this issue.
Okay. Having gone through and chewed on a bunch of the optimizations it has found, I have to say I officially absolutely adore this plugin!
It would be nice if it didn't report 'improved' expressions that are the same as the original, but everything else about it is amazing.
-Edward
On Tue, Sep 22, 2015 at 3:08 PM, Edward Kmett
wrote: In this case the primary concern would be that Herbie really needs to
have
an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install.
-Edward
On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron
wrote: Does GHC HQ consider the possibility of embedding/shipping plugins like this with normal releases?
________________________________ From: Haskell-Cafe
on behalf of Edward
Kmett
Sent: Tuesday, September 22, 2015 1:39 PM To: mike@izbicki.me Cc: Haskell Cafe Subject: Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run
On Tue, Sep 22, 2015 at 12:48 PM, Edward Kmett
wrote: this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Alternately: One thing I can do with HLint is hint with module annotations
to ignore certain things. Maybe just a HLint-like hint to tell the
HerbiePlugin to ignore a function would suffice?
-Edward
On Tue, Sep 22, 2015 at 4:26 PM, Edward Kmett
Another minor complications arises from applying these optimizations to the original source code. When I do so, they often involve the original expression as a sub-expression, which it then suggests the original optimization for, despite the ranges where it is inapplicable not being available at that call site.
I don't see a good way to handle that except to find a way to tell herbie the actual range the sub-expression can be applied to through some kind of source annotation.
-Edward
On Tue, Sep 22, 2015 at 4:24 PM, Mike Izbicki
wrote: Thanks :)
That's a good suggestion about not calling expressions improved when they're not. I've just pushed a new patch to the repo that fixes this issue.
Okay. Having gone through and chewed on a bunch of the optimizations it has found, I have to say I officially absolutely adore this plugin!
It would be nice if it didn't report 'improved' expressions that are the same as the original, but everything else about it is amazing.
-Edward
On Tue, Sep 22, 2015 at 3:08 PM, Edward Kmett
wrote: In this case the primary concern would be that Herbie really needs to
have
an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install.
-Edward
On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron
wrote: Does GHC HQ consider the possibility of embedding/shipping plugins
this with normal releases?
________________________________ From: Haskell-Cafe
on behalf of Edward Kmett Sent: Tuesday, September 22, 2015 1:39 PM To: mike@izbicki.me Cc: Haskell Cafe Subject: Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run
On Tue, Sep 22, 2015 at 12:48 PM, Edward Kmett
wrote: like this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: 80% of packages in stackage that contain floating point expressions have numerically unstable expressions. The Herbie GHC plugin automatically makes these expressions numerically stable.
You can find the project on github at: https://github.com/mikeizbicki/HerbiePlugin _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Let me switch to filing these as issues rather than spamming your
announcement thread. =)
On Tue, Sep 22, 2015 at 4:27 PM, Edward Kmett
Alternately: One thing I can do with HLint is hint with module annotations to ignore certain things. Maybe just a HLint-like hint to tell the HerbiePlugin to ignore a function would suffice?
-Edward
On Tue, Sep 22, 2015 at 4:26 PM, Edward Kmett
wrote: Another minor complications arises from applying these optimizations to the original source code. When I do so, they often involve the original expression as a sub-expression, which it then suggests the original optimization for, despite the ranges where it is inapplicable not being available at that call site.
I don't see a good way to handle that except to find a way to tell herbie the actual range the sub-expression can be applied to through some kind of source annotation.
-Edward
On Tue, Sep 22, 2015 at 4:24 PM, Mike Izbicki
wrote: Thanks :)
That's a good suggestion about not calling expressions improved when they're not. I've just pushed a new patch to the repo that fixes this issue.
Okay. Having gone through and chewed on a bunch of the optimizations it has found, I have to say I officially absolutely adore this plugin!
It would be nice if it didn't report 'improved' expressions that are
same as the original, but everything else about it is amazing.
-Edward
On Tue, Sep 22, 2015 at 3:08 PM, Edward Kmett
wrote: In this case the primary concern would be that Herbie really needs to
have
an install of racket as well to do its job right. Between that and a database, there are a lot of things that aren't usually involved in a normal GHC install.
-Edward
On Tue, Sep 22, 2015 at 2:08 PM, Elliot Cameron
wrote: Does GHC HQ consider the possibility of embedding/shipping plugins
this with normal releases?
________________________________ From: Haskell-Cafe
on behalf of Edward Kmett Sent: Tuesday, September 22, 2015 1:39 PM To: mike@izbicki.me Cc: Haskell Cafe Subject: Re: [Haskell-cafe] ANN: The Herbie GHC Plugin Very nice!
Now I just need to figure out how to extract the results of these analyses and see if I can understand and apply them manually to what libraries of mine are affected, so that folks who won't or can't run
On Tue, Sep 22, 2015 at 12:48 PM, Edward Kmett
wrote: the like this plugin can derive the benefits.
-Edward
On Tue, Sep 22, 2015 at 11:17 AM, Mike Izbicki
wrote: > > 80% of packages in stackage that contain floating point expressions > have numerically unstable expressions. The Herbie GHC plugin > automatically makes these expressions numerically stable. > > You can find the project on github at: > https://github.com/mikeizbicki/HerbiePlugin > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (8)
-
Arjen
-
Bryan Richter
-
Christopher Allen
-
Edward Kmett
-
Elliot Cameron
-
Eric Seidel
-
Erik de Castro Lopo
-
Mike Izbicki