Advice on translating code from very unrelated languages (in this case Scheme to Python)?
Asked Answered
P

5

5

Reasoning: I'm trying to convert a large library from Scheme to Python

Are there any good strategies for doing this kind of conversion? Specifically cross-paradigm in this case since Python is more OO and Scheme is Functional.

Totally subjective so I'm making it community wiki

Prescind answered 23/5, 2010 at 20:29 Comment(6)
Python isn't really an OO language, it's a language that includes OO features. It also includes some functional features, which you could use when appropriate.Barbie
@David: Python is definitely an OO language. It might not be "traditional" OO like Java or C#, but it's object-oriented nonetheless.Karissakarita
Just because you can do object-oriented programming in it doesn't make it an OO language. Or maybe it does, but then you could make the same argument to say that Python is a procedural language. Or a functional language.Barbie
Perfectly functional programming is very possible in Python. If you're pressed for time, no reason to spend great amounts of time re-writing your target.Mckissick
@Paul, a lot of anon functions are involved, in my experience python doesn't do those very wellPrescind
Actually, for the sake of everything, I could program functional code in Python. However, the piece I'm working with is specifically meant to be "Pure Python" and directly translating Scheme to Python would not really be in the spirit of the code. It's an extension to sympy to deal with differential geometry.Prescind
H
7

I would treat the original language implementation almost like a requirements specification, and write up a design based on it (most importantly including detailed interface definitions, both for the external interfaces and for those between modules within the library). Then I would implement from that design.

What I would most definitely NOT do is any kind of function-by-function translation.

Holloweyed answered 23/5, 2010 at 20:29 Comment(1)
Thanks! I figured that function-by-function wouldn't work (unfortunately this library seems tremendously mangled and hard to parse)Prescind
T
6

Use the scheme implementation as a way of generating test cases. I'd write a function that can call scheme code, and read the output, converting it back into python.

That way, you can write test cases that look like this:

def test_f():
  assert_equal(library.f(42), reference_implementation('(f 42)'))

This doesn't help you translate the library, but it will give you pretty good confidence that what you have gives the right results.

Of course, depending on what the scheme does, it may not be quite as simple as this...

Trichite answered 23/5, 2010 at 20:29 Comment(3)
how would that work exactly? So you'd check that the output of the Python with the output of the Scheme? Am I interpreting that correctly?Prescind
Yes! A simple (ish) way is to run the scheme interpreter, capture the output, and parse it into python.Trichite
Thanks, needed that clarification!Prescind
M
1

I would setup a bunch of whiteboards and write out the algorithms from the Scheme code. Then I would implement the algorithms in Python. Then, as @PaulHankin suggests, use the Scheme code as a way to write test cases to test the Python code

Mueller answered 23/5, 2010 at 20:29 Comment(1)
This seems to be the best way of going about it I thinkPrescind
D
0

Write a Python interpreter in Scheme and directly translate your program to that :-) You can start with def:

 (define-syntax def
      (syntax-rules ()
        ((def func-name rest ...)
         (define func-name (lambda rest ...)))))

 ;; test

 (def sqr (x) (* x x))
 (sqr 2) => 4
Damper answered 23/5, 2010 at 20:29 Comment(0)
M
0

If you don't have time to do as the others have suggested and actually re-implement the functionality, there is no reason you CAN'T implement it in a strictly functional fashion.

Python supports the key features necessary to do functional programming, and you might find that your time was better spent doing other things, especially if absolute optimization is not required. On the other hand, you might find bug-hunting to be quite hard.

Mckissick answered 23/5, 2010 at 20:29 Comment(1)
absolute optimization is in no way required on this (it'd be nice, but not required).Prescind

© 2022 - 2024 — McMap. All rights reserved.