Tutorial by Examples

It's a very common extension that allows type classes with multiple type parameters. You can think of MPTC as a relation between types. {-# LANGUAGE MultiParamTypeClasses #-} class Convertable a b where convert :: a -> b instance Convertable Int Float where convert i = fromIntegr...
Regular instances require: 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. That means that, for example, while you can create an instance for [a] you can't create an instance f...
Normally, string literals in Haskell have a type of String (which is a type alias for [Char]). While this isn't a problem for smaller, educational programs, real-world applications often require more efficient storage such as Text or ByteString. OverloadedStrings simply changes the type of literals...
A syntactic extension that allows applying the tuple constructor (which is an operator) in a section way: (a,b) == (,) a b -- With TupleSections (a,b) == (,) a b == (a,) b == (,b) a N-tuples It also works for tuples with arity greater than two (,2,) 1 3 == (1,2,3) Mapping This can be u...
An extension that allows you to use Unicode characters in lieu of certain built-in operators and names. ASCIIUnicodeUse(s)::∷has type->→function types, lambdas, case branches, etc.=>⇒class constraintsforall∀explicit polymorphism<-←do notation*★the type (or kind) of types (e.g., Int :: ★)&g...
Standard Haskell allows you to write integer literals in decimal (without any prefix), hexadecimal (preceded by 0x or 0X), and octal (preceded by 0o or 0O). The BinaryLiterals extension adds the option of binary (preceded by 0b or 0B). 0b1111 == 15 -- evaluates to: True
This is a type system extension that allows types that are existentially quantified, or, in other words, have type variables that only get instantiated at runtime†. A value of existential type is similar to an abstract-base-class reference in OO languages: you don't know the exact type in contains,...
A syntactic extension that lets you write \case in place of \arg -> case arg of. Consider the following function definition: dayOfTheWeek :: Int -> String dayOfTheWeek 0 = "Sunday" dayOfTheWeek 1 = "Monday" dayOfTheWeek 2 = "Tuesday" dayOfTheWeek 3 = "W...
Imagine the following situation: foo :: Show a => (a -> String) -> String -> Int -> IO () foo show' string int = do putStrLn (show' string) putStrLn (show' int) Here, we want to pass in a function that converts a value into a String, apply that function to both a string p...
added in GHC 7.8. OverloadedLists, similar to OverloadedStrings, allows list literals to be desugared as follows: [] -- fromListN 0 [] [x] -- fromListN 1 (x : []) [x .. ] -- fromList (enumFrom x) This comes handy when dealing with types such as Set, Vector and Maps. ['0'...
If you have a multi-parameter type-class with arguments a, b, c, and x, this extension lets you express that the type x can be uniquely identified from a, b, and c: class SomeClass a b c x | a b c -> x where ... When declaring an instance of such class, it will be checked against all other in...
Conventional algebraic datatypes are parametric in their type variables. For example, if we define an ADT like data Expr a = IntLit Int | BoolLit Bool | If (Expr Bool) (Expr a) (Expr a) with the hope that this will statically rule out non-well-typed conditionals, this...
ScopedTypeVariables let you refer to universally quantified types inside of a declaration. To be more explicit: import Data.Monoid foo :: forall a b c. (Monoid b, Monoid c) => (a, b, c) -> (b, c) -> (a, b, c) foo (a, b, c) (b', c') = (a :: a, b'', c'') where (b'', c'') = (b <&g...
Pattern synonyms are abstractions of patterns similar to how functions are abstractions of expressions. For this example, let's look at the interface Data.Sequence exposes, and let's see how it can be improved with pattern synonyms. The Seq type is a data type that, internally, uses a complicated r...

Page 1 of 1