简体   繁体   中英

How to force the evaluation of a lambda term in haskell to the strong normal form

I was wondering if it is possible to turn off lazy evaluation in Haskell;
I would like to force the evaluation of a lambda term to the strong normal form.

For instance: I would like \\x -> (\\y -> y) x 1 to be reduced to \\x -> x 1

Thanks in advance.

You can't do it. The reason is that Haskell isn't supposed to perform lambda term reductions. Haskell evaluates programs (lambda terms) to values. One possibility how to do this would be to really perform lambda term reductions, but this would be quite inefficient. So Haskell compilers use many sophisticated techniques such as graph reduction instead. Therefore you can't observe differences such as (\\x -> x) y vs y during the evaluation process - there are no actual lambda terms to be observed.

Note that GHC has NFData type class which allows to evaluate terms to their normal forms using rnf - r -educe to n -ormal f -orm (but only products or coproducts, it still doesn't evaluate terms under lambdas, as @JakeMcArthur pointed out). However, this doesn't give you access to the normal form expressed as a lambda term. It only tells Haskell to perform this evaluation during the computation.

This question may be tackled by supercompilers. Right now, there is none that works really well with the current ghc and current packages.

There exists an older implementation of a supercompiler: http://community.haskell.org/~ndm/supero/

I you are interested in some newer considerations, this may be of interest: http://pure.ltu.se/portal/files/2231262/nwpt08-scp.pdf

However, I cannot either propose a proper solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM