views:

229

answers:

3

Recently Erik Meijer and others have show how IObservable/IObserver is the dual of IEnumerable/IEnumerator. The fact that they are dual means that any operation on one interface is valid on the other, thus providing a theoretical foundation for the Reactive Extentions for .Net

Do other dual interfaces exist? I'm interested in any example, not just .Net based.

+2  A: 

Another example would be TextReader and TextWriter, though there is even more noise than in case of observables and enumerables. In principle the type signatures would be:

interface ITextReader {
  // Read: void -> int
  int Read();
}

interface ITextWriter {
  // Write: int -> void
  void Write(int val);
}
Tomas Petricek
+3  A: 

Another example is the product type A.B and the sum type A+B of two types A and B. In Haskell you can write them as:

data Prod a b = P a b -- this is the same as the pair type (a,b)

data Sum a b = Left a | Right b -- the same as the Either a b type

check here for details

Daniel Velkov
+2  A: 

Covariance and contravariance is another example. I think. I could be wrong.

Bart De Smet says: "Lots of dualities exist in various disciplines, providing for great knowledge transfers between different domains. For example, in formal logic, De Morgan’s law allows converting expressions built from conjunctions into ones built from disjunctions, and vice versa. In electronics, similarities exist between the behavior of capacitors and inductances: know one and how to go back and forth between domains, and you know the other. Fourier calculus provides duals between time and frequency domains." Interesting.

They also call System.Reactive the dual of System.Interactive. So most of the functions in one of the assemblies has it's dual in the other. To clarify, it's not just that IO is the dual of IE, but the functions that operate on them are also dualized.

So to answer your question, many dualized interfaces exist. You can dualize any interface. You just swap inputs and outputs and the direction of the function. Some won't be useful, or will be the dual of themselves. However, sometimes there are really powerful ones hiding waiting to be uncovered.

Richard Hein
Up-vote for quoting the great Bart De Smet's blog. :-D
Tuomas Hietanen