# Flip

Posted on 2018-02-17 by nbloomf

This post is literate Haskell; you can load the source into GHCi and play along.

{-# LANGUAGE NoImplicitPrelude #-}
module Flip (
flip, flip2, flip3, flip4, flip5, _test_flip, main_flip
) where

import Testing
import Functions

It turns out that equational proofs are much simpler to state and verify if we define new functions in the so-called pointfree style. A pointfree function definition is one that does not refer to its arguments explicitly; for example, suppose we have two functions $$f : A \rightarrow B$$ and $$g : B \rightarrow C$$, and have defined a new function $$h : A \rightarrow C$$ by $h(x) = g(f(x)).$ Of course this definition is equivalent to $h(x) = \compose(g)(f)(x).$ But now the rightmost argument, $$x$$, can be omitted, leaving $h = \compose(g)(f).$ This final statement is written in the pointfree style. We’ll see later that definitions will cooperate better with equational reasoning if they are in pointfree form. To make this work, we’ll need a stable of operators for manipulating functions, like $$\compose$$ in the above example. In all cases the goal will be to move function arguments around; for instance, $$\compose$$ lets us move a rightmost argument “up” one level of parentheses.

In this post we’ll define a family of operators that rearrange the arguments of a multi-argument function. The first example is $$\flip$$, which flips the arguments of a two-argument function.

Let $$A$$, $$B$$, and $$C$$ be sets. Given $$f : A \rightarrow B \rightarrow C$$, we define $$\flip(f) : B \rightarrow A \rightarrow C$$ by $\flip(f)(b)(a) = f(a)(b).$

flip :: (a -> b -> c) -> b -> a -> c
flip f b a = f a b

$$\flip$$ is an involution.

Let $$A$$, $$B$$, and $$C$$ be sets. For all $$f : A \rightarrow B \rightarrow C$$, we have $\flip(\flip(f)) = f.$

Let $$a \in A$$ and $$b \in B$$. Then we have $\begin{eqnarray*} & & \flip(\flip(f))(a)(b) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & \flip(f)(b)(a) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & f(a)(b) \end{eqnarray*}$ as needed.

_test_flip_involution
:: (Equal c)
=> a -> b -> c
-> Test ((a -> b -> c) -> a -> b -> Bool)
_test_flip_involution _ _ _ =
testName "flip(flip(f)) == f" $\f a b -> eq (flip (flip f) a b) (f a b) Ostensibly, $$\flip$$ acts on functions of two arguments. But since the return type of such a function can be another function, a better way to think of $$\flip$$ is that it flips the first two arguments of a multi-argument function. Let $$f : A \rightarrow B \rightarrow C \rightarrow D$$. Then we have $\flip(f)(b)(a)(c)(d) = f(a)(b)(c)(d).$ Note that $\begin{eqnarray*} & & \flip(f)(b)(a)(c) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & f(a)(b)(c) \end{eqnarray*}$ _test_flip_3 :: (Equal d) => a -> b -> c -> d -> Test ((a -> b -> c -> d) -> a -> b -> c -> Bool) _test_flip_3 _ _ _ _ = testName "flip(f)(a)(b)(c) == f(b)(a)(c)"$
\f a b c -> eq (flip f b a c) (f a b c)

Okay- what about more general permutations of function arguments? Let’s start with flipping the second and third.

Let $$A$$, $$B$$, $$C$$, and $$D$$ be sets. Given $$f : A \rightarrow B \rightarrow C \rightarrow D$$, we define $$\flipB(f) : A \rightarrow C \rightarrow B \rightarrow D$$ by $\flipB = \compose(\flip).$

flip2 :: (a -> b -> c -> d) -> a -> c -> b -> d
flip2 = compose flip

Now $$\flipB$$ permutes function arguments.

Let $$f : A \rightarrow B \rightarrow C \rightarrow D$$. Then $\flipB(f)(a)(c)(b) = f(a)(b)(c).$

We have $\begin{eqnarray*} & & \flipB(f)(a)(c)(b) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip2} = & \compose(\flip)(f)(a)(c)(b) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \flip(f(a))(c)(b) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & f(a)(b)(c) \end{eqnarray*}$ as claimed.

_test_flip2
:: (Equal d)
=> a -> b -> c -> d
-> Test ((a -> b -> c -> d) -> a -> b -> c -> Bool)
_test_flip2 _ _ _ _ =
testName "flip2(f)(a)(b)(c) == f(a)(c)(b)" $\f a b c -> eq (flip2 f a c b) (f a b c) And we can flip the third and fourth arguments. Let $$A$$, $$B$$, $$C$$, $$D$$, and $$E$$ be sets. We define $\flipC : (A \rightarrow B \rightarrow C \rightarrow D \rightarrow E) \rightarrow A \rightarrow B \rightarrow D \rightarrow C \rightarrow E$ by $\flipC = \compose(\compose(\flip)).$ In Haskell: flip3 :: (a -> b -> c -> d -> e) -> a -> b -> d -> c -> e flip3 = compose (compose flip) $$\flipC$$ does the thing: Let $$f : A \rightarrow B \rightarrow C \rightarrow D \rightarrow E$$. Then $\flipC(f)(a)(b)(d)(c) = f(a)(b)(c)(d).$ We have $\begin{eqnarray*} & & \flipC(f)(a)(b)(d)(c) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip3} = & \compose(\compose(\flip))(f)(a)(b)(d)(c) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\flip)(f(a))(b)(d)(c) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \flip(f(a)(b))(d)(c) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & f(a)(b)(c)(d) \end{eqnarray*}$ as claimed. _test_flip3 :: (Equal e) => a -> b -> c -> d -> e -> Test ((a -> b -> c -> d -> e) -> a -> b -> c -> d -> Bool) _test_flip3 _ _ _ _ _ = testName "flip3(f)(a)(b)(c)(d) == f(a)(b)(d)(c)"$
\f a b c d -> eq (flip3 f a b d c) (f a b c d)

