**Problem Detail:**

I am trying to understand the basic ideas of pattern calculus and do not have a lot of time to read through a rather long book. Can someone explain this in simple terms and give an example of how pattern calculus works?

http://www.amazon.com/dp/3540891846/?tag=stackoverfl08-20

I don't understand "The ability to pass patterns as parameters (pattern polymorphism) is illustrated by defining a generic eliminator." as explained here in the Wikipedia article. If someone could work through an example showing how the patterns were transformed and why they were transformed in that way, that would be useful to me, thanks.

###### Asked By : Phil

#### Answered By : AJed

I can't really say that I know bondi (this is the first time I heard about it), but my experience with functional languages will allow me to give you hints.

Let's first discuss what the example given in the wikipedia page do. There are two functions called *elimLeafs* and *elimCount*. Both are accessed such as elimLeaf (Leaf 3) returns 3 and elimCount(Count 3) returns 3. "Do not focus on what these functions do !" .. There is a similarity between the two functions in that:

- elimLeaf (Laef x) or elimCount (Count x) can be return as elimPattern (Pattern x) where Pattern is either Count, Leaf or any other thing you want to name.

Knowing this, you can write both functions such as "Pattern" in elimPattern (Pattern x) is a variable .. and whence you can write both elimPattern and elimLeaf such as:

` elim = | x -> | {y} x y -> y `

which can be called as:

` elim Leaf (Leaf 3) returns 3 .... for example. `

The function elim above is actually two functions (or I dont know what is called in Bondi). First, you got the name of the function, which is elim. The name shall be followed by =| . Then you have the definition of the function"s" which is followed by ->. So,

- "elim Leaf" is matched with "elim =| x" and therefore evaluated to | {y} x y
- Now, x is Leaf in "elim Leaf". Thus, "| {y} x y" is equivalent to "| {y} Leaf y".
- The function elim above can be further evaluated such that "| {y} Leaf y -> y" (that is "| {y} Leaf y" is evaluated to y.) y in this case would be 3, given the input above.
- Note here that right now you did exactly what elimLeaf did ! You can do the same for elimCount by simply replacing Leaf by Count in the input.

To clarify more: Assume you want to do this with C++. The definition of the functions elimCount and elimLeaf are as follows:

`int elimCount(int x) { return x;} int elimLeaf(int x) { return x;} // we are just assuming int in this example, `

To write both these functions with only one, perhaps you would do something like:

`int elim (const char* pattern, int x) { if (strcmp(pattern, "Leaf") == 0) return x if (strcmp(pattern, "Count") == 0), return x } `

###### Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/9081

**3.2K people like this**

## 0 comments:

## Post a Comment

Let us know your responses and feedback