help needed for adding isWHNF primop to 5.00.2

Simon Marlow
Tue, 31 Jul 2001 12:38:03 +0100

> From reading the documentation that comes with GHC and=20
> glancing over the
> source code, I came to these set of assumptions, please=20
> correct me if I am
> wrong:
> - values of unpointed types are never in WHNF

That's really up to you, but the normal definition of WHNF applies only
to types containing _|_, i.e. pointed types.  Unpointed types are never
represented by thunks, so I would be inclined to include them in WHNF.

> - values of pointed types may be in WHNF, in particular:
>      - data constructors are in WHNF
>      - partial applications are in WHNF
>      - functions are in WHNF
>      - thunks are not in WHNF
>        (this list is probably not exhaustive as you say, but=20
> I am happy
>         with this set of definitions for the moment)

You also have to deal with indirections, although you can assume (at the
moment) that an indirection will eventually lead to an object in WHNF.

> > I can also imagine it
> > could interact badly with the complexities of GHC's=20
> > simplifer.
> This is something I do not know. In the end I may define it=20
> with this type:
>    isWHNF :: a -> IO Bool=20

That's the way I'd go.  You have to declare the primitive like this,

	isWHNF# :: a -> (# State# RealWorld, Int# #)

because the compiler doesn't know about the IO type and you can't return
a Bool directly.

There's no problem with the simplifier as long as you declare the
primitive to have the correct properties, i.e. that the polymorphic
argument is lazy.

> I want it for the purposes of meta-programming, and also for=20
> debugging since
> that is what I am working on. At the end of a computation=20
> (that results in
> a value of pointed type, say) I want to know to what extent=20
> expressions were
> evaluated:
>       foo xs =3D take 3 xs
>       ... foo [4..] ...
>    debugger> foo [4,5,6,_ =3D [4,5,6]

It sounds like you also need a way to take apart arbitrary objects and
look at their components.  I know that Andy Gill was also interested in
having similar facilities, perhaps he can help out.