Sun, 7 May 2000 16:13:46 +0400 (MSD), S.D.Mechveliani <[EMAIL PROTECTED]> pisze:
> Also what do you do with
> class Foo a where weightOfType :: Int
> ?
In this case one solution is to have a sample argument, because Haskell
does not provide more convenient way of parametrizing values by a type
that is not a part of the type of the value.
There exists an elegant solution, but it's a bit less convenient to use:
newtype Const a b = Const a
class Foo a where weightOfType :: Const Int a
I'm not convinced whether it would be a good thing or not. Maybe it
should wait for more complete support for naming lambda-bound type
variables, i.e. pattern type signatures and result type signatures
(GHC and Hugs have them, but introducing variables in result type
signatures does not work in GHC yet). It would make it more convenient,
while from the beginning it clearly describes the intent and provide
everything needed for easy optimization.
> What we have for the *variable dimension* n ?
Haskell does have polymorphic recursion, and I hope local universal
quantification will get into Haskell2. Thus variable length vectors
can be expressed in a statically typed style, when the type alone
determines the domain:
data Vec0 a = Vec0
data Vec v a = Vec a (v a)
class Vector v where
listToVec :: [a] -> v a
vecToList :: v a -> [a]
dim :: v a -> Int -- Sample argument, may be translated to Const.
zero :: HasZero a => v a
instance Vector Vec0 where
listToVec [] = Vec0
vecToList Vec0 = []
dim _ = 0
zero = Vec0
instance Vector v => Vector (Vec v) where
listToVec (x:xs) = Vec x (listToVec xs)
vecToList (Vec x xs) = x : vecToList xs
dim ~(Vec x xs) = 1 + dim xs
zero = Vec zero zero
-- When the dimension used inside a computation disappears outside,
-- i.e. when it is determined at runtime, there must be a way of
-- introducing the context from the integer:
instVec:: Int -> (forall v. Vector v => v a -> x) -> x
instVec 0 f = f Vec0
instVec n f = instVec (n-1) (\v -> f (Vec undefined v))
-- Again Const can be used instead of the sample argument here.
-- The dimension is part of the context of a computation. Not of
-- individual values, as would be in typical OO languages.
--
__("< Marcin Kowalczyk * [EMAIL PROTECTED] http://qrczak.ids.net.pl/
\__/ GCS/M d- s+:-- a23 C+++$ UL++>++++$ P+++ L++>++++$ E-
^^ W++ N+++ o? K? w(---) O? M- V? PS-- PE++ Y? PGP+ t
QRCZAK 5? X- R tv-- b+>++ DI D- G+ e>++++ h! r--%>++ y-