From 67cf2e25aea39f9be8aab1c2269b1d3ebf6bfe3e Mon Sep 17 00:00:00 2001 From: Ian Lynagh Date: Mon, 23 Nov 2009 20:22:53 +0000 Subject: [PATCH] De-orphan the Eq/Ord Char instances --- GHC/Base.lhs | 14 -------------- GHC/Classes.hs | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/GHC/Base.lhs b/GHC/Base.lhs index c1db364..52d1252 100644 --- a/GHC/Base.lhs +++ b/GHC/Base.lhs @@ -468,20 +468,6 @@ by Unicode, use 'Prelude.toEnum' and 'Prelude.fromEnum' from the 'Prelude.Enum' class respectively (or equivalently 'ord' and 'chr'). -} --- We don't use deriving for Eq and Ord, because for Ord the derived --- instance defines only compare, which takes two primops. Then --- '>' uses compare, and therefore takes two primops instead of one. - -instance Eq Char where - (C# c1) == (C# c2) = c1 `eqChar#` c2 - (C# c1) /= (C# c2) = c1 `neChar#` c2 - -instance Ord Char where - (C# c1) > (C# c2) = c1 `gtChar#` c2 - (C# c1) >= (C# c2) = c1 `geChar#` c2 - (C# c1) <= (C# c2) = c1 `leChar#` c2 - (C# c1) < (C# c2) = c1 `ltChar#` c2 - {-# RULES "x# `eqChar#` x#" forall x#. x# `eqChar#` x# = True "x# `neChar#` x#" forall x#. x# `neChar#` x# = False diff --git a/GHC/Classes.hs b/GHC/Classes.hs index d1c18af..af810fb 100644 --- a/GHC/Classes.hs +++ b/GHC/Classes.hs @@ -21,6 +21,8 @@ import GHC.Bool -- GHC.Magic is used in some derived instances import GHC.Magic () import GHC.Ordering +import GHC.Prim +import GHC.Types infix 4 ==, /=, <, <=, >=, > infixr 3 && @@ -61,6 +63,10 @@ instance Eq Ordering where GT == GT = True _ == _ = False +instance Eq Char where + (C# c1) == (C# c2) = c1 `eqChar#` c2 + (C# c1) /= (C# c2) = c1 `neChar#` c2 + -- | The 'Ord' class is used for totally ordered datatypes. -- -- Instances of 'Ord' can be derived for any user-defined @@ -113,6 +119,15 @@ instance Ord Ordering where _ <= EQ = False GT <= GT = True +-- We don't use deriving for Ord Char, because for Ord the derived +-- instance defines only compare, which takes two primops. Then +-- '>' uses compare, and therefore takes two primops instead of one. +instance Ord Char where + (C# c1) > (C# c2) = c1 `gtChar#` c2 + (C# c1) >= (C# c2) = c1 `geChar#` c2 + (C# c1) <= (C# c2) = c1 `leChar#` c2 + (C# c1) < (C# c2) = c1 `ltChar#` c2 + -- OK, so they're technically not part of a class...: -- Boolean functions -- 1.7.10.4