@@ -251,7 +251,9 @@ deriveEq mn ds tyConNm = do
251251 preludeEq = App . App (Var (Qualified (Just (ModuleName [ProperName C. prelude])) (Ident C. eq)))
252252
253253 addCatch :: [CaseAlternative ] -> [CaseAlternative ]
254- addCatch = (++ [catchAll])
254+ addCatch xs
255+ | length xs /= 1 = xs ++ [catchAll]
256+ | otherwise = xs -- Avoid redundant case
255257 where
256258 catchAll = CaseAlternative [NullBinder , NullBinder ] (Right (BooleanLiteral False ))
257259
@@ -290,7 +292,7 @@ deriveOrd mn ds tyConNm = do
290292 mkCompareFunction (DataDeclaration _ _ _ args) = do
291293 x <- freshIdent " x"
292294 y <- freshIdent " y"
293- lamCase2 x y <$> (concat <$> mapM mkCtorClauses (splitLast args))
295+ lamCase2 x y <$> (addCatch . concat <$> mapM mkCtorClauses (splitLast args))
294296 mkCompareFunction (PositionedDeclaration _ _ d) = mkCompareFunction d
295297 mkCompareFunction _ = internalError " mkCompareFunction: expected DataDeclaration"
296298
@@ -299,6 +301,13 @@ deriveOrd mn ds tyConNm = do
299301 splitLast [x] = [(x, True )]
300302 splitLast (x : xs) = (x, False ) : splitLast xs
301303
304+ addCatch :: [CaseAlternative ] -> [CaseAlternative ]
305+ addCatch xs
306+ | null xs = [catchAll] -- No type constructors
307+ | otherwise = xs
308+ where
309+ catchAll = CaseAlternative [NullBinder , NullBinder ] (Right (preludeCtor " EQ" ))
310+
302311 preludeCtor :: String -> Expr
303312 preludeCtor = Constructor . Qualified (Just (ModuleName [ProperName C. prelude])) . ProperName
304313
0 commit comments