solirc committed 062e800

Use equational constraints for IsString instances

This helps with type checking. Here is an example:

"foo" *> "bar" :: Parser Text

This code did not type check before, because we had an `IsString`
instance for `Parser Text`, but all that the type checker can infer
about "foo" is that it is of type `Parser a`.

The instance I added is now for `Parser a` instead, but we also
constrain the type of `a` to `Text`. With this the above code does now
type check: If the type checker sees `Parser a` it selects the right
instance, and by extension will infer `a` to be of type `Text`.

  • Participants
  • Parent commits 1cd21cd

Comments (0)

Files changed (2)

File Data/Attoparsec/ByteString/Char8.hs

-{-# LANGUAGE BangPatterns, FlexibleInstances, TypeSynonymInstances #-}
+{-# LANGUAGE BangPatterns, FlexibleInstances, TypeSynonymInstances, GADTs #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 -- |
 import qualified Data.ByteString as B8
 import qualified Data.ByteString.Char8 as B
-instance IsString (Parser B.ByteString) where
+instance (a ~ B.ByteString) => IsString (Parser a) where
     fromString = I.string . B.pack
 -- $encodings

File Data/Attoparsec/Text/Internal.hs

 {-# LANGUAGE BangPatterns, CPP, FlexibleInstances, OverloadedStrings,
-    Rank2Types, RecordWildCards, TypeSynonymInstances #-}
+    Rank2Types, RecordWildCards, TypeSynonymInstances, GADTs #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 -- |
 -- Module      :  Data.Attoparsec.Text.Internal
 type Failure r = T.Failure Text r
 type Success a r = T.Success Text a r
-instance IsString (Parser Text) where
+instance (a ~ Text) => IsString (Parser a) where
     fromString = string . T.pack
 lengthAtLeast :: T.Text -> Int -> Bool