[GHC] #13572: Add ArgMin / ArgMax pattern synonyms

GHC ghc-devs at haskell.org
Thu Apr 13 13:33:02 UTC 2017


#13572: Add ArgMin / ArgMax pattern synonyms
-------------------------------------+-------------------------------------
           Reporter:  Iceland_jack   |             Owner:  (none)
               Type:  feature        |            Status:  new
  request                            |
           Priority:  normal         |         Milestone:
          Component:                 |           Version:  8.0.1
  libraries/base                     |
           Keywords:                 |  Operating System:  Unknown/Multiple
  PatternSynonyms                    |
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 {{{#!hs
 import Data.Semigroup

 pattern ArgMin :: a -> b -> ArgMin a b
 pattern ArgMin a b = Min (Arg a b)

 pattern ArgMax :: a -> b -> ArgMax a b
 pattern ArgMax a b = Max (Arg a b)
 }}}

 Or even record pattern synonyms, à la
 [https://github.com/energyflowanalysis/efa-2.1/blob/3eb86c0b6b2d2fb66cd5b76ce5c0437d42f8cdf8/sandbox/delaunay/HalfPlaneMap.hs#L181
 this]

 {{{#!hs
 pattern ArgMin :: a -> b -> ArgMin a b
 pattern ArgMin {minArg, minValue} = Min (Arg minArg minValue)

 pattern ArgMax :: a -> b -> ArgMax a b
 pattern ArgMax [maxArg, maxValue} = Max (Arg maxArg maxValue)
 }}}

 and we can define

 {{{#!hs
 import Data.Semigroup.Foldable

 argmin :: Ord k => Foldable1 f => (a -> k) -> (f a -> a)
 argmin f = minValue . foldMap1 (\a -> ArgMin (f a) a)

 argmax :: Ord k => Foldable1 f => (a -> k) -> (f a -> a)
 argmax f = maxValue . foldMap1 (\a -> ArgMax (f a) a)
 }}}

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


More information about the ghc-tickets mailing list