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
Thu, 03.07.2008 19:17
I recently was looking at opti ons for my blog, and decided t o try out blip.tv. Indeed thei r system appears to cate [...]
Thu, 03.07.2008 14:00
When HTML5 video comes out, it should be easy to host your o wn videos. My video site is here: http://video.nat [...]
Thu, 03.07.2008 12:51
You might want to check out [url="http://viddler.com"]Viddler[/url]. I have some command line tools for the API [...]
Thu, 03.07.2008 08:25
I haven't decided for sure yet . I found a nice review of some of them. [...]
Thu, 03.07.2008 07:53
What are you going to use to c apture/edit? You can have a look at kino, if you [...]
Thu, 03.07.2008 07:03
Thanks for the suggestions, ev eryone. To give a very brie f idea of what we have done: For the learning curve [...]
Thu, 03.07.2008 05:29
The original text was discussi ng whether religion is detrime ntal to science. For 1 it was putting the point that s [...]
Wed, 02.07.2008 16:15
Two primary concerns: compatib ility with other hardware, esp ecially MS servers; and ease o f staff updates and installs.