solirc avatar 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`.

Comments (0)

Files changed (2)

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

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.