Bidirectional Functional DependenciesLarge-scale design in Haskell?Functional Dependency in HaskellSpeed comparison with Project Euler: C vs Python vs Erlang vs HaskellHaskell functional dependency conflictAbusing the algebra of algebraic data types - why does this work?GHC code generation for type class function callsWhy not be dependently typed?Confused by the meaning of the 'Alternative' type class and its relationship to other type classesMultiParamTypeClasses, FunctionalDependencies, and calling ambiguous functionsWhy does my functional dependency conflict disappear when I expand the definition?

Bb13b9 confusion

Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?

Ability To Change Root User Password (Vulnerability?)

bash does not know the letter 'p'

A map of non-pathological topology?

Does the new finding on "reversing a quantum jump mid-flight" rule out any interpretations of QM?

What is the color of artificial intelligence?

Fermat's statement about the ancients: How serious was he?

Insert external file and modify each line from script

How can I remove material from this wood beam?

Can I utilise a baking stone to make crepes?

How to communicate to my GM that not being allowed to use stealth isn't fun for me?

How to publish items after pipeline is finished?

What is exactly Avijja -- and how to uproot it?

Which is the better way to call a method that is only available to one class that implements an interface but not the other one?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

I've been given a project I can't complete, what should I do?

Generate basis elements of the Steenrod algebra

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

How to make insert mode mapping count as multiple undos?

Is it possible to fly backward if you have REALLY STRONG headwind?

Why do American speakers pronounce "the" as "/ðə/" before vowels?

Non-aqueous eyes?

Why am I Seeing A Weird "Notch" on the Data Line For Some Logical 1s?



Bidirectional Functional Dependencies


Large-scale design in Haskell?Functional Dependency in HaskellSpeed comparison with Project Euler: C vs Python vs Erlang vs HaskellHaskell functional dependency conflictAbusing the algebra of algebraic data types - why does this work?GHC code generation for type class function callsWhy not be dependently typed?Confused by the meaning of the 'Alternative' type class and its relationship to other type classesMultiParamTypeClasses, FunctionalDependencies, and calling ambiguous functionsWhy does my functional dependency conflict disappear when I expand the definition?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








8















I have a type class that looks a bit like the following:



class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


Or at least these are the bits that are important to my question. This class does not compile, and for good reason. The problem with this class is that I could (if I wanted to) do the following:



instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False


Now if I call g True there are two separate results one for each instance. And the compiler picks up on this possibility and informs me that my type class is no good.



My issue is that the dependency | a -> b is not quite what I mean. I don't just mean that you can find a from b, but also that you can find b from a. That is each type should only ever be a member of Foo with one other type so we can given one type find the other. Or to put it in yet another way the dependency is bidirectional. Such a functional dependency would prevent me from having Bool present in two separate instances because the first parameter would be derivable from the second as well as the second from the first.



But I don't know how to express this idea to the compiler.



How can I create a bidirectional functional dependency? Or, more likely, is there a way that I can rephrase my type class to get something that could replace a bidirectional functional dependency?










share|improve this question
























  • You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

    – Willem Van Onsem
    Jun 1 at 21:33

















8















I have a type class that looks a bit like the following:



class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


Or at least these are the bits that are important to my question. This class does not compile, and for good reason. The problem with this class is that I could (if I wanted to) do the following:



instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False


Now if I call g True there are two separate results one for each instance. And the compiler picks up on this possibility and informs me that my type class is no good.



My issue is that the dependency | a -> b is not quite what I mean. I don't just mean that you can find a from b, but also that you can find b from a. That is each type should only ever be a member of Foo with one other type so we can given one type find the other. Or to put it in yet another way the dependency is bidirectional. Such a functional dependency would prevent me from having Bool present in two separate instances because the first parameter would be derivable from the second as well as the second from the first.



But I don't know how to express this idea to the compiler.



How can I create a bidirectional functional dependency? Or, more likely, is there a way that I can rephrase my type class to get something that could replace a bidirectional functional dependency?










share|improve this question
























  • You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

    – Willem Van Onsem
    Jun 1 at 21:33













8












8








8








I have a type class that looks a bit like the following:



class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


