On Thu, 15 Apr 2010, Artyom Shalkhakov wrote:
>>Hello,
>>
>>I've been experimenting with combinator parsing today, and there is
>>this function:
>>
>>> fun{a,b,t:t@...} lift (f: a -> b, p: parser_t (a, t)): parser_t (b, t)
>>
>>The meaning is to apply [f] to the result of parse we get from [p].
>>But it's usage is only for functions of one argument. Is it possible
>>to generalise to many arguments? There is a trick used in Haskell
>>libraries where they allow to put functions as results of parsing, and
>>then apply these using a combinator:
>>
>>> fun{a,b,t:t@...} app (p1: parser_t (a -> b, t), p2: parser_t (a, t)):<> parser_t (b, t)
>>
>>but I'm not so sure it is a "good" solution in ATS, since ATS
>>functions are not in a curried form by default and closures are
>>explicit.
Too me, it is not a good solution, either.
By the way, a function of several arguments can be turned into a function
of one argument. For instance,
f: (a1, a2) -> t
can be easily turned into
f' : @(a1, a2) -> t
Then you can use a combinator to turn
(parser (a1, t), parser (a2, t)) into parser ( @(a1, a2), t )
and then use 'app'.
This is not so pretty either, but it at least does not create any
heap-allocated values that need to be reclaimed later.
>>I looked into a combinator parsers library available at the web site
>>(PARCOMB), and solution to the similar problem seem to provide eight
>>version of [lift], from one to eight arguments.
>>
>>Is it possible to use a variadic function here? Or maybe, there are
>>other solutions?
I suppose it is possible to use a variadic function like:
fun{aa,pp:types;t:t@...}
app (RELATE (aa, pp) | f: (aa) -> t, parsers: pp): parser (aa, t) = ...
where RELATE is dataprop that relates aa and pp, but this seems too
involved to be of any practical value.
Given that there isn't much of type inference available in ATS,
handling parsing combinators, to me, is manageable in ATS but is never
pretty. Haskell has set a very high standard for parsing combinators :)
Cheers,
--Hongwei
Computer Science Department
Boston University
111 Cummington Street
Boston, MA 02215
Email: hwxi@...
Url: http://www.cs.bu.edu/~hwxi
Tel: +1 617 358 2511 (office)
Fax: +1 617 353 6457 (department)
|