I'm aware that the OverloadedStrings language pragma wraps an implicit fromString around all string literals. What I'd like to do is not actually overload strings, but merely change their meaning so that they are always turned into Text, and therefore, using a string literal as a list of characters should result in a type error.
It appears to be impossible to import the IsString class without also importing the String instance for that class. Does ghc provide some way for me to restrict string literals to Text only?
It's a little bit of overkill, but one solution is to combine
OverloadedStringsandRebindableSyntax. TheRebindableSyntaxextension causes all the implicit function calls that Haskell syntax uses to refer to whatever functions are in scope; for instance, integer literals use anyfromIntegral, not necessarilyPrelude.fromIntegral. As a side effect,Preludeis no longer implicitly imported, so you have to do that manually. As long as you do import it, there shouldn't be any issues with syntax using the wrong function implicitly (I think—I haven't actually used this technique). When combined withOverloadedStrings, this causes"foo"to be transformed intofromString "foo"for whateverfromString's in scope, not necessarilyData.String.fromString "foo". So makingfromStringsynonymous withpackwill do what you want. A complete example:This works fine, and changing
maintomain = putStrLn "Hello, world!"produces the desired error:Commenting out the definition of
fromStringcauses a different error:If you want it to work with both strict and lazy text, you could define your own
IsStringtype class, and make both of them instances; the class doesn't have to be calledIsString, just so long as it has afromStringmethod.Also, a word of warning: the section of the GHC manual on
RebindableSyntaxdoesn't mention thefromStringfunction, and the section onOverloadedStringsdoesn't mentionRebindableSyntax. There's no reason this shouldn't work, but I think that means that this solution technically relies on undocumented behavior.