-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-instance.hs
93 lines (73 loc) · 2.24 KB
/
class-instance.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
-- newtype Str = Str {
-- getStr :: String
-- } deriving (Show)
-- newtype Innnnt = Innnnt {
-- getInnnnt :: Integer
-- } deriving (Show)
-- -- data Str = Str String deriving (Show)
-- -- data Innnnt = Innnnt Integer deriving (Show)
-- newtype StrWith a = StrWith {
-- getStrWith :: (String, a)
-- } deriving (Show)
-- class Tatsumi m where
-- desc :: m -> (String, m)
-- instance Tatsumi Str where
-- desc x = ("hi string", x)
-- instance Tatsumi Innnnt where
-- desc x = ("hi integer", x)
-- instance Tatsumi (StrWith a) where
-- -- desc (StrWith x) = ("hi" ++ (fst $ x), StrWith x)
-- desc x = ("hi" ++ (fst $ getStrWith x), x)
-- newtype Lambda a b = Lambda {
-- getLambda :: (String, a) -> ((String, a), b)
-- }
-- class Add a where
-- plus :: a -> a -> a
-- instance Add Integer where
-- plus x y = x + y
-- instance Add Double where
-- plus x y = x + y
newtype IntWrap = IntWrap {
unwrap :: Integer
} deriving (Show)
class Couple m where
couple :: a -> (m, a)
instance Couple IntWrap where
couple x = (IntWrap 1, x)
-- newtype Stack a = Stack [a] deriving (Show)
-- newtype StackOp a b = StackOp {
-- run :: Stack a -> (b, Stack a)
-- }
-- class MyMonad m where
-- ret :: a -> m a
-- comb :: m a -> (a -> m b) -> m b
-- instance MyMonad (StackOp a) where
-- ret x = StackOp $ \stack -> (x, stack)
-- m `comb` n = StackOp $ \stack0 ->
-- let (x1, stack1) = run m stack0
-- (x2, stack2) = run (n x1) stack1
-- in (x2, stack2)
-- pop :: Stack a -> (a, Stack a)
-- pop (Stack (x:xs)) = (x, Stack xs)
main = do
print $ unwrap $ fst $ couple undefined
-- let f = (+1) $ fst $ (1, 2)
-- let s = (+1) $ snd $ wrap 20
-- ret = plus $ wrap 2
-- print $ plus (1 :: Integer) 2
-- print $ plus (1.1 :: Double) 2.0
-- let sample2 = Innnnt 1
-- let sample1 = Str "Tatsumi"
-- let sample3 = StrWith ("TatsumiWith", 1)
-- print $ sample1
-- print $ desc sample1
-- print $ sample2
-- print $ desc sample2
-- print $ sample3
-- print $ desc sample3
-- let sampleLambda = wrap 1
-- print $ sampleLambda ("hiLambda", False)
-- let s = Stack [1, 2, 3, 4, 5]
-- print $ run (StackOp $ topis (> 4)) s
-- print $ run (StackOp $ ret 1) s
-- print $ run (StackOp pop `comb` \i -> ret (i * 2)) $ s