rwallace
rwallace

Reputation: 33405

Functional programming languages introspection

I'm sketching a design of something (machine learning of functions) that will preferably want a functional programming language, and also introspection, specifically the ability to examine the program's own code in some nicely tractable format, and preferably also the ability to get machine generated code compiled at runtime, and I'm wondering what's the best language to write it in. Lisp of course has strong introspection capabilities, but the statically typed languages also have advantages; the ones I'm considering are:

F# - the .Net platform has a good story here, you can read byte code at run time and also emit byte code and get it compiled; I assume there's no problem accessing these facilities from F#.

Haskell, Ocaml - do these have similar facilities, either via byte code or parse tree?

Are there other languages I should also be looking at?

Upvotes: 18

Views: 2705

Answers (7)

J D
J D

Reputation: 48687

I'm sketching a design of something (machine learning of functions) that will preferably want a functional programming language, and also introspection, specifically the ability to examine the program's own code in some nicely tractable format, and preferably also the ability to get machine generated code compiled at runtime, and I'm wondering what's the best language to write it in. Lisp of course has strong introspection capabilities, but the statically typed languages also have advantages; the ones I'm considering are:

Can you not just parse the source code like an ordinary interpreter or compiler? Why do you need introspection?

F# - the .Net platform has a good story here, you can read byte code at run time and also emit byte code and get it compiled; I assume there's no problem accessing these facilities from F#.

F# has a rudimentary quotation mechanism but you can only quote some expressions and not other kinds of code, most notably type definitions. Also, its evaluation mechanism is orders of magnitude slower than genuine compilation so it is basically completely useless. You can use reflection to analyze type definitions but, again, it is quite rudimentary.

You can read byte code but that has been compiled so a lot of information and structure has been lost.

F# also has lexing and parsing technology (most notably fslex, fsyacc and FParsec) but it is not as mature as OCaml's.

Haskell, Ocaml - do these have similar facilities, either via byte code or parse tree?

Haskell has Template Haskell but I've never heard of anyone using it (abandonware?).

OCaml has its Camlp4 macro system and a few people do use it but it is poorly documented.

As for lexing and parsing, Haskell has a few libraries (most notably Parsec) and OCaml has many libraries.

Are there other languages I should also be looking at?

Term rewrite languages like Mathematica would be an obvious choice because they make it trivial to manipulate code. The Pure language might be of interest.

You might also consider MetaOCaml for its run-time compilation capabilities.

Upvotes: 1

Paul Johnson
Paul Johnson

Reputation: 17786

The Haskell approach would be more along the lines of parsing the source. The Haskell Platform includes a complete source parser, or you can use the GHC API to get access that way.

Upvotes: 3

dhaffey
dhaffey

Reputation: 1374

You might check out the typed variant of Racket (previously known as PLT Scheme). It retains most of the syntactic simplicity of Scheme, but provides a static type system. Since Racket is a Scheme, metaprogramming is par for the course, and the runtime can emit native code by way of a JIT.

Upvotes: 5

Ocaml has:

  • Camlp4 to manipulate Ocaml concrete syntax trees in Ocaml. The maintained implementation of Camlp4 is Camlp5.

  • MetaOCaml for full-scale multi-stage programming.

  • Ocamljit to generate native code at run time, but I don't think it's been maintained recently.

  • Ocaml-Java to compile Ocaml code for the Java virtual machine. I don't know if there are nice reflection capabilities.

Upvotes: 10

Don Stewart
Don Stewart

Reputation: 137957

Haskell's introspection mechanism is Template Haskell, which supports compile time metaprogramming, and when combined with e.g. llvm, provides runtime metaprogramming facilities.

Upvotes: 10

wheaties
wheaties

Reputation: 35980

I'd also look at Scala or Clojure which come with them all the libraries that have been developed for Java. You'll never need to worry if a library does not exist. But more to the point of your question, these languages give you the same reflection (or more powerful types) that you will find within Java.

Upvotes: 2

Brian
Brian

Reputation: 118865

Not really an answer, but note also the F# Quotations feature and library, for more homoiconicity stuff.

Upvotes: 6

Related Questions