9 ms·
Corrode: C to Rust translator written in Haskell
- felixangell1024 10y agoThe name "Corrode" doesn't seem very positive given the purpose of this program...
- glandium 10y agoRust is a type of corrosion.
- felixangell1024 10y agoAh very true. I interpret it as (from Googles define) "destroy or weaken (something) gradually." i.e. destroy/weaken the C code into Rust... Maybe it's just me, though.
- microcolonel 10y agoThey probably should've used a more specific term; like "Oxidant".
- dmytrish 10y agoExactly, _Corrode_ makes C code _rusty_!
- adamnemecek 10y agoIronically though the name rust is from the species of fungi https://en.m.wikipedia.org/wiki/Rust_(fungus) https://en.m.wikipedia.org/wiki/Rust_(fungus)
- fahrradflucht 10y agoI find the name creative, kind of funny and all in all very good.
- felixangell1024 10y agoI guess you could say its ironic in terms of how I think of it anyway.
- Bromskloss 10y ago> ironic I don't even know what is and what isn't a pun anymore.
- nathancahill 10y agoYeah, let's not tarnish this thread any further.
- favorited 10y agoTell us how you really FEel.
- gpm 10y agoIt also mirrors the name "oxidation" that Mozilla uses to talk about adding rust code to firefox.
- outworlder 10y agoOnly if you are talking about iron corrosion. If you own an aluminum Macbook, for instance, it was oxidized (or corroded) during manufacturing, on purpose. This protects the metal from further corrosion. You could say it's what this project does.
- therestisgone 10y agoCarbon doesn't corrode very well.
- SilasX 10y agoWell, it's just building off the same (negative) connotations of "Rust", which I think was itself a questionable name, trivial though it might seem. I've been looking at newer languages recently, and I see a lot of promise in Nim -- which renamed itself from Nimrod after users warned about what it connotes. Rust could take a cue.
- eridius 10y agoWell, Rust isn't actually named after iron oxide. It's actually named after the fungus (https://en.wikipedia.org/wiki/Rust_(fungus) https://en.wikipedia.org/wiki/Rust_(fungus)). Most people just don't know about the fungus.
- SilasX 10y agoWhen it comes to connotations, "what people know about" is more important than the intent.
- coldtea 10y agoWhen it comes to connotations, the one's associated with Rust are beyond negligible...
- SilasX 10y agoHow so? For me, "rust" conjures images of a rusty nail, rust getting in your tap water, a machine in disrepair from rust, and becoming "rusty" at some skill. And I don't think I'm alone.
- coldtea 10y agoAnd Python conjures images of dangerous snakes (and people getting killed by them), Go conjures images of that argument with my SO, when she told me "go!", C# conjures images of sharp objects like knives, and PHP conjures images of programming in PHP. It's just a name, nobody really cares about rusty nails and rust in tap water when discussing Rust the programming language. And by nobody I mean nobody in the statistical significant sense. It's really bikeshedding.
- jswny 10y agoMissed the chance to name it "Crust."
- steveklabnik 10y agoThere are already several projects called crust, including https://crates.io/crates/crust https://crates.io/crates/crust
- geonnave 10y agoWell then it could be `CRusHT` - C to Rust Haskell Translator
- cannonpr 10y agoI understand the world play, but perhaps it's a misunderstanding of Rusts name origin ? https://www.reddit.com/r/rust/comments/27jvdt/internet_archaeology_the_definitive_endall_source/ https://www.reddit.com/r/rust/comments/27jvdt/internet_archa... It's after a fungus https://en.wikipedia.org/wiki/Rust_(fungus) https://en.wikipedia.org/wiki/Rust_(fungus)
- kevinastone 10y agoTheir logo is a rust colored sprocket. Rust is full of iron/corrosion/oxidation references.
- coldpie 10y agoOh great, I can't wait to hear this "well, actually"[1] for the rest of my life. [1] https://www.recurse.com/manual#sub-sec-social-rules https://www.recurse.com/manual#sub-sec-social-rules
- SyneRyder 10y agoI was going to downvote you for a snarky response (I felt the reply explaining that Rust also has iron/oxidation references was a better response)... ... but that's a really good link that you provided. I've never read that before & it's a great resource for thinking about how to behave socially as a programmer. While I don't agree with all of it, I love their "don't pile-on" and "don't bring negativity from outside" approach. (Plus I'm guilty of the 'feigning surprise' rule.) +1 for the really excellent link.
- Bromskloss 10y agoWhy are they talking specifically about _feigning_ surprise? Isn't the effect the same even if you're honestly surprised?
- kbenson 10y agoIf you're honestly surprised (and can't/don't hide it), the effect on the other person may be similar, but that's the nature of communication. Feigning surprise is manufacturing a situation with negative aspects.
- DaGardner 10y agoas many "transpilers" / compilers, whatever you might name them, it lacks example input output. I want to see how my new rust code base looks light, does it compile with some heuritics, or just 1:1 C to rust primitives?
- steveklabnik 10y ago> Because the project is still in its early phases, it is not yet > possible to translate most real C programs or libraries. It is currently trying to port over semantics exactly, so the Rust code is far from idiomatic Rust. Doesn't mean it's not useful, just saying that it's trying to be 1:1.
- moosingin3space 10y agoI guess the next stage would involve translating common non-idiomatic patterns into idiomatic Rust. Looks like this could be a job for a community-managed database!
- stcredzero 10y agoThis is best handled on a per-project or per-organization basis. I would have such a project concentrate on the tooling for maintaining and developing such databases.
- Manishearth 10y agoOn the rust subreddit someone tongue-in-cheek suggested `cargo clippy | rustfix` to be used in conjunction with this tool for better rust code. But that actually could work! Clippy has a ton of lints that make your code more idiomatic, and rustfix basically takes diagnostic output and applies suggestions (still WIP). Clippy is geared towards making human-written unidiomatic code better, so it might not catch some silly things in this tool's output but or certainly could be extended to do that.
- moosingin3space 10y ago
- Mathnerd314 10y agoThe code has a lot of special cases. Could these be eliminated using machine translation techniques?
- DanWaterworth 10y agoTime to start sending PRs [1], :P [1] https://github.com/search?utf8=%E2%9C%93&q=language%3Ac https://github.com/search?utf8=%E2%9C%93&q=language%3Ac
- steveklabnik 10y agoI would hope that when this project is far enough along to feasibly do such a thing, that people don't. It's really rude.
- DanWaterworth 10y agoTotally agree, it was a joke.
- wspeirs 10y agoIt's too bad this is written in Haskell. I don't have anything against Haskell, it is just not as popular a language as others.[1] Any ANTLR target language would have been a solid choice.[2] This way more of the community could contribute. This is an invaluable tool if we're truly going to see a shift from C (or C++) to Rust. [1] http://pypl.github.io/PYPL.html http://pypl.github.io/PYPL.html [2] http://www.antlr.org/download.html http://www.antlr.org/download.html
- vvanders 10y agoI've used ANTLR in anger a few times and for some reason it's always left a bad taste in my mouth. I always seem to spend more time debugging how ANTLR works rather than doing the work I set out to do. Granted most of my use cases was building a simple DSL so it might be different when talking about whole source conversion.
- partisan 10y agoThe documentation is really spare, but coincidentally, the books written by Terrence Parr are good reads and with the right time investment, make dealing with Antlr feel less like voodoo and more like software engineering.
- DanWaterworth 10y agoI think people interested in compilers are disproportionately Haskell inclined.
- steveklabnik 10y agoThe author told me that one of the reasons he chose Haskell was that there was already great tooling around working with C source code: https://hackage.haskell.org/package/language-c https://hackage.haskell.org/package/language-c
- masklinn 10y agoIn fact they have explicitly documented it: http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-how-you-can.html http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-... > The only reason I wrote Corrode in Haskell is because I couldn't find a complete enough C parser for Rust, and I was already familiar with the language-c parser for Haskell.
- pierrec 10y agoI was curious about how this worked so I looked into the source a little (even though Haskell isn't exactly my cup of tea), and WOW... This is just amazing. The most important part of the source is highly educative literate haskell: https://github.com/jameysharp/corrode/blob/master/src/Language/Rust/Corrode/C.md https://github.com/jameysharp/corrode/blob/master/src/Langua...
- vvanders 10y agoThat's an incredibly brilliant idea, there's some serious craftsmanship going on in that source file. Kinda taking Rust's doc unit tests format to a whole new level.
- moosingin3space 10y agoRust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.
- steveklabnik 10y agohttps://github.com/rust-lang/rust/issues/26097 https://github.com/rust-lang/rust/issues/26097
- DanWaterworth 10y agoWith the literate style, code is prefixed and comments are raw. So, instead of: -- foo is a function foo :: String -> String It's foo is a function > foo :: String -> String
- vvanders 10y agoYou could probably hack something together with a build.rs and a preprocessor, but that would probably not be very nice long term.
- 10y ago
- tinco 10y agoAbsolutely blown away by the detail of the documentation. The main logic of this project is in a literate haskell file you can easily read on GitHub. https://github.com/jameysharp/corrode/blob/master/src/Language/Rust/Corrode/C.md https://github.com/jameysharp/corrode/blob/master/src/Langua... I wonder how readable is to someone who isn't experienced in Haskell. To me reads like a breeze, but I have a project using the exact same parsing library so maybe that puts me at an advantage. The language-c library he uses is an excellent one, it's a fully spec compliant C parser that's well maintained. I've based my C compiler on it and I haven't encountered any C code it couldn't parse yet. One time I upgraded to a new OSX and Apple added some stupid thing to their headers that broke the parser and a fix was merged within days. This means it takes away the entire headache of parsing C leaving just the actual compiling.
- eridius 10y agoBy "some stupid thing to their headers" are you referring to nullability annotations? I'm not sure what else Apple would have added to pure C headers (as opposed to obj-c) any time in the past few years.
- solidsnack9000 10y agoI would like to know, too.
- neat159 10y agoMe too!
- tinco 10y agoI wanted my compiler to be able to compile a hello world. Something which would be trivial in any language, though of course not C. In C the first line of hello world is "#include<stdio.h>". No doubt known to you, that file is a veritable quagmire of incomprehensible C constructions, specific to each operating system. So I spent a few too many nights getting my compiler to get any possible C declaration known to mankind into its symboltables, just so I could get it to the code generator phase and emit all of three or so instructions. I forgot what the change was, and I can't find the commit that fixed it, as it seems active development on the library has resumed.
- danidiaz 10y agoThat's an impressive use of literate programming. By coincidence, I had just read this post by John D. Cook "Literate programming: presenting code in human order" http://www.johndcook.com/blog/2016/07/06/literate-programming-presenting-code-in-human-order/ http://www.johndcook.com/blog/2016/07/06/literate-programmin...
- nategri 10y agoThis is probably the most "Hacker News" thing I've ever seen.
- valarauca1 10y agoWe won't reach peak HN until somebody writes an ARC to Node.js transpiler in Haskell.
- michaelcampbell 10y agoMaybe it's me having confirmation bias, but the transpiler "market" seems to favor Haskell heavily. "...in Haskell" almost seems superfluous anymore when talking about implementations.
- wink 10y agoI do get that Haskell is useful to be taken as a tool for these kind of code transformations (at least I have seen quite a few of those) but I am always a bit surprised that people would start such a project in a language that has -per se- nothing to do with either the source or the target language. I know, I know, it doesn't always have to be this way, but I am very much of the opinion that everytime good tools in an ecosystem are written in the language in said ecosystem you get a lot more (and meaningful) contributions. Best examples: rake (and everything in the ruby ecosystem basically), the amount of people touching ruby c code is very small compared to all the 'standard tools', or cargo.
- duaneb 10y agoHaskell is very good at writing correct parsers easily—that's one thing. This is part of the reason it was chosen as an early perl6 test bed via pugs. Rust is getting there (I'm a big fan of the lalrpop library) but the ecosystem is no where near as mature as Haskell's for feature-complete libraries. The pace of development of the rust ecosystem is mind-boggling, though—I never guessed that rust would have taken off in popularity as much as it has.
- samatman 10y agoIf you like lalrpop, check out nom: https://github.com/Geal/nom https://github.com/Geal/nom
- masklinn 10y ago> I do get that Haskell is useful to be taken as a tool for these kind of code transformations (at least I have seen quite a few of those) but I am always a bit surprised that people would start such a project in a language that has -per se- nothing to do with either the source or the target language. The author explained this in a blog post[0]: Haskell has a very complete C parser library with a nice API[1] which the author already knows, Rust doesn't; furthermore since one of the project's goal is to be as syntax-directed as possible the translator is straightforward and should be understandable with very little understanding of Haskell (which can be bootstrapped from understanding Rust) [0] http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-how-you-can.html http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-... [1] http://hackage.haskell.org/package/language-c http://hackage.haskell.org/package/language-c
- serge2k 10y ago> Partial automation for migrating legacy code that was implemented in C. (This tool does not fully automate the job because its output is only as safe as the input was; you should clean up the output afterward to use Rust features and idioms where appropriate.) This was my immediate concern. Is there any chance this tool can produce anything close to clean, safe, idiomatic, rust code?
- nemaar 10y ago> Is there any chance this tool can produce anything close to clean, safe, idiomatic, rust code? That is generally not possible, unless the C code only uses specific patterns known by the converter tool. That's very unlikely, considering that people write C code to be 'quick' and usually use all kinds of tricks.
- steveklabnik 10y agoThea author wanted me to drop a link to his blog post on contributing: http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-how-you-can.html http://jamey.thesharps.us/2016/07/translating-c-to-rust-and-...
- kerkeslager 10y agoThis is the coolest thing I've seen on HN in a long time, and useful to boot. Hopefully this will be a very big help to people moving over to Rust from C for its safety and type-checking. In general I don't support rewrites because, as many experienced programmers have pointed out, rewrites often make many of the same mistakes as the program they're rewriting. But transpilation allows us to keep the code with all the fixes to those mistakes. In theory I'm a big supporter of Rust. I strongly feel that we should be using stronger-typed languages than C for developing security-critical applications, and from the outside, it looks like Rust solves a lot of my criticisms of C without giving up any benefits of C. A transition over to Rust could be a big win for security and reliability of many systems. However, I'm reluctant to devote time to learning Rust primarily because it's not supported by GCC (or any other GPL compiler that I know of). I hope the next cool thing that that the Rust community does is to continue the work done by Philip Herron[1] on a Rust front-end for GCC. I know the classic response to this is, "Do it yourself!" but there are too many other areas of Open Source that are higher priorities for me, so sadly this will have to be done by someone else if it happens at all. [1] https://github.com/redbrain/gccrs https://github.com/redbrain/gccrs
- trajing 10y agoI'm curious why you have an issue with the mostly-MIT licensed main Rust compiler. Could you explain?
- kerkeslager 10y agoI'm hesitant to build on a platform that could be appropriated by corporate interests. An example of this is the way Apple has appropriated BSD for MacOS. The BSD code is still open, but the tooling and funding comes from Apple to an extent which allows Apple to determine the direction to some extent. I have no problem with MIT for smaller tools, but the compiler is too foundational a dependency to take risks on, and it needs better protections--protections which are afforded by the GPL and the FSF's funding of GNU projects.
- jjnoakes 10y agoI also would love to see a gcc front-end for rust, but my reasons are not related to the license. I have to support platforms that gcc has backend for, but LLVM does not.
- eutectic 10y agoI wonder if there would be any point in using this to fuzz the Rust compiler. On the one hand, you could use CSmith with a C compiler as a convenient oracle, but on the other you would only be covering a very limited subset of e.g. the type system.
- loeg 10y agoHas anyone tried it on some real-world codebases? How about kernel code? It would be very exciting to improve real-world crash safety and security by e.g. converting popular drivers quickly and automatically, followed by a manual pass applying safer Rust semantics.
- codygman 10y agoTrying to get to computer to do exactly this.
- steveklabnik 10y agoThe README explicitly states that this is far too early for real programs. It doesn't come close to supporting all of C yet.
- haimez 10y agoHere we have it gentlemen: HN bingo.
- deleted 10y ago[deleted]