-
Notifications
You must be signed in to change notification settings - Fork 1
/
8.hs
97 lines (72 loc) · 2.72 KB
/
8.hs
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
data Dyn = Fun (Dyn -> Dyn)
| App {
function :: Dyn,
argument :: Dyn
}
| Char Char
| Int Integer
reduce (App (Fun f) a) = f a
reduce (App a b) = reduce $ App (reduce a) b
instance Show Dyn where
show (Char a) = "Dynamic " ++ show a
show (Int a) = "Dynamic " ++ show a
show (Fun _) = "some function"
show a@(App f x) = show $ reduce a
instance Eq Dyn where
(==) (Char a) (Char b) = a == b
(==) (Int a) (Int b) = a == b
(==) (App (Fun f) a) b = (f a) == b
(==) b (App (Fun f) a) = (f a) == b
(==) _ _ = error "Not supported"
instance Ord Dyn where
(<=) (Char a) (Char b) = a <= b
(<=) (Int a) (Int b) = a <= b
(<=) (App (Fun f) a) b = (f a) <= b
(<=) b (App (Fun f) a) = b <= (f a)
(<=) _ _ = error "Not supported"
instance Num Dyn where
(+) (Int a) (Int b) = Int $ a + b
(+) (App (Fun f) a) b = (f a) + b
(+) b (App (Fun f) a) = (f a) + b
(+) _ _ = error "Not supported"
(*) (Int a) (Int b) = Int $ a * b
(*) (App (Fun f) a) b = (f a) * b
(*) b (App (Fun f) a) = (f a) * b
(*) _ _ = error "Not supported"
negate (Int a) = Int $ negate a
negate (App (Fun f) a) = negate $ f a
negate _ = error "Not supported"
signum (Int a) = Int $ signum a
signum (App (Fun f) a) = signum (f a)
signum _ = error "Not supported"
abs (Int a) = Int $ abs a
abs (App (Fun f) a) = abs (f a)
abs _ = error "Not supported"
fromInteger = Int
instance Enum Dyn where
toEnum = Int . toInteger
fromEnum (Char c) = fromEnum c
fromEnum (Int i) = fromEnum i
fromEnum (App (Fun f) a) = fromEnum $ f a
fromEnum _ = error "Not supported"
instance Real Dyn where
toRational (Int i) = toRational i
toRational (App (Fun f) a) = toRational $ f a
toRational _ = error "Not supported"
instance Integral Dyn where
toInteger (Int i) = i
toInteger (App (Fun f) a) = toInteger $ f a
toInteger _ = error "Not supported"
quotRem (Int a) (Int b) = let (c, d) = quotRem a b in (Int c, Int d)
quotRem (App (Fun f) a) b = quotRem (f a) b
quotRem a (App (Fun f) b) = quotRem a (f b)
quotRem _ _ = error "Not supported"
i :: Dyn
i = Fun id
k :: Dyn
k = Fun ( \a -> Fun $ \_ -> a )
s :: Dyn -> Dyn -> Dyn -> Dyn
s f1 f2 a = App (App f1 a) (App f2 a)
-- So it mean that s i i = w and w i = i
-- And it is:
test x = App (s i i i) x