Skip to content

Commit a3a97b3

Browse files
committed
Make column expressions printable.
1 parent c04db5e commit a3a97b3

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/DataFrame/Internal/Expression.hs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,83 +29,83 @@ import Data.Maybe (fromMaybe)
2929
data Expr a where
3030
Col :: Columnable a => T.Text -> Expr a
3131
Lit :: Columnable a => a -> Expr a
32-
Apply :: (Columnable a, Columnable b) => (b -> a) -> Expr b -> Expr a
33-
BinOp :: (Columnable c, Columnable b, Columnable a) => (c -> b -> a) -> Expr c -> Expr b -> Expr a
32+
Apply :: (Columnable a, Columnable b) => T.Text -> (b -> a) -> Expr b -> Expr a
33+
BinOp :: (Columnable c, Columnable b, Columnable a) => T.Text -> (c -> b -> a) -> Expr c -> Expr b -> Expr a
3434

3535
interpret :: forall a b . (Columnable a) => DataFrame -> Expr a -> TypedColumn a
3636
interpret df (Lit value) = TColumn $ toColumn' $ V.replicate (fst $ dataframeDimensions df) value
3737
interpret df (Col name) = case getColumn name df of
3838
Nothing -> throw $ ColumnNotFoundException name "" (map fst $ M.toList $ columnIndices df)
3939
Just col -> TColumn col
40-
interpret df (Apply (f :: c -> d) value) = let
40+
interpret df (Apply _ (f :: c -> d) value) = let
4141
(TColumn value') = interpret @c df value
4242
in TColumn $ fromMaybe (error "transform returned nothing") (transform f value')
43-
interpret df (BinOp (f :: c -> d -> e) left right) = let
43+
interpret df (BinOp _ (f :: c -> d -> e) left right) = let
4444
(TColumn left') = interpret @c df left
4545
(TColumn right') = interpret @d df right
4646
in TColumn $ zipWithColumns f left' right'
4747

4848
instance (Num a, Columnable a) => Num (Expr a) where
4949
(+) :: Expr a -> Expr a -> Expr a
50-
(+) = BinOp (+)
50+
(+) = BinOp "add" (+)
5151

5252
(*) :: Expr a -> Expr a -> Expr a
53-
(*) = BinOp (*)
53+
(*) = BinOp "mult" (*)
5454

5555
fromInteger :: Integer -> Expr a
5656
fromInteger = Lit . fromInteger
5757

5858
negate :: Expr a -> Expr a
59-
negate = Apply negate
59+
negate = Apply "negate" negate
6060

6161
abs :: Num a => Expr a -> Expr a
62-
abs = Apply abs
62+
abs = Apply "abs" abs
6363

6464
signum :: Num a => Expr a -> Expr a
65-
signum = Apply signum
65+
signum = Apply "signum" signum
6666

6767
instance (Fractional a, Columnable a) => Fractional (Expr a) where
6868
fromRational :: (Fractional a, Columnable a) => Rational -> Expr a
6969
fromRational = Lit . fromRational
7070

7171
(/) :: (Fractional a, Columnable a) => Expr a -> Expr a -> Expr a
72-
(/) = BinOp (/)
72+
(/) = BinOp "divide" (/)
7373

7474
instance (Floating a, Columnable a) => Floating (Expr a) where
7575
pi :: (Floating a, Columnable a) => Expr a
7676
pi = Lit pi
7777
exp :: (Floating a, Columnable a) => Expr a -> Expr a
78-
exp = Apply exp
78+
exp = Apply "exp" exp
7979
log :: (Floating a, Columnable a) => Expr a -> Expr a
80-
log = Apply log
80+
log = Apply "log" log
8181
sin :: (Floating a, Columnable a) => Expr a -> Expr a
82-
sin = Apply sin
82+
sin = Apply "sin" sin
8383
cos :: (Floating a, Columnable a) => Expr a -> Expr a
84-
cos = Apply cos
84+
cos = Apply "cos" cos
8585
asin :: (Floating a, Columnable a) => Expr a -> Expr a
86-
asin = Apply asin
86+
asin = Apply "asin" asin
8787
acos :: (Floating a, Columnable a) => Expr a -> Expr a
88-
acos = Apply acos
88+
acos = Apply "acos" acos
8989
atan :: (Floating a, Columnable a) => Expr a -> Expr a
90-
atan = Apply atan
90+
atan = Apply "atan" atan
9191
sinh :: (Floating a, Columnable a) => Expr a -> Expr a
92-
sinh = Apply sinh
92+
sinh = Apply "sinh" sinh
9393
cosh :: (Floating a, Columnable a) => Expr a -> Expr a
94-
cosh = Apply cosh
94+
cosh = Apply "cosh" cosh
9595
asinh :: (Floating a, Columnable a) => Expr a -> Expr a
96-
asinh = Apply sinh
96+
asinh = Apply "asinh" sinh
9797
acosh :: (Floating a, Columnable a) => Expr a -> Expr a
98-
acosh = Apply acosh
98+
acosh = Apply "acosh" acosh
9999
atanh :: (Floating a, Columnable a) => Expr a -> Expr a
100-
atanh = Apply atanh
100+
atanh = Apply "atanh" atanh
101101

102102

103103
instance (Show a) => Show (Expr a) where
104-
show :: Show a => Expr a -> String
105-
show (Col name) = "col(" ++ T.unpack name ++ ")"
104+
show :: forall a . Show a => Expr a -> String
105+
show (Col name) = "col@" ++ show (typeRep @a) ++ "(" ++ T.unpack name ++ ")"
106106
show (Lit value) = show value
107-
show (Apply f value) = "apply(" ++ show value ++ ")"
108-
show (BinOp f a b) = "binop(" ++ show a ++ ", " ++ show b ++ ")"
107+
show (Apply name f value) = T.unpack name ++ "(" ++ show value ++ ")"
108+
show (BinOp name f a b) = T.unpack name ++ "(" ++ show a ++ ", " ++ show b ++ ")"
109109

110110
col :: Columnable a => T.Text -> Expr a
111111
col = Col
@@ -114,22 +114,22 @@ lit :: Columnable a => a -> Expr a
114114
lit = Lit
115115

116116
lift :: (Columnable a, Columnable b) => (a -> b) -> Expr a -> Expr b
117-
lift = Apply
117+
lift = Apply "udf"
118118

119119
lift2 :: (Columnable c, Columnable b, Columnable a) => (c -> b -> a) -> Expr c -> Expr b -> Expr a
120-
lift2 = BinOp
120+
lift2 = BinOp "udf"
121121

122122
eq :: (Columnable a, Eq a) => Expr a -> Expr a -> Expr Bool
123-
eq = BinOp (==)
123+
eq = BinOp "eq" (==)
124124

125125
lt :: (Columnable a, Ord a) => Expr a -> Expr a -> Expr Bool
126-
lt = BinOp (<)
126+
lt = BinOp "lt" (<)
127127

128128
gt :: (Columnable a, Ord a) => Expr a -> Expr a -> Expr Bool
129-
gt = BinOp (<)
129+
gt = BinOp "gt" (<)
130130

131131
leq :: (Columnable a, Ord a, Eq a) => Expr a -> Expr a -> Expr Bool
132-
leq = BinOp (<=)
132+
leq = BinOp "leq" (<=)
133133

134134
geq :: (Columnable a, Ord a, Eq a) => Expr a -> Expr a -> Expr Bool
135-
geq = BinOp (<=)
135+
geq = BinOp "geq" (<=)

0 commit comments

Comments
 (0)