News for hackers. Stuff that matters.

Ask HN: Transpiling a dynamically typed language to a statically typed language

Hi, I have been pondering what I want to do for my master thesis (2+ years away) and I have an idea. I am writing to ask if the question has a known answer or is even theoretically possible to conduct.

My idea is to write a source-to-source compiler from a Lisp_2 to Rust, but I do not know if it is possible to transpile from a dynamic language to a statically typed language. Is it?

( 8 comments | 5 points | on HN )
Re:Ask HN: Transpiling a dynamically typed language to a sta... by nakkaya on Saturday June 11, 2016 @01:31PM (#11884347)
It is possible. Some scheme compilers use C as their intermediate language. Scheme to C to binary. (Gambit/Chicken/Stalin etc.). I have a project [1][2] that compiles a subset of Clojure to C++11 that is written using literate programming [3]. It thoroughly documented and should give you an idea on how to build your own.

[1] http://dropbox.nakkaya.com/builds/ferret-manual.html [2] http://nakkaya.com/2011/06/29/ferret-an-experimental-clojure... (Old version for more documentation on the idea.) [3] https://en.wikipedia.org/wiki/Literate_programming

Re:Ask HN: Transpiling a dynamically typed language to a sta... by malisper on Saturday June 11, 2016 @12:47PM (#11884125)
In one of the later chapters, the book Lisp in Small Pieces (great book), shows you how to write a a Scheme compiler to C. The general idea is you create a structure that contains a union type of all types in the dynamic language, and also a tag that tells you which type it is.
Re:Ask HN: Transpiling a dynamically typed language to a sta... by duncanawoods on Saturday June 11, 2016 @05:13PM (#11885359)
That doesn't sound hard to me - you just use a high level type that implements the coercion/conversion rules of the dynamic lang types.

GC lang to non-GC lang is a more interesting challenge. If anyone has transpiled a gc lang to C++ I'd love to know how they did it.

Re:Ask HN: Transpiling a dynamically typed language to a sta... by szemet on Saturday June 11, 2016 @12:27PM (#11884036)
Re:Ask HN: Transpiling a dynamically typed language to a sta... by ryanlm on Saturday June 11, 2016 @01:09PM (#11884238)
My honest opinion is that unless you've written C, or assembly, I don't consider you a real programmer.
Re:Ask HN: Transpiling a dynamically typed language to a sta... by leksak on Saturday June 11, 2016 @01:34PM (#11884367)
What does this have to do, at all, with the question?
Re:Ask HN: Transpiling a dynamically typed language to a sta... by bbcbasic on Sunday June 12, 2016 @12:11AM (#11886601)
Thats a true Scotsman argument.
Re:Ask HN: Transpiling a dynamically typed language to a sta... by metalon on Saturday June 11, 2016 @01:30PM (#11884343)
Ok.