Skip to content

Demand analysis for sum types

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.

Trac metadata
Trac field Value
Version 8.0.1
Type Task
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC ilya
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information