Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6996949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:13:19+00:00 2026-05-27T20:13:19+00:00

I’m writing an application that uses UTF-16 strings, and to make use of the

  • 0

I’m writing an application that uses UTF-16 strings, and to make use of the overloaded strings extension I tried to make an IsString instance for it:

import Data.Word ( Word16 )
import Data.String ( IsString(fromString) )

type String16 = [Word16]

instance IsString [Word16] where
    fromString = encodeUTF16

encodeUTF16 :: String -> String16

The problem is, when I try to compile the module, GHC 7.0.3 complains:

Data/String16.hs:35:10:
    Illegal instance declaration for `IsString [Word16]'
      (All instance types must be of the form (T a1 ... an)
       where a1 ... an are *distinct type variables*,
       and each type variable appears at most once in the instance head.
       Use -XFlexibleInstances if you want to disable this.)
    In the instance declaration for `IsString [Word16]'

If I comment out the instance declaration, it compiles successfully.

Why is this rejected? The instance for [Char] looks pretty much like the same thing, yet it compiles fine. Is there something I’ve missed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:13:19+00:00Added an answer on May 27, 2026 at 8:13 pm

    After having a look through the GHC manuals and around the Haskell wiki (especially the List instance page), I’ve got a better idea of how this works. Here’s a summary of what I’ve learned:

    Problem

    The Haskell Report defines an instance declaration like this:

    The type (T u1 … uk) must take the form of a type constructor T applied to simple type variables u1, … uk; furthermore, T must not be a type synonym, and the ui must all be distinct.

    The parts highlighted in bold are the restrictions that tripped me up. In English, they are:

    1. Anything after the type constructor must be a type variable.
    2. You can’t use a type alias (using the type keyword) to get around rule 1.

    So how does this relate to my problem?

    [Word16] is just another way of writing [] Word16. In other words, [] is the constructor and Word16 is its argument.

    So if we try to write:

    instance IsString [Word16]
    

    which is the same as

    instance IsString ([] Word16) where ...
    

    it won’t work, because it violates rule 1, as the compiler kindly points out.

    Trying to hide it in a type synonym with

    type String16 = [Word16]
    instance IsString String16 where ...
    

    won’t work either, because it violates part 2.

    So as it stands, it is impossible to get [Word16] (or a list of anything, for that matter) to implement IsString in standard Haskell.

    Enter… (drumroll please)

    Solution #1: newtype

    The solution @ehird suggested is to wrap it in a newtype:

    newtype String16 = String16 { unString16 :: [Word16] }
    instance IsString String16 where ...
    

    It gets around the restrictions because String16 is no longer an alias, it’s a new type (excuse the pun)! The only downside to this is we then have to wrap and unwrap it manually, which is annoying.

    Solution #2: Flexible instances

    At the expense of portability, we can drop the restriction altogether with flexible instances:

    {-# LANGUAGE FlexibleInstances #-}
    
    instance IsString [Word16] where ...
    

    This was the solution @[Daniel Wagner] suggested.

    Solution #3: Equality constraints

    Finally, there’s an even less portable solution using equality constraints:

    {-# LANGUAGE TypeFamilies #-}
    
    instance (a ~ Word16) => IsString [a] where ...
    

    This works better with type inference, but is more likely to overlap. See Chris Done’s article on the topic.

    (By the way, I ended up making a foldl' wrapper around Data.Text.Internal and writing the hash on top of that.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.