[Haskell-beginners] Compiling C into Haskell

Hein Hundal hundalhh at yahoo.com
Mon Apr 12 10:10:12 EDT 2010


Hi,

   I have been playing around with Haskell for about a month now and reading the nice book "Real World Haskell."  My main reason for learning Haskell is that I want to code up some machine learning projects (heavy use of matricies).  Normally, I use Mathematica and mix it with C++, but Mathematica is proprietary, slow, and can't produce executable while C++ is verbose.  Learning Haskell has been fun; however, I have been a little worried that I will sacrifice too much performance when coding in Haskell. 

   So, I recently asked one of my friends the following question, "Say you had a C program.  Can you always convert it to Haskell in such a way that the compiled Haskell is not too slow and does not need too much memory?"  Supposing that too slow means slower than 1/4 the speed of C and too much memory means twice the memory of C.  Hopefully, someone that knows Haskell well can comment on this question.  

   I am not sure, but I think the answer is yes, such a conversion can always be done and creating a C to Haskell compiler with the above performance constraints is not extremely hard.  I started thinking about how a compiler might convert a simple C program into Haskell.  Below I will paste a C program and the compile-by-hand Haskell code.  It seems to me that the ideas I used to create the Haskell code can be implemented in a compiler that converts a simple subset of C into Haskell.  I was thinking about restricting the C to one data type 32bit-integers, arithmetic (+-*/%), assignment (=), comparison (<,>,==,<=,>=), the if-condition-codeblock construct, and the while-condition-codeblock construct.  (I would also like to do integer arrays, but I have not read about mutable arrays and monads yet.)

   Any comments?

Cheers,
Hein H.

--------------------C to Haskell------------------------
module Main where

-- Convert a C-Program line by line into Haskell
--
-- 1-- #include <stdio.h>
-- 2-- void main()
-- 3-- {
-- 4--    long i,j;
-- 5--    i=7;
-- 6--    j=0;
-- 7--    while(j<10000)
-- 8--    {
-- 9--       if ((i % 17)== 11) 
--10--          i = i*2;
--11--       if ((i % 35)== 12) 
--12--          i = i+13;
--13--       if (i> 1000) 
--14--          i = i - 1000;
--15--       i++;
--16--       j++;
--17--    }
--18--    printf("%ld", i);
--19--}


--assignment statements
line5  (i,j)  = (7,j)
line6  (i,j)  = (i,0)
line10 (i,j)  = (i*2,j)
line12 (i,j)  = (i+13,j)
line14 (i,j)  = (i-1000,j)
line15 (i,j)  = (i+1,j)
line16 (i,j)  = (i,j+1)

--while statement
line7test (i,j) = j >=100000000
line7 (i,j) = until line7test body9To16 (i,j)

--if statements
line9  (i,j) = if ((mod i 17) == 11) 
   then line10 (i,j) else (i,j)
line11 (i,j) = if ((mod i 35) == 12) 
   then line12 (i,j) else (i,j)
line13 (i,j) = if (i>1000) 
   then line14 (i,j) else (i,j)

-- code blocks
mainprogram :: (Int, Int) -> (Int, Int)
mainprogram = line7  . line6  . line5
body9To16   = line16 . line15 . line13 . line11 . line9 


main :: IO ()
main = putStrLn(show(mainprogram (0,0)))

-----------------end of program-------------------------











 

 









      


More information about the Beginners mailing list