type-iso 0.1.0.0 → 1.0.1.0
raw patch · 4 files changed
Files
- Data/Types/Injective.hs +19/−3
- Data/Types/Isomorphic.hs +12/−15
- changelog.txt +1/−0
- type-iso.cabal +11/−3
Data/Types/Injective.hs view
@@ -1,18 +1,24 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE ExplicitForAll #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE GADTs #-} -- |Injective types. This module contains the 'Injective' typeclass and instances -- for the following equivalence classes: -- -- * @{Strict Text, Lazy Text, String}@. 'ByteString's are not part of this, --- since there exists more than one way to turn unicode text into a ByteString +-- since there is more than one way to turn unicode text into a ByteString -- (see "Data.Text.Encoding" and "Data.Text.Lazy.Encoding"). --- * @{Whole, Integer}@. Be advices, though, that Peano numbers may contain +-- * @{Whole, Integer}@. Be advised, though, that Peano numbers may contain -- unobservable infinities (i.e. @infinity = S infinity@) and thus, -- the conversion to Integer may not terminate. -- * @{Nat, Natural}@. For finite values, they're extensionally equivalent, -- but 'Nat' has lazy infinity. +-- * @{Seq a, Vector a}@. Supports all kinds of immutable vectors of elements: +-- boxed, unboxed, primitive, storable. @[a]@ is not part of this, as +-- typeclass 'IsList' provides 'fromList' and 'toList'. -- -- Additional injections: -- @@ -26,10 +32,14 @@ import Data.Default import qualified Data.Maybe as M import qualified Data.Ratio as R +import qualified Data.Sequence as S import qualified Data.Text as TS import qualified Data.Text.Encoding as TS import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Encoding as TL +import qualified Data.Vector.Generic as VG +import qualified VectorBuilder.Builder as VB +import qualified VectorBuilder.Vector as VB import qualified Numeric.Peano as PN -- |The class relation between types @a@ and @b@ s.t. @a@ can be injected @@ -47,7 +57,7 @@ -- @to@ should be a total function. No cheating by it undefined for parts of the set! class Injective a b where -- |Converts a value of type @a@ "losslessly" to one of type @b@. - to :: a -> b + to :: forall b1 a1. (b1 ~ b, a1 ~ a) => a -> b instance Injective a a where to = id @@ -88,3 +98,9 @@ instance Injective PN.Whole R.Rational where to (PN.Whole n PN.Pos) = (fromIntegral n) R.% 1 to (PN.Whole n PN.Neg) = negate (fromIntegral n) R.% 1 + +-- equivalence class of finite sequences +instance VG.Vector v a => Injective (S.Seq a) (v a) where + to = VB.build . VB.foldable +instance VG.Vector v a => Injective (v a) (S.Seq a) where + to v = S.fromFunction (VG.length v) (v VG.!)
Data/Types/Isomorphic.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE ExplicitForAll #-} -- |Contains the class definition of 'Iso', indicating isomorphism between two -- types. @@ -11,13 +12,15 @@ import qualified Numeric.Natural as N import qualified Numeric.Peano as PN +import qualified Data.Sequence as S import qualified Data.Text as TS import qualified Data.Text.Lazy as TL +import qualified Data.Vector.Generic as VG import Data.Types.Injective -- |The class of isomorphic types, i.e. those which can be cast to each --- other withouth loss of information. Type isomorphism is an equivalence +-- other without loss of information. Type isomorphism is an equivalence -- relation (reflexive, symmetric, transitive), but due to the limitations -- of the type system, only reflexivity is implemented for all types. -- Since there are no type inequality constraints, writing symmetry and @@ -28,27 +31,17 @@ -- -- [@Isomorphism@] -- @ --- isoFrom . isoTo = id +-- from . to = id -- @ -- -- Reflexivity, symmetry and transitivity are then "free": -- -- @ --- instance Iso a a where --- isoTo = id --- isoFrom = id --- @ --- --- @ --- instance Iso a b => Iso b a where --- isoTo = isoFrom --- isoFrom = isoTo +-- instance Iso a a -- @ -- -- @ --- instance (Iso a b, Iso b c) => Iso a c where --- isoTo = isoTo . isoTo --- isFrom = isoFrom . isoFrom +-- instance (Iso a b, Iso b c) => Iso a c -- @ -- -- Out of these, only the first one (reflexivity) is actually implemented, @@ -58,7 +51,7 @@ class (Injective a b, Injective b a) => Iso a b where -- |Synonym for 'to'. -from :: (Iso a b) => b -> a +from :: forall b a. (Iso a b) => b -> a from = to instance Iso a a where @@ -76,3 +69,7 @@ -- Peano wholes and integers. instance Iso PN.Whole Integer instance Iso Integer PN.Whole + +-- equivalence class of finite sequences +instance (VG.Vector v a) => Iso (S.Seq a) (v a) +instance (VG.Vector v a) => Iso (v a) (S.Seq a)
+ changelog.txt view
@@ -0,0 +1,1 @@+1.0.0.0 Renamed 'isoFrom' and 'isoTo' to just 'from' and 'to', flipped the type argument order to allow specifying the target type first.
type-iso.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: type-iso -version: 0.1.0.0 +version: 1.0.1.0 synopsis: Typeclasses for injective relations and isomorphisms between types. description: This package defines \"can be cast to\" relations between types: two types a and b are an instance of Injective if there's an injective function from a to b. If there is also an injective function from b to a, a and b are instances of Iso, meaning that one can convert back and forth losslessly (up to some appropriate notion of equality). The main purpose of this little package is to provide easy casting between the common string types (String, strict/lazy Text) and numeric types (Integers, Peano numbers), without having to look up the names of the various conversion functions all the time. homepage: https://github.com/ombocomp/type-iso @@ -13,6 +13,7 @@ category: Data, Cast, Types build-type: Simple cabal-version: >=1.10 +extra-source-files: changelog.txt source-repository head type: git @@ -20,6 +21,13 @@ library exposed-modules: Data.Types.Isomorphic, Data.Types.Injective - other-extensions: MultiParamTypeClasses, FlexibleInstances, FlexibleContexts - build-depends: base >=4.7 && <5, nats >=0.2, text >=1.1, numericpeano >= 0.2, data-default >= 0.5 + other-extensions: MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, ExplicitForAll, GADTs + build-depends: base >=4.7 && <5 + , containers >= 0.5.6.2 + , nats >=0.2 + , text >=1.1 + , numericpeano >= 0.2 + , data-default >= 0.5 + , vector >= 0.5 + , vector-builder >= 0.3.7 default-language: Haskell2010