[GHC] #16340: Improve properFraction for Ratio

GHC ghc-devs at haskell.org
Tue Feb 19 22:06:48 UTC 2019


#16340: Improve properFraction for Ratio
-------------------------------------+-------------------------------------
           Reporter:  dfeuer         |             Owner:  (none)
               Type:  task           |            Status:  new
           Priority:  normal         |         Milestone:  8.10.1
          Component:  Core           |           Version:  8.6.3
  Libraries                          |
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  Runtime
  Unknown/Multiple                   |  performance bug
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 We define

 {{{#!hs
     properFraction (x:%y) = (fromInteger (toInteger q), r:%y)
       where (q, r) = quotRem x y
 }}}

 The first problem is that this produces a lazy pair. The second problem is
 that it uses `fromInteger . toInteger` rather than `fromIntegral`; the
 latter has rewrite rules that the former lacks. The whole thing should be
 written as

 {{{#!hs
     properFraction (x:%y) = (q', r:%y)
       where !(q, r) = quotRem x y
             !q' = fromIntegral q
 }}}

 or possibly

 {{{#!hs
     properFraction (x:%y) = (fromIntegral q, r:%y)
       where !(q, r) = quotRem x y
 }}}

 The latter is better if someone only wants the fractional part and is
 using `Integral` types for which `fromIntegral` is unusually expensive,
 but I don't know if that's really worth worrying about.

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


More information about the ghc-tickets mailing list