Or at least these are the bits that are important to my question. This class does not compile, and for good reason. The problem with this class is that I could (if I wanted to) do the following:



instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False


Now if I call g True there are two separate results one for each instance. And the compiler picks up on this possibility and informs me that my type class is no good.



My issue is that the dependency | a -> b is not quite what I mean. I don't just mean that you can find a from b, but also that you can find b from a. That is each type should only ever be a member of Foo with one other type so we can given one type find the other. Or to put it in yet another way the dependency is bidirectional. Such a functional dependency would prevent me from having Bool present in two separate instances because the first parameter would be derivable from the second as well as the second from the first.



But I don't know how to express this idea to the compiler.



How can I create a bidirectional functional dependency? Or, more likely, is there a way that I can rephrase my type class to get something that could replace a bidirectional functional dependency?










share|improve this question
















I have a type class that looks a bit like the following:



class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


Or at least these are the bits that are important to my question. This class does not compile, and for good reason. The problem with this class is that I could (if I wanted to) do the following:



instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False


Now if I call g True there are two separate results one for each instance. And the compiler picks up on this possibility and informs me that my type class is no good.



My issue is that the dependency | a -> b is not quite what I mean. I don't just mean that you can find a from b, but also that you can find b from a. That is each type should only ever be a member of Foo with one other type so we can given one type find the other. Or to put it in yet another way the dependency is bidirectional. Such a functional dependency would prevent me from having Bool present in two separate instances because the first parameter would be derivable from the second as well as the second from the first.



But I don't know how to express this idea to the compiler.



How can I create a bidirectional functional dependency? Or, more likely, is there a way that I can rephrase my type class to get something that could replace a bidirectional functional dependency?







haskell typeclass functional-dependencies type-level-computation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 2 at 2:12







Sriotchilism O'Zaic

















asked Jun 1 at 21:24









Sriotchilism O'ZaicSriotchilism O'Zaic

910620




910620












  • You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

    – Willem Van Onsem
    Jun 1 at 21:33

















  • You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

    – Willem Van Onsem
    Jun 1 at 21:33
















You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

– Willem Van Onsem
Jun 1 at 21:33





You can list multiple ones like class Foo a b | a -> b, b -> a where ..., but that would make the two instance Foo ... Bools problematic.

– Willem Van Onsem
Jun 1 at 21:33












3 Answers
3






active

oldest

votes


















8














A bidirectional dependency between a and b can be presented as two functional dependencies a -> b and b -> a, like:



class Foo a b | a -> b, b -> a where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


So here a is functional dependent on b and b is functional dependent on a.



For your instances however this of course raises an error, since now you defined two different as for b ~ Bool. This will raise an error like:



file.hs:6:10: error:
Functional dependencies conflict between instance declarations:
instance Foo () Bool -- Defined at file.hs:6:10
instance Foo ((), ()) Bool -- Defined at file.hs:11:10
Failed, modules loaded: none.


Because of the functional dependency, you can only define one a for b ~ Bool. But this is probably exactly what you are looking for: a mechanism to prevent defining Foo twice for the same a, or the same b.






share|improve this answer




















  • 1





    Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

    – Sriotchilism O'Zaic
    Jun 2 at 2:06


















3














(This is more a comment than an answer, since it does not address the exact question the OP asked.)



To complement Willem's answer: nowadays we have another way to make GHC accept this class.



class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool


As GHC suggests in its error message, we can turn on AllowAmbiguousTypes. The OP noted that then run in troubles if we evaluate something like g False and there are two matching instances like



instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False


Indeed, in such case g False becomes ambiguous. We then have two options.



First, we can forbid having both the instances above by adding a functional dependency b -> a to the class (as Willem suggested). That makes g False to be unambiguous (and we do not need the extension in such case).



Alternatively, we can leave both instances in the code, and disambiguate the call g False using type applications (another extension). For instance, g @() False chooses the first instance, while g @((),()) False chooses the second one.



Full code:



-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
FlexibleInstances, AllowAmbiguousTypes, TypeApplications #-

class Foo a b | a -> b where
f :: a -> Bool
g :: b -> Bool
h :: a -> b -> Bool

instance Foo () Bool where
f x = True
g y = y
h x y = False

