20-CS-4003-001 Organization of Programming Languages Fall 2017
Types and Type Classes

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

built-in data types and type classes

   
Type classes
Eq- Includes any type where equality comparison makes sense (e.g. Integers)
ghci> :t (==) results in (==) :: Eq a => a -> a -> Bool
ghci> :t (/=) results in (/=) :: Eq a => a -> a -> Bool
Ord- Includes any type where elements can be compared (<=, >=, etc. apply)
This is a superset of the Eq class
ghci> :t compare results in compare :: Ord a => a -> a -> Ordering
ghci> :t (<) results in (<) :: Ord a => a -> a -> Bool
ghci> :t (<=) results in (<=) :: Ord a => a -> a -> Bool
ghci> :t (>) results in (>) :: Ord a => a -> a -> Bool
ghci> :t (>=) results in (>=) :: Ord a => a -> a -> Bool
ghci> :t max results in max :: Ord a => a -> a -> a
ghci> :t min results in min :: Ord a => a -> a -> a
Show- Includes any type where elements can be displayed as a string
such as Ordering, Integer, Int, Char, Bool, Float, Double, (), and []
ghci> show 3 results in "3"
ghci> show (3,2) results in "(3,2)"
Read- Includes any type. The associated read function gets confused easily.
The read function takes a string as argument, the return value
is either inferred or explicitly given and belongs to the Read class.
ghci> read "3" results in an error (of confusion)
ghci> read "3"::Int results in the number 3
ghci> read "3"::Float results in 3.0
ghci> read "'A'"::Char results in 'A'
ghci> read "4" + 2 results in 6 (type inferred)
Enum- Includes any type where elements can be enumerated
such as (), Bool, Double, Float, Integer, Int, Char, Ordering
ghci> :t pred results in pred :: Enum a => a -> a
ghci> :t succ results in succ :: Enum a => a -> a
Bounded- Includes any type where elements have a finite range of values.
ghci> :t minBound results in minBound :: a
ghci> :t maxBound results in maxBound :: a
ghci> minBound :: Bool returns False
ghci> maxBound :: Bool returns True
ghci> minBound :: Ordering returns LT
ghci> maxBound :: Ordering returns GT
ghci> minBound :: Char returns '\NUL'
ghci> maxBound :: Char returns '\1114111'
Num- Includes any type where elements can act like a number.
ghci> :t (*) returns (*) :: Num a => a -> a -> a
ghci> :t (+) returns (*) :: Num a => a -> a -> a
ghci> :t (-) returns (*) :: Num a => a -> a -> a
ghci> :t negate returns negate :: Num a => a -> a
ghci> :t abs returns abs :: Num a => a -> a
Real- Subclasses Num and Ord and includes types Double, Float, Integer, Int.
ghci> :t toRational returns toRational :: Real a => a -> Rational
Fractional- Subclasses Num and includes types Double and Float.
ghci> :t (/) returns (/) :: Fractional a => a -> a -> a
RealFrac- Subclasses Real and Fractional and includes types Double and Float.
ghci> :t properFraction returns properFraction :: (Integral b, RealFrac a) => a -> (b,a)
ghci> :t truncate returns truncate :: (Integral b, RealFrac a) => a -> b
ghci> :t round returns round :: (Integral b, RealFrac a) => a -> b
ghci> :t ceiling returns ceiling :: (Integral b, RealFrac a) => a -> b
ghci> :t floor returns floor :: (Integral b, RealFrac a) => a -> b
Integral- Subclasses Real and Enum and includes types Integer and Int.
ghci> :t quot returns quot :: Integral a => a -> a -> a
ghci> :t rem returns rem :: Integral a => a -> a -> a
ghci> :t div returns div :: Integral a => a -> a -> a
ghci> :t mod returns mod :: Integral a => a -> a -> a
ghci> :t quotRem returns quotRem :: Integral a => a -> a -> (a,a)
ghci> :t divMod returns divMod :: Integral a => a -> a -> (a,a)
Floating- Subclasses Fractional and includes types Double and Float.
ghci> :t pi returns pi :: Floating a => a
ghci> :t exp returns exp :: Floating a => a -> a
ghci> :t sqrt returns sqrt :: Floating a => a -> a
ghci> :t log returns log :: Floating a => a -> a
ghci> :t (**) returns (**) :: Floating a => a -> a -> a
ghci> :t logBase returns logBase :: Floating a => a -> a -> a
ghci> :t sin returns sin :: Floating a => a -> a
ghci> :t cos returns cos :: Floating a => a -> a
...
RealFloat- Subclasses RealFrac and Floating and includes types Double and Float.
ghci> :t floatRadix returns floatRadix :: RealFloat a => a -> Integer
ghci> :t floatDigits returns floatDigits :: RealFloat a => a -> Int
ghci> :t floatRange returns floatRange :: RealFloat a => a -> (Int,Int)
ghci> :t decodeFloat returns decodeFloat :: RealFloat a => a -> (Integer,Int)
ghci> :t encodeFloat returns encodeFloat :: RealFloat a => Integer -> Int -> a
ghci> :t exponent returns exponent :: RealFloat a => a -> Int
ghci> :t significand returns significand :: RealFloat a => a -> a
ghci> :t scaleFloat returns scaleFloat :: RealFloat a => Int -> a -> a
ghci> :t isNaN returns isNaN :: RealFloat a => a -> Bool
ghci> :t isInfinite returns isInfinite :: RealFloat a => a -> Bool
ghci> :t isDenormalized returns isDenormalized :: RealFloat a => a -> Bool
ghci> :t isNegativeZero returns isNegativeZero :: RealFloat a => a -> Bool
ghci> :t isIEEE returns isIEEE :: RealFloat a => a -> Bool
ghci> :t atan2 returns atan2 :: RealFloat a => a -> a -> a
 
Types
Bool -  Logic value (true/false).
Subclasses Eq, Enum, Show, Read, Ord, Bounded
Char -  Unicode value occupying 2 bytes.
Subclasses Eq, Enum, Show, Read, Ord, Bounded
Int -  Two's complement integer value typically occupying 4 bytes.
Subclasses Eq, Enum, Num, Show, Read, Real, Ord, Integral, Bounded
Integer -  Integer of unlimited size.
Subclasses Eq, Enum, Num, Show, Read, Real, Ord, Integral
Float -  Real value limited to 32 bits (single precision).
Subclasses Eq, Enum, Num, Show, Read, Real, Ord, RealFloat, RealFrac, Fractional, Floating
Double -  Real value limited to 64 bits (double precision).
Subclasses Eq, Enum, Num, Show, Read, Real, Ord, RealFloat, RealFrac, Fractional, Floating
[Char] -  Array of characters of unlimited size.
[Int] -  List of integers of unlimited size.
[[Int]] -  List of lists of integers of varying size.
Ordering- A type that can be "less than", "greater than", or "equal to"
Subclasses Eq, Enum, Show, Read, Ord, Bounded