Hacker News new | past | comments | ask | show | jobs | submit login

I took the insertion_sort impl from the bottom of the post and asked gpt4 to rewrite it into idiomatic Rust:

    pub fn insertion_sort(n: i32, p: &mut [i32]) {
        for i in 1..n as usize {
            let tmp = p[i];
            let mut j = i;
            while j > 0 && p[j - 1] > tmp {
                p[j] = p[j - 1];
                j -= 1;
            }
            p[j] = tmp;
        }
    }

    fn main() {
        let mut arr1: [i32; 3] = [1, 3, 2];
        insertion_sort(3, &mut arr1);
        // …
    }
I guess if this actually works, we can translate massive amounts of internal C libraries into human readable Rust... good stuff.

(funnily enough, passing in the "original" code without the `unsafe extern "C"` part makes it produce the exact same output as the above)




I don't believe GPT is built for this still. There's a too big risk it will fill in another implementation from its training instead of adapting the input.

Here, who says the idiomatic translation is not .sort()? It should use the stdlib.


I say the idiomatic translation of a function named insertion_sort() should not use the stdlib sort. The algorithm used by Rust’s stdlib is PDQsort.

In general translations should stay as close to the original as possible, while eliminating any possibility of segfaults.


That's a literal translation, not to idiomatic Rust - in my opinion. All depends on what the semantics of the program should be.


I wonder if there could be a synthesis of traditional testin g / verification / compiler technology that would help in filtering for correctness. Like property/fuzz testing that automatically checks for deviations in translated vs original by sampling the input space? Or symbolic execution that do the same. And also ask GPT to find a difference in semantics.. and verify its answer to check for hallucination.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: