GHC Extensions

>>> Turbo Haskell



Overview

As everyone knows - Haskell means GHC, and GHC is only worth using if you turn on "ghc --supported-extensions".


lyndon@endpin ~ ghc --supported-extensions | sed 's/^/-X/' | xargs ghci
	ghc: panic! (the 'impossible' happened)

But what are the GHC extensions and why should I (ab)use them?


This talk will cover a few of the extensions defined by the

Glorious Glasgow Compilation System.
lyndon@endpin ~ ghc --supported-extensions

...

Haskell98
Haskell2010
Unsafe
Trustworthy
Safe
CPP
NoCPP
PostfixOperators
NoPostfixOperators
TupleSections
NoTupleSections
PatternGuards
NoPatternGuards
UnicodeSyntax
NoUnicodeSyntax
MagicHash
NoMagicHash
PolymorphicComponents
NoPolymorphicComponents
ExistentialQuantification
NoExistentialQuantification
KindSignatures
NoKindSignatures
EmptyDataDecls
NoEmptyDataDecls
ParallelListComp
NoParallelListComp
TransformListComp
NoTransformListComp
MonadComprehensions
NoMonadComprehensions
ForeignFunctionInterface
NoForeignFunctionInterface
UnliftedFFITypes
NoUnliftedFFITypes
InterruptibleFFI
NoInterruptibleFFI
CApiFFI
NoCApiFFI
GHCForeignImportPrim
NoGHCForeignImportPrim
LiberalTypeSynonyms
NoLiberalTypeSynonyms
Rank2Types
NoRank2Types
RankNTypes
NoRankNTypes
ImpredicativeTypes
NoImpredicativeTypes
TypeOperators
NoTypeOperators
ExplicitNamespaces
NoExplicitNamespaces
RecursiveDo
NoRecursiveDo
DoRec
NoDoRec
Arrows
NoArrows
ParallelArrays
NoParallelArrays
TemplateHaskell
NoTemplateHaskell
QuasiQuotes
NoQuasiQuotes
ImplicitPrelude
NoImplicitPrelude
RecordWildCards
NoRecordWildCards
NamedFieldPuns
NoNamedFieldPuns
RecordPuns
NoRecordPuns
DisambiguateRecordFields
NoDisambiguateRecordFields
OverloadedStrings
NoOverloadedStrings
GADTs
NoGADTs
GADTSyntax
NoGADTSyntax
ViewPatterns
NoViewPatterns
TypeFamilies
NoTypeFamilies
BangPatterns
NoBangPatterns
MonomorphismRestriction
NoMonomorphismRestriction
NPlusKPatterns
NoNPlusKPatterns
DoAndIfThenElse
NoDoAndIfThenElse
RebindableSyntax
NoRebindableSyntax
ConstraintKinds
NoConstraintKinds
PolyKinds
NoPolyKinds
DataKinds
NoDataKinds
InstanceSigs
NoInstanceSigs
MonoPatBinds
NoMonoPatBinds
ExplicitForAll
NoExplicitForAll
AlternativeLayoutRule
NoAlternativeLayoutRule
AlternativeLayoutRuleTransitional
NoAlternativeLayoutRuleTransitional
DatatypeContexts
NoDatatypeContexts
NondecreasingIndentation
NoNondecreasingIndentation
RelaxedLayout
NoRelaxedLayout
TraditionalRecordSyntax
NoTraditionalRecordSyntax
LambdaCase
NoLambdaCase
MultiWayIf
NoMultiWayIf
MonoLocalBinds
NoMonoLocalBinds
RelaxedPolyRec
NoRelaxedPolyRec
ExtendedDefaultRules
NoExtendedDefaultRules
ImplicitParams
NoImplicitParams
ScopedTypeVariables
NoScopedTypeVariables
PatternSignatures
NoPatternSignatures
UnboxedTuples
NoUnboxedTuples
StandaloneDeriving
NoStandaloneDeriving
DeriveDataTypeable
NoDeriveDataTypeable
DeriveFunctor
NoDeriveFunctor
DeriveTraversable
NoDeriveTraversable
DeriveFoldable
NoDeriveFoldable
DeriveGeneric
NoDeriveGeneric
DefaultSignatures
NoDefaultSignatures
TypeSynonymInstances
NoTypeSynonymInstances
FlexibleContexts
NoFlexibleContexts
FlexibleInstances
NoFlexibleInstances
ConstrainedClassMethods
NoConstrainedClassMethods
MultiParamTypeClasses
NoMultiParamTypeClasses
FunctionalDependencies
NoFunctionalDependencies
GeneralizedNewtypeDeriving
NoGeneralizedNewtypeDeriving
OverlappingInstances
NoOverlappingInstances
UndecidableInstances
NoUndecidableInstances
IncoherentInstances
NoIncoherentInstances
PackageImports
NoPackageImports

