简体   繁体   中英

How to implement a Haskell extension?

I have several ideas of extensions for Haskell, that can be implemented by translating extended language to normal one (the extensions will provide some pragmas and keywords). What is the better way to implement them? It should be build over GHC.

One of ideas is to add keyword for "functional classes", the classes, that will be automatically defined by functions. For example, with this hypothetical extension, standard Num class can be defined like this:

class (funclass(+) a,funclass(-) a, ...) => Num a

As I see it, there are three ways you might do this:

  1. Implement a preprocessor which reads extended Haskell source code, translates it into normal Haskell, and saves that back to disk. You can then compile the translated Haskell code as normal.

  2. Implement some code with Template Haskell that will do the translation for you. (In particular, some kind of quasi-quoter may be appropriate.)

  3. Modify GHC itself. (This obviously requires recompiling GHC, and then checking you haven't accidentally broken any existing functionality.) I would suggest that this is probably a hell of a lot of work.

I was about to mention that you could write a GHC plugin - however, such plugins to no allow you to define new syntax. (Though they do allow you to define new compiler pragmas, which would then be translated into normal Haskell.) If you can get this approach to work, it means you get to avoid recompiling GHC. You would just be writing a small, self-contained plugin. But Template Haskell will probably do the job more easily.

As to whether your proposed extension is a good idea... I don't think it is, but you are of course welcome to experiment with using it.

I have several ideas of extensions for Haskell, that can be implemented by translating extended language to normal one.

SugarHaskell is built for this. It is part of the SugarJ project for extensible languages. Also see the Haskell symposium paper and package on Hackage .

Potential strong points about SugarHaskell: It comes with an extensible grammar that supports modular specification of layout constraints, so you can easily define syntax that behaves like do notation. It comes with an Eclipse plugin that is aware of the syntax extensions, so you get extensible syntax highlighting. Other features of the Eclipse plugin are also extensible (code completion, error reporting, outline view, ...).

Potential weak points about SugarHaskell: It is implemented in Java (glue code and Eclipse plugin), SDF (grammar) and Stratego (desugarings). The hackage package contains mostly a bunch of Java code. That's not necessarily a weak point, but maybe you would prefer to use Haskell to specify your desugarings instead of Stratego. No integration in other editors beside Eclipse. The Eclipse plugin is nicely extensible, but the basic support for Haskell is weak, so it is currently not really usable for large Haskell projects.

Given potential weak points, SugarHaskell is not suited for production use today, but it might be useful for experimentation and prototyping of language extensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM