Haskell Type System: Type Inference, Type Classes, and Polymorphism Quiz Quiz

Explore the fundamentals of Haskell’s type system with questions on type inference, type classes, and polymorphism. This quiz helps you solidify your understanding of how Haskell manages types, constraints, and generalized functions in functional programming.

  1. Basic Type Inference

    Given the Haskell expression 'x = 5 + 3', what type will the compiler infer for x?

    1. Char
    2. String
    3. Int
    4. Bool

    Explanation: The expression '5 + 3' involves integer addition, so the compiler infers the type as Int. String, Bool, and Char are not compatible with numeric operations in this context, making them incorrect.

  2. Type Class Concept

    Which Haskell keyword is used to define a type class?

    1. module
    2. derive
    3. data
    4. class

    Explanation: The keyword 'class' is used to define a type class in Haskell, such as 'class Eq a where ...'. The others are used for data type declarations, deriving instances, or organizing code and have different purposes.

  3. Function Polymorphism

    In the function 'identity x = x', what is the inferred type of identity?

    1. a -u003E a
    2. String -u003E Int
    3. Bool -u003E String
    4. Int -u003E Int

    Explanation: The function 'identity' returns any type it is given, making it polymorphic; its type is 'a -u003E a'. 'Int -u003E Int', 'String -u003E Int', and 'Bool -u003E String' are specific types, missing the generalization shown here.

  4. Type Class Instance

    If you want to allow a custom data type to use the '==' operator, which type class should it be an instance of?

    1. Show
    2. Ord
    3. Read
    4. Eq

    Explanation: 'Eq' is the type class responsible for equality operations like '=='. 'Ord' is for ordering, 'Show' for string representation, and 'Read' for parsing, making them less suitable here.

  5. Type Annotation Usage

    Why might you specify a type annotation in Haskell even though the compiler infers types?

    1. To speed up program execution
    2. To reduce file size
    3. For code clarity and to guide inference
    4. To avoid importing modules

    Explanation: Type annotations improve code readability and can help the compiler resolve ambiguous types. They do not affect execution speed, file size, or module importing; those options are incorrect.

  6. Polymorphic Function Example

    Which of these functions is polymorphic in Haskell?

    1. sum :: [Int] -u003E Int
    2. head :: [a] -u003E a
    3. not :: Bool -u003E Bool
    4. length :: [Char] -u003E Int

    Explanation: 'head' works with a list of any type, making it polymorphic. 'sum', 'not', and 'length' operate on specific types—integers, booleans, or characters—so they lack this generality.

  7. Type Constraint in Type Class

    In the type signature 'f :: (Eq a) =u003E a -u003E Bool', what does '(Eq a) =u003E' represent?

    1. A function composition
    2. A module import
    3. A type constraint requiring 'a' to be an instance of Eq
    4. A type synonym

    Explanation: '(Eq a) =u003E' means the type variable 'a' must be an instance of the 'Eq' type class. It is not related to function composition, module imports, or type synonyms, so those are incorrect.

  8. Deriving Instances

    What is achieved by writing 'data Color = Red | Blue deriving (Show)' in Haskell?

    1. Imports the 'Show' module
    2. Automatic generation of 'Show' instance for 'Color'
    3. Restricts 'Color' to only two values
    4. Evaluates 'Color' expressions at compile time

    Explanation: 'deriving (Show)' creates default implementations for displaying 'Color'. It does not import modules, restrict possible values, or affect compile-time evaluation; those interpretations are incorrect.

  9. Type Inference with Lists

    What will be the inferred type of the expression '[True, False, True]' in Haskell?

    1. [Char]
    2. [Int]
    3. [String]
    4. [Bool]

    Explanation: All elements are boolean values, so Haskell infers the type as '[Bool]'. '[Int]', '[Char]', and '[String]' do not match the boolean elements in the list, making those options incorrect.

  10. Type Class and Overloading

    How do type classes enable overloading of functions like '+' in Haskell?

    1. By using explicit casting
    2. By combining modules
    3. By enforcing static typing only
    4. By allowing multiple types to implement the same function name

    Explanation: Type classes let different types define their own implementation of shared functions such as '+'. Modules manage organization, casting changes specific values, and static typing is about type checks rather than overloading.