Q: What is Haskell?


A:

I already know that.
It was a prerequisite for attending this talk.

But did you know...

That there are official versions of the Haskell language?

http://en.wikipedia.org/wiki/Haskell_(programming_language)

  • Haskell 1.0 - 1.4
  • Haskell 98
  • Haskell Prime
  • Haskell 2010

Or that a large number of people use features that fall outside of these specifications?


GHC has become the de-facto standard for Haskell use in the Real World™


Luckily for us, GHC has taken a structured approach to adding further functionality to its implementation of Haskell.


This comes in the form of extensions.

How do I even??

How do I enable <Extension>?

With GHC Parameters...

foo.hs

 ghc --make -XExtension foo.hs 

In Your Source Files...

foo.hs

 {-# LANGUAGE Extension #-} 
 {-# OPTIONS_GHC -XExtension #-} 

With Cabal...

foo.cabal

 Extensions:  Extension 
 Ghc-Options: -fextension 

Extensions Covered

  • ImplicitParams
  • ViewPatterns
  • OverloadedStrings
  • MonadComprehensions
  • EmptyDataDecls
  • MultiParameterTypeClasses
  • GADTs
  • UnicodeSyntax

Implicit Parameters

http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#implicit-parameters

Turn it on → ImplicitParams

View Patterns

http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns

Turn it on → ViewPatterns

Overloaded Strings

http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/type-class-extensions.html#overloaded-strings

Turn it on → OverloadedStrings

Monad Comprehensions

http://hackage.haskell.org/trac/ghc/wiki/MonadComprehensions

Turn it on → MonadComprehensions

Generalises Various Concepts

Such as...


Parallel List Comprehensions

http://www.haskell.org/ghc/docs/5.00/set/parallel-list-comprehensions.html

Turn it on → ParallelListComp

Empty Data Declarations

http://hackage.haskell.org/trac/haskell-prime/wiki/EmptyDataDecls

Turn it on → EmptyDataDecls


Not terribly exciting, but turned on automatically when you use...

Multi Parameter Type Classes

http://www.haskell.org/ghc/docs/7.0.1/html/users_guide/type-class-extensions.html#id3172593

Turn it on → MultiParamTypeClasses

Seems Simple Enough...


But in actual fact, leads to the ability to write advanced type-level functions...

http://byorgey.wordpress.com/2010/06/29/ty ... nal-dependencies/


And more...

http://www.haskell.org/haskellwiki/GADTs_for_dummies



Speaking of GADTs...

Generalised Algebraic Data Types

http://www.haskell.org/ghc/docs/7.0.3/html/users_guide/data-type-extensions.html#gadt

Turn it on → GADTs

Examples taken from http://en.wikibooks.org/wiki/Haskell/GADT

Let's Say...

You want to represent a small pure expression language in haskell:

You can create Integers, Add them, and Multiply them.

And Now for Something Slightly Different

We want to be able to compare values, and derive a boolean result:


Oops, that doesn't work so well...

With Enough Data-Types...

We can make it work...

But it sucks!

Maybe if we...

introduce a type parameter to keep track of our expression type...

Finally, with GADTs

Things are working, concise, and expressive


This is just the tip of the iceberg when it comes to GADTs.

Unicode Syntax ‽‽‽‽

http://www.haskell.org/ghc/docs/7.4.1/html/users_guide/syntax-extns.html#unicode-syntax

Turn it on → UnicodeSyntax

Can this be abused?

Heck Yes!!

Also...

With Haskell's weird distinction between operators and functions, this can get ugly...

Like So:



:(

Obviously this is just an overview...


The best way to learn about GHC's extensions is to pick one and see what you can do with it. Experimentation is key!


Go and Play!

Questions?