info-sather
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Co-/Contravariance (was: mandatory newbie musing)


From: Norbert Nemec
Subject: Co-/Contravariance (was: mandatory newbie musing)
Date: Mon, 2 Oct 2000 09:13:18 +0200
User-agent: Mutt/1.0.1i

On Sun, Oct 01, 2000 at 06:33:02PM -0700, Quinn Dunkan wrote:
> So does that mean haskell is invariant? ...

I don't really know haskell, but it seems like that.

> > Don't worry: after working with Sather for a while, you'll understand 
> > what it means without really thinking about it. (Maybe not in the terms 
> > of co- and contravariance, but you'll understand the concept behind it 
> > - it's just natural...)
> 
> Well, I do agree that if your definition of "a is more general than b" means
> "a can do whatever b can" then "the interface of a should be more general than
> the interface of b" follows naturally.  But that sounds like co-variance,
> since a -> b and the interfaces of a -> b are varying in the same direction,
> they both get more general.
> 
> Of course, then you have to define what "more general" maps to in terms of
> types... so maybe contravariance says "a subtype is more general" while
> covariance says "a subtype is more specific".
> 
> But wait, now they're still both varying in the same direction, just the
> inheritance trees are upside down.  I thought I understood this stuff...
> 
> Errgg... now my head *really* hurts.
> 
> My intuition tells me that any attempt to organize types into a DAG of
> generality vs. specificity and maintain correctness is inherently doomed
> because there is nothing in the real world from preventing as-a relationships
> from going in circles.
> 
> If so, the mathematicians must have given up on it a long time ago...

I guess, you are trying to grasp it at the wrong point: co- and 
contravariance is about input and output of a class in the widest sense:

First, $ANIMALs and $HERBIVOREs are a bad example. $HERBIVORE should 
subtype from $ANIMAL, but there simply is no universal "feed" procedure 
for animals as long as the universal food has not been invented.

Well, that gives me an idea: You can feed water to any animal...

------------------------------
class $BEVERAGE is ... end;
class WATER < $BEVERAGE is ... end;
class COFFEE < $BEVERAGE is ... end;

class $MOVE is ... end;
class WALK < $MOVE is ... end;
class FLY < $MOVE is ... end;

class $ANIMAL is
    drink(liquid: WATER) is ... end;
    move: $MOVE is ... end;
end;

class $HUMAN < $ANIMAL is
    drink(liquid: $BEVERAGE) is ... end;
    move: WALK is ... end;
end;
-----------------------------

That should work to show both: the liquid works contravariant (a 
subtype can take everything the supertype could *and more*) the move 
works covariant (a subtype might make narrower specifications about its 
output.)

Don't try to think about it too hard. It's just as nature works - only 
difficulty is to understand what a subtype really is. Something you can 
use in any situation where the supertype would do...

> > Correct: gcc optimization is buggy. I don't know where the bug is 
> > located and which versions show it. Just today, I worked ~2 two hours 
> > on a bug before I realized that it was caused by just the same thing 
> > again. -O is the best workaround known...
> 
> Don't you just hate that...  the first thing I do with a mysterious segfault
> is compile with -g.  And then it goes away!  Arrrghhh.  Maybe if I decide life
> has gotten too sane one weekend I'll try to track down which generated source
> file it lives in, if it indeed lives in one file, and not the magic
> combination of them all (more likely).  And first go download the latest gcc
> snapshot and see if it's still there :)

All done yesterday in the evening. Looking at the Assembler code 
produced by gcc did it. I have a ~1k C-file sent in to bugs-gcc now...

> > It the issues are not covered there, go discuss it with Keith Hopper, 
> > the main author of the new library.
> 
> I'm looking now and I still have questions... does he read this list?  Keith,
> are you there? :)  If not, and if he's the *only* person working on the
> library, I'll just email him.

He is the head of the crew down there. Others are co-working but he is 
the one to contact. Just email him.

Ciao,
Nobbi

-- 
-- ______________________________________________________
-- JESUS CHRIST IS LORD!
--          To Him, even that machine here has to obey...
--
-- _________________________________Norbert "Nobbi" Nemec
-- Hindenburgstr. 44  ...  D-91054 Erlangen  ...  Germany
-- eMail: <address@hidden>   Tel: +49-(0)-9131-204180



reply via email to

[Prev in Thread] Current Thread [Next in Thread]