Skip to content

Commit 00e53cb

Browse files
committed
Comments and adjustments for GCompare instances
1 parent ad7bc0e commit 00e53cb

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lsp-types/src/Language/LSP/Types/Method.hs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,38 @@ data SMethod (m :: Method f t) where
188188
SCancelRequest :: SMethod CancelRequest
189189
SCustomMethod :: Text -> SMethod CustomMethod
190190

191+
-- This instance is written manually rather than derived to avoid a dependency
192+
-- on 'dependent-sum-template'.
191193
instance GEq SMethod where
192194
geq x y = case gcompare x y of
193195
GLT -> Nothing
194196
GEQ -> Just Refl
195197
GGT -> Nothing
196198

199+
-- This instance is written manually rather than derived to avoid a dependency
200+
-- on 'dependent-sum-template'.
197201
instance GCompare SMethod where
198202
gcompare (SCustomMethod x) (SCustomMethod y) = case x `compare` y of
199203
LT -> GLT
200204
EQ -> GEQ
201205
GT -> GGT
206+
-- This is much more compact than matching on every pair of constructors, which
207+
-- is what we would need to do for GHC to 'see' that this is correct. Nonetheless
208+
-- it is safe: since there is only one constructor of 'SMethod' for each 'Method',
209+
-- the argument types can only be equal if the constructor tag is equal.
202210
gcompare x y = case I# (dataToTag# x) `compare` I# (dataToTag# y) of
203211
LT -> GLT
204212
EQ -> unsafeCoerce GEQ
205213
GT -> GGT
206214

207-
deriving instance Eq (SMethod m)
208-
deriving instance Ord (SMethod m)
215+
instance Eq (SMethod m) where
216+
-- This defers to 'GEq', ensuring that this version is compatible.
217+
(==) = defaultEq
218+
219+
instance Ord (SMethod m) where
220+
-- This defers to 'GCompare', ensuring that this version is compatible.
221+
compare = defaultCompare
222+
209223
deriving instance Show (SMethod m)
210224

211225
-- Some useful type synonyms

0 commit comments

Comments
 (0)