Way to have a common function in an instance declaration?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to make the following code work:
instance (Integral n, OffsetCalculator o, HexDirection d) => (IsoEvidence (Axial d n) (Offset d o n)) where
po :: Proxy o
po = Proxy
pd :: Proxy d
pd = Proxy
adj :: Vector2D n -> n
adj p = adjustment po $ p ^. otherAxis pd
convert :: (n -> n -> n) -> Vector2D n -> Vector2D n
convert f p = (offsetAxis pd) %~ (flip f (adj p)) $ p
convertFrom (Offset p) = Axial $ convert (P.-) p
convertTo (Axial p) = Offset $ convert (P.+) p
I'm converting this from some previously compiling code, so I'm relatively confident the code is conceptually fine. My problem is that convertFrom and convertTo are the only exposed methods of the IsoEvidence class. Therefore, the rest of this doesn't compile.
If I move "convert" out of the instance declaration, it starts to need Proxy parameters itself which makes the code uglier. Is there a way to make what I'm trying to do work?
haskell typeclass
add a comment |
I'm trying to make the following code work:
instance (Integral n, OffsetCalculator o, HexDirection d) => (IsoEvidence (Axial d n) (Offset d o n)) where
po :: Proxy o
po = Proxy
pd :: Proxy d
pd = Proxy
adj :: Vector2D n -> n
adj p = adjustment po $ p ^. otherAxis pd
convert :: (n -> n -> n) -> Vector2D n -> Vector2D n
convert f p = (offsetAxis pd) %~ (flip f (adj p)) $ p
convertFrom (Offset p) = Axial $ convert (P.-) p
convertTo (Axial p) = Offset $ convert (P.+) p
I'm converting this from some previously compiling code, so I'm relatively confident the code is conceptually fine. My problem is that convertFrom and convertTo are the only exposed methods of the IsoEvidence class. Therefore, the rest of this doesn't compile.
If I move "convert" out of the instance declaration, it starts to need Proxy parameters itself which makes the code uglier. Is there a way to make what I'm trying to do work?
haskell typeclass
2
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
2
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37
add a comment |
I'm trying to make the following code work:
instance (Integral n, OffsetCalculator o, HexDirection d) => (IsoEvidence (Axial d n) (Offset d o n)) where
po :: Proxy o
po = Proxy
pd :: Proxy d
pd = Proxy
adj :: Vector2D n -> n
adj p = adjustment po $ p ^. otherAxis pd
convert :: (n -> n -> n) -> Vector2D n -> Vector2D n
convert f p = (offsetAxis pd) %~ (flip f (adj p)) $ p
convertFrom (Offset p) = Axial $ convert (P.-) p
convertTo (Axial p) = Offset $ convert (P.+) p
I'm converting this from some previously compiling code, so I'm relatively confident the code is conceptually fine. My problem is that convertFrom and convertTo are the only exposed methods of the IsoEvidence class. Therefore, the rest of this doesn't compile.
If I move "convert" out of the instance declaration, it starts to need Proxy parameters itself which makes the code uglier. Is there a way to make what I'm trying to do work?
haskell typeclass
I'm trying to make the following code work:
instance (Integral n, OffsetCalculator o, HexDirection d) => (IsoEvidence (Axial d n) (Offset d o n)) where
po :: Proxy o
po = Proxy
pd :: Proxy d
pd = Proxy
adj :: Vector2D n -> n
adj p = adjustment po $ p ^. otherAxis pd
convert :: (n -> n -> n) -> Vector2D n -> Vector2D n
convert f p = (offsetAxis pd) %~ (flip f (adj p)) $ p
convertFrom (Offset p) = Axial $ convert (P.-) p
convertTo (Axial p) = Offset $ convert (P.+) p
I'm converting this from some previously compiling code, so I'm relatively confident the code is conceptually fine. My problem is that convertFrom and convertTo are the only exposed methods of the IsoEvidence class. Therefore, the rest of this doesn't compile.
If I move "convert" out of the instance declaration, it starts to need Proxy parameters itself which makes the code uglier. Is there a way to make what I'm trying to do work?
haskell typeclass
haskell typeclass
asked Nov 26 '18 at 22:43
Julian BirchJulian Birch
1,480923
1,480923
2
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
2
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37
add a comment |
2
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
2
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37
2
2
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
2
2
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37
add a comment |
1 Answer
1
active
oldest
votes
Haskell doesn't allow local definitions in instance declaration where
clauses. But suppose, hypothetically, that it did. Then what you're trying to do still wouldn't work. The compiler has no way to know how to instantiate the type variables in the calls to convert
. You seem to want something like implicit parameters at the type level, and that sounds like an absolute nightmare to me. If proxy passing isn't to your taste, you can use TypeApplications
instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490241%2fway-to-have-a-common-function-in-an-instance-declaration%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Haskell doesn't allow local definitions in instance declaration where
clauses. But suppose, hypothetically, that it did. Then what you're trying to do still wouldn't work. The compiler has no way to know how to instantiate the type variables in the calls to convert
. You seem to want something like implicit parameters at the type level, and that sounds like an absolute nightmare to me. If proxy passing isn't to your taste, you can use TypeApplications
instead.
add a comment |
Haskell doesn't allow local definitions in instance declaration where
clauses. But suppose, hypothetically, that it did. Then what you're trying to do still wouldn't work. The compiler has no way to know how to instantiate the type variables in the calls to convert
. You seem to want something like implicit parameters at the type level, and that sounds like an absolute nightmare to me. If proxy passing isn't to your taste, you can use TypeApplications
instead.
add a comment |
Haskell doesn't allow local definitions in instance declaration where
clauses. But suppose, hypothetically, that it did. Then what you're trying to do still wouldn't work. The compiler has no way to know how to instantiate the type variables in the calls to convert
. You seem to want something like implicit parameters at the type level, and that sounds like an absolute nightmare to me. If proxy passing isn't to your taste, you can use TypeApplications
instead.
Haskell doesn't allow local definitions in instance declaration where
clauses. But suppose, hypothetically, that it did. Then what you're trying to do still wouldn't work. The compiler has no way to know how to instantiate the type variables in the calls to convert
. You seem to want something like implicit parameters at the type level, and that sounds like an absolute nightmare to me. If proxy passing isn't to your taste, you can use TypeApplications
instead.
answered Nov 27 '18 at 2:27
dfeuerdfeuer
33.8k349133
33.8k349133
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490241%2fway-to-have-a-common-function-in-an-instance-declaration%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Aren't you on SO long enough to know to post proper Minimal, Complete, and Verifiable examples?
– leftaroundabout
Nov 26 '18 at 23:18
No, there isn't a way to make this work, although it's maybe an interesting idea.
– jberryman
Nov 26 '18 at 23:43
2
@leftaroundabout It's not necessary to condescend with "you should know better". A comment like "Please include an MCVE" conveys the same message without putting the question-asker on the defensive. Or, simply use a close vote with the MCVE reason.
– amalloy
Nov 27 '18 at 0:59
Sorry, it's quite harder to get smaller without losing the motivation for the question.
– Julian Birch
Nov 27 '18 at 21:37