instance Foo ((), ()) Bool where
f x = True
g y = not y
h x y = False

main :: IO ()
main = print (g @() False, g @((),()) False)





share|improve this answer






























    0














    Willem Van Onsem's answer is pretty much exactly what I wanted, but there is another way I realized that might be worth mentioning. To get the intended behavior we can actually split our class up into multiple classes. There are a couple of ways to do this and the best option probably depends on the specifics. But here is one way you could do it for the code from the question:



    class Bar b where
    g :: b -> Bool

    class (Bar b) => Foo a b | a -> b where
    f :: a -> Bool
    h :: a -> b -> Bool


    Now we do still allow us to make two different Foo instances with the same b, but we no longer get the ambiguity since g is now a member of Bar there must be a single instance across the two.



    This can be done in general by moving the functions that might be ambiguous and moving it to a separate type class.



    Another way we can use additional type classes to create a second class to enforce the bidirectionality. For the example this would look like:



    class Bar a b | b -> a

    class (Bar a b) => Foo a b | a -> b where
    f :: a -> Bool
    g :: b -> Bool
    h :: a -> b -> Bool


    Here Bar is acts to make b dependent on a preventing us from having the ambiguity. Since Foo requires Bar and Bar allows a to be derived from b, any instance of Foo allows a to be derived from b. This is pretty much what I wanted originally, however it is just a slightly more complex version of Willem Van Onsem's answer.






    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%2f56410544%2fbidirectional-functional-dependencies%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      8














      A bidirectional dependency between a and b can be presented as two functional dependencies a -> b and b -> a, like:



      class Foo a b | a -> b, b -> a where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool


      So here a is functional dependent on b and b is functional dependent on a.



      For your instances however this of course raises an error, since now you defined two different as for b ~ Bool. This will raise an error like:



      file.hs:6:10: error:
      Functional dependencies conflict between instance declarations:
      instance Foo () Bool -- Defined at file.hs:6:10
      instance Foo ((), ()) Bool -- Defined at file.hs:11:10
      Failed, modules loaded: none.


      Because of the functional dependency, you can only define one a for b ~ Bool. But this is probably exactly what you are looking for: a mechanism to prevent defining Foo twice for the same a, or the same b.






      share|improve this answer




















      • 1





        Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

        – Sriotchilism O'Zaic
        Jun 2 at 2:06















      8














      A bidirectional dependency between a and b can be presented as two functional dependencies a -> b and b -> a, like:



      class Foo a b | a -> b, b -> a where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool


      So here a is functional dependent on b and b is functional dependent on a.



      For your instances however this of course raises an error, since now you defined two different as for b ~ Bool. This will raise an error like:



      file.hs:6:10: error:
      Functional dependencies conflict between instance declarations:
      instance Foo () Bool -- Defined at file.hs:6:10
      instance Foo ((), ()) Bool -- Defined at file.hs:11:10
      Failed, modules loaded: none.


      Because of the functional dependency, you can only define one a for b ~ Bool. But this is probably exactly what you are looking for: a mechanism to prevent defining Foo twice for the same a, or the same b.






      share|improve this answer




















      • 1





        Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

        – Sriotchilism O'Zaic
        Jun 2 at 2:06













      8












      8








      8







      A bidirectional dependency between a and b can be presented as two functional dependencies a -> b and b -> a, like:



      class Foo a b | a -> b, b -> a where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool


      So here a is functional dependent on b and b is functional dependent on a.



      For your instances however this of course raises an error, since now you defined two different as for b ~ Bool. This will raise an error like:



      file.hs:6:10: error:
      Functional dependencies conflict between instance declarations:
      instance Foo () Bool -- Defined at file.hs:6:10
      instance Foo ((), ()) Bool -- Defined at file.hs:11:10
      Failed, modules loaded: none.


      Because of the functional dependency, you can only define one a for b ~ Bool. But this is probably exactly what you are looking for: a mechanism to prevent defining Foo twice for the same a, or the same b.






      share|improve this answer















      A bidirectional dependency between a and b can be presented as two functional dependencies a -> b and b -> a, like:



      class Foo a b | a -> b, b -> a where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool


      So here a is functional dependent on b and b is functional dependent on a.



      For your instances however this of course raises an error, since now you defined two different as for b ~ Bool. This will raise an error like:



      file.hs:6:10: error:
      Functional dependencies conflict between instance declarations:
      instance Foo () Bool -- Defined at file.hs:6:10
      instance Foo ((), ()) Bool -- Defined at file.hs:11:10
      Failed, modules loaded: none.


      Because of the functional dependency, you can only define one a for b ~ Bool. But this is probably exactly what you are looking for: a mechanism to prevent defining Foo twice for the same a, or the same b.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 1 at 21:41

























      answered Jun 1 at 21:37









      Willem Van OnsemWillem Van Onsem

      161k17163251




      161k17163251







      • 1





        Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

        – Sriotchilism O'Zaic
        Jun 2 at 2:06












      • 1





        Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

        – Sriotchilism O'Zaic
        Jun 2 at 2:06







      1




      1





      Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

      – Sriotchilism O'Zaic
      Jun 2 at 2:06





      Thank you. That is exactly what I am looking for, the code I was showing was specifically code that I did not want to be possible, that was possible with the single functional dependency, perhaps I should have worded it better.

      – Sriotchilism O'Zaic
      Jun 2 at 2:06













      3














      (This is more a comment than an answer, since it does not address the exact question the OP asked.)



      To complement Willem's answer: nowadays we have another way to make GHC accept this class.



      class Foo a b | a -> b where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool


      As GHC suggests in its error message, we can turn on AllowAmbiguousTypes. The OP noted that then run in troubles if we evaluate something like g False and there are two matching instances like



      instance Foo () Bool where
      f x = True
      g y = y
      h x y = False

      instance Foo ((), ()) Bool where
      f x = True
      g y = not y
      h x y = False


      Indeed, in such case g False becomes ambiguous. We then have two options.



      First, we can forbid having both the instances above by adding a functional dependency b -> a to the class (as Willem suggested). That makes g False to be unambiguous (and we do not need the extension in such case).



      Alternatively, we can leave both instances in the code, and disambiguate the call g False using type applications (another extension). For instance, g @() False chooses the first instance, while g @((),()) False chooses the second one.



      Full code:



      -# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
      FlexibleInstances, AllowAmbiguousTypes, TypeApplications #-

      class Foo a b | a -> b where
      f :: a -> Bool
      g :: b -> Bool
      h :: a -> b -> Bool

      instance Foo () Bool where
      f x = True
      g y = y
      h x y = False

      instance Foo ((), ()) Bool where
      f x = True
      g y = not y
      h x y = False

      main :: IO ()
      main = print (g @() False, g @((),()) False)





      share|improve this answer



























        3














        (This is more a comment than an answer, since it does not address the exact question the OP asked.)



        To complement Willem's answer: nowadays we have another way to make GHC accept this class.



        class Foo a b | a -> b where
        f :: a -> Bool
        g :: b -> Bool
        h :: a -> b -> Bool


        As GHC suggests in its error message, we can turn on AllowAmbiguousTypes. The OP noted that then run in troubles if we evaluate something like g False and there are two matching instances like



        instance Foo () Bool where
        f x = True
        g y = y
        h x y = False

        instance Foo ((), ()) Bool where
        f x = True
        g y = not y
        h x y = False


        Indeed, in such case g False becomes ambiguous. We then have two options.



        First, we can forbid having both the instances above by adding a functional dependency b -> a to the class (as Willem suggested). That makes g False to be unambiguous (and we do not need the extension in such case).



        Alternatively, we can leave both instances in the code, and disambiguate the call g False using type applications (another extension). For instance, g @() False chooses the first instance, while g @((),()) False chooses the second one.



        Full code:



        -# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
        FlexibleInstances, AllowAmbiguousTypes, TypeApplications #-

        class Foo a b | a -> b where
        f :: a -> Bool
        g :: b -> Bool
        h :: a -> b -> Bool

        instance Foo () Bool where
        f x = True
        g y = y
        h x y = False

        instance Foo ((), ()) Bool where
        f x = True
        g y = not y
        h x y = False

        main :: IO ()
        main = print (g @() False, g @((),()) False)





        share|improve this answer

























          3












          3








          3







          (This is more a comment than an answer, since it does not address the exact question the OP asked.)



          To complement Willem's answer: nowadays we have another way to make GHC accept this class.



          class Foo a b | a -> b where
          f :: a -> Bool
          g :: b -> Bool
          h :: a -> b -> Bool


          As GHC suggests in its error message, we can turn on AllowAmbiguousTypes. The OP noted that then run in troubles if we evaluate something like g False and there are two matching instances like



          instance Foo () Bool where
          f x = True
          g y = y
          h x y = False

          instance Foo ((), ()) Bool where
          f x = True
          g y = not y
          h x y = False


          Indeed, in such case g False becomes ambiguous. We then have two options.



          First, we can forbid having both the instances above by adding a functional dependency b -> a to the class (as Willem suggested). That makes g False to be unambiguous (and we do not need the extension in such case).



          Alternatively, we can leave both instances in the code, and disambiguate the call g False using type applications (another extension). For instance, g @() False chooses the first instance, while g @((),()) False chooses the second one.



          Full code:



          -# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
          FlexibleInstances, AllowAmbiguousTypes, TypeApplications #-

          class Foo a b | a -> b where
          f :: a -> Bool
          g :: b -> Bool
          h :: a -> b -> Bool

          instance Foo () Bool where
          f x = True
          g y = y
          h x y = False

          instance Foo ((), ()) Bool where
          f x = True
          g y = not y
          h x y = False

          main :: IO ()
          main = print (g @() False, g @((),()) False)





          share|improve this answer













          (This is more a comment than an answer, since it does not address the exact question the OP asked.)



          To complement Willem's answer: nowadays we have another way to make GHC accept this class.



          class Foo a b | a -> b where
          f :: a -> Bool
          g :: b -> Bool
          h :: a -> b -> Bool


          As GHC suggests in its error message, we can turn on AllowAmbiguousTypes. The OP noted that then run in troubles if we evaluate something like g False and there are two matching instances like



          instance Foo () Bool where
          f x = True
          g y = y
          h x y = False

          instance Foo ((), ()) Bool where
          f x = True
          g y = not y
          h x y = False


          Indeed, in such case g False becomes ambiguous. We then have two options.



          First, we can forbid having both the instances above by adding a functional dependency b -> a to the class (as Willem suggested). That makes g False to be unambiguous (and we do not need the extension in such case).



          Alternatively, we can leave both instances in the code, and disambiguate the call g False using type applications (another extension). For instance, g @() False chooses the first instance, while g @((),()) False chooses the second one.



          Full code:



          -# LANGUAGE MultiParamTypeClasses, FunctionalDependencies,
          FlexibleInstances, AllowAmbiguousTypes, TypeApplications #-

          class Foo a b | a -> b where
          f :: a -> Bool
          g :: b -> Bool
          h :: a -> b -> Bool

          instance Foo () Bool where
          f x = True
          g y = y
          h x y = False

          instance Foo ((), ()) Bool where
          f x = True
          g y = not y
          h x y = False

          main :: IO ()
          main = print (g @() False, g @((),()) False)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 2 at 8:26









          chichi

          79.4k288151




          79.4k288151





















              0














              Willem Van Onsem's answer is pretty much exactly what I wanted, but there is another way I realized that might be worth mentioning. To get the intended behavior we can actually split our class up into multiple classes. There are a couple of ways to do this and the best option probably depends on the specifics. But here is one way you could do it for the code from the question:



              class Bar b where
              g :: b -> Bool

              class (Bar b) => Foo a b | a -> b where
              f :: a -> Bool
              h :: a -> b -> Bool


              Now we do still allow us to make two different Foo instances with the same b, but we no longer get the ambiguity since g is now a member of Bar there must be a single instance across the two.



              This can be done in general by moving the functions that might be ambiguous and moving it to a separate type class.



              Another way we can use additional type classes to create a second class to enforce the bidirectionality. For the example this would look like:



              class Bar a b | b -> a

              class (Bar a b) => Foo a b | a -> b where
              f :: a -> Bool
              g :: b -> Bool
              h :: a -> b -> Bool


              Here Bar is acts to make b dependent on a preventing us from having the ambiguity. Since Foo requires Bar and Bar allows a to be derived from b, any instance of Foo allows a to be derived from b. This is pretty much what I wanted originally, however it is just a slightly more complex version of Willem Van Onsem's answer.






              share|improve this answer



























                0














                Willem Van Onsem's answer is pretty much exactly what I wanted, but there is another way I realized that might be worth mentioning. To get the intended behavior we can actually split our class up into multiple classes. There are a couple of ways to do this and the best option probably depends on the specifics. But here is one way you could do it for the code from the question:



                class Bar b where
                g :: b -> Bool

                class (Bar b) => Foo a b | a -> b where
                f :: a -> Bool
                h :: a -> b -> Bool


                Now we do still allow us to make two different Foo instances with the same b, but we no longer get the ambiguity since g is now a member of Bar there must be a single instance across the two.



                This can be done in general by moving the functions that might be ambiguous and moving it to a separate type class.



                Another way we can use additional type classes to create a second class to enforce the bidirectionality. For the example this would look like:



                class Bar a b | b -> a

                class (Bar a b) => Foo a b | a -> b where
                f :: a -> Bool
                g :: b -> Bool
                h :: a -> b -> Bool


                Here Bar is acts to make b dependent on a preventing us from having the ambiguity. Since Foo requires Bar and Bar allows a to be derived from b, any instance of Foo allows a to be derived from b. This is pretty much what I wanted originally, however it is just a slightly more complex version of Willem Van Onsem's answer.






                share|improve this answer

























                  0












                  0








                  0







                  Willem Van Onsem's answer is pretty much exactly what I wanted, but there is another way I realized that might be worth mentioning. To get the intended behavior we can actually split our class up into multiple classes. There are a couple of ways to do this and the best option probably depends on the specifics. But here is one way you could do it for the code from the question:



                  class Bar b where
                  g :: b -> Bool

                  class (Bar b) => Foo a b | a -> b where
                  f :: a -> Bool
                  h :: a -> b -> Bool


                  Now we do still allow us to make two different Foo instances with the same b, but we no longer get the ambiguity since g is now a member of Bar there must be a single instance across the two.



                  This can be done in general by moving the functions that might be ambiguous and moving it to a separate type class.



                  Another way we can use additional type classes to create a second class to enforce the bidirectionality. For the example this would look like:



                  class Bar a b | b -> a

                  class (Bar a b) => Foo a b | a -> b where
                  f :: a -> Bool
                  g :: b -> Bool
                  h :: a -> b -> Bool


                  Here Bar is acts to make b dependent on a preventing us from having the ambiguity. Since Foo requires Bar and Bar allows a to be derived from b, any instance of Foo allows a to be derived from b. This is pretty much what I wanted originally, however it is just a slightly more complex version of Willem Van Onsem's answer.






                  share|improve this answer













                  Willem Van Onsem's answer is pretty much exactly what I wanted, but there is another way I realized that might be worth mentioning. To get the intended behavior we can actually split our class up into multiple classes. There are a couple of ways to do this and the best option probably depends on the specifics. But here is one way you could do it for the code from the question:



                  class Bar b where
                  g :: b -> Bool

                  class (Bar b) => Foo a b | a -> b where
                  f :: a -> Bool
                  h :: a -> b -> Bool


                  Now we do still allow us to make two different Foo instances with the same b, but we no longer get the ambiguity since g is now a member of Bar there must be a single instance across the two.



                  This can be done in general by moving the functions that might be ambiguous and moving it to a separate type class.



                  Another way we can use additional type classes to create a second class to enforce the bidirectionality. For the example this would look like:



                  class Bar a b | b -> a

                  class (Bar a b) => Foo a b | a -> b where
                  f :: a -> Bool
                  g :: b -> Bool
                  h :: a -> b -> Bool


                  Here Bar is acts to make b dependent on a preventing us from having the ambiguity. Since Foo requires Bar and Bar allows a to be derived from b, any instance of Foo allows a to be derived from b. This is pretty much what I wanted originally, however it is just a slightly more complex version of Willem Van Onsem's answer.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 3 at 1:55









                  Sriotchilism O'ZaicSriotchilism O'Zaic

                  910620




                  910620



























                      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%2f56410544%2fbidirectional-functional-dependencies%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

                      Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                      Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                      Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림