Ticket #102: Tst.2.hs

File Tst.2.hs, 0.7 KB (added by alar, 10 years ago)
Line 
1data    Object = Operatortype Operator | Integertype Integer | Realtype Float   deriving (Eq,Show)
2type    Stack = [Object]
3type    Operator= (Stack->Stack) 
4instance Eq Operator where
5        _ == _ = error "typecheck"
6instance Show Operator where
7        show _ = error "can't show operator!"
8instance Num Object where
9        (Integertype a) + (Integertype b) = Integertype (a+b)
10        (Realtype a) + (Realtype b) = Realtype (a + b)
11        (Realtype a) + (Integertype b) = Realtype (a + fromInteger(b) )
12        (Integertype b) + (Realtype a) = Realtype (a + fromInteger(b) )
13        _ + _ = error "typecheck"
14apply2:: (Object->Object->Object)->Stack->Stack
15apply2 op (s0:(s1:s2)) = ((op s0 s1):s2)
16
17add:: Operator
18add = apply2 (+)
19sub:: Operator
20sub =  apply2 (-)