@@ -16,49 +16,38 @@ import prelude.char
16class Show a where
16class Show a where
17 show : a -> String
17 show : a -> String
20instance Show Nat where
19instance Show Nat where
21 show O = "O"
20 show O = "O"
22 show (S k) = "s" ++ show k
21 show (S k) = "s" ++ show k
25instance Show Int where
23instance Show Int where
26 show = prim__intToStr
24 show = prim__intToStr
29instance Show Integer where
26instance Show Integer where
30 show = prim__bigIntToStr
27 show = prim__bigIntToStr
33instance Show Float where
29instance Show Float where
34 show = prim__floatToStr
30 show = prim__floatToStr
37instance Show Char where
32instance Show Char where
38 show x = strCons x ""
33 show x = strCons x ""
41instance Show String where
35instance Show String where
45instance Show Bool where
38instance Show Bool where
46 show True = "True"
39 show True = "True"
47 show False = "False"
40 show False = "False"
50instance (Show a, Show b) => Show (a, b) where
42instance (Show a, Show b) => Show (a, b) where
51 show (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
43 show (x, y) = "(" ++ show x ++ ", " ++ show y ++ ")"
54instance Show a => Show (List a) where
45instance Show a => Show (List a) where
55 show xs = "[" ++ show' xs ++ "]" where
46 show xs = "[" ++ show' xs ++ "]" where
56 show' : Show a => List a -> String
47 show' : Show a => List a -> String
57 show' [] = ""
48 show' [] = ""
58 show' [x] = show x
49 show' [x] = show x
59 show' (x :: xs) = show x ++ ", " ++ show' xs
50 show' (x :: xs) = show x ++ ", " ++ show' xs
63instance Show a => Show (Vect a n) where
52instance Show a => Show (Vect a n) where
64 show xs = "[" ++ show' xs ++ "]" where
53 show xs = "[" ++ show' xs ++ "]" where
@@ -66,14 +55,11 @@ instance Show a => Show (Vect a n) where
66 show' [] = ""
55 show' [] = ""
67 show' [x] = show x
56 show' [x] = show x
68 show' (x :: xs) = show x ++ ", " ++ show' xs
57 show' (x :: xs) = show x ++ ", " ++ show' xs
72instance Show a => Show (Maybe a) where
59instance Show a => Show (Maybe a) where
73 show Nothing = "Nothing"
60 show Nothing = "Nothing"
74 show (Just x) = "Just " ++ show x
61 show (Just x) = "Just " ++ show x
77---- Monad and instances
63---- Monad and instances
79infixl 5 >>=
65infixl 5 >>=
@@ -82,28 +68,20 @@ class Monad (m : Set -> Set) where
82 return : a -> m a
68 return : a -> m a
83 (>>=) : m a -> (a -> m b) -> m b
69 (>>=) : m a -> (a -> m b) -> m b
86class Monad m => MonadPlus (m : Set -> Set) where
71class Monad m => MonadPlus (m : Set -> Set) where
87 mplus : m a -> m a -> m a
72 mplus : m a -> m a -> m a
88 mzero : m a
73 mzero : m a
91−class Monad m => MonadFail (m : Set -> Set) where
92− fail : String -> m ()
95instance Monad IO where
75instance Monad IO where
96 return t = io_return t
76 return t = io_return t
97 b >>= k = io_bind b k
77 b >>= k = io_bind b k
100instance Monad Maybe where
79instance Monad Maybe where
101 return t = Just t
80 return t = Just t
103 Nothing >>= k = Nothing
82 Nothing >>= k = Nothing
104 (Just x) >>= k = k x
83 (Just x) >>= k = k x
107instance MonadPlus Maybe where
85instance MonadPlus Maybe where
108 mzero = Nothing
86 mzero = Nothing
@@ -111,17 +89,14 @@ instance MonadPlus Maybe where
111 mplus Nothing (Just y) = Just y
89 mplus Nothing (Just y) = Just y
112 mplus Nothing Nothing = Nothing
90 mplus Nothing Nothing = Nothing
115instance Monad List where
92instance Monad List where
116 return x = [x]
93 return x = [x]
117 m >>= f = concatMap f m
94 m >>= f = concatMap f m
120instance MonadPlus List where
96instance MonadPlus List where
121 mzero = []
97 mzero = []
122 mplus = app
98 mplus = app
125guard : MonadPlus m => Bool -> m ()
100guard : MonadPlus m => Bool -> m ()
126guard True = return ()
101guard True = return ()
127guard False = mzero
102guard False = mzero
@@ -131,16 +106,13 @@ guard False = mzero
131class Functor (f : Set -> Set) where
106class Functor (f : Set -> Set) where
132 fmap : (a -> b) -> f a -> f b
107 fmap : (a -> b) -> f a -> f b
135instance Functor Maybe where
109instance Functor Maybe where
136 fmap f (Just x) = Just (f x)
110 fmap f (Just x) = Just (f x)
137 fmap f Nothing = Nothing
111 fmap f Nothing = Nothing
140instance Functor List where
113instance Functor List where
141 fmap = map
114 fmap = map
144---- Applicative functors/Idioms
116---- Applicative functors/Idioms
146infixl 2 <$>
118infixl 2 <$>
@@ -149,7 +121,6 @@ class Functor f => Applicative (f : Set -> Set) where
149 pure : a -> f a
121 pure : a -> f a
150 (<$>) : f (a -> b) -> f a -> f b
122 (<$>) : f (a -> b) -> f a -> f b
153---- some mathematical operations
124---- some mathematical operations
155%include "math.h"
126%include "math.h"
@@ -191,7 +162,6 @@ floor x = prim__floatFloor x
191ceiling : Float -> Float
162ceiling : Float -> Float
192ceiling x = prim__floatCeil x
163ceiling x = prim__floatCeil x
197count : Num a => a -> a -> a -> List a
167count : Num a => a -> a -> a -> List a
@@ -244,7 +214,6 @@ openFile f m = fopen f (modeStr m) where
244 modeStr Write = "w"
214 modeStr Write = "w"
245 modeStr ReadWrite = "r+"
215 modeStr ReadWrite = "r+"
248do_fclose : Ptr -> IO ()
217do_fclose : Ptr -> IO ()
249do_fclose h = mkForeign (FFun "fileClose" [FPtr] FUnit) h
218do_fclose h = mkForeign (FFun "fileClose" [FPtr] FUnit) h
@@ -288,6 +257,4 @@ readFile fn = do h <- openFile fn Read
288 readFile' h (contents ++ l)
257 readFile' h (contents ++ l)
290 else (return contents)
259 else (return contents)