on master help sign in
Allow gunfold via a virtual pack constructor
2120f59Edward Kmett
Data/Text.hs+20−9
@@ -202,8 +202,8 @@ import Control.DeepSeq (NFData)
202import Control.Exception (assert)
202import Control.Exception (assert)
203#endif
203#endif
204import Data.Char (isSpace)
204import Data.Char (isSpace)
205−import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf))
205+import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
206−import Data.Data (mkNoRepType)
206+ Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
207import Control.Monad (foldM)
207import Control.Monad (foldM)
208import qualified Data.Text.Array as A
208import qualified Data.Text.Array as A
209import qualified Data.List as L
209import qualified Data.List as L
@@ -335,20 +335,31 @@ instance NFData Text
335-- | This instance preserves data abstraction at the cost of inefficiency.
335-- | This instance preserves data abstraction at the cost of inefficiency.
336-- We omit reflection services for the sake of data abstraction.
336-- We omit reflection services for the sake of data abstraction.
337--
337--
338−-- This instance was created by copying the behavior of Data.Set and
338+-- This instance was created by copying the updated behavior of @Data.Set@ and
339−-- Data.Map. If you feel a mistake has been made, please feel free to
339+-- @Data.Map@. If you feel a mistake has been made, please feel free to
340-- submit improvements.
340-- submit improvements.
341--
341--
342-- Original discussion is archived here:
342-- Original discussion is archived here:
343--
343--
344−-- "could we get a Data instance for Data.Text.Text?"
345−-- http://groups.google.com/group/haskell-cafe/browse_thread/thread/b5bbb1b28a7e525d/0639d46852575b93
344+-- <http://groups.google.com/group/haskell-cafe/browse_thread/thread/b5bbb1b28a7e525d/0639d46852575b93 could we get a Data instance for Data.Text.Text?>
345+--
346+-- The followup discussion that changed the behavior of @Data.Set@ and @Data.Map@ is archived here
347+--
348+-- <http://markmail.org/message/trovdc6zkphyi3cr#query:+page:1+mid:a46der3iacwjcf6n+state:results Proposal: Allow gunfold for Data.Map, ...>
346
349
347instance Data Text where
350instance Data Text where
348 gfoldl f z txt = z pack `f` (unpack txt)
351 gfoldl f z txt = z pack `f` (unpack txt)
349− toConstr _ = P.error "Data.Text.Text.toConstr"
352+ toConstr _ = packConstr
353+ gunfold k z c = case constrIndex c of
354+ 1 -> k (z pack)
350− gunfold _ _ = P.error "Data.Text.Text.gunfold"
355+ _ -> P.error "gunfold"
351− dataTypeOf _ = mkNoRepType "Data.Text.Text"
356+ dataTypeOf _ = textDataType
357+
358+packConstr :: Constr
359+packConstr = mkConstr textDataType "pack" [] Prefix
360+
361+textDataType :: DataType
362+textDataType = mkDataType "Data.Text.Text" [packConstr]
352
363
353-- | /O(n)/ Compare two 'Text' values lexicographically.
364-- | /O(n)/ Compare two 'Text' values lexicographically.
354compareText :: Text -> Text -> Ordering
365compareText :: Text -> Text -> Ordering
text.cabal+1−1
@@ -1,5 +1,5 @@
1name: text
1name: text
2−version: 1.1.0.1
2+version: 1.1.1
3homepage: https://github.com/bos/text
3homepage: https://github.com/bos/text
4bug-reports: https://github.com/bos/text/issues
4bug-reports: https://github.com/bos/text/issues
5synopsis: An efficient packed Unicode text type.
5synopsis: An efficient packed Unicode text type.