for my Master's thesis, I'm looking into functional hardware
descriptions, and in particular to translate haskell code into VHDL that
can be programmed into an FPGA.
For this, I'm using the GHC API to load the haskell source and give me
(simplified) core representation. I then walk over the core tree and
translate it to VHDL.
On the whole the GHC API has been very useful to me. It took me some
time to get used to all the (deeply) nested (algebraic) types (I didn't
have any previous haskell experience when I started a month or so ago),
but things are working out by now.
However, there are two issues bothering me still. The first is that the
Core types (in particular CoreExpr) are not instances of Show. They are
instances of Outputable, which allows them to be pretty printed.
However, this pretty printing is good to view the structure of the
expression that the CoreExpr represents, but doesn't show the structure
of the CoreExpr itself. For example, tuple construction is printed
simply as (a, b), while the actual core expression is a nested
application of two types, and a and b to the GHC.Tuple.(,) function
(or datacon?). Also, the exact constructors used are not quite clear,
which made it harder to work with the Core language for me. Instead of
looking at the structure of the CoreExpr and write the appropriate
patterns, I had to resort to a lot of trial and error.
I tried deriving show for CoreExpr myself, but that required me to
derive Show for a dozen other types and resulted in type errors beyond
Is there any compelling reason that CoreExprs are not instances of Show?
My second question concerns typle construction. Since tuple types are
not primitive types, but dependent types defined in various places
(GHC.Tuple and Base IIRC), code working with tuples is not fundamentally
different from code working with other (user defined) dependent types
and thus not trivial to recognize. I've found that there are some
predicate functions that can tell me if a type is a tuple type, but I've
had no such luck for the actual tuple construction.
In particular, when an expression creates a tuple of two Ints (a, b),
this is represented in Core as the application of the element types and
the element values to the (,) function (or data constructor? Not sure
about this, in Core it's just a Var and the isDataConName [IIRC]
predicate crashes when run on the Var).
For now, I've manually matched the Var's name to GHC.Tuple.(,) and
removed all type arguments to get at the actual values in the tuple.
This is of course completely non-portable, to tuples with more than two
elements, or unboxed tuples, etc.
Is there some predicate function that can do this for me in a more
Haskell-Cafe mailing list