Skip to content

Commit d947bae

Browse files
quasicomputationalhvr
authored andcommitted
Add Lift instances for Text.
These have similar trade-offs to the existing `Data` instances: preserving abstraction at the cost of efficiency. Due to haskell/cabal#5623, the tests exercising this feature have to live in their own package.
1 parent 7c86915 commit d947bae

File tree

11 files changed

+138
-4
lines changed

11 files changed

+138
-4
lines changed

Data/Text.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
#if __GLASGOW_HASKELL__ >= 708
77
{-# LANGUAGE TypeFamilies #-}
88
#endif
9+
-- Using TemplateHaskell in text unconditionally is unacceptable, as
10+
-- it's a GHC boot library. TemplateHaskellQuotes was added in 8.0, so
11+
-- this would seem to be a problem. However, GHC's policy of only
12+
-- needing to be able to compile itself from the last few releases
13+
-- allows us to use full-fat TH on older versions, while using THQ for
14+
-- GHC versions that may be used for bootstrapping.
15+
#if __GLASGOW_HASKELL__ >= 800
16+
{-# LANGUAGE TemplateHaskellQuotes #-}
17+
#else
18+
{-# LANGUAGE TemplateHaskell #-}
19+
#endif
920

1021
-- |
1122
-- Module : Data.Text
@@ -244,6 +255,8 @@ import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt)
244255
#if __GLASGOW_HASKELL__ >= 708
245256
import qualified GHC.Exts as Exts
246257
#endif
258+
import qualified Language.Haskell.TH.Lib as TH
259+
import Language.Haskell.TH.Syntax (Lift, lift)
247260
#if MIN_VERSION_base(4,7,0)
248261
import Text.Printf (PrintfArg, formatArg, formatString)
249262
#endif
@@ -413,6 +426,13 @@ instance Data Text where
413426
_ -> P.error "gunfold"
414427
dataTypeOf _ = textDataType
415428

429+
-- | This instance has similar considerations to the 'Data' instance:
430+
-- it preserves abstraction at the cost of inefficiency.
431+
--
432+
-- @since 1.2.4.0
433+
instance Lift Text where
434+
lift = TH.appE (TH.varE 'pack) . TH.stringE . unpack
435+
416436
#if MIN_VERSION_base(4,7,0)
417437
-- | Only defined for @base-4.7.0.0@ and later
418438
--

Data/Text/Lazy.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
#if __GLASGOW_HASKELL__ >= 708
77
{-# LANGUAGE TypeFamilies #-}
88
#endif
9+
-- Using TemplateHaskell in text unconditionally is unacceptable, as
10+
-- it's a GHC boot library. TemplateHaskellQuotes was added in 8.0, so
11+
-- this would seem to be a problem. However, GHC's policy of only
12+
-- needing to be able to compile itself from the last few releases
13+
-- allows us to use full-fat TH on older versions, while using THQ for
14+
-- GHC versions that may be used for bootstrapping.
15+
#if __GLASGOW_HASKELL__ >= 800
16+
{-# LANGUAGE TemplateHaskellQuotes #-}
17+
#else
18+
{-# LANGUAGE TemplateHaskell #-}
19+
#endif
920

1021
-- |
1122
-- Module : Data.Text.Lazy
@@ -239,6 +250,8 @@ import qualified GHC.Base as GHC
239250
import qualified GHC.Exts as Exts
240251
#endif
241252
import GHC.Prim (Addr#)
253+
import qualified Language.Haskell.TH.Lib as TH
254+
import Language.Haskell.TH.Syntax (Lift, lift)
242255
#if MIN_VERSION_base(4,7,0)
243256
import Text.Printf (PrintfArg, formatArg, formatString)
244257
#endif
@@ -399,6 +412,13 @@ instance Data Text where
399412
_ -> error "Data.Text.Lazy.Text.gunfold"
400413
dataTypeOf _ = textDataType
401414

415+
-- | This instance has similar considerations to the 'Data' instance:
416+
-- it preserves abstraction at the cost of inefficiency.
417+
--
418+
-- @since 1.2.4.0
419+
instance Lift Text where
420+
lift = TH.appE (TH.varE 'pack) . TH.stringE . unpack
421+
402422
#if MIN_VERSION_base(4,7,0)
403423
-- | Only defined for @base-4.7.0.0@ and later
404424
--

benchmarks/text-benchmarks.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ executable text-benchmarks
4242
ghc-prim,
4343
integer-gmp,
4444
stringsearch,
45+
template-haskell,
4546
transformers,
4647
utf8-string,
4748
vector

cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-- See http://cabal.readthedocs.io/en/latest/nix-local-build-overview.html
2-
packages: ., benchmarks
2+
packages: ., benchmarks, th-tests
33
tests: True

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### next
2+
3+
* `Lift` instances `Data.Text.Text` and `Data.Text.Lazy.Text`.
4+
15
### 1.2.3.1
26

37
* Make `decodeUtf8With` fail explicitly for unsupported non-BMP

tests/text-tests.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ library
146146
binary,
147147
deepseq,
148148
ghc-prim,
149-
integer-gmp
149+
integer-gmp,
150+
template-haskell
150151

151152
if flag(bytestring-builder)
152153
build-depends: bytestring >= 0.9 && < 0.10.4,

text.cabal

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: >= 1.8
22
name: text
3-
version: 1.2.3.1
3+
version: 1.2.4.0
44

55
homepage: https://github.com/haskell/text
66
bug-reports: https://github.com/haskell/text/issues
@@ -147,7 +147,8 @@ library
147147
base >= 4.2 && < 5,
148148
binary,
149149
deepseq >= 1.1.0.0,
150-
ghc-prim >= 0.2
150+
ghc-prim >= 0.2,
151+
template-haskell
151152

152153
if flag(bytestring-builder)
153154
build-depends: bytestring >= 0.9 && < 0.10.4,
@@ -255,6 +256,7 @@ test-suite tests
255256
ghc-prim,
256257
quickcheck-unicode >= 1.0.1.0,
257258
random,
259+
template-haskell,
258260
test-framework >= 0.4,
259261
test-framework-hunit >= 0.2,
260262
test-framework-quickcheck2 >= 0.2

th-tests/LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2008-2009, Tom Harper
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions
6+
are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

th-tests/tests/Lift.hs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
module Lift
4+
( tests
5+
)
6+
where
7+
8+
import qualified Data.Text as S
9+
import qualified Data.Text.Lazy as L
10+
import Language.Haskell.TH.Syntax (lift)
11+
import Test.HUnit (assertBool, assertEqual, assertFailure)
12+
import qualified Test.Framework as F
13+
import qualified Test.Framework.Providers.HUnit as F
14+
15+
tests :: F.Test
16+
tests = F.testGroup "TH lifting Text"
17+
[ F.testCase "strict" $ assertEqual "strict"
18+
$(lift ("foo" :: S.Text))
19+
("foo" :: S.Text)
20+
, F.testCase "lazy" $ assertEqual "lazy"
21+
$(lift ("foo" :: L.Text))
22+
("foo" :: L.Text)
23+
]

th-tests/tests/th-tests.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- | Provides a simple main function which runs all the tests
2+
--
3+
module Main
4+
( main
5+
) where
6+
7+
import Test.Framework (defaultMain)
8+
9+
import qualified Lift
10+
11+
main :: IO ()
12+
main = defaultMain [Lift.tests]

0 commit comments

Comments
 (0)