This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

[Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2004-09-27 (09:41) From: Jacques GARRIGUE Subject: Re: [Caml-list] C++ STL and template features compared with OCaml parametric polymorphism and OO features
```From: skaller <skaller@users.sourceforge.net>
> On Mon, 2004-09-27 at 11:59, Jon Harrop wrote:
>
> >  In practice, I'm all for
> > aggressive factoring but I can't see what we're factoring here...
>
> Sure you can. Consider:
>
> type 'a F = A | B of 'a | C of 'a * 'a * int
>
> Can you write a map function for this? Of course you
>
> type 'a F = A of int | C of 'a * int | X of 'a F * 'a
>
> Yes, of course you can!
>
> Its obvious how to do it!
>
> for writing map functions. But this pattern cannot
> be encoded in Ocaml as an HOF:
>
> map: 'a 'F -> ('a -> 'b) -> 'b 'F
>
> You can do this provided you have only types built
> from variants, tuples, and recursion (no function types).
>
> In fact you could probably write this function
> in camlp4 from the definition of each type
> for which you need a map.

Not exactly: what you demonstrated here is that you can automatically
build a map function for any type definition with one (or more) type
parameter. And the solution is more or less unique because you
required this map to be polymorphic in the parameter.

I often need to define map for types with no parameters. In that case
the problems is more complex, because there are many possible
definitions for map. You need to understand the semantics of the data
structure to choose.

And most types with parameters have small definitions, so defining map
by hand is not that bad...
(Yes, I know that there is a nice programming pattern involving big
types with parameters, but you cannot do this with normal sum types :-)

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

```