Does GHC do any trickery with equality? - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Pedro Minicz

Let T be a type with the default derived Eq instance and x and y elements of type T. If x == y evaluates to True did GHC fully evaluate both types or could GHC have taken some short cut such as comparing if they are the same pointer before comparing structural equality?

data T = ... deriving Eq

x, y :: T
x = ...
y = ...

main :: IO ()
main = print $ x == y

In particular, if main above prints True, does that mean that x and y are both structurally equal and not infinite (and not bottom)?

Georgi Lyubenov // googleson78

It depends on what types T contains

Georgi Lyubenov // googleson78

This statement is not true for Data.Void for example:

> (undefined :: Void) == undefined
True
Georgi Lyubenov // googleson78
> :set -XEmptyDataDeriving
> data Bla deriving Eq
> (undefined :: Bla) == undefined
True
Georgi Lyubenov // googleson78

so maybe it's true if you require

  • all types "contained" in T have a default deriving
  • T doesn't have any Voids in it (or alternatively, rephrase the question to only be about non-bottom values)
Pedro Minicz

The type I was thinking when I asked the question was this one:

import Data.Text (Text)

type Name = (Text, Maybe Int)

data Expr
  = Var Name
  | Sort Int
  | Pi Abstraction
  | Lam Abstraction
  | App Expr Expr
  | Annotation Expr Expr
  deriving Eq

type Abstraction = (Maybe Name, Expr, Expr)

I think the answer should be yes then, because it fulfills the required criteria.