简体   繁体   中英

What is GHC.Exts, and how were its contents chosen?

The GHC.Exts module in the Haskell standard library claims that it

is the Approved Way to get at GHC-specific extensions.

If that's true, it explains the inclusion of implementation-specific constants like the constant representing the maximum size of a tuple and (presumably) non-portable debugging functions .

However, it doesn't explain why sortWith is in this module. Its implementation looks like normal, portable Haskell to me. I'd expect to see it in, eg, Data.List and Data.Sequence.

It seems I misunderstand what GHC.Exts is, I don't understand the underlying logic behind its collection of exports, or there's some historical reason it exports a hodgepodge of things.

So, what is GHC.Exts for ? and why does it export such a weird mixture of stuff ?

Those functions and the Down newtype are for a syntactic extension: Generalised (SQL-Like) List Comprehensions , enabled with -XTransformListComp .

This extension introduces keywords that correspond to those functions:

There are three new keywords: group , by , and using . (The function sortWith is not a keyword; it is an ordinary function that is exported by GHC.Exts .)

Those functions are working on lists, but they really belong to an extension (and GHC.Exts is the extensions' home).

And the reason GHC.Exts exports a varied mixture of stuff is because there are a varied mixture of extensions.

If you'd like to see more, see the user guide page -- and for even more, the paper for it by Phil Wadler and Simon Peyton Jones. It's actually quite exciting imo, here's an example from the user guide page:

I truncated it, but you can say stuff like:

[ .. | (name, dept, salary) <- employees
, then group by dept
, then sortWith by (sum salary)
, then take 5 ]

And as another example:

output = [x| y <- [1..5], x <- "hello"
, then group using inits]

Yields:

["","h","he","hel","hell","hello","helloh","hellohe","hellohel","hellohell","hellohello","hellohelloh",...]

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