@@ -251,7 +251,9 @@ deriveEq mn ds tyConNm = do
251 preludeEq = App . App (Var (Qualified (Just (ModuleName [ProperName C.prelude])) (Ident C.eq)))
251 preludeEq = App . App (Var (Qualified (Just (ModuleName [ProperName C.prelude])) (Ident C.eq)))
253 addCatch :: [CaseAlternative] -> [CaseAlternative]
253 addCatch :: [CaseAlternative] -> [CaseAlternative]
254− addCatch = (++ [catchAll])
254+ addCatch xs
255+ | length xs /= 1 = xs ++ [catchAll]
256+ | otherwise = xs -- Avoid redundant case
256 catchAll = CaseAlternative [NullBinder, NullBinder] (Right (BooleanLiteral False))
258 catchAll = CaseAlternative [NullBinder, NullBinder] (Right (BooleanLiteral False))
@@ -290,7 +292,7 @@ deriveOrd mn ds tyConNm = do
290 mkCompareFunction (DataDeclaration _ _ _ args) = do
292 mkCompareFunction (DataDeclaration _ _ _ args) = do
291 x <- freshIdent "x"
293 x <- freshIdent "x"
292 y <- freshIdent "y"
294 y <- freshIdent "y"
293− lamCase2 x y <$> (concat <$> mapM mkCtorClauses (splitLast args))
295+ lamCase2 x y <$> (addCatch . concat <$> mapM mkCtorClauses (splitLast args))
294 mkCompareFunction (PositionedDeclaration _ _ d) = mkCompareFunction d
296 mkCompareFunction (PositionedDeclaration _ _ d) = mkCompareFunction d
295 mkCompareFunction _ = internalError "mkCompareFunction: expected DataDeclaration"
297 mkCompareFunction _ = internalError "mkCompareFunction: expected DataDeclaration"
@@ -299,6 +301,13 @@ deriveOrd mn ds tyConNm = do
299 splitLast [x] = [(x, True)]
301 splitLast [x] = [(x, True)]
300 splitLast (x : xs) = (x, False) : splitLast xs
302 splitLast (x : xs) = (x, False) : splitLast xs
304+ addCatch :: [CaseAlternative] -> [CaseAlternative]
306+ | null xs = [catchAll] -- No type constructors
309+ catchAll = CaseAlternative [NullBinder, NullBinder] (Right (preludeCtor "EQ"))
302 preludeCtor :: String -> Expr
311 preludeCtor :: String -> Expr
303 preludeCtor = Constructor . Qualified (Just (ModuleName [ProperName C.prelude])) . ProperName
312 preludeCtor = Constructor . Qualified (Just (ModuleName [ProperName C.prelude])) . ProperName