coerce-util (empty) → 0.1.0.0
raw patch · 5 files changed
+154/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- ChangeLog.md +5/−0
- Data/Coerce/Util.hs +108/−0
- LICENSE +20/−0
- Setup.hs +2/−0
- coerce-util.cabal +19/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for coerce-util++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ Data/Coerce/Util.hs view
@@ -0,0 +1,108 @@+module Data.Coerce.Util+ (+ -- * Function coercion utils+ (#.)+ , (.#)++ -- * Shorten coercion type signatures+ , Binary+ , CoerceBinary+ , WrapBinary+ , WrappedBinary+ + -- * Newtype utils + , showsNewtype+ , readsNewtype+ ) where++import Data.Coerce++import Text.Read+import Text.Read.Lex++-- | A definition for 'Data.Functor.Classes.liftShowsPrec',+-- suitable for newtypes.+--+-- Given a newtype declared as:+--+-- @newtype T a = T { unT :: a }@+--+-- The 'Data.Functor.Classes.Show1' definition can be given as:+--+-- @+-- instance Show1 T where+-- liftShowsPrec = showsNewtype "T" "unT"+-- @+showsNewtype+ :: Coercible b a+ => String+ -> String+ -> (Int -> a -> ShowS)+ -> ([a] -> ShowS)+ -> Int+ -> b+ -> ShowS+showsNewtype cons acc = s+ where+ s sp _ n x =+ showParen (n > 10)+ $ showString cons+ . showString " {"+ . showString acc+ . showString " = "+ . sp 0 (coerce x)+ . showChar '}'+{-# INLINE showsNewtype #-}++-- | A definition for 'Data.Functor.Classes.liftReadsPrec',+-- suitable for newtypes.+--+-- Given a newtype declared as:+--+-- @+-- newtype T a = T { unT :: a }+-- @+--+-- The 'Data.Functor.Classes.Read1' definition can be given as:+--+-- @+-- instance Read1 T where+-- liftReadsPrec = readsNewtype "T" "unT"+-- @+readsNewtype+ :: Coercible a b+ => String+ -> String+ -> (Int -> ReadS a)+ -> ReadS [a]+ -> Int+ -> ReadS b+readsNewtype cons acc = r+ where+ r rp _ = readPrec_to_S $ parens $ prec 10 $ do+ lift $ expect (Ident cons)+ Punc "{" <- lexP+ lift $ expect (Ident acc)+ Punc "=" <- lexP+ x <- reset (readS_to_Prec rp)+ Punc "}" <- lexP+ pure (coerce x)+{-# INLINE readsNewtype #-}++type Binary a = a -> a -> a+type CoerceBinary a b = Binary a -> Binary b+type WrapBinary f a = Binary a -> WrappedBinary f a+type WrappedBinary f a = Binary (f a)++-- | Coercive left-composition.+infixr 9 #.+(#.) :: Coercible b c => (b -> c) -> (a -> b) -> a -> c+(#.) _ = coerce+{-# INLINE (#.) #-}++-- | Coercive right-composition.+infixr 9 .#+(.#) :: Coercible a b => (b -> c) -> (a -> b) -> a -> c+(.#) f _ = coerce f+{-# INLINE (.#) #-}+
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2018 chessai++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ coerce-util.cabal view
@@ -0,0 +1,19 @@+name: coerce-util+version: 0.1.0.0+synopsis: utils for Data.Coerce+description: This library provides some convenience utilities for working with 'Data.Coerce'.+homepage: https://github.com/chessai/coerce-util.git+license: MIT+license-file: LICENSE+author: chessai+maintainer: chessai1996@gmail.com+copyright: 2018 (C) chessai+category: Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Data.Coerce.Util + build-depends: base >=4.9 && <5+ default-language: Haskell2010