Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Haskell extension?

Tags:

haskell

ghc

ghci

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
like image 819
Anon Imous Avatar asked Feb 08 '14 09:02

Anon Imous


Video Answer


1 Answers

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 do not 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.

like image 126
MathematicalOrchid Avatar answered Oct 01 '22 07:10

MathematicalOrchid