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;
}







0















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?










share|improve this question


















  • 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


















0















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?










share|improve this question


















  • 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 2:27









        dfeuerdfeuer

        33.8k349133




        33.8k349133
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen