Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] functors with style?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Marcin 'Qrczak' Kowalczyk <qrczak@k...>
Subject: Re: [Caml-list] functors with style?
Mon, 19 Nov 2001 14:10:25 -0800, Shivkumar Chandrasekaran <shiv@ece.ucsb.edu> pisze:

> For example, in Clean (which has type classes not functors of course), 
> instants of basic types be prevented from using dictionary passing. Not 
> sure what happens in haskell though...

GHC tries to specialize code which uses overloaded functions during
optimization.

Generally dictionaries are optimized out when the type is statically
known *or* the overloaded function is small enough to be inlined or
explicitly marked as inline (so they will be optimized out at call
site if types are known there).

You can also request generation of specializations of functions or
instances for particular types:

{-# SPECIALISE maximum :: [Int] -> Int #-}
{-# SPECIALISE minimum :: [Int] -> Int #-}
maximum, minimum        :: (Ord a) => [a] -> a
maximum []              =  errorEmptyList "maximum"
maximum xs              =  foldl1 max xs

minimum []              =  errorEmptyList "minimum"
minimum xs              =  foldl1 min xs

You can even substitute arbitrary definitions used for specialized
types; the compiler doesn't check if they have the same semantics.
They are used when the compiler statically knows the type (perhaps
after inlining at call site). For example:

fromIntegral :: (Integral a, Num b) => a -> b
fromIntegral = fromInteger . toInteger

{-# RULES
"fromIntegral/Word8->Int16"  fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
"fromIntegral/Int8->Int16"   fromIntegral = \(I8# x#) -> I16# x#
"fromIntegral/Int16->Int16"  fromIntegral = id :: Int16 -> Int16
"fromIntegral/a->Int16"      fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
"fromIntegral/Int16->a"      fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
  #-}

(RULES are even more powerful: the lhs doesn't have to be an identifier).

nhc and Hugs do less optimizations and I would guess they don't
optimize out class dictionaries at all. hbc did some optimizations
and had SPECIALISE pragmas too.

-- 
 __("<  Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/
 \__/
  ^^
QRCZAK

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr