Next: Structural unification (pattern matching) in Ada [was: Re: S-expression I/O in Ada]
From: _FrnchFrgg_ on 11 Aug 2010 13:51
Le 11/08/2010 16:10, Dmitry A. Kazakov a �crit :
> On Wed, 11 Aug 2010 01:04:29 +0200, _FrnchFrgg_ wrote:
>> Le 10/08/2010 13:19, Dmitry A. Kazakov a �crit :
>>> On Tue, 10 Aug 2010 13:06:58 +0200, _FrnchFrgg_ wrote:
>>>> Unification and pattern matching is independent of type inference.
>>> Did you mean the standard meaning of pattern matching instead of Standard
>>> ML's Volap�k?
>> I meant pattern matching as a ML construct, which is in fact structural
>> unification. It can be done even without type inference, and only needs
>> some kind of polymorphism; essentially you have an object and an
>> expression made of (possibly nested) constructors, with leaves being
>> either constants or variables, and the unification engine answers
>> a) if the object could be obtained by the sequence of constructors, or not
>> b) if yes, the content the variables would have had so that the sequence
>> of constructors would produce the object.
>> For convenience, you often have a list of expressions, and the engine
>> executes the code of the first which fits the object.
> This translated into Ada terms, looks like an automated generation of
> literals/aggregates. There is one step required for Ada, i.e.
> interpretation of a text as code, since Ada is a compiled language.
Objective Caml is also a compiled language, I don't really follow your
> Main objection to all this is that it is hard-coded and involves the
> object's structure (not nominal). Ada has similar mechanism, also
> hard-coded and also structural. One generates S'Input and S'Output
> attributes for stream I/O of built-in container types (arrays and records).
> Another generates record and array aggregates.
> I don't like this either. I would like to see a more general mechanism that
> would allow user-defined recursive non-generic implementations. Because
> beyond literals and stream I/O there is an infinite number of cases where
> this pattern apply. And secondly it should work for user-defined opaque
> container types.
I don't understand what Streams have to do with ML pattern matching.
Just to be sure we are talking about the same thing, I read one of you
wishing Ada had a more powerful/generic "switch" construct, and I
noticed that the description of such a "switch" looked like a subset of
structural unification as you can find in most functionnal languages.
Perhaps the current Ada is powerful enough to write some function/code
doing this switch machinery, but I was thinking it would be a new
language construct (or an extension of "switch").
From: _FrnchFrgg_ on 12 Aug 2010 08:43
Le 11/08/2010 21:43, Robert A Duff a �crit :
> Yes, generalizing Ada case statements pushes them roughly in the
> direction of OCaml pattern matching, which is much more powerful.
> But one of my favorite features of Ada is that case statements
> are checked at compile time to make sure they cover all
> possible cases ("full coverage") and don't overlap.
> If I remember OCaml correctly, it doesn't do either.
OCaml checks for full coverage, and warns about it (I even think you can
make it an error), so nothing would prevent such a feature in Ada to
make lack of full coverage an error. Overlapping I don't really know,
but if the check is feasible, then of course I'd expect it to be in Ada.
> My problem with OCaml is that to understand a pattern,
> you have to understand all the preceding ones simultaneously,
> and negate them in your head. I don't think it's easy to
> get the best of both worlds (powerful pattern matching
> with full coverage and overlap rules).
Why not ? The only way I use overlapping is when I want a special case
of a bigger rule treated separately (like in: do something special when
the root of the AST is an addition, and one of the leaves is an integer,
then use the generic "operator handling" for every other mathematical
operator). I never ever wrote cases where a rule overlaped an other
without one being a subset of the other.
And subset-overlapping can be rewritten to not overlap by matching for
the full case and using a nested "case" statement. So overlaping is not
> Note that "when others" in Ada turns off the full coverage rule.
> Similar to "_" in OCaml -- it means "none of the above".
Just as a side note, "_" in OCaml is just like any other variable (you
can even write "let _ = 5 in" or "let (_,x) =
function_returning_a_couple arg1 arg2 in"; its value is just never
stored (and it is illegal to use it as a r-value)
>> Perhaps the current Ada is powerful enough to write some function/code
>> doing this switch machinery, ...
> Well, one could write an OCaml compiler or interpreter in Ada. ;-)
I meant with a decent integration with the normal Ada :-)
>> ...but I was thinking it would be a new
>> language construct (or an extension of "switch").
> "case", please. This isn't comp.lang.c. ;-)
My bad. Sorry for that.