15221539785f9ac5a624f6bd35352f59003304c8
[packages/dph.git] / dph-prim-interface / interface / DPH_Interface.h
1 import qualified GHC.Base
2 import Prelude ((.), ($))
3
4 instance Elt Int
5 instance Elt Word8
6 instance Elt Bool
7 instance Elt Double
8 instance (Elt a, Elt b) => Elt (a :*: b)
9
10 infixl 9 !:
11 infixr 5 +:+
12 --infixr 5 ^+:+^
13 --infixr 9 >:
14
15 length :: Elt a => Array a -> Int
16 {-# INLINE_BACKEND length #-}
17
18 empty :: Elt a => Array a
19 {-# INLINE_BACKEND empty #-}
20
21 replicate :: Elt a => Int -> a -> Array a
22 {-# INLINE CONLIKE PHASE_BACKEND replicate #-}
23
24 repeat :: Elt a => Int -> Int -> Array a -> Array a
25 {-# INLINE_BACKEND repeat #-}
26
27 (!:) :: Elt a => Array a -> Int -> a
28 {-# INLINE_BACKEND (!:) #-}
29
30 extract :: Elt a => Array a -> Int -> Int -> Array a
31 {-# INLINE_BACKEND extract #-}
32
33 drop :: Elt a => Int -> Array a -> Array a
34 {-# INLINE_BACKEND drop #-}
35
36 permute :: Elt a => Array a -> Array Int -> Array a
37 {-# INLINE_BACKEND permute #-}
38
39 bpermute :: Elt a => Array a -> Array Int -> Array a
40 {-# INLINE_BACKEND bpermute #-}
41
42 update :: Elt a => Array a -> Array (Int :*: a) -> Array a
43 {-# INLINE_BACKEND update #-}
44
45 (+:+) :: Elt a => Array a -> Array a -> Array a
46 {-# INLINE_BACKEND (+:+) #-}
47
48
49 pack :: Elt a => Array a -> Array Bool -> Array a
50 {-# INLINE_BACKEND pack #-}
51
52 combine :: Elt a => Array Bool -> Array a -> Array a -> Array a
53 {-# INLINE_BACKEND combine #-}
54
55 map :: (Elt a, Elt b) => (a -> b) -> Array a -> Array b
56 {-# INLINE_BACKEND map #-}
57
58 filter :: Elt a => (a -> Bool) -> Array a -> Array a
59 {-# INLINE_BACKEND filter #-}
60
61 zip :: (Elt a, Elt b) => Array a -> Array b -> Array (a :*: b)
62 {-# INLINE CONLIKE PHASE_BACKEND zip #-}
63
64 unzip :: (Elt a, Elt b) => Array (a :*: b) -> Array a :*: Array b
65 {-# INLINE_BACKEND unzip #-}
66
67 fsts :: (Elt a, Elt b) => Array (a :*: b) -> Array a
68 {-# INLINE_BACKEND fsts #-}
69
70 snds :: (Elt a, Elt b) => Array (a :*: b) -> Array b
71 {-# INLINE_BACKEND snds #-}
72
73 zip3 :: (Elt a, Elt b, Elt c) => Array a -> Array b -> Array c
74 -> Array (a :*: b :*: c)
75 {-# INLINE_BACKEND zip3 #-}
76
77 unzip3 :: (Elt a, Elt b, Elt c)
78 => Array (a :*: b :*: c) -> Array a :*: Array b :*: Array c
79 {-# INLINE_BACKEND unzip3 #-}
80
81 zipWith :: (Elt a, Elt b, Elt c)
82 => (a -> b -> c) -> Array a -> Array b -> Array c
83 {-# INLINE_BACKEND zipWith #-}
84
85 zipWith3 :: (Elt a, Elt b, Elt c, Elt d)
86 => (a -> b -> c -> d) -> Array a -> Array b -> Array c -> Array d
87 {-# INLINE_BACKEND zipWith3 #-}
88
89
90 fold :: Elt a => (a -> a -> a) -> a -> Array a -> a
91 {-# INLINE_BACKEND fold #-}
92
93 fold1 :: Elt a => (a -> a -> a) -> Array a -> a
94 {-# INLINE_BACKEND fold1 #-}
95
96 and :: Array Bool -> Bool
97 {-# INLINE_BACKEND and #-}
98
99 sum :: (Num a, Elt a) => Array a -> a
100 {-# INLINE_BACKEND sum #-}
101
102 scan :: Elt a => (a -> a -> a) -> a -> Array a -> Array a
103 {-# INLINE_BACKEND scan #-}
104
105
106 indexed :: Elt a => Array a -> Array (Int :*: a)
107 {-# INLINE_BACKEND indexed #-}
108
109 enumFromTo :: Int -> Int -> Array Int
110 {-# INLINE_BACKEND enumFromTo #-}
111
112 enumFromThenTo :: Int -> Int -> Int -> Array Int
113 {-# INLINE_BACKEND enumFromThenTo #-}
114
115 enumFromStepLen :: Int -> Int -> Int -> Array Int
116 {-# INLINE_BACKEND enumFromStepLen #-}
117
118 enumFromToEach :: Int -> Array (Int :*: Int) -> Array Int
119 {-# INLINE_BACKEND enumFromToEach #-}
120
121 enumFromStepLenEach :: Int -> Array (Int :*: Int :*: Int) -> Array Int
122 {-# INLINE_BACKEND enumFromStepLenEach #-}
123
124 replicate_s :: Elt a => Segd -> Array a -> Array a
125 {-# INLINE_BACKEND replicate_s #-}
126
127 append_s :: Elt a => Segd -- ^ segment descriptor of first array
128 -> Array a -- ^ data of first array
129 -> Segd -- ^ segment descriptor of second array
130 -> Array a -- ^ data of first array
131 -> Array a
132 {-# INLINE_BACKEND append_s #-}
133
134 repeat_c :: Elt a => Int -> Array Int -> Segd -> Array a -> Array a
135 {-# INLINE_BACKEND repeat_c #-}
136 repeat_c k ns segd xs
137 = bpermute xs
138 . enumFromStepLenEach k
139 $ zip3 (snds ks) (replicate (elementsSegd segd') 1) (fsts ks)
140 where
141 segd' = lengthsToSegd ns
142 ks = replicate_s segd'
143 $ zip (lengthsSegd segd) (indicesSegd segd)
144
145 fold_s :: Elt a => (a -> a -> a) -> a -> Segd -> Array a -> Array a
146 {-# INLINE_BACKEND fold_s #-}
147
148 fold1_s :: Elt a => (a -> a -> a) -> Segd -> Array a -> Array a
149 {-# INLINE_BACKEND fold1_s #-}
150
151 sum_s :: (Num a, Elt a) => Segd -> Array a -> Array a
152 {-# INLINE sum_s #-}
153 sum_s = fold_s (Prelude.+) 0
154
155 sum_r :: (Num a, Elt a) => Int -> Int ->Array a -> Array a
156 {-# INLINE_BACKEND sum_r #-}
157
158 indices_s :: Int -- ^ number of segments
159 -> Segd -- ^ segment descriptor
160 -> Int -- ^ overall number of indices
161 -> Array Int
162 {-# INLINE indices_s #-}
163 indices_s m segd n = enumFromToEach n
164 . zip (replicate m 0)
165 . map (Prelude.subtract 1)
166 $ lengthsSegd segd
167
168 lengthSegd :: Segd -> Int
169 {-# INLINE_BACKEND lengthSegd #-}
170
171 lengthsSegd :: Segd -> Array Int
172 {-# INLINE_BACKEND lengthsSegd #-}
173
174 indicesSegd :: Segd -> Array Int
175 {-# INLINE_BACKEND indicesSegd #-}
176
177 elementsSegd :: Segd -> Int
178 {-# INLINE_BACKEND elementsSegd #-}
179
180 lengthsToSegd :: Array Int -> Segd
181 {-# INLINE_BACKEND lengthsToSegd #-}
182
183 mkSegd :: Array Int -> Array Int -> Int -> Segd
184 {-# INLINE CONLIKE PHASE_BACKEND mkSegd #-}
185
186 {-# RULES
187
188 "lengthsSegd/mkSegd" forall lens idxs n.
189 lengthsSegd (mkSegd lens idxs n) = lens
190
191 "indicesSegd/mkSegd" forall lens idxs n.
192 indicesSegd (mkSegd lens idxs n) = idxs
193
194 "elementsSegd/mkSegd" forall lens idxs n.
195 elementsSegd (mkSegd lens idxs n) = n
196
197 #-}
198
199 randoms :: (Elt a, System.Random.Random a, System.Random.RandomGen g)
200 => Int -> g -> Array a
201 {-# INLINE_BACKEND randoms #-}
202
203 randomRs :: (Elt a, System.Random.Random a, System.Random.RandomGen g)
204 => Int -> (a,a) -> g -> Array a
205 {-# INLINE_BACKEND randomRs #-}
206
207
208 instance IOElt Int
209 instance IOElt Double
210 instance (IOElt a, IOElt b) => IOElt (a :*: b)
211
212 hPut :: IOElt a => Handle -> Array a -> IO ()
213 {-# INLINE_BACKEND hPut #-}
214
215 hGet :: IOElt a => Handle -> IO (Array a)
216 {-# INLINE_BACKEND hGet #-}
217
218 toList :: Elt a => Array a -> [a]
219 {-# INLINE_BACKEND toList #-}
220
221 fromList :: Elt a => [a] -> Array a
222 {-# INLINE_BACKEND fromList #-}
223
224 dph_mod_index :: Int -> Int -> Int
225 {-# INLINE_BACKEND dph_mod_index #-}
226 dph_mod_index by idx = idx `Prelude.mod` by
227
228 {-# RULES
229
230 "bpermute/repeat" forall n len xs is.
231 bpermute (repeat n len xs) is
232 = len `Prelude.seq` bpermute xs (map (dph_mod_index len) is)
233
234 #-}
235
236 {-# RULES
237
238 "replicate_s/replicate" forall segd k x.
239 replicate_s segd (replicate k x) = replicate (elementsSegd segd) x
240
241 #-}
242
243 {-# RULES
244
245 "repeat_c/repeat" forall k ks n m idxs nm len xs.
246 repeat_c k ks (mkSegd (replicate n m) idxs nm) (repeat n len xs)
247 = repeat (sum ks) len xs
248
249 #-}
250
251 {-# RULES
252
253 "scan/replicate" forall z n x.
254 scan GHC.Base.plusInt z (replicate n x)
255 = enumFromStepLen z x n
256
257 "map/zipWith (+)/enumFromStepLen" forall m n is.
258 map (dph_mod_index m) (zipWith GHC.Base.plusInt (enumFromStepLen 0 m n) is)
259 = map (dph_mod_index m) is
260
261 #-}
262
263 -- These are for Gabi
264 {- RULES
265
266 "repeat/bpermute" forall n len xs is.
267 repeat n len (bpermute xs is)
268 = bpermute xs (repeat n len is)
269
270 "lengthsToSegd/replicate" forall m n.
271 lengthsToSegd (replicate m n)
272 = let { m' = m; n' = n } in toSegd (zip (replicate m' n')
273 (enumFromStepLen 0 n' m'))
274
275 "fromSegd/toSegd" forall ps.
276 fromSegd (toSegd ps) = ps
277
278 "sum/replicate" forall m n.
279 sum (replicate m n) = m Prelude.* n
280
281 "replicateEach/zip" forall n lens xs ys.
282 replicateEach n lens (zip xs ys)
283 = let { n' = n; lens' = lens } in zip (replicateEach n' lens' xs)
284 (replicateEach n' lens' ys)
285
286 "fsts/zip" forall xs ys.
287 fsts (zip xs ys) = xs
288
289 "snds/zip" forall xs ys.
290 snds (zip xs ys) = ys
291
292 "repeat/enumFromStepLenEach" forall n m m' ps.
293 repeat n m (enumFromStepLenEach m' ps)
294 = enumFromStepLenEach (n*m) (repeat n (length ps) ps)
295
296 "repeat/zip3" forall n len xs ys zs.
297 repeat n len (zip3 xs ys zs)
298 = zip3 (repeat n len xs) (repeat n len ys) (repeat n len zs)
299
300 "repeat/replicate" forall n len m x.
301 repeat n len (replicate m x)
302 = replicate len x
303
304 -}
305