git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
More tests, handle Void types
b4e5bf2Phil Freeman
examples/passing/Deriving.purs+6
@@ -3,6 +3,12 @@ module Main where
3import Prelude
3import Prelude
4import Test.Assert
4import Test.Assert
5
5
6+data V
7+
8+derive instance eqV :: Eq V
9+
10+derive instance ordV :: Ord V
11+
6data X = X Int | Y String
12data X = X Int | Y String
7
13
8derive instance eqX :: Eq X
14derive instance eqX :: Eq X
src/Language/PureScript/Sugar/TypeClasses/Deriving.hs+11−2
@@ -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)))
252
252
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
255 where
257 where
256 catchAll = CaseAlternative [NullBinder, NullBinder] (Right (BooleanLiteral False))
258 catchAll = CaseAlternative [NullBinder, NullBinder] (Right (BooleanLiteral False))
257
259
@@ -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"
296
298
@@ -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
301
303
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+
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
304
313