[GHC] #12364: Demand analysis for sum types

GHC ghc-devs at haskell.org
Mon Jul 4 12:33:29 UTC 2016


#12364: Demand analysis for sum types
-------------------------------------+-------------------------------------
           Reporter:  nomeata        |             Owner:
               Type:  task           |            Status:  new
           Priority:  normal         |         Milestone:
          Component:  Compiler       |           Version:  8.0.1
           Keywords:                 |  Operating System:  Unknown/Multiple
       Architecture:                 |   Type of failure:  None/Unknown
  Unknown/Multiple                   |
          Test Case:                 |        Blocked By:
           Blocking:                 |   Related Tickets:
Differential Rev(s):                 |         Wiki Page:
-------------------------------------+-------------------------------------
 While working on #10613 it crossed my mind that it might be worthwhile to
 expand the demand type also onto sum types.

 So instead of
 {{{
   | UProd [ArgUse]     -- Product
 }}}
 we would have
 {{{
   | UData [[ArgUse]]   -- Data type
 }}}
 and a function like
 {{{
 fromMaybe :: a -> Maybe a -> b
 }}}
 would have a signature of
 {{{
 <1*U><1*U(;1*U)>
 }}}
 which indicates that the second argument of `fromMaybe` is evaluated at
 most once; the first constructor of the result has no arguments, the
 second (separated by `;`) has one argument which is also used at most
 once.

 I could imagine that this gives a lot of useful information with parsers
 and other code that repeatedly retuns stuff wrapped in a `Maybe` or
 similar type.

 Now, sum types are often recursive (`[]`…), and we probably want to be
 smarter about recursion here. But note that this is not a new problem. If
 you write
 {{{
 data Stream = Stream Int Stream Stream Stream

 foo (Stream 0 x y z) = 0
 foo (Stream 1 x y z) = foo x
 foo (Stream 2 x y z) = foo y
 foo (Stream _ x y z) = foo z
 }}}
 you already get huge demand signatures.

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


More information about the ghc-tickets mailing list