String.hs 2.78 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
2
{-# LANGUAGE NoImplicitPrelude, FlexibleInstances #-}
3
{-# LANGUAGE TypeFamilies #-}
4

5 6 7 8 9 10 11 12 13 14
-----------------------------------------------------------------------------
-- |
-- Module      :  Data.String
-- Copyright   :  (c) The University of Glasgow 2007
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
--
15
-- The @String@ type and associated operations.
16 17 18 19
--
-----------------------------------------------------------------------------

module Data.String (
basvandijk's avatar
basvandijk committed
20 21
   String
 , IsString(..)
22 23 24 25 26 27

 -- * Functions on strings
 , lines
 , words
 , unlines
 , unwords
28 29 30
 ) where

import GHC.Base
31
import Data.Functor.Const (Const (Const))
32 33
import Data.List (lines, words, unlines, unwords)

34
-- | Class for string-like datastructures; used by the overloaded string
shelarcy's avatar
shelarcy committed
35
--   extension (-XOverloadedStrings in GHC).
36 37 38
class IsString a where
    fromString :: String -> a

Simon Peyton Jones's avatar
Simon Peyton Jones committed
39 40
{- Note [IsString String]
~~~~~~~~~~~~~~~~~~~~~~~~~
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
Previously, the IsString instance that covered String was a flexible
instance for [Char]. This is in some sense the most accurate choice,
but there are cases where it can lead to an ambiguity, for instance:

  show $ "foo" ++ "bar"

The use of (++) ensures that "foo" and "bar" must have type [t] for
some t, but a flexible instance for [Char] will _only_ match if
something further determines t to be Char, and nothing in the above
example actually does.

So, the above example generates an error about the ambiguity of t,
and what's worse, the above behavior can be generated by simply
typing:

   "foo" ++ "bar"

into GHCi with the OverloadedStrings extension enabled.

The new instance fixes this by defining an instance that matches all
[a], and forces a to be Char. This instance, of course, overlaps
with things that the [Char] flexible instance doesn't, but this was
judged to be an acceptable cost, for the gain of providing a less
confusing experience for people experimenting with overloaded strings.

It may be possible to fix this via (extended) defaulting. Currently,
the rules are not able to default t to Char in the above example. If
a more flexible system that enabled this defaulting were put in place,
then it would probably make sense to revert to the flexible [Char]
instance, since extended defaulting is enabled in GHCi. However, it
is not clear at the time of this note exactly what such a system
would be, and it certainly hasn't been implemented.

A test case (should_run/overloadedstringsrun01.hs) has been added to
ensure the good behavior of the above example remains in the future.
-}
Simon Peyton Jones's avatar
Simon Peyton Jones committed
77

78
instance (a ~ Char) => IsString [a] where
Simon Peyton Jones's avatar
Simon Peyton Jones committed
79
         -- See Note [IsString String]
80
    fromString xs = xs
dterei's avatar
dterei committed
81

82 83
instance IsString a => IsString (Const a b) where
    fromString = Const . fromString