matthijs at stdin.nl
Fri Jan 30 14:03:47 EST 2009
(I've previously sent this mail to haskell-cafe, but I guess this list is more
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20090130/1f50a8c6/attachment.bin
More information about the Glasgow-haskell-users