-
Notifications
You must be signed in to change notification settings - Fork 574
Eq1 / Ord1 deriving again #3207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4e75c67
Allow `Eq1` to be used when deriving `Eq`
garyb d4da7da
Derive `Eq1` (as `eq1 = eq`)
garyb d1e43fc
Allow `Ord1` to be used when deriving `Ord`
garyb 60198af
Derive `Ord1` (as `compare1 = compare`)
garyb 3bebc61
Remove unnecessary constraints
garyb de9e5ac
Update DerivingFunctor test for Eq1 deriving
garyb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| module Main where | ||
|
|
||
| import Prelude | ||
| import Data.Eq (class Eq1) | ||
| import Control.Monad.Eff.Console (log) | ||
|
|
||
| data Product a b = Product a b | ||
|
|
||
| derive instance eqMu :: (Eq a, Eq b) => Eq (Product a b) | ||
| derive instance eq1Mu :: Eq a => Eq1 (Product a) | ||
|
|
||
| main = log "Done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| module Main where | ||
|
|
||
| import Prelude | ||
| import Data.Eq (class Eq1) | ||
| import Control.Monad.Eff.Console (log) | ||
|
|
||
| newtype Mu f = In (f (Mu f)) | ||
|
|
||
| derive instance eqMu :: Eq1 f => Eq (Mu f) | ||
|
|
||
| main = log "Done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| module Main where | ||
|
|
||
| import Prelude | ||
| import Data.Eq (class Eq1) | ||
| import Data.Ord (class Ord1) | ||
| import Control.Monad.Eff.Console (log) | ||
|
|
||
| data Product a b = Product a b | ||
|
|
||
| derive instance eqMu :: (Eq a, Eq b) => Eq (Product a b) | ||
| derive instance eq1Mu :: Eq a => Eq1 (Product a) | ||
|
|
||
| derive instance ordMu :: (Ord a, Ord b) => Ord (Product a b) | ||
| derive instance ord1Mu :: Ord a => Ord1 (Product a) | ||
|
|
||
| main = log "Done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module Main where | ||
|
|
||
| import Prelude | ||
| import Data.Eq (class Eq1) | ||
| import Data.Ord (class Ord1) | ||
| import Control.Monad.Eff.Console (log) | ||
|
|
||
| newtype Mu f = In (f (Mu f)) | ||
|
|
||
| derive instance eqMu :: Eq1 f => Eq (Mu f) | ||
| derive instance ordMu :: Ord1 f => Ord (Mu f) | ||
|
|
||
| main = log "Done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -121,13 +121,27 @@ deriveInstance mn syns _ ds (TypeInstanceDeclaration sa@(ss, _) ch idx nm deps c | |
| -> TypeInstanceDeclaration sa ch idx nm deps className tys . ExplicitInstance <$> deriveEq ss mn syns ds tyCon | ||
| | otherwise -> throwError . errorMessage' ss $ ExpectedTypeConstructor className tys ty | ||
| _ -> throwError . errorMessage' ss $ InvalidDerivedInstance className tys 1 | ||
| | className == Qualified (Just dataEq) (ProperName "Eq1") | ||
| = case tys of | ||
| [ty] | Just (Qualified mn' _, _) <- unwrapTypeConstructor ty | ||
| , mn == fromMaybe mn mn' | ||
| -> pure . TypeInstanceDeclaration sa ch idx nm deps className tys . ExplicitInstance $ deriveEq1 ss | ||
| | otherwise -> throwError . errorMessage' ss $ ExpectedTypeConstructor className tys ty | ||
| _ -> throwError . errorMessage' ss $ InvalidDerivedInstance className tys 1 | ||
| | className == Qualified (Just dataOrd) (ProperName "Ord") | ||
| = case tys of | ||
| [ty] | Just (Qualified mn' tyCon, _) <- unwrapTypeConstructor ty | ||
| , mn == fromMaybe mn mn' | ||
| -> TypeInstanceDeclaration sa ch idx nm deps className tys . ExplicitInstance <$> deriveOrd ss mn syns ds tyCon | ||
| | otherwise -> throwError . errorMessage' ss $ ExpectedTypeConstructor className tys ty | ||
| _ -> throwError . errorMessage' ss $ InvalidDerivedInstance className tys 1 | ||
| | className == Qualified (Just dataOrd) (ProperName "Ord1") | ||
| = case tys of | ||
| [ty] | Just (Qualified mn' _, _) <- unwrapTypeConstructor ty | ||
| , mn == fromMaybe mn mn' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above |
||
| -> pure . TypeInstanceDeclaration sa ch idx nm deps className tys . ExplicitInstance $ deriveOrd1 ss | ||
| | otherwise -> throwError . errorMessage' ss $ ExpectedTypeConstructor className tys ty | ||
| _ -> throwError . errorMessage' ss $ InvalidDerivedInstance className tys 1 | ||
| | className == Qualified (Just dataFunctor) (ProperName "Functor") | ||
| = case tys of | ||
| [ty] | Just (Qualified mn' tyCon, _) <- unwrapTypeConstructor ty | ||
|
|
@@ -436,6 +450,9 @@ deriveEq ss mn syns ds tyConNm = do | |
| preludeEq :: Expr -> Expr -> Expr | ||
| preludeEq = App . App (Var (Qualified (Just dataEq) (Ident C.eq))) | ||
|
|
||
| preludeEq1 :: Expr -> Expr -> Expr | ||
| preludeEq1 = App . App (Var (Qualified (Just dataEq) (Ident C.eq1))) | ||
|
|
||
| addCatch :: [CaseAlternative] -> [CaseAlternative] | ||
| addCatch xs | ||
| | length xs /= 1 = xs ++ [catchAll] | ||
|
|
@@ -458,12 +475,21 @@ deriveEq ss mn syns ds tyConNm = do | |
| conjAll xs = foldl1 preludeConj xs | ||
|
|
||
| toEqTest :: Expr -> Expr -> Type -> Expr | ||
| toEqTest l r ty | Just rec <- objectType ty | ||
| , Just fields <- decomposeRec rec = | ||
| conjAll | ||
| . map (\((Label str), typ) -> toEqTest (Accessor str l) (Accessor str r) typ) | ||
| $ fields | ||
| toEqTest l r _ = preludeEq l r | ||
| toEqTest l r ty | ||
| | Just rec <- objectType ty | ||
| , Just fields <- decomposeRec rec = | ||
| conjAll | ||
| . map (\((Label str), typ) -> toEqTest (Accessor str l) (Accessor str r) typ) | ||
| $ fields | ||
| | isAppliedVar ty = preludeEq1 l r | ||
| | otherwise = preludeEq l r | ||
|
|
||
| deriveEq1 :: SourceSpan -> [Declaration] | ||
| deriveEq1 ss = | ||
| [ ValueDecl (ss, []) (Ident C.eq1) Public [] (unguarded preludeEq)] | ||
| where | ||
| preludeEq :: Expr | ||
| preludeEq = Var (Qualified (Just dataEq) (Ident C.eq)) | ||
|
|
||
| deriveOrd | ||
| :: forall m | ||
|
|
@@ -510,6 +536,9 @@ deriveOrd ss mn syns ds tyConNm = do | |
| ordCompare :: Expr -> Expr -> Expr | ||
| ordCompare = App . App (Var (Qualified (Just dataOrd) (Ident C.compare))) | ||
|
|
||
| ordCompare1 :: Expr -> Expr -> Expr | ||
| ordCompare1 = App . App (Var (Qualified (Just dataOrd) (Ident C.compare1))) | ||
|
|
||
| mkCtorClauses :: ((ProperName 'ConstructorName, [Type]), Bool) -> m [CaseAlternative] | ||
| mkCtorClauses ((ctorName, tys), isLast) = do | ||
| identsL <- replicateM (length tys) (freshIdent "l") | ||
|
|
@@ -547,12 +576,21 @@ deriveOrd ss mn syns ds tyConNm = do | |
| ] | ||
|
|
||
| toOrdering :: Expr -> Expr -> Type -> Expr | ||
| toOrdering l r ty | Just rec <- objectType ty | ||
| , Just fields <- decomposeRec rec = | ||
| appendAll | ||
| . map (\((Label str), typ) -> toOrdering (Accessor str l) (Accessor str r) typ) | ||
| $ fields | ||
| toOrdering l r _ = ordCompare l r | ||
| toOrdering l r ty | ||
| | Just rec <- objectType ty | ||
| , Just fields <- decomposeRec rec = | ||
| appendAll | ||
| . map (\((Label str), typ) -> toOrdering (Accessor str l) (Accessor str r) typ) | ||
| $ fields | ||
| | isAppliedVar ty = ordCompare1 l r | ||
| | otherwise = ordCompare l r | ||
|
|
||
| deriveOrd1 :: SourceSpan -> [Declaration] | ||
| deriveOrd1 ss = | ||
| [ ValueDecl (ss, []) (Ident C.compare1) Public [] (unguarded dataOrdCompare)] | ||
| where | ||
| dataOrdCompare :: Expr | ||
| dataOrdCompare = Var (Qualified (Just dataOrd) (Ident C.compare)) | ||
|
|
||
| deriveNewtype | ||
| :: forall m | ||
|
|
@@ -617,6 +655,10 @@ mkVarMn mn = Var . Qualified mn | |
| mkVar :: Ident -> Expr | ||
| mkVar = mkVarMn Nothing | ||
|
|
||
| isAppliedVar :: Type -> Bool | ||
| isAppliedVar (TypeApp (TypeVar _) _) = True | ||
| isAppliedVar _ = False | ||
|
|
||
| objectType :: Type -> Maybe Type | ||
| objectType (TypeApp (TypeConstructor (Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Record"))) rec) = Just rec | ||
| objectType _ = Nothing | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe True (== mn) mn'?I was a little confused there for a second :D There's also the more obscure
all (== mn) mn'but I think that's too cute :DThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just copied exactly the form of the other matches here. There's some other cleaning up I'd like to do actually, but didn't want to mix it in this PR 😉 so I can change it, but maybe it'd be better to keep them consistent for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should've looked at the whole file 🤦♂️