Newsgroups: comp.lang.misc,comp.lang.prolog
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!nntp.sei.cmu.edu!news.psc.edu!hudson.lm.com!godot.cc.duq.edu!newsfeed.pitt.edu!gatech!newsfeed.internetmci.com!news.mel.aone.net.au!inferno.mpx.com.au!news.unimelb.EDU.AU!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Subject: Mercury's `univ' type
Message-ID: <9523615.20673@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU (CS-Usenet)
Organization: Computer Science, University of Melbourne, Australia
Date: Thu, 24 Aug 1995 05:10:07 GMT
Lines: 43
Xref: glinda.oz.cs.cmu.edu comp.lang.misc:22859 comp.lang.prolog:13766

In email, Breanndan O Nuallain wrote:
> 
> I've been thinking about this [Mercury's `univ' type]
> and I can't for the life of me see the advantage of it.
> Isn't this just a kludge to get some kind of higher
> order behaviour? Or does it prevent "undesirable" higher order actions
> while allowing "desirable" ones, such as generic sorting? 
> 
> Curiously,       ,
>           Breanndan
> 
> PS Feel free to reply to this on comp.lang.prolog if you think it
>    worthwhile, i.e. not a dumb question :-)

The main use of `univ' is for heterogeneous collections, i.e. collections
of things which are not all the same type.  With parametric polymorphism,
you can only have homogenous collections; in a collection such as `list(T)',
all of the elements of the list must have the same type.  Usually
that is no problem, in fact most of time it is exactly what you want.
If you want to put elements of different types into a list, you
can create a new type which is a discriminated union of the two
types.  For example, if you want to put apples and oranges in the
same list, you create a type

	:- type fruit ---> apple(apple) ; orange(orange).

In certain situations, however, you may not know in advance
the full set of types that you will insert in a collection.
`univ' is useful for this fairly rare case when you want
a union of an unbounded set of types.

Although `univ' is needed only rarely, when you do need it is can be
very useful; if you need it but don't have it, the strong type system
will feel very restrictive.  For example, of the 60000 lines of code in
the Mercury compiler, only two lines use `univ'.  But if `univ' did not
exist, a substantial amount (about 1000 lines) of code would need to
be modified.

-- 
Fergus Henderson              | Designing grand concepts is fun;
fjh@cs.mu.oz.au               | finding nitty little bugs is just work.
http://www.cs.mu.oz.au/~fjh   | -- Brooks, in "The Mythical Man-Month".
PGP key fingerprint: 00 D7 A2 27 65 09 B6 AC  8B 3E 0F 01 E7 5D C4 3F
