[erlang-questions] Haskell on Erlang was: Flooring integer division in Erlang?
Dimitry Golubovsky
golubovsky@REDACTED
Sun Apr 27 03:52:18 CEST 2008
Robert,
On Sat, Apr 26, 2008 at 8:13 PM, Robert Virding <rvirding@REDACTED> wrote:
> 2008/4/27 Dimitry Golubovsky <golubovsky@REDACTED>:
> > I am implementing a program that converts intermediate output from a
> > Haskell compiler (Yhc Core) to Erlang Core. Therefore it is desirable
> I don't quite understand. Will you then compile Erlang Core to the Erlang VM
> and in that way get a Haskell running on Erlang? In that case cool. How are
> you representing Haskell data types in erlang?
Yes, Haskell programs running on Erlang runtime and VM is the goal. I
use some parts of Haskerl in my code, plus some pieces of the Yhc ->
Javascript converter. Not much to publish right now though.
As of now, Haskell objects are represented as tagged tuples (tag is an
atom, the first member of tuple). Basically, data types do not exist
(almost) in Yhc Core.
Numeric values are represented by themselves.
Data constructors and unevaluated thunks are tuples.
like this:
{'@dt','.CONS',_v23624,_v23625} is application of the (:) constructor
to two arguments
{'@ap',{'hs_test1','.5_c'},1,[_v26406|[]]} is a thunk - application of
the 'hs_test1':'.5_c'/1 to one argument
Lists are nested tuples (as they are nested applications of : and []
constructors). Strings may be in "compact" form (tuple consisting of a
special tag and an Erlang string), lazily converted to Haskell lists,
etc.
{'@lst',"ABCDEF"} is a compact form of a string. After the first
evaluation it would become:
{'@dt','.CONS', 65, {'@lst',"BCDEF"}} and so on.
I will definitely provide more information as things get finalized.
As a brief example:
Erlang factorial function:
fac(0) -> 0;
fac(1) -> 1;
fac(N) -> N * fac(N - 1).
yields this:
'fac'/1 =
%% Line 17
fun (_cor0) ->
case _cor0 of
<0> when 'true' ->
0
%% Line 18
<1> when 'true' ->
1
%% Line 19
<N> when 'true' ->
let <_cor1> =
call 'erlang':'-'
(N, 1)
in let <_cor2> =
apply 'fac'/1
(_cor1)
in call 'erlang':'*'
(N, _cor2)
end
while Haskell function:
fac :: Int -> Int
fac 0 = 0
fac 1 = 1
fac n = n * fac (n - 1)
yields this:
'fac'/1 =
fun (_v252_f) ->
let <_v252> =
<call 'hserl':'force'(_v252_f)>
in let <_v252_c> =
<call 'hserl':'force'(_v252)>
in case <_v252_c> of
<0> when 'true' ->
0
<1> when 'true' ->
1
<_> when 'true' ->
call 'erlang':'*'(_v252_c,call 'hs_test1':'fac'(call
'hs_test1':'.3'(_v252_c,1)))
end
Although this example is a bit artificial, it shows that sometimes
there is not so much difference between core generated from Erlang and
form Haskell.
--
Dimitry Golubovsky
Anywhere on the Web
More information about the erlang-questions
mailing list