-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
630 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{-# LANGUAGE FunctionalDependencies #-} | ||
{-# LANGUAGE TypeSynonymInstances #-} | ||
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-} | ||
-- | Per Conor McBride, the 'Newtype' typeclass represents the packing and | ||
-- unpacking of a newtype, and allows you to operatate under that newtype with | ||
-- functions such as 'ala'. | ||
module Distribution.Compat.Newtype where | ||
|
||
-- TODO: export only Newtype (..), ala | ||
|
||
import Data.Functor.Identity (Identity (..)) | ||
|
||
-- tmp | ||
import Control.Applicative (many) | ||
import Distribution.Text (Text) | ||
import qualified Distribution.Compat.Parsec as Parsec | ||
import Distribution.Parsec.Class | ||
|
||
class Newtype n o | n -> o where | ||
pack :: o -> n | ||
unpack :: n -> o | ||
|
||
instance Newtype (Identity a) a where | ||
pack = Identity | ||
unpack = runIdentity | ||
|
||
ala :: (Newtype n o, Newtype n' o') => (o -> n) -> ((o -> n) -> b -> n') -> (b -> o') | ||
ala pa hof = ala' pa hof id | ||
|
||
ala' :: (Newtype n o, Newtype n' o') => (o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> (b -> o') | ||
ala' _ hof f = unpack . hof (pack . f) | ||
|
||
------------------------------------------------------------------------------- | ||
-- Move to own module | ||
------------------------------------------------------------------------------- | ||
|
||
newtype CommaListWithSep a = CommaListWithSep { getCommaListWithSep :: [a] } | ||
|
||
instance Newtype (CommaListWithSep a) [a] where | ||
pack = CommaListWithSep | ||
unpack = getCommaListWithSep | ||
|
||
instance Parsec a => Parsec (CommaListWithSep a) where | ||
parsec = pack <$> parsecOptCommaList parsec | ||
|
||
instance Text a => Text (CommaListWithSep a) where | ||
|
||
-- | ||
newtype Token = Token { getToken :: String } | ||
|
||
instance Parsec Token where | ||
parsec = pack <$> parsecToken | ||
|
||
instance Newtype Token String where | ||
pack = Token | ||
unpack = getToken | ||
|
||
instance Text Token | ||
|
||
-- | ||
newtype FreeText = FreeText { getFreeText :: String } | ||
|
||
instance Parsec FreeText where | ||
parsec = pack <$> many Parsec.anyChar | ||
|
||
instance Newtype FreeText String where | ||
pack = FreeText | ||
unpack = getFreeText | ||
|
||
instance Text FreeText | ||
|
||
-- | ||
newtype FilePathNT = FilePathNT { getFilePathNT :: FilePath } | ||
|
||
instance Parsec FilePathNT where | ||
parsec = pack <$> parsecFilePath | ||
|
||
instance Newtype FilePathNT FilePath where | ||
pack = FilePathNT | ||
unpack = getFilePathNT | ||
|
||
instance Text FilePathNT | ||
|
||
------------------------------------------------------------------------------- | ||
-- Identity | ||
------------------------------------------------------------------------------- | ||
-- | ||
instance Parsec a => Parsec (Identity a) where | ||
parsec = pack <$> parsec | ||
|
||
instance Text (Identity a) where |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.