* RE: stream pattern [< pat=exp >] and extensions to ordinary patte rn m atching
@ 1999-05-14 16:14 Manuel Fahndrich
0 siblings, 0 replies; 2+ messages in thread
From: Manuel Fahndrich @ 1999-05-14 16:14 UTC (permalink / raw)
To: 'caml-list@inria.fr'
I have to reply to my own earlier (faulty) comments...
[...]
I believe that with such an extension to
OCAML, the pattern language we explored in the ICFP paper could be
expressed
completely (and more).
[...]
This is not entirely true. The pattern language explored in the ICFP paper
allows one to write non-deterministic patterns that get influenced by the
context. For example:
pat leftOrRight(x) =
(x, _)
| (_, x)
If I then write
match (1,2) with
leftOrRight(2) ->
| leftOrRight(y) ->
the first clause would apply. Using the pat=exp approach from stream
parsers, such choices in function of pat would not be possible. I consider
this feature not essential, and having pat=exp patterns whose semantics
evaluate exp first without regard to pat would still be very useful.
-Manuel Fahndrich
^ permalink raw reply [flat|nested] 2+ messages in thread
* RE: stream pattern [< pat=exp >] and extensions to ordinary patte rn m atching
@ 1999-05-10 16:12 Don Syme
0 siblings, 0 replies; 2+ messages in thread
From: Don Syme @ 1999-05-10 16:12 UTC (permalink / raw)
To: Manuel Fahndrich, 'caml-list@inria.fr'
An abstract pattern matching language would also be quite useful when
implementing theorem provers and other applications that involve a lot of
term manipulations. You would like to keep your terms abstract for obvious
software engineering reasons.
Cheers,
Don
-----Original Message-----
From: Manuel Fahndrich [mailto:maf@microsoft.com]
Sent: 07 May 1999 23:28
To: 'caml-list@inria.fr'
Subject: stream pattern [< pat=exp >] and extensions to ordinary pattern
m atching
Hi,
I just discovered the stream parser pattern [< pat = exp >] and I was
wondering why this kind of pattern is not allowed in regular patterns? It is
a very useful construct. For example:
Suppose I have some type ast for abstract syntax
type ast =
Constant of constant
| Binary of oper * ast * ast
| ...
where the type "constant" is not a datatype. There is however a function
refine : constant -> con
where
type con = String of string
| Int of int
| ...
In order to use pattern matching to detect a string constant, I have to use
the following two auxiliary functions (which can be defined with what is
given):
isString : constant -> bool
getString : constant -> string
Then I can write:
match a with
Constant c when isString c -> ... getString c ...
However, this is really not what I want, since I essentially test twice if c
is a string (i.e. getString needs an error case), and furthermore, my
operation 'refine' from above may be expensive.
Now, in a paper ICFP'97 "Statically Checkable Pattern Abstraction", me and a
colleague have explored more expressive patterns in the context of ML. It
seems to me that the stream patterns in OCAML would allow me to do what we
described in this paper, except for the fact that they can only appear in
stream parsers.
Suppose I could write pat = exp in the regular pattern language, with the
semantics that the value being matched is first passed to the function exp,
and the result is matched against pat:
Then I can write the above code as follows: I write a different auxilary
function to test whether a constant is a string that returns the string in
case it is one.
let isString2 c =
match refine c with
String s -> Some s
| _ -> None
Then my match becomes:
match a with
Constant (Some s = isString2) -> .... s ....
| ...
which is exactly what I want. I believe that with such an extension to
OCAML, the pattern language we explored in the ICFP paper could be expressed
completely (and more). In our paper, we used syntax that swaps the
arguments and returns to pattern abstractions, so we could write the above
as
match a with
Constant (isString2(s)) -> ... s ...
Note that in this case the success/failure of the pattern is implicit,
whereas in the above case I used an option to encode it. Any pattern
abstraction could be encoded in this form, where the pattern bindings become
the argument to Some.
... Just a random thought, but it would be neat.
-Manuel Fahndrich
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1999-05-14 16:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-05-14 16:14 stream pattern [< pat=exp >] and extensions to ordinary patte rn m atching Manuel Fahndrich
-- strict thread matches above, loose matches on Subject: below --
1999-05-10 16:12 Don Syme
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).