We’ll look at two more examples, to see the pattern emerging.

We define $$\flipD : (A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F) \rightarrow A \rightarrow B \rightarrow C \rightarrow E \rightarrow D \rightarrow F$$ by $\flipD = \compose(\compose(\compose(\flip))).$

flip4
:: (a -> b -> c -> d -> e -> f)
-> a -> b -> c -> e -> d -> f

flip4 =
compose (compose (compose flip))

And:

Let $$f : A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F$$. Then $\flipD(f)(a)(b)(c)(e)(d) = f(a)(b)(c)(d)(e).$

We have $\begin{eqnarray*} & & \flipD(f)(a)(b)(c)(e)(d) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip4} = & \compose(\compose(\compose(\flip)))(f)(a)(b)(c)(e)(d) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\compose(\flip))(f(a))(b)(c)(e)(d) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\flip)(f(a)(b))(c)(e)(d) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \flip(f(a)(b)(c))(e)(d) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & f(a)(b)(c)(d)(e) \end{eqnarray*}$ as claimed.

_test_flip4 :: (Equal f)
=> a -> b -> c -> d -> e -> f
-> Test ((a -> b -> c -> d -> e -> f) -> a -> b -> c -> d -> e -> Bool)
_test_flip4 _ _ _ _ _ _ =
testName "flip4(f)(a)(b)(c)(d)(e) == f(a)(b)(c)(e)(d)" $\f a b c d e -> eq (flip4 f a b c e d) (f a b c d e) One more. We define $\flipE : (A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F \rightarrow G) \rightarrow A \rightarrow B \rightarrow C \rightarrow D \rightarrow F \rightarrow E \rightarrow G$ by $\flipE = \compose(\compose(\compose(\compose(\flip)))).$ In Haskell: flip5 :: (a -> b -> c -> d -> e -> f -> g) -> a -> b -> c -> d -> f -> e -> g flip5 = compose (compose (compose (compose flip))) And: Let $$w : A \rightarrow B \rightarrow C \rightarrow D \rightarrow E \rightarrow F \rightarrow G$$. Then $\flipE(w)(a)(b)(c)(d)(f)(e) = w(a)(b)(c)(d)(e)(f).$ We have $\begin{eqnarray*} & & \flipE(w)(a)(b)(c)(d)(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip5} = & \compose(\compose(\compose(\compose(\flip))))(w)(a)(b)(c)(d)(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\compose(\compose(\flip)))(w(a))(b)(c)(d)(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\compose(\flip))(w(a)(b))(c)(d)(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \compose(\flip)(w(a)(b)(c))(d)(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Functions.html#def-compose} = & \flip(w(a)(b)(c)(d))(f)(e) \\ & \href{/posts/arithmetic-made-difficult/Flip.html#def-flip} = & w(a)(b)(c)(d)(e)(f) \end{eqnarray*}$ as claimed. _test_flip5 :: (Equal g) => a -> b -> c -> d -> e -> f -> g -> Test ((a -> b -> c -> d -> e -> f -> g) -> a -> b -> c -> d -> e -> f -> Bool) _test_flip5 _ _ _ _ _ _ _ = testName "flip5(w)(a)(b)(c)(d)(e)(f) == w(a)(b)(c)(d)(f)(e)"$
\w a b c d e f -> eq (flip5 w a b c d f e) (w a b c d e f)

Well that’s neat. This pattern continues, and we can define an operator that flips the $$n$$th and $$(n+1)$$th argument, for any $$n$$, using just $$\compose$$ and $$\flip$$. And it turns out that this is enough to generate arbitrary permutations of the arguments of any function, by composing the flip operators in the right order. We won’t prove this in full generality here (not yet, anyway).

## Testing

Suite:

_test_flip ::
( Equal a, Show a, Arbitrary a, CoArbitrary a, TypeName a
, Equal b, Show b, Arbitrary b, CoArbitrary b, TypeName b
, Equal c, Show c, Arbitrary c, CoArbitrary c, TypeName c
, Equal d, Show d, Arbitrary d, CoArbitrary d, TypeName d
, Equal e, Show e, Arbitrary e, CoArbitrary e, TypeName e
, Equal f, Show f, Arbitrary f, CoArbitrary f, TypeName f
, Equal g, Show g, Arbitrary g, CoArbitrary g, TypeName g
) => Int -> Int -> a -> b -> c -> d -> e -> f -> g -> IO ()

_test_flip size cases a b c d e f g = do
testLabel0 "flip"
let args = testArgs size cases

runTest args (_test_flip_involution a b c)
runTest args (_test_flip_3 a b c d)
runTest args (_test_flip2 a b c d)
runTest args (_test_flip3 a b c d e)
runTest args (_test_flip4 a b c d e f)
runTest args (_test_flip5 a b c d e f g)

Main:

main_flip :: IO ()
main_flip = do
_test_flip 1 1 () () () () () () ()