Core questions
Simon Peyton-Jones
simonpj at microsoft.com
Mon Feb 2 03:43:32 EST 2009
Matthijs
| 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,
There's absolutely no reason why
CoreExpr
CoreBind
Type
should not be an instance of Show. It'd take you 10 mins to make it so, with the aid of 'standalone deriving' (described in the GHC user manual).
There *is* a reason why TyCon and Class are not:
a TyCon
enumerates its DataCons
whose type mentions the TyCon
In short, the data structures are, by design, cyclic. Printing one of these would take a long time.
But I bet you could get a long way with the three above, plus just printing the *name* of a TyCon or Class or Id. Something like:
instance Show TyCon where
show tc = showSDoc (ppr tc)
| 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.
Well a tuple looks like (tdc ty1 ... tyn arg1 .. argn), where tdc is the data constructor for the tuple. So what you need is:
isTupleDataConId :: Id -> Bool
It's easy to write
isTupleDataConId id
| Just data_con <- isDataConId_maybe id
= isTupleTyCon (dataConTyCon data_con)
I hope this is helpful
Simon
More information about the Glasgow-haskell-users
mailing list