kadoban's blog

By kadoban, history, 15 months ago, In English,

The way codeforces is using GHC's "Safe Haskell" extension is disabling the use of very common and useful modules and extensions, for no benefit that I can figure out.

Importing any of these:

  • Control.Monad.ST
  • Data.Array.ST
  • Data.Array.MArray
  • Data.Array.IO
  • Data.Vector
  • Data.Vector.Unboxed

(and probably more, but these are the ones I tried) gives an error of the form:

    Control.Monad.ST: Can't be safely imported!
    The module itself isn't safe.

Additionally, the GeneralizedNewtypeDeriving (GND) and TemplateHaskell (TH) language extensions are disabled. (compiling with them turned on doesn't error, but they're ignored, so if you try to use them you then get errors).

All of these disabled things are very useful. As-is the Haskell setup on codeforces has no performant mutable arrays available at all, which it's hard to overstate how wacky that is in competitive programming.

The GND extension is quite useful as well since it avoids having to tediously reimplement typeclasses when you wrap a type.

TH is Haskell's metaprogramming facility, like templates in C++ or macros in C. It doesn't get used as much as those features do in their respective languages, but it's still handy now and then.

I'm not really sure why all of this is disabled or exactly what switches are being used to GHC, though it's obviously related to Safe Haskell.

To make it clear, the sense in which "Safe Haskell" is safe is not useful for codeforces. In the programs we write, we're allowed to do unrestricted IO (at the language level at least, I assume there's a VM wrapper or restrictions on I/O at the OS level) in the standard main entry point to haskell programs. And since we're not writing a library, this is just an executable and used for nothing else, it doesn't make sense to judge the safety of the API we're exporting. There is no safety, you're running the IO action that we specify anyway and that's the only interface.

I would be interested what the motivation is for turning on safe haskell, especially in this way that disallows the use of very common modules and data structures. Even if safe haskell is somehow required (maybe TH would be a risk at compile time the way CF is set up?), there are still ways to whitelist certain modules/packages so we could use them, and this would have no risk that I can imagine.

While I'm at it, the version of GHC allowed is getting pretty old too. Something in the 8.x family, or even 7.10.x would be nice; there have been a couple of nice language changes since 7.8.3. Also 7.8.3 is kind of a uniquely annoying version because one of the common ways to get GHC is via the 'stack' tool, which doesn't support that version. The oldest it knows about is GHC 7.8.4. This makes it a bit uncertain trying to develop and test code locally for CF use.

 
 
 
 
  • Vote: I like it  
  • +46
  • Vote: I do not like it  

»
15 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by kadoban (previous revision, new revision, compare).

»
9 months ago, # |
  Vote: I like it +18 Vote: I do not like it

Modules in vector package are still are unavailable, but the modules

  • Control.Monad.ST
  • Data.Array.ST
  • Data.Array.MArray
  • Data.Array.IO

can be replaced with

  • Control.Monad.ST.Safe
  • Data.Array.ST.Safe
  • Data.Array.MArray.Safe
  • Data.Array.IO.Safe

Which are available in Codeforces's current Haskell environment.

  • »
    »
    9 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks, that's a nice tip. Thankfully I don't really use 'vector' anyway, so for me that'll fix essentially all of my problem (though it'd still be nice to get this issue fixed for real).

»
5 months ago, # |
  Vote: I like it +4 Vote: I do not like it

This still hasn't been fixed. Some problems are quasi-unsolvable without Data.Vector. Of course, MArrays could be used instead, but that defeats the whole point of using Haskell, i.e. the speed-verbosity trade-off.

MikeMirzayanov, please add Data.Vector.

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Bump. It is still impossible to use Data.Vector. Why is -XSafe needed?