I recently implemented some new Haskell numeric types that, instead of performing calculations, can generate a rendering of the requested calculation or store units with it.
Here you see a transcript of my session with a Haskell interpreter. The mathematical statements I am entering after the ">" are standard Haskell expressions, and, as I demonstrate, normally evaluate to a single result.
Once I get a more powerful simplifier, I will probably write a LaTeX exporting function as well.
The entire implementation of this, BTW, is less than 200 lines.
NumTest> 5 + 1 * 3
8
NumTest> prettyShow $ 5 + 1 * 3
"5+(1*3)"
NumTest> rpnShow $ 5 + 1 * 3
"5 1 3 * +"
NumTest> prettyShow $ 5 + 1 * 3
"5+(1*3)"
NumTest> prettyShow $ simplify $ 5 + 1 * 3
"5+3"
NumTest> prettyShow $ 5 * (Symbol "x") + 3
"(5*x)+3"
NumTest> 5 / 2
2.5
NumTest> (units 5 "m") / (units 2 "s")
2.5_m/s
NumTest> (units 5 "m") / 2
2.5_m
NumTest> 10 * (units 5 "m") / (units 2 "s")
25.0_m/s
NumTest> sin (pi/2)
1.0
NumTest> sin (units (pi/2) "rad")
1.0_1.0
NumTest> sin (units 90 "deg")
1.0_1.0
NumTest> (units 50 "m") * sin (units 90 "deg")
50.0_m
NumTest> ((units 50 "m") * sin (units 90 "deg")) :: Units (SymbolicManip Double)
50.0*sin(((2.0*pi)*90.0)/360.0)_m
NumTest> rpnShow $ dropUnits $ ((units 50 "m") * sin (units 90 "deg"))
"50.0 2.0 pi * 90.0 * 360.0 / sin *"
NumTest> (units (Symbol "x") "m") * sin (units 90 "deg")
x*sin(((2.0*pi)*90.0)/360.0)_m
Also, I defined this in my source file:
test :: forall a. (Num a) => a
test = 2 * 5 + 3
Now, it can be used:
NumTest> test
13
NumTest> rpnShow test
"2 5 * 3 +"
NumTest> prettyShow test
"(2*5)+3"
NumTest> test + 5
18
NumTest> prettyShow (test + 5)
"((2*5)+3)+5"
NumTest> rpnShow $ test + 5
"2 5 * 3 + 5 +"
You can grab the very early experimental code with darcs get http://darcs.complete.org/num.
Haskell has no built-in support for numeric types with units, arbitrary symbols carried through computations, etc. But it was trivial to add it. This kind of extensibility is a key part of why Haskell is so amazing.
Comments
Fri, 18.07.2008 12:48
Actually, I just had a horribl e experience with VPSLink. Lon g story short, they did not pr otect our Xen4 (we were [...]
Fri, 18.07.2008 10:43
Good job, John! There is a fai rly large group here at my off ice that rides every day to wo rk, most from Goshen (ab [...]
Thu, 17.07.2008 04:46
Your earlier post inspired me and I too travel occasionally to office on bicycle. Howeve r, I could not manage it [...]
Wed, 16.07.2008 10:21
Please send the config files,I want to have an experience of it. Thanks, Regards, J ayadev Kranti
Wed, 16.07.2008 06:12
Did we file a bug yet? :) I have also been seeing this, f or at least a year, with both usb and ps2 keyboards an [...]
Tue, 15.07.2008 11:30
Hi Cliff, Thanks for the co mment! No idea what happened to your earlier one; strange. After your experience [...]
Mon, 14.07.2008 20:53
Hi John, I came back to reread this post it is one of the be st. Nice Job. I left a comme nt the day it was first [...]
Mon, 14.07.2008 10:17
Thanks to everyone for all the tips. It looks like this CON FIG_GROUP_SCHED is the most li kely culprit; I will hav [...]