2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[Demand]{@Demand@: the amount of demand on a value}
10 wwLazy, wwStrict, wwUnpackData, wwUnpackNew, wwPrim, wwEnum,
16 #include "HsVersions.h"
18 import BasicTypes ( NewOrData(..) )
24 %************************************************************************
26 \subsection{The @Demand@ data type}
28 %************************************************************************
32 = WwLazy -- Argument is lazy as far as we know
33 MaybeAbsent -- (does not imply worker's existence [etc]).
34 -- If MaybeAbsent == True, then it is
35 -- *definitely* lazy. (NB: Absence implies
38 | WwStrict -- Argument is strict but that's all we know
39 -- (does not imply worker's existence or any
40 -- calling-convention magic)
42 | WwUnpack -- Argument is strict & a single-constructor type
44 Bool -- True <=> wrapper unpacks it; False <=> doesn't
45 [Demand] -- Its constituent parts (whose StrictInfos
46 -- are in the list) should be passed
47 -- as arguments to the worker.
49 | WwPrim -- Argument is of primitive type, therefore
50 -- strict; doesn't imply existence of a worker;
51 -- argument should be passed as is to worker.
53 | WwEnum -- Argument is strict & an enumeration type;
54 -- an Int# representing the tag (start counting
55 -- at zero) should be passed to the worker.
58 type MaybeAbsent = Bool -- True <=> not even used
60 -- versions that don't worry about Absence:
63 wwUnpackData xs = WwUnpack DataType False xs
64 wwUnpackNew x = WwUnpack NewType False [x]
70 %************************************************************************
72 \subsection{Functions over @Demand@}
74 %************************************************************************
77 isStrict :: Demand -> Bool
79 isStrict WwStrict = True
80 isStrict (WwUnpack DataType _ _) = True
81 isStrict (WwUnpack NewType _ ds) = isStrict (head ds)
82 isStrict WwPrim = True
83 isStrict WwEnum = True
88 %************************************************************************
90 \subsection{Instances}
92 %************************************************************************
95 showDemands :: [Demand] -> String
96 showDemands wrap_args = show_demands wrap_args ""
99 #ifdef REALLY_HASKELL_1_3
101 instance Read Demand where
102 readList str = read_em [] str
103 instance Show Demand where
104 showsPrec prec wrap rest = show_demand wrap rest
105 showList wrap_args rest = show_demands wrap_args rest
109 instance Text Demand where
110 readList str = read_em [] str
111 showList wrap_args rest = show_demands wrap_args rest
115 read_em acc ('L' : xs) = read_em (WwLazy False : acc) xs
116 read_em acc ('A' : xs) = read_em (WwLazy True : acc) xs
117 read_em acc ('S' : xs) = read_em (WwStrict : acc) xs
118 read_em acc ('P' : xs) = read_em (WwPrim : acc) xs
119 read_em acc ('E' : xs) = read_em (WwEnum : acc) xs
120 read_em acc (')' : xs) = [(reverse acc, xs)]
121 read_em acc ( 'U' : '(' : xs) = do_unpack DataType True acc xs
122 read_em acc ( 'u' : '(' : xs) = do_unpack DataType False acc xs
123 read_em acc ( 'N' : '(' : xs) = do_unpack NewType True acc xs
124 read_em acc ( 'n' : '(' : xs) = do_unpack NewType False acc xs
125 read_em acc rest = [(reverse acc, rest)]
127 do_unpack new_or_data wrapper_unpacks acc xs
128 = case (read_em [] xs) of
129 [(stuff, rest)] -> read_em (WwUnpack new_or_data wrapper_unpacks stuff : acc) rest
130 _ -> panic ("Demand.do_unpack:"++show acc++"::"++xs)
132 show_demands wrap_args rest
133 = foldr show_demand rest wrap_args
135 show_demand (WwLazy False) rest = 'L' : rest
136 show_demand (WwLazy True) rest = 'A' : rest
137 show_demand WwStrict rest = 'S' : rest
138 show_demand WwPrim rest = 'P' : rest
139 show_demand WwEnum rest = 'E' : rest
140 show_demand (WwUnpack nd wu args) rest = ch:'(':showList args (')' : rest)
148 instance Outputable Demand where
149 ppr si = text (showList [si] "")