well-typed / hs-bindgen

Automatically generate Haskell bindings from C header files
20 stars 0 forks source link

Haskell AST #157

Closed edsko closed 1 month ago

edsko commented 1 month ago

This is just a first sketch so far.

edsko commented 1 month ago

Ok, this is ready. To quote the commit message:

Introduce Haskell AST and multiple backends

This also

* Adds a struct offset field (closes #133)
* Starts the translation from C to Haskell for structs
* Provide infrastructure for PHOAS, including generics for
  deriving lawful (parseable) `Show`.

The two backends (TH and haskell-src-exts) only need to define basic translations; they do not need to deal with all the details of our precise Haskell AST, that is done only once in a backend-independent way.

As the commit message mentions, we can show these instances (using SOP generics to derive ShowOpen instances). I have replaced the TH dump golden tests with "internal Haskell representation" golden tests. I'm not sure if we want to revive the TH golden tests; I'm not sure it adds much (we should just test the TH generation, we don't need to check it for all examples). It's a bit trickier to add golden tests for this now because the generation of TH now really does depend on Q (for fresh name generation), so we can't easily ppr them anymore; I don't think this is a great loss.

There are no ToExpr instances for the AST types yet, but I don't think there's any reason why wouldn't introduce some generic machinery to define them if we need them.

edsko commented 1 month ago

After this PR, we are also quite close to being able to generate Haskell modules with Storable instances for simple structs (I think the only thing that might still be missing is proper imports).

phadej commented 1 month ago

the generation of TH now really does depend on Q

It doesn't need to, it should work in any Quote m => ... context (which can be provided by State Int.