on master help sign in
Allow gunfold via a virtual pack constructor
commit2120f59cf557ba9f3a185ea67d77c7620c23c648 authorEdward Kmett <ekmett@gmail.com> dateThu 3 Apr 2014, 12:49 (13 years ago) parent260f3ab
2 files changed+21−10
Data/Text.hs+20−9 text.cabal+1−1
Data/Text.hs+20−9
@@ -202,8 +202,8 @@ import Control.DeepSeq (NFData)
202202import Control.Exception (assert)
203203#endif
204204import Data.Char (isSpace)
205−import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf))
206−import Data.Data (mkNoRepType)
205+import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf), constrIndex,
206+ Constr, mkConstr, DataType, mkDataType, Fixity(Prefix))
207207import Control.Monad (foldM)
208208import qualified Data.Text.Array as A
209209import qualified Data.List as L
@@ -335,20 +335,31 @@ instance NFData Text
335335-- | This instance preserves data abstraction at the cost of inefficiency.
336336-- We omit reflection services for the sake of data abstraction.
337337--
338−-- This instance was created by copying the behavior of Data.Set and
339−-- Data.Map. If you feel a mistake has been made, please feel free to
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
340340-- submit improvements.
341341--
342342-- Original discussion is archived here:
343343--
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, ...>
346349
347350instance Data Text where
348351 gfoldl f z txt = z pack `f` (unpack txt)
349− toConstr _ = P.error "Data.Text.Text.toConstr"
350− gunfold _ _ = P.error "Data.Text.Text.gunfold"
351− dataTypeOf _ = mkNoRepType "Data.Text.Text"
352+ toConstr _ = packConstr
353+ gunfold k z c = case constrIndex c of
354+ 1 -> k (z pack)
355+ _ -> P.error "gunfold"
356+ dataTypeOf _ = textDataType
357+
358+packConstr :: Constr
359+packConstr = mkConstr textDataType "pack" [] Prefix
360+
361+textDataType :: DataType
362+textDataType = mkDataType "Data.Text.Text" [packConstr]
352363
353364-- | /O(n)/ Compare two 'Text' values lexicographically.
354365compareText :: Text -> Text -> Ordering
text.cabal+1−1
@@ -1,5 +1,5 @@
11name: text
2−version: 1.1.0.1
2+version: 1.1.1
33homepage: https://github.com/bos/text
44bug-reports: https://github.com/bos/text/issues
55synopsis: An efficient packed Unicode text type.