caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* wrapping parameterized types
@ 2007-05-03 22:31 Christopher L Conway
  2007-05-03 23:16 ` [Caml-list] " Chris King
  0 siblings, 1 reply; 9+ messages in thread
From: Christopher L Conway @ 2007-05-03 22:31 UTC (permalink / raw)
  To: caml-list

I recently ran across a rather surprising behavior in the OCaml type
system. I hope somebody may be able to advise me how to work around it
(or at least explain to me why things are the way they are!).

Suppose I want to pass around a restricted, un-parameterized list type:

# type mylist = Intlist of int list | Strlist of string list ;;
type mylist = Intlist of int list | Strlist of string list

Now I end up doing a lot of tedious case splits, like:

# let length_of_mylist = function Intlist x -> List.length x | Strlist
x -> List.length x ;;
val length_of_mylist : mylist -> int = <fun>

What one would like is something like "app_to_mylist : ('a list -> 'b)
-> mylist -> 'b" so one could write "app_to_mylist List.length", but:

# let app_to_mylist f = function Intlist x -> f x | Strlist x -> f x ;;
Characters 65-66:
  let app_to_mylist f = function Intlist x -> f x | Strlist x -> f x ;;
                                                                   ^
This expression has type string list but is here used with type int list

The solution I've tripped over is to pass in separate functions for
the different cases, and then (only somewhat less tediously than
above) duplicate the same function at the call site:

# let app_to_mylist (fi,fs) = function Intlist x -> fi x | Strlist x -> fs x ;;
val app_to_mylist : (int list -> 'a) * (string list -> 'a) -> mylist -> 'a =
  <fun>
# let length_of_mylist = app_to_mylist (List.length, List.length);;
val length_of_mylist : mylist -> int = <fun>

I can't help but feel there is a more elegant way to handle this,
perhaps drawing in objects or functors. Any suggestions?

This question arises from an attempt to use the APRON library, which
provides types parameterized by numerical domain, alongside other
numerical tools in a uniform way---the APRON interface deals with the
parameterized types generically, but I need to hoist the abstraction
up one layer and deal with both APRON and non-APRON types generically.
The details are far too ugly to wade into here...

Thanks,
Chris


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

end of thread, other threads:[~2007-05-04 15:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-03 22:31 wrapping parameterized types Christopher L Conway
2007-05-03 23:16 ` [Caml-list] " Chris King
2007-05-04  1:10   ` skaller
2007-05-04  8:13     ` Dirk Thierbach
2007-05-04 11:47     ` rossberg
2007-05-04 12:13       ` Andrej Bauer
2007-05-04 13:34         ` Dirk Thierbach
2007-05-04 15:58           ` Christopher L Conway
2007-05-04  1:58   ` Christopher L Conway

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