caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Annotated trees
@ 2010-06-06 15:26 Daniel Bünzli
  2010-06-07  5:09 ` [Caml-list] " Martin Jambon
  2010-06-07  6:45 ` Jacques Garrigue
  0 siblings, 2 replies; 6+ messages in thread
From: Daniel Bünzli @ 2010-06-06 15:26 UTC (permalink / raw)
  To: caml-list List

Hello,

Does anybody have any real world experience in choosing between the
following two representations for a tree augmented with annotations ?

type 'a t = 'a * [ `Leaf of string | `Node of 'a t list ]
type 'a t = [ `Leaf of 'a * string | `Node of 'a * 'a t list ]

Which one is more convenient to process, pattern match on, makes code
more readable etc. ?

Thanks in advance,

Daniel


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

* Re: [Caml-list] Annotated trees
  2010-06-06 15:26 Annotated trees Daniel Bünzli
@ 2010-06-07  5:09 ` Martin Jambon
  2010-06-07  5:15   ` Martin Jambon
  2010-06-07  9:22   ` Daniel Bünzli
  2010-06-07  6:45 ` Jacques Garrigue
  1 sibling, 2 replies; 6+ messages in thread
From: Martin Jambon @ 2010-06-07  5:09 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list List

Daniel Bünzli wrote:
> Hello,
> 
> Does anybody have any real world experience in choosing between the
> following two representations for a tree augmented with annotations ?
> 
> type 'a t = 'a * [ `Leaf of string | `Node of 'a t list ]
> type 'a t = [ `Leaf of 'a * string | `Node of 'a * 'a t list ]
> 
> Which one is more convenient to process, pattern match on, makes code
> more readable etc. ?

I normally use the second form.

I see the following advantages over the tuple form:

1. Pattern-matching is more readable because the most important piece of
information comes first:

   `Leaf (_, s) -> ...
 | `Node (_, l) -> ...

instead of:

   (_, `Leaf s) -> ...
 | (_, `Node l) -> ...


2. If needed, writing an annotation_of_t function instead of just using fst is
simple enough and not invasive.

3. It is possible to not annotate certain kinds of nodes, or to have different
types of annotations depending on the kind of node.

4. The tuple version feels like there are 2 different definitions of a tree
node, i.e. it is easy to get confused about whether an annotated node (the
pair) or an non-annotated node (second member of the pair) is expected in one
given place.

5. I got this habit and therefore I won't change my mind and reject all
rational arguments against it ;-)



Martin

-- 
http://mjambon.com/


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

* Re: [Caml-list] Annotated trees
  2010-06-07  5:09 ` [Caml-list] " Martin Jambon
@ 2010-06-07  5:15   ` Martin Jambon
  2010-06-07  9:22   ` Daniel Bünzli
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Jambon @ 2010-06-07  5:15 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list List

Martin Jambon wrote:
> Daniel Bünzli wrote:
>> Hello,
>>
>> Does anybody have any real world experience in choosing between the
>> following two representations for a tree augmented with annotations ?
>>
>> type 'a t = 'a * [ `Leaf of string | `Node of 'a t list ]
>> type 'a t = [ `Leaf of 'a * string | `Node of 'a * 'a t list ]
>>
>> Which one is more convenient to process, pattern match on, makes code
>> more readable etc. ?
> 
> I normally use the second form.
> 
> I see the following advantages over the tuple form:
> 
> 1. Pattern-matching is more readable because the most important piece of
> information comes first:
> 
>    `Leaf (_, s) -> ...
>  | `Node (_, l) -> ...
> 
> instead of:
> 
>    (_, `Leaf s) -> ...
>  | (_, `Node l) -> ...
> 
> 
> 2. If needed, writing an annotation_of_t function instead of just using fst is
> simple enough and not invasive.
> 
> 3. It is possible to not annotate certain kinds of nodes, or to have different
> types of annotations depending on the kind of node.
> 
> 4. The tuple version feels like there are 2 different definitions of a tree
> node, i.e. it is easy to get confused about whether an annotated node (the
> pair) or an non-annotated node (second member of the pair) is expected in one
> given place.
> 
> 5. I got this habit and therefore I won't change my mind and reject all
> rational arguments against it ;-)

See, I even did not consider the possibility of using:

type 'a t = [ `Leaf of string | `Node of 'a t list ] * 'a


Note that point (4) remains valid and based on real experience.



Martin

-- 
http://mjambon.com/


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

