Project:Sandbox

License : This code and text are dedicated to the public domain. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Source : http://dev.stephendiehl.com/hask/

The Benign
It's not obvious which extensions are the most common but it's fairly safe to say that these extensions are benign and are safely used extensively:


 * OverloadedStrings
 * FlexibleContexts
 * FlexibleInstances
 * GeneralizedNewtypeDeriving
 * TypeSynonymInstances
 * MultiParamTypeClasses
 * FunctionalDependencies
 * NoMonomorphismRestriction
 * GADTs
 * BangPatterns
 * DeriveGeneric
 * ScopedTypeVariables

The Dangerous
GHC's typechecker sometimes just casually tells us to enable language extensions when it can't solve certain problems. These include:


 * DatatypeContexts
 * OverlappingInstances
 * IncoherentInstances
 * ImpredicativeTypes
 * AllowAmbigiousTypes

These almost always indicate a design flaw and shouldn't be turned on to remedy the error at hand, as much as GHC might suggest otherwise!

NoMonomorphismRestriction
The NoMonomorphismRestriction allows us to disable the monomorphism restriction typing rule GHC uses by default. See monomorphism restriction.

For example, if we load the following module into GHCi

and then we attempt to call the function  with a Double, we get a type error:

The problem is that GHC has inferred an overly specific type:

We can prevent GHC from specializing the type with this extension, i.e.

Now everything will work as expected:

ExtendedDefaultRules
In the absence of explicit type signatures, Haskell normally resolves ambiguous literals using several defaulting rules. When an ambiguous literal is typechecked, if at least one of its typeclass constraints is numeric and all of its classes are standard library classes, the module's default list is consulted, and the first type from the list that will satisfy the context of the type variable is instantiated. So for instance, given the following default rules

The following set of heuristics is used to determine what to instantiate the ambiguous type variable to.


 * 1) The type variable   appears in no other constraints
 * 2) All the classes   are standard.
 * 3) At least one of the classes   is numeric.

The default  is

This is normally fine, but sometimes we'd like more granular control over defaulting. The loosens the restriction that we're constrained with working on Numerical typeclasses and the constraint that we can only work with standard library classes. If we'd like to have our string literals (using ) automatically default to the more efficient   implementation instead of   we can twiddle the flag and GHC will perform the right substitution without the need for an explicit annotation on every string literal.

For code typed at the GHCi prompt, the  flag is always on, and cannot be switched off.

See: Monomorphism Restriction

''Safe
As everyone eventually finds out there are several functions within the implementation of GHC ( not the Haskell language ) that can be used to subvert the type-system, they are marked with the prefix. These functions exist only for when one can manually prove the soundness of an expression but can't express this property in the type-system or externalities to Haskell.

Using these functions to subvert the Haskell typesystem will cause all measure of undefined behavior with unimaginable pain and suffering, and are strongly discouraged. When initially starting out with Haskell there are no legitimate reason to use these functions at all, period.

The Safe Haskell language extensions allow us to restrict the use of unsafe language features using  which restricts the import of modules which are themselves marked as Safe. It also forbids the use of certain language extensions which can be used to produce unsafe code. The primary use case of these extensions is security auditing.

See: Safe Haskell

PartialTypeSignatures
Normally a function is either given a full explicit type signature or none at all. The partial type signature extension allows something in between.

Partial types may be used to avoid writing uninteresting pieces of the signature, which can be convenient in development:

If the  GHC option is set, partial types will still trigger warnings.

See: Partial Type Signatures