#ifdef considered harmful (was: DData)
Alastair Reid
alastair at reid-consulting-uk.ltd.uk
Fri Apr 16 13:26:23 EDT 2004
> Haskell libraries are
> chock-a-block with instances of "#ifdef __GHC__" which define things
> in terms of Int# instead of Int.
I think the correct fix in this case is for Hugs and NHC to implement Int#.
The representation of Int# as an unboxed object isn't so critical but its
strictness properties are exactly what you need in a bunch of circumstances:
- counters threaded through a monad
- line counters in parsers/preprocessors
- column numbers/widths in pretty-printing libraries
What I'd like to see is a general facility for defining strict types. That
is, types whose values will never be thunks so let binding causes evaluation
(as in ML) and use as a subexpression (which is not under a lambda) causes
evaluation (as in ML). The GHC syntax seems too restrictive for this. Maybe
something like this:
strict_data Int# = Int# Int -- declare a strict Int type
The typechecking issues would be the same as for GHC's unboxed types: there
are two kinds of typevar * and # and never the twain shall meet. (I think
GHC plays some neat tricks to ease the pain of this restriction?)
A semantically correct (but maybe a bit slow) implementation would be easy
enough:
1) let-bind all strict non-atomic subexpressions.
2) float strict variable bindings out to nearest enclosing lambda
(float out any lazy variable bindings if required)
3) Use 'seq' to force all the strict var thunks.
For example:
insert :: a -> (Int#,[a]) -> (Int#,[a])
insert x (cnt,xs) = (cnt+1,x:xs)
===>
insert x (cnt,xs) = let cnt'=cnt+1 in seq cnt' $
(cnt',x:xs)
--
Alastair Reid
More information about the Libraries
mailing list