1

I have a specification and I wish to transform it into a library. I can write a program that writes out Haskel source. However is there a cleaner way that would allow me to compile the specification directly (perhaps using templates)?

References to manuals and tutorials would be greatly appropriated.

Q the Platypus
  • 825
  • 1
  • 7
  • 13
  • "I can write a program that writes out Haskel source." If that program is written in Haskell, invariably the Haskell source will be represented by some AST at some point - convert that ast to the TH ast and that AST can be inserted directly as code by TH. You can't splice a module, or its imports and exports, so this has its limitations. Lastly "references to tutorials and manuals" are off topic on SO. – user2407038 Jan 26 '16 at 02:38
  • https://hackage.haskell.org/package/haskell-generate – Erik Kaplun Jan 26 '16 at 02:57
  • Is there any reason to transform it into a library instead of just writing an interpreter? – PyRulez Jan 26 '16 at 04:09
  • I want to be able to expose the invariants and types that the standard defines to the programmer at compile time. This way the programmer can be confident that what they have written conferment code. – Q the Platypus Jan 27 '16 at 00:32
  • This specification that you have, what's it written in? Haskell is sometimes called an executable specification language. Might look at specification languages such a Z (Zed), B-Method, Alloy, TLA, VDM-SL. Maaybe also look at "constructive programming" - a good intro is de Moor and Gibbon's "Bridging the Algorithm Gap: ..." – ja. Jan 29 '16 at 17:25
  • @ja. Unfortunately it is written in HTML. However the HTML has a structure constant enough to scrape all the normative information that I'm interested in out. – Q the Platypus Feb 01 '16 at 04:28

1 Answers1

1

Yes, you can use Template Haskell. The are a couple of approaches to using it.

One approach is to use quasiquotation to embed (parts of) the text of the specification in a quasiquotation within a source file. To implement it, you need to write a parser of the machine specification that outputs Haskell AST. This might be useful if the specification is relatively static, it makes sense to have subsets of the specification, or you want to manually map parts of the specification to different modules. This may also be useful, in addition to a different approach perhaps, to provide tools for users of the library to express things in terms of the specification.

Another approach is to execute IO in a normal Template Haskell splice. This would allow you to read the specification from a file (see addDependentFile too in this case), the network (don't do this), or to execute an arbitrary program to produce the Haskell AST needed. This might be more useful if the specification changes more often, or you want to keep a strict separation between the specification and code.

If it's much easier to produce Haskell source than Haskell AST, you can use a library like haskell-src-meta which will parse a string into Template Haskell AST.

Derek Elkins left SE
  • 2,079
  • 12
  • 14