caml-list - the Caml user's mailing list
 help / Atom feed
* [Caml-list] Recursive Types and -rectypes
@ 2018-07-13 11:10 Angelo Corsaro
  2018-07-13 12:27 ` Gabriel Scherer
  0 siblings, 1 reply; 3+ messages in thread
From: Angelo Corsaro @ 2018-07-13 11:10 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 2070 bytes --]

Dear All,

	I am looking for some advices and suggestions on the use of recursive types that  OCaml makes available through the -rectypes option. 

To give you some context we have built a simple Agent framework for Ocaml that provides primitives similar to those of Erlang — with the difference that at the present time we care only on using actors to control concurrency on the same process. As you can probably imagine the recursive type definition comes out from the actor message queue. The most straight forward definition would look like (removing some details to keep the essence):

type ‘msg actor_queue = ( ‘msg actor_queue option * ‘msg) some_queue_type 

This is a simplified version, but let’s say that the intuition is that we need to post on the receiving actor queue the message along to possibly the queue of the agent we may reply to. This gives rise to a recursive type definition which clearly would only be usable with the -rectypes option.

The traditional way of breaking things kind of recursive types is to use variant types recursion. Which is what I’ve done. Yet that comes at some cost w.r.t. the readability of the code. Thus the question, would you suggest using -rectypes to enable recursive data types? Use classes to represent this type, as they allow for recursive type definition? Or just stick with the variant type trick?

Thanks very much in advance for sharing your thoughts!

Ciao,
    Angelo

P.S. ‘msg is actually bound with some polymorphic variant to allow the actor to accept any user defined type as in Erlang. We also have a version that leverages functors to define the messages that may be exchanged between actors — if a strict control is required on the set of messages exchanged. 

—
“Simplicity is the ultimate sophistication." ~ Leonardo da Vinci


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 3310 bytes --]

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

* Re: [Caml-list] Recursive Types and -rectypes
  2018-07-13 11:10 [Caml-list] Recursive Types and -rectypes Angelo Corsaro
@ 2018-07-13 12:27 ` Gabriel Scherer
  2018-07-14 14:35   ` Angelo Corsaro
  0 siblings, 1 reply; 3+ messages in thread
From: Gabriel Scherer @ 2018-07-13 12:27 UTC (permalink / raw)
  To: Angelo Corsaro; +Cc: caml users

-rectypes is not the default because it leads to the type-checker
accepting code that is a programmer mistake but makes sense using
recursive types. This leads to hard-to-understand errors down the line
when you try to use the name at the type you think it has.

I would just go for the variant. I am not sure what kind of
readability overhead you have in mind, do you have a concrete example?
In my experience, the ability to use pattern-matching in function
arguments and let declarations makes this very smooth


  type ‘msg actor_queue = Act of ( ‘msg actor_queue option * ‘msg)
some_queue_type

  let queue (Act q) = q

On Fri, Jul 13, 2018 at 12:10 PM Angelo Corsaro <angelo@icorsaro.net> wrote:
>
> Dear All,
>
> I am looking for some advices and suggestions on the use of recursive types that  OCaml makes available through the -rectypes option.
>
> To give you some context we have built a simple Agent framework for Ocaml that provides primitives similar to those of Erlang — with the difference that at the present time we care only on using actors to control concurrency on the same process. As you can probably imagine the recursive type definition comes out from the actor message queue. The most straight forward definition would look like (removing some details to keep the essence):
>
> type ‘msg actor_queue = ( ‘msg actor_queue option * ‘msg) some_queue_type
>
> This is a simplified version, but let’s say that the intuition is that we need to post on the receiving actor queue the message along to possibly the queue of the agent we may reply to. This gives rise to a recursive type definition which clearly would only be usable with the -rectypes option.
>
> The traditional way of breaking things kind of recursive types is to use variant types recursion. Which is what I’ve done. Yet that comes at some cost w.r.t. the readability of the code. Thus the question, would you suggest using -rectypes to enable recursive data types? Use classes to represent this type, as they allow for recursive type definition? Or just stick with the variant type trick?
>
> Thanks very much in advance for sharing your thoughts!
>
> Ciao,
>     Angelo
>
> P.S. ‘msg is actually bound with some polymorphic variant to allow the actor to accept any user defined type as in Erlang. We also have a version that leverages functors to define the messages that may be exchanged between actors — if a strict control is required on the set of messages exchanged.
>
> —
> “Simplicity is the ultimate sophistication." ~ Leonardo da Vinci
>

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

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

* Re: [Caml-list] Recursive Types and -rectypes
  2018-07-13 12:27 ` Gabriel Scherer
