[GHC] #10858: Smaller generated instances

GHC ghc-devs at haskell.org
Tue Sep 8 19:52:36 UTC 2015


#10858: Smaller generated instances
-------------------------------------+-------------------------------------
        Reporter:  nomeata           |                   Owner:
            Type:  task              |                  Status:  new
        Priority:  normal            |               Milestone:
       Component:  Compiler          |                 Version:  7.10.2
      Resolution:                    |                Keywords:  newcomer
Operating System:  Unknown/Multiple  |            Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |               Test Case:
      Blocked By:                    |                Blocking:
 Related Tickets:                    |  Differential Revisions:
-------------------------------------+-------------------------------------
Changes (by nomeata):

 * keywords:   => newcomer


Comment:

 Consider this code generated for `Ord` for a product type:
 {{{
   instance Ord l_acv3 => Ord (ImportDecl l_acv3) where
     compare a_acXz b_acXA
       = case a_acXz of {
           ImportDecl a1_acXB
                      a2_acXC
                      a3_acXD
                      a4_acXE
                      a5_acXF
                      a6_acXG
                      a7_acXH
                      a8_acXI
             -> case b_acXA of {
                  ImportDecl b1_acXJ
                             b2_acXK
                             b3_acXL
                             b4_acXM
                             b5_acXN
                             b6_acXO
                             b7_acXP
                             b8_acXQ
                    -> case (compare a1_acXB b1_acXJ) of {
                         LT -> LT
                         EQ
                           -> case (compare a2_acXC b2_acXK) of {
                                LT -> LT
                                EQ
                                  -> case (compare a3_acXD b3_acXL) of {
                                       LT -> LT
                                       EQ
                                         -> case (compare a4_acXE b4_acXM)
 of {
                                              LT -> LT
                                              EQ
                                                -> case (compare a5_acXF
 b5_acXN) of {
                                                     LT -> LT
                                                     EQ
                                                       -> case (compare
 a6_acXG b6_acXO) of {
                                                            LT -> LT
                                                            EQ
                                                              -> case
 (compare a7_acXH b7_acXP) of {
                                                                   LT -> LT
                                                                   EQ ->
 (a8_acXI `compare` b8_acXQ)
                                                                   GT -> GT
 }
                                                            GT -> GT }
                                                     GT -> GT }
                                              GT -> GT }
                                       GT -> GT }
                                GT -> GT }
                         GT -> GT } } }
     (<) a_acXR b_acXS
       = case a_acXR of {
           ImportDecl a1_acXT
                      a2_acXU
                      a3_acXV
                      a4_acXW
                      a5_acXX
                      a6_acXY
                      a7_acXZ
                      a8_acY0
             -> case b_acXS of {
                  ImportDecl b1_acY1
                             b2_acY2
                             b3_acY3
                             b4_acY4
                             b5_acY5
                             b6_acY6
                             b7_acY7
                             b8_acY8
                    -> case (compare a1_acXT b1_acY1) of {
                         LT -> True
                         EQ
                           -> case (compare a2_acXU b2_acY2) of {
                                LT -> True
                                EQ
                                  -> case (compare a3_acXV b3_acY3) of {
                                       LT -> True
                                       EQ
                                         -> case (compare a4_acXW b4_acY4)
 of {
                                              LT -> True
                                              EQ
                                                -> case (compare a5_acXX
 b5_acY5) of {
                                                     LT -> True
                                                     EQ
                                                       -> case (compare
 a6_acXY b6_acY6) of {
                                                            LT -> True
                                                            EQ
                                                              -> case
 (compare a7_acXZ b7_acY7) of {
                                                                   LT ->
 True
                                                                   EQ ->
 (a8_acY0 < b8_acY8)
                                                                   GT ->
 False }
                                                            GT -> False }
                                                     GT -> False }
                                              GT -> False }
                                       GT -> False }
                                GT -> False }
                         GT -> False } } }
     (<=) a_acY9 b_acYa
       = case a_acY9 of {
           ImportDecl a1_acYb
                      a2_acYc
                      a3_acYd
                      a4_acYe
                      a5_acYf
                      a6_acYg
                      a7_acYh
                      a8_acYi
             -> case b_acYa of {
                  ImportDecl b1_acYj
                             b2_acYk
                             b3_acYl
                             b4_acYm
                             b5_acYn
                             b6_acYo
                             b7_acYp
                             b8_acYq
                    -> case (compare a1_acYb b1_acYj) of {
                         LT -> True
                         EQ
                           -> case (compare a2_acYc b2_acYk) of {
                                LT -> True
                                EQ
                                  -> case (compare a3_acYd b3_acYl) of {
                                       LT -> True
                                       EQ
                                         -> case (compare a4_acYe b4_acYm)
 of {
                                              LT -> True
                                              EQ
                                                -> case (compare a5_acYf
 b5_acYn) of {
                                                     LT -> True
                                                     EQ
                                                       -> case (compare
 a6_acYg b6_acYo) of {
                                                            LT -> True
                                                            EQ
                                                              -> case
 (compare a7_acYh b7_acYp) of {
                                                                   LT ->
 True
                                                                   EQ ->
 (a8_acYi <= b8_acYq)
                                                                   GT ->
 False }
                                                            GT -> False }
                                                     GT -> False }
                                              GT -> False }
                                       GT -> False }
                                GT -> False }
                         GT -> False } } }
     (>=) a_acYr b_acYs
       = case a_acYr of {
           ImportDecl a1_acYt
                      a2_acYu
                      a3_acYv
                      a4_acYw
                      a5_acYx
                      a6_acYy
                      a7_acYz
                      a8_acYA
             -> case b_acYs of {
                  ImportDecl b1_acYB
                             b2_acYC
                             b3_acYD
                             b4_acYE
                             b5_acYF
                             b6_acYG
                             b7_acYH
                             b8_acYI
                    -> case (compare a1_acYt b1_acYB) of {
                         LT -> False
                         EQ
                           -> case (compare a2_acYu b2_acYC) of {
                                LT -> False
                                EQ
                                  -> case (compare a3_acYv b3_acYD) of {
                                       LT -> False
                                       EQ
                                         -> case (compare a4_acYw b4_acYE)
 of {
                                              LT -> False
                                              EQ
                                                -> case (compare a5_acYx
 b5_acYF) of {
                                                     LT -> False
                                                     EQ
                                                       -> case (compare
 a6_acYy b6_acYG) of {
                                                            LT -> False
                                                            EQ
                                                              -> case
 (compare a7_acYz b7_acYH) of {
                                                                   LT ->
 False
                                                                   EQ ->
 (a8_acYA >= b8_acYI)
                                                                   GT ->
 True }
                                                            GT -> True }
                                                     GT -> True }
                                              GT -> True }
                                       GT -> True }
                                GT -> True }
                         GT -> True } } }
     (>) a_acYJ b_acYK
       = case a_acYJ of {
           ImportDecl a1_acYL
                      a2_acYM
                      a3_acYN
                      a4_acYO
                      a5_acYP
                      a6_acYQ
                      a7_acYR
                      a8_acYS
             -> case b_acYK of {
                  ImportDecl b1_acYT
                             b2_acYU
                             b3_acYV
                             b4_acYW
                             b5_acYX
                             b6_acYY
                             b7_acYZ
                             b8_acZ0
                    -> case (compare a1_acYL b1_acYT) of {
                         LT -> False
                         EQ
                           -> case (compare a2_acYM b2_acYU) of {
                                LT -> False
                                EQ
                                  -> case (compare a3_acYN b3_acYV) of {
                                       LT -> False
                                       EQ
                                         -> case (compare a4_acYO b4_acYW)
 of {
                                              LT -> False
                                              EQ
                                                -> case (compare a5_acYP
 b5_acYX) of {
                                                     LT -> False
                                                     EQ
                                                       -> case (compare
 a6_acYQ b6_acYY) of {
                                                            LT -> False
                                                            EQ
                                                              -> case
 (compare a7_acYR b7_acYZ) of {
                                                                   LT ->
 False
                                                                   EQ ->
 (a8_acYS > b8_acZ0)
                                                                   GT ->
 True }
                                                            GT -> True }
                                                     GT -> True }
                                              GT -> True }
                                       GT -> True }
                                GT -> True }
                         GT -> True } } }
 }}}

 This is huge! And so redundant.

 If the implementation of the operators `>` etc. build on the individual
 `compare` functions anyways, then it would surely be worth it to use the
 `compare` chain that was generated for the `compare` function – i.e.
 simply use the default instance.

 Also, for the compare function, maybe we should add a function
 {{{
 thenCmp :: Ordering -> Ordering -> Ordering
 thenCmp EQ o2 = o2
 thenCmp o1 _  = o1
 }}}
 to `Data.Ord` and use that to simply this to
 {{{
   instance Ord l_acv3 => Ord (ImportDecl l_acv3) where
     compare a_acXz b_acXA
       = case a_acXz of {
           ImportDecl a1_acXB
                      a2_acXC
                      a3_acXD
                      a4_acXE
                      a5_acXF
                      a6_acXG
                      a7_acXH
                      a8_acXI
             -> case b_acXA of {
                  ImportDecl b1_acXJ
                             b2_acXK
                             b3_acXL
                             b4_acXM
                             b5_acXN
                             b6_acXO
                             b7_acXP
                             b8_acXQ
                    -> compare a1_acXB b1_acXJ `thenComp`
                       compare a2_acXC b2_acXK `thenComp`
                       compare a3_acXD b3_acXL `thenComp`
                       compare a4_acXE b4_acXM `thenComp`
                       compare a5_acXF b5_acXN `thenComp`
                       compare a6_acXG b6_acXO `thenComp`
                       compare a7_acXH b7_acXP `thenComp`
                       compare a8_acXI b8_acXQ
 }}}

 Maybe this is a nice newcomer ticket: Relatively local, rewarding, and
 easy to get right.

 This is `mkCompareFields` in `TcGenDeriv`

 Also look at the other deriving instances for refactoring possibilities!

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/10858#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list