Can I convert template haskell to .hs? [duplicate] - haskell

This question already has answers here:
Preferred method for viewing code generated by Template Haskell
(3 answers)
Closed 6 years ago.
I created Data with a help of Template Haskell. I use it from another program by $(code here).
So, can I convert Data to .hs file? I want to see the structure of Data.

One common way to do this is to write you normal Haskell code that uses the template Haskell and then feed GHC the option -ddump-splices and -ddump-to-file to have it output a file containing all of your original code but with the template Haskell expanded so you can see exactly what is being compiled.
Another option if you want to not see the full file is to use the pretty printer that comes with the template-haskell package. The module contains a number of helper functions for constructing a Doc which may then be rendered to a string with pprint :: Doc -> String and outputted using the IO features of the Q monad.

Related

How to embed arbitrary dynamic HTML String into Haskell's Lucid Html type?

How can I parse an html string for example <p>Test</p> and have this be converted to value that would be similar to p_ "Test"?
So the function type would be String -> Html ().
I've found the following project https://github.com/dbaynard/lucid-from-html but this generates actual Haskell source code as a String.
I'm aware that there are HTML parsing libraries, but I'm just wondering if there already exists a library that has implemented this?
http://hackage.haskell.org/package/lucid-2.9.11/docs/Lucid-Base.html#v:toHtmlRaw
toHtmlRaw "<p>Test</p>"
The above seems to work well enough, though possibly it does not check for valid syntax.

Best way to bring the contents of a textfile into a reflex project

I have a 70 line text file, whose contents I want to have as the initial value of a text area within my project. What is the best way to do this? Normally I would use readFile but I can't seem to use it in this context.
You can use Template Haskell to load the file at compile time and store its contents in a toplevel definition. The file-embed package on Hackage implements this functionality for you:
This module uses Template Haskell. Following is a simplified
explanation of usage for those unfamiliar with calling Template
Haskell functions.
The function embedFile in this modules embeds a file into the
executable that you can use it at runtime. A file is represented as a
ByteString. However, as you can see below, the type signature
indicates a value of type Q Exp will be returned. In order to
convert this into a ByteString, you must use Template Haskell
syntax, e.g.:
$(embedFile "myfile.txt")
This expression will have type ByteString.

Converting an ASTNode into code

How does one convert an ASTNode (or at least a CompilationUnit) into a valid piece of source code?
The documentation says that one shouldn't use toString, but doesn't mention any alternatives:
Returns a string representation of this node suitable for debugging purposes only.
CompilationUnits have rewrite, but that one does not work for ASTs created by hand.
Formatting options would be nice to have, but I'd basically be satisfied with anything that turns arbitrary ASTNodes into semantically equivalent source code.
In JDT the normal way for AST manipulation is to start with a basic CompilationUnit and then use a rewriter to add content. Then ASTRewriteAnalyzer / ASTRewriteFormatter should take care of creating formatted source code. Creating a CU just containing a stub type declaration shouldn't be hard, so that's one option.
If that doesn't suite your needs, you may want to experiement with directly calling the internal org.eclipse.jdt.internal.core.dom.rewrite.ASTRewriteFlattener.asString(ASTNode, RewriteEventStore). If not editing existing files, you may probably ignore the events collected in the RewriteEventStore, just use the returned String.

How can I automate testing a Template Haskell function?

I defined a function using Template Haskell which generates a function definition given some type. The type is basically
makeFunc :: Name -> Q [Dec]
Right now, I use the -ddump-splices switch with GHC to see the generated splices. How can I automate this to verify that different types yield the expected splices?
A basic approach might be to just redirect the generated splice to a file and then compare that, but the generated code may well be different since it involves various identifiers constructed via newName.

Basic definitions/difference b/w Library and Headers [duplicate]

This question already has answers here:
What's the difference between a header file and a library?
(16 answers)
Closed 9 years ago.
I'm confused about these two terms: Library and Headers.
Headers contain function definitions as far as I know, but I don't have any concept of Library. I'm a new programmer and have worked with C language a little. I am now learning C# so keep explanations in simple terms. If possible provide examples also as I've tried this link:
What's the difference between a header file and a library?
But I am unable to make an exact picture of these terms in mind.
As the link say, the interface (header) tells you how to call some functionality (without knowing how it works), while the implementation (library) is the actual functionality.
Example:
To use the printf function you need to include the header and the tells you how to call the printf function. It says, printf can be called like this
int printf ( const char * format, ... );
The library is the one which implements it
int printf ( const char * format, ... )
{
...
...
...
}
One more example:
On linux, if you want to work with xml file, there is libxml library. Suppose, if you want to read a xml file, there are function exposed like
xmlTextReaderRead, `xmlReaderForFile` etc...
These function are declare in the header file <libxml/xmlreader.h> means this <libxml/xmlreader.h> header tells you how to call the above said function, mean what parameters this function takes and what is its return value.
The library libxm2 implements these function and you have to link this library when you compile the code.

Resources