Haskell_Language

Haskell IntroductionData.Aeson - JSON in HaskellHaskell Applicative FunctorHaskell Arbitrary-rank polymorphism with RankNTypesHaskell ArithmeticHaskell ArrowsHaskell AttoparsecHaskell BifunctorHaskell CabalHaskell Category TheoryHaskell Common functors as the base of cofree comonadsHaskell Common GHC Language ExtensionsHaskell Common monads as free monadsHaskell ConcurrencyHaskell Containers - Data.MapHaskell Creating Custom Data TypesHaskell Data.TextHaskell DatabasesHaskell Date and TimeHaskell Fixity declarationsHaskell FoldableHaskell Foreign Function InterfaceHaskell Free MonadsHaskell Function call syntaxHaskell Function compositionHaskell FunctorHaskell Generalized Algebraic Data TypesHaskell GHCJSHaskell Google Protocol BuffersHaskell Graphics with GlossHaskell Gtk3Haskell Higher-order functionsHaskell Infix operatorsHaskell IOHaskell LensHaskell List ComprehensionsHaskell ListsHaskell LoggingHaskell ModulesHaskell Monad TransformersHaskell MonadsHaskell MonoidHaskell OptimizationHaskell Overloaded LiteralsHaskell ParallelismHaskell Parsing HTML with taggy-lens and lensHaskell Partial ApplicationHaskell Phantom typesHaskell PipesHaskell ProfunctorHaskell ProxiesHaskell QuickCheckHaskell Reactive-bananaHaskell Reader / ReaderTHaskell Record SyntaxHaskell Recursion SchemesHaskell Rewrite rules (GHC)Haskell RoleHaskell Sorting AlgorithmsHaskell StackHaskell State MonadHaskell Streaming IOHaskell StrictnessHaskell Syntax in FunctionsHaskell Testing with TastyHaskell TraversableHaskell Tuples (Pairs, Triples, ...)Haskell Type algebraHaskell Type ApplicationHaskell Type ClassesHaskell Type FamiliesHaskell Typed holesHaskell Using GHCiHaskell VectorsHaskell Web DevelopmentHaskell XMLHaskell zipWithMTemplate Haskell & QuasiQuotes



Haskell Overloaded Literals

From WikiOD

Remarks[edit | edit source]

Integer Literals[edit | edit source]

is a numeral without a decimal point

for example 0, 1, 42, ...

is implicitly applied to fromInteger which is part of the Num type class so it indeed has type Num a => a - that is it can have any type that is an instance of Num


Fractional Literals[edit | edit source]

is a numeral with a decimal point

for example 0.0, -0.1111, ...

is implicitly applied to fromRational which is part of the Fractional type class so it indeed has type a => a - that is it can have any type that is an instance of Fractional


String Literals[edit | edit source]

If you add the language extension OverloadedStrings to GHC you can have the same for String-literals which then are applied to fromString from the Data.String.IsString type class

This is often used to replace String with Text or ByteString.


List Literals[edit | edit source]

Lists can defined with the [1, 2, 3] literal syntax. In GHC 7.8 and beyond, this can also be used to define other list-like structures with the OverloadedLists extension.

By default, the type of [] is:

> :t []
[] :: [t]

With OverloadedLists, this becomes:

[] :: GHC.Exts.IsList l => l

Strings[edit | edit source]

The type of the literal[edit | edit source]

Without any extensions, the type of a string literal – i.e., something between double quotes – is just a string, aka list of characters:

Prelude> :t "foo"
"foo" :: [Char]

However, when the OverloadedStrings extension is enabled, string literals become polymorphic, similar to number literals:

Prelude> :set -XOverloadedStrings
Prelude> :t "foo"
"foo" :: Data.String.IsString t => t

This allows us to define values of string-like types without the need for any explicit conversions. In essence, the OverloadedStrings extension just wraps every string literal in the generic fromString conversion function, so if the context demands e.g. the more efficient Text instead of String, you don't need to worry about that yourself.

Using string literals[edit | edit source]

{*# LANGUAGE OverloadedStrings #-}

import Data.Text (Text, pack)
import Data.ByteString (ByteString, pack)

withString :: String
withString = "Hello String"
** The following two examples are only allowed with OverloadedStrings

withText :: Text
withText = "Hello Text"      -- instead of: withText = Data.Text.pack "Hello Text"

withBS :: ByteString
withBS = "Hello ByteString"  -- instead of: withBS = Data.ByteString.pack "Hello ByteString"

Notice how we were able to construct values of Text and ByteString in the same way we construct ordinary String (or [Char]) Values, rather than using each types pack function to encode the string explicitly.

For more information on the OverloadedStrings language extension, see the extension documentation.

Floating Numeral[edit | edit source]

The type of the literal[edit | edit source]

Prelude> :t 1.0
1.0 :: Fractional a => a

Choosing a concrete type with annotations[edit | edit source]

You can specify the type with a type annotation. The only requirement is that the type must have a Fractional instance.

Prelude> 1.0 :: Double
1.0
it :: Double
Prelude> 1.0 :: Data.Ratio.Ratio Int
1 % 1
it :: GHC.Real.Ratio Int

if not the compiler will complain

Prelude> 1.0 :: Int
<interactive>:
    No instance for (Fractional Int) arising from the literal `1.0'
    In the expression: 1.0 :: Int
    In an equation for `it': it = 1.0 :: Int

Integer Numeral[edit | edit source]

The type of the literal[edit | edit source]

Prelude> :t 1
1 :: Num a => a

choosing a concrete type with annotations[edit | edit source]

You can specify the type as long as the target type is Num with an annotation:

Prelude> 1 :: Int
1
it :: Int
Prelude> 1 :: Double
1.0
it :: Double
Prelude> 1 :: Word
1
it :: Word

if not the compiler will complain

Prelude> 1 :: String

<interactive>:
    No instance for (Num String) arising from the literal `1'
    In the expression: 1 :: String
    In an equation for `it': it = 1 :: String

List Literals[edit | edit source]

GHC's OverloadedLists extension allows you to construct list-like data structures with the list literal syntax.

This allows you to Data.Map like this:

> :set -XOverloadedLists
> import qualified Data.Map as M
> M.lookup "foo" [("foo", 1), ("bar", 2)]
Just 1

Instead of this (note the use of the extra M.fromList):

> import Data.Map as M
> M.lookup "foo" (M.fromList [("foo", 1), ("bar", 2)])
Just 1

Credit:Stack_Overflow_Documentation