Home > Mobile >  Source code definition of `==` for lists in Haskell
Source code definition of `==` for lists in Haskell

Time:06-05

Does anyone know how exactly == is defined for lists in Haskell? I've tried Hoogling it but can't seem to find where the instance Eq [a] is defined.

CodePudding user response:

The GHC implementation of Eq and other built-in instances is written in GHC.Classes. Specifically, you're looking for Eq [a]

instance (Eq a) => Eq [a] where
    {-# SPECIALISE instance Eq [[Char]] #-}
    {-# SPECIALISE instance Eq [Char] #-}
    {-# SPECIALISE instance Eq [Int] #-}
    []     == []     = True
    (x:xs) == (y:ys) = x == y && xs == ys
    _xs    == _ys    = False

Nothing super exciting. Two empty lists are equal, and two nonempty lists are equal if the heads and tails are equal. Finally, two arbitrary lists are non-equal. The only interesting part is the specialization directives, which should monomorphize equality checks on integer lists, strings, and lists of strings.

  • Related