[Haskell-beginners] Compiling C into Haskell

allan a.d.clark at ed.ac.uk
Mon Apr 12 11:40:18 EDT 2010


Hi

I'm not quite sure I understand why you would wish to compile C into 
Haskell?
It seems to me that you would then get the "worst" of both worlds. That 
is you have to program in C without using the greater abstraction of 
Haskell, but then suffer the performance hit of compiling FROM Haskell 
to machine code (yes I know some things are easier to write in C and I 
know that some things are faster in Haskell, but that aside ...).
Compilation from C to Haskell would be useful if you THEN wish to edit 
the produced Haskell code, in other words if you're hoping to port a 
currently available program in C to Haskell.  Even then I would think 
the resultant Haskell would be pretty much unreadable.
For your specific situation it would seem that you just want to use the 
foreign function interface and write some of your code in Haskell, and 
the performance heavy matrix routines in C. This can sometimes be quite 
a nice work flow;
1) write everything in Haskell
2) spot the performance bottleneck and replace those with 
handcoded/optimised C routines
This means as well that you have a back up implementation in Haskell of 
the C routines to compare against when debugging.

regards
allan


Hein Hundal wrote:
> 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-------------------------
>
>
>
>
>
>
>
>
>
>
>
>  
>
>  
>
>
>
>
>
>
>
>
>
>       
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>   


-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.



More information about the Beginners mailing list