| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Overloaded
Contents
Description
Overloaded* language extensions as a source plugin.
Synopsis
- plugin :: Plugin
- class FromSymbol (s :: Symbol) a where
- fromSymbol :: a
- class FromNumeral (n :: Nat) a where
- fromNumeral :: a
- defaultFromNumeral :: forall n a. (KnownNat n, Integral a) => a
- class FromNatural a where
- fromNatural :: Natural -> a
- class FromChar a where
- class Nil a where
- nil :: a
- class Cons x ys zs | zs -> x ys where
- cons :: x -> ys -> zs
- class ToBool b where
- ifte :: ToBool b => b -> a -> a -> a
Plugin
Overloaded plugin.
To enable plugin put the following at top of the module:
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
At least one option is required, multiple can given
either using multiple -fplugin-opt options, or by separating options
with colon:
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols:Numerals #-}
Options also take optional desugaring names, for example
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Labels=Data.Generics.ProductFields.field #-}
to desugar OverloadedLabels directly into field from generics-lens (no need to import orphan instance!)
Supported options
Symbolsdesugars literal strings tofromSymbol@symStringsworks like built-inOverloadedStrings(but you can use different method thanfromString)Numeralsdesugars literal numbers tofromNumeral@natNaturalsdesugars literal numbers to(i.e. likefromNaturalnatfromString)Charsdesugars literal characters to. Note: there isn't type-level alternative: we cannot promotefromCharscChars.Listsis not like built-inOverloadedLists, but desugars explicit lists toconsandnilIfdesugarsif-expressions toifteb t eLabelsworks like built-inOverloadedLabels(you should enableOverloadedLabelsso parser recognises the syntax)
Known limitations
- Doesn't desugar inside patterns
Overloaded:Symbols
class FromSymbol (s :: Symbol) a where Source #
Another way to desugar overloaded string literals using this class.
A string literal "example" is desugared to
fromSymbol @"example"
Enabled with:
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
Methods
fromSymbol :: a Source #
Instances
| (KnownSymbol s, SeqList (ToList s)) => FromSymbol s ByteString Source # | |
Defined in Overloaded.Symbols Methods | |
| (KnownSymbol s, SeqList (ToList s)) => FromSymbol s ByteString Source # | |
Defined in Overloaded.Symbols Methods | |
| KnownSymbol s => FromSymbol s Text Source # | |
Defined in Overloaded.Symbols Methods fromSymbol :: Text Source # | |
| KnownSymbol s => FromSymbol s Text Source # | |
Defined in Overloaded.Symbols Methods fromSymbol :: Text Source # | |
| (KnownSymbol s, a ~ Char) => FromSymbol s [a] Source # | |
Defined in Overloaded.Symbols Methods fromSymbol :: [a] Source # | |
Overloaded:Strings
See Data.String for fromString.
Overloaded:Numerals
class FromNumeral (n :: Nat) a where Source #
Another way to desugar numerals.
A numeric literal 123 is desugared to
fromNumeral @123
Enabled with:
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Numerals #-}
One can do type-level computations with this.
Methods
fromNumeral :: a Source #
Instances
defaultFromNumeral :: forall n a. (KnownNat n, Integral a) => a Source #
Default implementation of fromNumeral.
Usage example:
instance (KnownNatn, ...) =>FromNumeraln MyType wherefromNumeral=defaultFromNumeral@n
Overloaded:Naturals
class FromNatural a where Source #
Methods
fromNatural :: Natural -> a Source #
Instances
| FromNatural Integer Source # | |
Defined in Overloaded.Naturals Methods fromNatural :: Natural -> Integer Source # | |
| FromNatural Natural Source # | |
Defined in Overloaded.Naturals Methods fromNatural :: Natural -> Natural Source # | |
Overloaded:Chars
Overloaded:Lists
Class for nil, []
See test-suite for ways to define instances for Map.
There are at-least two-ways.
class Cons x ys zs | zs -> x ys where Source #
Class for Cons :.
Overloaded:If
Class for Bool-like datastrucutres
An if--expression if b then t else e is desugared to
ifte (toBool b) t e
Enabled with:
{-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:If #-}
Overloaded:Labels
See GHC.OverloadedLabels for fromLabel.
generic-lens example!