How is Eq typeclass implemented for user defined types?

前端 未结 1 1515
孤独总比滥情好
孤独总比滥情好 2021-01-18 13:44

For some user defined type such as the below how does the implementation of the Eq typeclass work? Its simple to write an implementation for things like Int or Float. But ho

相关标签:
1条回答
  • 2021-01-18 13:51

    It pattern matches against every possible value constructor, just like you said! For example, if you put your code in a file and run ghc with -ddump-deriv, here's what you get:

    ==================== Derived instances ====================
    Derived instances:
      instance GHC.Classes.Eq Main.Person where
        GHC.Classes.==
          (Main.Person a1_alh a2_ali a3_alj)
          (Main.Person b1_alk b2_all b3_alm)
          = ((((a1_alh GHC.Classes.== b1_alk))
              GHC.Classes.&& ((a2_ali GHC.Classes.== b2_all)))
             GHC.Classes.&& ((a3_alj GHC.Classes.== b3_alm)))
        GHC.Classes./= a_aln b_alo
          = GHC.Classes.not ((GHC.Classes.==) a_aln b_alo)
    
    0 讨论(0)
提交回复
热议问题