1 module Database.RRDtool.Expression
3 , ApplyMentionedVarsOf(..)
14 , IsShortEnoughForVarName
15 , IsGoodLetterForVarName
25 , VariableShiftPredictOp(..)
26 , FixedShiftPredictOp(..)
29 , IterativeValueOf(..)
30 , AggregativeUnaryOp(..)
35 import Data.HList.String
36 import Types.Data.Bool
37 import Types.Data.Num hiding ((:*:))
42 class IsVariableSet (MentionedVarsOf a) => MentionedVars a where
43 type MentionedVarsOf a
45 -- ApplyMentionedVarsOf
46 data ApplyMentionedVarsOf = ApplyMentionedVarsOf
48 instance Applyable ApplyMentionedVarsOf a where
49 type Apply ApplyMentionedVarsOf a = MentionedVarsOf a
53 class (Show e, Eq e) => IsExpr e
54 class IsExpr e => IsCommonExpr e
55 class IsExpr e => IterativeExpr e
56 class IsExpr e => IsAggregativeExpr e
58 class (Show es, Eq es, HList es) => IsExprSet es
59 instance IsExprSet HNil
60 instance (IsExpr e, IsExprSet es) => IsExprSet (HCons e es)
62 class (Show es, Eq es, HList es) => IsCommonExprSet es
63 instance IsCommonExprSet HNil
64 instance (IsCommonExpr e, IsCommonExprSet es) => IsCommonExprSet (HCons e es)
67 -- Constants and variable names
70 deriving (Show, Eq, Ord)
71 instance IsExpr Constant
72 instance IsCommonExpr Constant
73 instance MentionedVars Constant where
74 type MentionedVarsOf Constant = HNil
76 {- This is what we want to do but GHC can't handle this for now.
77 class ( (HLengthOf str :<=: D255) ~ True
82 class ( IsShortEnoughForVarName str
87 class HString str => IsShortEnoughForVarName str
88 instance ( HString str
89 , (HLength str :<=: D255) ~ True
91 => IsShortEnoughForVarName str
93 class IntegerT c => GoodLetterForVarName c where
94 type IsGoodLetterForVarName c
96 instance IntegerT c => GoodLetterForVarName c where
97 type IsGoodLetterForVarName c = ( (c :>=: D65) :&&: (c :<=: D90) ) -- A-Z
99 ( (c :>=: D99) :&&: (c :<=: D122) ) -- a-z
101 ( c :==: D45 ) -- '-'
103 ( c :==: D95 ) -- '_'
105 --instance (a :>=: D65) ~ True => IsGoodLetterForVarName a
110 deriving (Show, Eq, Ord)
112 instance IsVarName vn => IsExpr (Variable vn)
113 instance IsVarName vn => IsCommonExpr (Variable vn)
114 instance IsVarName vn => MentionedVars (Variable vn) where
115 type MentionedVarsOf (Variable vn) = vn :*: HNil
117 class HList vs => IsVariableSet vs
118 instance IsVariableSet HNil
119 instance (IsVarName v, IsVariableSet vs) => IsVariableSet (HCons v vs)
136 deriving (Show, Eq, Ord)
137 instance IsExpr a => IsExpr (CommonUnaryOp a)
138 instance IsCommonExpr a => IsCommonExpr (CommonUnaryOp a)
139 instance IsVariableSet (MentionedVarsOf a) => MentionedVars (CommonUnaryOp a) where
140 type MentionedVarsOf (CommonUnaryOp a) = MentionedVarsOf a
142 data CommonBinaryOp a b
158 deriving (Show, Eq, Ord)
160 instance (IsExpr a, IsExpr b) =>
161 IsExpr (CommonBinaryOp a b)
163 instance (IsCommonExpr a, IsCommonExpr b) =>
164 IsCommonExpr (CommonBinaryOp a b)
166 instance IsVariableSet (MentionedVarsOf a :++: MentionedVarsOf b) =>
167 MentionedVars (CommonBinaryOp a b) where
168 type MentionedVarsOf (CommonBinaryOp a b)
169 = MentionedVarsOf a :++: MentionedVarsOf b
172 data CommonTrinaryOp a b c
175 deriving (Show, Eq, Ord)
177 instance (IsExpr a, IsExpr b, IsExpr c)
178 => IsExpr (CommonTrinaryOp a b c)
180 instance (IsCommonExpr a, IsCommonExpr b, IsCommonExpr c)
181 => IsCommonExpr (CommonTrinaryOp a b c)
183 instance IsVariableSet (MentionedVarsOf a :++:
184 MentionedVarsOf b :++:
185 MentionedVarsOf c) =>
186 MentionedVars (CommonTrinaryOp a b c) where
187 type MentionedVarsOf (CommonTrinaryOp a b c)
188 = MentionedVarsOf a :++:
189 MentionedVarsOf b :++:
192 -- SORT and REV can't be expressed in this way as they pushes possibly
193 -- multiple values onto the stack...
197 deriving (Show, Eq, Ord)
199 instance IsExprSet es => IsExpr (CommonSetOp es)
200 instance (IsExprSet es, IsCommonExprSet es) => IsCommonExpr (CommonSetOp es)
201 instance IsVariableSet (HConcat (HMap ApplyMentionedVarsOf es)) =>
202 MentionedVars (CommonSetOp es) where
203 type MentionedVarsOf (CommonSetOp es)
204 = HConcat (HMap ApplyMentionedVarsOf es)
207 = Trend !(Variable vn) !a
208 | TrendNan !(Variable vn) !a
209 deriving (Show, Eq, Ord)
210 instance (IsVarName vn, IsExpr a) => IsExpr (TrendOp vn a)
211 instance (IsVarName vn, IsCommonExpr a) => IsCommonExpr (TrendOp vn a)
213 instance ( IsVariableSet (vn :*: MentionedVarsOf a)
214 ) => MentionedVars (TrendOp vn a) where
215 type MentionedVarsOf (TrendOp vn a) = vn :*: MentionedVarsOf a
217 data VariableShiftPredictOp ss w vn
218 = VariableShiftPredictAverage !ss !w !(Variable vn)
219 | VariableShiftPredictSigma !ss !w !(Variable vn)
220 deriving (Show, Eq, Ord)
221 instance (IsExprSet ss, IsExpr w, IsVarName vn)
222 => IsExpr (VariableShiftPredictOp ss w vn)
223 instance (IsExprSet ss, IsCommonExprSet ss, IsCommonExpr w, IsVarName vn)
224 => IsCommonExpr (VariableShiftPredictOp ss w vn)
225 instance ( IsVariableSet (vn :*: (MentionedVarsOf ss :++: MentionedVarsOf w))
226 ) => MentionedVars (VariableShiftPredictOp ss w vn) where
227 type MentionedVarsOf (VariableShiftPredictOp ss w vn)
228 = vn :*: (MentionedVarsOf ss :++: MentionedVarsOf w)
230 -- FixedShiftPredictOp
231 data FixedShiftPredictOp sm w vn
232 = FixedShiftPredictAverage !sm !w !(Variable vn)
233 | FixedShiftPredictSigma !sm !w !(Variable vn)
234 deriving (Show, Eq, Ord)
236 instance (IsExpr sm, IsExpr w, IsVarName vn)
237 => IsExpr (FixedShiftPredictOp sm w vn)
239 instance (IsCommonExpr sm, IsCommonExpr w, IsVarName vn)
240 => IsCommonExpr (FixedShiftPredictOp sm w vn)
242 instance ( IsVariableSet (vn :*: (MentionedVarsOf sm :++: MentionedVarsOf w))
243 ) => MentionedVars (FixedShiftPredictOp sm w vn) where
244 type MentionedVarsOf (FixedShiftPredictOp sm w vn)
245 = vn :*: (MentionedVarsOf sm :++: MentionedVarsOf w)
247 -- Common special values
253 deriving (Show, Eq, Ord)
255 instance IsExpr CommonValue
257 instance IsCommonExpr CommonValue
259 instance MentionedVars CommonValue where
260 type MentionedVarsOf CommonValue = HNil
262 -- Iterative special values
268 deriving (Show, Eq, Ord)
270 instance IsExpr IterativeValue
272 instance IterativeExpr IterativeValue
274 instance MentionedVars IterativeValue where
275 type MentionedVarsOf IterativeValue = HNil
277 -- Iterative special values of something
278 data IterativeValueOf vn
279 = PreviousOf !(Variable vn)
280 deriving (Show, Eq, Ord)
282 instance IsVarName vn => IsExpr (IterativeValueOf vn)
284 instance IsVarName vn => IterativeExpr (IterativeValueOf vn)
286 instance IsVarName vn => MentionedVars (IterativeValueOf vn) where
287 type MentionedVarsOf (IterativeValueOf vn) = vn :*: HNil
289 -- Aggregative operators (fairly restricted due to rrdtool's
291 data AggregativeUnaryOp vn
292 = Maximum !(Variable vn)
293 | Minimum !(Variable vn)
294 | Average !(Variable vn)
295 | StandardDeviation !(Variable vn)
296 | First !(Variable vn)
297 | Last !(Variable vn)
298 | Total !(Variable vn)
299 | Percent !(Variable vn) !Constant
300 | PercentNan !(Variable vn) !Constant
301 | LSLSlope !(Variable vn)
302 | LSLInt !(Variable vn)
303 | LSLCorrel !(Variable vn)
304 deriving (Show, Eq, Ord)
306 instance IsVarName vn => IsExpr (AggregativeUnaryOp vn)
308 instance IsVarName vn => IsAggregativeExpr (AggregativeUnaryOp vn)
310 instance IsVarName vn => MentionedVars (AggregativeUnaryOp vn) where
311 type MentionedVarsOf (AggregativeUnaryOp vn) = vn :*: HNil