Vect.hs 2.94 KB
Newer Older
1
2

-- | Simple vectorised constructors and projections.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module Vectorise.Vect (
	Vect, VVar, VExpr, VBind,

	vectorised,
	lifted,
	mapVect,

	vVarType,
	vNonRec,
	vRec,
	vVar,
	vType,
	vNote,
	vLet,
	vLams,
	vLamsWithoutLC,
	vVarApps,
	vCaseDEFAULT
21
22
) where
import CoreSyn
23
import Type           ( Type )
24
25
import Var

26
-- | Contains the vectorised and lifted versions of some thing.
27
28
29
type Vect a = (a,a)
type VVar   = Vect Var
type VExpr  = Vect CoreExpr
30
type VBind  = Vect CoreBind
31

32
33

-- | Get the vectorised version of a thing.
34
35
36
vectorised :: Vect a -> a
vectorised = fst

37
38

-- | Get the lifted version of a thing.
39
40
41
lifted :: Vect a -> a
lifted = snd

42
43

-- | Apply some function to both the vectorised and lifted versions of a thing.
44
45
46
mapVect :: (a -> b) -> Vect a -> Vect b
mapVect f (x,y) = (f x, f y)

47
48

-- | Combine vectorised and lifted versions of two things componentwise.
49
50
51
zipWithVect :: (a -> b -> c) -> Vect a -> Vect b -> Vect c
zipWithVect f (x1,y1) (x2,y2) = (f x1 x2, f y1 y2)

52
53

-- | Get the type of a vectorised variable.
54
55
56
vVarType :: VVar -> Type
vVarType = varType . vectorised

57
58

-- | Wrap a vectorised variable as a vectorised expression.
59
60
61
vVar :: VVar -> VExpr
vVar = mapVect Var

62
63

-- | Wrap a vectorised type as a vectorised expression.
64
65
66
vType :: Type -> VExpr
vType ty = (Type ty, Type ty)

67
68

-- | Make a vectorised note.
69
70
71
vNote :: Note -> VExpr -> VExpr
vNote = mapVect . Note

72
73

-- | Make a vectorised non-recursive binding.
74
75
76
vNonRec :: VVar -> VExpr -> VBind
vNonRec = zipWithVect NonRec

77
78

-- | Make a vectorised recursive binding.
79
80
81
82
83
84
vRec :: [VVar] -> [VExpr] -> VBind
vRec vs es = (Rec (zip vvs ves), Rec (zip lvs les))
  where
    (vvs, lvs) = unzip vs
    (ves, les) = unzip es

85
86

-- | Make a vectorised let expresion.
87
88
89
vLet :: VBind -> VExpr -> VExpr
vLet = zipWithVect Let

90
91
92
93
94
95
96
97
98
99

-- | Make a vectorised lambda abstraction.
--   The lifted version also binds the lifting context.
vLams	:: Var		-- ^ Var bound to the lifting context.
	-> [VVar]	-- ^ Parameter vars for the abstraction.
	-> VExpr	-- ^ Body of the abstraction.
	-> VExpr

vLams lc vs (ve, le) 
  = (mkLams vvs ve, mkLams (lc:lvs) le)
100
101
102
  where
    (vvs,lvs) = unzip vs

103
104

-- | Like `vLams` but the lifted version doesn't bind the lifting context.
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
105
vLamsWithoutLC :: [VVar] -> VExpr -> VExpr
106
107
vLamsWithoutLC vvs (ve,le) 
  = (mkLams vs ve, mkLams ls le)
108
109
110
  where
    (vs,ls) = unzip vvs

111
112
113

-- | Apply some argument variables to an expression.
--   The lifted version is also applied to the variable of the lifting context.
rl@cse.unsw.edu.au's avatar
rl@cse.unsw.edu.au committed
114
vVarApps :: Var -> VExpr -> [VVar] -> VExpr
115
116
vVarApps lc (ve, le) vvs 
  = (ve `mkVarApps` vs, le `mkVarApps` (lc : ls))
117
118
119
  where
    (vs,ls) = unzip vvs 

120
121
122
123
124
125
126
127
128

vCaseDEFAULT
	:: VExpr	-- scrutiniy
	-> VVar		-- bnder
	-> Type		-- type of vectorised version
	-> Type		-- type of lifted version
	-> VExpr	-- body of alternative.
	-> VExpr

129
130
131
132
133
134
vCaseDEFAULT (vscrut, lscrut) (vbndr, lbndr) vty lty (vbody, lbody)
  = (Case vscrut vbndr vty (mkDEFAULT vbody),
     Case lscrut lbndr lty (mkDEFAULT lbody))
  where
    mkDEFAULT e = [(DEFAULT, [], e)]