pseudo-lang / pseudo

transpile algorithms/libs to idiomatic JS, Go, C#, Ruby
MIT License
685 stars 43 forks source link

Notions of side-effecting/monads? #8

Open JoshuaGross opened 8 years ago

JoshuaGross commented 8 years ago

I've been playing around with similar ideas recently and was considering what kind of information you'd need to encode the same logic that could be transpiled to Java as well as Haskell. Have you thought at all about AST decorations that would indicate, for example, that something would need access to IO to execute?

alehander92 commented 8 years ago

That would be actually easy to implement. The problem with most kinds of compilers / transpilers is that they can't see the whole picture and they can't assume that there is no external dependency that e.g. uses I/O. Pseudo-Python does whole program analysis and it supports a limited subset of external dependencies(currently a subset of the standard library) for which it can maintain all kind of metadata. It would be trivial to add "uses IO" and "pure"(as in no other side effects) markers to the current type signatures and to add a Pseudo middleware that infers them for user-defined methods / functions.

The other issue with converting to Haskell / Clojure is local variable changes. Pseudo AST semantics are similar to those of a classic imperative language in order to support easily Python, Ruby, JS, C# transpilers as a target. That means that often logic in a function can be encoded with .push / []= / sort in place / a lot of temp variables. It's easy to convert that to nested let/letrec in Clojure or let in Haskell, however it would be probably very ugly. It would be nice to add a middleware that turns that kind of heavy-imperative style of code to more functional style so the actual generated code would be more idiomatic.