on master help sign in
Merge pull request #4116 from Synthetica9/Combinators

Added various combinators, most of which are also present in Haskell

commit1be3ed752f8d47e4fd705bb682cf73652ad267ba authorAhmad Salim Al-Sibahi <asal@itu.dk> dateThu 12 Oct 2017, 09:35 (9 years ago) parent1eae8f50b7b908
4 files changed+82
libs/contrib/Data/Combinators.idr+39 libs/contrib/Data/Combinators/Applicative.idr+27 libs/contrib/Data/Combinators/Arrow.idr+15 libs/contrib/contrib.ipkg+1
libs/contrib/Data/Combinators.idrnew file+39
@@ -0,0 +1,39 @@
1+||| Combinators that are thought to be at least situationally useful, mostly for
2+||| tacit (or point-free) programming. "To Mock a Mockingbird" refers to the book
3+||| "To Mock a Mockingbird and Other Logic Puzzles: Including an Amazing Adventure
4+||| in Combinatory Logic", by Raymond Smullyan (which the author of this module
5+||| hasn't actually read). See
6+||| http://www.angelfire.com/tx4/cus/combinator/birds.html and
7+||| https://hackage.haskell.org/package/data-aviary-0.4.0/docs/Data-Aviary-Birds.html
8+||| an (incomplete?) list of birds introduced. Also includes some operations defined
9+||| over Arrows.
10+
11+module Data.Combinators
12+
13+%default total
14+%access public export
15+
16+||| Psi combinator - psi bird (?) - Haskell on.
17+on : (b -> b -> c) -> (a -> b) -> (a -> a -> c)
18+on f g x y = g x `f` g y
19+
20+
21+infixl 9 ...
22+||| Blackbird, as named in "To Mock a Mockingbird". This is like
23+||| function composition, but second function takes two arguments. See the talk
24+||| "Point-Free or Die: Tacit Programming in Haskell and Beyond" by Amar Shah
25+||| (https://youtu.be/seVSlKazsNk).
26+(...) : (c -> d) -> (a -> b -> c) -> (a -> b -> d)
27+(...) = (.) . (.)
28+
29+||| Warbler, as named in "To Mock a Mockingbird".
30+||| See http://code.jsoftware.com/wiki/Vocabulary/tilde
31+||| Equivalent to `join` on the Reader monad (`(->) e` in Haskell)
32+reflex : (a -> a -> b) -> (a -> b)
33+reflex f x = f x x
34+
35+||| Phoenix, according to Data.Aviary.
36+||| See http://code.jsoftware.com/wiki/Vocabulary/fork.
37+||| Equivalent to `liftA2` on the Reader monad (`(->) e` in Haskell)
38+fork2 : (b -> c -> d) -> (a -> b) -> (a -> c) -> (a -> d)
39+fork2 f g h x = f (g x) (h x)
libs/contrib/Data/Combinators/Applicative.idrnew file+27
@@ -0,0 +1,27 @@
1+||| Enable applicative-style syntax (Mostly f <$> g <*> h and idiom brackets)
2+||| for function composition:
3+module Data.Combinators.Applicative
4+
5+import Data.Combinators
6+
7+%default total
8+%access public export
9+
10+||| Pure is just const, also known as the K combinator
11+pure : a -> b -> a
12+pure = const
13+
14+||| `map` is (.), but isn't here to discourage that use of the syntax.
15+||| `foo <$> bar <*> baz` applies `foo` to the results of `bar` and `baz`,
16+||| and generalizes to multiple arguments.
17+(<$>) : (b -> c) -> (a -> b) -> (a -> c)
18+(<$>) f g x = f (g x)
19+
20+||| Starling, as named in "To Mock a Mockingbird". This is the S combinator.
21+||| Equivalent to `<*>` on the Reader monad (`(->) e` in Haskell).
22+||| See http://code.jsoftware.com/wiki/Vocabulary/hook.
23+(<*>) : (a -> b -> c) -> (a -> b)-> (a -> c)
24+(<*>) f g x = f x (g x)
25+
26+liftA2 : (b -> c -> d) -> (a -> b) -> (a -> c) -> (a -> d)
27+liftA2 = fork2
libs/contrib/Data/Combinators/Arrow.idrnew file+15
@@ -0,0 +1,15 @@
1+||| Combinators from Control.Arrow
2+
3+first : (a -> b) -> (a, c) -> (b, c)
4+first f (a, c) = (f a, c)
5+
6+second : (a -> b) -> ((c, a) -> (c, b))
7+second f (c, a) = (c, f a)
8+
9+infixr 3 ***
10+(***) : (a -> b) -> (a' -> b') -> ((a, a') -> (b, b'))
11+(***) f g (a, a') = (f a, g a')
12+
13+infixr 3 &&&
14+(&&&) : (a -> b) -> (a -> b') -> (a -> (b, b'))
15+(&&&) f g x = (f x, g x)
libs/contrib/contrib.ipkg+1
@@ -25,6 +25,7 @@ modules = CFFI, CFFI.Types, CFFI.Memory,
2525 Data.CoList, Data.Storable,
2626 Data.List.Zipper,
2727 Data.Pairs.Implicit,
28+ Data.Combinators, Data.Combinators.Applicative, Data.Combinators.Arrow,
2829
2930 Decidable.Decidable, Decidable.Order,
3031