Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: -fglasgow-exts should one avoid code that requires this?

I am a beginner with Haskell and I've started seeing errors similar to:

Illegal parallel list comprehension: use -fglasgow-exts

I am working within ghci and with ghc but only due to the fact that it was the first one I found on a search.

I'm curious if this is the type of situation one would want to avoid moving forward. Any searches I've turned up mention that these extensions expose underlying facilities that may (or may not) be useful.

A specific example is

fibs = 0 : 1 : [ a + b | a <- fibs | b <- tail fibs ]

I assume the fact that both a and b are reading from the list at the same time causes problems here...? So, if the glasgow extensions are the only means to support this construct is it more common to generate the list another way or just assume that the extensions will be available?

Thanks in advance for any input.

[EDIT] Sorry if this was not entirely clear, but my question is if including glasgow (or any other) extensions is considered bad practice. The example above was just to illustrate the type of error that prompted this question.

like image 277
ezpz Avatar asked Aug 25 '09 19:08

ezpz


4 Answers

Instead of requesting all GHC extensions, specify which ones are used using the LANGUAGE pragma mechanism:

{-# LANGUAGE ParallelListComp #-}
xy = [ x+y | x <- [1, 2, 3, 4] | y <- [5, 6, 7, 8] ]

I assume the fact that both a and b are reading from the list at the same time causes problems here...? So, if the glasgow extensions are the only means to support this construct is it more common to generate the list another way or just assume that the extensions will be available?

Parallel iteration over the same list is allowed. The problem is that parallel comprehensions are not defined in the Haskell 98 standard. They can be easily simulated using zip:

xy = [x+y | (x,y) <- zip [1, 2, 3, 4] [5, 6, 7, 8]]

Extensions themselves are not bad -- much of the standard library uses extensions, of one sort or another. Many are being considered for inclusion in Haskell', the next iteration of the Haskell standard. Some extensions, such as GADTs are commonly used throughout user-written libraries. Others, such as templates or incoherent instances, are probably not a good idea to use unless you really know what you're doing.

Any extension listed in the HaskellExtensions wiki page with support from two or more compilers is probably safe to use.

like image 130
John Millikin Avatar answered Nov 07 '22 22:11

John Millikin


GHC is definitely pervasive - I think its the most-used Haskell compiler, so it probably won't cause too much trouble. You should always try to write standards-compliant code, though - maybe not for personal projects, but for OSS or work ones, definitely.

Anything can happen, right? So can a sudden change in compiler half-way through your project.

With OSS, different people use different compilers - HUGS is also quite common, for example.

like image 44
Lucas Jones Avatar answered Nov 07 '22 23:11

Lucas Jones


Using extensions is fine. Flag them specifically with -XFoo or LANGUAGE FOO. Which extensions you choose to use are up to you, you might want to stick to those listed for inclusion in Haskell Prime.

like image 22
Don Stewart Avatar answered Nov 07 '22 23:11

Don Stewart


What you want is:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

This is because list comprehensions don't really work with self-referential lists. Also, although GHC is more popular, HUGS generally produces clearer error messages.

like image 45
Lucky Avatar answered Nov 07 '22 23:11

Lucky