I would like to create three Haskell functions: a, b, and c.
Each function is to have one argument. The argument is one of the three functions.
I would like function a to have this behavior:
- if the argument is function
athen return functiona. - if the argument is function b then return function
b. - if the argument is function c then return function
a.
Here’s a recap of the behavior I desire for function a:
a a = a
a b = c
a c = a
And here’s the behavior I desire for the other two functions:
b a = a
b b = a
b c = c
c a = c
c b = b
c c = c
Once created, I would like to be able to compose the functions in various ways, for example:
a (c b)
= a (b)
= c
How do I create these functions?
Since you have given no criteria for how you are going to observe the results, then
a = b = c = idsatisfies your criteria. But of course that is not what you want. But the idea is important: it doesn’t just matter what behavior you want your functions to have, but how you are going to observe that behavior.There is a most general model if you allow some freedom in the notation, and you get this by using an algebraic data type:
and so on. Instead of saying
a b, you have to sayA % B, but that is the only difference. You can compose them:and you can turn them into functions by partially applying
(%):But you cannot compare this
a, as ehird says. This model is equivalent to the one you specified, it just looks a little different.