9

The pipe & filter architectural pattern is defined as a chain of processing elements, arranged so that the output of each element is the input of the next. Every example seems to consider inter-process or inter-thread connection performed through some kind of shared buffer.

To me, it seems that Haskell function composition is performing the same task. Can we say that it is an instance of this pattern even if it is just about function ordering and no explicit buffer is use as pipe? If yes, can we say the same thing for non-lazy language?

2 Answers 2

8

They are connected, but the connection is the other way around (sort of).

Morphism composition in an appropriate category accurately models both function composition (where the category is Set for a strict language and CPO for a lazy language) and process composition (where the category is the (AFAIK, unnamed) category where the objects are strings , morphisms are shell processes, and composition is the pipe operator). Shell processes can be seen, without loss of either generality or accuracy, as (lazy) functions of type String -> WriterT String IO String, and pure functions of type String -> String can be losslessly converted to and from functions of type String -> Identity String, so both are really just (>>=) in disguise.

2
  • 1
    What is a CPO category? My google-fu is weak :(
    – Andres F.
    Jun 13, 2013 at 21:44
  • 3
    @AndresF. The category of Complete Partial Orders. Jun 14, 2013 at 2:28
2

There is a difference. Pipes and filters deal with data and data connections, whereas Haskell Function Composition deals with first-class functions. First-class functions are composable in ways that pipes and filters are not.

Further Reading
What does composability mean in context of functional programming?

3
  • 1
    Composition doesn't necessarily deal with first-class function so much as composition is itself a first-class function. Take for instance the composition of the function ++, where composition is ., ++ . ++ would take a value and return that value incremented twice, but ++ is not a first-class function, it only takes a value and returns a value. The composition function is the first class function which takes two functions and returns one. Jun 25, 2013 at 17:18
  • 1
    @JimmyHoffa, I think you are confusing the notion of 'first-class function' with 'higher-order function'. When a language has 'first-class functions' it treats functions the same as it treats other first-class data types; they can be passed to and returned from functions, stored in data structures, etc. A higher-order function takes advantage of the first-class nature of functions by taking function parameters and/or returning function values. Aug 14, 2013 at 17:32
  • @LeviPearson yeah, I must have accidentally put my brain in sideways when I got up the morning I wrote that comment. If you're going to keep poking around here writing good content for P.SE, ponder idling in P.SEs general chat Aug 14, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.