9

I'm having trouble with currying a function to remove three arguments in Haskell.

Disclaimer: Not Coursework, I was asked this question by someone struggling with this today and it's been bugging me.

The custom types/functions we were given were (can only remember types)

type MyThing
  = (Char, String)
type MyThings
  = [MyThing]

funcA :: MyThings -> String -> String
funcB :: MyThings -> String -> Int -> String

We started with:

funcB as str n = iterate (funcA as) str !! n

And reduced it down as follows:

funcB as str n = iterate (funcA as) str !! n
funcB as str = (!!) . (iterate (funcA as)) str
funcB as = (!!) . (iterate (funcA as))
funcB as = (!!) . (iterate . funcA) as

Then, stuck. We just can't figure out how to avoid using the last argument. I know I've seen a similar situation somewhere before and there was a solution.

Hoping some Haskell genius can point out why I'm being an idiot...

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58

3 Answers3

15

All you need here is the following three "laws" of operator sections:

(a `op` b) = (a `op`) b = (`op` b) a = op a b
          (1)          (2)          (3)

so that the operand goes into the free slot near the operator.

For (.) this means that: (a . b) = (a .) b = (. b) a = (.) a b. So,

f (g x) y !! n      
= (!!) (f (g x) y) n              by (3) 
= ((!!) . f (g x)) y n
= ((!!) . (f . g) x) y n
= ((!!) .) ((f . g) x) y n        by (1)
= (((!!) .) . (f . g)) x y n
= (((!!) .) . f . g) x y n

You should only do as much pointfree transformation as you're comfortable with, so that the resulting expression is still readable for you - and in fact, clearer than the original. The "pointfree" tool can at times produce unreadable results.

It is perfectly OK to stop in the middle. If it's too hard for you to complete it manually, probably it will be hard for you to read it, too.

((a .) . b) x y = (a .) (b x) y = (a . b x) y = a (b x y) is a common pattern that you will quickly learn to recognize immediately. So the above expression can be read back fairly easily as

(!!) ((f . g) x y) n = f (g x) y !! n

considering that (.) is associative:

(a . b . c) = ((a . b) . c) = (a . (b . c))
Will Ness
  • 70,110
  • 9
  • 98
  • 181
11
funcB = ((!!) .) . iterate . funcA

I think you did all the hard work, and there was just one tiny step left.

You can indeed do this automatically with pointfree. See the HaskellWiki page

As it says in the github readme, once you've installed it, you can edit your ghci.conf or .ghci file with the line

:def pf \str -> return $ ":! pointfree \"" ++ str ++ "\""

and then in ghci when you type

:pf funcB as = (!!) . (iterate . funcA) as

or even

:pf funcB as str n = iterate (funcA as) str !! n

you get

funcB = ((!!) .) . iterate . funcA
AndrewC
  • 32,300
  • 7
  • 79
  • 115
  • 3
    That's not eta reduction. Eta reduction is moving from `\x -> e x` to `e`. The transformation you've given (moving from `\x -> f (g x)` to `f . g`) is something else -- perhaps delta expansion of `(.)`? – Daniel Wagner Oct 30 '12 at 17:36
  • If you read a little more carefully you'll see I didn't make the transformation you said either! – AndrewC Oct 30 '12 at 19:57
  • Yeah, I realized afterwards that delta expansion was only a part of the transformation you made, but it had already passed the five-minute-no-takebacks mark! – Daniel Wagner Oct 30 '12 at 20:48
5

The key observation for me is that infix operators can be written prefix:

funcB as = (!!) . (iterate . funcA) as
funcB as = (.) (!!) ((iterate . funcA) as)

Once you've gotten here, you have half a chance of recognizing that this is a composition, with (.) (!!) as the first argument and iterate . funcA as the second argument:

funcB as = (  ((.) (!!)) . (iterate . funcA)  ) as

Now it's clear how to simplify this; after that, there's a lot of aesthetic choices about how to write it. For example, we might observe that (.) is associative, and so we can drop some parentheses; likewise, we can use operator sections to merge the unsightly ((.) (!!)) if you think it's more readable that way.

funcB = (  ((.) (!!)) . (iterate . funcA)  )
funcB = (.) (!!) . iterate . funcA -- uncontroversial parenthesis removal
funcB = ((!!) .) . iterate . funcA -- possibly controversial section rewrite

By the way, I don't think the beginning of your derivation is correct. You reached the right conclusion, but via incorrect middle steps. Corrected, it should look like this:

funcB as str n = iterate (funcA as) str !! n
funcB as str n = (!!) (iterate (funcA as) str) n
funcB as str = (!!) (iterate (funcA as) str)
funcB as = (!!) . iterate (funcA as)
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380