[Haskell-cafe] Evaluating arithmetic expressions at run time

Bulat Ziganshin bulatz at HotPOP.com
Sat Jan 28 06:45:52 EST 2006

Hello Andrew,

Saturday, January 28, 2006, 1:38:08 PM, you wrote:

AS> def myeval(x, y, op)
AS>   x.send op, y
AS> end

AS> I had a feeling this sort of dynamic sending of messages
AS> to objects at run time was impossible in Haskell, hence
AS> my question. What I'm still unsure about is why this sort
AS> of thing is impossible in Haskell. Is it a fair comment
AS> to state that this sort of thing is impossible in Haskell
AS> as a consequence of its static typing? Or could it be done
AS> in a static typed language with more run time support?

it's because Haskell is compiled laguage and in compiled program "a+b"
represented just by asm instructions and don't had any link with the
string "+". To have such link it's necessary to had run-time typing
information, and current Haskell implementations has that in the
module Data.Dynamics. but even this information in current state don't
know anything about "+" and other operations, it is only informatio
about types itself. so, the anser: that can be done, but there is no
much need in it. on the other side, there is a huge area of "generic
programming" techniques that is something like to run-time
investigation of type structure, that is available in Ruby. using this
one can, for example, traverse some complex data structure that
uses hundreds of different types, and replace, say, only Int's with
another values. Data.Dynamics is for such types of tasks

AS> Despite being longer, overall I prefer the Haskell version
AS> because it is faster and "safer" (in that a number of run
AS> time errors in the Ruby version are caught at compile time
AS> in the Haskell version).

yes, and that is unvaluable for real-world large projects

Best regards,
 Bulat                            mailto:bulatz at HotPOP.com

More information about the Haskell-Cafe mailing list