When you enable the
MagicHash GHC extension, two things change:
- The identifier naming rules are modified slightly so that names ending with one or more hash (
#) characters are valid identifiers. Normally this wouldn’t be allowed.See identifiers and operators for the full details of what constitutes a valid name for something in Haskell.
- You can now write unlifted literals such as
3#. We discuss this further below.
Given how little this extension actually does, the choice to name it “magic hash” was perhaps a bit overdramatic.
When to enable MagicHash
Only enable the
MagicHash extension if you need it because you’re working with the primitives found in
You technically can apply a hash suffix to the names of your own types and variables if you really want to:
But this is ill-advised. It is strongly conventional for the hash suffix to be applied only to unlifted types (types that have kind
#).See primitives, levity, and boxing for more a lengthier discussion of this convention.
Hashes in operators are normal
MagicHash only affects the rules for what constitutes a valid identifier name. You do not need an extension to have operator symbols that contain a hash.See identifiers and operators for a discussion on the difference between identifiers and operator symbols. That is always permissible.
Here are some examples of ordinary, non-weird examples of hash operators:
(#)as a synonym for
($)with a different fixity.
(Control.Lens.Operators.#)also defines an operator named
Using those functions does not require a language extension because they are operator symbols, not identifiers.
Unlifted literals look like regular literals, but with one or two
# characters at the end.