aboutsummaryrefslogtreecommitdiff
path: root/src/atom.hs
blob: d01b157dae405799ddde935cfafdddcd7d010e40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Atom where


data Atom
    = ARational Float
    | AImaginary Float

infixl 6 +?
(+?) :: Atom -> Atom -> Maybe Atom
(ARational a) +? (ARational b) = Just $ ARational (a + b)
(AImaginary a) +? (AImaginary b) = Just $ AImaginary (a + b)
_ +? _ = Nothing

infixl 6 -?
(-?) :: Atom -> Atom -> Maybe Atom
(ARational a) -? (ARational b) = Just $ ARational (a - b)
(AImaginary a) -? (AImaginary b) = Just $ AImaginary (a - b)
_ -? _ = Nothing

infixl 7 *?
(*?) :: Atom -> Atom -> Maybe Atom
(ARational a) *? (ARational b) = Just $ ARational (a * b)
(ARational a) *? (AImaginary b) = Just $ AImaginary (a * b)
(AImaginary a) *? (AImaginary b) = (AImaginary (a * b)) ^? ARational 2
_ *? _ = Nothing

infixl 7 /?
(/?) :: Atom -> Atom -> Maybe Atom
_ /? (ARational 0) = Nothing
(ARational a) /? (ARational b) = Just $ ARational (a / b)
_ /? _ = Nothing

infixr 8 ^?
(^?) :: Atom -> Atom -> Maybe Atom
(ARational a) ^? (ARational b) = Just $ ARational (a ** b)
(AImaginary a) ^? (ARational 0) = Just $ ARational a
(AImaginary a) ^? (ARational 1) = Just $ AImaginary a
(AImaginary a) ^? (ARational 2) = Just $ ARational (-a)
(AImaginary a) ^? (ARational 3) = Just $ AImaginary (-a)
(AImaginary a) ^? (ARational b) = AImaginary a ^? (ARational (b - 4))
_ ^? _ = Nothing

instance Show Atom where
    show (ARational r) = show r
    show (AImaginary i) = show i ++ "i"