Modified syntax slightly (using `rec` keyword)
Getty Ritter
10 years ago
| 1 | *~ |
| 1 | exports | |
| 2 | Category(..) | |
| 3 | _>>>_ | |
| 4 | _<<<_ | |
| 5 | arrowCategory | |
| 6 | ||
| 7 | record Category cat | |
| 8 | .id (cat a a) | |
| 9 | .compose (cat b c -> cat a b -> cat a c) | |
| 10 | ||
| 11 | _>>>_ : implicit (Category cat) -> cat a b -> cat b c -> cat a c | |
| 12 | c = flip c.compose | |
| 13 | ||
| 14 | _<<<_ : implicit (Category cat) -> cat b c -> cat a b -> cat a c | |
| 15 | c = c.compose | |
| 16 | ||
| 17 | implicit arrowCategory : Category _->_ = rec | |
| 18 | .id = id | |
| 19 | .compose = compose |
| 1 | 1 | exports |
| 2 | ( HasLength(..) | |
| 3 | , length | |
| 4 | , null | |
| 5 | , Sequence(..) | |
| 6 | , Functor(..) | |
| 7 | , map | |
| 8 |
|
|
| 2 | Functor(..) | |
| 3 | HasLength(..) | |
| 4 | Sequence(..) | |
| 5 | length | |
| 6 | map | |
| 7 | null | |
| 9 | 8 | |
| 10 |
record HasLength f = |
|
| 9 | record HasLength f = .length (f -> Nat) | |
| 11 | 10 | |
| 12 | 11 | length : implicit (HasLength l) -> l -> Nat |
| 13 |
|
|
| 12 | l = l.length | |
| 14 | 13 | |
| 15 | 14 | null : implicit (HasLength l) -> l -> Bool |
| 16 |
|
|
| 15 | l ls = l.length ls == 0 | |
| 17 | 16 | |
| 18 | 17 | record Sequence f = |
| 19 | { .toList (f a -> List a) | |
| 20 | , .fromList (List a -> f a) | |
| 21 |
|
|
| 18 | .toList (f a -> List a) | |
| 19 | .fromList (List a -> f a) | |
| 22 | 20 | |
| 23 |
record Functor f = |
|
| 21 | record Functor f = .map ((a -> b) -> f a -> f b) | |
| 24 | 22 | |
| 25 | 23 | map : implicit (Functor f) -> (a -> b) -> f a -> f b |
| 26 | 24 | f = f.map |
| 1 | import list | |
| 2 | import monoid | |
| 1 | exports | |
| 2 | listHasLength | |
| 3 | listSequence | |
| 4 | listFunctor | |
| 5 | listMonoid | |
| 6 | head | |
| 7 | tail | |
| 3 | 8 | |
| 4 | implicit listHasLength : HasLength List = | |
| 5 | { .length | |
| 6 | Cons _ xs = 1 + length xs | |
| 7 | Nil = 0 | |
| 8 | } | |
| 9 | import Monoid | |
| 9 | 10 | |
| 10 | implicit listSequence : Sequence List = | |
| 11 | { .toList = id | |
| 12 | , .fromList = id | |
| 13 | } | |
| 11 | implicit listHasLength : HasLength List = rec | |
| 12 | .length | |
| 13 | Cons _ xs = 1 + length xs | |
| 14 | Nil = 0 | |
| 14 | 15 | |
| 15 | implicit listFunctor : Functor List = | |
| 16 | { .map | |
| 17 | f (Cons x xs) = Cons (f x) xs | |
| 18 | _ Nil = Nil | |
| 19 | } | |
| 16 | implicit listSequence : Sequence List = rec | |
| 17 | .toList = id | |
| 18 | .fromList = id | |
| 20 | 19 | |
| 21 | implicit listMonoid : Monoid List = | |
| 22 | { .empty = [] | |
| 23 | , .append | |
| 24 | Nil ys = ys | |
| 25 | (Cons x xs) ys = Cons x (append xs ys) | |
| 26 | } | |
| 20 | implicit listFunctor : Functor List = rec | |
| 21 | .map | |
| 22 | f (Cons x xs) = Cons (f x) xs | |
| 23 | _ Nil = Nil | |
| 24 | ||
| 25 | implicit listMonoid : Monoid List = rec | |
| 26 | .empty = [] | |
| 27 | .append | |
| 28 | Nil ys = ys | |
| 29 | (Cons x xs) ys = Cons x (append xs ys) | |
| 27 | 30 | |
| 28 | 31 | head : List a -> a |
| 29 | 32 | x :: _ = x |
| 1 | 1 | exports |
| 2 | ( Monoid(..) | |
| 3 | , empty | |
| 4 | , _<>_ | |
| 5 | ) | |
| 2 | Monoid(..) | |
| 3 | empty | |
| 4 | _<>_ | |
| 5 | unitMonoid | |
| 6 | additive | |
| 7 | multiplicative | |
| 8 | conjunctive | |
| 9 | disjunctive | |
| 10 | dual | |
| 11 | endo | |
| 12 | reader | |
| 13 | pair | |
| 6 | 14 | |
| 7 | 15 | import Num |
| 8 | 16 | |
| 9 | 17 | -- The Monoid type |
| 10 | 18 | |
| 11 | record Monoid m = | |
| 12 | { .empty m | |
| 13 | , .append (m -> m -> m) | |
| 14 | } | |
| 19 | record Monoid m | |
| 20 | .empty m | |
| 21 | .append (m -> m -> m) | |
| 15 | 22 | |
| 16 | 23 | -- implicit sugars for the Monoid operations |
| 17 | 24 | |
| 23 | 30 | |
| 24 | 31 | -- some Monoid values |
| 25 | 32 | |
| 26 | implicit unitMonoid : Monoid () = | |
| 27 | { .empty = () | |
| 28 | , .append _ _ = () | |
| 29 | } | |
| 33 | implicit unitMonoid : Monoid () = rec | |
| 34 | .empty = () | |
| 35 | .append _ _ = () | |
| 30 | 36 | |
| 31 | 37 | additive : implicit (Add m) -> Monoid m |
| 32 | add = { .empty = add.zero | |
| 33 | , .append = add.plus | |
| 34 |
|
|
| 38 | add = rec .empty = add.zero | |
| 39 | .append = add.plus | |
| 35 | 40 | |
| 36 | 41 | multiplicative : implicit (Mul m) -> Monoid m |
| 37 | mul = { .empty = mul.one | |
| 38 | , .append = mul.times | |
| 39 |
|
|
| 42 | mul = rec .empty = mul.one | |
| 43 | .append = mul.times | |
| 40 | 44 | |
| 41 | implicit conjunctive : Monoid Bool = | |
| 42 | { .empty = False | |
| 43 | , .append = _&&_ | |
| 44 | } | |
| 45 | implicit conjunctive : Monoid Bool = rec | |
| 46 | .empty = False | |
| 47 | .append = _&&_ | |
| 45 | 48 | |
| 46 | disjunctive : Monoid Bool = | |
| 47 | { .empty = True | |
| 48 | , .append = _||_ | |
| 49 | } | |
| 49 | disjunctive : Monoid Bool = rec | |
| 50 | .empty = True | |
| 51 | .append = _||_ | |
| 50 | 52 | |
| 51 | 53 | dual : Monoid m -> Monoid m |
| 52 | m = { .empty = m.empty | |
| 53 | , .append x y = m.append y x | |
| 54 |
|
|
| 54 | m = rec .empty = m.empty | |
| 55 | .append x y = m.append y x | |
| 55 | 56 | |
| 56 | endo : Monoid (a -> a) = | |
| 57 | { .empty = id | |
| 58 | , .append x y = compose x y | |
| 59 | } | |
| 57 | endo : Monoid (a -> a) = rec | |
| 58 | .empty = id | |
| 59 | .append x y = compose x y | |
| 60 | 60 | |
| 61 | 61 | reader : implicit (Monoid b) -> Monoid (a -> b) |
| 62 | m = { .empty _ = m.empty | |
| 63 | , .append x f g = m.append (f x) (g x) | |
| 64 |
|
|
| 62 | m = rec .empty _ = m.empty | |
| 63 | .append x f g = m.append (f x) (g x) | |
| 65 | 64 | |
| 66 | 65 | pair : implicit (Monoid l) -> implicit (Monoid r) -> Monoid (l * r) |
| 67 | l r = { .empty = (l.empty, r.empty) | |
| 68 | , .append (x1, y1) (x2, y2) = (l.append x1 x2, r.append y1 y2) | |
| 69 |
|
|
| 66 | l r = rec .empty = (l.empty, r.empty) | |
| 67 | .append (x1, y1) (x2, y2) = (l.append x1 x2, r.append y1 y2) | |
| 1 | 1 | exports |
| 2 | ( Add(..) | |
| 3 | , Mul(..) | |
| 4 | , _+_ | |
| 5 | , _*_ | |
| 6 |
|
|
| 2 | Add(..) | |
| 3 | Mul(..) | |
| 4 | _+_ | |
| 5 | _*_ | |
| 7 | 6 | |
| 8 | 7 | -- mathematical operations |
| 9 | 8 | |
| 10 |
record IsNumber n = |
|
| 9 | record IsNumber n = .fromInteger (Int -> n) | |
| 11 | 10 | |
| 12 | 11 | record Add n = |
| 13 | { .zero n | |
| 14 | , .plus (n -> n -> n) | |
| 15 |
|
|
| 12 | .zero n | |
| 13 | .plus (n -> n -> n) | |
| 16 | 14 | |
| 17 | 15 | record Mul n = |
| 18 | { .one n | |
| 19 | , .times (n -> n -> n) | |
| 20 |
|
|
| 16 | .one n | |
| 17 | .times (n -> n -> n) | |
| 21 | 18 | |
| 22 | 19 | record Sub n = |
| 23 | { .pSubAdd (Add n) | |
| 24 | , .sub (n -> n -> n) | |
| 25 |
|
|
| 20 | .pSubAdd (Add n) | |
| 21 | .sub (n -> n -> n) | |
| 26 | 22 | |
| 27 | 23 | record Div n = |
| 28 | { .pDivMul (Mul n) | |
| 29 | , .div (n -> n -> n) | |
| 30 | , .mod (n -> n -> n) | |
| 31 | , .rem (n -> n -> n) | |
| 32 |
|
|
| 24 | .pDivMul (Mul n) | |
| 25 | .div (n -> n -> n) | |
| 26 | .mod (n -> n -> n) | |
| 27 | .rem (n -> n -> n) | |
| 33 | 28 | |
| 34 | 29 | -- some teaspoons for mathematical operations |
| 35 | 30 |