@ 2018-07-14 14:35   ` Angelo Corsaro
  0 siblings, 0 replies; 3+ messages in thread
From: Angelo Corsaro @ 2018-07-14 14:35 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 3382 bytes --]

Hello Gabriel,


> On 13 Jul 2018, at 14:34, Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
> 
> -rectypes is not the default because it leads to the type-checker
> accepting code that is a programmer mistake but makes sense using
> recursive types. This leads to hard-to-understand errors down the line
> when you try to use the name at the type you think it has.
> 
> I would just go for the variant. I am not sure what kind of
> readability overhead you have in mind, do you have a concrete example?
> In my experience, the ability to use pattern-matching in function
> arguments and let declarations makes this very smooth
> 
> 
>  type ‘msg actor_queue = Act of ( ‘msg actor_queue option * ‘msg)
> some_queue_type
> 
>  let queue (Act q) = q

Yes, this is similar to what I did. I shared your concerns on the use of -rectypes this is why I was looking for advises. We are a bit new to OCaml and from time to time it is good to get some advise from the community. Thanks again for the valuable feedback!

Enjoy the weekend.

Ciao,
	Angelo
 

> 
> On Fri, Jul 13, 2018 at 12:10 PM Angelo Corsaro <angelo@icorsaro.net> wrote:
>> 
>> Dear All,
>> 
>> I am looking for some advices and suggestions on the use of recursive types that  OCaml makes available through the -rectypes option.
>> 
>> To give you some context we have built a simple Agent framework for Ocaml that provides primitives similar to those of Erlang — with the difference that at the present time we care only on using actors to control concurrency on the same process. As you can probably imagine the recursive type definition comes out from the actor message queue. The most straight forward definition would look like (removing some details to keep the essence):
>> 
>> type ‘msg actor_queue = ( ‘msg actor_queue option * ‘msg) some_queue_type
>> 
>> This is a simplified version, but let’s say that the intuition is that we need to post on the receiving actor queue the message along to possibly the queue of the agent we may reply to. This gives rise to a recursive type definition which clearly would only be usable with the -rectypes option.
>> 
>> The traditional way of breaking things kind of recursive types is to use variant types recursion. Which is what I’ve done. Yet that comes at some cost w.r.t. the readability of the code. Thus the question, would you suggest using -rectypes to enable recursive data types? Use classes to represent this type, as they allow for recursive type definition? Or just stick with the variant type trick?
>> 
>> Thanks very much in advance for sharing your thoughts!
>> 
>> Ciao,
>>    Angelo
>> 
>> P.S. ‘msg is actually bound with some polymorphic variant to allow the actor to accept any user defined type as in Erlang. We also have a version that leverages functors to define the messages that may be exchanged between actors — if a strict control is required on the set of messages exchanged.
>> 
>> —
>> “Simplicity is the ultimate sophistication." ~ Leonardo da Vinci
>> 

—
“Simplicity is the ultimate sophistication." ~ Leonardo da Vinci


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 5048 bytes --]

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

end of thread, back to index

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-07-13 11:10 [Caml-list] Recursive Types and -rectypes Angelo Corsaro
2018-07-13 12:27 ` Gabriel Scherer
2018-07-14 14:35   ` Angelo Corsaro

caml-list - the Caml user's mailing list

Archives are clonable: git clone --mirror https://inbox.ocaml.org/caml-list

AGPL code for this site: git clone https://public-inbox.org/ public-inbox