Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 121 lines (93 sloc) 3.226 kb
fb0abbd Made some changes.
Sebastiaan Visser authored
1 {-# LANGUAGE
2 NoImplicitPrelude
3 , GADTs
4 , MultiParamTypeClasses
5 , TypeFamilies
6 , FunctionalDependencies
7 , FlexibleInstances
8 , ScopedTypeVariables
9 #-}
8b57a9e @tomlokhorst Initial checkin
authored
10
11 module AwesomePrelude where
12
f8df28b @tomlokhorst Working on Eq...
authored
13 undefined :: a
14 undefined = undefined
15
be9fc12 @tomlokhorst Moved stuff around
authored
16 -- A type class for the normal Prelude.Bool data type
8b57a9e @tomlokhorst Initial checkin
authored
17 class Bool f r | f -> r where
18 bool :: r -> r -> f -> r
19 false :: f
20 true :: f
21
40d631e @tomlokhorst Made some more changes, code is better now.
authored
22 if' :: Bool b r => b -> r -> r -> r
e46e951 @tomlokhorst Renamed if_, maybee, eitherr
authored
23 if' p t f = bool f t p
8b57a9e @tomlokhorst Initial checkin
authored
24
fb0abbd Made some changes.
Sebastiaan Visser authored
25 (&&) :: Bool b b => b -> b -> b
e46e951 @tomlokhorst Renamed if_, maybee, eitherr
authored
26 (&&) x y = if' x y x
8b57a9e @tomlokhorst Initial checkin
authored
27
fb0abbd Made some changes.
Sebastiaan Visser authored
28 (||) :: Bool b b => b -> b -> b
e46e951 @tomlokhorst Renamed if_, maybee, eitherr
authored
29 (||) x y = if' x x y
8b57a9e @tomlokhorst Initial checkin
authored
30
40d631e @tomlokhorst Made some more changes, code is better now.
authored
31 otherwise :: Bool b r => b
32 otherwise = true
8b57a9e @tomlokhorst Initial checkin
authored
33
be9fc12 @tomlokhorst Moved stuff around
authored
34 not :: (Bool b b', Bool b' r) => b -> b'
35 not b = if' b false true
36
37 -- A type class for the normal Prelude.Maybe data type
8b57a9e @tomlokhorst Initial checkin
authored
38 class Maybe f a r | f -> a, f -> r where
40d631e @tomlokhorst Made some more changes, code is better now.
authored
39 maybe :: r -> (a -> r) -> f a -> r
8b57a9e @tomlokhorst Initial checkin
authored
40 nothing :: f a
41 just :: a -> f a
42
be9fc12 @tomlokhorst Moved stuff around
authored
43 -- A type class for the normal Prelude.Either data type
8b57a9e @tomlokhorst Initial checkin
authored
44 class Either f a b r | f -> a, f -> b, f -> r where
e46e951 @tomlokhorst Renamed if_, maybee, eitherr
authored
45 either :: (a -> r) -> (b -> r) -> f a b -> r
46 left :: a -> f a b
47 right :: b -> f a b
8b57a9e @tomlokhorst Initial checkin
authored
48
be9fc12 @tomlokhorst Moved stuff around
authored
49 -- A type class for the normal Prelude pair: (,)
8b57a9e @tomlokhorst Initial checkin
authored
50 class Tuple2 f a b r | f -> a, f -> b, f -> r where
51 tuple2 :: (a -> b -> r) -> f a b -> r
52 ctuple2 :: a -> b -> f a b
53
54 fst :: (Tuple2 f a b a) => f a b -> a
55 fst = tuple2 (\x _ -> x)
56
57 snd :: (Tuple2 f a b b) => f a b -> b
58 snd = tuple2 (\_ y -> y)
59
60 curry :: (Tuple2 f a b r) => (f a b -> c) -> a -> b -> c
61 curry f x y = f (ctuple2 x y)
62
63 uncurry :: (Tuple2 f a b r) => (a -> b -> r) -> f a b -> r
64 uncurry f t = tuple2 f t
65
be9fc12 @tomlokhorst Moved stuff around
authored
66 class List f a r | f -> a, f -> r where
67 list :: r -> (a -> r -> r) -> f a -> r
68 nil :: f a
69 cons :: a -> f a -> f a
70
71 map :: (List f a (g b), List g b r) => (a -> b) -> f a -> g b
72 map f = list nil (\x ys -> f x `cons` ys)
73
74 (++) :: (List f a (g a), List g a r) => f a -> g a -> g a
75 xs ++ ys = list ys cons xs
76
77 filter :: (List f a (g a), Bool b (g a), List g a r) =>
78 (a -> b) -> f a -> g a
79 filter p = list nil (\x xs -> bool xs (x `cons` xs) (p x))
f8df28b @tomlokhorst Working on Eq...
authored
80
be9fc12 @tomlokhorst Moved stuff around
authored
81 -- An equivalent type class for the normal Prelude.Eq type class,
82 -- now based on the Bool type class, instead of the Prelude.Bool data type
40d631e @tomlokhorst Made some more changes, code is better now.
authored
83 class Eq a b | a -> b where
f8df28b @tomlokhorst Working on Eq...
authored
84 (==), (/=) :: Bool b r => a -> a -> b
fb0abbd Made some changes.
Sebastiaan Visser authored
85
be9fc12 @tomlokhorst Moved stuff around
authored
86 class (Eq f r) => Ordering f r | f -> r where
87 ordering :: r -> r -> r -> f -> r
88 lt :: f
89 eq :: f
90 gt :: f
8b57a9e @tomlokhorst Initial checkin
authored
91
f8df28b @tomlokhorst Working on Eq...
authored
92 -- isLt :: (Ordering o b, Bool b r) => o -> b
93 -- isLt = ordering true false false
94 --
95 -- isEq :: (Ordering o b, Bool b r) => o -> b
96 -- isEq = ordering false true false
8b57a9e @tomlokhorst Initial checkin
authored
97
f8df28b @tomlokhorst Working on Eq...
authored
98 -- isGt :: (Ordering o b, Bool b r) => o -> b
99 -- isGt = ordering false false true
8b57a9e @tomlokhorst Initial checkin
authored
100
101
f8df28b @tomlokhorst Working on Eq...
authored
102 -- class (Eq a) => Ord a where
103 -- compare :: (Bool o o, Ordering o r) => a -> a -> o
104 -- (<), (<=), (>), (>=) :: Ordering o r => a -> a -> o
105 --
106 -- max, min :: a -> a -> a
8b57a9e @tomlokhorst Initial checkin
authored
107
108 -- compare x y = bool eq lt (x == x) --if_ (x == y)
109 -- eq lt
110 -- (if_ (x <= y)
111 -- lt
112 -- gt)
113
114 -- x < y = ordering true false false (compare x y)
115 -- x <= y = ordering true true false (compare x y)
116 -- x > y = ordering false false true (compare x y)
117 -- x >= y = ordering false true true (compare x y)
118 --
119 -- max x y = if_ (x <= y) y x
120 -- min x y = if_ (x <= y) x y
Something went wrong with that request. Please try again.