Compiling C into Haskell

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

One comment, The question is a dumb one, and makes the standard programmer assumption that you want one mega-language to kill all other languages. Haskell has some cases in which it's fast, quick to code, and very useful. C also has some cases in which it's fast, quick to code, and very useful. Use Haskell where the first holds, use C where the second holds, use something else where neither holds. Bob On 12 Apr 2010, at 15:10, 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
-- 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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

--- On Mon, 4/12/10, Thomas Davie
... makes the standard programmer assumption that you want one mega-language to kill all other languages.
Haskell has some cases in which it's fast, quick to code, and very useful. C also has some cases in which it's fast, quick to code, and very useful.
Use Haskell where the first holds, use C where the second holds, use something else where neither holds.
Hi Thomas, I currently don't know enough about Haskell to answer the question "Will Haskell be fast, quick to code, and very useful for this project?" For my machine learning project, I will definitely have to do a lot of complex operations on matricies. I want the abstraction power of Lisp, but all the compilers for Lisp seem to produce rather slow executables. I was hoping that Haskell might work for this project or Haskell plus the LAPACK numerical computation library. Cheers, Hein PS: I feel that I need to comment on your use of the phrase "dumb question". Some beginners are young and they might be put off by the use of language that could be interpreted as a put down. Perhaps in the future, if you want to use that phrase in a beginners forum you could put the word dumb in quotes. That way, the reader is less likely to interpret it as a put down. :)

On Mon, Apr 12, 2010 at 08:29:51AM -0700, Hein Hundal wrote:
PS: I feel that I need to comment on your use of the phrase "dumb question". Some beginners are young and they might be put off by the use of language that could be interpreted as a put down. Perhaps in the future, if you want to use that phrase in a beginners forum you could put the word dumb in quotes. That way, the reader is less likely to interpret it as a put down. :)
I agree with Hein; please don't use language like that when replying to questions on this list, even if it is only in jest, and even if the question IS dumb (which Hein's very clearly was not!). -Brent

Hein Hundal wrote:
I currently don't know enough about Haskell to answer the question "Will Haskell be fast, quick to code, and very useful for this project?"
Haskell is extremely quick to code and can be made fast, but you have to spend time learning it. It's not a language that you can just pick up like that when coming from an imperative background, especially concerning the "can be made fast" part.
For my machine learning project, I will definitely have to do a lot of complex operations on matrices. I want the abstraction power of Lisp, but all the compilers for Lisp seem to produce rather slow executables. I was hoping that Haskell might work for this project or Haskell plus the LAPACK numerical computation library.
What are the complex operations you want to do on matrices? There is the hmatrix that binds to LAPACK and is a pleasure to use, but it's mainly about matrix multiplication and other numerical matrix operations. Regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com

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
-- 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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
participants (5)
-
allan
-
Brent Yorgey
-
Hein Hundal
-
Heinrich Apfelmus
-
Thomas Davie