[Haskell-cafe] Re: Polymorphic lists...

MR K P SCHUPKE k.schupke at imperial.ac.uk
Fri Mar 12 15:16:20 EST 2004

Just running through the implementation of some of the functions I mentioned,
with the newtyped tuple version. Thought the run-time indexed code looked neat
so I thought i'd post it:

First the definition:

newtype RNil = RNil ()
newtype RCons attribute relation = RCons (attribute,relation)

class Relation relation
instance Relation (RNil ())
instance Relation relation => Relation (RCons attribute relation)

emptyRM :: RNil
emptyRM = RNil ()

now the code:

class Relation r => IndexRM' r a where
   indexRM' :: r -> Int -> Maybe a
instance IndexRM' RNil a where
   indexRM' _ _ = Nothing
instance IndexRM' r a => IndexRM' (RCons a r) a where
   indexRM' (RCons (a,_)) 0 = Just a
   indexRM' (RCons (_,r)) n = indexRM' r (n-1)
instance IndexRM' r a => IndexRM' (RCons b r) a where
   indexRM' (RCons (_,_)) 0 = Nothing
   indexRM' (RCons (_,r)) n = indexRM' r (n-1)

This finds the Nth value where 'n' is a normal integer. The last block
repeats twice because one matches the case where the return type is the same, and
the other is for then the return type and indexed value differ. This is why the
return type is Maybe. With the compile time natural number lookup, the program
will fail to compile if the types differ, but with the runtime lookup the type
and the success cannot be determined (hence no functional dependancy) and returning
Maybe makes the most sense.


More information about the Haskell-Cafe mailing list