| 1 |
asTypeIn |
| 2 |
a `asTypeIn` f = a where _ = f a |
| 3 |
infixl 0 `asTypeIn` |
| 4 |
|
| 5 |
asAppliedTo |
| 6 |
f `asAppliedTo` a = f where _ = f a |
| 7 |
infixl 0 `asAppliedTo` |
| 8 |
|
| 9 |
Mu |
| 10 |
newtype Mu f = In { out :: f (Mu f) } |
| 11 |
|
| 12 |
In |
| 13 |
newtype Mu f = In { out :: f (Mu f) } |
| 14 |
|
| 15 |
out |
| 16 |
newtype Mu f = In { out :: f (Mu f) } |
| 17 |
|
| 18 |
Rec |
| 19 |
newtype Rec a = InR { outR :: Rec a -> a } |
| 20 |
|
| 21 |
InR |
| 22 |
newtype Rec a = InR { outR :: Rec a -> a } |
| 23 |
|
| 24 |
outR |
| 25 |
newtype Rec a = InR { outR :: Rec a -> a } |
| 26 |
|
| 27 |
(=<<) |
| 28 |
f =<< x = x >>= f |
| 29 |
|
| 30 |
Reader |
| 31 |
type Reader r = ReaderT r Identity |
| 32 |
--OR |
| 33 |
data Reader r a = Reader { runReader :: r -> a } |
| 34 |
|
| 35 |
Reader return |
| 36 |
return a = Reader $ \_ -> a |
| 37 |
|
| 38 |
Reader (>>=) |
| 39 |
m >>= k = Reader $ \r -> |
| 40 |
let a = runReader m r |
| 41 |
in runReader (k a) r |
| 42 |
|
| 43 |
Writer |
| 44 |
type Writer w = WriterT w Identity |
| 45 |
--OR |
| 46 |
data Writer w a = Writer { runWriter :: (a, w) } |
| 47 |
|
| 48 |
Writer return |
| 49 |
return a = Writer (a, mempty) |
| 50 |
|
| 51 |
Writer (>>=) |
| 52 |
m >>= k = Writer $ |
| 53 |
let (a, w) = runWriter m |
| 54 |
(b, w') = runWriter (k a) |
| 55 |
in (b, w <> w') |
| 56 |
|
| 57 |
State |
| 58 |
type State s = StateT s Identity |
| 59 |
--OR |
| 60 |
data State s a = State { runState :: s -> (a, s) } |
| 61 |
|
| 62 |
State return |
| 63 |
return a = State $ \s -> (a, s) |
| 64 |
|
| 65 |
State (>>=) |
| 66 |
m >>= k = State $ \s -> |
| 67 |
let (a, s') = runState m s |
| 68 |
in runState (k a) s' |
| 69 |
|
| 70 |
sequence |
| 71 |
sequence [] = return [] |
| 72 |
sequence (x:xs) = do v <- x; vs <- sequence xs; return (v:vs) |
| 73 |
--OR |
| 74 |
sequence xs = foldr (liftM2 (:)) (return []) xs |
| 75 |
|
| 76 |
sequence_ |
| 77 |
sequence_ ms = foldr (>>) (return ()) ms |
| 78 |
|
| 79 |
mapM |
| 80 |
mapM f as = sequence (map f as) |
| 81 |
|
| 82 |
mapM_ |
| 83 |
mapM_ f as = sequence_ (map f as) |
| 84 |
|
| 85 |
guard |
| 86 |
guard True = pure () |
| 87 |
guard False = empty |
| 88 |
|
| 89 |
forM |
| 90 |
forM = flip mapM |
| 91 |
|
| 92 |
forM_ |
| 93 |
forM_ = flip mapM_ |
| 94 |
|
| 95 |
msum |
| 96 |
msum = foldr mplus mzero |
| 97 |
|
| 98 |
join |
| 99 |
join x = x >>= id |
| 100 |
|
| 101 |
forever |
| 102 |
forever a = let a' = a >> a' in a' |
| 103 |
|
| 104 |
void |
| 105 |
void = fmap (const ()) |
| 106 |
|
| 107 |
mapAndUnzipM |
| 108 |
mapAndUnzipM f xs = sequence (map f xs) >>= return . unzip |
| 109 |
|
| 110 |
zipWithM |
| 111 |
zipWithM f xs ys = sequence (zipWith f xs ys) |
| 112 |
|
| 113 |
zipWithM_ |
| 114 |
zipWithM_ f xs ys = sequence_ (zipWith f xs ys) |
| 115 |
|
| 116 |
foldM |
| 117 |
foldM _ a [] = return a |
| 118 |
foldM f a (x:xs) = f a x >>= \fax -> foldM f fax xs |
| 119 |
|
| 120 |
foldM_ |
| 121 |
foldM_ f a xs = foldM f a xs >> return () |
| 122 |
|
| 123 |
replicateM |
| 124 |
replicateM n x = sequence (replicate n x) |
| 125 |
|
| 126 |
replicateM_ |
| 127 |
replicateM_ n x = sequence_ (replicate n x) |
| 128 |
|
| 129 |
when |
| 130 |
when p s = if p then s else return () |
| 131 |
|
| 132 |
unless |
| 133 |
unless p s = if p then return () else s |
| 134 |
|
| 135 |
liftM |
| 136 |
liftM f m1 = do |
| 137 |
x1 <- m1 |
| 138 |
return (f x1) |
| 139 |
|
| 140 |
liftM2 |
| 141 |
liftM2 f m1 m2 = do |
| 142 |
x1 <- m1 |
| 143 |
x2 <- m2 |
| 144 |
return (f x1 x2) |
| 145 |
|
| 146 |
liftM3 |
| 147 |
liftM3 f m1 m2 m3 = do |
| 148 |
x1 <- m1 |
| 149 |
x2 <- m2 |
| 150 |
x3 <- m3 |
| 151 |
return (f x1 x2 x3) |
| 152 |
|
| 153 |
liftM4 |
| 154 |
liftM4 f m1 m2 m3 m4 = do |
| 155 |
x1 <- m1 |
| 156 |
x2 <- m2 |
| 157 |
x3 <- m3 |
| 158 |
x4 <- m4 |
| 159 |
return (f x1 x2 x3 x4) |
| 160 |
|
| 161 |
ap |
| 162 |
ap = liftM2 id |
| 163 |
|
| 164 |
(>>) |
| 165 |
m >> k = m >>= \_ -> k |
| 166 |
|
| 167 |
fail |
| 168 |
fail s = error s |
| 169 |
|
| 170 |
compare |
| 171 |
compare x y | x == y = EQ |
| 172 |
| x <= y = LT |
| 173 |
| otherwise = GT |
| 174 |
|
| 175 |
comparing |
| 176 |
comparing p x y = compare (p x) (p y) |
| 177 |
|
| 178 |
(/=) |
| 179 |
x /= y = not (x == y) |
| 180 |
|
| 181 |
(==) |
| 182 |
x == y = not (x /= y) |
| 183 |
|
| 184 |
(<) |
| 185 |
x < y = case compare x y of |
| 186 |
LT -> True |
| 187 |
_ -> False |
| 188 |
|
| 189 |
(<=) |
| 190 |
x <= y = case compare x y of |
| 191 |
GT -> False |
| 192 |
_ -> True |
| 193 |
|
| 194 |
(>) |
| 195 |
x > y = case compare x y of |
| 196 |
GT -> True |
| 197 |
_ -> False |
| 198 |
|
| 199 |
(>=) |
| 200 |
x >= y = case compare x y of |
| 201 |
LT -> False |
| 202 |
_ -> True |
| 203 |
|
| 204 |
max |
| 205 |
max x y = if x <= y then y else x |
| 206 |
|
| 207 |
min |
| 208 |
min x y = if x <= y then x else y |
| 209 |
|
| 210 |
[] |
| 211 |
data [] a = [] | a : [a] |
| 212 |
|
| 213 |
foldr |
| 214 |
foldr f z [] = z |
| 215 |
foldr f z (x:xs) = f x (foldr f z xs) |
| 216 |
|
| 217 |
build |
| 218 |
build g = g (:) [] |
| 219 |
|
| 220 |
augment |
| 221 |
augment g xs = g (:) xs |
| 222 |
|
| 223 |
map |
| 224 |
map _ [] = [] |
| 225 |
map f (x:xs) = f x : map f xs |
| 226 |
|
| 227 |
(++) |
| 228 |
[] ++ ys = ys |
| 229 |
(x:xs) ++ ys = x : (xs ++ ys) |
| 230 |
-- OR |
| 231 |
xs ++ ys = foldr (:) ys xs |
| 232 |
|
| 233 |
Bool |
| 234 |
data Bool = False | True deriving (Eq, Ord) |
| 235 |
|
| 236 |
(&&) |
| 237 |
True && x = x |
| 238 |
False && _ = False |
| 239 |
|
| 240 |
(||) |
| 241 |
True || _ = True |
| 242 |
False || x = x |
| 243 |
|
| 244 |
not |
| 245 |
not True = False |
| 246 |
not False = True |
| 247 |
|
| 248 |
otherwise |
| 249 |
otherwise = True |
| 250 |
|
| 251 |
bool |
| 252 |
bool f _ False = f |
| 253 |
bool _ t True = t |
| 254 |
|
| 255 |
() |
| 256 |
data () = () |
| 257 |
|
| 258 |
Ordering |
| 259 |
data Ordering = LT | EQ | GT |
| 260 |
|
| 261 |
String |
| 262 |
type String = [Char] |
| 263 |
|
| 264 |
Char |
| 265 |
data Char = C# Char# |
| 266 |
|
| 267 |
Int |
| 268 |
data Int = I# Int# |
| 269 |
|
| 270 |
id |
| 271 |
id x = x |
| 272 |
|
| 273 |
const |
| 274 |
const x _ = x |
| 275 |
|
| 276 |
(.) |
| 277 |
(f . g) x = f (g x) |
| 278 |
|
| 279 |
flip |
| 280 |
flip f x y = f y x |
| 281 |
|
| 282 |
($) |
| 283 |
f $ x = f x |
| 284 |
|
| 285 |
($!) |
| 286 |
f $! x = x `seq` f x |
| 287 |
|
| 288 |
until |
| 289 |
until p f x | p x = x |
| 290 |
| otherwise = until p f (f x) |
| 291 |
|
| 292 |
asTypeOf |
| 293 |
asTypeOf = const |
| 294 |
|
| 295 |
head |
| 296 |
head (x:_) = x |
| 297 |
head [] = error "Prelude.head: empty list" |
| 298 |
|
| 299 |
tail |
| 300 |
tail (_:xs) = xs |
| 301 |
tail [] = error "Prelude.tail: empty list" |
| 302 |
|
| 303 |
last |
| 304 |
last [x] = x |
| 305 |
last (_:xs) = last xs |
| 306 |
last [] = error "Prelude.last: empty list" |
| 307 |
|
| 308 |
init |
| 309 |
init [x] = [] |
| 310 |
init (x:xs) = x : init xs |
| 311 |
init [] = error "Prelude.init: empty list" |
| 312 |
|
| 313 |
null |
| 314 |
null [] = True |
| 315 |
null (_:_) = False |
| 316 |
|
| 317 |
filter |
| 318 |
filter _ [] = [] |
| 319 |
filter p (x:xs) |
| 320 |
| p x = x : filter p xs |
| 321 |
| otherwise = filter p xs |
| 322 |
|
| 323 |
foldl |
| 324 |
foldl f z [] = z |
| 325 |
foldl f z (x:xs) = foldl f (f z x) xs |
| 326 |
|
| 327 |
scanr |
| 328 |
scanr _ q0 [] = [q0] |
| 329 |
scanr f q0 (x:xs) = f x q : qs |
| 330 |
where qs@(q:_) = scanr f q0 xs |
| 331 |
|
| 332 |
iterate |
| 333 |
iterate f x = x : iterate f (f x) |
| 334 |
|
| 335 |
repeat |
| 336 |
repeat x = xs where xs = x : xs |
| 337 |
|
| 338 |
replicate |
| 339 |
replicate n x = take n (repeat x) |
| 340 |
|
| 341 |
cycle |
| 342 |
cycle [] = undefined |
| 343 |
cycle xs = xs' where xs' = xs ++ xs' |
| 344 |
|
| 345 |
takeWhile |
| 346 |
takeWhile _ [] = [] |
| 347 |
takeWhile p (x:xs) | p x = x : takeWhile p xs |
| 348 |
| otherwise = [] |
| 349 |
|
| 350 |
take |
| 351 |
take n _ | n <= 0 = [] |
| 352 |
take _ [] = [] |
| 353 |
take n (x:xs) = x : take (n-1) xs |
| 354 |
|
| 355 |
dropWhile |
| 356 |
dropWhile _ [] = [] |
| 357 |
dropWhile p (x:xs) | p x = dropWhile p xs |
| 358 |
| otherwise = x:xs |
| 359 |
|
| 360 |
drop |
| 361 |
drop n xs | n <= 0 = xs |
| 362 |
drop _ [] = [] |
| 363 |
drop n (_:xs) = drop (n-1) xs |
| 364 |
|
| 365 |
stripPrefix |
| 366 |
stripPrefix [] ys = Just ys |
| 367 |
stripPrefix (x:xs) (y:ys) | x == y = stripPrefix xs ys |
| 368 |
stripPrefix _ _ = Nothing |
| 369 |
|
| 370 |
splitAt |
| 371 |
splitAt n xs = (take n xs, drop n xs) |
| 372 |
|
| 373 |
break |
| 374 |
break p = span (not . p) |
| 375 |
|
| 376 |
span |
| 377 |
span _ xs@[] = (xs, xs) |
| 378 |
span p xs@(x:xs') | p x = let (ys,zs) = span p xs' in (x:ys,zs) |
| 379 |
| otherwise = ([],xs) |
| 380 |
|
| 381 |
reverse |
| 382 |
reverse = foldl (flip (:)) [] |
| 383 |
|
| 384 |
and |
| 385 |
and = foldr (&&) True |
| 386 |
|
| 387 |
or |
| 388 |
or = foldr (||) False |
| 389 |
|
| 390 |
any |
| 391 |
any p = or . map p |
| 392 |
|
| 393 |
all |
| 394 |
all p = and . map p |
| 395 |
|
| 396 |
elem |
| 397 |
elem x = any (== x) |
| 398 |
|
| 399 |
notElem |
| 400 |
notElem x = all (/= x) |
| 401 |
|
| 402 |
lookup |
| 403 |
lookup _key [] = Nothing |
| 404 |
lookup key ((x,y):xys) | key == x = Just y |
| 405 |
| otherwise = lookup key xys |
| 406 |
|
| 407 |
concatMap |
| 408 |
concatMap f = foldr ((++) . f) [] |
| 409 |
|
| 410 |
concat |
| 411 |
concat = foldr (++) [] |
| 412 |
|
| 413 |
(!!) |
| 414 |
xs !! n | n < 0 = undefined |
| 415 |
[] !! _ = undefined |
| 416 |
(x:_) !! 0 = x |
| 417 |
(_:xs) !! n = xs !! (n-1) |
| 418 |
|
| 419 |
zip |
| 420 |
zip (a:as) (b:bs) = (a,b) : zip as bs |
| 421 |
zip _ _ = [] |
| 422 |
|
| 423 |
zip3 |
| 424 |
zip3 (a:as) (b:bs) (c:cs) = (a,b,c) : zip3 as bs cs |
| 425 |
zip3 _ _ _ = [] |
| 426 |
|
| 427 |
zipWith |
| 428 |
zipWith f (a:as) (b:bs) = f a b : zipWith f as bs |
| 429 |
zipWith _ _ _ = [] |
| 430 |
|
| 431 |
unzip |
| 432 |
unzip = foldr (\(a,b) ~(as,bs) -> (a:as,b:bs)) ([],[]) |
| 433 |
|
| 434 |
elemIndex |
| 435 |
elemIndex x = findIndex (x==) |
| 436 |
|
| 437 |
elemIndices |
| 438 |
elemIndices x = findIndices (x==) |
| 439 |
|
| 440 |
find |
| 441 |
find p = listToMaybe . filter p |
| 442 |
|
| 443 |
findIndex |
| 444 |
findIndex p = listToMaybe . findIndices p |
| 445 |
|
| 446 |
findIndices |
| 447 |
findIndices p xs = [ i | (x,i) <- zip xs [0..], p x] |
| 448 |
|
| 449 |
isPrefixOf |
| 450 |
isPrefixOf [] _ = True |
| 451 |
isPrefixOf _ [] = False |
| 452 |
isPrefixOf (x:xs) (y:ys) = x == y && isPrefixOf xs ys |
| 453 |
|
| 454 |
isSuffixOf |
| 455 |
isSuffixOf x y = reverse x `isPrefixOf` reverse y |
| 456 |
|
| 457 |
isInfixOf |
| 458 |
isInfixOf needle haystack = any (isPrefixOf needle) (tails haystack) |
| 459 |
|
| 460 |
nub |
| 461 |
nub = nubBy (==) |
| 462 |
--OR |
| 463 |
nub l = go l [] |
| 464 |
where go [] _ = [] |
| 465 |
go (x:xs) ls |
| 466 |
| x `elem` ls = go xs ls |
| 467 |
| otherwise = x : go xs (x:ls) |
| 468 |
|
| 469 |
nubBy |
| 470 |
nubBy eq [] = [] |
| 471 |
nubBy eq (x:xs) = x : nubBy eq (filter (\ y -> not (eq x y)) xs) |
| 472 |
|
| 473 |
delete |
| 474 |
delete = deleteBy (==) |
| 475 |
|
| 476 |
deleteBy |
| 477 |
deleteBy eq x [] = [] |
| 478 |
deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys |
| 479 |
|
| 480 |
(\\) |
| 481 |
(\\) = foldl (flip delete) |
| 482 |
|
| 483 |
union |
| 484 |
union = unionBy (==) |
| 485 |
|
| 486 |
unionBy |
| 487 |
unionBy eq xs ys = xs ++ foldl (flip (deleteBy eq)) (nubBy eq ys) xs |
| 488 |
|
| 489 |
intersect |
| 490 |
intersect = intersectBy (==) |
| 491 |
|
| 492 |
intersectBy |
| 493 |
intersectBy eq xs ys = [x | x <- xs, any (eq x) ys] |
| 494 |
|
| 495 |
intersperse |
| 496 |
intersperse _ [] = [] |
| 497 |
intersperse _ [x] = [x] |
| 498 |
intersperse sep (x:xs) = x : sep : intersperse sep xs |
| 499 |
|
| 500 |
intercalate |
| 501 |
intercalate xs xss = concat (intersperse xs xss) |
| 502 |
|
| 503 |
transpose |
| 504 |
transpose [] = [] |
| 505 |
transpose ([] : xss) = transpose xss |
| 506 |
transpose ((x:xs) : xss) = (x : [h | (h:t) <- xss]) : transpose (xs : [ t | (h:t) <- xss]) |
| 507 |
|
| 508 |
partition |
| 509 |
partition p xs = foldr (select p) ([],[]) xs |
| 510 |
where select p x ~(ts,fs) | p x = (x:ts,fs) |
| 511 |
| otherwise = (ts, x:fs) |
| 512 |
|
| 513 |
mapAccumL |
| 514 |
mapAccumL _ s [] = (s, []) |
| 515 |
mapAccumL f s (x:xs) = (s'',y:ys) |
| 516 |
where (s', y ) = f s x |
| 517 |
(s'',ys) = mapAccumL f s' xs |
| 518 |
|
| 519 |
insert |
| 520 |
insert e ls = insertBy (compare) e ls |
| 521 |
|
| 522 |
insertBy |
| 523 |
insertBy _ x [] = [x] |
| 524 |
insertBy cmp x ys@(y:ys') = case cmp x y of |
| 525 |
GT -> y : insertBy cmp x ys' |
| 526 |
_ -> x : ys |
| 527 |
|
| 528 |
maximum |
| 529 |
maximum [] = undefined |
| 530 |
maximum xs = foldl1 max xs |
| 531 |
|
| 532 |
minimum |
| 533 |
minimum [] = undefined |
| 534 |
minimum xs = foldl1 min xs |
| 535 |
|
| 536 |
genericLength |
| 537 |
genericLength [] = 0 |
| 538 |
genericLength (_:l) = 1 + genericLength l |
| 539 |
|
| 540 |
group |
| 541 |
group = groupBy (==) |
| 542 |
|
| 543 |
groupBy |
| 544 |
groupBy _ [] = [] |
| 545 |
groupBy eq (x:xs) = (x:ys) : groupBy eq zs |
| 546 |
where (ys, zs) = span (eq x) xs |
| 547 |
|
| 548 |
inits |
| 549 |
inits [] = [[]] |
| 550 |
inits (x:xs) = [[]] ++ map (x:) (inits xs) |
| 551 |
|
| 552 |
tails |
| 553 |
tails [] = [[]] |
| 554 |
tails xxs@(_:xs) = xxs : tails xs |
| 555 |
|
| 556 |
sort |
| 557 |
sort = sortBy compare |
| 558 |
|
| 559 |
sortBy |
| 560 |
-- The actual definition used by GHC is an optimised mergesort. |
| 561 |
sortBy cmp = foldr (insertBy cmp) [] |
| 562 |
|
| 563 |
sortWith |
| 564 |
sortWith f = sortBy (\x y -> compare (f x) (f y)) |
| 565 |
|
| 566 |
the |
| 567 |
the (x:xs) |
| 568 |
| all (x ==) xs = x |
| 569 |
| otherwise = error "GHC.Exts.the: non-identical elements" |
| 570 |
the [] = error "GHC.Exts.the: empty list" |
| 571 |
|
| 572 |
unfoldr |
| 573 |
unfoldr f b = case f b of |
| 574 |
Just (a, b') -> a : unfoldr f b' |
| 575 |
Nothing -> [] |
| 576 |
|
| 577 |
foldl' |
| 578 |
foldl' f a [] = a |
| 579 |
foldl' f a (x:xs) = let a' = f a x in a' `seq` foldl' f a' xs |
| 580 |
|
| 581 |
foldl1 |
| 582 |
foldl1 f (x:xs) = foldl f x xs |
| 583 |
foldl1 _ [] = undefined |
| 584 |
|
| 585 |
sum |
| 586 |
sum = foldl (+) 0 |
| 587 |
|
| 588 |
product |
| 589 |
product = foldl (*) 1 |
| 590 |
|
| 591 |
unlines |
| 592 |
unlines = concatMap (++ "\n") |
| 593 |
|
| 594 |
unwords |
| 595 |
unwords [] = "" |
| 596 |
unwords ws = foldr1 (\w s -> w ++ ' ':s) ws |
| 597 |
|
| 598 |
words |
| 599 |
words s = case dropWhile isSpace s of |
| 600 |
"" -> [] |
| 601 |
s' -> w : words s'' where (w, s'') = break isSpace s' |
| 602 |
|
| 603 |
Maybe |
| 604 |
data Maybe a = Nothing | Just a |
| 605 |
|
| 606 |
maybe |
| 607 |
maybe n _ Nothing = n |
| 608 |
maybe _ f (Just x) = f x |
| 609 |
|
| 610 |
isJust |
| 611 |
isJust Nothing = False |
| 612 |
isJust _ = True |
| 613 |
|
| 614 |
isNothing |
| 615 |
isNothing Nothing = True |
| 616 |
isNothing _ = False |
| 617 |
|
| 618 |
fromJust |
| 619 |
fromJust Nothing = undefined |
| 620 |
fromJust (Just x) = x |
| 621 |
|
| 622 |
fromMaybe |
| 623 |
fromMaybe d Nothing = d |
| 624 |
fromMaybe _ (Just v) = v |
| 625 |
|
| 626 |
maybeToList |
| 627 |
maybeToList Nothing = [] |
| 628 |
maybeToList (Just x) = [x] |
| 629 |
|
| 630 |
listToMaybe |
| 631 |
listToMaybe [] = Nothing |
| 632 |
listToMaybe (a:_) = Just a |
| 633 |
|
| 634 |
catMaybes |
| 635 |
catMaybes ls = [x | Just x <- ls] |
| 636 |
|
| 637 |
data Either a b = Left a | Right b |
| 638 |
|
| 639 |
either |
| 640 |
either f _ (Left x) = f x |
| 641 |
either _ g (Right y) = g y |
| 642 |
|
| 643 |
lefts |
| 644 |
lefts x = [a | Left a <- x] |
| 645 |
|
| 646 |
rights |
| 647 |
rights x = [a | Right a <- x] |
| 648 |
|
| 649 |
isLeft |
| 650 |
isLeft (Left _) = True |
| 651 |
isLeft (Right _) = False |
| 652 |
|
| 653 |
isRight |
| 654 |
isRight (Left _) = False |
| 655 |
isRight (Right _) = True |
| 656 |
|
| 657 |
fst |
| 658 |
fst (x,_) = x |
| 659 |
|
| 660 |
snd |
| 661 |
snd (_,y) = y |
| 662 |
|
| 663 |
curry |
| 664 |
curry f x y = f (x, y) |
| 665 |
|
| 666 |
uncurry |
| 667 |
uncurry f p = f (fst p) (snd p) |
| 668 |
|
| 669 |
fix |
| 670 |
fix f = let x = f x in x |
| 671 |
|
| 672 |
on |
| 673 |
(*) `on` f = \x y -> f x * f y |
| 674 |
|
| 675 |
Complex |
| 676 |
data (RealFloat a) => Complex a = !a :+ !a |
| 677 |
|
| 678 |
realPart |
| 679 |
realPart (x :+ _) = x |
| 680 |
|
| 681 |
imagPart |
| 682 |
imagPart (_ :+ y) = y |
| 683 |
|
| 684 |
conjugate |
| 685 |
conjugate (x:+y) = x :+ (-y) |
| 686 |
|
| 687 |
mkPolar |
| 688 |
mkPolar r theta = r * cos theta :+ r * sin theta |
| 689 |
|
| 690 |
cis |
| 691 |
cis theta = cos theta :+ sin theta |
| 692 |
|
| 693 |
polar |
| 694 |
polar z = (magnitude z, phase z) |
| 695 |
|
| 696 |
phase |
| 697 |
phase (0 :+ 0) = 0 |
| 698 |
phase (x :+ y) = atan2 y x |
| 699 |
|
| 700 |
toDyn |
| 701 |
toDyn v = Dynamic (typeOf v) (unsafeCoerce v) |
| 702 |
|
| 703 |
fromDyn |
| 704 |
fromDyn (Dynamic t v) def |
| 705 |
| typeOf def == t = unsafeCoerce v |
| 706 |
| otherwise = def |
| 707 |
|
| 708 |
fromDynamic |
| 709 |
fromDynamic (Dynamic t v) = case unsafeCoerce v of |
| 710 |
r | t == typeOf r -> Just r |
| 711 |
| otherwise -> Nothing |
| 712 |
|
| 713 |
second f = arr swap >>> first f >>> arr swap |
| 714 |
where swap ~(x,y) = (y,x) |
| 715 |
|
| 716 |
(***) |
| 717 |
f *** g = first f >>> second g |
| 718 |
|
| 719 |
(&&&) |
| 720 |
f &&& g = arr (\b -> (b,b)) >>> f *** g |
| 721 |
|
| 722 |
returnA |
| 723 |
returnA = arr id |
| 724 |
|
| 725 |
(^>>) |
| 726 |
f ^>> a = arr f >>> a |
| 727 |
|
| 728 |
(>>^) |
| 729 |
a >>^ f = a >>> arr f |
| 730 |
|
| 731 |
(<<<) |
| 732 |
f <<< g = g >>> f |
| 733 |
|
| 734 |
(<<^) |
| 735 |
a <<^ f = a <<< arr f |
| 736 |
|
| 737 |
(^<<) |
| 738 |
f ^<< a = arr f <<< a |
| 739 |
|
| 740 |
modifyIORef |
| 741 |
modifyIORef ref f = writeIORef ref . f =<< readIORef ref |
| 742 |
|
| 743 |
(<$>) |
| 744 |
f <$> a = fmap f a |
| 745 |
|
| 746 |
(<$) |
| 747 |
(<$) = (<$>) . const |
| 748 |
|
| 749 |
(*>) |
| 750 |
(*>) = liftA2 (const id) |
| 751 |
|
| 752 |
(<*) |
| 753 |
(<*) = liftA2 const |
| 754 |
|
| 755 |
(<**>) |
| 756 |
(<**>) = liftA2 (flip ($)) |
| 757 |
|
| 758 |
liftA |
| 759 |
liftA f a = pure f <*> a |
| 760 |
|
| 761 |
liftA2 |
| 762 |
liftA2 f a b = f <$> a <*> b |
| 763 |
|
| 764 |
optional |
| 765 |
optional v = Just <$> v <|> pure Nothing |
| 766 |
|
| 767 |
some |
| 768 |
some v = some_v |
| 769 |
where many_v = some_v <|> pure [] |
| 770 |
some_v = (:) <$> v <*> many_v |
| 771 |
|
| 772 |
readMVar |
| 773 |
readMVar m = block $ do |
| 774 |
a <- takeMVar m |
| 775 |
putMVar m a |
| 776 |
return a |
| 777 |
|
| 778 |
swapMVar |
| 779 |
swapMVar mvar new = block $ do |
| 780 |
old <- takeMVar mvar |
| 781 |
putMVar mvar new |
| 782 |
return old |
| 783 |
|
| 784 |
withMVar m io = block $ do |
| 785 |
a <- takeMVar m |
| 786 |
b <- Exception.catch (unblock (io a)) (\e -> do putMVar m a; throw e) |
| 787 |
putMVar m a |
| 788 |
return b |
| 789 |
|
| 790 |
modifyMVar_ |
| 791 |
modifyMVar_ m io = block $ do |
| 792 |
a <- takeMVar m |
| 793 |
a' <- Exception.catch (unblock (io a)) (\e -> do putMVar m a; throw e) |
| 794 |
putMVar m a' |
| 795 |
|
| 796 |
modifyMVar |
| 797 |
modifyMVar m io = block $ do |
| 798 |
a <- takeMVar m |
| 799 |
(a',b) <- Exception.catch (unblock (io a)) (\e -> do putMVar m a; throw e) |
| 800 |
putMVar m a' |
| 801 |
return b |
| 802 |
|
| 803 |
handle |
| 804 |
handle = flip catch |
| 805 |
|
| 806 |
handleJust |
| 807 |
handleJust p = flip (catchJust p) |
| 808 |
|
| 809 |
mapException |
| 810 |
mapException f v = unsafePerformIO (catch (evaluate v) (\x -> throw (f x))) |
| 811 |
|
| 812 |
try |
| 813 |
try a = catch (a >>= \ v -> return (Right v)) (\e -> return (Left e)) |
| 814 |
|
| 815 |
bracket |
| 816 |
bracket before after thing = block $ do |
| 817 |
a <- before |
| 818 |
r <- catch (unblock (thing a)) (\e -> after a >> throw e) |
| 819 |
after a |
| 820 |
return r |
| 821 |
|
| 822 |
finally |
| 823 |
a `finally` sequel = block $ do |
| 824 |
r <- catch (unblock a) (\e -> sequel >> throw e) |
| 825 |
sequel |
| 826 |
return r |
| 827 |
|
| 828 |
bracket_ |
| 829 |
bracket_ before after thing = bracket before (const after) (const thing) |
| 830 |
|
| 831 |
putChar |
| 832 |
putChar c = hPutChar stdout c |
| 833 |
|
| 834 |
putStr |
| 835 |
putStr s = hPutStr stdout s |
| 836 |
|
| 837 |
putStrLn |
| 838 |
putStrLn s = do putStr s; putChar '\n' |
| 839 |
|
| 840 |
print |
| 841 |
print x = putStrLn (show x) |
| 842 |
|
| 843 |
getChar |
| 844 |
getChar = hGetChar stdin |
| 845 |
|
| 846 |
getLine |
| 847 |
getLine = hGetLine stdin |
| 848 |
|
| 849 |
getContents |
| 850 |
getContents = hGetContents stdin |
| 851 |
|
| 852 |
interact |
| 853 |
interact f = do s <- getContents; putStr (f s) |
| 854 |
|
| 855 |
readFile |
| 856 |
readFile name = openFile name ReadMode >>= hGetContents |
| 857 |
|
| 858 |
writeFile |
| 859 |
writeFile f txt = withFile f WriteMode (\ hdl -> hPutStr hdl txt) |
| 860 |
|
| 861 |
appendFile |
| 862 |
appendFile f txt = withFile f AppendMode (\ hdl -> hPutStr hdl txt) |
| 863 |
|
| 864 |
readLn |
| 865 |
readLn = do l <- getLine; r <- readIO l; return r |
| 866 |
|
| 867 |
hPutStrLn |
| 868 |
hPutStrLn hndl str = do hPutStr hndl str; hPutChar hndl '\n' |
| 869 |
|
| 870 |
withFile |
| 871 |
withFile name mode = bracket (openFile name mode) hClose |
| 872 |
|
| 873 |
exitFailure |
| 874 |
exitFailure = exitWith (ExitFailure 1) |
| 875 |
|
| 876 |
failIO |
| 877 |
failIO s = ioError (userError s) |
| 878 |
|
| 879 |
FilePath |
| 880 |
type FilePath = String |
| 881 |
|
| 882 |
IORef |
| 883 |
newtype IORef a = IORef (STRef RealWorld a) |
| 884 |
|
| 885 |
newIORef |
| 886 |
newIORef v = stToIO (newSTRef v) >>= \ var -> return (IORef var) |
| 887 |
|
| 888 |
readIORef |
| 889 |
readIORef (IORef var) = stToIO (readSTRef var) |
| 890 |
|
| 891 |
writeIORef |
| 892 |
writeIORef (IORef var) v = stToIO (writeSTRef var v) |
| 893 |
|
| 894 |
read |
| 895 |
read s = either error id (readEither s) |
| 896 |
|
| 897 |
reads |
| 898 |
reads = readsPrec minPrec |
| 899 |
|
| 900 |
readMaybe |
| 901 |
readMaybe s = case readEither s of |
| 902 |
Left _ -> Nothing |
| 903 |
Right a -> Just a |
| 904 |
|
| 905 |
readEither |
| 906 |
-- The old-style Read definition. The real one uses readPrec instead. |
| 907 |
readEither s = |
| 908 |
let s' = dropWhile isSpace s |
| 909 |
in case [x | (x, rest) <- reads s', all isSpace rest] of |
| 910 |
[x] -> Right x |
| 911 |
[] -> Left "Prelude.read: no parse" |
| 912 |
_ -> Left "Prelude.read: ambiguous parse" |
| 913 |
|
| 914 |
IOArray |
| 915 |
newtype IOArray i e = IOArray (STArray RealWorld i e) |
| 916 |
|
| 917 |
ExitCode |
| 918 |
data ExitCode = ExitSuccess | ExitFailure Int |
| 919 |
|
| 920 |
throw |
| 921 |
throw exception = raise# exception |
| 922 |
|
| 923 |
IOMode |
| 924 |
data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode |
| 925 |
|
| 926 |
killThread |
| 927 |
killThread tid = throwTo tid (AsyncException ThreadKilled) |
| 928 |
|
| 929 |
STArray |
| 930 |
data STArray s i e = STArray !i !i (MutableArray# s e) |
| 931 |
|
| 932 |
(!) |
| 933 |
arr@(Array l u _) ! i = unsafeAt arr (index (l,u) i) |
| 934 |
|
| 935 |
bounds |
| 936 |
bounds (Array l u _) = (l,u) |
| 937 |
|
| 938 |
indices |
| 939 |
indices (Array l u _) = range (l,u) |
| 940 |
|
| 941 |
elems |
| 942 |
elems arr@(Array l u _) = [unsafeAt arr i | i <- [0 .. rangeSize (l,u) - 1]] |
| 943 |
|
| 944 |
assocs |
| 945 |
assocs arr@(Array l u _) = [(i, unsafeAt arr (unsafeIndex (l,u) i)) | i <- range (l,u)] |
| 946 |
|
| 947 |
(//) |
| 948 |
arr@(Array l u _) // ies = unsafeReplace arr [(index (l,u) i, e) | (i, e) <- ies] |
| 949 |
|
| 950 |
undefined |
| 951 |
undefined = error "Prelude.undefined" |
| 952 |
|
| 953 |
error |
| 954 |
error s = throw (ErrorCall s) |
| 955 |
|
| 956 |
catch |
| 957 |
catch m k = catchException m handler where handler (IOException err) = k err |
| 958 |
|
| 959 |
Float |
| 960 |
data Float = F# Float# |
| 961 |
|
| 962 |
Double |
| 963 |
data Double = D# Double# |
| 964 |
|
| 965 |
ForeignPtr |
| 966 |
data ForeignPtr a = ForeignPtr Addr# ForeignPtrContents |
| 967 |
|
| 968 |
ForeignPtrContents |
| 969 |
data ForeignPtrContents = PlainForeignPtr !(IORef [IO ()]) |
| 970 |
| MallocPtr (MutableByteArray# RealWorld) !(IORef [IO ()]) |
| 971 |
| PlainPtr (MutableByteArray# RealWorld) |
| 972 |
|
| 973 |
Int16 |
| 974 |
data Int16 = I16# Int# |
| 975 |
|
| 976 |
Int32 |
| 977 |
data Int32 = I32# Int32# |
| 978 |
|
| 979 |
Int64 |
| 980 |
data Int64 = I64# Int64# |
| 981 |
|
| 982 |
Integer |
| 983 |
data Integer = S# Int# |
| 984 |
| J# Int# ByteArray# |
| 985 |
|
| 986 |
(-) |
| 987 |
x - y = x + negate y |
| 988 |
|
| 989 |
negate |
| 990 |
negate x = 0 - x |
| 991 |
|
| 992 |
subtract |
| 993 |
subtract x y = y - x |
| 994 |
|
| 995 |
Ptr |
| 996 |
data Ptr a = Ptr Addr# |
| 997 |
|
| 998 |
nullPtr |
| 999 |
nullPtr = Ptr nullAddr# |
| 1000 |
|
| 1001 |
castPtr |
| 1002 |
castPtr (Ptr addr) = Ptr addr |
| 1003 |
|
| 1004 |
plusPtr |
| 1005 |
plusPtr (Ptr addr) (I# d) = Ptr (plusAddr# addr d) |
| 1006 |
|
| 1007 |
minusPtr |
| 1008 |
minusPtr (Ptr a1) (Ptr a2) = I# (minusAddr# a1 a2) |
| 1009 |
|
| 1010 |
Ratio |
| 1011 |
data (Integral a) => Ratio a = !a :% !a |
| 1012 |
|
| 1013 |
Rational |
| 1014 |
type Rational = Ratio Integer |
| 1015 |
|
| 1016 |
reduce |
| 1017 |
reduce _ 0 = undefined |
| 1018 |
reduce x y = (x `quot` d) :% (y `quot` d) |
| 1019 |
where d = gcd x y |
| 1020 |
|
| 1021 |
(%) |
| 1022 |
x % y = reduce (x * signum y) (abs y) |
| 1023 |
|
| 1024 |
numerator |
| 1025 |
numerator (x :% _) = x |
| 1026 |
|
| 1027 |
denominator |
| 1028 |
denominator (_ :% y) = y |
| 1029 |
|
| 1030 |
fromIntegral |
| 1031 |
fromIntegral = fromInteger . toInteger |
| 1032 |
|
| 1033 |
realToFrac |
| 1034 |
realToFrac = fromRational . toRational |
| 1035 |
|
| 1036 |
even |
| 1037 |
even n = n `rem` 2 == 0 |
| 1038 |
|
| 1039 |
odd |
| 1040 |
odd = not . even |
| 1041 |
|
| 1042 |
(^) |
| 1043 |
x ^ 0 = 1 |
| 1044 |
x ^ n | n > 0 = f x (n-1) x |
| 1045 |
where |
| 1046 |
f _ 0 y = y |
| 1047 |
f x n y = g x n |
| 1048 |
where g x n | even n = g (x*x) (n `quot` 2) |
| 1049 |
| otherwise = f x (n-1) (x*y) |
| 1050 |
_ ^ _ = error "Prelude.^: negative exponent" |
| 1051 |
|
| 1052 |
(^^) |
| 1053 |
x ^^ n = if n >= 0 then x^n else recip (x^(negate n)) |
| 1054 |
|
| 1055 |
gcd |
| 1056 |
gcd x y = gcd' (abs x) (abs y) |
| 1057 |
where gcd' a 0 = a |
| 1058 |
gcd' a b = gcd' b (a `rem` b) |
| 1059 |
|
| 1060 |
lcm |
| 1061 |
lcm _ 0 = 0 |
| 1062 |
lcm 0 _ = 0 |
| 1063 |
lcm x y = abs ((x `quot` (gcd x y)) * y) |
| 1064 |
|
| 1065 |
ST |
| 1066 |
newtype ST s a = ST (STRep s a) |
| 1067 |
|
| 1068 |
STRep |
| 1069 |
type STRep s a = State# s -> (# State# s, a #) |
| 1070 |
|
| 1071 |
runST |
| 1072 |
runST st = runSTRep (case st of { ST st_rep -> st_rep }) |
| 1073 |
|
| 1074 |
runSTRep |
| 1075 |
runSTRep st_rep = case st_rep realWorld# of (# _, r #) -> r |
| 1076 |
|
| 1077 |
ShowS |
| 1078 |
type ShowS = String -> String |
| 1079 |
|
| 1080 |
showsPrec |
| 1081 |
showsPrec _ x s = show x ++ s |
| 1082 |
|
| 1083 |
show |
| 1084 |
show x = shows x "" |
| 1085 |
|
| 1086 |
stToIO |
| 1087 |
stToIO (ST m) = IO m |
| 1088 |
|
| 1089 |
ioToST |
| 1090 |
ioToST (IO m) = (ST m) |
| 1091 |
|
| 1092 |
unsafeIOToST |
| 1093 |
unsafeIOToST (IO io) = ST $ \ s -> (unsafeCoerce# io) s |
| 1094 |
|
| 1095 |
unsafeSTToIO |
| 1096 |
unsafeSTToIO (ST m) = IO (unsafeCoerce# m) |
| 1097 |
|
| 1098 |
scanl |
| 1099 |
scanl f q ls = q : case ls of |
| 1100 |
[] -> [] |
| 1101 |
x:xs -> scanl f (f q x) xs |
| 1102 |
|
| 1103 |
scanl1 |
| 1104 |
scanl1 f (x:xs) = scanl f x xs |
| 1105 |
scanl1 _ [] = [] |
| 1106 |
|
| 1107 |
foldr1 |
| 1108 |
foldr1 _ [x] = x |
| 1109 |
foldr1 f (x:xs) = f x (foldr1 f xs) |
| 1110 |
foldr1 _ [] = undefined |
| 1111 |
|
| 1112 |
scanr1 |
| 1113 |
scanr1 f [] = [] |
| 1114 |
scanr1 f [x] = [x] |
| 1115 |
scanr1 f (x:xs) = f x q : qs where qs@(q:_) = scanr1 f xs |
| 1116 |
|
| 1117 |
trace |
| 1118 |
trace string expr = unsafePerformIO $ do |
| 1119 |
hPutStrLn stderr string |
| 1120 |
return expr |
| 1121 |
|
| 1122 |
STRef |
| 1123 |
data STRef s a = STRef (MutVar# s a) |
| 1124 |
|
| 1125 |
newSTRef |
| 1126 |
newSTRef init = ST $ \s1# -> |
| 1127 |
case newMutVar# init s1# of |
| 1128 |
(# s2#, var# #) -> (# s2#, STRef var# #) |
| 1129 |
|
| 1130 |
readSTRef |
| 1131 |
readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1# |
| 1132 |
|
| 1133 |
writeSTRef |
| 1134 |
writeSTRef (STRef var#) val = ST $ \s1# -> |
| 1135 |
case writeMutVar# var# val s1# of |
| 1136 |
s2# -> (# s2#, () #) |
| 1137 |
|
| 1138 |
STRef (==) |
| 1139 |
STRef v1# == STRef v2# = sameMutVar# v1# v2# |
| 1140 |
|
| 1141 |
Eq |
| 1142 |
class Eq a where |
| 1143 |
(==), (/=) :: a -> a -> Bool |
| 1144 |
|
| 1145 |
Ord |
| 1146 |
class (Eq a) => Ord a where |
| 1147 |
compare :: a -> a -> Ordering |
| 1148 |
(<), (<=), (>), (>=) :: a -> a -> Bool |
| 1149 |
max, min :: a -> a -> a |
| 1150 |
|
| 1151 |
Ix |
| 1152 |
class (Ord a) => Ix a where |
| 1153 |
range :: (a, a) -> [a] |
| 1154 |
index :: (a, a) -> a -> Int |
| 1155 |
inRange :: (a, a) -> a -> Bool |
| 1156 |
rangeSize :: (a, a) -> Int |
| 1157 |
|
| 1158 |
Bounded |
| 1159 |
class Bounded a where |
| 1160 |
minBound, maxBound :: a |
| 1161 |
|
| 1162 |
Real |
| 1163 |
class (Num a, Ord a) => Real a where |
| 1164 |
toRational :: a -> Rational |
| 1165 |
|
| 1166 |
Integral |
| 1167 |
class (Real a, Enum a) => Integral a where |
| 1168 |
quot, rem, div, mod :: a -> a -> a |
| 1169 |
quotRem, divMod :: a -> a -> (a, a) |
| 1170 |
toInteger :: a -> Integer |
| 1171 |
|
| 1172 |
Fractional |
| 1173 |
class (Num a) => Fractional a where |
| 1174 |
(/) :: a -> a -> a |
| 1175 |
recip :: a -> a |
| 1176 |
fromRational :: Rational -> a |
| 1177 |
|
| 1178 |
RealFrac |
| 1179 |
class (Real a, Fractional a) => RealFrac a where |
| 1180 |
properFraction :: (Integral b) => a -> (b, a) |
| 1181 |
truncate, round, ceiling, floor :: (Integral b) => a -> b |
| 1182 |
|
| 1183 |
Show |
| 1184 |
class Show a where |
| 1185 |
showsPrec :: Int -> a -> ShowS |
| 1186 |
show :: a -> String |
| 1187 |
showList :: [a] -> ShowS |
| 1188 |
|
| 1189 |
Enum |
| 1190 |
class Enum a where |
| 1191 |
succ, pred :: a -> a |
| 1192 |
toEnum :: Int -> a |
| 1193 |
fromEnum :: a -> Int |
| 1194 |
enumFrom :: a -> [a] |
| 1195 |
enumFromThen, enumFromTo :: a -> a -> [a] |
| 1196 |
enumFromThenTo :: a -> a -> a -> [a] |
| 1197 |
|
| 1198 |
Splittable |
| 1199 |
class Splittable t where |
| 1200 |
split :: t -> (t,t) |
| 1201 |
|
| 1202 |
Floating |
| 1203 |
class (Fractional a) => Floating a where |
| 1204 |
pi :: a |
| 1205 |
exp, log, sqrt :: a -> a |
| 1206 |
sin, cos, tan :: a -> a |
| 1207 |
asin, acos, atan :: a -> a |
| 1208 |
sinh, cosh, tanh :: a -> a |
| 1209 |
asinh, acosh, atanh :: a -> a |
| 1210 |
(**), logBase :: a -> a -> a |
| 1211 |
|
| 1212 |
Num |
| 1213 |
class Num a where |
| 1214 |
(+), (-), (*) :: a -> a -> a |
| 1215 |
negate, abs, signum :: a -> a |
| 1216 |
fromInteger :: Integer -> a |
| 1217 |
|
| 1218 |
RandomGen |
| 1219 |
class RandomGen g where |
| 1220 |
next :: g -> (Int, g) |
| 1221 |
split :: g -> (g, g) |
| 1222 |
genRange :: g -> (Int, Int) |
| 1223 |
|
| 1224 |
Random |
| 1225 |
class Random a where |
| 1226 |
random :: RandomGen g => g -> (a, g) |
| 1227 |
randoms :: RandomGen g => g -> [a] |
| 1228 |
randomR :: RandomGen g => (a, a) -> g -> (a, g) |
| 1229 |
randomRs :: RandomGen g => (a, a) -> g -> [a] |
| 1230 |
randomRIO :: (a, a) -> IO a |
| 1231 |
randomIO :: IO a |
| 1232 |
|
| 1233 |
Functor |
| 1234 |
class Functor f where |
| 1235 |
fmap :: (a -> b) -> f a -> f b |
| 1236 |
|
| 1237 |
Read |
| 1238 |
class Read a where |
| 1239 |
readsPrec :: Int -> ReadS a |
| 1240 |
readList :: ReadS [a] |
| 1241 |
readPrec :: ReadPrec a |
| 1242 |
readListPrec :: ReadPrec [a] |
| 1243 |
|
| 1244 |
Applicative |
| 1245 |
class Functor f => Applicative f where |
| 1246 |
pure :: a -> f a |
| 1247 |
(<*>) :: f (a -> b) -> f a -> f b |
| 1248 |
|
| 1249 |
Alternative |
| 1250 |
class Applicative f => Alternative f where |
| 1251 |
empty :: f a |
| 1252 |
(<|>) :: f a -> f a -> f a |
| 1253 |
|
| 1254 |
Monad |
| 1255 |
class Applicative m => Monad m where |
| 1256 |
-- Note: Applicative wasn't a superclass before GHC 7.10 |
| 1257 |
(>>=) :: m a -> (a -> m b) -> m b |
| 1258 |
(>>) :: m a -> m b -> m b |
| 1259 |
return :: a -> m a |
| 1260 |
fail :: String -> m a |
| 1261 |
|
| 1262 |
MonadPlus |
| 1263 |
class Alternative m => MonadPlus m where |
| 1264 |
-- Note: Alternative wasn't a superclass before GHC 7.10 |
| 1265 |
mzero :: m a |
| 1266 |
mplus :: m a -> m a -> m a |
| 1267 |
|
| 1268 |
Monoid |
| 1269 |
class Monoid a where |
| 1270 |
mempty :: a |
| 1271 |
mappend :: a -> a -> a |
| 1272 |
mconcat :: [a] -> a |
| 1273 |
|
| 1274 |
(<>) |
| 1275 |
(<>) = mappend |
| 1276 |
|
| 1277 |
Dual |
| 1278 |
newtype Dual a = Dual { getDual :: a } |
| 1279 |
|
| 1280 |
getDual |
| 1281 |
newtype Dual a = Dual { getDual :: a } |
| 1282 |
|
| 1283 |
Dual mempty |
| 1284 |
mempty = Dual mempty |
| 1285 |
|
| 1286 |
Dual mappend |
| 1287 |
Dual x `mappend` Dual y = Dual (y `mappend` x) |
| 1288 |
|
| 1289 |
Endo |
| 1290 |
newtype Endo a = Endo { appEndo :: a -> a } |
| 1291 |
|
| 1292 |
appEndo |
| 1293 |
newtype Endo a = Endo { appEndo :: a -> a } |
| 1294 |
|
| 1295 |
Endo mempty |
| 1296 |
mempty = Endo id |
| 1297 |
|
| 1298 |
Endo mappend |
| 1299 |
Endo f `mappend` Endo g = Endo (f . g) |
| 1300 |
|
| 1301 |
All |
| 1302 |
newtype All = All { getAll :: Bool } |
| 1303 |
|
| 1304 |
getAll |
| 1305 |
newtype All = All { getAll :: Bool } |
| 1306 |
|
| 1307 |
All mempty |
| 1308 |
mempty = All True |
| 1309 |
|
| 1310 |
All mappend |
| 1311 |
All x `mappend` All y = All (x && y) |
| 1312 |
|
| 1313 |
Any |
| 1314 |
newtype Any = Any { getAny :: Bool } |
| 1315 |
|
| 1316 |
getAny |
| 1317 |
newtype Any = Any { getAny :: Bool } |
| 1318 |
|
| 1319 |
Any mempty |
| 1320 |
mempty = Any False |
| 1321 |
|
| 1322 |
Any mappend |
| 1323 |
Any x `mappend` Any y = Any (x || y) |
| 1324 |
|
| 1325 |
Sum |
| 1326 |
newtype Sum a = Sum { getSum :: a } |
| 1327 |
|
| 1328 |
getSum |
| 1329 |
newtype Sum a = Sum { getSum :: a } |
| 1330 |
|
| 1331 |
Sum mempty |
| 1332 |
mempty = Sum 0 |
| 1333 |
|
| 1334 |
Sum mappend |
| 1335 |
Sum x `mappend` Sum y = Sum (x + y) |
| 1336 |
|
| 1337 |
Product |
| 1338 |
newtype Product a = Product { getProduct :: a } |
| 1339 |
|
| 1340 |
getProduct |
| 1341 |
newtype Product a = Product { getProduct :: a } |
| 1342 |
|
| 1343 |
Product mempty |
| 1344 |
mempty = Product 1 |
| 1345 |
|
| 1346 |
Product mappend |
| 1347 |
Product x `mappend` Product y = Product (x * y) |
| 1348 |
|
| 1349 |
First |
| 1350 |
newtype First a = First { getFirst :: Maybe a } |
| 1351 |
|
| 1352 |
getFirst |
| 1353 |
newtype First a = First { getFirst :: Maybe a } |
| 1354 |
|
| 1355 |
First mempty |
| 1356 |
mempty = First Nothing |
| 1357 |
|
| 1358 |
First mappend |
| 1359 |
r@(First (Just _)) `mappend` _ = r |
| 1360 |
First Nothing `mappend` r = r |
| 1361 |
|
| 1362 |
Last |
| 1363 |
newtype Last a = Last { getLast :: Maybe a } |
| 1364 |
|
| 1365 |
getLast |
| 1366 |
newtype Last a = Last { getLast :: Maybe a } |
| 1367 |
|
| 1368 |
Last mempty |
| 1369 |
mempty = Last Nothing |
| 1370 |
|
| 1371 |
Last mappend |
| 1372 |
_ `mappend` r@(Last (Just _)) = r |
| 1373 |
r `mappend` Last Nothing = r |
| 1374 |
|
| 1375 |
Arrow |
| 1376 |
class Arrow a where |
| 1377 |
arr, pure :: (b -> c) -> a b c |
| 1378 |
(>>>) :: a b c -> a c d -> a b d |
| 1379 |
first :: a b c -> a (b,d) (c,d) |
| 1380 |
second :: a b c -> a (d,b) (d,c) |
| 1381 |
(***) :: a b c -> a b' c' -> a (b,b') (c,c') |
| 1382 |
(&&&) :: a b c -> a b c' -> a b (c,c') |
| 1383 |
|
| 1384 |
Traversable |
| 1385 |
class (Functor t, Foldable t) => Traversable t where |
| 1386 |
traverse :: Applicative f => (a -> f b) -> t a -> f (t b) |
| 1387 |
sequenceA :: Applicative f => t (f a) -> f (t a) |
| 1388 |
mapM :: Monad m => (a -> m b) -> t a -> m (t b) |
| 1389 |
sequence :: Monad m => t (m a) -> m (t a) |
| 1390 |
|
| 1391 |
traverse_ |
| 1392 |
traverse_ h xs = foldr (\fx fxs -> h fx *> fxs) (pure ()) |
| 1393 |
--OR |
| 1394 |
traverse_ h xs = traverse h xs *> pure () |
| 1395 |
|
| 1396 |
map_ |
| 1397 |
map_ = flip traverse_ |
| 1398 |
|
| 1399 |
IO fmap |
| 1400 |
fmap f x = x >>= (return . f) |
| 1401 |
|
| 1402 |
IO (>>) |
| 1403 |
m >> k = m >>= \ _ -> k |
| 1404 |
|
| 1405 |
IO return |
| 1406 |
return x = returnIO x |
| 1407 |
|
| 1408 |
IO (>>=) |
| 1409 |
m >>= k = bindIO m k |
| 1410 |
|
| 1411 |
IO fail |
| 1412 |
fail s = failIO s |
| 1413 |
|
| 1414 |
IO mzero |
| 1415 |
mzero = ioError (userError "mzero") |
| 1416 |
|
| 1417 |
IO mplus |
| 1418 |
m `mplus` n = m `catch` \_ -> n |
| 1419 |
|
| 1420 |
[] (==) |
| 1421 |
[] == [] = True |
| 1422 |
(x:xs) == (y:ys) = x == y && xs == ys |
| 1423 |
_ == _ = False |
| 1424 |
|
| 1425 |
[] fmap |
| 1426 |
fmap = map |
| 1427 |
|
| 1428 |
[] (>>=) |
| 1429 |
xs >>= f = concatMap f xs |
| 1430 |
|
| 1431 |
[] (>>) |
| 1432 |
xs >> ys = concatMap (const ys) xs |
| 1433 |
|
| 1434 |
[] return |
| 1435 |
return x = [x] |
| 1436 |
|
| 1437 |
[] fail |
| 1438 |
fail _ = [] |
| 1439 |
|
| 1440 |
[] mzero |
| 1441 |
mzero = [] |
| 1442 |
|
| 1443 |
[] mplus |
| 1444 |
mplus = (++) |
| 1445 |
|
| 1446 |
Maybe (>>=) |
| 1447 |
(Just x) >>= k = k x |
| 1448 |
Nothing >>= _ = Nothing |
| 1449 |
|
| 1450 |
Maybe (>>) |
| 1451 |
(Just _) >> k = k |
| 1452 |
Nothing >> _ = Nothing |
| 1453 |
|
| 1454 |
Maybe return |
| 1455 |
return = Just |
| 1456 |
|
| 1457 |
Maybe fail |
| 1458 |
fail _ = Nothing |
| 1459 |
|
| 1460 |
Maybe mzero |
| 1461 |
mzero = Nothing |
| 1462 |
|
| 1463 |
Maybe mplus |
| 1464 |
Nothing `mplus` ys = ys |
| 1465 |
xs `mplus` ys = xs |
| 1466 |
|
| 1467 |
Maybe fmap |
| 1468 |
fmap _ Nothing = Nothing |
| 1469 |
fmap f (Just a) = Just (f a) |
| 1470 |
|
| 1471 |
Either fmap |
| 1472 |
fmap _ (Left x) = Left x |
| 1473 |
fmap f (Right y) = Right (f y) |
| 1474 |
|
| 1475 |
Either return |
| 1476 |
return = Right |
| 1477 |
|
| 1478 |
Either (>>=) |
| 1479 |
Left l >>= _ = Left l |
| 1480 |
Right r >>= k = k r |
| 1481 |
|
| 1482 |
Either fail |
| 1483 |
fail msg = Left (strMsg msg) |
| 1484 |
|
| 1485 |
Either mzero |
| 1486 |
mzero = Left noMsg |
| 1487 |
|
| 1488 |
Either mplus |
| 1489 |
Left _ `mplus` n = n |
| 1490 |
m `mplus` _ = m |
| 1491 |
|
| 1492 |
Either mfix |
| 1493 |
mfix f = let a = f $ case a of |
| 1494 |
Right r -> r |
| 1495 |
_ -> error "empty mfix argument" |
| 1496 |
in a |
| 1497 |
|
| 1498 |
() (==) |
| 1499 |
() == () = True |
| 1500 |
|
| 1501 |
() (/=) |
| 1502 |
() /= () = False |
| 1503 |
|
| 1504 |
() (<=) |
| 1505 |
() <= () = True |
| 1506 |
|
| 1507 |
() (<) |
| 1508 |
() < () = False |
| 1509 |
|
| 1510 |
() (>=) |
| 1511 |
() >= () = True |
| 1512 |
|
| 1513 |
() (>) |
| 1514 |
() > () = False |
| 1515 |
|
| 1516 |
() max |
| 1517 |
max () () = () |
| 1518 |
|
| 1519 |
() min |
| 1520 |
min () () = () |
| 1521 |
|
| 1522 |
() compare |
| 1523 |
compare () () = EQ |
| 1524 |
|
| 1525 |
Char (==) |
| 1526 |
(C# c1) == (C# c2) = c1 `eqChar#` c2 |
| 1527 |
(C# c1) /= (C# c2) = c1 `neChar#` c2 |
| 1528 |
|
| 1529 |
Int (==) |
| 1530 |
(==) = eqInt |
| 1531 |
|
| 1532 |
Int (/=) |
| 1533 |
(/=) = neInt |
| 1534 |
|
| 1535 |
(->) fmap |
| 1536 |
fmap = (.) |
| 1537 |
|
| 1538 |
(->) return |
| 1539 |
return = const |
| 1540 |
|
| 1541 |
(->) (>>=) |
| 1542 |
f >>= k = \ r -> k (f r) r |
| 1543 |
|
| 1544 |
(,) fmap |
| 1545 |
fmap f (x,y) = (x, f y) |
| 1546 |
|
| 1547 |
(->) ask |
| 1548 |
ask = id |
| 1549 |
|
| 1550 |
(->) local |
| 1551 |
local f m = m . f |
| 1552 |
|
| 1553 |
(->) mfix |
| 1554 |
mfix f = \ r -> let a = f a r in a |
| 1555 |
|
| 1556 |
IORef (==) |
| 1557 |
IORef x == IORef y = x == y |
| 1558 |
|
| 1559 |
IOArray (==) |
| 1560 |
IOArray x == IOArray y = x == y |
| 1561 |
|
| 1562 |
asks |
| 1563 |
asks f = do |
| 1564 |
r <- ask |
| 1565 |
return (f r) |
| 1566 |
|
| 1567 |
Identity |
| 1568 |
newtype Identity a = Identity { runIdentity :: a } |
| 1569 |
|
| 1570 |
Identity fmap |
| 1571 |
fmap f m = Identity (f (runIdentity m)) |
| 1572 |
|
| 1573 |
Identity return |
| 1574 |
return a = Identity a |
| 1575 |
|
| 1576 |
Identity (>>=) |
| 1577 |
m >>= k = k (runIdentity m) |
| 1578 |
|
| 1579 |
Identity mfix |
| 1580 |
mfix f = Identity (fix (runIdentity . f)) |
| 1581 |
|
| 1582 |
MonadCont |
| 1583 |
class (Monad m) => MonadCont m where |
| 1584 |
callCC :: ((a -> m b) -> m a) -> m a |
| 1585 |
|
| 1586 |
Cont |
| 1587 |
newtype Cont r a = Cont { runCont :: (a -> r) -> r } |
| 1588 |
|
| 1589 |
Cont fmap |
| 1590 |
fmap f m = Cont $ \c -> runCont m (c . f) |
| 1591 |
|
| 1592 |
Cont return |
| 1593 |
return a = Cont ($ a) |
| 1594 |
|
| 1595 |
Cont (>>=) |
| 1596 |
m >>= k = Cont $ \c -> runCont m $ \a -> runCont (k a) c |
| 1597 |
|
| 1598 |
Cont callCC |
| 1599 |
callCC f = Cont $ \c -> runCont (f (\a -> Cont $ \_ -> c a)) c |
| 1600 |
|
| 1601 |
ContT |
| 1602 |
newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r } |
| 1603 |
|
| 1604 |
ContT fmap |
| 1605 |
fmap f m = ContT $ \c -> runContT m (c . f) |
| 1606 |
|
| 1607 |
ContT return |
| 1608 |
return a = ContT ($ a) |
| 1609 |
|
| 1610 |
ContT (>>=) |
| 1611 |
m >>= k = ContT $ \c -> runContT m (\a -> runContT (k a) c) |
| 1612 |
|
| 1613 |
ContT callCC |
| 1614 |
callCC f = ContT $ \c -> runContT (f (\a -> ContT $ \_ -> c a)) c |
| 1615 |
|
| 1616 |
ContT ask |
| 1617 |
ask = lift ask |
| 1618 |
|
| 1619 |
ContT local |
| 1620 |
local f m = ContT $ \c -> do |
| 1621 |
r <- ask |
| 1622 |
local f (runContT m (local (const r) . c)) |
| 1623 |
|
| 1624 |
Error |
| 1625 |
class Error a where |
| 1626 |
noMsg :: a |
| 1627 |
strMsg :: String -> a |
| 1628 |
|
| 1629 |
MonadError |
| 1630 |
class (Monad m) => MonadError e m | m -> e where |
| 1631 |
throwError :: e -> m a |
| 1632 |
catchError :: m a -> (e -> m a) -> m a |
| 1633 |
|
| 1634 |
IO throwError |
| 1635 |
throwError = ioError |
| 1636 |
|
| 1637 |
IO catchError |
| 1638 |
catchError = catch |
| 1639 |
|
| 1640 |
ErrorT |
| 1641 |
newtype ErrorT e m a = ErrorT { runErrorT :: m (Either e a) } |
| 1642 |
|
| 1643 |
ErrorT fmap |
| 1644 |
fmap f m = ErrorT $ do |
| 1645 |
a <- runErrorT m |
| 1646 |
case a of Left l -> return (Left l) |
| 1647 |
Right r -> return (Right (f r)) |
| 1648 |
|
| 1649 |
ErrorT return |
| 1650 |
return a = ErrorT $ return (Right a) |
| 1651 |
|
| 1652 |
ErrorT (>>=) |
| 1653 |
m >>= k = ErrorT $ do |
| 1654 |
a <- runErrorT m |
| 1655 |
case a of Left l -> return (Left l) |
| 1656 |
Right r -> runErrorT (k r) |
| 1657 |
|
| 1658 |
ErrorT fail |
| 1659 |
fail msg = ErrorT $ return (Left (strMsg msg)) |
| 1660 |
|
| 1661 |
ErrorT mzero |
| 1662 |
mzero = ErrorT $ return (Left noMsg) |
| 1663 |
|
| 1664 |
ErrorT mplus |
| 1665 |
m `mplus` n = ErrorT $ do |
| 1666 |
a <- runErrorT m |
| 1667 |
case a of Left _ -> runErrorT n |
| 1668 |
Right r -> return (Right r) |
| 1669 |
|
| 1670 |
ErrorT mfix |
| 1671 |
mfix f = ErrorT $ mfix $ \a -> runErrorT $ f $ case a of |
| 1672 |
Right r -> r |
| 1673 |
_ -> error "empty mfix argument" |
| 1674 |
|
| 1675 |
ErrorT ask |
| 1676 |
ask = lift ask |
| 1677 |
|
| 1678 |
ErrorT local |
| 1679 |
local f m = ErrorT $ local f (runErrorT m) |
| 1680 |
|
| 1681 |
MonadFix |
| 1682 |
class (Monad m) => MonadFix m where |
| 1683 |
mfix :: (a -> m a) -> m a |
| 1684 |
|
| 1685 |
Maybe mfix |
| 1686 |
mfix f = let a = f (unJust a) in a where unJust (Just x) = x |
| 1687 |
|
| 1688 |
[] mfix |
| 1689 |
mfix f = case fix (f . head) of |
| 1690 |
[] -> [] |
| 1691 |
(x:_) -> x : mfix (tail . f) |
| 1692 |
|
| 1693 |
IO mfix |
| 1694 |
mfix = fixIO |
| 1695 |
|
| 1696 |
Maybe pure |
| 1697 |
pure = return |
| 1698 |
|
| 1699 |
Maybe (<*>) |
| 1700 |
(<*>) = ap |
| 1701 |
|
| 1702 |
Maybe empty |
| 1703 |
empty = Nothing |
| 1704 |
|
| 1705 |
Maybe (<|>) |
| 1706 |
Nothing <|> p = p |
| 1707 |
Just x <|> _ = Just x |
| 1708 |
|
| 1709 |
[] pure |
| 1710 |
pure = return |
| 1711 |
|
| 1712 |
[] (<*>) |
| 1713 |
(<*>) = ap |
| 1714 |
|
| 1715 |
[] empty |
| 1716 |
empty = [] |
| 1717 |
|
| 1718 |
[] (<|>) |
| 1719 |
(<|>) = (++) |
| 1720 |
|
| 1721 |
IO pure |
| 1722 |
pure = return |
| 1723 |
|
| 1724 |
IO (<*>) |
| 1725 |
(<*>) = ap |
| 1726 |
|
| 1727 |
(->) pure |
| 1728 |
pure = const |
| 1729 |
|
| 1730 |
(->) (<*>) |
| 1731 |
(<*>) f g x = f x (g x) |
| 1732 |
|
| 1733 |
(,) pure |
| 1734 |
pure x = (mempty, x) |
| 1735 |
|
| 1736 |
(,) (<*>) |
| 1737 |
(u, f) <*> (v, x) = (u `mappend` v, f x) |
| 1738 |
|
| 1739 |
(->) arr |
| 1740 |
arr f = f |
| 1741 |
|
| 1742 |
(->) (>>>) |
| 1743 |
f >>> g = g . f |
| 1744 |
|
| 1745 |
(->) first |
| 1746 |
first f = f *** id |
| 1747 |
|
| 1748 |
(->) second |
| 1749 |
second f = id *** f |
| 1750 |
|
| 1751 |
(->) (***) |
| 1752 |
(f *** g) ~(x,y) = (f x, g y) |
| 1753 |
|