Reorganise the way the lifted reference implementation works
[packages/dph.git] / dph-lifted-boxed / Data / Array / Parallel / PArray / PRepr.hs
1
2 module Data.Array.Parallel.PArray.PRepr
3 ( PRepr
4 , PA (..)
5 , toVectorPA
6 , fromVectorPA)
7 where
8 import Data.Array.Parallel.PArray.PData
9 import Data.Array.Parallel.PArray.Types
10 import qualified Data.Vector as V
11 import Data.Vector (Vector)
12 import Data.Word
13
14
15 -- Wrap
16 newtype instance PData (Wrap a) = PWrap (PData a)
17 newtype instance PDatas (Wrap a) = PWraps (PDatas a)
18
19 instance PA a => PR (Wrap a) where
20 fromVectorPR vec
21 = PWrap $ fromVectorPA $ V.map unWrap vec
22
23 toVectorPR (PWrap pdata)
24 = V.map Wrap $ toVectorPA pdata
25
26
27 ------------------------------------------------------------------------
28 -- | Fake PRepr and PA classes.
29 -- The vectoriser wants to build PA instances involving these types,
30 -- but we don't need that support for this library.
31 type family PRepr a
32
33 class PR (PRepr a) => PA a where
34 toPRepr :: a -> PRepr a
35 fromPRepr :: PRepr a -> a
36
37 toArrPRepr :: PData a -> PData (PRepr a)
38 fromArrPRepr :: PData (PRepr a) -> PData a
39
40 toArrPReprs :: PDatas a -> PDatas (PRepr a)
41 fromArrPReprs :: PDatas (PRepr a) -> PDatas a
42
43
44 -- PA Functions ------------------------------------------
45 fromVectorPA :: PA a => Vector a -> PData a
46 fromVectorPA vec
47 = fromArrPRepr
48 $ fromVectorPR (V.map toPRepr vec)
49
50
51 toVectorPA :: PA a => PData a -> Vector a
52 toVectorPA pdata
53 = V.map fromPRepr
54 $ toVectorPR (toArrPRepr pdata)
55
56 -----------------------------------------------------------
57
58 -- Void
59 type instance PRepr Void
60 = Void
61
62 instance PA Void where
63 toPRepr = id
64 fromPRepr = id
65 toArrPRepr = id
66 fromArrPRepr = id
67 toArrPReprs = id
68 fromArrPReprs = id
69
70
71 -- ()
72 type instance PRepr ()
73 = ()
74
75 instance PA () where
76 toPRepr = id
77 fromPRepr = id
78 toArrPRepr = id
79 fromArrPRepr = id
80 toArrPReprs = id
81 fromArrPReprs = id
82
83 -- Int
84 type instance PRepr Int
85 = Int
86
87 instance PA Int where
88 toPRepr = id
89 fromPRepr = id
90 toArrPRepr = id
91 fromArrPRepr = id
92 toArrPReprs = id
93 fromArrPReprs = id
94
95 -- Double
96 type instance PRepr Double
97 = Double
98
99 instance PA Double where
100 toPRepr = id
101 fromPRepr = id
102 toArrPRepr = id
103 fromArrPRepr = id
104 toArrPReprs = id
105 fromArrPReprs = id
106
107 -- Word8
108 type instance PRepr Word8
109 = Word8
110
111 instance PA Word8 where
112 toPRepr = id
113 fromPRepr = id
114 toArrPRepr = id
115 fromArrPRepr = id
116 toArrPReprs = id
117 fromArrPReprs = id
118
119 -- Tuple2
120 type instance PRepr (a, b)
121 = (Wrap a, Wrap b)
122
123 instance (PA a, PA b) => PA (a, b) where
124 toPRepr (a, b)
125 = (Wrap a, Wrap b)
126
127 fromPRepr (Wrap a, Wrap b)
128 = (a, b)
129
130 toArrPRepr (PTuple2 as bs)
131 = PTuple2 (PWrap as) (PWrap bs)
132
133 fromArrPRepr (PTuple2 (PWrap as) (PWrap bs))
134 = PTuple2 as bs
135
136 toArrPReprs (PTuple2s as bs)
137 = PTuple2s (PWraps as) (PWraps bs)
138
139 fromArrPReprs (PTuple2s (PWraps as) (PWraps bs))
140 = PTuple2s as bs
141
142 -- PArray
143 type instance PRepr (PArray a)
144 = PArray (PRepr a)
145
146 instance PA a => PA (PArray a) where
147 toPRepr (PArray n xs)
148 = PArray n $ toArrPRepr xs
149
150 fromPRepr (PArray n xs)
151 = PArray n $ fromArrPRepr xs
152
153 toArrPRepr (PNested xs)
154 = PNested $ V.map toPRepr xs
155
156 fromArrPRepr (PNested xs)
157 = PNested $ V.map fromPRepr xs
158
159 toArrPReprs (PNesteds vec)
160 = PNesteds $ V.map toArrPRepr vec
161
162 fromArrPReprs (PNesteds vec)
163 = PNesteds $ V.map fromArrPRepr vec
164
165
166 -- Bool
167 type instance PRepr Bool
168 = Bool
169
170 instance PA Bool where
171 toPRepr = id
172 fromPRepr = id
173 toArrPRepr = id
174 fromArrPRepr = id
175 toArrPReprs = id
176 fromArrPReprs = id
177
178
179