3
$\begingroup$

My question is basically how one can produce a python program from a functional programming language such as Lean or Haskell.

Personally, I am attracted to the functional programming style and some of the special capabilities (such as theorem proving). However, in practice, mature scientific computing packages are often written in python or some other imperative languages.

So I am wondering, if I write a small library (e.g. for matrix or numerical computation) in Lean (or Haskell), is there a good way to translate these into python code that use scientific libraries such as numpy and pytorch? What needs to be done conceptually?

More specifically, is this task a string-to-string translation from Lean to Python? Or, does it involve analyzing the syntax of both the source and the target languages, and translate between the syntax? Or does it require something else?

In the imperative world (more or less), I know that one can start from a language like Haxe and translate to the python target and many other targets, probably because Haxe and these target languages are quite similar. But I am not sure Lean (or other functional languages) has this kind of capabilities.

I realize that this question is a bit general. What I am interested is knowing (the principles of) how to generate python code or translate into python from a functional language like Lean. If I write a small library of matrix computation in Lean, how would I generate the corresponding python code? What is the proper way?

$\endgroup$
1
  • $\begingroup$ If you just want a one-off translation of a specific (smallish) codebase, and you want it to be idiomatic and readable in the target language (not the unreadable spaghetti that generated code typically is), the traditional way is to translate it by hand. $\endgroup$ Commented 2 hours ago

1 Answer 1

3
$\begingroup$

To convert a Haskell program to pure Python code, you could in theory:

  1. Identify the subset of Haskell functionality which your program needs
  2. Define an intermediate representation which abstracts that functionality away from Haskell-specific syntax (e.g. expands "syntax sugar" into multiple semantic steps)
  3. For each instruction in that intermediate representation, either a) identify a direct correspondence to Python syntax; or b) create an emulated implementation in Python
  4. Convert the Haskell program to the intermediate representation
  5. Convert the intermediate representation to pure Python code

This is how "asm.js" (a predecessor of WebAssembly) worked - a virtual machine was written in pure JavaScript which emulated a specific machine language; and then compilers were written targeting that machine language.

However, this is probably not the best way to achieve your aim.

Your described use case does not require pure Python code, it requires code you can call from Python. The usual way of achieving this kind of inter-operability is using "bindings" or "foreign function interface" ("FFI") wrappers.

Large parts of libraries like NumPy and SciPy are written in various language like C, C++, and (perhaps surprisingly) Fortran, rather than Python. Since the aim in this case is performance, converting to Python code would defeat the purpose - it would execute slower than hand-coded Python for the same algorithms.

Instead, the compiled code is made available to Python functions via "extension modules".

So your actual process would look more like this:

  1. Compile your Haskell program to native machine code
  2. Create a C header which exports function for calling into that compiled code
  3. Create a Python extension module which exposes those entry points to Python programs
  4. Write and execute a Python program making use of the module

From a theoretical point of view, what you are doing is converting both the Haskell and Python code to a third language, which in this case is the machine language implemented by your hardware. In other cases, the target language might be JVM bytecode, .NET IR, or WebAssembly.

$\endgroup$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.