Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
List instance
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Answer == First you have to find out, what you really want to do. === Define a custom type === If you do not need list functions, but e.g. just want to use <hask>String</hask> values as identifiers, then I advise to just define a custom type: <haskell> newtype Identifier = Identifier String </haskell> and write the instances you need for that type. === Keep working with the list (or container) type === If you need list functions, then the wrapper approach means that you have to litter your code with calls to the wrapper constructor. In this case you should stick to the original composed type. The trick in the Prelude for the <hask>Show</hask> class is, that <hask>listToX</hask> has a default implementation. If this is not possible in your application then introduce a new class like <haskell> class Element a where listToX :: [a] -> X </haskell> and define instances like <haskell> instance Element Char where listToX = ... instance Element a => C [a] where toX = listToX </haskell> . More generally, you can introduce a new class like <haskell> class IsChar a where fromChar :: Char -> a toChar :: a -> Char instance IsChar Char where fromChar = id toChar = id </haskell> and then whenever you want to use the "<hask>a</hask>" type as a <hask>Char</hask>, you just convert it from or to <hask>Char</hask> with <hask>fromChar</hask> or <hask>toChar</hask>, respectively. === FlexibleInstances === If none of the above solution is appropriate for you, then you may choose the <code>FlexibleInstances</code> language extension that is available in GHC and Hugs. See [https://downloads.haskell.org/~ghc/8.0.2/docs/html/users_guide/glasgow_exts.html#instance-termination-rules instance rules]. <haskell> {-# LANGUAGE FlexibleInstances #-} instance C [Char] where toX = ... </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width