Re: Design Patterns and Functional programming
- From: "Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx>
- Date: Tue, 10 Jul 2007 11:45:04 +0200
On Mon, 09 Jul 2007 21:53:26 +0100, Jon Harrop wrote:
Dmitry A. Kazakov wrote:
The above is not the Ada's grammar, which is far more complex.
Did your calculator example implement Ada's grammar?
No, that is not Ada. The sample of Ada 95 expression is at the end of the
chapter 8:
http://www.dmitry-kazakov.de/ada/components.htm#8.9
The problem is in additional constraints, like a**-b (illegal).
Depending upon what exactly is illegal (is a** -b illegal?),
Brackets are required:
2.0**-1 -- This is illegal
2.0**(-1) -- This is OK
I would raise an exception in the lexer or parser.
How, non-functionally... (:-))
Further, diagnostics will be a mess...
Why do you believe that?
Because diagnostics is a side-effect, while the result is a compiled
program. It does not fit into a purely functional model. You have to bend
your design or else face "states."
If we're talking about parsers then the handling of precedence levels
should be clear from the above example.
Yep, you can trash them into the grammar, and in fact, people are doing
this all the time. But the result will most likely be an unreadable mess,
You need to make a comparison before you can draw a justifiable conclusion.
Look at the grammar of Ada 95, I gave you the link. It does that thing and
also handles many constraints at the grammar level. If you consider it
readable, then well, I take my hat off to you.
I would still be interested to see an object oriented equivalent to the
term rewriter. :-)
Rewriter is defined as a function,
A pattern match.
No, a pattern match gives yes/no, rewriter translates from one
representation to another.
its OO equivalent would be again a function.
There is no OO equivalent to the pattern match.
I am not sure what you mean. In the solution space, an OO equivalent is:
Source'Class -- The class of sources
Pattern'Class -- The class of patterns
function Match (X : in out Source; Y : Pattern) return Boolean;
If you mean pattern matching as a language feature, then you should
elaborate why do you believe it essential.
(I have some personal interest in pattern matching. There was a time I
worked much on it. If you are interested, see:
http://www.dmitry-kazakov.de/match/match.htm
At some point I came to conclusion that pattern matching is not an answer.
IMO it brings more problems than solves.)
You could reach for a design
pattern but it will not provide the same static checking or performance
without an enormous amount of effort (essentially writing a pattern
matcher).
Hmm, I don't see an obvious connection between "pattern matching" and
"design patterns." You could also add "pattern recognition" as known in AI
to the list. These three are way different things, as I understand them.
Don't expect me reinventing wheels. (:-))
I would like the see the an OO equivalent.
Functional decomposition is an integral part of OOP. [Many here would
probably disagree.]
But, what about sets of rewriters?
1. How would you describe a class of, with certain properties?
A higher-order function parameterized over the properties.
That means you need the properties being evaluated to certain values. This
looks very constraining. Consider a property of being non-zero everywhere
except at a finite number of points.
How a user of the class could design a program in terms of these
properties?
Apply the appropriate arguments.
That's too late. The whole idea is to know properties in advance during
design.
2. How would you extend / modify a rewriter?
Using a continuation.
That is same as composition. BTW, this is a great problem of many OO
languages too. They also use extension = composition with some epilogue and
prologues, or overriding. That is too limiting in many cases. A typical
example is dispatching constructors / destructors.
OO does not limit you here in any way.
With enough work you can solve the same problems with an OO design but
requiring 10x as much code to do the same thing is severely limiting in
practice.
I doubt it is a relation. It is rather a+b/x+b/x*x+... I would expect that
in a large project you will eventually have much *less* code in an OOP
variant than in a pure FP. If you give something away from FP and
OO-preachers do the same, then you will probably meet somewhere at the
optimum. Stateless design is a great advantage *when* appropriate.
I don't either. But I see reasons when an object should better have a
state.
I agree. But why objects?
Because a value cannot have state. It is a state of something else.
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.
- Follow-Ups:
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- References:
- Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- From: H. S. Lahman
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- From: Daniel T.
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- From: Dmitry A. Kazakov
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- From: Dmitry A. Kazakov
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Re: Design Patterns and Functional programming
- From: Dmitry A. Kazakov
- Re: Design Patterns and Functional programming
- From: Jon Harrop
- Design Patterns and Functional programming
- Prev by Date: Re: Design Patterns and Functional programming
- Next by Date: Re: Design Patterns and Functional programming
- Previous by thread: Re: Design Patterns and Functional programming
- Next by thread: Re: Design Patterns and Functional programming
- Index(es):
Relevant Pages
|