caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to add a hook to Stream.junk?
@ 2012-11-21  1:07 bob zhang
  2012-11-21  1:35 ` Wojciech Meyer
  0 siblings, 1 reply; 8+ messages in thread
From: bob zhang @ 2012-11-21  1:07 UTC (permalink / raw)
  To: Caml List

Hi List,
   Does anyone know any trick to add a hook to Stream.junk,  I have
tried different ways,  but did not find any solution, yet
I want to trigger an action each time when I junk a token from the
stream. (copy the whole file from stdlib is fine, but I don't
know how to make the type checker happy :-()
-- 
Regards
-- Bob

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] How to add a hook to Stream.junk?
  2012-11-21  1:07 [Caml-list] How to add a hook to Stream.junk? bob zhang
@ 2012-11-21  1:35 ` Wojciech Meyer
  2012-11-21  2:58   ` [Caml-list] " bobzhang
  2012-11-21  3:05   ` [Caml-list] " Francois Berenger
  0 siblings, 2 replies; 8+ messages in thread
From: Wojciech Meyer @ 2012-11-21  1:35 UTC (permalink / raw)
  To: bob zhang; +Cc: Caml List

bob zhang <bobzhang1988@gmail.com> writes:

> Hi List,
>    Does anyone know any trick to add a hook to Stream.junk,  I have
> tried different ways,  but did not find any solution, yet
> I want to trigger an action each time when I junk a token from the
> stream. (copy the whole file from stdlib is fine, but I don't
> know how to make the type checker happy :-()
> --
> Regards
> -- Bob

You can parametrise the Stream.t type with your desired type and the
hook.

Then you can substitute type using destructive substitution of a type:

module Stream : module type of Stream with type 'elt t := ('hook, 'elt)
CustomStream.t = Stream

Finally you can implement your own CustomStream.junk function that
operates on the parametrised type and include the CustomStream module.

This should work, but I've not tested or compiled it.

--
Wojciech Meyer
http://danmey.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] Re: How to add a hook to Stream.junk?
  2012-11-21  1:35 ` Wojciech Meyer
@ 2012-11-21  2:58   ` bobzhang
  2012-11-21  3:37     ` Wojciech Meyer
  2012-11-21  3:05   ` [Caml-list] " Francois Berenger
  1 sibling, 1 reply; 8+ messages in thread
From: bobzhang @ 2012-11-21  2:58 UTC (permalink / raw)
  To: Wojciech Meyer; +Cc: Caml List

On 11/20/12 8:35 PM, Wojciech Meyer wrote:
> bob zhang <bobzhang1988@gmail.com> writes:
>

>> Hi List,
>>     Does anyone know any trick to add a hook to Stream.junk,  I have
>> tried different ways,  but did not find any solution, yet
>> I want to trigger an action each time when I junk a token from the
>> stream. (copy the whole file from stdlib is fine, but I don't
>> know how to make the type checker happy :-()
>> --
>> Regards
>> -- Bob
>
Greetings,
> You can parametrise the Stream.t type with your desired type and the
> hook.
>
> Then you can substitute type using destructive substitution of a type:
>
> module Stream : module type of Stream with type 'elt t := ('hook, 'elt)
> CustomStream.t = Stream
>
> Finally you can implement your own CustomStream.junk function that
> operates on the parametrised type and include the CustomStream module.
>
> This should work, but I've not tested or compiled it.
>
Stream specialized on char stream :-(

> --
> Wojciech Meyer
> http://danmey.org
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] How to add a hook to Stream.junk?
  2012-11-21  1:35 ` Wojciech Meyer
  2012-11-21  2:58   ` [Caml-list] " bobzhang
@ 2012-11-21  3:05   ` Francois Berenger
  2012-11-21  3:34     ` Wojciech Meyer
  1 sibling, 1 reply; 8+ messages in thread
From: Francois Berenger @ 2012-11-21  3:05 UTC (permalink / raw)
  To: caml-list

On 11/21/2012 10:35 AM, Wojciech Meyer wrote:
> bob zhang <bobzhang1988@gmail.com> writes:
>
>> Hi List,
>>     Does anyone know any trick to add a hook to Stream.junk,  I have
>> tried different ways,  but did not find any solution, yet
>> I want to trigger an action each time when I junk a token from the
>> stream. (copy the whole file from stdlib is fine, but I don't
>> know how to make the type checker happy :-()
>> --
>> Regards
>> -- Bob
>
> You can parametrise the Stream.t type with your desired type and the
> hook.
>
> Then you can substitute type using destructive substitution of a type:
>
> module Stream : module type of Stream with type 'elt t := ('hook, 'elt)
> CustomStream.t = Stream
>
> Finally you can implement your own CustomStream.junk function that
> operates on the parametrised type and include the CustomStream module.

Why the need for module type of and all?

Why not simply:

module MyStream = struct
   include Stream
   let junk =
     failwith "put your code here"
end;;

Which I can read as "MyStream is a Stream where I redefined the junk 
operation".

> This should work, but I've not tested or compiled it.
> --
> Wojciech Meyer
> http://danmey.org
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Caml-list] How to add a hook to Stream.junk?
  2012-11-21  3:05   ` [Caml-list] " Francois Berenger
@ 2012-11-21  3:34     ` Wojciech Meyer
  2012-11-21  3:39       ` [Caml-list] " bobzhang
  0 siblings, 1 reply; 8+ messages in thread
From: Wojciech Meyer @ 2012-11-21  3:34 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

Francois Berenger <berenger@riken.jp> writes:

> Why the need for module type of and all?
>
> Why not simply:
>
> module MyStream = struct
>   include Stream
>   let junk =
>     failwith "put your code here"
> end;;

Yes you could also do through a functor application.

module MyStream(H : sig val junk_hook : unit -> unit end) = 
struct
  include Stream
  let junk stream =
     H.junk_hook ();
     junk stream
end


--
Wojciech Meyer
http://danmey.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] Re: How to add a hook to Stream.junk?
  2012-11-21  2:58   ` [Caml-list] " bobzhang
@ 2012-11-21  3:37     ` Wojciech Meyer
  0 siblings, 0 replies; 8+ messages in thread
From: Wojciech Meyer @ 2012-11-21  3:37 UTC (permalink / raw)
  To: bobzhang; +Cc: Wojciech Meyer, Caml List

bobzhang <bobzhang1988@gmail.com> writes:

> Stream specialized on char stream :-(

Please see then functor application idea, I just posted, inspired by
Francois Berenger's idea of ad-hoc inclusion of the module.

--
Wojciech Meyer
http://danmey.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] Re: How to add a hook to Stream.junk?
  2012-11-21  3:34     ` Wojciech Meyer
@ 2012-11-21  3:39       ` bobzhang
  2012-11-21  3:45         ` Wojciech Meyer
  0 siblings, 1 reply; 8+ messages in thread
From: bobzhang @ 2012-11-21  3:39 UTC (permalink / raw)
  To: Wojciech Meyer; +Cc: Francois Berenger, caml-list

On 11/20/12 10:34 PM, Wojciech Meyer wrote:
> Francois Berenger <berenger@riken.jp> writes:
>
>> Why the need for module type of and all?
>>
>> Why not simply:
>>
>> module MyStream = struct
>>    include Stream
>>    let junk =
>>      failwith "put your code here"
>> end;;
>
> Yes you could also do through a functor application.
>
> module MyStream(H : sig val junk_hook : unit -> unit end) =
> struct
>    include Stream
>    let junk stream =
>       H.junk_hook ();
>       junk stream
> end
neither works.
functor approach, as I pointed out, stream specialized on char.
overshadowing junk does not work
   a. it's not late binding, existing definitions will not be shadowed
   b. type does not check...
>
>
> --
> Wojciech Meyer
> http://danmey.org
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Caml-list] Re: How to add a hook to Stream.junk?
  2012-11-21  3:39       ` [Caml-list] " bobzhang
@ 2012-11-21  3:45         ` Wojciech Meyer
  0 siblings, 0 replies; 8+ messages in thread
From: Wojciech Meyer @ 2012-11-21  3:45 UTC (permalink / raw)
  To: bobzhang; +Cc: Francois Berenger, caml-list

bobzhang <bobzhang1988@gmail.com> writes:

>   a. it's not late binding, existing definitions will not be shadowed

If the existing code uses junk then you have no other choice as to copy
paste the Stream module and customise it.

--
Wojciech Meyer
http://danmey.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-11-21  3:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-21  1:07 [Caml-list] How to add a hook to Stream.junk? bob zhang
2012-11-21  1:35 ` Wojciech Meyer
2012-11-21  2:58   ` [Caml-list] " bobzhang
2012-11-21  3:37     ` Wojciech Meyer
2012-11-21  3:05   ` [Caml-list] " Francois Berenger
2012-11-21  3:34     ` Wojciech Meyer
2012-11-21  3:39       ` [Caml-list] " bobzhang
2012-11-21  3:45         ` Wojciech Meyer

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).