Question

I've been reading about combinators and seen how useful they are (for example, in Haskell's Parsec). My problem is that I'm not quite sure how to use them practically.

Here's an outline of the problem: distributions can be generated, filtered, and modified. Distributions may be combined to create new distributions.

The basic interfaces are (in pseudo-Haskell type terminology):

generator::      parameters -> distribution

selector::       parameters -> (distribution -> distribution)

modifier::       parameters -> (distribution -> distribution)

Now, I think that I see three combinators:

combine::     generator -> generator -> generator

filter::      generator -> selector -> generator

modify::      generator -> modifier -> generator

Are these actually combinators? Do the combinators make sense/are there any other obvious combinators that I'm missing?

Thanks for any advice.

Was it helpful?

Solution

The selector and modifier functions are already perfectly good combinators! Along with generator and combine you can do stuff like (I'm going to assume statistical distributions for concreteness and just make things up!):

modifier (Scale 3.0) $ generator StandardGaussian `combine` selector (LargerThan 10) . modifier (Shift 7) $ generator (Binomial 30 0.2)

You may have to mess around a bit with the priority of the combine operator for this to work smoothly :)

In general, when I'm trying to design a combinator library for values of type A, I like to keep my A's "at the end", so that the partially applied combinators (your selector and modifier) can be chained together with . instead of having to flip through hoops.

Here's a nice blog article which can help you design combinators, it influenced a lot of my thinking: Semantic Editor Combinators.

EDIT: I may have misread your question, given the type signature of combine. Maybe I'm missing something, but wouldn't the distributions be the more natural objects your combinator should work on?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top