Browse Source

Typeclass renamed into a better name

Marcos Dumay de Medeiros 9 years ago
parent
commit
365ddbffff
3 changed files with 49 additions and 32 deletions
  1. 0 29
      src/Data/Text/IsText.hs
  2. 46 0
      src/Data/Textual.hs
  3. 3 3
      textual.cabal

+ 0 - 29
src/Data/Text/IsText.hs

@@ -1,29 +0,0 @@
-{-# LANGUAGE TypeSynonymInstances #-}
-{-# LANGUAGE FlexibleInstances #-}
-
-module Data.Text.IsText where
-
-import Data.String
-import qualified Data.ByteString as SB
-import qualified Data.ByteString.Lazy as LB
-import qualified Codec.Binary.UTF8.String as UTF8
-import qualified Data.Text as T
-
-{- | Type class for data structures that are logically text -}
-class IsString a => IsText a where
-  toString :: a -> String
-
-instance IsText String where
-  toString = id
-{- | The UTF-8 encoding is assumed for ByteStrings -}
-instance IsText SB.ByteString where
-  toString = UTF8.decode . SB.unpack
-{- | The UTF-8 encoding is assumed for ByteStrings -}
-instance IsText LB.ByteString where
-  toString = UTF8.decode . LB.unpack
-instance IsText T.Text where
-  toString = T.unpack
-
-{- | Converts between instances of IsText -}
-fromText :: (IsText a, IsText b) => a -> b
-fromText = fromString . toString

+ 46 - 0
src/Data/Textual.hs

@@ -0,0 +1,46 @@
+{-# LANGUAGE TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
+
+{- |
+Type class for textual data and simple (fromItegral like) conversion
+between them.
+
+The conversion utility here, although simple may not be the fastest one
+available, and aims at preserving the textual representation of data,
+not its binary structure. This, given the existence of codecs with
+ambiguous representation means that the following function may evaluate
+to False:
+
+mayBeFalse :: Textual a => a -> Bool
+mayBeFalse a = let
+    b = fromText a
+    in a == b
+
+-}
+module Data.Textual where
+
+import Data.String
+import qualified Data.ByteString as SB
+import qualified Data.ByteString.Lazy as LB
+import qualified Codec.Binary.UTF8.String as UTF8
+import qualified Data.Text as ST
+import qualified Data.Text.Lazy as LT
+
+{- | Type class for data structures that are logically text -}
+class IsString a => Textual a where
+  toString :: a -> String
+
+instance Textual String where
+  toString = id
+{- | With UTF-8 encoding -}
+instance Textual SB.ByteString where
+  toString = UTF8.decode . SB.unpack
+{- | With UTF-8 encoding -}
+instance Textual LB.ByteString where
+  toString = UTF8.decode . LB.unpack
+instance Textual ST.Text where
+  toString = ST.unpack
+
+{- | Converts between instances of Textual -}
+fromTextual :: (Textual a, Textual b) => a -> b
+fromTextual = fromString . toString

+ 3 - 3
istext.cabal → textual.cabal

@@ -1,9 +1,9 @@
 -- Initial istext.cabal generated by cabal init.  For further 
 -- documentation, see http://haskell.org/cabal/users-guide/
 
-name:                istext
+name:                textual
 version:             0.1.0.0
-synopsis:            IsText type class for data that can be converted to and from Strings.
+synopsis:            Textual type class for data that represent text
 -- description:         
 homepage:            https://sealgram.com/git/haskell/text-like/
 license:             MIT
@@ -17,7 +17,7 @@ build-type:          Simple
 cabal-version:       >=1.10
 
 library
-  exposed-modules:     Data.Text.IsText
+  exposed-modules:     Data.Textual
   -- other-modules:       
   other-extensions:    TypeSynonymInstances, FlexibleInstances
   build-depends:       base >=4.7 && <4.8, bytestring >=0.10 && <0.11, utf8-string >=1 && <1.1, text >=1.2 && <1.3