0 | module Prelude.Interfaces
3 | import Prelude.Basics
19 | interface Semigroup ty where
20 | constructor MkSemigroup
21 | (<+>) : ty -> ty -> ty
33 | interface Semigroup ty => Monoid ty where
34 | constructor MkMonoid
46 | Semigroup a => Semigroup b => Semigroup (a, b) where
47 | (x, y) <+> (v, w) = (x <+> v, y <+> w)
50 | Monoid a => Monoid b => Monoid (a, b) where
51 | neutral = (neutral, neutral)
54 | Semigroup Ordering where
60 | Monoid Ordering where
64 | Semigroup b => Semigroup (a -> b) where
65 | (f <+> g) x = f x <+> g x
68 | Monoid b => Monoid (a -> b) where
78 | interface Functor f where
79 | constructor MkFunctor
83 | map : (func : a -> b) -> f a -> f b
89 | %inline %tcinline public export
90 | (<$>) : Functor f => (func : a -> b) -> f a -> f b
91 | (<$>) func x = map func x
97 | %inline %tcinline public export
98 | (<&>) : Functor f => f a -> (func : a -> b) -> f b
99 | (<&>) x func = map func x
102 | %inline %tcinline public export
103 | (<$) : Functor f => b -> f a -> f b
104 | (<$) b = map (const b)
107 | %inline %tcinline public export
108 | ($>) : Functor f => f a -> b -> f b
109 | ($>) fa b = map (const b) fa
112 | %inline %tcinline public export
113 | ignore : Functor f => f a -> f ()
114 | ignore = map (const ())
119 | [Compose] (l : Functor f) => (r : Functor g) => Functor (f . g) where
126 | interface Bifunctor f where
127 | constructor MkBifunctor
135 | bimap : (a -> c) -> (b -> d) -> f a b -> f c d
136 | bimap f g = mapFst f . mapSnd g
145 | mapFst : (a -> c) -> f a b -> f c b
146 | mapFst f = bimap f id
155 | mapSnd : (b -> d) -> f a b -> f a d
158 | public export %tcinline
159 | mapHom : Bifunctor f => (a -> b) -> f a a -> f b b
160 | mapHom f = bimap f f
162 | namespace Bifunctor
165 | public export %inline
166 | [Compose] (l : Functor f) => (r : Bifunctor g) => Bifunctor (f .: g) where
167 | bimap = map .: bimap
168 | mapFst = map . mapFst
169 | mapSnd = map . mapSnd
172 | interface Functor f => Applicative f where
173 | constructor MkApplicative
175 | (<*>) : f (a -> b) -> f a -> f b
177 | public export %tcinline
178 | (<*) : Applicative f => f a -> f b -> f a
179 | a <* b = map const a <*> b
181 | public export %tcinline
182 | (*>) : Applicative f => f a -> f b -> f b
183 | a *> b = map (const id) a <*> b
185 | %allow_overloads pure
186 | %allow_overloads (<*)
187 | %allow_overloads (*>)
189 | namespace Applicative
192 | [Compose] (l : Applicative f) => (r : Applicative g) => Applicative (f . g)
193 | using Functor.Compose where
195 | fun <*> x = [| fun <*> x |]
201 | interface Applicative f => Alternative f where
202 | constructor MkAlternative
204 | (<|>) : f a -> Lazy (f a) -> f a
210 | interface Applicative m => Monad m where
211 | constructor MkMonad
214 | (>>=) : m a -> (a -> m b) -> m b
218 | join : m (m a) -> m a
221 | (>>=) x f = join (f <$> x)
224 | %allow_overloads (>>=)
227 | %inline %tcinline public export
228 | (=<<) : Monad m => (a -> m b) -> m a -> m b
232 | %inline %tcinline public export
233 | (>>) : Monad m => m () -> Lazy (m b) -> m b
234 | a >> b = a >>= \_ => b
237 | public export %tcinline
238 | (>=>) : Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
239 | (>=>) f g = \x => f x >>= g
242 | public export %tcinline
243 | (<=<) : Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
248 | guard : Alternative f => Bool -> f ()
249 | guard x = if x then pure () else empty
253 | when : Applicative f => Bool -> Lazy (f ()) -> f ()
255 | when False f = pure ()
258 | %inline %tcinline public export
259 | unless : Applicative f => Bool -> Lazy (f ()) -> f ()
260 | unless = when . not
272 | interface Foldable t where
273 | constructor MkFoldable
280 | foldr : (func : elem -> acc -> acc) -> (init : acc) -> (input : t elem) -> acc
287 | foldl : (func : acc -> elem -> acc) -> (init : acc) -> (input : t elem) -> acc
288 | foldl f z t = foldr (flip (.) . flip f) id t z
292 | null : t elem -> Bool
293 | null xs = foldr {acc = Lazy Bool} (\ _,_ => False) True xs
297 | foldlM : Monad m => (funcM : acc -> elem -> m acc) -> (init : acc) -> (input : t elem) -> m acc
298 | foldlM fm a0 = foldl (\ma, b => ma >>= flip fm b) (pure a0)
301 | toList : t elem -> List elem
302 | toList = foldr (::) []
308 | foldMap : Monoid m => (f : a -> m) -> t a -> m
309 | foldMap f = foldr ((<+>) . f) neutral
312 | %inline %tcinline public export
313 | concat : Monoid a => Foldable t => t a -> a
314 | concat = foldMap id
318 | %inline %tcinline public export
319 | concatMap : Monoid m => Foldable t => (a -> m) -> t a -> m
320 | concatMap = foldMap
322 | namespace Bool.Lazy
323 | namespace Semigroup
325 | [Any] Semigroup (Lazy Bool) where
326 | x <+> y = force x || y
329 | [All] Semigroup (Lazy Bool) where
330 | x <+> y = force x && y
334 | [Any] Monoid (Lazy Bool) using Semigroup.Any where
335 | neutral = delay False
338 | [All] Monoid (Lazy Bool) using Semigroup.All where
339 | neutral = delay True
344 | public export %tcinline
345 | and : Foldable t => t (Lazy Bool) -> Bool
346 | and = force . concat @{All}
351 | public export %tcinline
352 | or : Foldable t => t (Lazy Bool) -> Bool
353 | or = force . concat @{Any}
356 | namespace Semigroup
358 | [Any] Semigroup Bool where
359 | x <+> y = x || delay y
362 | [All] Semigroup Bool where
363 | x <+> y = x && delay y
367 | [Any] Monoid Bool using Bool.Semigroup.Any where
371 | [All] Monoid Bool using Bool.Semigroup.All where
376 | %inline %tcinline public export
377 | any : Foldable t => (a -> Bool) -> t a -> Bool
378 | any = foldMap @{%search} @{Any}
382 | %inline %tcinline public export
383 | all : Foldable t => (a -> Bool) -> t a -> Bool
384 | all = foldMap @{%search} @{All}
387 | namespace Semigroup
389 | [Additive] Num a => Semigroup a where
393 | [Multiplicative] Num a => Semigroup a where
398 | [Additive] Num a => Monoid a using Semigroup.Additive where
402 | [Multiplicative] Num a => Monoid a using Semigroup.Multiplicative where
406 | public export %tcinline
407 | sum : Num a => Foldable t => t a -> a
408 | sum = concat @{Additive}
413 | sum' : Num a => Foldable t => t a -> a
417 | public export %tcinline
418 | product : Num a => Foldable t => t a -> a
419 | product = concat @{Multiplicative}
424 | product' : Num a => Foldable t => t a -> a
429 | public export %tcinline
430 | traverse_ : Applicative f => Foldable t => (a -> f b) -> t a -> f ()
431 | traverse_ f = foldr ((*>) . f) (pure ())
434 | public export %tcinline
435 | sequence_ : Applicative f => Foldable t => t (f a) -> f ()
436 | sequence_ = foldr (*>) (pure ())
439 | public export %tcinline
440 | for_ : Applicative f => Foldable t => t a -> (a -> f b) -> f ()
441 | for_ = flip traverse_
444 | [SemigroupApplicative] Applicative f => Semigroup a => Semigroup (f a) where
445 | x <+> y = [| x <+> y |]
448 | [MonoidApplicative] Applicative f => Monoid a => Monoid (f a) using SemigroupApplicative where
449 | neutral = pure neutral
453 | [SemigroupAlternative] Alternative f => Semigroup (Lazy (f a)) where
454 | x <+> y = force x <|> y
457 | [MonoidAlternative] Alternative f => Monoid (Lazy (f a)) using Lazy.SemigroupAlternative where
458 | neutral = delay empty
461 | [SemigroupAlternative] Alternative f => Semigroup (f a) where
462 | x <+> y = x <|> delay y
465 | [MonoidAlternative] Alternative f => Monoid (f a) using Interfaces.SemigroupAlternative where
488 | %inline %tcinline public export
489 | choice : Alternative f => Foldable t => t (Lazy (f a)) -> f a
490 | choice = force . concat @{Lazy.MonoidAlternative}
493 | %inline %tcinline public export
494 | choiceMap : Alternative f => Foldable t => (a -> f b) -> t a -> f b
495 | choiceMap = foldMap @{%search} @{MonoidAlternative}
499 | public export %tcinline
500 | [Compose] (l : Foldable t) => (r : Foldable f) => Foldable (t . f) where
501 | foldr = foldr . flip . foldr
502 | foldl = foldl . foldl
503 | null tf = null tf || all null tf
504 | foldMap = foldMap . foldMap
511 | interface Bifoldable p where
512 | constructor MkBifoldable
513 | bifoldr : (a -> acc -> acc) -> (b -> acc -> acc) -> acc -> p a b -> acc
515 | bifoldl : (acc -> a -> acc) -> (acc -> b -> acc) -> acc -> p a b -> acc
516 | bifoldl f g z t = bifoldr (flip (.) . flip f) (flip (.) . flip g) id t z
518 | binull : p a b -> Bool
519 | binull t = bifoldr {acc = Lazy Bool} (\ _,_ => False) (\ _,_ => False) True t
522 | public export %tcinline
523 | bifoldMap : Monoid acc => Bifoldable p => (a -> acc) -> (b -> acc) -> p a b -> acc
524 | bifoldMap f g = bifoldr ((<+>) . f) ((<+>) . g) neutral
527 | public export %tcinline
528 | bifoldMapFst : Monoid acc => Bifoldable p => (a -> acc) -> p a b -> acc
529 | bifoldMapFst f = bifoldMap f (const neutral)
531 | namespace Bifoldable
534 | public export %tcinline
535 | [Compose] (l : Foldable f) => (r : Bifoldable p) => Bifoldable (f .: p) where
536 | bifoldr = foldr .: flip .: bifoldr
537 | bifoldl = foldl .: bifoldl
538 | binull fp = null fp || all binull fp
541 | interface (Functor t,
Foldable t) => Traversable t where
542 | constructor MkTraversable
545 | traverse : Applicative f => (a -> f b) -> t a -> f (t b)
548 | public export %tcinline
549 | sequence : Applicative f => Traversable t => t (f a) -> f (t a)
550 | sequence = traverse id
553 | %inline %tcinline public export
554 | for : Applicative f => Traversable t => t a -> (a -> f b) -> f (t b)
555 | for = flip traverse
558 | interface (Bifunctor p,
Bifoldable p) => Bitraversable p where
559 | constructor MkBitraversable
562 | bitraverse : Applicative f => (a -> f c) -> (b -> f d) -> p a b -> f (p c d)
565 | public export %tcinline
566 | bisequence : Applicative f => Bitraversable p => p (f a) (f b) -> f (p a b)
567 | bisequence = bitraverse id id
570 | public export %tcinline
571 | bifor : Applicative f => Bitraversable p
576 | bifor t f g = bitraverse f g t
578 | namespace Traversable
580 | public export %tcinline
581 | [Compose] (l : Traversable t) => (r : Traversable f) => Traversable (t . f)
582 | using Foldable.Compose Functor.Compose where
583 | traverse = traverse . traverse
585 | namespace Bitraversable
587 | public export %tcinline
588 | [Compose] (l : Traversable t) => (r : Bitraversable p) => Bitraversable (t .: p)
589 | using Bifoldable.Compose Bifunctor.Compose where
590 | bitraverse = traverse .: bitraverse
594 | public export %tcinline
595 | [Compose] (l : Monad m) => (r : Monad t) => (tr : Traversable t) => Monad (m . t)
596 | using Applicative.Compose where
597 | a >>= f = a >>= map join . traverse f