Function that behaves differently depending on which type classes the argument belong to











up vote
1
down vote

favorite












In Haskell, is there any way to declare function it behaves differently depending on whether the type of a argument is an instance of specific type class?
For example, can I define genericShow in the following example?



-- If type `a` is an instance of `Show`.
genericShow :: Show a => a -> String
genericShow = show
-- If type `a` is not an instance of `Show`.
genericShow :: a -> String
genericShow _ = "(Cannot be shown)"

> genericShow 3
"3"
> genericShow const
"(Cannot be shown)"









share|improve this question


















  • 6




    (1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
    – duplode
    Nov 19 at 2:35












  • It helps me. Thanks a lot!
    – user3749167
    Nov 19 at 9:27















up vote
1
down vote

favorite












In Haskell, is there any way to declare function it behaves differently depending on whether the type of a argument is an instance of specific type class?
For example, can I define genericShow in the following example?



-- If type `a` is an instance of `Show`.
genericShow :: Show a => a -> String
genericShow = show
-- If type `a` is not an instance of `Show`.
genericShow :: a -> String
genericShow _ = "(Cannot be shown)"

> genericShow 3
"3"
> genericShow const
"(Cannot be shown)"









share|improve this question


















  • 6




    (1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
    – duplode
    Nov 19 at 2:35












  • It helps me. Thanks a lot!
    – user3749167
    Nov 19 at 9:27













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In Haskell, is there any way to declare function it behaves differently depending on whether the type of a argument is an instance of specific type class?
For example, can I define genericShow in the following example?



-- If type `a` is an instance of `Show`.
genericShow :: Show a => a -> String
genericShow = show
-- If type `a` is not an instance of `Show`.
genericShow :: a -> String
genericShow _ = "(Cannot be shown)"

> genericShow 3
"3"
> genericShow const
"(Cannot be shown)"









share|improve this question













In Haskell, is there any way to declare function it behaves differently depending on whether the type of a argument is an instance of specific type class?
For example, can I define genericShow in the following example?



-- If type `a` is an instance of `Show`.
genericShow :: Show a => a -> String
genericShow = show
-- If type `a` is not an instance of `Show`.
genericShow :: a -> String
genericShow _ = "(Cannot be shown)"

> genericShow 3
"3"
> genericShow const
"(Cannot be shown)"






haskell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 1:01









user3749167

616




616








  • 6




    (1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
    – duplode
    Nov 19 at 2:35












  • It helps me. Thanks a lot!
    – user3749167
    Nov 19 at 9:27














  • 6




    (1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
    – duplode
    Nov 19 at 2:35












  • It helps me. Thanks a lot!
    – user3749167
    Nov 19 at 9:27








6




6




(1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
– duplode
Nov 19 at 2:35






(1) Closely related: Check whether a type is an instance of Show in Haskell?; Use specialized implementation if a class instance is available. I'm not closing against those now because I'm not sure about which kind of answer would best fit here. (2) In the context of those two questions, the constraints-emerge package is also worth a shout. (3) In any case, as those discussions suggest, this isn't something to be undertaken lightly.
– duplode
Nov 19 at 2:35














It helps me. Thanks a lot!
– user3749167
Nov 19 at 9:27




It helps me. Thanks a lot!
– user3749167
Nov 19 at 9:27












1 Answer
1






active

oldest

votes

















up vote
1
down vote













No.



The closest you can get is to use Overlapping instances, with a catch-all instance for anything not having a more specific Show instance.



instance {-# OVERLAPPABLE #-} Show a  where
show _ = "(Cannot be shown)"


Overlapping instances come with lots of caveats: see topics like 'orphan instances', 'Incoherent instances'. That's particularly awkward with Prelude classes like Show, because there's likely to be lots of instances hidden away in libraries.



As @duplode says, there are many dangers. Almost certainly there's a better way to achieve whatever it is you think you want.






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',
    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%2f53367056%2ffunction-that-behaves-differently-depending-on-which-type-classes-the-argument-b%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








    up vote
    1
    down vote













    No.



    The closest you can get is to use Overlapping instances, with a catch-all instance for anything not having a more specific Show instance.



    instance {-# OVERLAPPABLE #-} Show a  where
    show _ = "(Cannot be shown)"


    Overlapping instances come with lots of caveats: see topics like 'orphan instances', 'Incoherent instances'. That's particularly awkward with Prelude classes like Show, because there's likely to be lots of instances hidden away in libraries.



    As @duplode says, there are many dangers. Almost certainly there's a better way to achieve whatever it is you think you want.






    share|improve this answer

























      up vote
      1
      down vote













      No.



      The closest you can get is to use Overlapping instances, with a catch-all instance for anything not having a more specific Show instance.



      instance {-# OVERLAPPABLE #-} Show a  where
      show _ = "(Cannot be shown)"


      Overlapping instances come with lots of caveats: see topics like 'orphan instances', 'Incoherent instances'. That's particularly awkward with Prelude classes like Show, because there's likely to be lots of instances hidden away in libraries.



      As @duplode says, there are many dangers. Almost certainly there's a better way to achieve whatever it is you think you want.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        No.



        The closest you can get is to use Overlapping instances, with a catch-all instance for anything not having a more specific Show instance.



        instance {-# OVERLAPPABLE #-} Show a  where
        show _ = "(Cannot be shown)"


        Overlapping instances come with lots of caveats: see topics like 'orphan instances', 'Incoherent instances'. That's particularly awkward with Prelude classes like Show, because there's likely to be lots of instances hidden away in libraries.



        As @duplode says, there are many dangers. Almost certainly there's a better way to achieve whatever it is you think you want.






        share|improve this answer












        No.



        The closest you can get is to use Overlapping instances, with a catch-all instance for anything not having a more specific Show instance.



        instance {-# OVERLAPPABLE #-} Show a  where
        show _ = "(Cannot be shown)"


        Overlapping instances come with lots of caveats: see topics like 'orphan instances', 'Incoherent instances'. That's particularly awkward with Prelude classes like Show, because there's likely to be lots of instances hidden away in libraries.



        As @duplode says, there are many dangers. Almost certainly there's a better way to achieve whatever it is you think you want.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 8:56









        AntC

        542210




        542210






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367056%2ffunction-that-behaves-differently-depending-on-which-type-classes-the-argument-b%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

            Costa Masnaga

            Fotorealismo

            Sidney Franklin