ghc-plugin-non-empty-0.0.0.0: GHC Plugin for non-empty lists
Copyright(c) 2022 Dmitrii Kovanikov
LicenseMPL-2.0
MaintainerDmitrii Kovanikov <kovanikov@gmail.com>
StabilityStable
PortabilityPortable
Safe HaskellSafe-Inferred
LanguageHaskell2010

GhcPluginNonEmpty

Description

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

Documentation

plugin :: Plugin Source #

Main compiler plugin. Use the following GHC option to enable it:

{-# OPTIONS_GHC -fplugin=GhcPluginNonEmpty #-}

Implemented in two steps:

  1. Rewrites [3, 1, 2] to _xxx_ghc_plugin_nonEmpty_fromList cons [3, 1, 2]
  2. Find applications of _xxx_ghc_plugin_nonEmpty_fromList and rewrites them to cons 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

Methods

_xxx_ghc_plugin_nonEmpty_fromList Source #

Arguments

:: (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

Instances details
GhcPlugnNonEmptyClass NonEmpty Source #

Since: 0.0.0.0

Instance details

Defined in GhcPluginNonEmpty

Methods

_xxx_ghc_plugin_nonEmpty_fromList :: (a -> [a] -> NonEmpty a) -> [a] -> NonEmpty a Source #

GhcPlugnNonEmptyClass [] Source #

Since: 0.0.0.0

Instance details

Defined in GhcPluginNonEmpty

Methods

_xxx_ghc_plugin_nonEmpty_fromList :: (a -> [a] -> NonEmpty a) -> [a] -> [a] Source #

cons :: a -> [a] -> NonEmpty a Source #

Constructor for NonEmpty. Named alias to :|.

Since: 0.0.0.0