[GHC] #15921: Data.List.maximumBy uses counter-intuitive ordering

GHC ghc-devs at haskell.org
Thu Dec 27 16:34:07 UTC 2018


#15921: Data.List.maximumBy uses counter-intuitive ordering
-------------------------------------+-------------------------------------
        Reporter:  qqwy              |                Owner:  (none)
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.6.2
      Resolution:                    |             Keywords:  List
                                     |  maximumBy minimumBy
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by j.waldmann):

 Documentation already says "the (comparison) function is assumed to define
 a total ordering". The function `comparing snd` does not. It defines a
 relation that is total,
 transitive, reflexive, but not antisymmetric. Then all bets are off, at
 least officially.

 There are local `minBy/maxBy` functions in the implementation
 {{{
 min' x y = case cmp x y of
                         GT -> y
                         _  -> x

 max' x y = case cmp x y of
                         GT -> x
                         _  -> y
 }}}
 that treat the `EQ` case differently. That does not feel consistent
 with the "laws for `Ord` that are expected to hold" (where `EQ` would
 always select the first argument)
 {{{
 max x y == if x >= y then x else y = True
 min x y == if x <= y then x else y = True
 }}}

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


More information about the ghc-tickets mailing list