packages feed

conversion (empty) → 1.1.0.0

raw patch · 5 files changed

+623/−0 lines, 5 filesdep +base-preludesetup-changed

Dependencies added: base-prelude

Files

+ CHANGELOG.md view
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2015, Nikita Volkov++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
+ conversion.cabal view
@@ -0,0 +1,51 @@+name:+  conversion+version:+  1.1.0.0+synopsis:+  Universal converter between values of different types+category:+  Control, Data, Conversion+homepage:+  https://github.com/nikita-volkov/conversion +bug-reports:+  https://github.com/nikita-volkov/conversion/issues +author:+  Nikita Volkov <nikita.y.volkov@mail.ru>+maintainer:+  Nikita Volkov <nikita.y.volkov@mail.ru>+copyright:+  (c) 2015, Nikita Volkov+license:+  MIT+license-file:+  LICENSE+build-type:+  Simple+cabal-version:+  >=1.10+extra-source-files:+  CHANGELOG.md+++source-repository head+  type:+    git+  location:+    git://github.com/nikita-volkov/conversion.git+++library+  hs-source-dirs:+    library+  ghc-options:+    -funbox-strict-fields+  default-extensions:+    Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFoldable, DeriveFunctor, DeriveGeneric, DeriveTraversable, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples+  default-language:+    Haskell2010+  other-modules:+  exposed-modules:+    Conversion+  build-depends:+    base-prelude >= 0.1.19 && < 0.2
+ library/Conversion.hs view
@@ -0,0 +1,548 @@+module Conversion (Conversion(..)) where++import BasePrelude+++-- |+-- A type-class, +-- which provides a non-partial conversion function from a value of type @a@ +-- to a value of type @b@.+-- +-- As per <http://en.wikipedia.org/wiki/Morphism#Definition the definition of Morphism>,+-- @a@ is the /domain/ and @b@ is the /codomain/.+class Conversion a b where+  convert :: a -> b+++-- |+-- Equivalent to 'atomically'.+instance Conversion (STM a) (IO a) where+  {-# INLINE convert #-}+  convert = atomically++-- |+-- Converts to any 'Alternative' type ('Maybe', list).+instance Alternative f => Conversion (Either a b) (f b) where+  {-# INLINE convert #-}+  convert = either (const empty) pure++-- |+-- Checks whether the value is 'Right'.+instance Conversion (Either a b) Bool where+  {-# INLINE convert #-}+  convert = either (const False) (const True)++-- |+-- Converts to any 'Alternative' type ('Either', list).+instance Alternative f => Conversion (Maybe a) (f a) where+  {-# INLINE convert #-}+  convert = maybe empty pure++-- |+-- Checks whether the value is 'Just'.+instance Conversion (Maybe a) Bool where+  {-# INLINE convert #-}+  convert = maybe False (const True)++-- |+-- Converts into a function, which extracts the value, +-- given a default value in the 'Nothing' case.+-- +-- Equivalent to 'fromMaybe'.+instance Conversion (Maybe a) (a -> a) where+  {-# INLINE convert #-}+  convert = flip fromMaybe++-- |+-- Gets the head of a list.+instance Alternative f => Conversion [a] (f a) where+  {-# INLINABLE convert #-}+  convert = \case [] -> empty; a : _ -> pure a++-- |+-- Checks whether the list is not empty.+instance Conversion [a] Bool where+  {-# INLINE convert #-}+  convert = null++-- |+-- Equivalent to 'catMaybes'.+instance Conversion [Maybe a] [a] where+  {-# INLINE convert #-}+  convert = catMaybes+++instance Conversion Int Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Int32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Int Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Int8 Integer where+  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int8 Int where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int8 Int16 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int8 Int32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int8 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int8 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int8 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int8 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int8 (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int8 (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Int16 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int16 Int where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int16 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Int16 Int32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int16 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int16 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int16 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int16 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int16 (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int16 (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Int32 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Int32 Int where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int32 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int32 (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Int32 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int32 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int32 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int32 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int32 (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int32 (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Int64 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Int64 (f Int) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Int32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Int64 (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Word Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word (f Int) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Int32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Int64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word Word64 where +  {-# INLINE convert #-}+  convert = fromIntegral+++instance Conversion Word8 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Int where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word8 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word8 Int16 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Int32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Word where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Word16 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Word32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word8 Word64 where +  {-# INLINE convert #-}+  convert = fromIntegral+++instance Conversion Word16 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word16 Int where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word16 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word16 (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word16 Int32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word16 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word16 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word16 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word16 Word32 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word16 Word64 where +  {-# INLINE convert #-}+  convert = fromIntegral+++instance Conversion Word32 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word32 (f Int) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word32 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word32 (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word32 (f Int32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word32 Int64 where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Conversion Word32 Word where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word32 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word32 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Conversion Word32 Word64 where +  {-# INLINE convert #-}+  convert = fromIntegral+++instance Conversion Word64 Integer where +  {-# INLINE convert #-}+  convert = fromIntegral++instance Alternative f => Conversion Word64 (f Int) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Int8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Int16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Int32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Int64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Word64 (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Alternative f => Conversion Integer (f Int) where+  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Int8) where+  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Int16) where+  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Int32) where+  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Int64) where+  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Word) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Word8) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Word16) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Word32) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral++instance Alternative f => Conversion Integer (f Word64) where +  {-# INLINE convert #-}+  convert = checkedFromIntegral+++instance Conversion Float Rational where+  {-# INLINE convert #-}+  convert = realToFrac++instance Conversion Float Double where+  {-# INLINE convert #-}+  convert = realToFrac+++instance Conversion Double Rational where+  {-# INLINE convert #-}+  convert = realToFrac+++{-# INLINABLE isomorphicallyChecked #-}+isomorphicallyChecked :: (Alternative f, Conversion b a, Eq a) => (a -> b) -> a -> f b+isomorphicallyChecked =+  \f a -> f a & \b -> if a == convert b then pure b else empty++{-# INLINABLE checkedFromIntegral #-}+checkedFromIntegral :: (Alternative f, Integral a, Integral b) => a -> f b+checkedFromIntegral =+  \a -> fromIntegral a & \b -> if fromIntegral b == a then pure b else empty