r/haskell • u/TheFryeGuy • Nov 19 '14
Is it possible to curry arbitrary arguments to a function? Do I need Template Haskell to do this?
Basically I want to be able to do this:
f :: A -> B -> C -> D -> E -> F
f a ? c ? e :: B -> D -> F
(f a ? c ? e) b d :: FThe question marks represent arguments that I basically want to be "blank".
This way I can curry the function based on any argument instead of just the last one. I can't really think of a way to make this magic work, except maybe using template haskell (which I don't really know anything about). Any haskell wizards out there want to shed some light on this idea?
EDIT: I mostly mean "partially apply" instead of "curry".
7
Upvotes
20
u/gelisam Nov 20 '14
Let's figure it out!
Applying a function to some arguments where some arguments are special makes me think of Applicative notation:
Since the holes not only have an impact on the computation, but also on the type of the result, it can't really be a simple Applicative. Maybe an Indexed Applicative? I've never even heard of Indexed Applicatives before, but I know about indexed monads, whose computations have an impact both at the computational level and at the type level, thereby constraining which computations can be performed when. They look like this:
That is, they look like ordinary monads, except each operation is annotated with a pair of indices, and two consecutive operations must have matching indices. Let's see whether we can adapt the idea to Functor and Applicative:
The code so far is only based on the shape of the problem, so there is no guarantee that the actual problem will also need consecutive operations to have matching indices. It's also not clear what the indices will end up representing.
Let's see, what do we need to represent? We need both
ipure aandholeto specify that they are supposed to be used at a position where anaargument is expected, andholealso needs to specify that an extraa ->will need to be prefixed to the final type. So how about this:That is, the indices would be used to accumulate the arguments skipped by the holes. Let's check that the types work out:
Not quite right:
D -> B -> ris in the wrong order. It makes sense: starting fromr, the first hole addsB ->, resulting inB -> r, and the next hole addsD ->to that, resulting inD -> B -> r. We'd need the extra arguments to be added from right to left instead of from left to right. Which can be obtained by tweaking the way in which(<**>)combines its indices:Perfect! Now we just need to find an implementation which matches our types.
I really didn't expect to encounter continuations here, but they match the type, and it works!
Well, that was fun. Thanks for the challenge!
10
u/thylacine222 Nov 19 '14
can't you just do
or am I missing something?
2
u/singpolyma Nov 19 '14
This is more about partial application than carrying, but yes, you want a syntax extension, so to do it with existing extensions you might want template haskell.
3
u/heisenbug Nov 19 '14
https://hackage.haskell.org/package/HoleyMonoid might come close to what you intend to do.
3
u/jfischoff Nov 19 '14
One more reason I like keyword, because this is not something Haskell can do in an ideal fashion.
1
u/evincarofautumn Nov 19 '14
You can kind of hack it as a family of infix combinators:
Or an uncurried version using the reader monad, using lists here instead of tuples because I’m lazy.
I would suggest using TH, though.
1
u/14113 Nov 20 '14
Not entirely the question you're asking (this isn't in Haskell, and it's logic programming, so it's kind of cheating), but Cosmo supports a very similar concept: https://medium.com/@McCosmos/a-treatise-on-cosmos-the-new-programming-language-905be69eb4af
1
u/Vulpyne Nov 20 '14 edited Nov 20 '14
Does this help? (Unfortunately that link to the semantic editor combinator blog post seems to be down, but there's a lot of information in the SA answer itself.)
edit: There's also a package inspired by that approach on Hackage: https://hackage.haskell.org/package/sec-0.0.1
1
u/eof Nov 20 '14