* Re: [Caml-list] Annotated trees
  2010-06-06 15:26 Annotated trees Daniel Bünzli
  2010-06-07  5:09 ` [Caml-list] " Martin Jambon
@ 2010-06-07  6:45 ` Jacques Garrigue
  1 sibling, 0 replies; 6+ messages in thread
From: Jacques Garrigue @ 2010-06-07  6:45 UTC (permalink / raw)
  To: daniel.buenzli; +Cc: caml-list

From: Daniel Bünzli <daniel.buenzli@erratique.ch>

> Does anybody have any real world experience in choosing between the
> following two representations for a tree augmented with annotations ?
> 
> type 'a t = 'a * [ `Leaf of string | `Node of 'a t list ]
> type 'a t = [ `Leaf of 'a * string | `Node of 'a * 'a t list ]
> 
> Which one is more convenient to process, pattern match on, makes code
> more readable etc. ?

First, if you have no specific reason to use them, I would avoid
polymorphic variants in this case. They can make debugging trickier.

Concerning annotations, I usually adopt the technique used in the
ocaml compiler: define a wrapper recort type. I.e., this would be

type 'a t = { desc: 'a t_desc ; anno: 'a }
and 'a t_desc = Leaf of string | Node of 'a t list

The main advantage is that you can read the annotation without
pattern-matching.
But I know that many people are happy with the other aproach, so I'm
not sure that it matters that much.

Jacques Garrigue


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

* Re: [Caml-list] Annotated trees
  2010-06-07  5:09 ` [Caml-list] " Martin Jambon
  2010-06-07  5:15   ` Martin Jambon
@ 2010-06-07  9:22   ` Daniel Bünzli
  2010-06-07 12:01     ` blue storm
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Bünzli @ 2010-06-07  9:22 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list List

> 1. Pattern-matching is more readable because the most important piece of
> information comes first:
>
>   `Leaf (_, s) -> ...
>  | `Node (_, l) -> ...
>
> instead of:
>
>   (_, `Leaf s) -> ...
>  | (_, `Node l) -> ...

I'm not sure I agree on this one since in the second case the
parentheses are not needed. And if you put the annotation in the
second component (as you suggest your second message) then we are back
to most important first and without parentheses :

let f = function
| `Leaf s, _ -> ...
| `Node l, _ -> ...

> 3. It is possible to not annotate certain kinds of nodes, or to have different
> types of annotations depending on the kind of node.

That's true and certainly a good argument for the second form if you
already know the type of your annotations and have such needs. However
if you want to keep that polymorphic (the annotation is a convenience
for the client) it results in a type variable per case which becomes
heavy.

> 4. The tuple version feels like there are 2 different definitions of a tree
> node, i.e. it is easy to get confused about whether an annotated node (the
> pair) or an non-annotated node (second member of the pair) is expected in one
> given place.

I can understand that. It's true that it may make you think that you
are able to get a non-annotated value by just dropping the first
component. On the other hand the rule is quite simple, you always work
with pairs, period.

> 5. I got this habit and therefore I won't change my mind and reject all
> rational arguments against it ;-)

Yes, that's mainly a discussion about taste.

Thanks for your comments,

Daniel


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

* Re: [Caml-list] Annotated trees
  2010-06-07  9:22   ` Daniel Bünzli
@ 2010-06-07 12:01     ` blue storm
  0 siblings, 0 replies; 6+ messages in thread
From: blue storm @ 2010-06-07 12:01 UTC (permalink / raw)
  To: caml-list List

On Mon, Jun 7, 2010 at 11:22 AM, Daniel Bünzli
<daniel.buenzli@erratique.ch> wrote:
> That's true and certainly a good argument for the second form if you
> already know the type of your annotations and have such needs. However
> if you want to keep that polymorphic (the annotation is a convenience
> for the client) it results in a type variable per case which becomes
> heavy.

You probably know that already but there is a nice use of object types
as "type-level records" that make that kind of "lots of type
variables" situations tractable :

type 'annot tree =
 | Leaf of 'leaf_annot * string
 | Node of 'node_annot * 'annot tree list
constraint 'annot =
 < leaf : 'leaf_annot;
   node : 'node_annot >


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

end of thread, other threads:[~2010-06-07 12:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-06 15:26 Annotated trees Daniel Bünzli
2010-06-07  5:09 ` [Caml-list] " Martin Jambon
2010-06-07  5:15   ` Martin Jambon
2010-06-07  9:22   ` Daniel Bünzli
2010-06-07 12:01     ` blue storm
2010-06-07  6:45 ` Jacques Garrigue

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