* [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading
@ 2014-10-13 13:49 Jun Furuse
2014-10-13 13:56 ` Peter Zotov
0 siblings, 1 reply; 5+ messages in thread
From: Jun Furuse @ 2014-10-13 13:49 UTC (permalink / raw)
To: caml-list
Hi,
I have OPAM-released ppx_overload, a ppx for user definable SML style
overloading.
For example you can overload (+) and (+.) to Plus.(+) as follows:
module Plus = struct
external (+) : 'a -> 'a -> 'a = "%OVERLOADED"
module Int = struct
let (+) = Pervasives.(+)
end
module Float = struct
let (+) = Pervasives.(+.)
end
end
Then Plus.(+) works for int and float additions!
It works as follows:
* Declaration of overloaded values by external "%OVERLOADED"
* List the instances with the same name in its sub modules.
* The uses of overloaded values are resolved to one of their instances
at typing phase.
The overloading of ppx_overload is pretty limited: you cannot derive
overloading by using overloaded values polymorphicially. You cannot
define overloaded double with the overloaded Plus.(+). But still it is
a very interesting ppx example to combine with the OCaml typing.
ppx_overload is now available in OPAM. More detailed explanation how
it works is explained in the project page:
https://bitbucket.org/camlspotter/compiler-libs-hack . Enjoy!
Thanks
Jun
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading
2014-10-13 13:49 [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading Jun Furuse
@ 2014-10-13 13:56 ` Peter Zotov
2014-10-13 14:54 ` Alain Frisch
0 siblings, 1 reply; 5+ messages in thread
From: Peter Zotov @ 2014-10-13 13:56 UTC (permalink / raw)
To: Jun Furuse; +Cc: caml-list, caml-list-request
On 2014-10-13 17:49, Jun Furuse wrote:
> Hi,
>
> I have OPAM-released ppx_overload, a ppx for user definable SML style
> overloading.
Hi,
Great hack! I wanted to do something similar, but yours is much more
elegant than what I had in mind.
One note though:
> Unfortunately this ppx trick does not work for the toplevel. It is
> since OCaml toplevel
> re-execute the ppx filter each time it gets a toplevel phrase. This
> makes the ppx filter
> hard to keep its state, in this case, the typing environment.
In 4.02.1 the toplevel allows the ppx rewriter to save some state.
See
http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/trunk/parsing/ast_mapper.mli?view=markup#l192.
--
Peter Zotov
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading
2014-10-13 13:56 ` Peter Zotov
@ 2014-10-13 14:54 ` Alain Frisch
2014-10-14 13:08 ` Jun Furuse
0 siblings, 1 reply; 5+ messages in thread
From: Alain Frisch @ 2014-10-13 14:54 UTC (permalink / raw)
To: Peter Zotov, Jun Furuse; +Cc: caml-list, caml-list-request
On 10/13/2014 03:56 PM, Peter Zotov wrote:
> On 2014-10-13 17:49, Jun Furuse wrote:
>> Hi,
>>
>> I have OPAM-released ppx_overload, a ppx for user definable SML style
>> overloading.
>
> Hi,
>
> Great hack! I wanted to do something similar, but yours is much more
> elegant than what I had in mind.
>
> One note though:
>
>> Unfortunately this ppx trick does not work for the toplevel. It is
>> since OCaml toplevel
>> re-execute the ppx filter each time it gets a toplevel phrase. This
>> makes the ppx filter
>> hard to keep its state, in this case, the typing environment.
>
> In 4.02.1 the toplevel allows the ppx rewriter to save some state.
> See
> http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/trunk/parsing/ast_mapper.mli?view=markup#l192.
And sedlex ( https://github.com/alainfrisch/sedlex ) illustrates one
possible approach for storing the state. Instead of marshaling in any
form some kind of internal state, it simply stores fragments of
parsetree (here, structure items) that impacted its internal state and
simply replay them on later phrases. (This is not very efficient, but
for quick experiments in the toplevel, this is fine.) I don't know if
this technique would apply to ppx_overload.
-- Alain
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading
2014-10-13 14:54 ` Alain Frisch
@ 2014-10-14 13:08 ` Jun Furuse
2014-10-14 14:55 ` Peter Zotov
0 siblings, 1 reply; 5+ messages in thread
From: Jun Furuse @ 2014-10-14 13:08 UTC (permalink / raw)
To: Alain Frisch; +Cc: Peter Zotov, caml-list, caml-list-request
I do not think the ppx cookies can help ppx_overload in toplevel... It
requires typing environments which can be big. Maybe possible but
sounds odd.
In toplevel, it should be natural that ppx filters would keep living
along with the main toplevel process, rather than respawning it for
each toplevel phrase. But I understand the current design does not
permit such filter behavior.
Jun
On Mon, Oct 13, 2014 at 10:54 PM, Alain Frisch <alain@frisch.fr> wrote:
> On 10/13/2014 03:56 PM, Peter Zotov wrote:
>>
>> On 2014-10-13 17:49, Jun Furuse wrote:
>>>
>>> Hi,
>>>
>>> I have OPAM-released ppx_overload, a ppx for user definable SML style
>>> overloading.
>>
>>
>> Hi,
>>
>> Great hack! I wanted to do something similar, but yours is much more
>> elegant than what I had in mind.
>>
>> One note though:
>>
>>> Unfortunately this ppx trick does not work for the toplevel. It is
>>> since OCaml toplevel
>>> re-execute the ppx filter each time it gets a toplevel phrase. This
>>> makes the ppx filter
>>> hard to keep its state, in this case, the typing environment.
>>
>>
>> In 4.02.1 the toplevel allows the ppx rewriter to save some state.
>> See
>>
>> http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/trunk/parsing/ast_mapper.mli?view=markup#l192.
>
>
>
> And sedlex ( https://github.com/alainfrisch/sedlex ) illustrates one
> possible approach for storing the state. Instead of marshaling in any form
> some kind of internal state, it simply stores fragments of parsetree (here,
> structure items) that impacted its internal state and simply replay them on
> later phrases. (This is not very efficient, but for quick experiments in
> the toplevel, this is fine.) I don't know if this technique would apply to
> ppx_overload.
>
> -- Alain
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading
2014-10-14 13:08 ` Jun Furuse
@ 2014-10-14 14:55 ` Peter Zotov
0 siblings, 0 replies; 5+ messages in thread
From: Peter Zotov @ 2014-10-14 14:55 UTC (permalink / raw)
To: Jun Furuse; +Cc: Alain Frisch, caml-list, caml-list-request
On 2014-10-14 17:08, Jun Furuse wrote:
> I do not think the ppx cookies can help ppx_overload in toplevel... It
> requires typing environments which can be big. Maybe possible but
> sounds odd.
>
> In toplevel, it should be natural that ppx filters would keep living
> along with the main toplevel process, rather than respawning it for
> each toplevel phrase. But I understand the current design does not
> permit such filter behavior.
If this is the case, then Alain's solution would help. Basically, you
could store only the modules with overloaded values in the cookies,
as parsetree fragments, which would not be very big.
>
> Jun
>
> On Mon, Oct 13, 2014 at 10:54 PM, Alain Frisch <alain@frisch.fr> wrote:
>> On 10/13/2014 03:56 PM, Peter Zotov wrote:
>>>
>>> On 2014-10-13 17:49, Jun Furuse wrote:
>>>>
>>>> Hi,
>>>>
>>>> I have OPAM-released ppx_overload, a ppx for user definable SML
>>>> style
>>>> overloading.
>>>
>>>
>>> Hi,
>>>
>>> Great hack! I wanted to do something similar, but yours is much more
>>> elegant than what I had in mind.
>>>
>>> One note though:
>>>
>>>> Unfortunately this ppx trick does not work for the toplevel. It is
>>>> since OCaml toplevel
>>>> re-execute the ppx filter each time it gets a toplevel phrase. This
>>>> makes the ppx filter
>>>> hard to keep its state, in this case, the typing environment.
>>>
>>>
>>> In 4.02.1 the toplevel allows the ppx rewriter to save some state.
>>> See
>>>
>>> http://caml.inria.fr/cgi-bin/viewvc.cgi/ocaml/trunk/parsing/ast_mapper.mli?view=markup#l192.
>>
>>
>>
>> And sedlex ( https://github.com/alainfrisch/sedlex ) illustrates one
>> possible approach for storing the state. Instead of marshaling in any
>> form
>> some kind of internal state, it simply stores fragments of parsetree
>> (here,
>> structure items) that impacted its internal state and simply replay
>> them on
>> later phrases. (This is not very efficient, but for quick experiments
>> in
>> the toplevel, this is fine.) I don't know if this technique would
>> apply to
>> ppx_overload.
>>
>> -- Alain
>>
>>
>>
--
Peter Zotov
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-10-14 14:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-13 13:49 [Caml-list] [ANN] ppx_overload : ppx for user definable SML style overloading Jun Furuse
2014-10-13 13:56 ` Peter Zotov
2014-10-13 14:54 ` Alain Frisch
2014-10-14 13:08 ` Jun Furuse
2014-10-14 14:55 ` Peter Zotov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox