Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

haskell

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, e.g., 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?

like image 398
Jim Witschey Avatar asked Jan 30 '15 04:01

Jim Witschey


1 Answers

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",...]
like image 165
MasterMastic Avatar answered Nov 18 '22 00:11

MasterMastic