Actually, in 6.8 we can build isWHNF on top of the GHC-API.

First, you need to import the ghc package:

ghci -package ghc
GHCi, version 6.7: http://www.haskell.org/ghc/  :? for help

Then, you can define the isWHNF function as follows:

Prelude> :m +RtClosureInspect
Prelude RtClosureInspect> let isWHNF = fmap (isConstr . tipe) . getClosureData

Prelude RtClosureInspect> :t isWHNF
isWHNF :: a -> IO Bool

What the code above does is to inspect the info table associated to the value given, and check if the closure is a Constructor closure, i.e. in WHNF.

We can put it to test now:

Prelude RtClosureInspect> let a = [1..10]
Prelude RtClosureInspect> isWHNF a
False
Prelude RtClosureInspect> seq a ()
()
Prelude RtClosureInspect> isWHNF a
True


As a bonus because this code is included in GHC itself it should stay in sync with any changes in the GHC internal representations.

Cheers
pepe

On 27/09/2007, at 14:51, Bernie Pope wrote:

Hi Tristan,

I've implemented it for earlier versions of GHC, by calling some C code which then peeps at the internal representation of a value.

From memory, I needed to pass a stable pointer to the value to the C code, so that it can be polymorphic, without having to make it a primitive in GHC.

Have a look at the "reify" code on this page: http:// www.cs.mu.oz.au/~bjpop/code.html - its more than what you want, but you can trim it down easily.
Let me know if you get stuck.

The internal representation in GHC tends to change between releases, so it might need a bit of polishing up.

Cheers,
Bernie.

On 27/09/2007, at 10:07 PM, Tristan Allwood wrote:

Hi,

Does anyone know if there is a function that tells you if a haskell
value has been forced or not?

e.g.
isWHNF :: a -> IO Bool

let x = (map succ [0..]) in do
  putStrLn . show (isWHNF x)                -- False
  putStrLn . show . head $ x
  putStrLn . show (isWHNF x)                -- True
  putStrLn . show (isWHNF (Just undefined)) -- True


If not, would it be hard/easy/possible to implement on-top-of or using
GHC?  I'm happy (if it's possible) to have a stab at implementing it
myself, so any pointers to right directions would be helpful.

I'm thinking it could be useful to allow creation of sparse-check [1]
like libraries without needing a separate logic encoding, or things
along those lines / in that area.

Cheers,

Tris

[1] http://www-users.cs.york.ac.uk/~mfn/sparsecheck/index.html#lim

--
Tristan Allwood
PhD Student
Department of Computing
Imperial College London
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to