Copyright | (c) 2022 Dmitrii Kovanikov |
---|---|
License | MPL-2.0 |
Maintainer | Dmitrii Kovanikov <kovanikov@gmail.com> |
Stability | Stable |
Portability | Portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
GHC Plugin for rewriting list literals of non-empty list to NonEmpty
type. Enable the plugin in a module where you want to use it like on
the example below:
{-# OPTIONS_GHC -fplugin=GhcPluginNonEmpty #-} import Data.List.NonEmpty (NonEmpty
) portsToListen ::NonEmpty
Int
portsToListen = [8000, 8080, 8081]
You can also enable the plugin globally in your entire project from
the .cabal
file:
library ... ghc-options: -fplugin=GhcPluginNonEmpty
It guarantees that only non-empty lists will be automatically rewritten. Otherwise, if you use an empty list:
portsToListen ::NonEmpty
Int
portsToListen = []
You'll see ordinary compiler error:
src/Path/To/My/Module:34:17: error: • Couldn't match expected type: NonEmpty Int with actual type: [a0] • In the expression: [] In an equation for ‘portsToListen’: portsToListen = [] | 34 | portsToListen = [] | ^^
Since: 0.0.0.0
Synopsis
- plugin :: Plugin
- class GhcPlugnNonEmptyClass listOf where
- _xxx_ghc_plugin_nonEmpty_fromList :: (a -> [a] -> NonEmpty a) -> [a] -> listOf a
- cons :: a -> [a] -> NonEmpty a
Documentation
Main compiler plugin. Use the following GHC option to enable it:
{-# OPTIONS_GHC -fplugin=GhcPluginNonEmpty #-}
Implemented in two steps:
- Rewrites
[3, 1, 2]
to_xxx_ghc_plugin_nonEmpty_fromList
cons
[3, 1, 2] - Find applications of
_xxx_ghc_plugin_nonEmpty_fromList
and rewrites them tocons
3 [1, 2]
Since: 0.0.0.0
Internal typeclass for the plugin work
class GhcPlugnNonEmptyClass listOf where Source #
⚠️ WARNING! Don't use this typeclass! ⚠️
This is an internal typeclass for the plugin to work correctly but it must be imported from this module. Don't use methods of this typeclass in your code as it may result in incorrect compilation of your code.
Since: 0.0.0.0
_xxx_ghc_plugin_nonEmpty_fromList Source #
:: (a -> [a] -> NonEmpty a) | Typechecked non-empty constructor |
-> [a] | List literal we're going to rewrite |
-> listOf a | Resulting list |
Since: 0.0.0.0
Instances
GhcPlugnNonEmptyClass NonEmpty Source # | Since: 0.0.0.0 |
Defined in GhcPluginNonEmpty _xxx_ghc_plugin_nonEmpty_fromList :: (a -> [a] -> NonEmpty a) -> [a] -> NonEmpty a Source # | |
GhcPlugnNonEmptyClass [] Source # | Since: 0.0.0.0 |
Defined in GhcPluginNonEmpty _xxx_ghc_plugin_nonEmpty_fromList :: (a -> [a] -> NonEmpty a) -> [a] -> [a] Source # |