* A C to OCaml helper ? @ 2008-03-12 19:42 Fabrice Marchant 2008-03-12 20:51 ` [Caml-list] " Richard Jones ` (3 more replies) 0 siblings, 4 replies; 9+ messages in thread From: Fabrice Marchant @ 2008-03-12 19:42 UTC (permalink / raw) To: caml-list Hi ! Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? Thanks, Fabrice ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 19:42 A C to OCaml helper ? Fabrice Marchant @ 2008-03-12 20:51 ` Richard Jones 2008-03-12 21:03 ` Basile STARYNKEVITCH ` (2 subsequent siblings) 3 siblings, 0 replies; 9+ messages in thread From: Richard Jones @ 2008-03-12 20:51 UTC (permalink / raw) To: Fabrice Marchant; +Cc: caml-list On Wed, Mar 12, 2008 at 08:42:17PM +0100, Fabrice Marchant wrote: > Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? camlidl (http://caml.inria.fr/pub/old_caml_site/camlidl/) is the obvious choice. It has a number of shortcomings though which I wrote about in this posting: http://tech.groups.yahoo.com/group/ocaml_beginners/message/9487 Rich. -- Richard Jones Red Hat ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 19:42 A C to OCaml helper ? Fabrice Marchant 2008-03-12 20:51 ` [Caml-list] " Richard Jones @ 2008-03-12 21:03 ` Basile STARYNKEVITCH 2008-03-12 21:09 ` Fabrice Marchant 2008-03-13 1:56 ` [Caml-list] " Christopher L Conway 2008-03-13 3:14 ` Jon Harrop 3 siblings, 1 reply; 9+ messages in thread From: Basile STARYNKEVITCH @ 2008-03-12 21:03 UTC (permalink / raw) To: Fabrice Marchant; +Cc: caml-list Fabrice Marchant wrote: > Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? Why do you want to do that? You could call most of your C routines from Ocaml. And most importantly, the main interest of ocaml is the way of thinking it. When mastered, it is a language much more expressive & much more fun. And no mechanical translation would give you that.... If it is a homework, better to start thinking & coding in Ocaml, than to translate blindly C into Ocaml. And a blind translation won't learn anything about Ocaml or functional programming. -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359 8, rue de la Faiencerie, 92340 Bourg La Reine, France *** opinions {are only mines, sont seulement les miennes} *** ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 21:03 ` Basile STARYNKEVITCH @ 2008-03-12 21:09 ` Fabrice Marchant 2008-03-13 6:41 ` Basile STARYNKEVITCH 0 siblings, 1 reply; 9+ messages in thread From: Fabrice Marchant @ 2008-03-12 21:09 UTC (permalink / raw) To: caml-list On Wed, 12 Mar 2008 22:03:51 +0100 Basile STARYNKEVITCH <basile@starynkevitch.net> wrote: > Fabrice Marchant wrote: > > Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? > > Why do you want to do that? Why do I want a tool to do the _very mechanical parts_ of the job ? A simple example : because I would be happy to get the comment brackets automatically translated ( and the C++ like C extension // too.) There are several such very automatic things like if( ) -> if then. > You could call most of your C routines from Ocaml. Yes, but in the present case, I'm interesting in a full rewriting. This is precisely because I imagine to be able to rewrite the C ( game ) program a very different and terse way, that I undertake this task. > And most importantly, the main interest of ocaml is the way of thinking > it. Absolutely : that's the reason why I play to rewrite the C program an OCaml way. > When mastered, it is a language much more expressive & much more fun. Without daring to brag about 'mastering' the language, I however declare to perfectly know this. > And no mechanical translation would give you that.... Obviously. > If it is a homework, better to start thinking & coding in Ocaml, than to > translate blindly C into Ocaml. > > And a blind translation won't learn anything about Ocaml or functional > programming. When I play chess, I always think the other player will give the best things and never suppose he has absurd ideas in mind... However, I remember you are the man who oriented me to OCaml and I'm very grateful of this, because OCaml programming is a real pleasure Best Regards, Fabrice http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=626 http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=627 http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=619 ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 21:09 ` Fabrice Marchant @ 2008-03-13 6:41 ` Basile STARYNKEVITCH 2008-03-17 19:07 ` Kuba Ober 0 siblings, 1 reply; 9+ messages in thread From: Basile STARYNKEVITCH @ 2008-03-13 6:41 UTC (permalink / raw) To: Fabrice Marchant; +Cc: caml-list Fabrice Marchant wrote: > On Wed, 12 Mar 2008 22:03:51 +0100 > Basile STARYNKEVITCH <basile@starynkevitch.net> wrote: > >> Fabrice Marchant wrote: >>> Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? >> Why do you want to do that? > > Why do I want a tool to do the _very mechanical parts_ of the job ? > A simple example : > because I would be happy to get the comment brackets automatically translated ( and the C++ like C extension // too.) > There are several such very automatic things like if( ) -> if then. > >> You could call most of your C routines from Ocaml. > Yes, but in the present case, I'm interesting in a full rewriting. This is precisely because I imagine to be able to rewrite the C ( game ) program a very different and terse way, that I undertake this task. > >> And most importantly, the main interest of ocaml is the way of thinking >> it. > Absolutely : that's the reason why I play to rewrite the C program an OCaml way. Then you have to rewrite it manually, because you have to re design your program differently (in a functional way). In particular, functional values are very common in Ocaml, and do not exist as such in C (a C pointer function is *not* a closure, you have to bring it some data); and combining both is done manually: callbacks, signal & slots [like in QT or GTK], etc... Also, in Ocaml you frequently have tiny functions : just look at how easily you can sort an array of records of person's first & last names in Ocaml (it really fits in one or two lines), and how it become harder in C. > However, I remember you are the man who oriented me to OCaml and I'm > very grateful of this, because OCaml programming is a real pleasure So you learnt that Ocaml programming paradigms are different from those in C. I'm really not convinced that any automatic tool could bring you something valuable. Because tools do not help to re design (not only refactor or translate) programs. And my advice is even to avoid doing any immediate translation by hand. Do not look often at your C code, but redesign your game (and your former experience in C game coding would help you). Perhaps this could be done incrementally, by keeping some routines in C (and calling them from Ocaml). -- Basile STARYNKEVITCH http://starynkevitch.net/Basile/ email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359 8, rue de la Faiencerie, 92340 Bourg La Reine, France *** opinions {are only mines, sont seulement les miennes} *** ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-13 6:41 ` Basile STARYNKEVITCH @ 2008-03-17 19:07 ` Kuba Ober 2008-03-17 20:33 ` Sylvain Le Gall 0 siblings, 1 reply; 9+ messages in thread From: Kuba Ober @ 2008-03-17 19:07 UTC (permalink / raw) To: caml-list On Thursday 13 March 2008, Basile STARYNKEVITCH wrote: > Fabrice Marchant wrote: > > On Wed, 12 Mar 2008 22:03:51 +0100 > > > > Basile STARYNKEVITCH <basile@starynkevitch.net> wrote: > >> Fabrice Marchant wrote: > >>> Please does it exist some tool that could do at least the very > >>> mechanical first parts of the translation of a C source to OCaml ? > >> > >> Why do you want to do that? > > > > Why do I want a tool to do the _very mechanical parts_ of the job ? > > A simple example : > > because I would be happy to get the comment brackets automatically > > translated ( and the C++ like C extension // too.) There are several such > > very automatic things like if( ) -> if then. > > > >> You could call most of your C routines from Ocaml. > > > > Yes, but in the present case, I'm interesting in a full rewriting. This > > is precisely because I imagine to be able to rewrite the C ( game ) > > program a very different and terse way, that I undertake this task. > > > >> And most importantly, the main interest of ocaml is the way of thinking > >> it. > > > > Absolutely : that's the reason why I play to rewrite the C program an > > OCaml way. > > Then you have to rewrite it manually, because you have to re design your > program differently (in a functional way). In particular, functional > values are very common in Ocaml, and do not exist as such in C (a C > pointer function is *not* a closure, you have to bring it some data); > and combining both is done manually: callbacks, signal & slots [like in > QT or GTK], etc... > > Also, in Ocaml you frequently have tiny functions : just look at how > easily you can sort an array of records of person's first & last names > in Ocaml (it really fits in one or two lines), and how it become harder > in C. > > > However, I remember you are the man who oriented me to OCaml and I'm > > very grateful of this, because OCaml programming is a real pleasure > > So you learnt that Ocaml programming paradigms are different from those > in C. > > I'm really not convinced that any automatic tool could bring you > something valuable. Because tools do not help to re design (not only > refactor or translate) programs. > > And my advice is even to avoid doing any immediate translation by hand. > Do not look often at your C code, but redesign your game (and your > former experience in C game coding would help you). Perhaps this could > be done incrementally, by keeping some routines in C (and calling them > from Ocaml). I disagree somewhat. A good starting point is to have mechanically-translated code that works, and then work on refactoring it to utilize what OCaml has to offer. In fact, C++ may provide much better translation results than C, since, if properly written, the C++ sources will utilize higher-level primitives that look better in OCaml. C is a really primitive language, by OCaml's standards ;) Cheers, Kuba ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: A C to OCaml helper ? 2008-03-17 19:07 ` Kuba Ober @ 2008-03-17 20:33 ` Sylvain Le Gall 0 siblings, 0 replies; 9+ messages in thread From: Sylvain Le Gall @ 2008-03-17 20:33 UTC (permalink / raw) To: caml-list On 17-03-2008, Kuba Ober <ober.14@osu.edu> wrote: > On Thursday 13 March 2008, Basile STARYNKEVITCH wrote: >> Fabrice Marchant wrote: >> > On Wed, 12 Mar 2008 22:03:51 +0100 >> > >> > Basile STARYNKEVITCH <basile@starynkevitch.net> wrote: >> >> Fabrice Marchant wrote: >> >>> Please does it exist some tool that could do at least the very >> >>> mechanical first parts of the translation of a C source to OCaml ? >> >> >> >> Why do you want to do that? >> > > I disagree somewhat. A good starting point is to have mechanically-translated > code that works, and then work on refactoring it to utilize what OCaml has to > offer. > > In fact, C++ may provide much better translation results than C, since, if > properly written, the C++ sources will utilize higher-level primitives that > look better in OCaml. C is a really primitive language, by OCaml's > standards ;) > In my former work i have been able to see (and to participate) to some kind of langage rewrite (PLI -> C++ for example). This is interesting, because it "mechanically work" but it is far from being "humand readable". Just to give you a couple of example: * integer arithmetic are most of the time not what you think (i.e. COBOL/PLI doesn't have the same integer representation, C play with bit arithmetic, a lot of people like i++ on pointer...) * float arithmetic is a nightmare (less than integer because you cannot do a lot of tricky stuff with it) * with 99% of the tools you will find, you will loose your comments * you will get a lot more lines than the initial program (because most of the tools do a local rewrite translating one token in langage SRC to many token in langage DST) Results: You will get a BIG program without any comments, containing tricky behavior in one langage translated to buggy source code in OCaml (i let you think about the pointer/pointer arithmetic problem). BUT i am not saying this is not possible... It is always possible, you will just get a pile of junk that works more or less... But don't think you will be able to dig into a huge amount of code autogenerated without comments and anything like that. (just for fun: imagine all C macro expanded everywhere in your program and then translated to ocaml -- take a look at stdlib.h to see how many macros it uses ;-) Conclusion of my former work: it works, but soure need to be near destination language (e.g. COBOL MVS -> COBOL MicroFocus -- which is already a nightmare). IMHO, the real good way: define piece of code that will stay in C, translate other parts, by hand, into OCaml, link both. When you feel this or that part can be rewritten, remove the C stub and replace it. And use the former C part for regression testing against the new OCaml code. Regards, Sylvain Le Gall ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 19:42 A C to OCaml helper ? Fabrice Marchant 2008-03-12 20:51 ` [Caml-list] " Richard Jones 2008-03-12 21:03 ` Basile STARYNKEVITCH @ 2008-03-13 1:56 ` Christopher L Conway 2008-03-13 3:14 ` Jon Harrop 3 siblings, 0 replies; 9+ messages in thread From: Christopher L Conway @ 2008-03-13 1:56 UTC (permalink / raw) To: Fabrice Marchant; +Cc: caml-list You could try writing a CIL visitor that outputs an OCaml transliteration (http://hal.cs.berkeley.edu/cil/). How well that would work depends on what your C source code looks like. Chris On Wed, Mar 12, 2008 at 3:42 PM, Fabrice Marchant <fabrice.marchant@orange.fr> wrote: > Hi ! > > Please does it exist some tool that could do at least the very mechanical first parts of the translation of a C source to OCaml ? > > Thanks, > > Fabrice > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [Caml-list] A C to OCaml helper ? 2008-03-12 19:42 A C to OCaml helper ? Fabrice Marchant ` (2 preceding siblings ...) 2008-03-13 1:56 ` [Caml-list] " Christopher L Conway @ 2008-03-13 3:14 ` Jon Harrop 3 siblings, 0 replies; 9+ messages in thread From: Jon Harrop @ 2008-03-13 3:14 UTC (permalink / raw) To: caml-list On Wednesday 12 March 2008 19:42:17 Fabrice Marchant wrote: > Hi ! > > Please does it exist some tool that could do at least the very mechanical > first parts of the translation of a C source to OCaml ? I do not believe such a tool exists but it would be a great educational exercise to try and write one. I can't think of any particularly difficult aspects. Maybe goto statements but they can be compiled to nested functions that call each other. -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/products/?e ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2008-03-17 20:34 UTC | newest] Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-03-12 19:42 A C to OCaml helper ? Fabrice Marchant 2008-03-12 20:51 ` [Caml-list] " Richard Jones 2008-03-12 21:03 ` Basile STARYNKEVITCH 2008-03-12 21:09 ` Fabrice Marchant 2008-03-13 6:41 ` Basile STARYNKEVITCH 2008-03-17 19:07 ` Kuba Ober 2008-03-17 20:33 ` Sylvain Le Gall 2008-03-13 1:56 ` [Caml-list] " Christopher L Conway 2008-03-13 3:14 ` Jon Harrop
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox